6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * Copyright (C) 2014 Ismael Ferreras Morezuelas <swyterzone+ros@gmail.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #define WIN32_LEAN_AND_MEAN
29 #define NTOS_MODE_USER
30 #include <ndk/psfuncs.h>
31 #include <ndk/exfuncs.h>
33 CRITICAL_SECTION PerfDataCriticalSection
;
34 PPERFDATA pPerfDataOld
= NULL
; /* Older perf data (saved to establish delta values) */
35 PPERFDATA pPerfData
= NULL
; /* Most recent copy of perf data */
36 ULONG ProcessCountOld
= 0;
37 ULONG ProcessCount
= 0;
41 LARGE_INTEGER liOldIdleTime
= {{0,0}};
42 double OldKernelTime
= 0;
43 LARGE_INTEGER liOldSystemTime
= {{0,0}};
44 SYSTEM_PERFORMANCE_INFORMATION SystemPerfInfo
;
45 SYSTEM_BASIC_INFORMATION SystemBasicInfo
;
46 SYSTEM_FILECACHE_INFORMATION SystemCacheInfo
;
47 ULONG SystemNumberOfHandles
;
48 PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SystemProcessorTimeInfo
= NULL
;
49 PSID SystemUserSid
= NULL
;
51 PCMD_LINE_CACHE global_cache
= NULL
;
53 #define CMD_LINE_MIN(a, b) (a < b ? a - sizeof(WCHAR) : b)
55 typedef struct _SIDTOUSERNAME
60 } SIDTOUSERNAME
, *PSIDTOUSERNAME
;
62 static LIST_ENTRY SidToUserNameHead
= {&SidToUserNameHead
, &SidToUserNameHead
};
64 BOOL
PerfDataInitialize(void)
66 SID_IDENTIFIER_AUTHORITY NtSidAuthority
= {SECURITY_NT_AUTHORITY
};
69 InitializeCriticalSection(&PerfDataCriticalSection
);
72 * Get number of processors in the system
74 status
= NtQuerySystemInformation(SystemBasicInformation
, &SystemBasicInfo
, sizeof(SystemBasicInfo
), NULL
);
75 if (!NT_SUCCESS(status
))
79 * Create the SYSTEM Sid
81 AllocateAndInitializeSid(&NtSidAuthority
, 1, SECURITY_LOCAL_SYSTEM_RID
, 0, 0, 0, 0, 0, 0, 0, &SystemUserSid
);
85 void PerfDataUninitialize(void)
88 PSIDTOUSERNAME pEntry
;
90 if (pPerfData
!= NULL
)
91 HeapFree(GetProcessHeap(), 0, pPerfData
);
93 DeleteCriticalSection(&PerfDataCriticalSection
);
95 if (SystemUserSid
!= NULL
)
97 FreeSid(SystemUserSid
);
101 /* Free user names cache list */
102 pCur
= SidToUserNameHead
.Flink
;
103 while (pCur
!= &SidToUserNameHead
)
105 pEntry
= CONTAINING_RECORD(pCur
, SIDTOUSERNAME
, List
);
107 HeapFree(GetProcessHeap(), 0, pEntry
);
110 if (SystemProcessorTimeInfo
) {
111 HeapFree(GetProcessHeap(), 0, SystemProcessorTimeInfo
);
115 static void SidToUserName(PSID Sid
, LPWSTR szBuffer
, DWORD BufferSize
)
117 static WCHAR szDomainNameUnused
[255];
118 DWORD DomainNameLen
= sizeof(szDomainNameUnused
) / sizeof(szDomainNameUnused
[0]);
122 LookupAccountSidW(NULL
, Sid
, szBuffer
, &BufferSize
, szDomainNameUnused
, &DomainNameLen
, &Use
);
127 CachedGetUserFromSid(
133 PSIDTOUSERNAME pEntry
;
134 ULONG cbSid
, cwcUserName
;
136 cwcUserName
= *pcwcUserName
;
138 /* Walk through the list */
139 for(pCur
= SidToUserNameHead
.Flink
;
140 pCur
!= &SidToUserNameHead
;
143 pEntry
= CONTAINING_RECORD(pCur
, SIDTOUSERNAME
, List
);
144 if (EqualSid((PSID
)&pEntry
->Data
, pSid
))
146 wcsncpy(pUserName
, pEntry
->pszName
, cwcUserName
);
147 *pcwcUserName
= wcslen(pUserName
);
152 /* We didn't find the SID in the list, get the name conventional */
153 SidToUserName(pSid
, pUserName
, cwcUserName
);
154 *pcwcUserName
= wcslen(pUserName
);
156 /* Allocate a new entry */
157 cwcUserName
= *pcwcUserName
+ 1;
158 cbSid
= GetLengthSid(pSid
);
159 pEntry
= HeapAlloc(GetProcessHeap(), 0, sizeof(SIDTOUSERNAME
) + cbSid
+ cwcUserName
* sizeof(WCHAR
));
161 /* Copy the Sid and name to our entry */
162 CopySid(cbSid
, (PSID
)&pEntry
->Data
, pSid
);
163 pEntry
->pszName
= (LPWSTR
)(pEntry
->Data
+ cbSid
);
164 wcsncpy(pEntry
->pszName
, pUserName
, cwcUserName
);
166 /* Insert the new entry */
167 pEntry
->List
.Flink
= &SidToUserNameHead
;
168 pEntry
->List
.Blink
= SidToUserNameHead
.Blink
;
169 SidToUserNameHead
.Blink
->Flink
= &pEntry
->List
;
170 SidToUserNameHead
.Blink
= &pEntry
->List
;
175 void PerfDataRefresh(void)
181 PSYSTEM_PROCESS_INFORMATION pSPI
;
185 HANDLE hProcessToken
;
186 SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo
;
187 SYSTEM_TIMEOFDAY_INFORMATION SysTimeInfo
;
188 SYSTEM_FILECACHE_INFORMATION SysCacheInfo
;
189 SYSTEM_HANDLE_INFORMATION SysHandleInfoData
;
190 PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SysProcessorTimeInfo
;
191 double CurrentKernelTime
;
192 PSECURITY_DESCRIPTOR ProcessSD
;
194 ULONG Buffer
[64]; /* must be 4 bytes aligned! */
197 /* Get new system time */
198 status
= NtQuerySystemInformation(SystemTimeOfDayInformation
, &SysTimeInfo
, sizeof(SysTimeInfo
), NULL
);
199 if (!NT_SUCCESS(status
))
202 /* Get new CPU's idle time */
203 status
= NtQuerySystemInformation(SystemPerformanceInformation
, &SysPerfInfo
, sizeof(SysPerfInfo
), NULL
);
204 if (!NT_SUCCESS(status
))
207 /* Get system cache information */
208 status
= NtQuerySystemInformation(SystemFileCacheInformation
, &SysCacheInfo
, sizeof(SysCacheInfo
), NULL
);
209 if (!NT_SUCCESS(status
))
212 /* Get processor time information */
213 SysProcessorTimeInfo
= (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
)HeapAlloc(GetProcessHeap(), 0, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
) * SystemBasicInfo
.NumberOfProcessors
);
214 status
= NtQuerySystemInformation(SystemProcessorPerformanceInformation
, SysProcessorTimeInfo
, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
) * SystemBasicInfo
.NumberOfProcessors
, &ulSize
);
216 if (!NT_SUCCESS(status
))
218 if (SysProcessorTimeInfo
!= NULL
)
219 HeapFree(GetProcessHeap(), 0, SysProcessorTimeInfo
);
223 /* Get handle information
224 * Number of handles is enough, no need for data array.
226 status
= NtQuerySystemInformation(SystemHandleInformation
, &SysHandleInfoData
, sizeof(SysHandleInfoData
), NULL
);
227 /* On unexpected error, reuse previous value.
228 * STATUS_SUCCESS (0-1 handle) should never happen.
230 if (status
!= STATUS_INFO_LENGTH_MISMATCH
)
231 SysHandleInfoData
.NumberOfHandles
= SystemNumberOfHandles
;
233 /* Get process information
234 * We don't know how much data there is so just keep
235 * increasing the buffer size until the call succeeds
240 BufferSize
+= 0x10000;
241 pBuffer
= (LPBYTE
)HeapAlloc(GetProcessHeap(), 0, BufferSize
);
243 status
= NtQuerySystemInformation(SystemProcessInformation
, pBuffer
, BufferSize
, &ulSize
);
245 if (status
== STATUS_INFO_LENGTH_MISMATCH
) {
246 HeapFree(GetProcessHeap(), 0, pBuffer
);
249 } while (status
== STATUS_INFO_LENGTH_MISMATCH
);
251 EnterCriticalSection(&PerfDataCriticalSection
);
254 * Save system performance info
256 memcpy(&SystemPerfInfo
, &SysPerfInfo
, sizeof(SYSTEM_PERFORMANCE_INFORMATION
));
259 * Save system cache info
261 memcpy(&SystemCacheInfo
, &SysCacheInfo
, sizeof(SYSTEM_FILECACHE_INFORMATION
));
264 * Save system processor time info
266 if (SystemProcessorTimeInfo
) {
267 HeapFree(GetProcessHeap(), 0, SystemProcessorTimeInfo
);
269 SystemProcessorTimeInfo
= SysProcessorTimeInfo
;
272 * Save system handle info
274 SystemNumberOfHandles
= SysHandleInfoData
.NumberOfHandles
;
276 for (CurrentKernelTime
=0, Idx
=0; Idx
<(ULONG
)SystemBasicInfo
.NumberOfProcessors
; Idx
++) {
277 CurrentKernelTime
+= Li2Double(SystemProcessorTimeInfo
[Idx
].KernelTime
);
278 CurrentKernelTime
+= Li2Double(SystemProcessorTimeInfo
[Idx
].DpcTime
);
279 CurrentKernelTime
+= Li2Double(SystemProcessorTimeInfo
[Idx
].InterruptTime
);
282 /* If it's a first call - skip idle time calcs */
283 if (liOldIdleTime
.QuadPart
!= 0) {
284 /* CurrentValue = NewValue - OldValue */
285 dbIdleTime
= Li2Double(SysPerfInfo
.IdleProcessTime
) - Li2Double(liOldIdleTime
);
286 dbKernelTime
= CurrentKernelTime
- OldKernelTime
;
287 dbSystemTime
= Li2Double(SysTimeInfo
.CurrentTime
) - Li2Double(liOldSystemTime
);
289 /* CurrentCpuIdle = IdleTime / SystemTime */
290 dbIdleTime
= dbIdleTime
/ dbSystemTime
;
291 dbKernelTime
= dbKernelTime
/ dbSystemTime
;
293 /* CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors */
294 dbIdleTime
= 100.0 - dbIdleTime
* 100.0 / (double)SystemBasicInfo
.NumberOfProcessors
; /* + 0.5; */
295 dbKernelTime
= 100.0 - dbKernelTime
* 100.0 / (double)SystemBasicInfo
.NumberOfProcessors
; /* + 0.5; */
298 /* Store new CPU's idle and system time */
299 liOldIdleTime
= SysPerfInfo
.IdleProcessTime
;
300 liOldSystemTime
= SysTimeInfo
.CurrentTime
;
301 OldKernelTime
= CurrentKernelTime
;
303 /* Determine the process count
304 * We loop through the data we got from NtQuerySystemInformation
305 * and count how many structures there are (until RelativeOffset is 0)
307 ProcessCountOld
= ProcessCount
;
309 pSPI
= (PSYSTEM_PROCESS_INFORMATION
)pBuffer
;
312 if (pSPI
->NextEntryOffset
== 0)
314 pSPI
= (PSYSTEM_PROCESS_INFORMATION
)((LPBYTE
)pSPI
+ pSPI
->NextEntryOffset
);
317 /* Now alloc a new PERFDATA array and fill in the data */
318 pPerfData
= (PPERFDATA
)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(PERFDATA
) * ProcessCount
);
320 pSPI
= (PSYSTEM_PROCESS_INFORMATION
)pBuffer
;
321 for (Idx
=0; Idx
<ProcessCount
; Idx
++) {
322 /* Get the old perf data for this process (if any) */
323 /* so that we can establish delta values */
326 for (Idx2
=0; Idx2
<ProcessCountOld
; Idx2
++) {
327 if (pPerfDataOld
[Idx2
].ProcessId
== pSPI
->UniqueProcessId
) {
328 pPDOld
= &pPerfDataOld
[Idx2
];
334 if (pSPI
->ImageName
.Buffer
) {
335 /* Don't assume a UNICODE_STRING Buffer is zero terminated: */
336 int len
= pSPI
->ImageName
.Length
/ 2;
337 /* Check against max size and allow for terminating zero (already zeroed): */
338 if(len
>= MAX_PATH
)len
=MAX_PATH
- 1;
339 wcsncpy(pPerfData
[Idx
].ImageName
, pSPI
->ImageName
.Buffer
, len
);
341 LoadStringW(hInst
, IDS_IDLE_PROCESS
, pPerfData
[Idx
].ImageName
,
342 sizeof(pPerfData
[Idx
].ImageName
) / sizeof(pPerfData
[Idx
].ImageName
[0]));
345 pPerfData
[Idx
].ProcessId
= pSPI
->UniqueProcessId
;
348 double CurTime
= Li2Double(pSPI
->KernelTime
) + Li2Double(pSPI
->UserTime
);
349 double OldTime
= Li2Double(pPDOld
->KernelTime
) + Li2Double(pPDOld
->UserTime
);
350 double CpuTime
= (CurTime
- OldTime
) / dbSystemTime
;
351 CpuTime
= CpuTime
* 100.0 / (double)SystemBasicInfo
.NumberOfProcessors
; /* + 0.5; */
352 pPerfData
[Idx
].CPUUsage
= (ULONG
)CpuTime
;
354 pPerfData
[Idx
].CPUTime
.QuadPart
= pSPI
->UserTime
.QuadPart
+ pSPI
->KernelTime
.QuadPart
;
355 pPerfData
[Idx
].WorkingSetSizeBytes
= pSPI
->WorkingSetSize
;
356 pPerfData
[Idx
].PeakWorkingSetSizeBytes
= pSPI
->PeakWorkingSetSize
;
358 pPerfData
[Idx
].WorkingSetSizeDelta
= labs((LONG
)pSPI
->WorkingSetSize
- (LONG
)pPDOld
->WorkingSetSizeBytes
);
360 pPerfData
[Idx
].WorkingSetSizeDelta
= 0;
361 pPerfData
[Idx
].PageFaultCount
= pSPI
->PageFaultCount
;
363 pPerfData
[Idx
].PageFaultCountDelta
= labs((LONG
)pSPI
->PageFaultCount
- (LONG
)pPDOld
->PageFaultCount
);
365 pPerfData
[Idx
].PageFaultCountDelta
= 0;
366 pPerfData
[Idx
].VirtualMemorySizeBytes
= pSPI
->VirtualSize
;
367 pPerfData
[Idx
].PagedPoolUsagePages
= pSPI
->QuotaPeakPagedPoolUsage
;
368 pPerfData
[Idx
].NonPagedPoolUsagePages
= pSPI
->QuotaPeakNonPagedPoolUsage
;
369 pPerfData
[Idx
].BasePriority
= pSPI
->BasePriority
;
370 pPerfData
[Idx
].HandleCount
= pSPI
->HandleCount
;
371 pPerfData
[Idx
].ThreadCount
= pSPI
->NumberOfThreads
;
372 pPerfData
[Idx
].SessionId
= pSPI
->SessionId
;
373 pPerfData
[Idx
].UserName
[0] = UNICODE_NULL
;
374 pPerfData
[Idx
].USERObjectCount
= 0;
375 pPerfData
[Idx
].GDIObjectCount
= 0;
376 ProcessUser
= SystemUserSid
;
379 if (pSPI
->UniqueProcessId
!= NULL
) {
380 hProcess
= OpenProcess(PROCESS_QUERY_INFORMATION
| READ_CONTROL
, FALSE
, PtrToUlong(pSPI
->UniqueProcessId
));
382 /* don't query the information of the system process. It's possible but
383 returns Administrators as the owner of the process instead of SYSTEM */
384 if (pSPI
->UniqueProcessId
!= (HANDLE
)0x4)
386 if (OpenProcessToken(hProcess
, TOKEN_QUERY
, &hProcessToken
))
391 Ret
= GetTokenInformation(hProcessToken
, TokenUser
, (LPVOID
)Buffer
, sizeof(Buffer
), &RetLen
);
392 CloseHandle(hProcessToken
);
395 ProcessUser
= ((PTOKEN_USER
)Buffer
)->User
.Sid
;
402 GetSecurityInfo(hProcess
, SE_KERNEL_OBJECT
, OWNER_SECURITY_INFORMATION
, &ProcessUser
, NULL
, NULL
, NULL
, &ProcessSD
);
405 pPerfData
[Idx
].USERObjectCount
= GetGuiResources(hProcess
, GR_USEROBJECTS
);
406 pPerfData
[Idx
].GDIObjectCount
= GetGuiResources(hProcess
, GR_GDIOBJECTS
);
409 GetProcessIoCounters(hProcess
, &pPerfData
[Idx
].IOCounters
);
410 CloseHandle(hProcess
);
416 /* clear information we were unable to fetch */
417 ZeroMemory(&pPerfData
[Idx
].IOCounters
, sizeof(IO_COUNTERS
));
420 cwcUserName
= sizeof(pPerfData
[0].UserName
) / sizeof(pPerfData
[0].UserName
[0]);
421 CachedGetUserFromSid(ProcessUser
, pPerfData
[Idx
].UserName
, &cwcUserName
);
423 if (ProcessSD
!= NULL
)
425 LocalFree((HLOCAL
)ProcessSD
);
428 pPerfData
[Idx
].UserTime
.QuadPart
= pSPI
->UserTime
.QuadPart
;
429 pPerfData
[Idx
].KernelTime
.QuadPart
= pSPI
->KernelTime
.QuadPart
;
430 pSPI
= (PSYSTEM_PROCESS_INFORMATION
)((LPBYTE
)pSPI
+ pSPI
->NextEntryOffset
);
432 HeapFree(GetProcessHeap(), 0, pBuffer
);
434 HeapFree(GetProcessHeap(), 0, pPerfDataOld
);
436 pPerfDataOld
= pPerfData
;
437 LeaveCriticalSection(&PerfDataCriticalSection
);
440 ULONG
PerfDataGetProcessIndex(ULONG pid
)
444 EnterCriticalSection(&PerfDataCriticalSection
);
446 for (idx
= 0; idx
< ProcessCount
; idx
++)
448 if (PtrToUlong(pPerfData
[idx
].ProcessId
) == pid
)
454 LeaveCriticalSection(&PerfDataCriticalSection
);
456 if (idx
== ProcessCount
)
463 ULONG
PerfDataGetProcessCount(void)
466 EnterCriticalSection(&PerfDataCriticalSection
);
467 Result
= ProcessCount
;
468 LeaveCriticalSection(&PerfDataCriticalSection
);
472 ULONG
PerfDataGetProcessorUsage(void)
475 EnterCriticalSection(&PerfDataCriticalSection
);
476 Result
= (ULONG
)dbIdleTime
;
477 LeaveCriticalSection(&PerfDataCriticalSection
);
481 ULONG
PerfDataGetProcessorSystemUsage(void)
484 EnterCriticalSection(&PerfDataCriticalSection
);
485 Result
= (ULONG
)dbKernelTime
;
486 LeaveCriticalSection(&PerfDataCriticalSection
);
490 BOOL
PerfDataGetImageName(ULONG Index
, LPWSTR lpImageName
, ULONG nMaxCount
)
494 EnterCriticalSection(&PerfDataCriticalSection
);
496 if (Index
< ProcessCount
) {
497 wcsncpy(lpImageName
, pPerfData
[Index
].ImageName
, nMaxCount
);
502 LeaveCriticalSection(&PerfDataCriticalSection
);
506 ULONG
PerfDataGetProcessId(ULONG Index
)
510 EnterCriticalSection(&PerfDataCriticalSection
);
512 if (Index
< ProcessCount
)
513 ProcessId
= PtrToUlong(pPerfData
[Index
].ProcessId
);
517 LeaveCriticalSection(&PerfDataCriticalSection
);
522 BOOL
PerfDataGetUserName(ULONG Index
, LPWSTR lpUserName
, ULONG nMaxCount
)
526 EnterCriticalSection(&PerfDataCriticalSection
);
528 if (Index
< ProcessCount
) {
529 wcsncpy(lpUserName
, pPerfData
[Index
].UserName
, nMaxCount
);
535 LeaveCriticalSection(&PerfDataCriticalSection
);
540 BOOL
PerfDataGetCommandLine(ULONG Index
, LPWSTR lpCommandLine
, ULONG nMaxCount
)
542 static const LPWSTR ellipsis
= L
"...";
544 PROCESS_BASIC_INFORMATION pbi
= {0};
545 UNICODE_STRING CommandLineStr
= {0};
547 PVOID ProcessParams
= NULL
;
554 PCMD_LINE_CACHE new_entry
;
557 PCMD_LINE_CACHE cache
= global_cache
;
559 /* [A] Search for a string already in cache? If so, use it */
560 while (cache
&& cache
->pnext
!= NULL
)
562 if (cache
->idx
== Index
&& cache
->str
!= NULL
)
564 /* Found it. Use it, and add some ellipsis at the very end to make it cute */
565 wcsncpy(lpCommandLine
, cache
->str
, CMD_LINE_MIN(nMaxCount
, cache
->len
));
566 wcscpy(lpCommandLine
+ CMD_LINE_MIN(nMaxCount
, cache
->len
) - wcslen(ellipsis
), ellipsis
);
570 cache
= cache
->pnext
;
573 /* [B] We don't; let's allocate and load a value from the process mem... and cache it */
574 ProcessId
= PerfDataGetProcessId(Index
);
576 /* Default blank command line in case things don't work out */
577 wcsncpy(lpCommandLine
, L
"", nMaxCount
);
579 /* Ask for a handle to the target process so that we can read its memory and query stuff */
580 hProcess
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
, FALSE
, ProcessId
);
584 /* First off, get the ProcessEnvironmentBlock location in that process' address space */
585 Status
= NtQueryInformationProcess(hProcess
, 0, &pbi
, sizeof(pbi
), NULL
);
586 if (!NT_SUCCESS(Status
))
589 /* Then get the PEB.ProcessParameters member pointer */
590 result
= ReadProcessMemory(hProcess
,
591 (PVOID
)((ULONG_PTR
)pbi
.PebBaseAddress
+ FIELD_OFFSET(PEB
, ProcessParameters
)),
593 sizeof(ProcessParams
),
598 /* Then copy the PEB->ProcessParameters.CommandLine member
599 to get the pointer to the string buffer and its size */
600 result
= ReadProcessMemory(hProcess
,
601 (PVOID
)((ULONG_PTR
)ProcessParams
+ FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS
, CommandLine
)),
603 sizeof(CommandLineStr
),
608 /* Allocate the next cache entry and its accompanying string in one go */
609 new_entry
= HeapAlloc(GetProcessHeap(),
611 sizeof(CMD_LINE_CACHE
) + CommandLineStr
.Length
+ sizeof(UNICODE_NULL
));
615 new_string
= (LPWSTR
)((ULONG_PTR
)new_entry
+ sizeof(CMD_LINE_CACHE
));
617 /* Bingo, the command line should be stored there,
618 copy the string from the other process */
619 result
= ReadProcessMemory(hProcess
,
620 CommandLineStr
.Buffer
,
622 CommandLineStr
.Length
,
626 /* Weird, after successfully reading the mem of that process
627 various times it fails now, forget it and bail out */
628 HeapFree(GetProcessHeap(), 0, new_entry
);
632 /* Add our pointer to the cache... */
633 new_entry
->idx
= Index
;
634 new_entry
->str
= new_string
;
635 new_entry
->len
= CommandLineStr
.Length
;
638 global_cache
= new_entry
;
640 cache
->pnext
= new_entry
;
642 /* ... and print the buffer for the first time */
643 wcsncpy(lpCommandLine
, new_string
, CMD_LINE_MIN(nMaxCount
, CommandLineStr
.Length
));
646 if (hProcess
) CloseHandle(hProcess
);
650 void PerfDataDeallocCommandLineCache()
652 PCMD_LINE_CACHE cache
= global_cache
;
653 PCMD_LINE_CACHE cache_old
;
655 while (cache
&& cache
->pnext
!= NULL
)
658 cache
= cache
->pnext
;
660 HeapFree(GetProcessHeap(), 0, cache_old
);
664 ULONG
PerfDataGetSessionId(ULONG Index
)
668 EnterCriticalSection(&PerfDataCriticalSection
);
670 if (Index
< ProcessCount
)
671 SessionId
= pPerfData
[Index
].SessionId
;
675 LeaveCriticalSection(&PerfDataCriticalSection
);
680 ULONG
PerfDataGetCPUUsage(ULONG Index
)
684 EnterCriticalSection(&PerfDataCriticalSection
);
686 if (Index
< ProcessCount
)
687 CpuUsage
= pPerfData
[Index
].CPUUsage
;
691 LeaveCriticalSection(&PerfDataCriticalSection
);
696 LARGE_INTEGER
PerfDataGetCPUTime(ULONG Index
)
698 LARGE_INTEGER CpuTime
= {{0,0}};
700 EnterCriticalSection(&PerfDataCriticalSection
);
702 if (Index
< ProcessCount
)
703 CpuTime
= pPerfData
[Index
].CPUTime
;
705 LeaveCriticalSection(&PerfDataCriticalSection
);
710 ULONG
PerfDataGetWorkingSetSizeBytes(ULONG Index
)
712 ULONG WorkingSetSizeBytes
;
714 EnterCriticalSection(&PerfDataCriticalSection
);
716 if (Index
< ProcessCount
)
717 WorkingSetSizeBytes
= pPerfData
[Index
].WorkingSetSizeBytes
;
719 WorkingSetSizeBytes
= 0;
721 LeaveCriticalSection(&PerfDataCriticalSection
);
723 return WorkingSetSizeBytes
;
726 ULONG
PerfDataGetPeakWorkingSetSizeBytes(ULONG Index
)
728 ULONG PeakWorkingSetSizeBytes
;
730 EnterCriticalSection(&PerfDataCriticalSection
);
732 if (Index
< ProcessCount
)
733 PeakWorkingSetSizeBytes
= pPerfData
[Index
].PeakWorkingSetSizeBytes
;
735 PeakWorkingSetSizeBytes
= 0;
737 LeaveCriticalSection(&PerfDataCriticalSection
);
739 return PeakWorkingSetSizeBytes
;
742 ULONG
PerfDataGetWorkingSetSizeDelta(ULONG Index
)
744 ULONG WorkingSetSizeDelta
;
746 EnterCriticalSection(&PerfDataCriticalSection
);
748 if (Index
< ProcessCount
)
749 WorkingSetSizeDelta
= pPerfData
[Index
].WorkingSetSizeDelta
;
751 WorkingSetSizeDelta
= 0;
753 LeaveCriticalSection(&PerfDataCriticalSection
);
755 return WorkingSetSizeDelta
;
758 ULONG
PerfDataGetPageFaultCount(ULONG Index
)
760 ULONG PageFaultCount
;
762 EnterCriticalSection(&PerfDataCriticalSection
);
764 if (Index
< ProcessCount
)
765 PageFaultCount
= pPerfData
[Index
].PageFaultCount
;
769 LeaveCriticalSection(&PerfDataCriticalSection
);
771 return PageFaultCount
;
774 ULONG
PerfDataGetPageFaultCountDelta(ULONG Index
)
776 ULONG PageFaultCountDelta
;
778 EnterCriticalSection(&PerfDataCriticalSection
);
780 if (Index
< ProcessCount
)
781 PageFaultCountDelta
= pPerfData
[Index
].PageFaultCountDelta
;
783 PageFaultCountDelta
= 0;
785 LeaveCriticalSection(&PerfDataCriticalSection
);
787 return PageFaultCountDelta
;
790 ULONG
PerfDataGetVirtualMemorySizeBytes(ULONG Index
)
792 ULONG VirtualMemorySizeBytes
;
794 EnterCriticalSection(&PerfDataCriticalSection
);
796 if (Index
< ProcessCount
)
797 VirtualMemorySizeBytes
= pPerfData
[Index
].VirtualMemorySizeBytes
;
799 VirtualMemorySizeBytes
= 0;
801 LeaveCriticalSection(&PerfDataCriticalSection
);
803 return VirtualMemorySizeBytes
;
806 ULONG
PerfDataGetPagedPoolUsagePages(ULONG Index
)
808 ULONG PagedPoolUsage
;
810 EnterCriticalSection(&PerfDataCriticalSection
);
812 if (Index
< ProcessCount
)
813 PagedPoolUsage
= pPerfData
[Index
].PagedPoolUsagePages
;
817 LeaveCriticalSection(&PerfDataCriticalSection
);
819 return PagedPoolUsage
;
822 ULONG
PerfDataGetNonPagedPoolUsagePages(ULONG Index
)
824 ULONG NonPagedPoolUsage
;
826 EnterCriticalSection(&PerfDataCriticalSection
);
828 if (Index
< ProcessCount
)
829 NonPagedPoolUsage
= pPerfData
[Index
].NonPagedPoolUsagePages
;
831 NonPagedPoolUsage
= 0;
833 LeaveCriticalSection(&PerfDataCriticalSection
);
835 return NonPagedPoolUsage
;
838 ULONG
PerfDataGetBasePriority(ULONG Index
)
842 EnterCriticalSection(&PerfDataCriticalSection
);
844 if (Index
< ProcessCount
)
845 BasePriority
= pPerfData
[Index
].BasePriority
;
849 LeaveCriticalSection(&PerfDataCriticalSection
);
854 ULONG
PerfDataGetHandleCount(ULONG Index
)
858 EnterCriticalSection(&PerfDataCriticalSection
);
860 if (Index
< ProcessCount
)
861 HandleCount
= pPerfData
[Index
].HandleCount
;
865 LeaveCriticalSection(&PerfDataCriticalSection
);
870 ULONG
PerfDataGetThreadCount(ULONG Index
)
874 EnterCriticalSection(&PerfDataCriticalSection
);
876 if (Index
< ProcessCount
)
877 ThreadCount
= pPerfData
[Index
].ThreadCount
;
881 LeaveCriticalSection(&PerfDataCriticalSection
);
886 ULONG
PerfDataGetUSERObjectCount(ULONG Index
)
888 ULONG USERObjectCount
;
890 EnterCriticalSection(&PerfDataCriticalSection
);
892 if (Index
< ProcessCount
)
893 USERObjectCount
= pPerfData
[Index
].USERObjectCount
;
897 LeaveCriticalSection(&PerfDataCriticalSection
);
899 return USERObjectCount
;
902 ULONG
PerfDataGetGDIObjectCount(ULONG Index
)
904 ULONG GDIObjectCount
;
906 EnterCriticalSection(&PerfDataCriticalSection
);
908 if (Index
< ProcessCount
)
909 GDIObjectCount
= pPerfData
[Index
].GDIObjectCount
;
913 LeaveCriticalSection(&PerfDataCriticalSection
);
915 return GDIObjectCount
;
918 BOOL
PerfDataGetIOCounters(ULONG Index
, PIO_COUNTERS pIoCounters
)
922 EnterCriticalSection(&PerfDataCriticalSection
);
924 if (Index
< ProcessCount
)
926 memcpy(pIoCounters
, &pPerfData
[Index
].IOCounters
, sizeof(IO_COUNTERS
));
932 LeaveCriticalSection(&PerfDataCriticalSection
);
937 ULONG
PerfDataGetCommitChargeTotalK(void)
942 EnterCriticalSection(&PerfDataCriticalSection
);
944 Total
= SystemPerfInfo
.CommittedPages
;
945 PageSize
= SystemBasicInfo
.PageSize
;
947 LeaveCriticalSection(&PerfDataCriticalSection
);
949 Total
= Total
* (PageSize
/ 1024);
954 ULONG
PerfDataGetCommitChargeLimitK(void)
959 EnterCriticalSection(&PerfDataCriticalSection
);
961 Limit
= SystemPerfInfo
.CommitLimit
;
962 PageSize
= SystemBasicInfo
.PageSize
;
964 LeaveCriticalSection(&PerfDataCriticalSection
);
966 Limit
= Limit
* (PageSize
/ 1024);
971 ULONG
PerfDataGetCommitChargePeakK(void)
976 EnterCriticalSection(&PerfDataCriticalSection
);
978 Peak
= SystemPerfInfo
.PeakCommitment
;
979 PageSize
= SystemBasicInfo
.PageSize
;
981 LeaveCriticalSection(&PerfDataCriticalSection
);
983 Peak
= Peak
* (PageSize
/ 1024);
988 ULONG
PerfDataGetKernelMemoryTotalK(void)
995 EnterCriticalSection(&PerfDataCriticalSection
);
997 Paged
= SystemPerfInfo
.PagedPoolPages
;
998 NonPaged
= SystemPerfInfo
.NonPagedPoolPages
;
999 PageSize
= SystemBasicInfo
.PageSize
;
1001 LeaveCriticalSection(&PerfDataCriticalSection
);
1003 Paged
= Paged
* (PageSize
/ 1024);
1004 NonPaged
= NonPaged
* (PageSize
/ 1024);
1006 Total
= Paged
+ NonPaged
;
1011 ULONG
PerfDataGetKernelMemoryPagedK(void)
1016 EnterCriticalSection(&PerfDataCriticalSection
);
1018 Paged
= SystemPerfInfo
.PagedPoolPages
;
1019 PageSize
= SystemBasicInfo
.PageSize
;
1021 LeaveCriticalSection(&PerfDataCriticalSection
);
1023 Paged
= Paged
* (PageSize
/ 1024);
1028 ULONG
PerfDataGetKernelMemoryNonPagedK(void)
1033 EnterCriticalSection(&PerfDataCriticalSection
);
1035 NonPaged
= SystemPerfInfo
.NonPagedPoolPages
;
1036 PageSize
= SystemBasicInfo
.PageSize
;
1038 LeaveCriticalSection(&PerfDataCriticalSection
);
1040 NonPaged
= NonPaged
* (PageSize
/ 1024);
1045 ULONG
PerfDataGetPhysicalMemoryTotalK(void)
1050 EnterCriticalSection(&PerfDataCriticalSection
);
1052 Total
= SystemBasicInfo
.NumberOfPhysicalPages
;
1053 PageSize
= SystemBasicInfo
.PageSize
;
1055 LeaveCriticalSection(&PerfDataCriticalSection
);
1057 Total
= Total
* (PageSize
/ 1024);
1062 ULONG
PerfDataGetPhysicalMemoryAvailableK(void)
1067 EnterCriticalSection(&PerfDataCriticalSection
);
1069 Available
= SystemPerfInfo
.AvailablePages
;
1070 PageSize
= SystemBasicInfo
.PageSize
;
1072 LeaveCriticalSection(&PerfDataCriticalSection
);
1074 Available
= Available
* (PageSize
/ 1024);
1079 ULONG
PerfDataGetPhysicalMemorySystemCacheK(void)
1084 EnterCriticalSection(&PerfDataCriticalSection
);
1086 PageSize
= SystemBasicInfo
.PageSize
;
1087 SystemCache
= SystemCacheInfo
.CurrentSizeIncludingTransitionInPages
* PageSize
;
1089 LeaveCriticalSection(&PerfDataCriticalSection
);
1091 return SystemCache
/ 1024;
1094 ULONG
PerfDataGetSystemHandleCount(void)
1098 EnterCriticalSection(&PerfDataCriticalSection
);
1100 HandleCount
= SystemNumberOfHandles
;
1102 LeaveCriticalSection(&PerfDataCriticalSection
);
1107 ULONG
PerfDataGetTotalThreadCount(void)
1109 ULONG ThreadCount
= 0;
1112 EnterCriticalSection(&PerfDataCriticalSection
);
1114 for (i
=0; i
<ProcessCount
; i
++)
1116 ThreadCount
+= pPerfData
[i
].ThreadCount
;
1119 LeaveCriticalSection(&PerfDataCriticalSection
);
1124 BOOL
PerfDataGet(ULONG Index
, PPERFDATA
*lppData
)
1126 BOOL bSuccessful
= FALSE
;
1128 EnterCriticalSection(&PerfDataCriticalSection
);
1129 if (Index
< ProcessCount
)
1131 *lppData
= pPerfData
+ Index
;
1134 LeaveCriticalSection(&PerfDataCriticalSection
);