STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
config.h
Go to the documentation of this file.
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 <[email protected]>
7  * Copyright (C) 2008, 2009 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2013 Timo Bingmann <[email protected]>
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 
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.
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
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)
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)
102 
103  //! desired queue length for linuxaio_file and linuxaio_queue
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
121 
122  //! In disks_list, flash devices come after all regular disks
123  unsigned first_flash;
124 
125  //! Finished initializing config
127 
128  //! Constructor: this must be inlined to print the header version
129  //! string.
130  inline config()
131  : is_initialized(false)
132  {
133  logger::get_instance();
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.
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.
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
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
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
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
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
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
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
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
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 
274 
275 #endif // !STXXL_MNG_CONFIG_HEADER
276 // vim: et:ts=4:sw=4
int queue
select request queue for disk. Use different queues for files on different disks. queue=-1 -&gt; default...
Definition: config.h:91
unsigned long long int uint64
Definition: types.h:39
direct_type
tristate variable: direct=0 -&gt; force direct OFF, direct=1 -&gt; try direct ON, if fails print warning an...
Definition: config.h:84
disk_config & disk(size_t disk)
Returns mutable disk_config structure for additional disk parameters.
Definition: config.h:226
void check_initialized()
Check that initialize() was called.
Definition: config.h:155
unsigned first_flash
In disks_list, flash devices come after all regular disks.
Definition: config.h:123
std::vector< disk_config > disk_list_type
typedef of list of configured disks
Definition: config.h:117
unsigned int m_max_device_id
static counter for automatic physical device enumeration
Definition: config.h:183
bool unlink_on_open
unlink file immediately after opening (available on most Unix)
Definition: config.h:101
stxxl::uint64 disk_size(size_t disk) const
Returns disk size.
Definition: config.h:251
const disk_config & disk(size_t disk) const
Returns constant disk_config structure for additional disk parameters.
Definition: config.h:233
const char * get_version_string_long()
Return longer &quot;X.Y.Z (feature) (version)&quot; version string (of headers)
Definition: version.h:38
disk_list_type disks_list
list of configured disks
Definition: config.h:120
void print_library_version_mismatch()
Check and print mismatch between header and library versions.
Definition: version.h:88
bool delete_on_exit
delete file on program exit (default for autoconfigurated files)
Definition: config.h:79
std::string path
the file path used by the io implementation
Definition: config.h:41
Encapsulate the configuration of one &quot;disk&quot;. The disk is actually a file I/O object which block_manag...
Definition: config.h:34
const std::string & disk_io_impl(size_t disk) const
Returns name of I/O implementation of particular disk.
Definition: config.h:259
size_t disks_number()
Returns number of disks available to user.
Definition: config.h:203
bool autogrow
autogrow file if more disk space is needed, automatically set if size == 0.
Definition: config.h:76
config & add_disk(const disk_config &cfg)
Add a disk to the configuration list.
Definition: config.h:170
const std::string & disk_path(size_t disk) const
Returns path of disks.
Definition: config.h:242
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
unsigned int device_id
the selected physical device id (e.g. for calculating prefetching sequences). If -1 then the device i...
Definition: config.h:95
bool is_initialized
Finished initializing config.
Definition: config.h:126
int queue_length
desired queue length for linuxaio_file and linuxaio_queue
Definition: config.h:104
Access point to disks properties. Since 1.4.0: no config files are read automatically! ...
Definition: config.h:112
#define STXXL_MSG(x)
Definition: verbose.h:73
config()
Constructor: this must be inlined to print the header version string.
Definition: config.h:130
bool raw_device
turned on by syscall fileio when the path points to a raw block device
Definition: config.h:98
std::pair< unsigned, unsigned > flash_range() const
Returns contiguous range of flash devices in the array of all disks.
Definition: config.h:219
bool flash
marks flash drives (configuration entries with flash= instead of disk=)
Definition: config.h:87
std::pair< unsigned, unsigned > regular_disk_range() const
Returns contiguous range of regular disks w/o flash devices in the array of all disks.
Definition: config.h:211
std::string io_impl
io implementation to access file
Definition: config.h:47
uint64 size
file size to initially allocate
Definition: config.h:44
#define STXXL_END_NAMESPACE
Definition: namespace.h:17