hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCEResourceLimitsChecker.cpp
Go to the documentation of this file.
1 #include <string.h>
2 #include <Poco/Logger.h>
3 
6 #include "DRCEMessageConst.hpp"
7 #include "DRCEError.hpp"
9 
10 namespace HCE
11 {
12 namespace drce
13 {
14 
15 //-----------------------------------------------------------------------------
17 : message(message_)
18 {
19 }
20 //-----------------------------------------------------------------------------
21 void DRCEResourceLimitsChecker::checkLimits(const ResourceLimits& resourceLimit) throw (Poco::Exception)
22 {
23  if ((resourceLimit.cpuMax > 0) || (resourceLimit.iowaitMax > 0))
24  {
25  if (resourceLimit.cpuMax > 0)
26  {
28  throw Poco::Exception(message(message_const::EXCEEDED_CPU_LOAD)+
29  message(message_const::PRESENT_LOADING, HCE::drce::ResourceUsageExtractor::getInstance().getCpuUsageExtractor().getCpuUsage())+" %",
31  }
32 
33  if (resourceLimit.iowaitMax > 0)
34  {
36  throw Poco::Exception(message(message_const::EXCEEDED_IOWAIT_LOAD)+
37  message(message_const::PRESENT_LOADING, HCE::drce::ResourceUsageExtractor::getInstance().getCpuUsageExtractor().getIOWait())+" %",
39  }
40  }
41 
42  if (resourceLimit.diskFree > 0 || resourceLimit.diskFreeMin)
43  {
44  std::stringstream outMsg;
45  outMsg << " resourceLimit.diskFreeMin = " << resourceLimit.diskFreeMin
46  << " diskSpaceExtractor.getDiskFree() = " << HCE::drce::ResourceUsageExtractor::getInstance().getDiskSpaceExtractor().getDiskFree()
47  << "\nresourceLimit.diskFree = " << resourceLimit.diskFree
48  << " diskSpaceExtractor.getDiskFreePercent() = " << HCE::drce::ResourceUsageExtractor::getInstance().getDiskSpaceExtractor().getDiskFreePercent();
49  Poco::Logger::root().log(Poco::Message(drce_const::moduleName, outMsg.str(), Poco::Message::Priority::PRIO_TRACE));
50 
52  throw Poco::Exception(message(message_const::EXCEEDED_DISK_SPACE)+
53  message(message_const::FREE, HCE::drce::ResourceUsageExtractor::getInstance().getDiskSpaceExtractor().getDiskFree())+
55 
57  throw Poco::Exception(message(message_const::EXCEEDED_DISK_SPACE)+
58  message(message_const::FREE, HCE::drce::ResourceUsageExtractor::getInstance().getDiskSpaceExtractor().getDiskFreePercent())+
60  }
61 
62  if (resourceLimit.rramFree > 0.0 || resourceLimit.rramFreeMin || resourceLimit.vramFree > 0.0 || resourceLimit.vramFreeMin)
63  {
64  std::stringstream outMsg;
65  outMsg << " resourceLimit.rramFreeMin = " << resourceLimit.rramFreeMin
66  << " memoryExtractor.getRramFree() = " << HCE::drce::ResourceUsageExtractor::getInstance().getMemoryExtractor().getRramFree()
67  << "\nresourceLimit.rramFree = " << resourceLimit.rramFree
68  << " memoryExtractor.getRramFreePercent() = " << HCE::drce::ResourceUsageExtractor::getInstance().getMemoryExtractor().getRramFreePercent()
69  << "\nresourceLimit.vramFreeMin = " << resourceLimit.vramFreeMin
70  << " memoryExtractor.getVramFree() = " << HCE::drce::ResourceUsageExtractor::getInstance().getMemoryExtractor().getVramFree()
71  << "\nresourceLimit.vramFree = " << resourceLimit.vramFree
72  << " memoryExtractor.getVramFreePercent() = " << HCE::drce::ResourceUsageExtractor::getInstance().getMemoryExtractor().getVramFreePercent();
73  Poco::Logger::root().log(Poco::Message(drce_const::moduleName, outMsg.str(), Poco::Message::Priority::PRIO_TRACE));
74 
76  throw Poco::Exception(message(message_const::EXCEEDED_RESOURCE_RAM)+
77  message(message_const::FREE, HCE::drce::ResourceUsageExtractor::getInstance().getMemoryExtractor().getRramFree())+
79 
81  throw Poco::Exception(message(message_const::EXCEEDED_RESOURCE_RAM)+
82  message(message_const::FREE, HCE::drce::ResourceUsageExtractor::getInstance().getMemoryExtractor().getRramFreePercent())+
84 
86  throw Poco::Exception(message(message_const::EXCEEDED_VIRTUAL_RAM)+
87  message(message_const::FREE, HCE::drce::ResourceUsageExtractor::getInstance().getMemoryExtractor().getVramFree())+
89 
91  throw Poco::Exception(message(message_const::EXCEEDED_VIRTUAL_RAM)+
92  message(message_const::FREE, HCE::drce::ResourceUsageExtractor::getInstance().getMemoryExtractor().getVramFreePercent())+
94  }
95 
96  if (resourceLimit.procMax || resourceLimit.threadsMax)
97  {
98  unsigned int processCount = 0;
99  unsigned int threadsCount = 0;
100  getUsingProcessAndThreads(processCount, threadsCount);
101 
102  std::stringstream outMsg;
103  outMsg << " processCount = " << processCount << " threadsCount = " << threadsCount;
104  Poco::Logger::root().log(Poco::Message(drce_const::moduleName, outMsg.str(), Poco::Message::Priority::PRIO_TRACE));
105 
106  if (resourceLimit.procMax)
107  {
108  if (resourceLimit.procMax < processCount)
109  throw Poco::Exception(message(message_const::EXCEEDED_PROCESSES_COUNT)+
110  message(message_const::USAGE, processCount), ERROR_EXCEEDED_RESOURCE_LIMITS);
111  }
112 
113  if (resourceLimit.threadsMax)
114  {
115  if (resourceLimit.threadsMax < threadsCount)
116  throw Poco::Exception(message(message_const::EXCEEDED_THREADS_COUNT)+
117  message(message_const::USAGE, threadsCount), ERROR_EXCEEDED_RESOURCE_LIMITS);
118  }
119  }
120 }
121 //-----------------------------------------------------------------------------
122 void DRCEResourceLimitsChecker::getUsingProcessAndThreads(unsigned int& processCount, unsigned int& threadsCount)
123 {
124  DRCEReadProcessData::getInstance().getProcessAndThreads(processCount, threadsCount);
125 }
126 //-----------------------------------------------------------------------------
127 void DRCEResourceLimitsChecker::checkLimits(const AsyncTasks& asyncTask, const ResourceUsageLimits& resourceUsageLimit) throw (Poco::Exception)
128 {
130  if (resourceUsageLimit.iowaitMax > 0)
131  {
132  if (resourceUsageLimit.iowaitMax < HCE::drce::ResourceUsageExtractor::getInstance().getCpuUsageExtractor().getIOWait())
133  throw Poco::Exception(message(message_const::EXCEEDED_USAGE_IOWAIT_LOAD)+
134  message(message_const::PRESENT_LOADING, HCE::drce::ResourceUsageExtractor::getInstance().getCpuUsageExtractor().getIOWait())+" %",
136  }
137 
138  if (resourceUsageLimit.vramMax > 0 || resourceUsageLimit.rramMax > 0 || resourceUsageLimit.diskMax > 0 || resourceUsageLimit.diskMaxBytes > 0)
139  {
141 
142  if (resourceUsageLimit.vramMax > 0)
143  {
144  double usedPercent = static_cast<double>(fieldsArray.vram * 100)/HCE::drce::ResourceUsageExtractor::getInstance().getMemoryExtractor().getVramUsed();
145 
146  if (resourceUsageLimit.vramMax < usedPercent)
147  throw Poco::Exception(message(message_const::EXCEEDED_USAGE_VIRTUAL_RAM)+
148  message(message_const::USAGE, trim(usedPercent))+
150  }
151 
152  if (resourceUsageLimit.rramMax > 0)
153  {
154  double usedPercent = static_cast<double>(fieldsArray.rram * 100)/HCE::drce::ResourceUsageExtractor::getInstance().getMemoryExtractor().getRramUsed();
155 
156  if (resourceUsageLimit.rramMax < usedPercent)
157  throw Poco::Exception(message(message_const::EXCEEDED_USAGE_RESOURCE_RAM)+
158  message(message_const::USAGE, trim(usedPercent))+
160  }
161 
162  if (resourceUsageLimit.diskMax > 0)
163  {
164  double usedPercent = static_cast<double>(fieldsArray.disk * 100)/HCE::drce::ResourceUsageExtractor::getInstance().getDiskSpaceExtractor().getDiskSize();
165 
166  if (resourceUsageLimit.diskMax < usedPercent)
167  throw Poco::Exception(message(message_const::EXCEEDED_USAGE_DISK_SPACE)+
168  message(message_const::USAGE, trim(usedPercent))+
170  }
171 
172  if (resourceUsageLimit.diskMaxBytes > 0)
173  {
174  if (resourceUsageLimit.diskMaxBytes < fieldsArray.disk)
175  throw Poco::Exception(message(message_const::EXCEEDED_USAGE_DISK_SPACE)+
176  message(message_const::USAGE, fieldsArray.disk)+
178  }
179  }
180 }
181 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
183 } // end namespace drce
184 } // end namespace HCE