TPIE

2362a60
resource_manager.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 //
4 // Copyright 2011, The TPIE development team
5 //
6 // This file is part of TPIE.
7 //
8 // TPIE is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License as published by the
10 // Free Software Foundation, either version 3 of the License, or (at your
11 // option) any later version.
12 //
13 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
14 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 // License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
20 
24 
25 #ifndef __TPIE_RESOURCE_MANAGER_H__
26 #define __TPIE_RESOURCE_MANAGER_H__
27 
28 #include <tpie/config.h>
29 #include <tpie/util.h>
30 #include <tpie/resources.h>
31 #include <mutex>
32 #include <unordered_map>
33 #include <type_traits>
34 #include <utility>
35 #include <memory>
36 #include <atomic>
37 #include <cstdlib>
38 #include <iostream>
39 #include <sstream>
40 #include <tpie/exception.h>
41 namespace tpie {
42 
47 public:
51  enum enforce_t {
62  };
63 
67  size_t used() const noexcept;
68 
72  size_t available() const noexcept;
73 
77  size_t limit() const noexcept {return m_limit;}
78 
85  void set_limit(size_t new_limit);
86 
91  void set_enforcement(enforce_t e);
92 
96  enforce_t enforcement() const noexcept {return m_enforce;}
97 
98  void register_increased_usage(size_t amount);
99 
100  void register_decreased_usage(size_t amount);
101 
102  virtual std::string amount_with_unit(size_t amount) const {
103  std::ostringstream os;
104  os << amount << " amount of " << resource_managed;
105  return os.str();
106  }
107 
112  resource_manager(resource_type type);
113 
114  virtual ~resource_manager() = default;
115 
116 private:
117  void print_resource_complaint(std::ostream & os, size_t amount, size_t usage);
118 protected:
119  virtual void throw_out_of_resource_error(const std::string & s) = 0;
120 
121  std::atomic<size_t> m_used;
122  size_t m_limit;
123  size_t m_maxExceeded;
124  size_t m_nextWarning;
125  enforce_t m_enforce;
126 
127  resource_type resource_managed;
128 };
129 
130 } //namespace tpie
131 
132 #endif //__TPIE_RESOURCE_MANAGER_H__
Resource management object used to track resource usage.
Defines all types of managed resources.
Log a warning when the resource limit is exceeded.
Throw an out_of_resource_error when the resource limit is exceeded.
Exception classes.
Miscellaneous utility functions.
void set_limit(size_t new_limit)
Update the resource limit.
size_t available() const noexcept
Return the amount of the resource still available to be assigned.
resource_manager(resource_type type)
size_t limit() const noexcept
Return the resource limit.
void set_enforcement(enforce_t e)
Set the resource limit enforcement policy.
size_t used() const noexcept
Return the current amount of the resource used.
enforce_t enforcement() const noexcept
Return the current resource limit enforcement policy.
Log to debug log when the resource limit is exceeded.
Ignore when running out of the resource.
enforce_t
Memory limit enforcement policies.