Thrill  0.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  * foxxll/mng/block_manager.cpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
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 
15 #include <cstddef>
16 #include <string>
17 
18 #include <tlx/logger.hpp>
19 
21 
22 #include <foxxll/common/types.hpp>
25 #include <foxxll/io/file.hpp>
26 #include <foxxll/mng/config.hpp>
28 
29 namespace foxxll {
30 
31 class io_error;
32 
34 {
36 
37  // initialize config (may read config files now)
38  config->check_initialized();
39 
40  // allocate block_allocators_
41  ndisks_ = config->disks_number();
42  block_allocators_.resize(ndisks_);
43  disk_files_.resize(ndisks_);
44 
45  uint64_t total_size = 0;
46 
47  for (size_t i = 0; i < ndisks_; ++i)
48  {
49  disk_config& cfg = config->disk(i);
50 
51  // assign queues in order of disks.
52  if (cfg.queue == file::DEFAULT_QUEUE)
53  cfg.queue = i;
54 
55  try
56  {
58 
59  LOG1 << "Disk '" << cfg.path << "' is allocated, space: " <<
60  (cfg.size) / (1024 * 1024) <<
61  " MiB, I/O implementation: " << cfg.fileio_string();
62  }
63  catch (io_error&)
64  {
65  LOG1 << "Error allocating disk '" << cfg.path << "', space: " <<
66  (cfg.size) / (1024 * 1024) <<
67  " MiB, I/O implementation: " << cfg.fileio_string();
68  throw;
69  }
70 
71  total_size += cfg.size;
72 
73  // create queue for the file.
74  disk_queues::get_instance()->make_queue(disk_files_[i].get());
75 
76  block_allocators_[i] = new disk_block_allocator(disk_files_[i].get(), cfg);
77  }
78 
79  if (ndisks_ > 1)
80  {
81  LOG1 << "In total " << ndisks_ << " disks are allocated, space: " <<
82  (total_size / (1024 * 1024)) <<
83  " MiB";
84  }
85 }
86 
88 {
89  LOG << "Block manager destructor";
90  for (size_t i = ndisks_; i > 0; )
91  {
92  --i;
93  delete block_allocators_[i];
94  disk_files_[i].reset();
95  }
96 }
97 
99 {
100  std::unique_lock<std::mutex> lock(mutex_);
101 
102  uint64_t total = 0;
103 
104  for (size_t i = 0; i < ndisks_; ++i)
105  total += block_allocators_[i]->total_bytes();
106 
107  return total;
108 }
109 
111 {
112  std::unique_lock<std::mutex> lock(mutex_);
113 
114  uint64_t total = 0;
115 
116  for (size_t i = 0; i < ndisks_; ++i)
117  total += block_allocators_[i]->free_bytes();
118 
119  return total;
120 }
121 
123 {
124  std::unique_lock<std::mutex> lock(mutex_);
125  return total_allocation_;
126 }
127 
129 {
130  std::unique_lock<std::mutex> lock(mutex_);
131  return current_allocation_;
132 }
133 
135 {
136  std::unique_lock<std::mutex> lock(mutex_);
137  return maximum_allocation_;
138 }
139 
140 } // namespace foxxll
141 
142 /**************************************************************************/
external_size_type size
file size to initially allocate
Definition: config.hpp:46
std::mutex mutex_
protect internal data structures
std::string path
the file path used by the io implementation
Definition: config.hpp:43
uint64_t current_allocation() const
return currently allocated bytes
uint64_t current_allocation_
currently allocated bytes
uint64_t total_bytes() const
return total number of bytes available in all disks
void check_initialized()
Definition: config.hpp:151
#define LOG1
Definition: logger.hpp:145
file_ptr create_file(const std::string &io_impl, const std::string &filename, int options, int physical_device_id, int disk_allocator_id)
create fileio object from io_impl string and a few parameters
Definition: create_file.cpp:29
uint64_t total_allocation() const
return total requested allocation in bytes
tlx::simple_vector< disk_block_allocator * > block_allocators_
one block allocator per disk
uint64_t total_allocation_
total requested allocation in bytes
disk_config & disk(size_t disk)
Returns mutable disk_config structure for additional disk parameters.
Definition: config.cpp:214
static const int DEFAULT_QUEUE
Definition: file.hpp:89
size_t disks_number()
Definition: config.hpp:195
static instance_pointer get_instance()
Definition: singleton.hpp:44
size_t ndisks_
number of managed disks
std::string fileio_string() const
return formatted fileio name and optional configuration parameters
Definition: config.cpp:537
block_manager()
private construction from singleton
uint64_t free_bytes() const
Return total number of free bytes.
tlx::simple_vector< file_ptr > disk_files_
vector of opened disk files
uint64_t maximum_allocation() const
return maximum number of bytes allocated during program run.
read and write of the file are allowed
Definition: file.hpp:71
#define LOG
Default logging method: output if the local debug variable is true.
Definition: logger.hpp:141
uint64_t maximum_allocation_
maximum number of bytes allocated during program run.
This class manages allocation of blocks onto a single disk.