Prozess.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "Prozess.h"
  2. #ifdef WIN32
  3. #include <Psapi.h>
  4. #include <tlHelp32.h>
  5. #else
  6. #include <sys/time.h>
  7. #include <fstream>
  8. #include <string>
  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::ios_base;
  79. using std::ifstream;
  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
  96. >> utime >> stime >> cutime >> cstime >> priority >> nice
  97. >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
  98. stat_stream.close();
  99. long page_size_kb = sysconf( _SC_PAGE_SIZE ) / 1024; // in case x86-64 is configured to use 2MB pages
  100. return rss * page_size_kb;
  101. #endif
  102. }
  103. #ifdef WIN32
  104. MemoryInfo Prozess::getMemInfo() const
  105. {
  106. PROCESS_MEMORY_COUNTERS pMemCountr;
  107. pMemCountr = PROCESS_MEMORY_COUNTERS();
  108. int result = GetProcessMemoryInfo( pHandle, &pMemCountr, sizeof( pMemCountr ) );
  109. MemoryInfo ret;
  110. ZeroMemory( &ret, sizeof( ret ) );
  111. if( result )
  112. {
  113. ret.ausgelagerteFehler = pMemCountr.PageFaultCount;
  114. ret.ausgelagerterPool = pMemCountr.QuotaPagedPoolUsage;
  115. ret.ausgelagerterSpeicher = pMemCountr.WorkingSetSize;
  116. ret.höchsteAusgelagerterSpeicher = pMemCountr.PeakWorkingSetSize;
  117. ret.höchsterAusgelagerterPool = pMemCountr.QuotaPeakPagedPoolUsage;
  118. ret.höchsterNichtAusgelagerterPool = pMemCountr.QuotaPeakNonPagedPoolUsage;
  119. ret.höchsterVorreservierterSpeicher = pMemCountr.PeakPagefileUsage;
  120. ret.nichtAusgelagerterPool = pMemCountr.QuotaNonPagedPoolUsage;
  121. ret.vorreservierterSpeicher = pMemCountr.PagefileUsage;
  122. return ret;
  123. }
  124. return ret;
  125. }
  126. int Prozess::getThreadAnzahl() const
  127. {
  128. int ret = 0;
  129. DWORD processID = GetCurrentProcessId();
  130. HANDLE snap = CreateToolhelp32Snapshot( TH32CS_SNAPALL, processID );
  131. if( snap != INVALID_HANDLE_VALUE )
  132. {
  133. PROCESSENTRY32 entry = { 0 };
  134. entry.dwSize = sizeof( PROCESSENTRY32 );
  135. if( Process32First( snap, &entry ) )
  136. {
  137. do
  138. {
  139. if( entry.th32ProcessID == processID )
  140. {
  141. ret = entry.cntThreads;
  142. break;
  143. }
  144. } while( Process32Next( snap, &entry ) );
  145. }
  146. CloseHandle( snap );
  147. }
  148. return ret;
  149. }
  150. #endif