1 /***************************************************************************
2  *  foxxll/mng/config.hpp
3  *
4  *  Part of FOXXLL. See http://foxxll.org
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 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/core.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.
36 class disk_config
37 {
38 public:
39     //! \name Basic Disk Configuration Parameters
40     //! \{
41 
42     //! the file path used by the io implementation
43     std::string path;
44 
45     //! file size to initially allocate
46     external_size_type size;
47 
48     //! io implementation to access file
49     std::string io_impl;
50 
51     //! \}
52 
53 public:
54     //! default constructor
55     disk_config();
56 
57     //! initializing constructor, also parses fileio parameter
58     disk_config(const std::string& path, external_size_type size, const std::string& fileio);
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)
81     bool delete_on_exit;
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.
86     enum direct_type { DIRECT_OFF = 0, DIRECT_TRY = 1, DIRECT_ON = 2 } direct;
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
100     bool raw_device;
101 
102     //! unlink file immediately after opening (available on most Unix)
103     bool unlink_on_open;
104 
105     //! desired queue length for linuxaio_file and linuxaio_queue
106     int queue_length;
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
122     disk_list_type disks_list;
123 
124     //! In disks_list, flash devices come after all regular disks
125     unsigned first_flash;
126 
127     //! Finished initializing config
128     bool is_initialized;
129 
130 protected:
131     //! Constructor: this must be inlined to print the header version string.
132     config();
133 
134     //! deletes autogrow files
135     virtual ~config();
136 
137     //! Search several places for a config file.
138     virtual 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     virtual 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.
check_initialized()151     void check_initialized()
152     {
153         if (!is_initialized) initialize();
154     }
155 
156     //! Load disk configuration file.
157     virtual void load_config_file(const std::string& config_path);
158 
159     //! Load default configuration.
160     virtual 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
disks_number()195     size_t disks_number()
196     {
197         check_initialized();
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
226     virtual std::string default_disk_path();
227 
228     //! Returns name of the default I/O implementation
229     virtual std::string default_disk_io_impl();
230 
231     //! returns the name of the default config file prefix
232     virtual std::string default_config_file_name();
233 
234     //! Returns disk size.
235     //! \param disk disk's identifier
236     //! \return disk size in bytes
237     external_size_type disk_size(size_t disk) const;
238 
239     //! Returns name of I/O implementation of particular disk.
240     //! \param disk disk's identifier
241     const std::string & disk_io_impl(size_t disk) const;
242 
243     //! Returns the total size over all disks
244     external_size_type total_size() const;
245 
246     //! \}
247 };
248 
249 //! \}
250 
251 } // namespace foxxll
252 
253 #endif // !FOXXLL_MNG_CONFIG_HEADER
254 
255 /**************************************************************************/
256