perfdata.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * ReactOS Task Manager
  3. *
  4. * perfdata.c
  5. *
  6. * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <windows.h>
  25. #include <commctrl.h>
  26. #include <winnt.h>
  27. #include "taskmgr.h"
  28. #include "perfdata.h"
  29. static CRITICAL_SECTION PerfDataCriticalSection;
  30. static PPERFDATA pPerfDataOld = NULL; /* Older perf data (saved to establish delta values) */
  31. static PPERFDATA pPerfData = NULL; /* Most recent copy of perf data */
  32. static ULONG ProcessCountOld = 0;
  33. static ULONG ProcessCount = 0;
  34. static double dbIdleTime;
  35. static double dbKernelTime;
  36. static double dbSystemTime;
  37. static LARGE_INTEGER liOldIdleTime = {{0,0}};
  38. static double OldKernelTime = 0;
  39. static LARGE_INTEGER liOldSystemTime = {{0,0}};
  40. static SYSTEM_PERFORMANCE_INFORMATION SystemPerfInfo;
  41. static SYSTEM_BASIC_INFORMATION SystemBasicInfo;
  42. static SYSTEM_CACHE_INFORMATION SystemCacheInfo;
  43. static SYSTEM_HANDLE_INFORMATION SystemHandleInfo;
  44. static PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SystemProcessorTimeInfo = NULL;
  45. static size_t size_diff(size_t x, size_t y)
  46. {
  47. return x > y ? x - y : y - x;
  48. }
  49. BOOL PerfDataInitialize(void)
  50. {
  51. LONG status;
  52. InitializeCriticalSection(&PerfDataCriticalSection);
  53. /*
  54. * Get number of processors in the system
  55. */
  56. status = NtQuerySystemInformation(SystemBasicInformation, &SystemBasicInfo, sizeof(SystemBasicInfo), NULL);
  57. if (status != NO_ERROR)
  58. return FALSE;
  59. return TRUE;
  60. }
  61. void PerfDataRefresh(void)
  62. {
  63. ULONG ulSize;
  64. LONG status;
  65. LPBYTE pBuffer;
  66. ULONG BufferSize;
  67. PSYSTEM_PROCESS_INFORMATION pSPI;
  68. PPERFDATA pPDOld;
  69. ULONG Idx, Idx2;
  70. HANDLE hProcess;
  71. HANDLE hProcessToken;
  72. WCHAR wszTemp[MAX_PATH];
  73. DWORD dwSize;
  74. SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
  75. SYSTEM_TIMEOFDAY_INFORMATION SysTimeInfo;
  76. SYSTEM_CACHE_INFORMATION SysCacheInfo;
  77. LPBYTE SysHandleInfoData;
  78. SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *SysProcessorTimeInfo;
  79. double CurrentKernelTime;
  80. /* Get new system time */
  81. status = NtQuerySystemInformation(SystemTimeOfDayInformation, &SysTimeInfo, sizeof(SysTimeInfo), 0);
  82. if (status != NO_ERROR)
  83. return;
  84. /* Get new CPU's idle time */
  85. status = NtQuerySystemInformation(SystemPerformanceInformation, &SysPerfInfo, sizeof(SysPerfInfo), NULL);
  86. if (status != NO_ERROR)
  87. return;
  88. /* Get system cache information */
  89. status = NtQuerySystemInformation(SystemFileCacheInformation, &SysCacheInfo, sizeof(SysCacheInfo), NULL);
  90. if (status != NO_ERROR)
  91. return;
  92. /* Get processor time information */
  93. SysProcessorTimeInfo = HeapAlloc(GetProcessHeap(), 0,
  94. sizeof(*SysProcessorTimeInfo) * SystemBasicInfo.NumberOfProcessors);
  95. status = NtQuerySystemInformation(SystemProcessorPerformanceInformation, SysProcessorTimeInfo,
  96. sizeof(*SysProcessorTimeInfo) * SystemBasicInfo.NumberOfProcessors, &ulSize);
  97. if (status != NO_ERROR) {
  98. HeapFree(GetProcessHeap(), 0, SysProcessorTimeInfo);
  99. return;
  100. }
  101. /* Get handle information
  102. * We don't know how much data there is so just keep
  103. * increasing the buffer size until the call succeeds
  104. */
  105. BufferSize = 0;
  106. do
  107. {
  108. BufferSize += 0x10000;
  109. SysHandleInfoData = HeapAlloc(GetProcessHeap(), 0, BufferSize);
  110. status = NtQuerySystemInformation(SystemHandleInformation, SysHandleInfoData, BufferSize, &ulSize);
  111. if (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/) {
  112. HeapFree(GetProcessHeap(), 0, SysHandleInfoData);
  113. }
  114. } while (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/);
  115. /* Get process information
  116. * We don't know how much data there is so just keep
  117. * increasing the buffer size until the call succeeds
  118. */
  119. BufferSize = 0;
  120. do
  121. {
  122. BufferSize += 0x10000;
  123. pBuffer = HeapAlloc(GetProcessHeap(), 0, BufferSize);
  124. status = NtQuerySystemInformation(SystemProcessInformation, pBuffer, BufferSize, &ulSize);
  125. if (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/) {
  126. HeapFree(GetProcessHeap(), 0, pBuffer);
  127. }
  128. } while (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/);
  129. EnterCriticalSection(&PerfDataCriticalSection);
  130. /*
  131. * Save system performance info
  132. */
  133. memcpy(&SystemPerfInfo, &SysPerfInfo, sizeof(SYSTEM_PERFORMANCE_INFORMATION));
  134. /*
  135. * Save system cache info
  136. */
  137. memcpy(&SystemCacheInfo, &SysCacheInfo, sizeof(SYSTEM_CACHE_INFORMATION));
  138. /*
  139. * Save system processor time info
  140. */
  141. HeapFree(GetProcessHeap(), 0, SystemProcessorTimeInfo);
  142. SystemProcessorTimeInfo = SysProcessorTimeInfo;
  143. /*
  144. * Save system handle info
  145. */
  146. memcpy(&SystemHandleInfo, SysHandleInfoData, sizeof(SYSTEM_HANDLE_INFORMATION));
  147. HeapFree(GetProcessHeap(), 0, SysHandleInfoData);
  148. for (CurrentKernelTime=0, Idx=0; Idx<SystemBasicInfo.NumberOfProcessors; Idx++) {
  149. CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].KernelTime);
  150. CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].Reserved1[0]);
  151. CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].Reserved1[1]);
  152. }
  153. /* If it's a first call - skip idle time calcs */
  154. if (liOldIdleTime.QuadPart != 0) {
  155. /* CurrentValue = NewValue - OldValue */
  156. dbIdleTime = Li2Double(SysPerfInfo.IdleTime) - Li2Double(liOldIdleTime);
  157. dbKernelTime = CurrentKernelTime - OldKernelTime;
  158. dbSystemTime = Li2Double(SysTimeInfo.SystemTime) - Li2Double(liOldSystemTime);
  159. /* CurrentCpuIdle = IdleTime / SystemTime */
  160. dbIdleTime = dbIdleTime / dbSystemTime;
  161. dbKernelTime = dbKernelTime / dbSystemTime;
  162. /* CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors */
  163. dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
  164. dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
  165. }
  166. /* Store new CPU's idle and system time */
  167. liOldIdleTime = SysPerfInfo.IdleTime;
  168. liOldSystemTime = SysTimeInfo.SystemTime;
  169. OldKernelTime = CurrentKernelTime;
  170. /* Determine the process count
  171. * We loop through the data we got from NtQuerySystemInformation
  172. * and count how many structures there are (until RelativeOffset is 0)
  173. */
  174. ProcessCountOld = ProcessCount;
  175. ProcessCount = 0;
  176. pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
  177. while (pSPI) {
  178. ProcessCount++;
  179. if (pSPI->NextEntryOffset == 0)
  180. break;
  181. pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->NextEntryOffset);
  182. }
  183. /* Now alloc a new PERFDATA array and fill in the data */
  184. HeapFree(GetProcessHeap(), 0, pPerfDataOld);
  185. pPerfDataOld = pPerfData;
  186. pPerfData = HeapAlloc(GetProcessHeap(), 0, sizeof(PERFDATA) * ProcessCount);
  187. pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
  188. for (Idx=0; Idx<ProcessCount; Idx++) {
  189. /* Get the old perf data for this process (if any) */
  190. /* so that we can establish delta values */
  191. pPDOld = NULL;
  192. for (Idx2=0; Idx2<ProcessCountOld; Idx2++) {
  193. if (pPerfDataOld[Idx2].ProcessId == (DWORD_PTR)pSPI->UniqueProcessId) {
  194. pPDOld = &pPerfDataOld[Idx2];
  195. break;
  196. }
  197. }
  198. /* Clear out process perf data structure */
  199. memset(&pPerfData[Idx], 0, sizeof(PERFDATA));
  200. if (pSPI->ProcessName.Buffer)
  201. lstrcpyW(pPerfData[Idx].ImageName, pSPI->ProcessName.Buffer);
  202. else
  203. {
  204. WCHAR idleW[255];
  205. LoadStringW(hInst, IDS_SYSTEM_IDLE_PROCESS, idleW, ARRAY_SIZE(idleW));
  206. lstrcpyW(pPerfData[Idx].ImageName, idleW );
  207. }
  208. pPerfData[Idx].ProcessId = (DWORD_PTR)pSPI->UniqueProcessId;
  209. if (pPDOld) {
  210. double CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
  211. double OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
  212. double CpuTime = (CurTime - OldTime) / dbSystemTime;
  213. CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
  214. pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
  215. }
  216. pPerfData[Idx].CPUTime.QuadPart = pSPI->UserTime.QuadPart + pSPI->KernelTime.QuadPart;
  217. pPerfData[Idx].vmCounters.WorkingSetSize = pSPI->vmCounters.WorkingSetSize;
  218. pPerfData[Idx].vmCounters.PeakWorkingSetSize = pSPI->vmCounters.PeakWorkingSetSize;
  219. if (pPDOld)
  220. pPerfData[Idx].WorkingSetSizeDelta = size_diff(pSPI->vmCounters.WorkingSetSize, pPDOld->vmCounters.WorkingSetSize);
  221. else
  222. pPerfData[Idx].WorkingSetSizeDelta = 0;
  223. pPerfData[Idx].vmCounters.PageFaultCount = pSPI->vmCounters.PageFaultCount;
  224. if (pPDOld)
  225. pPerfData[Idx].PageFaultCountDelta = size_diff(pSPI->vmCounters.PageFaultCount, pPDOld->vmCounters.PageFaultCount);
  226. else
  227. pPerfData[Idx].PageFaultCountDelta = 0;
  228. pPerfData[Idx].vmCounters.VirtualSize = pSPI->vmCounters.VirtualSize;
  229. pPerfData[Idx].vmCounters.QuotaPagedPoolUsage = pSPI->vmCounters.QuotaPagedPoolUsage;
  230. pPerfData[Idx].vmCounters.QuotaNonPagedPoolUsage = pSPI->vmCounters.QuotaNonPagedPoolUsage;
  231. pPerfData[Idx].BasePriority = pSPI->dwBasePriority;
  232. pPerfData[Idx].HandleCount = pSPI->HandleCount;
  233. pPerfData[Idx].ThreadCount = pSPI->dwThreadCount;
  234. pPerfData[Idx].SessionId = pSPI->SessionId;
  235. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, (DWORD_PTR)pSPI->UniqueProcessId);
  236. if (hProcess) {
  237. if (OpenProcessToken(hProcess, TOKEN_QUERY|TOKEN_DUPLICATE|TOKEN_IMPERSONATE, &hProcessToken)) {
  238. ImpersonateLoggedOnUser(hProcessToken);
  239. memset(wszTemp, 0, sizeof(wszTemp));
  240. dwSize = MAX_PATH;
  241. GetUserNameW(wszTemp, &dwSize);
  242. RevertToSelf();
  243. CloseHandle(hProcessToken);
  244. }
  245. pPerfData[Idx].USERObjectCount = GetGuiResources(hProcess, GR_USEROBJECTS);
  246. pPerfData[Idx].GDIObjectCount = GetGuiResources(hProcess, GR_GDIOBJECTS);
  247. GetProcessIoCounters(hProcess, &pPerfData[Idx].IOCounters);
  248. IsWow64Process(hProcess, &pPerfData[Idx].Wow64Process);
  249. CloseHandle(hProcess);
  250. }
  251. pPerfData[Idx].UserTime.QuadPart = pSPI->UserTime.QuadPart;
  252. pPerfData[Idx].KernelTime.QuadPart = pSPI->KernelTime.QuadPart;
  253. pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->NextEntryOffset);
  254. }
  255. HeapFree(GetProcessHeap(), 0, pBuffer);
  256. LeaveCriticalSection(&PerfDataCriticalSection);
  257. }
  258. ULONG PerfDataGetProcessCount(void)
  259. {
  260. return ProcessCount;
  261. }
  262. ULONG PerfDataGetProcessorUsage(void)
  263. {
  264. if( dbIdleTime < 0.0 )
  265. return 0;
  266. if( dbIdleTime > 100.0 )
  267. return 100;
  268. return (ULONG)dbIdleTime;
  269. }
  270. ULONG PerfDataGetProcessorSystemUsage(void)
  271. {
  272. if( dbKernelTime < 0.0 )
  273. return 0;
  274. if( dbKernelTime > 100.0 )
  275. return 100;
  276. return (ULONG)dbKernelTime;
  277. }
  278. BOOL PerfDataGetImageName(ULONG Index, LPWSTR lpImageName, int nMaxCount)
  279. {
  280. static const WCHAR proc32W[] = {' ','*','3','2',0};
  281. BOOL bSuccessful;
  282. EnterCriticalSection(&PerfDataCriticalSection);
  283. if (Index < ProcessCount) {
  284. lstrcpynW(lpImageName, pPerfData[Index].ImageName, nMaxCount);
  285. if (pPerfData[Index].Wow64Process &&
  286. nMaxCount - lstrlenW(lpImageName) > 4 /* =lstrlenW(proc32W) */)
  287. lstrcatW(lpImageName, proc32W);
  288. bSuccessful = TRUE;
  289. } else {
  290. bSuccessful = FALSE;
  291. }
  292. LeaveCriticalSection(&PerfDataCriticalSection);
  293. return bSuccessful;
  294. }
  295. ULONG PerfDataGetProcessId(ULONG Index)
  296. {
  297. ULONG ProcessId;
  298. EnterCriticalSection(&PerfDataCriticalSection);
  299. if (Index < ProcessCount)
  300. ProcessId = pPerfData[Index].ProcessId;
  301. else
  302. ProcessId = 0;
  303. LeaveCriticalSection(&PerfDataCriticalSection);
  304. return ProcessId;
  305. }
  306. BOOL PerfDataGetUserName(ULONG Index, LPWSTR lpUserName, int nMaxCount)
  307. {
  308. BOOL bSuccessful;
  309. EnterCriticalSection(&PerfDataCriticalSection);
  310. if (Index < ProcessCount) {
  311. lstrcpynW(lpUserName, pPerfData[Index].UserName, nMaxCount);
  312. bSuccessful = TRUE;
  313. } else {
  314. bSuccessful = FALSE;
  315. }
  316. LeaveCriticalSection(&PerfDataCriticalSection);
  317. return bSuccessful;
  318. }
  319. ULONG PerfDataGetSessionId(ULONG Index)
  320. {
  321. ULONG SessionId;
  322. EnterCriticalSection(&PerfDataCriticalSection);
  323. if (Index < ProcessCount)
  324. SessionId = pPerfData[Index].SessionId;
  325. else
  326. SessionId = 0;
  327. LeaveCriticalSection(&PerfDataCriticalSection);
  328. return SessionId;
  329. }
  330. ULONG PerfDataGetCPUUsage(ULONG Index)
  331. {
  332. ULONG CpuUsage;
  333. EnterCriticalSection(&PerfDataCriticalSection);
  334. if (Index < ProcessCount)
  335. CpuUsage = pPerfData[Index].CPUUsage;
  336. else
  337. CpuUsage = 0;
  338. LeaveCriticalSection(&PerfDataCriticalSection);
  339. return CpuUsage;
  340. }
  341. TIME PerfDataGetCPUTime(ULONG Index)
  342. {
  343. TIME CpuTime = {{0,0}};
  344. EnterCriticalSection(&PerfDataCriticalSection);
  345. if (Index < ProcessCount)
  346. CpuTime = pPerfData[Index].CPUTime;
  347. LeaveCriticalSection(&PerfDataCriticalSection);
  348. return CpuTime;
  349. }
  350. ULONG PerfDataGetWorkingSetSizeBytes(ULONG Index)
  351. {
  352. ULONG WorkingSetSizeBytes;
  353. EnterCriticalSection(&PerfDataCriticalSection);
  354. if (Index < ProcessCount)
  355. WorkingSetSizeBytes = pPerfData[Index].vmCounters.WorkingSetSize;
  356. else
  357. WorkingSetSizeBytes = 0;
  358. LeaveCriticalSection(&PerfDataCriticalSection);
  359. return WorkingSetSizeBytes;
  360. }
  361. ULONG PerfDataGetPeakWorkingSetSizeBytes(ULONG Index)
  362. {
  363. ULONG PeakWorkingSetSizeBytes;
  364. EnterCriticalSection(&PerfDataCriticalSection);
  365. if (Index < ProcessCount)
  366. PeakWorkingSetSizeBytes = pPerfData[Index].vmCounters.PeakWorkingSetSize;
  367. else
  368. PeakWorkingSetSizeBytes = 0;
  369. LeaveCriticalSection(&PerfDataCriticalSection);
  370. return PeakWorkingSetSizeBytes;
  371. }
  372. ULONG PerfDataGetWorkingSetSizeDelta(ULONG Index)
  373. {
  374. ULONG WorkingSetSizeDelta;
  375. EnterCriticalSection(&PerfDataCriticalSection);
  376. if (Index < ProcessCount)
  377. WorkingSetSizeDelta = pPerfData[Index].WorkingSetSizeDelta;
  378. else
  379. WorkingSetSizeDelta = 0;
  380. LeaveCriticalSection(&PerfDataCriticalSection);
  381. return WorkingSetSizeDelta;
  382. }
  383. ULONG PerfDataGetPageFaultCount(ULONG Index)
  384. {
  385. ULONG PageFaultCount;
  386. EnterCriticalSection(&PerfDataCriticalSection);
  387. if (Index < ProcessCount)
  388. PageFaultCount = pPerfData[Index].vmCounters.PageFaultCount;
  389. else
  390. PageFaultCount = 0;
  391. LeaveCriticalSection(&PerfDataCriticalSection);
  392. return PageFaultCount;
  393. }
  394. ULONG PerfDataGetPageFaultCountDelta(ULONG Index)
  395. {
  396. ULONG PageFaultCountDelta;
  397. EnterCriticalSection(&PerfDataCriticalSection);
  398. if (Index < ProcessCount)
  399. PageFaultCountDelta = pPerfData[Index].PageFaultCountDelta;
  400. else
  401. PageFaultCountDelta = 0;
  402. LeaveCriticalSection(&PerfDataCriticalSection);
  403. return PageFaultCountDelta;
  404. }
  405. ULONG PerfDataGetVirtualMemorySizeBytes(ULONG Index)
  406. {
  407. ULONG VirtualMemorySizeBytes;
  408. EnterCriticalSection(&PerfDataCriticalSection);
  409. if (Index < ProcessCount)
  410. VirtualMemorySizeBytes = pPerfData[Index].vmCounters.VirtualSize;
  411. else
  412. VirtualMemorySizeBytes = 0;
  413. LeaveCriticalSection(&PerfDataCriticalSection);
  414. return VirtualMemorySizeBytes;
  415. }
  416. ULONG PerfDataGetPagedPoolUsagePages(ULONG Index)
  417. {
  418. ULONG PagedPoolUsagePages;
  419. EnterCriticalSection(&PerfDataCriticalSection);
  420. if (Index < ProcessCount)
  421. PagedPoolUsagePages = pPerfData[Index].vmCounters.QuotaPagedPoolUsage;
  422. else
  423. PagedPoolUsagePages = 0;
  424. LeaveCriticalSection(&PerfDataCriticalSection);
  425. return PagedPoolUsagePages;
  426. }
  427. ULONG PerfDataGetNonPagedPoolUsagePages(ULONG Index)
  428. {
  429. ULONG NonPagedPoolUsagePages;
  430. EnterCriticalSection(&PerfDataCriticalSection);
  431. if (Index < ProcessCount)
  432. NonPagedPoolUsagePages = pPerfData[Index].vmCounters.QuotaNonPagedPoolUsage;
  433. else
  434. NonPagedPoolUsagePages = 0;
  435. LeaveCriticalSection(&PerfDataCriticalSection);
  436. return NonPagedPoolUsagePages;
  437. }
  438. ULONG PerfDataGetBasePriority(ULONG Index)
  439. {
  440. ULONG BasePriority;
  441. EnterCriticalSection(&PerfDataCriticalSection);
  442. if (Index < ProcessCount)
  443. BasePriority = pPerfData[Index].BasePriority;
  444. else
  445. BasePriority = 0;
  446. LeaveCriticalSection(&PerfDataCriticalSection);
  447. return BasePriority;
  448. }
  449. ULONG PerfDataGetHandleCount(ULONG Index)
  450. {
  451. ULONG HandleCount;
  452. EnterCriticalSection(&PerfDataCriticalSection);
  453. if (Index < ProcessCount)
  454. HandleCount = pPerfData[Index].HandleCount;
  455. else
  456. HandleCount = 0;
  457. LeaveCriticalSection(&PerfDataCriticalSection);
  458. return HandleCount;
  459. }
  460. ULONG PerfDataGetThreadCount(ULONG Index)
  461. {
  462. ULONG ThreadCount;
  463. EnterCriticalSection(&PerfDataCriticalSection);
  464. if (Index < ProcessCount)
  465. ThreadCount = pPerfData[Index].ThreadCount;
  466. else
  467. ThreadCount = 0;
  468. LeaveCriticalSection(&PerfDataCriticalSection);
  469. return ThreadCount;
  470. }
  471. ULONG PerfDataGetUSERObjectCount(ULONG Index)
  472. {
  473. ULONG USERObjectCount;
  474. EnterCriticalSection(&PerfDataCriticalSection);
  475. if (Index < ProcessCount)
  476. USERObjectCount = pPerfData[Index].USERObjectCount;
  477. else
  478. USERObjectCount = 0;
  479. LeaveCriticalSection(&PerfDataCriticalSection);
  480. return USERObjectCount;
  481. }
  482. ULONG PerfDataGetGDIObjectCount(ULONG Index)
  483. {
  484. ULONG GDIObjectCount;
  485. EnterCriticalSection(&PerfDataCriticalSection);
  486. if (Index < ProcessCount)
  487. GDIObjectCount = pPerfData[Index].GDIObjectCount;
  488. else
  489. GDIObjectCount = 0;
  490. LeaveCriticalSection(&PerfDataCriticalSection);
  491. return GDIObjectCount;
  492. }
  493. BOOL PerfDataGetIOCounters(ULONG Index, PIO_COUNTERS pIoCounters)
  494. {
  495. BOOL bSuccessful;
  496. EnterCriticalSection(&PerfDataCriticalSection);
  497. if (Index < ProcessCount)
  498. {
  499. memcpy(pIoCounters, &pPerfData[Index].IOCounters, sizeof(IO_COUNTERS));
  500. bSuccessful = TRUE;
  501. }
  502. else
  503. bSuccessful = FALSE;
  504. LeaveCriticalSection(&PerfDataCriticalSection);
  505. return bSuccessful;
  506. }
  507. ULONG PerfDataGetCommitChargeTotalK(void)
  508. {
  509. ULONG Total;
  510. ULONG PageSize;
  511. EnterCriticalSection(&PerfDataCriticalSection);
  512. Total = SystemPerfInfo.TotalCommittedPages;
  513. PageSize = SystemBasicInfo.PageSize;
  514. LeaveCriticalSection(&PerfDataCriticalSection);
  515. Total = Total * (PageSize / 1024);
  516. return Total;
  517. }
  518. ULONG PerfDataGetCommitChargeLimitK(void)
  519. {
  520. ULONG Limit;
  521. ULONG PageSize;
  522. EnterCriticalSection(&PerfDataCriticalSection);
  523. Limit = SystemPerfInfo.TotalCommitLimit;
  524. PageSize = SystemBasicInfo.PageSize;
  525. LeaveCriticalSection(&PerfDataCriticalSection);
  526. Limit = Limit * (PageSize / 1024);
  527. return Limit;
  528. }
  529. ULONG PerfDataGetCommitChargePeakK(void)
  530. {
  531. ULONG Peak;
  532. ULONG PageSize;
  533. EnterCriticalSection(&PerfDataCriticalSection);
  534. Peak = SystemPerfInfo.PeakCommitment;
  535. PageSize = SystemBasicInfo.PageSize;
  536. LeaveCriticalSection(&PerfDataCriticalSection);
  537. Peak = Peak * (PageSize / 1024);
  538. return Peak;
  539. }
  540. ULONG PerfDataGetKernelMemoryTotalK(void)
  541. {
  542. ULONG Total;
  543. ULONG Paged;
  544. ULONG NonPaged;
  545. ULONG PageSize;
  546. EnterCriticalSection(&PerfDataCriticalSection);
  547. Paged = SystemPerfInfo.PagedPoolUsage;
  548. NonPaged = SystemPerfInfo.NonPagedPoolUsage;
  549. PageSize = SystemBasicInfo.PageSize;
  550. LeaveCriticalSection(&PerfDataCriticalSection);
  551. Paged = Paged * (PageSize / 1024);
  552. NonPaged = NonPaged * (PageSize / 1024);
  553. Total = Paged + NonPaged;
  554. return Total;
  555. }
  556. ULONG PerfDataGetKernelMemoryPagedK(void)
  557. {
  558. ULONG Paged;
  559. ULONG PageSize;
  560. EnterCriticalSection(&PerfDataCriticalSection);
  561. Paged = SystemPerfInfo.PagedPoolUsage;
  562. PageSize = SystemBasicInfo.PageSize;
  563. LeaveCriticalSection(&PerfDataCriticalSection);
  564. Paged = Paged * (PageSize / 1024);
  565. return Paged;
  566. }
  567. ULONG PerfDataGetKernelMemoryNonPagedK(void)
  568. {
  569. ULONG NonPaged;
  570. ULONG PageSize;
  571. EnterCriticalSection(&PerfDataCriticalSection);
  572. NonPaged = SystemPerfInfo.NonPagedPoolUsage;
  573. PageSize = SystemBasicInfo.PageSize;
  574. LeaveCriticalSection(&PerfDataCriticalSection);
  575. NonPaged = NonPaged * (PageSize / 1024);
  576. return NonPaged;
  577. }
  578. ULONG PerfDataGetPhysicalMemoryTotalK(void)
  579. {
  580. ULONG Total;
  581. ULONG PageSize;
  582. EnterCriticalSection(&PerfDataCriticalSection);
  583. Total = SystemBasicInfo.MmNumberOfPhysicalPages;
  584. PageSize = SystemBasicInfo.PageSize;
  585. LeaveCriticalSection(&PerfDataCriticalSection);
  586. Total = Total * (PageSize / 1024);
  587. return Total;
  588. }
  589. ULONG PerfDataGetPhysicalMemoryAvailableK(void)
  590. {
  591. ULONG Available;
  592. ULONG PageSize;
  593. EnterCriticalSection(&PerfDataCriticalSection);
  594. Available = SystemPerfInfo.AvailablePages;
  595. PageSize = SystemBasicInfo.PageSize;
  596. LeaveCriticalSection(&PerfDataCriticalSection);
  597. Available = Available * (PageSize / 1024);
  598. return Available;
  599. }
  600. ULONG PerfDataGetPhysicalMemorySystemCacheK(void)
  601. {
  602. ULONG SystemCache;
  603. EnterCriticalSection(&PerfDataCriticalSection);
  604. SystemCache = SystemCacheInfo.CurrentSize;
  605. LeaveCriticalSection(&PerfDataCriticalSection);
  606. SystemCache = SystemCache / 1024;
  607. return SystemCache;
  608. }
  609. ULONG PerfDataGetSystemHandleCount(void)
  610. {
  611. ULONG HandleCount;
  612. EnterCriticalSection(&PerfDataCriticalSection);
  613. HandleCount = SystemHandleInfo.Count;
  614. LeaveCriticalSection(&PerfDataCriticalSection);
  615. return HandleCount;
  616. }
  617. ULONG PerfDataGetTotalThreadCount(void)
  618. {
  619. ULONG ThreadCount = 0;
  620. ULONG i;
  621. EnterCriticalSection(&PerfDataCriticalSection);
  622. for (i=0; i<ProcessCount; i++)
  623. {
  624. ThreadCount += pPerfData[i].ThreadCount;
  625. }
  626. LeaveCriticalSection(&PerfDataCriticalSection);
  627. return ThreadCount;
  628. }