1 #include <sys/resource.h>
2 #include <sys/statvfs.h>
10 #include <sys/types.h>
16 #include <Poco/TemporaryFile.h>
17 #include <Poco/Logger.h>
18 #include <Poco/Message.h>
33 : diskSize(0), diskUsed(0),
diskFree(0), diskUsedPercent(0.0), diskFreePercent(0.0), externalDiskSize(0)
39 catch(Poco::Exception& e)
41 Poco::Logger::root().log(Poco::Message(
drce_const::moduleName,
"DiskSpaceExtractor "+e.message(), Poco::Message::Priority::PRIO_ERROR));
47 externalDiskSize = diskSize_;
52 Poco::TemporaryFile temporaryFile;
53 temporaryFile.createFile();
54 const std::string
fileName = temporaryFile.path();
56 struct statvfs64 buf={0};
58 if (statvfs64(fileName.c_str(), &buf) != 0)
59 throw Poco::Exception(std::string(
"DiskSpaceExtractor::update: ")+strerror(errno));
61 size_t blksize = buf.f_bsize;
62 size_t blocks = buf.f_blocks;
63 size_t freeblks = buf.f_bfree;
66 diskSize = externalDiskSize;
68 diskSize = blocks * blksize;
73 diskUsedPercent =
static_cast<double>(diskUsed*100)/diskSize;
74 diskFreePercent =
static_cast<double>(diskFree*100)/diskSize;
78 size_t DiskSpaceExtractor::getDiskUsageBytes(pid_t
pid)
81 char buf[PATH_MAX] = {0};
82 struct dirent* entry =
nullptr;
84 snprintf(buf, PATH_MAX,
"/proc/%i/fd/", pid);
86 DIR* dir = opendir(buf);
89 while ((entry = readdir(dir)) !=
nullptr)
91 if (entry->d_name != std::string(
".") && entry->d_name != std::string(
".."))
93 diskUsage += getFileSize(std::string(buf)+
"/"+entry->d_name);
101 size_t DiskSpaceExtractor::getFileSize(
const std::string& filename)
104 struct stat buff = {0};
106 if (stat (filename.c_str(), &buff) == 0)
108 result = buff.st_size;
115 size_t diskUsage = 0;
117 for (
size_t i=0;i<ids.size();++i)
119 diskUsage += getDiskUsageBytes(ids[i]);
125 void CpuUsageExtractor::getCpuData(pid_t pid, std::pair<unsigned long int, unsigned long int>& p)
127 const unsigned int count = 14;
128 char path[PATH_MAX]={0};
129 char pId[PATH_MAX]={0};
130 snprintf(path, PATH_MAX,
"%s%d%s",
"/proc/", pid,
"/stat");
132 FILE* fp = fopen(path,
"r");
135 for(
unsigned int i = 1; i < count; ++i)
137 fscanf(fp,
"%s", pId);
140 fscanf(fp,
"%ld%ld", &p.first, &p.second);
150 const double defaultUsage = 100.0;
151 const unsigned int SLPTIME = 200000;
153 std::vector<std::pair<unsigned long int, unsigned long int> > cpuPrevData, cpuData;
155 for (
size_t i=0;i<ids.size();++i)
157 std::pair<unsigned long int, unsigned long int>
data;
158 getCpuData(ids[i], data);
159 cpuPrevData.push_back(data);
164 for (
size_t i=0;i<ids.size();++i)
166 std::pair<unsigned long int, unsigned long int>
data;
167 getCpuData(ids[i], data);
168 cpuData.push_back(data);
171 unsigned long int q(0), r(0);
173 for (
size_t i=0;i<cpuData.size();++i)
175 if ((cpuData[i].first) && (cpuPrevData[i].first))
176 q += abs(cpuData[i].first-cpuPrevData[i].first);
178 if ((cpuData[i].second) && (cpuPrevData[i].second))
179 r += abs(cpuData[i].second-cpuPrevData[i].second);
182 double result = defaultUsage;
183 long int frec = sysconf(_SC_CLK_TCK);
186 result = ((q + r)*100.0/((
double)0.2*frec));
192 : cpuUsage(0.0),
iowait(0.0)
199 memset(&oldCpu, 0,
sizeof(oldCpu));
205 void CpuUsageExtractor::updateUsage(
void)
207 std::ifstream ifs(
"/proc/stat");
211 while(getline(ifs, line))
213 if (line.compare(0, 4,
"cpu ")==0)
215 unsigned long long nice(0), irq(0), softirq(0);
217 int items = sscanf(line.c_str(),
218 "cpu %20llu %20llu %20llu %20llu %20llu %20llu %20llu",
229 newCpu.system += irq + softirq;
238 cpu.user = newCpu.user - oldCpu.user;
239 cpu.system = newCpu.system - oldCpu.system;
240 cpu.idle = newCpu.idle - oldCpu.idle;
241 cpu.iowait = newCpu.iowait - oldCpu.iowait;
242 double total =
static_cast<double>(
cpu.user +
cpu.system +
cpu.idle +
cpu.iowait)/100.0;
244 cpuUsage =
static_cast<double>(
cpu.user/
total)+static_cast<double>(
cpu.system/total);
257 std::ifstream ifs(
"/proc/loadavg");
261 while(getline(ifs, line))
263 sscanf(line.c_str(),
"%lf %lf %lf", &loadAverage[0], &loadAverage[1], &loadAverage[2]);
274 value = loadAverage[
index];
281 : memTotal(0), memFree(0), buffers(0), cached(0), swapTotal(0), swapFree(0), vmallocTotal(0), vmallocUsed(0)
286 : memTotal(0), memFree(0), buffers(0), cached(0), swapTotal(0), swapFree(0), vmallocTotal(0), vmallocUsed(0)
288 *
this = std::forward<MemInfo>(rhs);
295 memTotal = std::move(rhs.memTotal);
296 memFree = std::move(rhs.memFree);
297 buffers = std::move(rhs.buffers);
298 cached = std::move(rhs.cached);
299 swapTotal = std::move(rhs.swapTotal);
300 swapFree = std::move(rhs.swapFree);
301 vmallocTotal = std::move(rhs.vmallocTotal);
302 vmallocUsed = std::move(rhs.vmallocUsed);
309 : vramUsed(0), rramUsed(0),
vramFree(0),
rramFree(0), vramUsedPercent(0.0), rramUsedPercent(0.0), vramFreePercent(0.0), rramFreePercent(0.0),
310 externalVramTotal(0), externalRramTotal(0)
317 externalVramTotal = vramTotal_;
322 externalRramTotal = rramTotal_;
329 if (externalRramTotal)
330 memInfo.
memTotal = externalRramTotal;
335 size_t vmemTotal = (externalVramTotal)?externalVramTotal:(memInfo.
swapTotal+memInfo.
memTotal);
339 rramFreePercent =
static_cast<double>(
rramFree*100)/(memInfo.
memTotal*1024);
340 rramUsedPercent =
static_cast<double>(rramUsed*100)/(memInfo.
memTotal*1024);
342 vramFreePercent =
static_cast<double>(
vramFree*100)/(vmemTotal*1024);
343 vramUsedPercent =
static_cast<double>(vramUsed*100)/(vmemTotal*1024);
345 vramTotal = vmemTotal*1024;
353 std::ifstream ifs(
"/proc/meminfo");
357 while(getline(ifs, line))
359 if (line.find(
"MemTotal")!=std::string::npos)
360 sscanf(line.c_str(),
"MemTotal: %zu", &(memInfo.
memTotal));
362 if (line.find(
"MemFree")!=std::string::npos)
363 sscanf(line.c_str(),
"MemFree: %zu", &(memInfo.
memFree));
365 if (line.find(
"Buffers")!=std::string::npos)
366 sscanf(line.c_str(),
"Buffers: %zu", &(memInfo.
buffers));
368 if (line.find(
"Cached")!=std::string::npos)
369 sscanf(line.c_str(),
"Cached: %zu", &(memInfo.
cached));
371 if (line.find(
"SwapTotal")!=std::string::npos)
372 sscanf(line.c_str(),
"SwapTotal: %zu", &(memInfo.
swapTotal));
374 if (line.find(
"SwapFree")!=std::string::npos)
375 sscanf(line.c_str(),
"SwapFree: %zu", &(memInfo.
swapFree));
377 if (line.find(
"VmallocTotal")!=std::string::npos)
378 sscanf(line.c_str(),
"VmallocTotal: %zu", &(memInfo.
vmallocTotal));
380 if (line.find(
"VmallocUsed")!=std::string::npos)
381 sscanf(line.c_str(),
"VmallocUsed: %zu", &(memInfo.
vmallocUsed));
403 *
this = std::forward<FieldsArray>(rhs);
423 cpu = std::move(rhs.cpu);
424 vram = std::move(rhs.vram);
425 rram = std::move(rhs.rram);
426 threads = std::move(rhs.threads);
427 disk = std::move(rhs.disk);
448 for (
size_t i=0;i<ids.size();++i)
453 fieldsArray.
vram += processData.
vram;
454 fieldsArray.
rram += processData.
rram;
464 if (fieldsArray.
cpu > 1000 || fieldsArray.
threads > 100)
466 std::cout <<
"taskId: " << asyncTask.
taskId <<
" pid: "<< asyncTask.
pid <<
" count: " << ids.size() <<
std::endl;
467 std::cout <<
"Children: ";
468 for (
size_t i=0;i<ids.size();++i)
469 std::cout << ids[i] <<
" ";
471 std::cout <<
"cpu: " << fieldsArray.
cpu <<
" vram: " << fieldsArray.
vram <<
" rram: "<< fieldsArray.
rram <<
" threads: " << fieldsArray.
threads <<
std::endl;
480 ResourceUsageExtractor::ResourceUsageExtractor(
void)
481 : cpuUsageExtractor(), diskSpaceExtractor(), memoryExtractor(), loadAverageExtractor(), maxThreadsCount(0), maxProcessesCount(0)
487 Poco::Mutex::ScopedLock lock(
mutex);
495 Poco::Mutex::ScopedLock lock(
mutex);
505 Poco::Mutex::ScopedLock lock(
mutex);
514 Poco::Mutex::ScopedLock lock(
mutex);
532 long sz = sysconf(_SC_PAGESIZE);
534 throw Poco::Exception(std::string(
"ResourceUsageExtractor::extract: ")+strerror(errno));
538 size_t stacksize = 0;
540 pthread_attr_init(&attr);
541 pthread_attr_getstacksize(&attr, &stacksize);
566 return resourceUsage;
571 Poco::Mutex::ScopedLock lock(
mutex);
577 Poco::Mutex::ScopedLock lock(
mutex);
583 Poco::Mutex::ScopedLock lock(
mutex);
589 Poco::Mutex::ScopedLock lock(
mutex);
595 Poco::Mutex::ScopedLock lock(
mutex);
601 Poco::Mutex::ScopedLock lock(
mutex);
607 Poco::Mutex::ScopedLock lock(
mutex);
613 Poco::Mutex::ScopedLock lock(
mutex);
619 Poco::Mutex::ScopedLock lock(
mutex);
625 Poco::Mutex::ScopedLock lock(
mutex);
631 Poco::Mutex::ScopedLock lock(
mutex);
637 Poco::Mutex::ScopedLock lock(
mutex);
643 Poco::Mutex::ScopedLock lock(
mutex);
649 Poco::Mutex::ScopedLock lock(
mutex);