Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
manager.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/mem/manager.hpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
9  ******************************************************************************/
10 
11 #pragma once
12 #ifndef THRILL_MEM_MANAGER_HEADER
13 #define THRILL_MEM_MANAGER_HEADER
14 
15 #include <algorithm>
16 #include <atomic>
17 #include <cassert>
18 
19 namespace thrill {
20 namespace mem {
21 
22 /*!
23  * Object shared by allocators and other classes to track memory
24  * allocations. These is one global mem::Manager per compute host. To track
25  * memory consumption of subcomponents of Thrill, one can create local child
26  * mem::Managers which report allocation automatically to their superiors.
27  */
28 class Manager
29 {
30  static constexpr bool debug = false;
31 
32 public:
33  explicit Manager(Manager* super, const char* name)
34  : super_(super), name_(name)
35  { }
36 
37  ~Manager();
38 
39  //! return the superior Manager
40  Manager * super() { return super_; }
41 
42  //! return total allocation (local value)
43  size_t total() const { return total_; }
44 
45  //! add memory consumption.
46  Manager& add(size_t amount) {
47  size_t current = (total_ += amount);
48  peak_ = std::max(peak_.load(), current);
49  ++alloc_count_;
50  if (super_) super_->add(amount);
51  return *this;
52  }
53 
54  //! subtract memory consumption.
55  Manager& subtract(size_t amount) {
56  assert(total_ >= amount);
57  total_ -= amount;
58  if (super_) super_->subtract(amount);
59  return *this;
60  }
61 
62 private:
63  //! reference to superior memory counter
65 
66  //! description for output
67  const char* name_;
68 
69  //! total allocation
70  std::atomic<size_t> total_ { 0 };
71 
72  //! peak allocation
73  std::atomic<size_t> peak_ { 0 };
74 
75  //! number of allocation
76  std::atomic<size_t> alloc_count_ { 0 };
77 };
78 
79 } // namespace mem
80 } // namespace thrill
81 
82 #endif // !THRILL_MEM_MANAGER_HEADER
83 
84 /******************************************************************************/
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.hpp:226
const char * name_
description for output
Definition: manager.hpp:67
Manager & add(size_t amount)
add memory consumption.
Definition: manager.hpp:46
Manager & subtract(size_t amount)
subtract memory consumption.
Definition: manager.hpp:55
Manager(Manager *super, const char *name)
Definition: manager.hpp:33
std::atomic< size_t > alloc_count_
number of allocation
Definition: manager.hpp:76
size_t total() const
return total allocation (local value)
Definition: manager.hpp:43
Manager * super()
return the superior Manager
Definition: manager.hpp:40
Manager * super_
reference to superior memory counter
Definition: manager.hpp:64
std::atomic< size_t > peak_
peak allocation
Definition: manager.hpp:73
Object shared by allocators and other classes to track memory allocations.
Definition: manager.hpp:28
static constexpr bool debug
Definition: manager.hpp:30
std::atomic< size_t > total_
total allocation
Definition: manager.hpp:70