STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
block_manager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/mng/block_manager.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2004 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008, 2010 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 
17 #include <stxxl/bits/io/file.h>
19 #include <stxxl/bits/mng/config.h>
21 #include <stxxl/bits/namespace.h>
22 #include <stxxl/bits/verbose.h>
23 
24 #include <cstddef>
25 #include <fstream>
26 #include <string>
27 
29 
30 class io_error;
31 
32 block_manager::block_manager()
33 {
34  config* config = config::get_instance();
35 
36  // initialize config (may read config files now)
37  config->check_initialized();
38 
39  // allocate disk_allocators
40  ndisks = config->disks_number();
41  disk_allocators = new disk_allocator*[ndisks];
42  disk_files = new file*[ndisks];
43 
44  uint64 total_size = 0;
45 
46  for (unsigned i = 0; i < ndisks; ++i)
47  {
48  disk_config& cfg = config->disk(i);
49 
50  // assign queues in order of disks.
51  if (cfg.queue == file::DEFAULT_QUEUE)
52  cfg.queue = i;
53 
54  try
55  {
56  disk_files[i] = create_file(cfg, file::CREAT | file::RDWR, i);
57 
58  STXXL_MSG("Disk '" << cfg.path << "' is allocated, space: " <<
59  (cfg.size) / (1024 * 1024) <<
60  " MiB, I/O implementation: " << cfg.fileio_string());
61  }
62  catch (io_error&)
63  {
64  STXXL_MSG("Error allocating disk '" << cfg.path << "', space: " <<
65  (cfg.size) / (1024 * 1024) <<
66  " MiB, I/O implementation: " << cfg.fileio_string());
67  throw;
68  }
69 
70  total_size += cfg.size;
71 
72  disk_allocators[i] = new disk_allocator(disk_files[i], cfg);
73  }
74 
75  if (ndisks > 1)
76  {
77  STXXL_MSG("In total " << ndisks << " disks are allocated, space: " <<
78  (total_size / (1024 * 1024)) <<
79  " MiB");
80  }
81 
82 #if STXXL_MNG_COUNT_ALLOCATION
83  m_current_allocation = 0;
84  m_total_allocation = 0;
85  m_maximum_allocation = 0;
86 #endif // STXXL_MNG_COUNT_ALLOCATION
87 }
88 
89 block_manager::~block_manager()
90 {
91  STXXL_VERBOSE1("Block manager destructor");
92  for (size_t i = ndisks; i > 0; )
93  {
94  --i;
95  delete disk_allocators[i];
96  delete disk_files[i];
97  }
98  delete[] disk_allocators;
99  delete[] disk_files;
100 }
101 
102 uint64 block_manager::get_total_bytes() const
103 {
104  uint64 total = 0;
105 
106  for (unsigned i = 0; i < ndisks; ++i)
107  total += disk_allocators[i]->get_total_bytes();
108 
109  return total;
110 }
111 
112 uint64 block_manager::get_free_bytes() const
113 {
114  uint64 total = 0;
115 
116  for (unsigned i = 0; i < ndisks; ++i)
117  total += disk_allocators[i]->get_free_bytes();
118 
119  return total;
120 }
121 
123 // vim: et:ts=4:sw=4
file * create_file(const std::string &io_impl, const std::string &filename, int options, int physical_device_id=file::DEFAULT_QUEUE, int disk_allocator_id=file::NO_ALLOCATOR)
create fileio object from io_impl string and a few parameters
Definition: create_file.cpp:27
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
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
Defines interface of file.
Definition: file.h:56
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
size_t disks_number()
Returns number of disks available to user.
Definition: config.h:203
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
#define STXXL_VERBOSE1(x)
Definition: verbose.h:99
std::string fileio_string() const
return formatted fileio name and optional configuration parameters
Definition: config.cpp:426
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
uint64 size
file size to initially allocate
Definition: config.h:44
#define STXXL_END_NAMESPACE
Definition: namespace.h:17