1 /***************************************************************************
2  *  include/stxxl/bits/mng/config.h
3  *
4  *  Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  *  Copyright (C) 2002-2005 Roman Dementiev <dementiev@mpi-sb.mpg.de>
7  *  Copyright (C) 2008, 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
8  *  Copyright (C) 2013 Timo Bingmann <tb@panthema.net>
9  *
10  *  Distributed under the Boost Software License, Version 1.0.
11  *  (See accompanying file LICENSE_1_0.txt or copy at
12  *  http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_MNG_CONFIG_HEADER
16 #define STXXL_MNG_CONFIG_HEADER
17 
18 #include <vector>
19 #include <string>
20 #include <cstdlib>
21 #include <cassert>
22 
23 #include <stxxl/version.h>
24 #include <stxxl/bits/singleton.h>
25 #include <stxxl/bits/common/log.h>
26 
27 STXXL_BEGIN_NAMESPACE
28 
29 //! \addtogroup mnglayer
30 //! \{
31 
32 //! Encapsulate the configuration of one "disk". The disk is actually a file
33 //! I/O object which block_manager uses to read/write blocks.
34 class disk_config
35 {
36 public:
37     //! \name Basic Disk Configuration Parameters
38     //! \{
39 
40     //! the file path used by the io implementation
41     std::string path;
42 
43     //! file size to initially allocate
44     uint64 size;
45 
46     //! io implementation to access file
47     std::string io_impl;
48 
49     //! \}
50 
51 public:
52     //! default constructor
53     disk_config();
54 
55     //! initializing constructor, also parses fileio parameter
56     disk_config(const std::string& path, uint64 size, const std::string& fileio);
57 
58     //! initializing constructor, parse full line as in config files
59     disk_config(const std::string& line);
60 
61     //! parse a disk=\<path>,\<size>,\<fileio> options line into disk_config,
62     //! throws std::runtime_error on parse errors.
63     void parse_line(const std::string& line);
64 
65     //! parse the "io_impl" parameter into the optional parameter fields.
66     void parse_fileio();
67 
68     //! return formatted fileio name and optional configuration parameters
69     std::string fileio_string() const;
70 
71 public:
72     //! \name Optional Disk / File I/O Implementation Parameters
73     //! \{
74 
75     //! autogrow file if more disk space is needed, automatically set if size == 0.
76     bool autogrow;
77 
78     //! delete file on program exit (default for autoconfigurated files)
79     bool delete_on_exit;
80 
81     //! tristate variable: direct=0 -> force direct OFF, direct=1 -> try direct
82     //! ON, if fails print warning and open without direct, direct=2 -> force
83     //! direct ON, fail if unavailable.
84     enum direct_type { DIRECT_OFF = 0, DIRECT_TRY = 1, DIRECT_ON = 2 } direct;
85 
86     //! marks flash drives (configuration entries with flash= instead of disk=)
87     bool flash;
88 
89     //! select request queue for disk. Use different queues for files on
90     //! different disks. queue=-1 -> default queue (one for each disk).
91     int queue;
92 
93     //! the selected physical device id (e.g. for calculating prefetching
94     //! sequences). If -1 then the device id is chosen automatically.
95     unsigned int device_id;
96 
97     //! turned on by syscall fileio when the path points to a raw block device
98     bool raw_device;
99 
100     //! unlink file immediately after opening (available on most Unix)
101     bool unlink_on_open;
102 
103     //! desired queue length for linuxaio_file and linuxaio_queue
104     int queue_length;
105 
106     //! \}
107 };
108 
109 //! Access point to disks properties. Since 1.4.0: no config files are read
110 //! automatically!
111 //! \remarks is a singleton
112 class config : public singleton<config>
113 {
114     friend class singleton<config>;
115 
116     //! typedef of list of configured disks
117     typedef std::vector<disk_config> disk_list_type;
118 
119     //! list of configured disks
120     disk_list_type disks_list;
121 
122     //! In disks_list, flash devices come after all regular disks
123     unsigned first_flash;
124 
125     //! Finished initializing config
126     bool is_initialized;
127 
128     //! Constructor: this must be inlined to print the header version
129     //! string.
config()130     inline config()
131         : is_initialized(false)
132     {
133         logger::get_instance();
134         STXXL_MSG(get_version_string_long());
135         print_library_version_mismatch();
136     }
137 
138     //! deletes autogrow files
139     ~config();
140 
141     //! Search several places for a config file.
142     void find_config();
143 
144     //! If disk list is empty, then search different locations for a disk
145     //! configuration file, or load a default config if everything fails.
146     void initialize();
147 
148 public:
149     //! \name Initialization Functions
150     //! \{
151 
152     //! Check that initialize() was called.
153     //! \note This function need not be called by the user, block_manager will
154     //! always call it.
check_initialized()155     void check_initialized()
156     {
157         if (!is_initialized) initialize();
158     }
159 
160     //! Load disk configuration file.
161     void load_config_file(const std::string& config_path);
162 
163     //! Load default configuration.
164     void load_default_config();
165 
166     //! Add a disk to the configuration list.
167     //!
168     //! \warning This function should only be used during initialization, as it
169     //! has no effect after construction of block_manager.
add_disk(const disk_config & cfg)170     inline config & add_disk(const disk_config& cfg)
171     {
172         disks_list.push_back(cfg);
173         return *this;
174     }
175 
176     //! \}
177 
178 protected:
179     //! \name Automatic Disk Enumeration Functions
180     //! \{
181 
182     //! static counter for automatic physical device enumeration
183     unsigned int m_max_device_id;
184 
185 public:
186     //! Returns automatic physical device id counter
187     unsigned int get_max_device_id();
188 
189     //! Returns next automatic physical device id counter
190     unsigned int get_next_device_id();
191 
192     //! Update the automatic physical device id counter
193     void update_max_device_id(unsigned int devid);
194 
195     //! \}
196 
197 public:
198     //! \name Query Functions
199     //! \{
200 
201     //! Returns number of disks available to user.
202     //! \return number of disks
disks_number()203     inline size_t disks_number()
204     {
205         check_initialized();
206         return disks_list.size();
207     }
208 
209     //! Returns contiguous range of regular disks w/o flash devices in the array of all disks.
210     //! \return range [begin, end) of regular disk indices
regular_disk_range()211     inline std::pair<unsigned, unsigned> regular_disk_range() const
212     {
213         assert(is_initialized);
214         return std::pair<unsigned, unsigned>(0, first_flash);
215     }
216 
217     //! Returns contiguous range of flash devices in the array of all disks.
218     //! \return range [begin, end) of flash device indices
flash_range()219     inline std::pair<unsigned, unsigned> flash_range() const
220     {
221         assert(is_initialized);
222         return std::pair<unsigned, unsigned>(first_flash, (unsigned)disks_list.size());
223     }
224 
225     //! Returns mutable disk_config structure for additional disk parameters
disk(size_t disk)226     inline disk_config & disk(size_t disk)
227     {
228         check_initialized();
229         return disks_list[disk];
230     }
231 
232     //! Returns constant disk_config structure for additional disk parameters
disk(size_t disk)233     inline const disk_config & disk(size_t disk) const
234     {
235         assert(is_initialized);
236         return disks_list[disk];
237     }
238 
239     //! Returns path of disks.
240     //! \param disk disk's identifier
241     //! \return string that contains the disk's path name
disk_path(size_t disk)242     inline const std::string & disk_path(size_t disk) const
243     {
244         assert(is_initialized);
245         return disks_list[disk].path;
246     }
247 
248     //! Returns disk size.
249     //! \param disk disk's identifier
250     //! \return disk size in bytes
disk_size(size_t disk)251     inline stxxl::uint64 disk_size(size_t disk) const
252     {
253         assert(is_initialized);
254         return disks_list[disk].size;
255     }
256 
257     //! Returns name of I/O implementation of particular disk.
258     //! \param disk disk's identifier
disk_io_impl(size_t disk)259     inline const std::string & disk_io_impl(size_t disk) const
260     {
261         assert(is_initialized);
262         return disks_list[disk].io_impl;
263     }
264 
265     //! Returns the total size over all disks
266     uint64 total_size() const;
267 
268     //! \}
269 };
270 
271 //! \}
272 
273 STXXL_END_NAMESPACE
274 
275 #endif // !STXXL_MNG_CONFIG_HEADER
276 // vim: et:ts=4:sw=4
277