Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
config.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/mng/config.hpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
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 FOXXLL_MNG_CONFIG_HEADER
16 #define FOXXLL_MNG_CONFIG_HEADER
17 
18 #include <cassert>
19 #include <cstdlib>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include <tlx/logger.hpp>
25 
26 #include <foxxll/singleton.hpp>
27 #include <foxxll/version.hpp>
28 
29 namespace foxxll {
30 
31 //! \addtogroup foxxll_mnglayer
32 //! \{
33 
34 //! Encapsulate the configuration of one "disk". The disk is actually a file
35 //! I/O object which block_manager uses to read/write blocks.
37 {
38 public:
39  //! \name Basic Disk Configuration Parameters
40  //! \{
41 
42  //! the file path used by the io implementation
44 
45  //! file size to initially allocate
47 
48  //! io implementation to access file
50 
51  //! \}
52 
53 public:
54  //! default constructor
55  disk_config();
56 
57  //! initializing constructor, also parses fileio parameter
59 
60  //! initializing constructor, parse full line as in config files
61  explicit disk_config(const std::string& line);
62 
63  //! parse a disk=<path>,<size>,<fileio> options line into disk_config,
64  //! throws std::runtime_error on parse errors.
65  void parse_line(const std::string& line);
66 
67  //! parse the "io_impl" parameter into the optional parameter fields.
68  void parse_fileio();
69 
70  //! return formatted fileio name and optional configuration parameters
71  std::string fileio_string() const;
72 
73 public:
74  //! \name Optional Disk / File I/O Implementation Parameters
75  //! \{
76 
77  //! autogrow file if more disk space is needed, automatically set if size == 0.
78  bool autogrow;
79 
80  //! delete file on program exit (default for autoconfigurated files)
82 
83  //! tristate variable: direct=0 -> force direct OFF, direct=1 -> try direct
84  //! ON, if fails print warning and open without direct, direct=2 -> force
85  //! direct ON, fail if unavailable.
87 
88  //! marks flash drives (configuration entries with flash= instead of disk=)
89  bool flash;
90 
91  //! select request queue for disk. Use different queues for files on
92  //! different disks. queue=-1 -> default queue (one for each disk).
93  int queue;
94 
95  //! the selected physical device id (e.g. for calculating prefetching
96  //! sequences). If -1 then the device id is chosen automatically.
97  unsigned int device_id;
98 
99  //! turned on by syscall fileio when the path points to a raw block device
101 
102  //! unlink file immediately after opening (available on most Unix)
104 
105  //! desired queue length for linuxaio_file and linuxaio_queue
107 
108  //! \}
109 };
110 
111 //! Access point to disks properties. Since 1.4.0: no config files are read
112 //! automatically!
113 //! \remarks is a singleton
114 class config : public singleton<config>
115 {
116  friend class singleton<config>;
117 
118  //! typedef of list of configured disks
119  using disk_list_type = std::vector<disk_config>;
120 
121  //! list of configured disks
123 
124  //! In disks_list, flash devices come after all regular disks
125  unsigned first_flash;
126 
127  //! Finished initializing config
129 
130  //! Constructor: this must be inlined to print the header version
131  //! string.
132  config();
133 
134  //! deletes autogrow files
135  ~config();
136 
137  //! Search several places for a config file.
138  void find_config();
139 
140  //! If disk list is empty, then search different locations for a disk
141  //! configuration file, or load a default config if everything fails.
142  void initialize();
143 
144 public:
145  //! \name Initialization Functions
146  //! \{
147 
148  //! Check that initialize() was called.
149  //! \note This function need not be called by the user, block_manager will
150  //! always call it.
152  {
153  if (!is_initialized) initialize();
154  }
155 
156  //! Load disk configuration file.
157  void load_config_file(const std::string& config_path);
158 
159  //! Load default configuration.
160  void load_default_config();
161 
162  //! Add a disk to the configuration list.
163  //!
164  //! \warning This function should only be used during initialization, as it
165  //! has no effect after construction of block_manager.
166  config & add_disk(const disk_config& cfg);
167 
168  //! \}
169 
170 protected:
171  //! \name Automatic Disk Enumeration Functions
172  //! \{
173 
174  //! static counter for automatic physical device enumeration
175  unsigned int max_device_id_;
176 
177 public:
178  //! Returns automatic physical device id counter
179  unsigned int max_device_id();
180 
181  //! Returns next automatic physical device id counter
182  unsigned int next_device_id();
183 
184  //! Update the automatic physical device id counter
185  void update_max_device_id(unsigned int devid);
186 
187  //! \}
188 
189 public:
190  //! \name Query Functions
191  //! \{
192 
193  //! Returns number of disks available to user.
194  //! \return number of disks
195  size_t disks_number()
196  {
198  return disks_list.size();
199  }
200 
201  /*!
202  * Returns contiguous range of regular disks w/o flash devices in the array
203  * of all disks.
204  *
205  * \return range [begin, end) of regular disk indices
206  */
207  std::pair<unsigned, unsigned> regular_disk_range() const;
208 
209  //! Returns contiguous range of flash devices in the array of all disks.
210  //! \return range [begin, end) of flash device indices
211  std::pair<unsigned, unsigned> flash_range() const;
212 
213  //! Returns mutable disk_config structure for additional disk parameters
214  disk_config & disk(size_t disk);
215 
216  //! Returns constant disk_config structure for additional disk parameters
217  const disk_config & disk(size_t disk) const;
218 
219  //! Returns path of disks.
220  //! \param disk disk's identifier
221  //! \return string that contains the disk's path name
222  const std::string & disk_path(size_t disk) const;
223 
224  //! Returns default path of disk.
225  //! \return string that contains the disk's path name
227 
228  //! Returns disk size.
229  //! \param disk disk's identifier
230  //! \return disk size in bytes
231  external_size_type disk_size(size_t disk) const;
232 
233  //! Returns name of I/O implementation of particular disk.
234  //! \param disk disk's identifier
235  const std::string & disk_io_impl(size_t disk) const;
236 
237  //! Returns name of the default I/O implementation
239 
240  //! Returns the total size over all disks
242 
243  //! \}
244 };
245 
246 //! \}
247 
248 } // namespace foxxll
249 
250 #endif // !FOXXLL_MNG_CONFIG_HEADER
251 
252 /**************************************************************************/
external_size_type size
file size to initially allocate
Definition: config.hpp:46
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.cpp:202
std::vector< disk_config > disk_list_type
typedef of list of configured disks
Definition: config.hpp:119
void update_max_device_id(unsigned int devid)
Update the automatic physical device id counter.
Definition: config.cpp:196
unsigned int next_device_id()
Returns next automatic physical device id counter.
Definition: config.cpp:191
std::string path
the file path used by the io implementation
Definition: config.hpp:43
void initialize()
Definition: config.cpp:69
void check_initialized()
Definition: config.hpp:151
void parse_fileio()
parse the "io_impl" parameter into the optional parameter fields.
Definition: config.cpp:393
unsigned int max_device_id()
Returns automatic physical device id counter.
Definition: config.cpp:186
void load_config_file(const std::string &config_path)
Load disk configuration file.
Definition: config.cpp:143
bool unlink_on_open
unlink file immediately after opening (available on most Unix)
Definition: config.hpp:103
static std::string default_disk_path()
Definition: config.cpp:227
std::string io_impl
io implementation to access file
Definition: config.hpp:49
disk_config()
default constructor
Definition: config.cpp:280
static std::string default_disk_io_impl()
Returns name of the default I/O implementation.
Definition: config.cpp:254
unsigned int max_device_id_
static counter for automatic physical device enumeration
Definition: config.hpp:175
void parse_line(const std::string &line)
Definition: config.cpp:326
void find_config()
Search several places for a config file.
Definition: config.cpp:82
~config()
deletes autogrow files
Definition: config.cpp:56
disk_config & disk(size_t disk)
Returns mutable disk_config structure for additional disk parameters.
Definition: config.cpp:214
disk_list_type disks_list
list of configured disks
Definition: config.hpp:122
external_size_type total_size() const
Returns the total size over all disks.
Definition: config.cpp:263
bool flash
marks flash drives (configuration entries with flash= instead of disk=)
Definition: config.hpp:89
size_t disks_number()
Definition: config.hpp:195
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
bool is_initialized
Finished initializing config.
Definition: config.hpp:128
std::string fileio_string() const
return formatted fileio name and optional configuration parameters
Definition: config.cpp:537
const std::string & disk_path(size_t disk) const
Definition: config.cpp:221
const std::string & disk_io_impl(size_t disk) const
Definition: config.cpp:248
bool raw_device
turned on by syscall fileio when the path points to a raw block device
Definition: config.hpp:100
int queue_length
desired queue length for linuxaio_file and linuxaio_queue
Definition: config.hpp:106
external_size_type disk_size(size_t disk) const
Definition: config.cpp:242
config & add_disk(const disk_config &cfg)
Definition: config.cpp:180
bool delete_on_exit
delete file on program exit (default for autoconfigurated files)
Definition: config.hpp:81
void load_default_config()
Load default configuration.
Definition: config.cpp:130
bool autogrow
autogrow file if more disk space is needed, automatically set if size == 0.
Definition: config.hpp:78
unsigned int device_id
Definition: config.hpp:97
std::pair< unsigned, unsigned > flash_range() const
Definition: config.cpp:208
unsigned first_flash
In disks_list, flash devices come after all regular disks.
Definition: config.hpp:125
uint64_t external_size_type
Definition: types.hpp:27
enum foxxll::disk_config::direct_type direct