STXXL  1.4.0
 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 
27 
29 
30 //! \addtogroup mnglayer
31 //! \{
32 
33 //! Encapsulate the configuration of one "disk". The disk is actually a file
34 //! I/O object which block_manager uses to read/write blocks.
36 {
37 public:
38  //! \name Basic Disk Configuration Parameters
39  //! \{
40 
41  //! the file path used by the io implementation
42  std::string path;
43 
44  //! file size to initially allocate
46 
47  //! io implementation to access file
48  std::string io_impl;
49 
50  //! \}
51 
52 public:
53  //! default constructor
54  disk_config();
55 
56  //! initializing constructor, also parses fileio parameter
57  disk_config(const std::string& path, uint64 size, const std::string& fileio);
58 
59  //! initializing constructor, parse full line as in config files
60  disk_config(const std::string& line);
61 
62  //! parse a disk=<path>,<size>,<fileio> options line into disk_config,
63  //! throws std::runtime_error on parse errors.
64  void parse_line(const std::string& line);
65 
66  //! parse the "io_impl" parameter into the optional parameter fields.
67  void parse_fileio();
68 
69  //! return formatted fileio name and optional configuration parameters
70  std::string fileio_string() const;
71 
72 public:
73  //! \name Optional Disk / File I/O Implementation Parameters
74  //! \{
75 
76  //! autogrow file if more disk space is needed, automatically set if size == 0.
77  bool autogrow;
78 
79  //! delete file on program exit (default for autoconfigurated files)
81 
82  //! tristate variable: direct=0 -> force direct OFF, direct=1 -> try direct
83  //! ON, if fails print warning and open without direct, direct=2 -> force
84  //! direct ON, fail if unavailable.
85  enum direct_type { DIRECT_OFF = 0, DIRECT_TRY = 1, DIRECT_ON = 2 } direct;
86 
87  //! marks flash drives (configuration entries with flash= instead of disk=)
88  bool flash;
89 
90  //! select request queue for disk. Use different queues for files on
91  //! different disks. queue=-1 -> default queue (one for each disk).
92  int queue;
93 
94  //! turned on by syscall fileio when the path points to a raw block device
95  bool raw_device;
96 
97  //! unlink file immediately after opening (available on most Unix)
99 
100  //! \}
101 };
102 
103 //! Access point to disks properties. Since 1.4.0: no config files are read
104 //! automatically!
105 //! \remarks is a singleton
106 class config : public singleton<config>
107 {
108  friend class singleton<config>;
109 
110  //! typedef of list of configured disks
111  typedef std::vector<disk_config> disk_list_type;
112 
113  //! list of configured disks
115 
116  //! In disks_list, flash devices come after all regular disks
117  unsigned first_flash;
118 
119  //! Finished initializing config
121 
122  //! Constructor: this must be inlined to print the header version
123  //! string.
124  inline config()
125  : is_initialized(false)
126  {
127  logger::get_instance();
130  }
131 
132  //! deletes autogrow files
133  ~config();
134 
135  //! Search several places for a config file.
136  void find_config();
137 
138  //! If disk list is empty, then search different locations for a disk
139  //! configuration file, or load a default config if everything fails.
140  void initialize();
141 
142 public:
143  //! \name Initialization Functions
144  //! \{
145 
146  //! Check that initialize() was called.
147  //! \note This function need not be called by the user, block_manager will
148  //! always call it.
150  {
151  if (!is_initialized) initialize();
152  }
153 
154  //! Load disk configuration file.
155  void load_config_file(const std::string& config_path);
156 
157  //! Load default configuration.
158  void load_default_config();
159 
160  //! Add a disk to the configuration list.
161  //!
162  //! \warning This function should only be used during initialization, as it
163  //! has no effect after construction of block_manager.
164  inline config & add_disk(const disk_config& cfg)
165  {
166  disks_list.push_back(cfg);
167  return *this;
168  }
169 
170  //! \}
171 
172 public:
173  //! \name Query Functions
174  //! \{
175 
176  //! Returns number of disks available to user.
177  //! \return number of disks
178  inline size_t disks_number()
179  {
180  check_initialized();
181  return disks_list.size();
182  }
183 
184  //! Returns contiguous range of regular disks w/o flash devices in the array of all disks.
185  //! \return range [begin, end) of regular disk indices
186  inline std::pair<unsigned, unsigned> regular_disk_range() const
187  {
188  assert(is_initialized);
189  return std::pair<unsigned, unsigned>(0, first_flash);
190  }
191 
192  //! Returns contiguous range of flash devices in the array of all disks.
193  //! \return range [begin, end) of flash device indices
194  inline std::pair<unsigned, unsigned> flash_range() const
195  {
196  assert(is_initialized);
197  return std::pair<unsigned, unsigned>(first_flash, (unsigned)disks_list.size());
198  }
199 
200  //! Returns mutable disk_config structure for additional disk parameters
201  inline disk_config & disk(size_t disk)
202  {
203  check_initialized();
204  return disks_list[disk];
205  }
206 
207  //! Returns constant disk_config structure for additional disk parameters
208  inline const disk_config & disk(size_t disk) const
209  {
210  assert(is_initialized);
211  return disks_list[disk];
212  }
213 
214  //! Returns path of disks.
215  //! \param disk disk's identifier
216  //! \return string that contains the disk's path name
217  inline const std::string & disk_path(size_t disk) const
218  {
219  assert(is_initialized);
220  return disks_list[disk].path;
221  }
222 
223  //! Returns disk size.
224  //! \param disk disk's identifier
225  //! \return disk size in bytes
226  inline stxxl::uint64 disk_size(size_t disk) const
227  {
228  assert(is_initialized);
229  return disks_list[disk].size;
230  }
231 
232  //! Returns name of I/O implementation of particular disk.
233  //! \param disk disk's identifier
234  inline const std::string & disk_io_impl(size_t disk) const
235  {
236  assert(is_initialized);
237  return disks_list[disk].io_impl;
238  }
239 
240  //! Returns the total size over all disks
241  uint64 total_size() const;
242 
243  //! \}
244 };
245 
246 //! \}
247 
249 
250 #endif // !STXXL_MNG_CONFIG_HEADER
251 // 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:92
unsigned long long int uint64
Definition: types.h:41
direct_type
tristate variable: direct=0 -&gt; force direct OFF, direct=1 -&gt; try direct ON, if fails print warning an...
Definition: config.h:85
disk_config & disk(size_t disk)
Returns mutable disk_config structure for additional disk parameters.
Definition: config.h:201
void check_initialized()
Check that initialize() was called.
Definition: config.h:149
unsigned first_flash
In disks_list, flash devices come after all regular disks.
Definition: config.h:117
std::vector< disk_config > disk_list_type
typedef of list of configured disks
Definition: config.h:111
bool unlink_on_open
unlink file immediately after opening (available on most Unix)
Definition: config.h:98
stxxl::uint64 disk_size(size_t disk) const
Returns disk size.
Definition: config.h:226
const disk_config & disk(size_t disk) const
Returns constant disk_config structure for additional disk parameters.
Definition: config.h:208
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:114
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:80
std::string path
the file path used by the io implementation
Definition: config.h:42
Encapsulate the configuration of one &quot;disk&quot;. The disk is actually a file I/O object which block_manag...
Definition: config.h:35
const std::string & disk_io_impl(size_t disk) const
Returns name of I/O implementation of particular disk.
Definition: config.h:234
size_t disks_number()
Returns number of disks available to user.
Definition: config.h:178
bool autogrow
autogrow file if more disk space is needed, automatically set if size == 0.
Definition: config.h:77
config & add_disk(const disk_config &cfg)
Add a disk to the configuration list.
Definition: config.h:164
const std::string & disk_path(size_t disk) const
Returns path of disks.
Definition: config.h:217
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
bool is_initialized
Finished initializing config.
Definition: config.h:120
Access point to disks properties. Since 1.4.0: no config files are read automatically! ...
Definition: config.h:106
#define STXXL_MSG(x)
Definition: verbose.h:72
config()
Constructor: this must be inlined to print the header version string.
Definition: config.h:124
bool raw_device
turned on by syscall fileio when the path points to a raw block device
Definition: config.h:95
std::pair< unsigned, unsigned > flash_range() const
Returns contiguous range of flash devices in the array of all disks.
Definition: config.h:194
bool flash
marks flash drives (configuration entries with flash= instead of disk=)
Definition: config.h:88
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:186
std::string io_impl
io implementation to access file
Definition: config.h:48
uint64 size
file size to initially allocate
Definition: config.h:45
#define STXXL_END_NAMESPACE
Definition: namespace.h:17