TPIE

11a2c2d
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 #include <tpie/tpie_export.h>
28 #include <tpie/config.h>
29 #include <tpie/resources.h>
30 #include <atomic>
31 #include <iosfwd>
32 
33 namespace tpie {
34 
38 class TPIE_EXPORT resource_manager {
39 public:
43  enum enforce_t {
53  ENFORCE_THROW
54  };
55 
59  size_t used() const noexcept;
60 
64  size_t available() const noexcept;
65 
69  size_t limit() const noexcept {return m_limit;}
70 
77  void set_limit(size_t new_limit);
78 
83  void set_enforcement(enforce_t e);
84 
88  enforce_t enforcement() const noexcept {return m_enforce;}
89 
90  void register_increased_usage(size_t amount);
91 
92  void register_decreased_usage(size_t amount);
93 
94  virtual std::string amount_with_unit(size_t amount) const;
95 
100  resource_manager(resource_type type);
101 
102  virtual ~resource_manager() = default;
103 
104 private:
105  void print_resource_complaint(std::ostream & os, size_t amount, size_t usage);
106 protected:
107  virtual void throw_out_of_resource_error(const std::string & s) = 0;
108 
109  std::atomic<size_t> m_used;
110  size_t m_limit;
111  size_t m_maxExceeded;
112  size_t m_nextWarning;
113  enforce_t m_enforce;
114 
115  resource_type resource_managed;
116 };
117 
118 } //namespace tpie
119 
120 #endif //__TPIE_RESOURCE_MANAGER_H__
tpie::resource_manager::ENFORCE_DEBUG
@ ENFORCE_DEBUG
Log to debug log when the resource limit is exceeded.
Definition: resource_manager.h:48
tpie::resource_manager::ENFORCE_WARN
@ ENFORCE_WARN
Log a warning when the resource limit is exceeded.
Definition: resource_manager.h:51
tpie::resource_manager
Resource management object used to track resource usage.
Definition: resource_manager.h:38
tpie::resource_manager::enforce_t
enforce_t
Memory limit enforcement policies.
Definition: resource_manager.h:43
resources.h
tpie::resource_manager::ENFORCE_IGNORE
@ ENFORCE_IGNORE
Ignore when running out of the resource.
Definition: resource_manager.h:45
tpie
Definition: access_type.h:26
tpie::resource_manager::enforcement
enforce_t enforcement() const noexcept
Return the current resource limit enforcement policy.
Definition: resource_manager.h:88