SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Resources.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #ifndef swcdb_common_sys_Resources_h
8 #define swcdb_common_sys_Resources_h
9 
10 
14 
15 
16 namespace SWC {
17 
18 
20 namespace System {
21 
22 struct Notifier {
23 
24  virtual void rss_used_reg(size_t) noexcept = 0;
25  virtual void rss_free(size_t) noexcept = 0;
26  virtual void rss_used(size_t) noexcept = 0;
27 
28  virtual void cpu_user(size_t) noexcept = 0;
29  virtual void cpu_sys(size_t) noexcept = 0;
30  virtual void cpu_threads(size_t) noexcept = 0;
31 
32  virtual uint64_t get_cpu_ms_interval() const noexcept = 0;
33 
34  virtual ~Notifier() noexcept { }
35 
36 };
37 
38 }}
39 
40 
41 
44 
45 
46 
47 namespace SWC { namespace System {
48 
49 
50 class Resources final {
51  public:
54 
55  SWC_SHOULD_NOT_INLINE
57  Config::Property::Value_int32_g::Ptr ram_percent_allowed,
58  Config::Property::Value_int32_g::Ptr ram_percent_reserved,
59  Config::Property::Value_int32_g::Ptr ram_release_rate,
60  Notifier* notifier=nullptr,
61  Mem::ReleaseCall_t&& release_call=nullptr)
62  : cpu(notifier),
63  mem(
64  ram_percent_allowed,
65  ram_percent_reserved,
66  ram_release_rate,
67  notifier,
68  std::move(release_call)
69  ),
70  running(0),
71  m_mutex_timer(),
72  m_timer(ioctx->executor()) {
73  checker();
74  }
75 
76  Resources(const Resources& other) = delete;
77  Resources(Resources&& other) = delete;
78  Resources operator=(const Resources& other) = delete;
79  Resources operator=(Resources&& other) = delete;
80 
81  ~Resources() noexcept { }
82 
84  bool is_low_mem_state() const noexcept {
85  return mem.is_low_mem_state();
86  }
87 
89  size_t need_ram() const noexcept {
90  return mem.need_ram();
91  }
92 
94  size_t avail_ram() const noexcept {
95  return mem.avail_ram();
96  }
97 
99  bool need_ram(uint32_t sz) const noexcept {
100  return mem.need_ram(sz);
101  }
102 
104  void more_mem_future(size_t sz) noexcept {
105  mem.more_mem_future(sz);
106  }
107 
109  void less_mem_future(size_t sz) noexcept {
110  mem.less_mem_future(sz);
111  }
112 
114  void more_mem_releasable(size_t sz) noexcept {
116  }
117 
119  void less_mem_releasable(size_t sz) noexcept {
121  }
122 
124  void adj_mem_releasable(ssize_t sz) noexcept {
126  }
127 
129  uint32_t available_mem_mb() const noexcept {
130  return mem.available_mem_mb();
131  }
132 
134  uint32_t concurrency() const noexcept {
135  return cpu.concurrency;
136  }
137 
139  uint32_t available_cpu_mhz() const noexcept {
140  return cpu.mhz;
141  }
142 
144  uint32_t cpu_usage() const noexcept {
145  return cpu.usage;
146  }
147 
148  void stop() {
149  for(;;) {
150  {
152  m_timer.cancel();
153  }
154  if(!running.count())
155  break;
156  std::this_thread::sleep_for(std::chrono::milliseconds(2));
157  }
158  }
159 
160  SWC_SHOULD_NOT_INLINE
161  void print(std::ostream& out) const {
162  out << "Resources(";
163  cpu.print(out << "CPU-");
164  mem.print(out << " Mem-MB-", 1048576);
165  out << ')';
166  }
167 
168  private:
169 
170  SWC_SHOULD_NOT_INLINE
171  void checker() noexcept {
172  running.increment();
173  uint64_t ts = Time::now_ms();
174  uint64_t t1 = mem.check(ts);
175  uint64_t t2 = cpu.check(ts);
176  try {
177  schedule(t1 < t2 ? t1 : t2);
178  } catch(...) {
180  }
181  }
182 
184  void schedule(uint64_t intval) {
186  m_timer.expires_after(std::chrono::milliseconds(intval));
187  struct TimerTask {
188  Resources* ptr;
190  TimerTask(Resources* a_ptr) noexcept : ptr(a_ptr) { }
191  void operator()(const asio::error_code& ec) {
192  if(ec != asio::error::operation_aborted)
193  ptr->checker();
194  ptr->running.is_last();
195  }
196  };
197  m_timer.async_wait(TimerTask(this));
198  }
199 
202  asio::steady_timer m_timer;
203 
204 };
205 
206 
207 }}
208 
209 
210 #endif // swcdb_common_sys_Resources_h
SWC::System::Resources::print
SWC_SHOULD_NOT_INLINE void print(std::ostream &out) const
Definition: Resources.h:161
SWC::System::Notifier::cpu_sys
virtual void cpu_sys(size_t) noexcept=0
SWC::System::Mem::avail_ram
SWC_CAN_INLINE size_t avail_ram() const noexcept
Definition: Resource_Mem.h:166
SWC::System::Resources
Definition: Resources.h:50
SWC::System::CPU::mhz
Core::Atomic< uint32_t > mhz
Definition: Resource_CPU.h:27
SWC::System::Mem::ReleaseCall_t
std::function< size_t(size_t)> ReleaseCall_t
Definition: Resource_Mem.h:48
SWC::System::Resources::available_mem_mb
SWC_CAN_INLINE uint32_t available_mem_mb() const noexcept
Definition: Resources.h:129
SWC::System::Resources::concurrency
SWC_CAN_INLINE uint32_t concurrency() const noexcept
Definition: Resources.h:134
SWC::System::Resources::avail_ram
SWC_CAN_INLINE size_t avail_ram() const noexcept
Definition: Resources.h:94
SWC::System::Resources::operator=
Resources operator=(const Resources &other)=delete
SWC::System::Notifier::rss_used
virtual void rss_used(size_t) noexcept=0
CompletionCounter.h
SWC::System::Resources::~Resources
~Resources() noexcept
Definition: Resources.h:81
SWC::System::Resources::Resources
SWC_SHOULD_NOT_INLINE Resources(const Comm::IoContextPtr &ioctx, Config::Property::Value_int32_g::Ptr ram_percent_allowed, Config::Property::Value_int32_g::Ptr ram_percent_reserved, Config::Property::Value_int32_g::Ptr ram_release_rate, Notifier *notifier=nullptr, Mem::ReleaseCall_t &&release_call=nullptr)
Definition: Resources.h:56
SWC::System::Notifier
Definition: Resources.h:22
SWC::System::Notifier::rss_used_reg
virtual void rss_used_reg(size_t) noexcept=0
SWC::System::Resources::mem
Mem mem
Definition: Resources.h:53
SWC::System::Resources::stop
void stop()
Definition: Resources.h:148
SWC::System::Resources::more_mem_future
SWC_CAN_INLINE void more_mem_future(size_t sz) noexcept
Definition: Resources.h:104
SWC::System::Mem::available_mem_mb
SWC_CAN_INLINE uint32_t available_mem_mb() const noexcept
Definition: Resource_Mem.h:230
SWC::System::Resources::cpu
CPU cpu
Definition: Resources.h:52
SWC::Comm::IoContextPtr
std::shared_ptr< IoContext > IoContextPtr
Definition: IoContext.h:16
SWC::System::Resources::need_ram
SWC_CAN_INLINE bool need_ram(uint32_t sz) const noexcept
Definition: Resources.h:99
SWC::System::Mem
Definition: Resource_Mem.h:38
SWC::System::Mem::more_mem_releasable
SWC_CAN_INLINE void more_mem_releasable(size_t sz) noexcept
Definition: Resource_Mem.h:202
SWC::System::Notifier::get_cpu_ms_interval
virtual uint64_t get_cpu_ms_interval() const noexcept=0
IoContext.h
Resource_CPU.h
SWC::Core::MutexAtomic::scope
Definition: MutexAtomic.h:77
SWC::System::Resources::available_cpu_mhz
SWC_CAN_INLINE uint32_t available_cpu_mhz() const noexcept
Definition: Resources.h:139
SWC::System::Resources::more_mem_releasable
SWC_CAN_INLINE void more_mem_releasable(size_t sz) noexcept
Definition: Resources.h:114
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::System::Resources::less_mem_releasable
SWC_CAN_INLINE void less_mem_releasable(size_t sz) noexcept
Definition: Resources.h:119
SWC::System::CPU
Definition: Resource_CPU.h:22
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
Property.h
SWC::System::Mem::less_mem_future
SWC_CAN_INLINE void less_mem_future(size_t sz) noexcept
Definition: Resource_Mem.h:189
SWC::Core::MutexAtomic
Definition: MutexAtomic.h:17
SWC::System::Resources::m_timer
asio::steady_timer m_timer
Definition: Resources.h:202
SWC::System::Notifier::cpu_threads
virtual void cpu_threads(size_t) noexcept=0
SWC::System::Resources::checker
SWC_SHOULD_NOT_INLINE void checker() noexcept
Definition: Resources.h:171
SWC::System::Resources::need_ram
SWC_CAN_INLINE size_t need_ram() const noexcept
Definition: Resources.h:89
SWC::Core::CompletionCounter< uint8_t >
SWC::System::Resources::adj_mem_releasable
SWC_CAN_INLINE void adj_mem_releasable(ssize_t sz) noexcept
Definition: Resources.h:124
SWC::System::Mem::more_mem_future
SWC_CAN_INLINE void more_mem_future(size_t sz) noexcept
Definition: Resource_Mem.h:185
SWC::System::CPU::print
void print(std::ostream &out) const
Definition: Resource_CPU.h:56
SWC::System::Mem::need_ram
SWC_CAN_INLINE size_t need_ram() const noexcept
Definition: Resource_Mem.h:147
SWC::System::Mem::print
void print(std::ostream &out, size_t base=1048576) const
Definition: Resource_Mem.h:234
SWC::System::Resources::less_mem_future
SWC_CAN_INLINE void less_mem_future(size_t sz) noexcept
Definition: Resources.h:109
SWC::System::Resources::running
Core::CompletionCounter< uint8_t > running
Definition: Resources.h:200
Resource_Mem.h
SWC::Core::CompletionCounter::count
constexpr SWC_CAN_INLINE CountT count() const noexcept
Definition: CompletionCounter.h:42
SWC::System::Resources::m_mutex_timer
Core::MutexAtomic m_mutex_timer
Definition: Resources.h:201
SWC::Core::CompletionCounter::increment
constexpr SWC_CAN_INLINE void increment(CountT v=1) noexcept
Definition: CompletionCounter.h:32
SWC::System::Resources::is_low_mem_state
SWC_CAN_INLINE bool is_low_mem_state() const noexcept
Definition: Resources.h:84
SWC::Time::now_ms
SWC_CAN_INLINE int64_t now_ms() noexcept
Definition: Time.h:36
SWC::System::Mem::check
SWC_SHOULD_NOT_INLINE uint64_t check(uint64_t ts) noexcept
Definition: Resource_Mem.h:99
SWC::System::Resources::cpu_usage
SWC_CAN_INLINE uint32_t cpu_usage() const noexcept
Definition: Resources.h:144
SWC::System::Mem::adj_mem_releasable
SWC_CAN_INLINE void adj_mem_releasable(ssize_t sz) noexcept
Definition: Resource_Mem.h:217
SWC::System::Mem::less_mem_releasable
SWC_CAN_INLINE void less_mem_releasable(size_t sz) noexcept
Definition: Resource_Mem.h:206
SWC::Core::CompletionCounter::is_last
constexpr SWC_CAN_INLINE bool is_last() noexcept
Definition: CompletionCounter.h:37
SWC::System::Mem::is_low_mem_state
SWC_CAN_INLINE bool is_low_mem_state() const noexcept
Definition: Resource_Mem.h:142
SWC::System::Notifier::rss_free
virtual void rss_free(size_t) noexcept=0
SWC::System::Notifier::cpu_user
virtual void cpu_user(size_t) noexcept=0
SWC::System::Resources::Resources
Resources(Resources &&other)=delete
SWC::System::CPU::check
SWC_SHOULD_NOT_INLINE uint64_t check(uint64_t ts) noexcept
Definition: Resource_CPU.h:45
SWC::System::Resources::operator=
Resources operator=(Resources &&other)=delete
SWC::System::CPU::usage
Core::Atomic< uint32_t > usage
Definition: Resource_CPU.h:28
SWC::System::CPU::concurrency
Core::Atomic< uint32_t > concurrency
Definition: Resource_CPU.h:26
SWC_LOG_CURRENT_EXCEPTION
#define SWC_LOG_CURRENT_EXCEPTION(_s_)
Definition: Exception.h:144
SWC::System::Resources::Resources
Resources(const Resources &other)=delete
SWC::Config::Property::Value_int32_g
Definition: Property.h:586
SWC::System::Resources::schedule
SWC_CAN_INLINE void schedule(uint64_t intval)
Definition: Resources.h:184