Thrill  0.1
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/core.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  TLX_LOG1 << "foxxll: 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  TLX_LOG1 << "foxxll: 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  TLX_LOG1 << "foxxll: In total " << ndisks_ << " disks are allocated, space: "
82  << (total_size / (1024 * 1024)) << " MiB";
83  }
84 }
85 
87 {
88  TLX_LOG << "foxxll: Block manager destructor";
89  for (size_t i = ndisks_; i > 0; )
90  {
91  --i;
92  delete block_allocators_[i];
93  disk_files_[i].reset();
94  }
95 }
96 
98 {
99  std::unique_lock<std::mutex> lock(mutex_);
100 
101  uint64_t total = 0;
102 
103  for (size_t i = 0; i < ndisks_; ++i)
104  total += block_allocators_[i]->total_bytes();
105 
106  return total;
107 }
108 
110 {
111  std::unique_lock<std::mutex> lock(mutex_);
112 
113  uint64_t total = 0;
114 
115  for (size_t i = 0; i < ndisks_; ++i)
116  total += block_allocators_[i]->free_bytes();
117 
118  return total;
119 }
120 
122 {
123  std::unique_lock<std::mutex> lock(mutex_);
124  return total_allocation_;
125 }
126 
128 {
129  std::unique_lock<std::mutex> lock(mutex_);
130  return current_allocation_;
131 }
132 
134 {
135  std::unique_lock<std::mutex> lock(mutex_);
136  return maximum_allocation_;
137 }
138 
139 } // namespace foxxll
140 
141 /**************************************************************************/
external_size_type size
file size to initially allocate
Definition: config.hpp:46
std::mutex mutex_
protect internal data structures
uint64_t free_bytes() const
Return total number of free bytes.
std::string path
the file path used by the io implementation
Definition: config.hpp:43
uint64_t current_allocation_
currently allocated bytes
void check_initialized()
Definition: config.hpp:151
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 current_allocation() const
return currently allocated bytes
tlx::simple_vector< disk_block_allocator * > block_allocators_
one block allocator per disk
uint64_t total_allocation_
total requested allocation in bytes
uint64_t maximum_allocation() const
return maximum number of bytes allocated during program run.
disk_config & disk(size_t disk)
Returns mutable disk_config structure for additional disk parameters.
Definition: config.cpp:219
uint64_t total_bytes() const
return total number of bytes available in all disks
static const int DEFAULT_QUEUE
Definition: file.hpp:89
size_t disks_number()
Definition: config.hpp:195
FOXXLL library namespace
static instance_pointer get_instance()
return instance or create base instance if empty
Definition: singleton.hpp:41
size_t ndisks_
number of managed disks
uint64_t total_allocation() const
return total requested allocation in bytes
std::string fileio_string() const
return formatted fileio name and optional configuration parameters
Definition: config.cpp:547
block_manager()
private construction from singleton
tlx::simple_vector< file_ptr > disk_files_
vector of opened disk files
#define TLX_LOG1
Definition: core.hpp:145
read and write of the file are allowed
Definition: file.hpp:71
uint64_t maximum_allocation_
maximum number of bytes allocated during program run.
This class manages allocation of blocks onto a single disk.
#define TLX_LOG
Default logging method: output if the local debug variable is true.
Definition: core.hpp:141