e1c921437a575c058beed65b66acdc023ccdd8f5
[reactos.git] / rosapps / taskmgr / perfdata.cpp
1 /*
2 * ReactOS Task Manager
3 *
4 * perfdata.cpp
5 *
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program 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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef _MSC_VER
24 #include "stdafx.h"
25 #else
26 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
27 #include <windows.h>
28 #include <commctrl.h>
29 #include <stdlib.h>
30 #include <malloc.h>
31 #include <memory.h>
32 #include <tchar.h>
33 #include <process.h>
34 #include <stdio.h>
35 #endif
36
37 #include "TaskMgr.h"
38 #include "perfdata.h"
39
40 PROCNTQSI NtQuerySystemInformation = NULL;
41 PROCGGR pGetGuiResources = NULL;
42 PROCGPIC pGetProcessIoCounters = NULL;
43 CRITICAL_SECTION PerfDataCriticalSection;
44 PPERFDATA pPerfDataOld = NULL; // Older perf data (saved to establish delta values)
45 PPERFDATA pPerfData = NULL; // Most recent copy of perf data
46 ULONG ProcessCountOld = 0;
47 ULONG ProcessCount = 0;
48 double dbIdleTime;
49 double dbKernelTime;
50 double dbSystemTime;
51 LARGE_INTEGER liOldIdleTime = {0,0};
52 double OldKernelTime = 0;
53 LARGE_INTEGER liOldSystemTime = {0,0};
54 SYSTEM_PERFORMANCE_INFORMATION SystemPerfInfo;
55 SYSTEM_BASIC_INFORMATION SystemBasicInfo;
56 SYSTEM_CACHE_INFORMATION SystemCacheInfo;
57 SYSTEM_HANDLE_INFORMATION SystemHandleInfo;
58 PSYSTEM_PROCESSORTIME_INFO SystemProcessorTimeInfo = NULL;
59
60 BOOL PerfDataInitialize(void)
61 {
62 LONG status;
63
64 NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQuerySystemInformation");
65 pGetGuiResources = (PROCGGR)GetProcAddress(GetModuleHandle("user32.dll"), "GetGuiResources");
66 pGetProcessIoCounters = (PROCGPIC)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetProcessIoCounters");
67
68 InitializeCriticalSection(&PerfDataCriticalSection);
69
70 if (!NtQuerySystemInformation)
71 return FALSE;
72
73 //
74 // Get number of processors in the system
75 //
76 status = NtQuerySystemInformation(SystemBasicInformation, &SystemBasicInfo, sizeof(SystemBasicInfo), NULL);
77 if (status != NO_ERROR)
78 return FALSE;
79
80 return TRUE;
81 }
82
83 void PerfDataUninitialize(void)
84 {
85 NtQuerySystemInformation = NULL;
86
87 DeleteCriticalSection(&PerfDataCriticalSection);
88 }
89
90 void PerfDataRefresh(void)
91 {
92 ULONG ulSize;
93 LONG status;
94 LPBYTE pBuffer;
95 ULONG BufferSize;
96 PSYSTEM_PROCESS_INFORMATION pSPI;
97 PPERFDATA pPDOld;
98 ULONG Idx, Idx2;
99 HANDLE hProcess;
100 HANDLE hProcessToken;
101 TCHAR szTemp[MAX_PATH];
102 DWORD dwSize;
103 SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
104 SYSTEM_TIME_INFORMATION SysTimeInfo;
105 SYSTEM_CACHE_INFORMATION SysCacheInfo;
106 LPBYTE SysHandleInfoData;
107 PSYSTEM_PROCESSORTIME_INFO SysProcessorTimeInfo;
108 double CurrentKernelTime;
109
110
111 if (!NtQuerySystemInformation)
112 return;
113
114 // Get new system time
115 status = NtQuerySystemInformation(SystemTimeInformation, &SysTimeInfo, sizeof(SysTimeInfo), 0);
116 if (status != NO_ERROR)
117 return;
118
119 // Get new CPU's idle time
120 status = NtQuerySystemInformation(SystemPerformanceInformation, &SysPerfInfo, sizeof(SysPerfInfo), NULL);
121 if (status != NO_ERROR)
122 return;
123
124 // Get system cache information
125 status = NtQuerySystemInformation(SystemCacheInformation, &SysCacheInfo, sizeof(SysCacheInfo), NULL);
126 if (status != NO_ERROR)
127 return;
128
129 // Get processor time information
130 SysProcessorTimeInfo = new SYSTEM_PROCESSORTIME_INFO[SystemBasicInfo.bKeNumberProcessors];
131 status = NtQuerySystemInformation(SystemProcessorTimeInformation, SysProcessorTimeInfo, sizeof(SYSTEM_PROCESSORTIME_INFO) * SystemBasicInfo.bKeNumberProcessors, &ulSize);
132 if (status != NO_ERROR)
133 return;
134
135 // Get handle information
136 // We don't know how much data there is so just keep
137 // increasing the buffer size until the call succeeds
138 BufferSize = 0;
139 do
140 {
141 BufferSize += 0x10000;
142 SysHandleInfoData = new BYTE[BufferSize];
143
144 status = NtQuerySystemInformation(SystemHandleInformation, SysHandleInfoData, BufferSize, &ulSize);
145
146 if (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/)
147 delete[] SysHandleInfoData;
148
149 } while (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/);
150
151 // Get process information
152 // We don't know how much data there is so just keep
153 // increasing the buffer size until the call succeeds
154 BufferSize = 0;
155 do
156 {
157 BufferSize += 0x10000;
158 pBuffer = new BYTE[BufferSize];
159
160 status = NtQuerySystemInformation(SystemProcessInformation, pBuffer, BufferSize, &ulSize);
161
162 if (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/)
163 delete[] pBuffer;
164
165 } while (status == 0xC0000004 /*STATUS_INFO_LENGTH_MISMATCH*/);
166
167 EnterCriticalSection(&PerfDataCriticalSection);
168
169 //
170 // Save system performance info
171 //
172 memcpy(&SystemPerfInfo, &SysPerfInfo, sizeof(SYSTEM_PERFORMANCE_INFORMATION));
173
174 //
175 // Save system cache info
176 //
177 memcpy(&SystemCacheInfo, &SysCacheInfo, sizeof(SYSTEM_CACHE_INFORMATION));
178
179 //
180 // Save system processor time info
181 //
182 if (SystemProcessorTimeInfo)
183 {
184 delete[] SystemProcessorTimeInfo;
185 }
186 SystemProcessorTimeInfo = SysProcessorTimeInfo;
187
188 //
189 // Save system handle info
190 //
191 memcpy(&SystemHandleInfo, SysHandleInfoData, sizeof(SYSTEM_HANDLE_INFORMATION));
192 delete[] SysHandleInfoData;
193
194 for (CurrentKernelTime=0, Idx=0; Idx<SystemBasicInfo.bKeNumberProcessors; Idx++)
195 {
196 CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].KernelTime);
197 CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].DpcTime);
198 CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].InterruptTime);
199 }
200
201 // If it's a first call - skip idle time calcs
202 if (liOldIdleTime.QuadPart != 0)
203 {
204 // CurrentValue = NewValue - OldValue
205 dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
206 dbKernelTime = CurrentKernelTime - OldKernelTime;
207 dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);
208
209 // CurrentCpuIdle = IdleTime / SystemTime
210 dbIdleTime = dbIdleTime / dbSystemTime;
211 dbKernelTime = dbKernelTime / dbSystemTime;
212
213 // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
214 dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.bKeNumberProcessors;// + 0.5;
215 dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.bKeNumberProcessors;// + 0.5;
216 }
217
218 // Store new CPU's idle and system time
219 liOldIdleTime = SysPerfInfo.liIdleTime;
220 liOldSystemTime = SysTimeInfo.liKeSystemTime;
221 OldKernelTime = CurrentKernelTime;
222
223 // Determine the process count
224 // We loop through the data we got from NtQuerySystemInformation
225 // and count how many structures there are (until RelativeOffset is 0)
226 ProcessCountOld = ProcessCount;
227 ProcessCount = 0;
228 pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
229 while (pSPI)
230 {
231 ProcessCount++;
232
233 if (pSPI->RelativeOffset == 0)
234 break;
235
236 pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->RelativeOffset);
237 }
238
239 // Now alloc a new PERFDATA array and fill in the data
240 if (pPerfDataOld)
241 delete[] pPerfDataOld;
242 pPerfDataOld = pPerfData;
243 pPerfData = new PERFDATA[ProcessCount];
244 pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
245 for (Idx=0; Idx<ProcessCount; Idx++)
246 {
247 // Get the old perf data for this process (if any)
248 // so that we can establish delta values
249 pPDOld = NULL;
250 for (Idx2=0; Idx2<ProcessCountOld; Idx2++)
251 {
252 if (pPerfDataOld[Idx2].ProcessId == pSPI->ProcessId)
253 {
254 pPDOld = &pPerfDataOld[Idx2];
255 break;
256 }
257 }
258
259 // Clear out process perf data structure
260 memset(&pPerfData[Idx], 0, sizeof(PERFDATA));
261
262 if (pSPI->Name.Buffer)
263 wcscpy(pPerfData[Idx].ImageName, pSPI->Name.Buffer);
264 else
265 wcscpy(pPerfData[Idx].ImageName, L"System Idle Process");
266
267 pPerfData[Idx].ProcessId = pSPI->ProcessId;
268
269 if (pPDOld)
270 {
271 double CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
272 double OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
273
274 double CpuTime = (CurTime - OldTime) / dbSystemTime;
275
276 CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.bKeNumberProcessors;// + 0.5;
277
278 pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
279 }
280 pPerfData[Idx].CPUTime.QuadPart = pSPI->UserTime.QuadPart + pSPI->KernelTime.QuadPart;
281 pPerfData[Idx].WorkingSetSizeBytes = pSPI->TotalWorkingSetSizeBytes;
282 pPerfData[Idx].PeakWorkingSetSizeBytes = pSPI->PeakWorkingSetSizeBytes;
283 if (pPDOld)
284 pPerfData[Idx].WorkingSetSizeDelta = labs((LONG)pSPI->TotalWorkingSetSizeBytes - (LONG)pPDOld->WorkingSetSizeBytes);
285 else
286 pPerfData[Idx].WorkingSetSizeDelta = 0;
287 pPerfData[Idx].PageFaultCount = pSPI->PageFaultCount;
288 if (pPDOld)
289 pPerfData[Idx].PageFaultCountDelta = labs((LONG)pSPI->PageFaultCount - (LONG)pPDOld->PageFaultCount);
290 else
291 pPerfData[Idx].PageFaultCountDelta = 0;
292 pPerfData[Idx].VirtualMemorySizeBytes = pSPI->TotalVirtualSizeBytes;
293 pPerfData[Idx].PagedPoolUsagePages = pSPI->TotalPagedPoolUsagePages;
294 pPerfData[Idx].NonPagedPoolUsagePages = pSPI->TotalNonPagedPoolUsagePages;
295 pPerfData[Idx].BasePriority = pSPI->BasePriority;
296 pPerfData[Idx].HandleCount = pSPI->HandleCount;
297 pPerfData[Idx].ThreadCount = pSPI->ThreadCount;
298
299 pPerfData[Idx].SessionId = pSPI->SessionId;
300
301 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pSPI->ProcessId);
302 if (hProcess)
303 {
304 if (OpenProcessToken(hProcess, TOKEN_QUERY|TOKEN_DUPLICATE|TOKEN_IMPERSONATE, &hProcessToken))
305 {
306 ImpersonateLoggedOnUser(hProcessToken);
307 memset(szTemp, 0, sizeof(TCHAR[MAX_PATH]));
308 dwSize = MAX_PATH;
309 GetUserName(szTemp, &dwSize);
310 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szTemp, -1, pPerfData[Idx].UserName, MAX_PATH);
311 RevertToSelf();
312
313 CloseHandle(hProcessToken);
314 }
315
316 if (pGetGuiResources)
317 {
318 pPerfData[Idx].USERObjectCount = pGetGuiResources(hProcess, GR_USEROBJECTS);
319 pPerfData[Idx].GDIObjectCount = pGetGuiResources(hProcess, GR_GDIOBJECTS);
320 }
321 if (pGetProcessIoCounters)
322 pGetProcessIoCounters(hProcess, &pPerfData[Idx].IOCounters);
323
324 CloseHandle(hProcess);
325 }
326
327 pPerfData[Idx].UserTime.QuadPart = pSPI->UserTime.QuadPart;
328 pPerfData[Idx].KernelTime.QuadPart = pSPI->KernelTime.QuadPart;
329
330 pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->RelativeOffset);
331 }
332
333 delete[] pBuffer;
334
335 LeaveCriticalSection(&PerfDataCriticalSection);
336 }
337
338 ULONG PerfDataGetProcessCount(void)
339 {
340 return ProcessCount;
341 }
342
343 ULONG PerfDataGetProcessorUsage(void)
344 {
345 return (ULONG)dbIdleTime;
346 }
347
348 ULONG PerfDataGetProcessorSystemUsage(void)
349 {
350 return (ULONG)dbKernelTime;
351 }
352
353 BOOL PerfDataGetImageName(ULONG Index, LPTSTR lpImageName, int nMaxCount)
354 {
355 BOOL bSuccessful;
356
357 EnterCriticalSection(&PerfDataCriticalSection);
358
359 if (Index < ProcessCount)
360 {
361 #ifdef _UNICODE
362 wcsncpy(lpImageName, pPerfData[Index].ImageName, nMaxCount);
363 #else
364 WideCharToMultiByte(CP_ACP, 0, pPerfData[Index].ImageName, -1, lpImageName, nMaxCount, NULL, NULL);
365 #endif
366
367 bSuccessful = TRUE;
368 }
369 else
370 bSuccessful = FALSE;
371
372 LeaveCriticalSection(&PerfDataCriticalSection);
373
374 return bSuccessful;
375 }
376
377 ULONG PerfDataGetProcessId(ULONG Index)
378 {
379 ULONG ProcessId;
380
381 EnterCriticalSection(&PerfDataCriticalSection);
382
383 if (Index < ProcessCount)
384 ProcessId = pPerfData[Index].ProcessId;
385 else
386 ProcessId = 0;
387
388 LeaveCriticalSection(&PerfDataCriticalSection);
389
390 return ProcessId;
391 }
392
393 BOOL PerfDataGetUserName(ULONG Index, LPTSTR lpUserName, int nMaxCount)
394 {
395 BOOL bSuccessful;
396
397 EnterCriticalSection(&PerfDataCriticalSection);
398
399 if (Index < ProcessCount)
400 {
401 #ifdef _UNICODE
402 wcsncpy(lpUserName, pPerfData[Index].UserName, nMaxCount);
403 #else
404 WideCharToMultiByte(CP_ACP, 0, pPerfData[Index].UserName, -1, lpUserName, nMaxCount, NULL, NULL);
405 #endif
406
407 bSuccessful = TRUE;
408 }
409 else
410 bSuccessful = FALSE;
411
412 LeaveCriticalSection(&PerfDataCriticalSection);
413
414 return bSuccessful;
415 }
416
417 ULONG PerfDataGetSessionId(ULONG Index)
418 {
419 ULONG SessionId;
420
421 EnterCriticalSection(&PerfDataCriticalSection);
422
423 if (Index < ProcessCount)
424 SessionId = pPerfData[Index].SessionId;
425 else
426 SessionId = 0;
427
428 LeaveCriticalSection(&PerfDataCriticalSection);
429
430 return SessionId;
431 }
432
433 ULONG PerfDataGetCPUUsage(ULONG Index)
434 {
435 ULONG CpuUsage;
436
437 EnterCriticalSection(&PerfDataCriticalSection);
438
439 if (Index < ProcessCount)
440 CpuUsage = pPerfData[Index].CPUUsage;
441 else
442 CpuUsage = 0;
443
444 LeaveCriticalSection(&PerfDataCriticalSection);
445
446 return CpuUsage;
447 }
448
449 TIME PerfDataGetCPUTime(ULONG Index)
450 {
451 TIME CpuTime = {0,0};
452
453 EnterCriticalSection(&PerfDataCriticalSection);
454
455 if (Index < ProcessCount)
456 CpuTime = pPerfData[Index].CPUTime;
457
458 LeaveCriticalSection(&PerfDataCriticalSection);
459
460 return CpuTime;
461 }
462
463 ULONG PerfDataGetWorkingSetSizeBytes(ULONG Index)
464 {
465 ULONG WorkingSetSizeBytes;
466
467 EnterCriticalSection(&PerfDataCriticalSection);
468
469 if (Index < ProcessCount)
470 WorkingSetSizeBytes = pPerfData[Index].WorkingSetSizeBytes;
471 else
472 WorkingSetSizeBytes = 0;
473
474 LeaveCriticalSection(&PerfDataCriticalSection);
475
476 return WorkingSetSizeBytes;
477 }
478
479 ULONG PerfDataGetPeakWorkingSetSizeBytes(ULONG Index)
480 {
481 ULONG PeakWorkingSetSizeBytes;
482
483 EnterCriticalSection(&PerfDataCriticalSection);
484
485 if (Index < ProcessCount)
486 PeakWorkingSetSizeBytes = pPerfData[Index].PeakWorkingSetSizeBytes;
487 else
488 PeakWorkingSetSizeBytes = 0;
489
490 LeaveCriticalSection(&PerfDataCriticalSection);
491
492 return PeakWorkingSetSizeBytes;
493 }
494
495 ULONG PerfDataGetWorkingSetSizeDelta(ULONG Index)
496 {
497 ULONG WorkingSetSizeDelta;
498
499 EnterCriticalSection(&PerfDataCriticalSection);
500
501 if (Index < ProcessCount)
502 WorkingSetSizeDelta = pPerfData[Index].WorkingSetSizeDelta;
503 else
504 WorkingSetSizeDelta = 0;
505
506 LeaveCriticalSection(&PerfDataCriticalSection);
507
508 return WorkingSetSizeDelta;
509 }
510
511 ULONG PerfDataGetPageFaultCount(ULONG Index)
512 {
513 ULONG PageFaultCount;
514
515 EnterCriticalSection(&PerfDataCriticalSection);
516
517 if (Index < ProcessCount)
518 PageFaultCount = pPerfData[Index].PageFaultCount;
519 else
520 PageFaultCount = 0;
521
522 LeaveCriticalSection(&PerfDataCriticalSection);
523
524 return PageFaultCount;
525 }
526
527 ULONG PerfDataGetPageFaultCountDelta(ULONG Index)
528 {
529 ULONG PageFaultCountDelta;
530
531 EnterCriticalSection(&PerfDataCriticalSection);
532
533 if (Index < ProcessCount)
534 PageFaultCountDelta = pPerfData[Index].PageFaultCountDelta;
535 else
536 PageFaultCountDelta = 0;
537
538 LeaveCriticalSection(&PerfDataCriticalSection);
539
540 return PageFaultCountDelta;
541 }
542
543 ULONG PerfDataGetVirtualMemorySizeBytes(ULONG Index)
544 {
545 ULONG VirtualMemorySizeBytes;
546
547 EnterCriticalSection(&PerfDataCriticalSection);
548
549 if (Index < ProcessCount)
550 VirtualMemorySizeBytes = pPerfData[Index].VirtualMemorySizeBytes;
551 else
552 VirtualMemorySizeBytes = 0;
553
554 LeaveCriticalSection(&PerfDataCriticalSection);
555
556 return VirtualMemorySizeBytes;
557 }
558
559 ULONG PerfDataGetPagedPoolUsagePages(ULONG Index)
560 {
561 ULONG PagedPoolUsagePages;
562
563 EnterCriticalSection(&PerfDataCriticalSection);
564
565 if (Index < ProcessCount)
566 PagedPoolUsagePages = pPerfData[Index].PagedPoolUsagePages;
567 else
568 PagedPoolUsagePages = 0;
569
570 LeaveCriticalSection(&PerfDataCriticalSection);
571
572 return PagedPoolUsagePages;
573 }
574
575 ULONG PerfDataGetNonPagedPoolUsagePages(ULONG Index)
576 {
577 ULONG NonPagedPoolUsagePages;
578
579 EnterCriticalSection(&PerfDataCriticalSection);
580
581 if (Index < ProcessCount)
582 NonPagedPoolUsagePages = pPerfData[Index].NonPagedPoolUsagePages;
583 else
584 NonPagedPoolUsagePages = 0;
585
586 LeaveCriticalSection(&PerfDataCriticalSection);
587
588 return NonPagedPoolUsagePages;
589 }
590
591 ULONG PerfDataGetBasePriority(ULONG Index)
592 {
593 ULONG BasePriority;
594
595 EnterCriticalSection(&PerfDataCriticalSection);
596
597 if (Index < ProcessCount)
598 BasePriority = pPerfData[Index].BasePriority;
599 else
600 BasePriority = 0;
601
602 LeaveCriticalSection(&PerfDataCriticalSection);
603
604 return BasePriority;
605 }
606
607 ULONG PerfDataGetHandleCount(ULONG Index)
608 {
609 ULONG HandleCount;
610
611 EnterCriticalSection(&PerfDataCriticalSection);
612
613 if (Index < ProcessCount)
614 HandleCount = pPerfData[Index].HandleCount;
615 else
616 HandleCount = 0;
617
618 LeaveCriticalSection(&PerfDataCriticalSection);
619
620 return HandleCount;
621 }
622
623 ULONG PerfDataGetThreadCount(ULONG Index)
624 {
625 ULONG ThreadCount;
626
627 EnterCriticalSection(&PerfDataCriticalSection);
628
629 if (Index < ProcessCount)
630 ThreadCount = pPerfData[Index].ThreadCount;
631 else
632 ThreadCount = 0;
633
634 LeaveCriticalSection(&PerfDataCriticalSection);
635
636 return ThreadCount;
637 }
638
639 ULONG PerfDataGetUSERObjectCount(ULONG Index)
640 {
641 ULONG USERObjectCount;
642
643 EnterCriticalSection(&PerfDataCriticalSection);
644
645 if (Index < ProcessCount)
646 USERObjectCount = pPerfData[Index].USERObjectCount;
647 else
648 USERObjectCount = 0;
649
650 LeaveCriticalSection(&PerfDataCriticalSection);
651
652 return USERObjectCount;
653 }
654
655 ULONG PerfDataGetGDIObjectCount(ULONG Index)
656 {
657 ULONG GDIObjectCount;
658
659 EnterCriticalSection(&PerfDataCriticalSection);
660
661 if (Index < ProcessCount)
662 GDIObjectCount = pPerfData[Index].GDIObjectCount;
663 else
664 GDIObjectCount = 0;
665
666 LeaveCriticalSection(&PerfDataCriticalSection);
667
668 return GDIObjectCount;
669 }
670
671 BOOL PerfDataGetIOCounters(ULONG Index, PIO_COUNTERS pIoCounters)
672 {
673 BOOL bSuccessful;
674
675 EnterCriticalSection(&PerfDataCriticalSection);
676
677 if (Index < ProcessCount)
678 {
679 memcpy(pIoCounters, &pPerfData[Index].IOCounters, sizeof(IO_COUNTERS));
680 bSuccessful = TRUE;
681 }
682 else
683 bSuccessful = FALSE;
684
685 LeaveCriticalSection(&PerfDataCriticalSection);
686
687 return bSuccessful;
688 }
689
690 ULONG PerfDataGetCommitChargeTotalK(void)
691 {
692 ULONG Total;
693 ULONG PageSize;
694
695 EnterCriticalSection(&PerfDataCriticalSection);
696
697 Total = SystemPerfInfo.MmTotalCommitedPages;
698 PageSize = SystemBasicInfo.uPageSize;
699
700 LeaveCriticalSection(&PerfDataCriticalSection);
701
702 Total = Total * (PageSize / 1024);
703
704 return Total;
705 }
706
707 ULONG PerfDataGetCommitChargeLimitK(void)
708 {
709 ULONG Limit;
710 ULONG PageSize;
711
712 EnterCriticalSection(&PerfDataCriticalSection);
713
714 Limit = SystemPerfInfo.MmTotalCommitLimit;
715 PageSize = SystemBasicInfo.uPageSize;
716
717 LeaveCriticalSection(&PerfDataCriticalSection);
718
719 Limit = Limit * (PageSize / 1024);
720
721 return Limit;
722 }
723
724 ULONG PerfDataGetCommitChargePeakK(void)
725 {
726 ULONG Peak;
727 ULONG PageSize;
728
729 EnterCriticalSection(&PerfDataCriticalSection);
730
731 Peak = SystemPerfInfo.MmPeakLimit;
732 PageSize = SystemBasicInfo.uPageSize;
733
734 LeaveCriticalSection(&PerfDataCriticalSection);
735
736 Peak = Peak * (PageSize / 1024);
737
738 return Peak;
739 }
740
741 ULONG PerfDataGetKernelMemoryTotalK(void)
742 {
743 ULONG Total;
744 ULONG Paged;
745 ULONG NonPaged;
746 ULONG PageSize;
747
748 EnterCriticalSection(&PerfDataCriticalSection);
749
750 Paged = SystemPerfInfo.PoolPagedBytes;
751 NonPaged = SystemPerfInfo.PoolNonPagedBytes;
752 PageSize = SystemBasicInfo.uPageSize;
753
754 LeaveCriticalSection(&PerfDataCriticalSection);
755
756 Paged = Paged * (PageSize / 1024);
757 NonPaged = NonPaged * (PageSize / 1024);
758
759 Total = Paged + NonPaged;
760
761 return Total;
762 }
763
764 ULONG PerfDataGetKernelMemoryPagedK(void)
765 {
766 ULONG Paged;
767 ULONG PageSize;
768
769 EnterCriticalSection(&PerfDataCriticalSection);
770
771 Paged = SystemPerfInfo.PoolPagedBytes;
772 PageSize = SystemBasicInfo.uPageSize;
773
774 LeaveCriticalSection(&PerfDataCriticalSection);
775
776 Paged = Paged * (PageSize / 1024);
777
778 return Paged;
779 }
780
781 ULONG PerfDataGetKernelMemoryNonPagedK(void)
782 {
783 ULONG NonPaged;
784 ULONG PageSize;
785
786 EnterCriticalSection(&PerfDataCriticalSection);
787
788 NonPaged = SystemPerfInfo.PoolNonPagedBytes;
789 PageSize = SystemBasicInfo.uPageSize;
790
791 LeaveCriticalSection(&PerfDataCriticalSection);
792
793 NonPaged = NonPaged * (PageSize / 1024);
794
795 return NonPaged;
796 }
797
798 ULONG PerfDataGetPhysicalMemoryTotalK(void)
799 {
800 ULONG Total;
801 ULONG PageSize;
802
803 EnterCriticalSection(&PerfDataCriticalSection);
804
805 Total = SystemBasicInfo.uMmNumberOfPhysicalPages;
806 PageSize = SystemBasicInfo.uPageSize;
807
808 LeaveCriticalSection(&PerfDataCriticalSection);
809
810 Total = Total * (PageSize / 1024);
811
812 return Total;
813 }
814
815 ULONG PerfDataGetPhysicalMemoryAvailableK(void)
816 {
817 ULONG Available;
818 ULONG PageSize;
819
820 EnterCriticalSection(&PerfDataCriticalSection);
821
822 Available = SystemPerfInfo.MmAvailablePages;
823 PageSize = SystemBasicInfo.uPageSize;
824
825 LeaveCriticalSection(&PerfDataCriticalSection);
826
827 Available = Available * (PageSize / 1024);
828
829 return Available;
830 }
831
832 ULONG PerfDataGetPhysicalMemorySystemCacheK(void)
833 {
834 ULONG SystemCache;
835 ULONG PageSize;
836
837 EnterCriticalSection(&PerfDataCriticalSection);
838
839 SystemCache = SystemCacheInfo.CurrentSize;
840 PageSize = SystemBasicInfo.uPageSize;
841
842 LeaveCriticalSection(&PerfDataCriticalSection);
843
844 //SystemCache = SystemCache * (PageSize / 1024);
845 SystemCache = SystemCache / 1024;
846
847 return SystemCache;
848 }
849
850 ULONG PerfDataGetSystemHandleCount(void)
851 {
852 ULONG HandleCount;
853
854 EnterCriticalSection(&PerfDataCriticalSection);
855
856 HandleCount = SystemHandleInfo.Count;
857
858 LeaveCriticalSection(&PerfDataCriticalSection);
859
860 return HandleCount;
861 }
862
863 ULONG PerfDataGetTotalThreadCount(void)
864 {
865 ULONG ThreadCount = 0;
866 ULONG i;
867
868 EnterCriticalSection(&PerfDataCriticalSection);
869
870 for (i=0; i<ProcessCount; i++)
871 {
872 ThreadCount += pPerfData[i].ThreadCount;
873 }
874
875 LeaveCriticalSection(&PerfDataCriticalSection);
876
877 return ThreadCount;
878 }