Prozess.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "Prozess.h"
  2. #ifdef WIN32
  3. # include <Psapi.h>
  4. # include <tlHelp32.h>
  5. #else
  6. # include <fstream>
  7. # include <string>
  8. # include <sys/time.h>
  9. #endif
  10. using namespace Framework;
  11. // Inhalt der Prozess Klasse aus Prozess.h
  12. // Konstruktor
  13. Prozess::Prozess()
  14. : ReferenceCounter()
  15. {
  16. #ifdef WIN32
  17. pHandle = GetCurrentProcess();
  18. SYSTEM_INFO sysInfo;
  19. FILETIME ftime, fsys, fuser;
  20. GetSystemInfo(&sysInfo);
  21. numProcessors = sysInfo.dwNumberOfProcessors;
  22. GetSystemTimeAsFileTime(&ftime);
  23. memcpy(&lastCPU, &ftime, sizeof(FILETIME));
  24. GetProcessTimes(pHandle, &ftime, &ftime, &fsys, &fuser);
  25. memcpy(&lastSysCPU, &fsys, sizeof(FILETIME));
  26. memcpy(&lastUserCPU, &fuser, sizeof(FILETIME));
  27. #endif
  28. }
  29. // nicht constant
  30. #ifdef WIN32
  31. void Prozess::setProcess(void* pHandle)
  32. {
  33. this->pHandle = pHandle;
  34. SYSTEM_INFO sysInfo;
  35. FILETIME ftime, fsys, fuser;
  36. GetSystemInfo(&sysInfo);
  37. numProcessors = sysInfo.dwNumberOfProcessors;
  38. GetSystemTimeAsFileTime(&ftime);
  39. memcpy(&lastCPU, &ftime, sizeof(FILETIME));
  40. GetProcessTimes(pHandle, &ftime, &ftime, &fsys, &fuser);
  41. memcpy(&lastSysCPU, &fsys, sizeof(FILETIME));
  42. memcpy(&lastUserCPU, &fuser, sizeof(FILETIME));
  43. }
  44. #endif
  45. // constant
  46. double Prozess::getCPU() const
  47. {
  48. #ifdef WIN32
  49. FILETIME ftime, fsys, fuser;
  50. ULARGE_INTEGER now, sys, user;
  51. double percent;
  52. GetSystemTimeAsFileTime(&ftime);
  53. memcpy(&now, &ftime, sizeof(FILETIME));
  54. GetProcessTimes(pHandle, &ftime, &ftime, &fsys, &fuser);
  55. memcpy(&sys, &fsys, sizeof(FILETIME));
  56. memcpy(&user, &fuser, sizeof(FILETIME));
  57. percent = (double)((sys.QuadPart - lastSysCPU.QuadPart)
  58. + (user.QuadPart - lastUserCPU.QuadPart));
  59. percent /= (now.QuadPart - lastCPU.QuadPart);
  60. percent /= numProcessors;
  61. memcpy((void*)&lastCPU, (void*)&now, sizeof(now));
  62. memcpy((void*)&lastUserCPU, (void*)&user, sizeof(now));
  63. memcpy((void*)&lastSysCPU, (void*)&sys, sizeof(now));
  64. return percent * 100;
  65. #else
  66. return 0; // usage.ru_stime usage.ru_utime;
  67. #endif
  68. }
  69. __int64 Prozess::getMem() const
  70. {
  71. #ifdef WIN32
  72. PROCESS_MEMORY_COUNTERS pMemCountr;
  73. pMemCountr = PROCESS_MEMORY_COUNTERS();
  74. if (GetProcessMemoryInfo(pHandle, &pMemCountr, sizeof(pMemCountr)))
  75. return pMemCountr.WorkingSetSize;
  76. return 0;
  77. #else
  78. using std::ifstream;
  79. using std::ios_base;
  80. using std::string;
  81. // 'file' stat seems to give the most reliable results
  82. //
  83. ifstream stat_stream("/proc/self/stat", ios_base::in);
  84. // dummy vars for leading entries in stat that we don't care about
  85. //
  86. string pid, comm, state, ppid, pgrp, session, tty_nr;
  87. string tpgid, flags, minflt, cminflt, majflt, cmajflt;
  88. string utime, stime, cutime, cstime, priority, nice;
  89. string O, itrealvalue, starttime;
  90. // the two fields we want
  91. //
  92. unsigned long vsize;
  93. long rss;
  94. stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
  95. >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt >> utime
  96. >> stime >> cutime >> cstime >> priority >> nice >> O >> itrealvalue
  97. >> starttime >> vsize >> rss; // don't care about the rest
  98. stat_stream.close();
  99. long page_size_kb = sysconf(_SC_PAGE_SIZE)
  100. / 1024; // in case x86-64 is configured to use 2MB pages
  101. return rss * page_size_kb;
  102. #endif
  103. }
  104. #ifdef WIN32
  105. MemoryInfo Prozess::getMemInfo() const
  106. {
  107. PROCESS_MEMORY_COUNTERS pMemCountr;
  108. pMemCountr = PROCESS_MEMORY_COUNTERS();
  109. int result = GetProcessMemoryInfo(pHandle, &pMemCountr, sizeof(pMemCountr));
  110. MemoryInfo ret;
  111. ZeroMemory(&ret, sizeof(ret));
  112. if (result)
  113. {
  114. ret.ausgelagerteFehler = pMemCountr.PageFaultCount;
  115. ret.ausgelagerterPool = pMemCountr.QuotaPagedPoolUsage;
  116. ret.ausgelagerterSpeicher = pMemCountr.WorkingSetSize;
  117. ret.höchsteAusgelagerterSpeicher = pMemCountr.PeakWorkingSetSize;
  118. ret.höchsterAusgelagerterPool = pMemCountr.QuotaPeakPagedPoolUsage;
  119. ret.höchsterNichtAusgelagerterPool
  120. = pMemCountr.QuotaPeakNonPagedPoolUsage;
  121. ret.höchsterVorreservierterSpeicher = pMemCountr.PeakPagefileUsage;
  122. ret.nichtAusgelagerterPool = pMemCountr.QuotaNonPagedPoolUsage;
  123. ret.vorreservierterSpeicher = pMemCountr.PagefileUsage;
  124. return ret;
  125. }
  126. return ret;
  127. }
  128. int Prozess::getThreadAnzahl() const
  129. {
  130. int ret = 0;
  131. DWORD processID = GetCurrentProcessId();
  132. HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, processID);
  133. if (snap != INVALID_HANDLE_VALUE)
  134. {
  135. PROCESSENTRY32 entry = {0};
  136. entry.dwSize = sizeof(PROCESSENTRY32);
  137. if (Process32First(snap, &entry))
  138. {
  139. do
  140. {
  141. if (entry.th32ProcessID == processID)
  142. {
  143. ret = entry.cntThreads;
  144. break;
  145. }
  146. } while (Process32Next(snap, &entry));
  147. }
  148. CloseHandle(snap);
  149. }
  150. return ret;
  151. }
  152. #endif