msvcrt/tests: Fix the %n format test for Vista.
[wine.git] / programs / taskmgr / perfdata.c
blobcda872d7a9c1516c7580ba284fe8da38f70e34df
1 /*
2 * ReactOS Task Manager
4 * perfdata.c
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
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.
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.
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
23 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
24 #include <windows.h>
25 #include <commctrl.h>
26 #include <stdlib.h>
27 #include <malloc.h>
28 #include <memory.h>
29 #include <tchar.h>
30 #include <stdio.h>
31 #include <winnt.h>
32 #include "taskmgr.h"
33 #include "perfdata.h"
35 static PROCNTQSI NtQuerySystemInformation = NULL;
36 static PROCGGR pGetGuiResources = NULL;
37 static PROCGPIC pGetProcessIoCounters = NULL;
38 static CRITICAL_SECTION PerfDataCriticalSection;
39 static PPERFDATA pPerfDataOld = NULL; /* Older perf data (saved to establish delta values) */
40 static PPERFDATA pPerfData = NULL; /* Most recent copy of perf data */
41 static ULONG ProcessCountOld = 0;
42 static ULONG ProcessCount = 0;
43 static double dbIdleTime;
44 static double dbKernelTime;
45 static double dbSystemTime;
46 static LARGE_INTEGER liOldIdleTime = {{0,0}};
47 static double OldKernelTime = 0;
48 static LARGE_INTEGER liOldSystemTime = {{0,0}};
49 static SYSTEM_PERFORMANCE_INFORMATION SystemPerfInfo;
50 static SYSTEM_BASIC_INFORMATION SystemBasicInfo;
51 static SYSTEM_CACHE_INFORMATION SystemCacheInfo;
52 static SYSTEM_HANDLE_INFORMATION SystemHandleInfo;
53 static PSYSTEM_PROCESSORTIME_INFO SystemProcessorTimeInfo = NULL;
55 BOOL PerfDataInitialize(void)
57 LONG status;
59 NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(GetModuleHandle(_T("ntdll.dll")), "NtQuerySystemInformation");
60 pGetGuiResources = (PROCGGR)GetProcAddress(GetModuleHandle(_T("user32.dll")), "GetGuiResources");
61 pGetProcessIoCounters = (PROCGPIC)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetProcessIoCounters");
63 InitializeCriticalSection(&PerfDataCriticalSection);
65 if (!NtQuerySystemInformation)
66 return FALSE;
69 * Get number of processors in the system
71 status = NtQuerySystemInformation(SystemBasicInformation, &SystemBasicInfo, sizeof(SystemBasicInfo), NULL);
72 if (status != NO_ERROR)
73 return FALSE;
75 return TRUE;
78 void PerfDataUninitialize(void)
80 NtQuerySystemInformation = NULL;
82 DeleteCriticalSection(&PerfDataCriticalSection);
85 void PerfDataRefresh(void)
87 ULONG ulSize;
88 LONG status;
89 LPBYTE pBuffer;
90 ULONG BufferSize;
91 PSYSTEM_PROCESS_INFORMATION pSPI;
92 PPERFDATA pPDOld;
93 ULONG Idx, Idx2;
94 HANDLE hProcess;
95 HANDLE hProcessToken;
96 TCHAR szTemp[MAX_PATH];
97 DWORD dwSize;
98 SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
99 SYSTEM_TIME_INFORMATION SysTimeInfo;
100 SYSTEM_CACHE_INFORMATION SysCacheInfo;
101 LPBYTE SysHandleInfoData;
102 PSYSTEM_PROCESSORTIME_INFO SysProcessorTimeInfo;
103 double CurrentKernelTime;
106 if (!NtQuerySystemInformation)
107 return;
109 /* Get new system time */
110 status = NtQuerySystemInformation(SystemTimeInformation, &SysTimeInfo, sizeof(SysTimeInfo), 0);
111 if (status != NO_ERROR)
112 return;
114 /* Get new CPU's idle time */
115 status = NtQuerySystemInformation(SystemPerformanceInformation, &SysPerfInfo, sizeof(SysPerfInfo), NULL);
116 if (status != NO_ERROR)
117 return;
119 /* Get system cache information */
120 status = NtQuerySystemInformation(SystemCacheInformation, &SysCacheInfo, sizeof(SysCacheInfo), NULL);
121 if (status != NO_ERROR)
122 return;
124 /* Get processor time information */
125 SysProcessorTimeInfo = (PSYSTEM_PROCESSORTIME_INFO)malloc(sizeof(SYSTEM_PROCESSORTIME_INFO) * SystemBasicInfo.bKeNumberProcessors);
126 status = NtQuerySystemInformation(SystemProcessorTimeInformation, SysProcessorTimeInfo, sizeof(SYSTEM_PROCESSORTIME_INFO) * SystemBasicInfo.bKeNumberProcessors, &ulSize);
127 if (status != NO_ERROR) {
128 free(SysProcessorTimeInfo);
129 return;
132 /* Get handle information
133 * We don't know how much data there is so just keep
134 * increasing the buffer size until the call succeeds
136 BufferSize = 0;
139 BufferSize += 0x10000;
140 SysHandleInfoData = (LPBYTE)malloc(BufferSize);
142 status = NtQuerySystemInformation(SystemHandleInformation, SysHandleInfoData, BufferSize, &ulSize);
144 if (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/) {
145 free(SysHandleInfoData);
148 } while (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/);
150 /* Get process information
151 * We don't know how much data there is so just keep
152 * increasing the buffer size until the call succeeds
154 BufferSize = 0;
157 BufferSize += 0x10000;
158 pBuffer = (LPBYTE)malloc(BufferSize);
160 status = NtQuerySystemInformation(SystemProcessInformation, pBuffer, BufferSize, &ulSize);
162 if (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/) {
163 free(pBuffer);
166 } while (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/);
168 EnterCriticalSection(&PerfDataCriticalSection);
171 * Save system performance info
173 memcpy(&SystemPerfInfo, &SysPerfInfo, sizeof(SYSTEM_PERFORMANCE_INFORMATION));
176 * Save system cache info
178 memcpy(&SystemCacheInfo, &SysCacheInfo, sizeof(SYSTEM_CACHE_INFORMATION));
181 * Save system processor time info
183 free(SystemProcessorTimeInfo);
184 SystemProcessorTimeInfo = SysProcessorTimeInfo;
187 * Save system handle info
189 memcpy(&SystemHandleInfo, SysHandleInfoData, sizeof(SYSTEM_HANDLE_INFORMATION));
190 free(SysHandleInfoData);
192 for (CurrentKernelTime=0, Idx=0; Idx<SystemBasicInfo.bKeNumberProcessors; Idx++) {
193 CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].KernelTime);
194 CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].DpcTime);
195 CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].InterruptTime);
198 /* If it's a first call - skip idle time calcs */
199 if (liOldIdleTime.QuadPart != 0) {
200 /* CurrentValue = NewValue - OldValue */
201 dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
202 dbKernelTime = CurrentKernelTime - OldKernelTime;
203 dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);
205 /* CurrentCpuIdle = IdleTime / SystemTime */
206 dbIdleTime = dbIdleTime / dbSystemTime;
207 dbKernelTime = dbKernelTime / dbSystemTime;
209 /* CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors */
210 dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.bKeNumberProcessors; /* + 0.5; */
211 dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.bKeNumberProcessors; /* + 0.5; */
214 /* Store new CPU's idle and system time */
215 liOldIdleTime = SysPerfInfo.liIdleTime;
216 liOldSystemTime = SysTimeInfo.liKeSystemTime;
217 OldKernelTime = CurrentKernelTime;
219 /* Determine the process count
220 * We loop through the data we got from NtQuerySystemInformation
221 * and count how many structures there are (until RelativeOffset is 0)
223 ProcessCountOld = ProcessCount;
224 ProcessCount = 0;
225 pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
226 while (pSPI) {
227 ProcessCount++;
228 if (pSPI->RelativeOffset == 0)
229 break;
230 pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->RelativeOffset);
233 /* Now alloc a new PERFDATA array and fill in the data */
234 free(pPerfDataOld);
235 pPerfDataOld = pPerfData;
236 pPerfData = (PPERFDATA)malloc(sizeof(PERFDATA) * ProcessCount);
237 pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
238 for (Idx=0; Idx<ProcessCount; Idx++) {
239 /* Get the old perf data for this process (if any) */
240 /* so that we can establish delta values */
241 pPDOld = NULL;
242 for (Idx2=0; Idx2<ProcessCountOld; Idx2++) {
243 if (pPerfDataOld[Idx2].ProcessId == pSPI->ProcessId) {
244 pPDOld = &pPerfDataOld[Idx2];
245 break;
249 /* Clear out process perf data structure */
250 memset(&pPerfData[Idx], 0, sizeof(PERFDATA));
252 if (pSPI->Name.Buffer)
253 lstrcpyW(pPerfData[Idx].ImageName, pSPI->Name.Buffer);
254 else
256 static const WCHAR idleW[] = {'S','y','s','t','e','m',' ','I','d','l','e',' ','P','r','o','c','e','s','s',0};
257 lstrcpyW(pPerfData[Idx].ImageName, idleW );
260 pPerfData[Idx].ProcessId = pSPI->ProcessId;
262 if (pPDOld) {
263 double CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
264 double OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
265 double CpuTime = (CurTime - OldTime) / dbSystemTime;
266 CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.bKeNumberProcessors; /* + 0.5; */
267 pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
269 pPerfData[Idx].CPUTime.QuadPart = pSPI->UserTime.QuadPart + pSPI->KernelTime.QuadPart;
270 pPerfData[Idx].WorkingSetSizeBytes = pSPI->TotalWorkingSetSizeBytes;
271 pPerfData[Idx].PeakWorkingSetSizeBytes = pSPI->PeakWorkingSetSizeBytes;
272 if (pPDOld)
273 pPerfData[Idx].WorkingSetSizeDelta = labs((LONG)pSPI->TotalWorkingSetSizeBytes - (LONG)pPDOld->WorkingSetSizeBytes);
274 else
275 pPerfData[Idx].WorkingSetSizeDelta = 0;
276 pPerfData[Idx].PageFaultCount = pSPI->PageFaultCount;
277 if (pPDOld)
278 pPerfData[Idx].PageFaultCountDelta = labs((LONG)pSPI->PageFaultCount - (LONG)pPDOld->PageFaultCount);
279 else
280 pPerfData[Idx].PageFaultCountDelta = 0;
281 pPerfData[Idx].VirtualMemorySizeBytes = pSPI->TotalVirtualSizeBytes;
282 pPerfData[Idx].PagedPoolUsagePages = pSPI->TotalPagedPoolUsagePages;
283 pPerfData[Idx].NonPagedPoolUsagePages = pSPI->TotalNonPagedPoolUsagePages;
284 pPerfData[Idx].BasePriority = pSPI->BasePriority;
285 pPerfData[Idx].HandleCount = pSPI->HandleCount;
286 pPerfData[Idx].ThreadCount = pSPI->ThreadCount;
287 pPerfData[Idx].SessionId = pSPI->SessionId;
289 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pSPI->ProcessId);
290 if (hProcess) {
291 if (OpenProcessToken(hProcess, TOKEN_QUERY|TOKEN_DUPLICATE|TOKEN_IMPERSONATE, &hProcessToken)) {
292 ImpersonateLoggedOnUser(hProcessToken);
293 memset(szTemp, 0, sizeof(TCHAR[MAX_PATH]));
294 dwSize = MAX_PATH;
295 GetUserName(szTemp, &dwSize);
296 #ifndef UNICODE
297 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szTemp, -1, pPerfData[Idx].UserName, MAX_PATH);
299 int MultiByteToWideChar(
300 UINT CodePage, // code page
301 DWORD dwFlags, // character-type options
302 LPCSTR lpMultiByteStr, // string to map
303 int cbMultiByte, // number of bytes in string
304 LPWSTR lpWideCharStr, // wide-character buffer
305 int cchWideChar // size of buffer
308 #endif
309 RevertToSelf();
310 CloseHandle(hProcessToken);
312 if (pGetGuiResources) {
313 pPerfData[Idx].USERObjectCount = pGetGuiResources(hProcess, GR_USEROBJECTS);
314 pPerfData[Idx].GDIObjectCount = pGetGuiResources(hProcess, GR_GDIOBJECTS);
316 if (pGetProcessIoCounters)
317 pGetProcessIoCounters(hProcess, &pPerfData[Idx].IOCounters);
318 CloseHandle(hProcess);
320 pPerfData[Idx].UserTime.QuadPart = pSPI->UserTime.QuadPart;
321 pPerfData[Idx].KernelTime.QuadPart = pSPI->KernelTime.QuadPart;
322 pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->RelativeOffset);
324 free(pBuffer);
325 LeaveCriticalSection(&PerfDataCriticalSection);
328 ULONG PerfDataGetProcessCount(void)
330 return ProcessCount;
333 ULONG PerfDataGetProcessorUsage(void)
335 if( dbIdleTime < 0.0 )
336 return 0;
337 if( dbIdleTime > 100.0 )
338 return 100;
339 return (ULONG)dbIdleTime;
342 ULONG PerfDataGetProcessorSystemUsage(void)
344 if( dbKernelTime < 0.0 )
345 return 0;
346 if( dbKernelTime > 100.0 )
347 return 100;
348 return (ULONG)dbKernelTime;
351 BOOL PerfDataGetImageName(ULONG Index, LPTSTR lpImageName, int nMaxCount)
353 BOOL bSuccessful;
355 EnterCriticalSection(&PerfDataCriticalSection);
357 if (Index < ProcessCount) {
358 #ifdef _UNICODE
359 wcsncpy(lpImageName, pPerfData[Index].ImageName, nMaxCount);
360 #else
361 WideCharToMultiByte(CP_ACP, 0, pPerfData[Index].ImageName, -1, lpImageName, nMaxCount, NULL, NULL);
362 #endif
364 bSuccessful = TRUE;
365 } else {
366 bSuccessful = FALSE;
368 LeaveCriticalSection(&PerfDataCriticalSection);
369 return bSuccessful;
372 ULONG PerfDataGetProcessId(ULONG Index)
374 ULONG ProcessId;
376 EnterCriticalSection(&PerfDataCriticalSection);
378 if (Index < ProcessCount)
379 ProcessId = pPerfData[Index].ProcessId;
380 else
381 ProcessId = 0;
383 LeaveCriticalSection(&PerfDataCriticalSection);
385 return ProcessId;
388 BOOL PerfDataGetUserName(ULONG Index, LPTSTR lpUserName, int nMaxCount)
390 BOOL bSuccessful;
392 EnterCriticalSection(&PerfDataCriticalSection);
394 if (Index < ProcessCount) {
395 #ifdef _UNICODE
396 wcsncpy(lpUserName, pPerfData[Index].UserName, nMaxCount);
397 #else
398 WideCharToMultiByte(CP_ACP, 0, pPerfData[Index].UserName, -1, lpUserName, nMaxCount, NULL, NULL);
399 #endif
401 bSuccessful = TRUE;
402 } else {
403 bSuccessful = FALSE;
406 LeaveCriticalSection(&PerfDataCriticalSection);
408 return bSuccessful;
411 ULONG PerfDataGetSessionId(ULONG Index)
413 ULONG SessionId;
415 EnterCriticalSection(&PerfDataCriticalSection);
417 if (Index < ProcessCount)
418 SessionId = pPerfData[Index].SessionId;
419 else
420 SessionId = 0;
422 LeaveCriticalSection(&PerfDataCriticalSection);
424 return SessionId;
427 ULONG PerfDataGetCPUUsage(ULONG Index)
429 ULONG CpuUsage;
431 EnterCriticalSection(&PerfDataCriticalSection);
433 if (Index < ProcessCount)
434 CpuUsage = pPerfData[Index].CPUUsage;
435 else
436 CpuUsage = 0;
438 LeaveCriticalSection(&PerfDataCriticalSection);
440 return CpuUsage;
443 TIME PerfDataGetCPUTime(ULONG Index)
445 TIME CpuTime = {{0,0}};
447 EnterCriticalSection(&PerfDataCriticalSection);
449 if (Index < ProcessCount)
450 CpuTime = pPerfData[Index].CPUTime;
452 LeaveCriticalSection(&PerfDataCriticalSection);
454 return CpuTime;
457 ULONG PerfDataGetWorkingSetSizeBytes(ULONG Index)
459 ULONG WorkingSetSizeBytes;
461 EnterCriticalSection(&PerfDataCriticalSection);
463 if (Index < ProcessCount)
464 WorkingSetSizeBytes = pPerfData[Index].WorkingSetSizeBytes;
465 else
466 WorkingSetSizeBytes = 0;
468 LeaveCriticalSection(&PerfDataCriticalSection);
470 return WorkingSetSizeBytes;
473 ULONG PerfDataGetPeakWorkingSetSizeBytes(ULONG Index)
475 ULONG PeakWorkingSetSizeBytes;
477 EnterCriticalSection(&PerfDataCriticalSection);
479 if (Index < ProcessCount)
480 PeakWorkingSetSizeBytes = pPerfData[Index].PeakWorkingSetSizeBytes;
481 else
482 PeakWorkingSetSizeBytes = 0;
484 LeaveCriticalSection(&PerfDataCriticalSection);
486 return PeakWorkingSetSizeBytes;
489 ULONG PerfDataGetWorkingSetSizeDelta(ULONG Index)
491 ULONG WorkingSetSizeDelta;
493 EnterCriticalSection(&PerfDataCriticalSection);
495 if (Index < ProcessCount)
496 WorkingSetSizeDelta = pPerfData[Index].WorkingSetSizeDelta;
497 else
498 WorkingSetSizeDelta = 0;
500 LeaveCriticalSection(&PerfDataCriticalSection);
502 return WorkingSetSizeDelta;
505 ULONG PerfDataGetPageFaultCount(ULONG Index)
507 ULONG PageFaultCount;
509 EnterCriticalSection(&PerfDataCriticalSection);
511 if (Index < ProcessCount)
512 PageFaultCount = pPerfData[Index].PageFaultCount;
513 else
514 PageFaultCount = 0;
516 LeaveCriticalSection(&PerfDataCriticalSection);
518 return PageFaultCount;
521 ULONG PerfDataGetPageFaultCountDelta(ULONG Index)
523 ULONG PageFaultCountDelta;
525 EnterCriticalSection(&PerfDataCriticalSection);
527 if (Index < ProcessCount)
528 PageFaultCountDelta = pPerfData[Index].PageFaultCountDelta;
529 else
530 PageFaultCountDelta = 0;
532 LeaveCriticalSection(&PerfDataCriticalSection);
534 return PageFaultCountDelta;
537 ULONG PerfDataGetVirtualMemorySizeBytes(ULONG Index)
539 ULONG VirtualMemorySizeBytes;
541 EnterCriticalSection(&PerfDataCriticalSection);
543 if (Index < ProcessCount)
544 VirtualMemorySizeBytes = pPerfData[Index].VirtualMemorySizeBytes;
545 else
546 VirtualMemorySizeBytes = 0;
548 LeaveCriticalSection(&PerfDataCriticalSection);
550 return VirtualMemorySizeBytes;
553 ULONG PerfDataGetPagedPoolUsagePages(ULONG Index)
555 ULONG PagedPoolUsagePages;
557 EnterCriticalSection(&PerfDataCriticalSection);
559 if (Index < ProcessCount)
560 PagedPoolUsagePages = pPerfData[Index].PagedPoolUsagePages;
561 else
562 PagedPoolUsagePages = 0;
564 LeaveCriticalSection(&PerfDataCriticalSection);
566 return PagedPoolUsagePages;
569 ULONG PerfDataGetNonPagedPoolUsagePages(ULONG Index)
571 ULONG NonPagedPoolUsagePages;
573 EnterCriticalSection(&PerfDataCriticalSection);
575 if (Index < ProcessCount)
576 NonPagedPoolUsagePages = pPerfData[Index].NonPagedPoolUsagePages;
577 else
578 NonPagedPoolUsagePages = 0;
580 LeaveCriticalSection(&PerfDataCriticalSection);
582 return NonPagedPoolUsagePages;
585 ULONG PerfDataGetBasePriority(ULONG Index)
587 ULONG BasePriority;
589 EnterCriticalSection(&PerfDataCriticalSection);
591 if (Index < ProcessCount)
592 BasePriority = pPerfData[Index].BasePriority;
593 else
594 BasePriority = 0;
596 LeaveCriticalSection(&PerfDataCriticalSection);
598 return BasePriority;
601 ULONG PerfDataGetHandleCount(ULONG Index)
603 ULONG HandleCount;
605 EnterCriticalSection(&PerfDataCriticalSection);
607 if (Index < ProcessCount)
608 HandleCount = pPerfData[Index].HandleCount;
609 else
610 HandleCount = 0;
612 LeaveCriticalSection(&PerfDataCriticalSection);
614 return HandleCount;
617 ULONG PerfDataGetThreadCount(ULONG Index)
619 ULONG ThreadCount;
621 EnterCriticalSection(&PerfDataCriticalSection);
623 if (Index < ProcessCount)
624 ThreadCount = pPerfData[Index].ThreadCount;
625 else
626 ThreadCount = 0;
628 LeaveCriticalSection(&PerfDataCriticalSection);
630 return ThreadCount;
633 ULONG PerfDataGetUSERObjectCount(ULONG Index)
635 ULONG USERObjectCount;
637 EnterCriticalSection(&PerfDataCriticalSection);
639 if (Index < ProcessCount)
640 USERObjectCount = pPerfData[Index].USERObjectCount;
641 else
642 USERObjectCount = 0;
644 LeaveCriticalSection(&PerfDataCriticalSection);
646 return USERObjectCount;
649 ULONG PerfDataGetGDIObjectCount(ULONG Index)
651 ULONG GDIObjectCount;
653 EnterCriticalSection(&PerfDataCriticalSection);
655 if (Index < ProcessCount)
656 GDIObjectCount = pPerfData[Index].GDIObjectCount;
657 else
658 GDIObjectCount = 0;
660 LeaveCriticalSection(&PerfDataCriticalSection);
662 return GDIObjectCount;
665 BOOL PerfDataGetIOCounters(ULONG Index, PIO_COUNTERS pIoCounters)
667 BOOL bSuccessful;
669 EnterCriticalSection(&PerfDataCriticalSection);
671 if (Index < ProcessCount)
673 memcpy(pIoCounters, &pPerfData[Index].IOCounters, sizeof(IO_COUNTERS));
674 bSuccessful = TRUE;
676 else
677 bSuccessful = FALSE;
679 LeaveCriticalSection(&PerfDataCriticalSection);
681 return bSuccessful;
684 ULONG PerfDataGetCommitChargeTotalK(void)
686 ULONG Total;
687 ULONG PageSize;
689 EnterCriticalSection(&PerfDataCriticalSection);
691 Total = SystemPerfInfo.MmTotalCommittedPages;
692 PageSize = SystemBasicInfo.uPageSize;
694 LeaveCriticalSection(&PerfDataCriticalSection);
696 Total = Total * (PageSize / 1024);
698 return Total;
701 ULONG PerfDataGetCommitChargeLimitK(void)
703 ULONG Limit;
704 ULONG PageSize;
706 EnterCriticalSection(&PerfDataCriticalSection);
708 Limit = SystemPerfInfo.MmTotalCommitLimit;
709 PageSize = SystemBasicInfo.uPageSize;
711 LeaveCriticalSection(&PerfDataCriticalSection);
713 Limit = Limit * (PageSize / 1024);
715 return Limit;
718 ULONG PerfDataGetCommitChargePeakK(void)
720 ULONG Peak;
721 ULONG PageSize;
723 EnterCriticalSection(&PerfDataCriticalSection);
725 Peak = SystemPerfInfo.MmPeakLimit;
726 PageSize = SystemBasicInfo.uPageSize;
728 LeaveCriticalSection(&PerfDataCriticalSection);
730 Peak = Peak * (PageSize / 1024);
732 return Peak;
735 ULONG PerfDataGetKernelMemoryTotalK(void)
737 ULONG Total;
738 ULONG Paged;
739 ULONG NonPaged;
740 ULONG PageSize;
742 EnterCriticalSection(&PerfDataCriticalSection);
744 Paged = SystemPerfInfo.PoolPagedBytes;
745 NonPaged = SystemPerfInfo.PoolNonPagedBytes;
746 PageSize = SystemBasicInfo.uPageSize;
748 LeaveCriticalSection(&PerfDataCriticalSection);
750 Paged = Paged * (PageSize / 1024);
751 NonPaged = NonPaged * (PageSize / 1024);
753 Total = Paged + NonPaged;
755 return Total;
758 ULONG PerfDataGetKernelMemoryPagedK(void)
760 ULONG Paged;
761 ULONG PageSize;
763 EnterCriticalSection(&PerfDataCriticalSection);
765 Paged = SystemPerfInfo.PoolPagedBytes;
766 PageSize = SystemBasicInfo.uPageSize;
768 LeaveCriticalSection(&PerfDataCriticalSection);
770 Paged = Paged * (PageSize / 1024);
772 return Paged;
775 ULONG PerfDataGetKernelMemoryNonPagedK(void)
777 ULONG NonPaged;
778 ULONG PageSize;
780 EnterCriticalSection(&PerfDataCriticalSection);
782 NonPaged = SystemPerfInfo.PoolNonPagedBytes;
783 PageSize = SystemBasicInfo.uPageSize;
785 LeaveCriticalSection(&PerfDataCriticalSection);
787 NonPaged = NonPaged * (PageSize / 1024);
789 return NonPaged;
792 ULONG PerfDataGetPhysicalMemoryTotalK(void)
794 ULONG Total;
795 ULONG PageSize;
797 EnterCriticalSection(&PerfDataCriticalSection);
799 Total = SystemBasicInfo.uMmNumberOfPhysicalPages;
800 PageSize = SystemBasicInfo.uPageSize;
802 LeaveCriticalSection(&PerfDataCriticalSection);
804 Total = Total * (PageSize / 1024);
806 return Total;
809 ULONG PerfDataGetPhysicalMemoryAvailableK(void)
811 ULONG Available;
812 ULONG PageSize;
814 EnterCriticalSection(&PerfDataCriticalSection);
816 Available = SystemPerfInfo.MmAvailablePages;
817 PageSize = SystemBasicInfo.uPageSize;
819 LeaveCriticalSection(&PerfDataCriticalSection);
821 Available = Available * (PageSize / 1024);
823 return Available;
826 ULONG PerfDataGetPhysicalMemorySystemCacheK(void)
828 ULONG SystemCache;
829 ULONG PageSize;
831 EnterCriticalSection(&PerfDataCriticalSection);
833 SystemCache = SystemCacheInfo.CurrentSize;
834 PageSize = SystemBasicInfo.uPageSize;
836 LeaveCriticalSection(&PerfDataCriticalSection);
838 /* SystemCache = SystemCache * (PageSize / 1024); */
839 SystemCache = SystemCache / 1024;
841 return SystemCache;
844 ULONG PerfDataGetSystemHandleCount(void)
846 ULONG HandleCount;
848 EnterCriticalSection(&PerfDataCriticalSection);
850 HandleCount = SystemHandleInfo.Count;
852 LeaveCriticalSection(&PerfDataCriticalSection);
854 return HandleCount;
857 ULONG PerfDataGetTotalThreadCount(void)
859 ULONG ThreadCount = 0;
860 ULONG i;
862 EnterCriticalSection(&PerfDataCriticalSection);
864 for (i=0; i<ProcessCount; i++)
866 ThreadCount += pPerfData[i].ThreadCount;
869 LeaveCriticalSection(&PerfDataCriticalSection);
871 return ThreadCount;