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 SYSTEM_HANDLE_INFORMATION SystemHandleInfo
;
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
);
111 static void SidToUserName(PSID Sid
, LPWSTR szBuffer
, DWORD BufferSize
)
113 static WCHAR szDomainNameUnused
[255];
114 DWORD DomainNameLen
= sizeof(szDomainNameUnused
) / sizeof(szDomainNameUnused
[0]);
118 LookupAccountSidW(NULL
, Sid
, szBuffer
, &BufferSize
, szDomainNameUnused
, &DomainNameLen
, &Use
);
123 CachedGetUserFromSid(
129 PSIDTOUSERNAME pEntry
;
130 ULONG cbSid
, cwcUserName
;
132 cwcUserName
= *pcwcUserName
;
134 /* Walk through the list */
135 for(pCur
= SidToUserNameHead
.Flink
;
136 pCur
!= &SidToUserNameHead
;
139 pEntry
= CONTAINING_RECORD(pCur
, SIDTOUSERNAME
, List
);
140 if (EqualSid((PSID
)&pEntry
->Data
, pSid
))
142 wcsncpy(pUserName
, pEntry
->pszName
, cwcUserName
);
143 *pcwcUserName
= cwcUserName
;
148 /* We didn't find the SID in the list, get the name conventional */
149 SidToUserName(pSid
, pUserName
, cwcUserName
);
151 /* Allocate a new entry */
152 *pcwcUserName
= wcslen(pUserName
);
153 cwcUserName
= *pcwcUserName
+ 1;
154 cbSid
= GetLengthSid(pSid
);
155 pEntry
= HeapAlloc(GetProcessHeap(), 0, sizeof(SIDTOUSERNAME
) + cbSid
+ cwcUserName
* sizeof(WCHAR
));
157 /* Copy the Sid and name to our entry */
158 CopySid(cbSid
, (PSID
)&pEntry
->Data
, pSid
);
159 pEntry
->pszName
= (LPWSTR
)(pEntry
->Data
+ cbSid
);
160 wcsncpy(pEntry
->pszName
, pUserName
, cwcUserName
);
162 /* Insert the new entry */
163 pEntry
->List
.Flink
= &SidToUserNameHead
;
164 pEntry
->List
.Blink
= SidToUserNameHead
.Blink
;
165 SidToUserNameHead
.Blink
->Flink
= &pEntry
->List
;
166 SidToUserNameHead
.Blink
= &pEntry
->List
;
171 void PerfDataRefresh(void)
177 PSYSTEM_PROCESS_INFORMATION pSPI
;
181 HANDLE hProcessToken
;
182 SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo
;
183 SYSTEM_TIMEOFDAY_INFORMATION SysTimeInfo
;
184 SYSTEM_FILECACHE_INFORMATION SysCacheInfo
;
185 LPBYTE SysHandleInfoData
;
186 PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SysProcessorTimeInfo
;
187 double CurrentKernelTime
;
188 PSECURITY_DESCRIPTOR ProcessSD
;
190 ULONG Buffer
[64]; /* must be 4 bytes aligned! */
193 /* Get new system time */
194 status
= NtQuerySystemInformation(SystemTimeOfDayInformation
, &SysTimeInfo
, sizeof(SysTimeInfo
), NULL
);
195 if (!NT_SUCCESS(status
))
198 /* Get new CPU's idle time */
199 status
= NtQuerySystemInformation(SystemPerformanceInformation
, &SysPerfInfo
, sizeof(SysPerfInfo
), NULL
);
200 if (!NT_SUCCESS(status
))
203 /* Get system cache information */
204 status
= NtQuerySystemInformation(SystemFileCacheInformation
, &SysCacheInfo
, sizeof(SysCacheInfo
), NULL
);
205 if (!NT_SUCCESS(status
))
208 /* Get processor time information */
209 SysProcessorTimeInfo
= (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
)HeapAlloc(GetProcessHeap(), 0, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
) * SystemBasicInfo
.NumberOfProcessors
);
210 status
= NtQuerySystemInformation(SystemProcessorPerformanceInformation
, SysProcessorTimeInfo
, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
) * SystemBasicInfo
.NumberOfProcessors
, &ulSize
);
212 if (!NT_SUCCESS(status
))
214 if (SysProcessorTimeInfo
!= NULL
)
215 HeapFree(GetProcessHeap(), 0, SysProcessorTimeInfo
);
219 /* Get handle information
220 * We don't know how much data there is so just keep
221 * increasing the buffer size until the call succeeds
226 BufferSize
+= 0x10000;
227 SysHandleInfoData
= (LPBYTE
)HeapAlloc(GetProcessHeap(), 0, BufferSize
);
229 status
= NtQuerySystemInformation(SystemHandleInformation
, SysHandleInfoData
, BufferSize
, &ulSize
);
231 if (status
== STATUS_INFO_LENGTH_MISMATCH
) {
232 HeapFree(GetProcessHeap(), 0, SysHandleInfoData
);
235 } while (status
== STATUS_INFO_LENGTH_MISMATCH
);
237 /* Get process information
238 * We don't know how much data there is so just keep
239 * increasing the buffer size until the call succeeds
244 BufferSize
+= 0x10000;
245 pBuffer
= (LPBYTE
)HeapAlloc(GetProcessHeap(), 0, BufferSize
);
247 status
= NtQuerySystemInformation(SystemProcessInformation
, pBuffer
, BufferSize
, &ulSize
);
249 if (status
== STATUS_INFO_LENGTH_MISMATCH
) {
250 HeapFree(GetProcessHeap(), 0, pBuffer
);
253 } while (status
== STATUS_INFO_LENGTH_MISMATCH
);
255 EnterCriticalSection(&PerfDataCriticalSection
);
258 * Save system performance info
260 memcpy(&SystemPerfInfo
, &SysPerfInfo
, sizeof(SYSTEM_PERFORMANCE_INFORMATION
));
263 * Save system cache info
265 memcpy(&SystemCacheInfo
, &SysCacheInfo
, sizeof(SYSTEM_FILECACHE_INFORMATION
));
268 * Save system processor time info
270 if (SystemProcessorTimeInfo
) {
271 HeapFree(GetProcessHeap(), 0, SystemProcessorTimeInfo
);
273 SystemProcessorTimeInfo
= SysProcessorTimeInfo
;
276 * Save system handle info
278 memcpy(&SystemHandleInfo
, SysHandleInfoData
, sizeof(SYSTEM_HANDLE_INFORMATION
));
279 HeapFree(GetProcessHeap(), 0, SysHandleInfoData
);
281 for (CurrentKernelTime
=0, Idx
=0; Idx
<(ULONG
)SystemBasicInfo
.NumberOfProcessors
; Idx
++) {
282 CurrentKernelTime
+= Li2Double(SystemProcessorTimeInfo
[Idx
].KernelTime
);
283 CurrentKernelTime
+= Li2Double(SystemProcessorTimeInfo
[Idx
].DpcTime
);
284 CurrentKernelTime
+= Li2Double(SystemProcessorTimeInfo
[Idx
].InterruptTime
);
287 /* If it's a first call - skip idle time calcs */
288 if (liOldIdleTime
.QuadPart
!= 0) {
289 /* CurrentValue = NewValue - OldValue */
290 dbIdleTime
= Li2Double(SysPerfInfo
.IdleProcessTime
) - Li2Double(liOldIdleTime
);
291 dbKernelTime
= CurrentKernelTime
- OldKernelTime
;
292 dbSystemTime
= Li2Double(SysTimeInfo
.CurrentTime
) - Li2Double(liOldSystemTime
);
294 /* CurrentCpuIdle = IdleTime / SystemTime */
295 dbIdleTime
= dbIdleTime
/ dbSystemTime
;
296 dbKernelTime
= dbKernelTime
/ dbSystemTime
;
298 /* CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors */
299 dbIdleTime
= 100.0 - dbIdleTime
* 100.0 / (double)SystemBasicInfo
.NumberOfProcessors
; /* + 0.5; */
300 dbKernelTime
= 100.0 - dbKernelTime
* 100.0 / (double)SystemBasicInfo
.NumberOfProcessors
; /* + 0.5; */
303 /* Store new CPU's idle and system time */
304 liOldIdleTime
= SysPerfInfo
.IdleProcessTime
;
305 liOldSystemTime
= SysTimeInfo
.CurrentTime
;
306 OldKernelTime
= CurrentKernelTime
;
308 /* Determine the process count
309 * We loop through the data we got from NtQuerySystemInformation
310 * and count how many structures there are (until RelativeOffset is 0)
312 ProcessCountOld
= ProcessCount
;
314 pSPI
= (PSYSTEM_PROCESS_INFORMATION
)pBuffer
;
317 if (pSPI
->NextEntryOffset
== 0)
319 pSPI
= (PSYSTEM_PROCESS_INFORMATION
)((LPBYTE
)pSPI
+ pSPI
->NextEntryOffset
);
322 /* Now alloc a new PERFDATA array and fill in the data */
324 HeapFree(GetProcessHeap(), 0, pPerfDataOld
);
326 pPerfDataOld
= pPerfData
;
327 /* Clear out process perf data structures with HEAP_ZERO_MEMORY flag: */
328 pPerfData
= (PPERFDATA
)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(PERFDATA
) * ProcessCount
);
329 pSPI
= (PSYSTEM_PROCESS_INFORMATION
)pBuffer
;
330 for (Idx
=0; Idx
<ProcessCount
; Idx
++) {
331 /* Get the old perf data for this process (if any) */
332 /* so that we can establish delta values */
334 for (Idx2
=0; Idx2
<ProcessCountOld
; Idx2
++) {
335 if (pPerfDataOld
[Idx2
].ProcessId
== pSPI
->UniqueProcessId
) {
336 pPDOld
= &pPerfDataOld
[Idx2
];
341 if (pSPI
->ImageName
.Buffer
) {
342 /* Don't assume a UNICODE_STRING Buffer is zero terminated: */
343 int len
= pSPI
->ImageName
.Length
/ 2;
344 /* Check against max size and allow for terminating zero (already zeroed): */
345 if(len
>= MAX_PATH
)len
=MAX_PATH
- 1;
346 wcsncpy(pPerfData
[Idx
].ImageName
, pSPI
->ImageName
.Buffer
, len
);
348 LoadStringW(hInst
, IDS_IDLE_PROCESS
, pPerfData
[Idx
].ImageName
,
349 sizeof(pPerfData
[Idx
].ImageName
) / sizeof(pPerfData
[Idx
].ImageName
[0]));
352 pPerfData
[Idx
].ProcessId
= pSPI
->UniqueProcessId
;
355 double CurTime
= Li2Double(pSPI
->KernelTime
) + Li2Double(pSPI
->UserTime
);
356 double OldTime
= Li2Double(pPDOld
->KernelTime
) + Li2Double(pPDOld
->UserTime
);
357 double CpuTime
= (CurTime
- OldTime
) / dbSystemTime
;
358 CpuTime
= CpuTime
* 100.0 / (double)SystemBasicInfo
.NumberOfProcessors
; /* + 0.5; */
359 pPerfData
[Idx
].CPUUsage
= (ULONG
)CpuTime
;
361 pPerfData
[Idx
].CPUTime
.QuadPart
= pSPI
->UserTime
.QuadPart
+ pSPI
->KernelTime
.QuadPart
;
362 pPerfData
[Idx
].WorkingSetSizeBytes
= pSPI
->WorkingSetSize
;
363 pPerfData
[Idx
].PeakWorkingSetSizeBytes
= pSPI
->PeakWorkingSetSize
;
365 pPerfData
[Idx
].WorkingSetSizeDelta
= labs((LONG
)pSPI
->WorkingSetSize
- (LONG
)pPDOld
->WorkingSetSizeBytes
);
367 pPerfData
[Idx
].WorkingSetSizeDelta
= 0;
368 pPerfData
[Idx
].PageFaultCount
= pSPI
->PageFaultCount
;
370 pPerfData
[Idx
].PageFaultCountDelta
= labs((LONG
)pSPI
->PageFaultCount
- (LONG
)pPDOld
->PageFaultCount
);
372 pPerfData
[Idx
].PageFaultCountDelta
= 0;
373 pPerfData
[Idx
].VirtualMemorySizeBytes
= pSPI
->VirtualSize
;
374 pPerfData
[Idx
].PagedPoolUsagePages
= pSPI
->QuotaPeakPagedPoolUsage
;
375 pPerfData
[Idx
].NonPagedPoolUsagePages
= pSPI
->QuotaPeakNonPagedPoolUsage
;
376 pPerfData
[Idx
].BasePriority
= pSPI
->BasePriority
;
377 pPerfData
[Idx
].HandleCount
= pSPI
->HandleCount
;
378 pPerfData
[Idx
].ThreadCount
= pSPI
->NumberOfThreads
;
379 pPerfData
[Idx
].SessionId
= pSPI
->SessionId
;
380 pPerfData
[Idx
].UserName
[0] = UNICODE_NULL
;
381 pPerfData
[Idx
].USERObjectCount
= 0;
382 pPerfData
[Idx
].GDIObjectCount
= 0;
383 ProcessUser
= SystemUserSid
;
386 if (pSPI
->UniqueProcessId
!= NULL
) {
387 hProcess
= OpenProcess(PROCESS_QUERY_INFORMATION
| READ_CONTROL
, FALSE
, PtrToUlong(pSPI
->UniqueProcessId
));
389 /* don't query the information of the system process. It's possible but
390 returns Administrators as the owner of the process instead of SYSTEM */
391 if (pSPI
->UniqueProcessId
!= (HANDLE
)0x4)
393 if (OpenProcessToken(hProcess
, TOKEN_QUERY
, &hProcessToken
))
398 Ret
= GetTokenInformation(hProcessToken
, TokenUser
, (LPVOID
)Buffer
, sizeof(Buffer
), &RetLen
);
399 CloseHandle(hProcessToken
);
402 ProcessUser
= ((PTOKEN_USER
)Buffer
)->User
.Sid
;
409 GetSecurityInfo(hProcess
, SE_KERNEL_OBJECT
, OWNER_SECURITY_INFORMATION
, &ProcessUser
, NULL
, NULL
, NULL
, &ProcessSD
);
412 pPerfData
[Idx
].USERObjectCount
= GetGuiResources(hProcess
, GR_USEROBJECTS
);
413 pPerfData
[Idx
].GDIObjectCount
= GetGuiResources(hProcess
, GR_GDIOBJECTS
);
416 GetProcessIoCounters(hProcess
, &pPerfData
[Idx
].IOCounters
);
417 CloseHandle(hProcess
);
423 /* clear information we were unable to fetch */
424 ZeroMemory(&pPerfData
[Idx
].IOCounters
, sizeof(IO_COUNTERS
));
427 cwcUserName
= sizeof(pPerfData
[0].UserName
) / sizeof(pPerfData
[0].UserName
[0]);
428 CachedGetUserFromSid(ProcessUser
, pPerfData
[Idx
].UserName
, &cwcUserName
);
430 if (ProcessSD
!= NULL
)
432 LocalFree((HLOCAL
)ProcessSD
);
435 pPerfData
[Idx
].UserTime
.QuadPart
= pSPI
->UserTime
.QuadPart
;
436 pPerfData
[Idx
].KernelTime
.QuadPart
= pSPI
->KernelTime
.QuadPart
;
437 pSPI
= (PSYSTEM_PROCESS_INFORMATION
)((LPBYTE
)pSPI
+ pSPI
->NextEntryOffset
);
439 HeapFree(GetProcessHeap(), 0, pBuffer
);
440 LeaveCriticalSection(&PerfDataCriticalSection
);
443 ULONG
PerfDataGetProcessIndex(ULONG pid
)
447 EnterCriticalSection(&PerfDataCriticalSection
);
449 for (idx
= 0; idx
< ProcessCount
; idx
++)
451 if (PtrToUlong(pPerfData
[idx
].ProcessId
) == pid
)
457 LeaveCriticalSection(&PerfDataCriticalSection
);
459 if (idx
== ProcessCount
)
466 ULONG
PerfDataGetProcessCount(void)
471 ULONG
PerfDataGetProcessorUsage(void)
473 return (ULONG
)dbIdleTime
;
476 ULONG
PerfDataGetProcessorSystemUsage(void)
478 return (ULONG
)dbKernelTime
;
481 BOOL
PerfDataGetImageName(ULONG Index
, LPWSTR lpImageName
, ULONG nMaxCount
)
485 EnterCriticalSection(&PerfDataCriticalSection
);
487 if (Index
< ProcessCount
) {
488 wcsncpy(lpImageName
, pPerfData
[Index
].ImageName
, nMaxCount
);
493 LeaveCriticalSection(&PerfDataCriticalSection
);
497 ULONG
PerfDataGetProcessId(ULONG Index
)
501 EnterCriticalSection(&PerfDataCriticalSection
);
503 if (Index
< ProcessCount
)
504 ProcessId
= PtrToUlong(pPerfData
[Index
].ProcessId
);
508 LeaveCriticalSection(&PerfDataCriticalSection
);
513 BOOL
PerfDataGetUserName(ULONG Index
, LPWSTR lpUserName
, ULONG nMaxCount
)
517 EnterCriticalSection(&PerfDataCriticalSection
);
519 if (Index
< ProcessCount
) {
520 wcsncpy(lpUserName
, pPerfData
[Index
].UserName
, nMaxCount
);
526 LeaveCriticalSection(&PerfDataCriticalSection
);
531 BOOL
PerfDataGetCommandLine(ULONG Index
, LPWSTR lpCommandLine
, ULONG nMaxCount
)
533 static const LPWSTR ellipsis
= L
"...";
535 PROCESS_BASIC_INFORMATION pbi
= {0};
536 UNICODE_STRING CommandLineStr
= {0};
538 PVOID ProcessParams
= NULL
;
545 PCMD_LINE_CACHE new_entry
;
548 PCMD_LINE_CACHE cache
= global_cache
;
550 /* [A] Search for a string already in cache? If so, use it */
551 while (cache
&& cache
->pnext
!= NULL
)
553 if (cache
->idx
== Index
&& cache
->str
!= NULL
)
555 /* Found it. Use it, and add some ellipsis at the very end to make it cute */
556 wcsncpy(lpCommandLine
, cache
->str
, CMD_LINE_MIN(nMaxCount
, cache
->len
));
557 wcscpy(lpCommandLine
+ CMD_LINE_MIN(nMaxCount
, cache
->len
) - sizeof(ellipsis
)/sizeof(WCHAR
), ellipsis
);
561 cache
= cache
->pnext
;
564 /* [B] We don't; let's allocate and load a value from the process mem... and cache it */
565 ProcessId
= PerfDataGetProcessId(Index
);
567 /* Default blank command line in case things don't work out */
568 wcsncpy(lpCommandLine
, L
"", nMaxCount
);
570 /* Ask for a handle to the target process so that we can read its memory and query stuff */
571 hProcess
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
, FALSE
, ProcessId
);
575 /* First off, get the ProcessEnvironmentBlock location in that process' address space */
576 Status
= NtQueryInformationProcess(hProcess
, 0, &pbi
, sizeof(pbi
), NULL
);
577 if (!NT_SUCCESS(Status
))
580 /* Then get the PEB.ProcessParameters member pointer */
581 result
= ReadProcessMemory(hProcess
,
582 (PVOID
)((ULONG_PTR
)pbi
.PebBaseAddress
+ FIELD_OFFSET(PEB
, ProcessParameters
)),
584 sizeof(ProcessParams
),
589 /* Then copy the PEB->ProcessParameters.CommandLine member
590 to get the pointer to the string buffer and its size */
591 result
= ReadProcessMemory(hProcess
,
592 (PVOID
)((ULONG_PTR
)ProcessParams
+ FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS
, CommandLine
)),
594 sizeof(CommandLineStr
),
599 /* Allocate the next cache entry and its accompanying string in one go */
600 new_entry
= HeapAlloc(GetProcessHeap(),
602 sizeof(CMD_LINE_CACHE
) + CommandLineStr
.Length
+ sizeof(UNICODE_NULL
));
606 new_string
= (LPWSTR
)((ULONG_PTR
)new_entry
+ sizeof(CMD_LINE_CACHE
));
608 /* Bingo, the command line should be stored there,
609 copy the string from the other process */
610 result
= ReadProcessMemory(hProcess
,
611 CommandLineStr
.Buffer
,
613 CommandLineStr
.Length
,
617 /* Weird, after sucessfully reading the mem of that process
618 various times it fails now, forget it and bail out */
619 HeapFree(GetProcessHeap(), 0, new_entry
);
623 /* Add our pointer to the cache... */
624 new_entry
->idx
= Index
;
625 new_entry
->str
= new_string
;
626 new_entry
->len
= CommandLineStr
.Length
;
629 global_cache
= new_entry
;
631 cache
->pnext
= new_entry
;
633 /* ... and print the buffer for the first time */
634 wcsncpy(lpCommandLine
, new_string
, CMD_LINE_MIN(nMaxCount
, CommandLineStr
.Length
));
637 if (hProcess
) CloseHandle(hProcess
);
641 void PerfDataDeallocCommandLineCache()
643 PCMD_LINE_CACHE cache
= global_cache
;
644 PCMD_LINE_CACHE cache_old
;
646 while (cache
&& cache
->pnext
!= NULL
)
649 cache
= cache
->pnext
;
651 HeapFree(GetProcessHeap(), 0, cache_old
);
655 ULONG
PerfDataGetSessionId(ULONG Index
)
659 EnterCriticalSection(&PerfDataCriticalSection
);
661 if (Index
< ProcessCount
)
662 SessionId
= pPerfData
[Index
].SessionId
;
666 LeaveCriticalSection(&PerfDataCriticalSection
);
671 ULONG
PerfDataGetCPUUsage(ULONG Index
)
675 EnterCriticalSection(&PerfDataCriticalSection
);
677 if (Index
< ProcessCount
)
678 CpuUsage
= pPerfData
[Index
].CPUUsage
;
682 LeaveCriticalSection(&PerfDataCriticalSection
);
687 LARGE_INTEGER
PerfDataGetCPUTime(ULONG Index
)
689 LARGE_INTEGER CpuTime
= {{0,0}};
691 EnterCriticalSection(&PerfDataCriticalSection
);
693 if (Index
< ProcessCount
)
694 CpuTime
= pPerfData
[Index
].CPUTime
;
696 LeaveCriticalSection(&PerfDataCriticalSection
);
701 ULONG
PerfDataGetWorkingSetSizeBytes(ULONG Index
)
703 ULONG WorkingSetSizeBytes
;
705 EnterCriticalSection(&PerfDataCriticalSection
);
707 if (Index
< ProcessCount
)
708 WorkingSetSizeBytes
= pPerfData
[Index
].WorkingSetSizeBytes
;
710 WorkingSetSizeBytes
= 0;
712 LeaveCriticalSection(&PerfDataCriticalSection
);
714 return WorkingSetSizeBytes
;
717 ULONG
PerfDataGetPeakWorkingSetSizeBytes(ULONG Index
)
719 ULONG PeakWorkingSetSizeBytes
;
721 EnterCriticalSection(&PerfDataCriticalSection
);
723 if (Index
< ProcessCount
)
724 PeakWorkingSetSizeBytes
= pPerfData
[Index
].PeakWorkingSetSizeBytes
;
726 PeakWorkingSetSizeBytes
= 0;
728 LeaveCriticalSection(&PerfDataCriticalSection
);
730 return PeakWorkingSetSizeBytes
;
733 ULONG
PerfDataGetWorkingSetSizeDelta(ULONG Index
)
735 ULONG WorkingSetSizeDelta
;
737 EnterCriticalSection(&PerfDataCriticalSection
);
739 if (Index
< ProcessCount
)
740 WorkingSetSizeDelta
= pPerfData
[Index
].WorkingSetSizeDelta
;
742 WorkingSetSizeDelta
= 0;
744 LeaveCriticalSection(&PerfDataCriticalSection
);
746 return WorkingSetSizeDelta
;
749 ULONG
PerfDataGetPageFaultCount(ULONG Index
)
751 ULONG PageFaultCount
;
753 EnterCriticalSection(&PerfDataCriticalSection
);
755 if (Index
< ProcessCount
)
756 PageFaultCount
= pPerfData
[Index
].PageFaultCount
;
760 LeaveCriticalSection(&PerfDataCriticalSection
);
762 return PageFaultCount
;
765 ULONG
PerfDataGetPageFaultCountDelta(ULONG Index
)
767 ULONG PageFaultCountDelta
;
769 EnterCriticalSection(&PerfDataCriticalSection
);
771 if (Index
< ProcessCount
)
772 PageFaultCountDelta
= pPerfData
[Index
].PageFaultCountDelta
;
774 PageFaultCountDelta
= 0;
776 LeaveCriticalSection(&PerfDataCriticalSection
);
778 return PageFaultCountDelta
;
781 ULONG
PerfDataGetVirtualMemorySizeBytes(ULONG Index
)
783 ULONG VirtualMemorySizeBytes
;
785 EnterCriticalSection(&PerfDataCriticalSection
);
787 if (Index
< ProcessCount
)
788 VirtualMemorySizeBytes
= pPerfData
[Index
].VirtualMemorySizeBytes
;
790 VirtualMemorySizeBytes
= 0;
792 LeaveCriticalSection(&PerfDataCriticalSection
);
794 return VirtualMemorySizeBytes
;
797 ULONG
PerfDataGetPagedPoolUsagePages(ULONG Index
)
799 ULONG PagedPoolUsage
;
801 EnterCriticalSection(&PerfDataCriticalSection
);
803 if (Index
< ProcessCount
)
804 PagedPoolUsage
= pPerfData
[Index
].PagedPoolUsagePages
;
808 LeaveCriticalSection(&PerfDataCriticalSection
);
810 return PagedPoolUsage
;
813 ULONG
PerfDataGetNonPagedPoolUsagePages(ULONG Index
)
815 ULONG NonPagedPoolUsage
;
817 EnterCriticalSection(&PerfDataCriticalSection
);
819 if (Index
< ProcessCount
)
820 NonPagedPoolUsage
= pPerfData
[Index
].NonPagedPoolUsagePages
;
822 NonPagedPoolUsage
= 0;
824 LeaveCriticalSection(&PerfDataCriticalSection
);
826 return NonPagedPoolUsage
;
829 ULONG
PerfDataGetBasePriority(ULONG Index
)
833 EnterCriticalSection(&PerfDataCriticalSection
);
835 if (Index
< ProcessCount
)
836 BasePriority
= pPerfData
[Index
].BasePriority
;
840 LeaveCriticalSection(&PerfDataCriticalSection
);
845 ULONG
PerfDataGetHandleCount(ULONG Index
)
849 EnterCriticalSection(&PerfDataCriticalSection
);
851 if (Index
< ProcessCount
)
852 HandleCount
= pPerfData
[Index
].HandleCount
;
856 LeaveCriticalSection(&PerfDataCriticalSection
);
861 ULONG
PerfDataGetThreadCount(ULONG Index
)
865 EnterCriticalSection(&PerfDataCriticalSection
);
867 if (Index
< ProcessCount
)
868 ThreadCount
= pPerfData
[Index
].ThreadCount
;
872 LeaveCriticalSection(&PerfDataCriticalSection
);
877 ULONG
PerfDataGetUSERObjectCount(ULONG Index
)
879 ULONG USERObjectCount
;
881 EnterCriticalSection(&PerfDataCriticalSection
);
883 if (Index
< ProcessCount
)
884 USERObjectCount
= pPerfData
[Index
].USERObjectCount
;
888 LeaveCriticalSection(&PerfDataCriticalSection
);
890 return USERObjectCount
;
893 ULONG
PerfDataGetGDIObjectCount(ULONG Index
)
895 ULONG GDIObjectCount
;
897 EnterCriticalSection(&PerfDataCriticalSection
);
899 if (Index
< ProcessCount
)
900 GDIObjectCount
= pPerfData
[Index
].GDIObjectCount
;
904 LeaveCriticalSection(&PerfDataCriticalSection
);
906 return GDIObjectCount
;
909 BOOL
PerfDataGetIOCounters(ULONG Index
, PIO_COUNTERS pIoCounters
)
913 EnterCriticalSection(&PerfDataCriticalSection
);
915 if (Index
< ProcessCount
)
917 memcpy(pIoCounters
, &pPerfData
[Index
].IOCounters
, sizeof(IO_COUNTERS
));
923 LeaveCriticalSection(&PerfDataCriticalSection
);
928 ULONG
PerfDataGetCommitChargeTotalK(void)
933 EnterCriticalSection(&PerfDataCriticalSection
);
935 Total
= SystemPerfInfo
.CommittedPages
;
936 PageSize
= SystemBasicInfo
.PageSize
;
938 LeaveCriticalSection(&PerfDataCriticalSection
);
940 Total
= Total
* (PageSize
/ 1024);
945 ULONG
PerfDataGetCommitChargeLimitK(void)
950 EnterCriticalSection(&PerfDataCriticalSection
);
952 Limit
= SystemPerfInfo
.CommitLimit
;
953 PageSize
= SystemBasicInfo
.PageSize
;
955 LeaveCriticalSection(&PerfDataCriticalSection
);
957 Limit
= Limit
* (PageSize
/ 1024);
962 ULONG
PerfDataGetCommitChargePeakK(void)
967 EnterCriticalSection(&PerfDataCriticalSection
);
969 Peak
= SystemPerfInfo
.PeakCommitment
;
970 PageSize
= SystemBasicInfo
.PageSize
;
972 LeaveCriticalSection(&PerfDataCriticalSection
);
974 Peak
= Peak
* (PageSize
/ 1024);
979 ULONG
PerfDataGetKernelMemoryTotalK(void)
986 EnterCriticalSection(&PerfDataCriticalSection
);
988 Paged
= SystemPerfInfo
.PagedPoolPages
;
989 NonPaged
= SystemPerfInfo
.NonPagedPoolPages
;
990 PageSize
= SystemBasicInfo
.PageSize
;
992 LeaveCriticalSection(&PerfDataCriticalSection
);
994 Paged
= Paged
* (PageSize
/ 1024);
995 NonPaged
= NonPaged
* (PageSize
/ 1024);
997 Total
= Paged
+ NonPaged
;
1002 ULONG
PerfDataGetKernelMemoryPagedK(void)
1007 EnterCriticalSection(&PerfDataCriticalSection
);
1009 Paged
= SystemPerfInfo
.PagedPoolPages
;
1010 PageSize
= SystemBasicInfo
.PageSize
;
1012 LeaveCriticalSection(&PerfDataCriticalSection
);
1014 Paged
= Paged
* (PageSize
/ 1024);
1019 ULONG
PerfDataGetKernelMemoryNonPagedK(void)
1024 EnterCriticalSection(&PerfDataCriticalSection
);
1026 NonPaged
= SystemPerfInfo
.NonPagedPoolPages
;
1027 PageSize
= SystemBasicInfo
.PageSize
;
1029 LeaveCriticalSection(&PerfDataCriticalSection
);
1031 NonPaged
= NonPaged
* (PageSize
/ 1024);
1036 ULONG
PerfDataGetPhysicalMemoryTotalK(void)
1041 EnterCriticalSection(&PerfDataCriticalSection
);
1043 Total
= SystemBasicInfo
.NumberOfPhysicalPages
;
1044 PageSize
= SystemBasicInfo
.PageSize
;
1046 LeaveCriticalSection(&PerfDataCriticalSection
);
1048 Total
= Total
* (PageSize
/ 1024);
1053 ULONG
PerfDataGetPhysicalMemoryAvailableK(void)
1058 EnterCriticalSection(&PerfDataCriticalSection
);
1060 Available
= SystemPerfInfo
.AvailablePages
;
1061 PageSize
= SystemBasicInfo
.PageSize
;
1063 LeaveCriticalSection(&PerfDataCriticalSection
);
1065 Available
= Available
* (PageSize
/ 1024);
1070 ULONG
PerfDataGetPhysicalMemorySystemCacheK(void)
1075 EnterCriticalSection(&PerfDataCriticalSection
);
1077 PageSize
= SystemBasicInfo
.PageSize
;
1078 SystemCache
= SystemCacheInfo
.CurrentSizeIncludingTransitionInPages
* PageSize
;
1080 LeaveCriticalSection(&PerfDataCriticalSection
);
1082 return SystemCache
/ 1024;
1085 ULONG
PerfDataGetSystemHandleCount(void)
1089 EnterCriticalSection(&PerfDataCriticalSection
);
1091 HandleCount
= SystemHandleInfo
.NumberOfHandles
;
1093 LeaveCriticalSection(&PerfDataCriticalSection
);
1098 ULONG
PerfDataGetTotalThreadCount(void)
1100 ULONG ThreadCount
= 0;
1103 EnterCriticalSection(&PerfDataCriticalSection
);
1105 for (i
=0; i
<ProcessCount
; i
++)
1107 ThreadCount
+= pPerfData
[i
].ThreadCount
;
1110 LeaveCriticalSection(&PerfDataCriticalSection
);
1115 BOOL
PerfDataGet(ULONG Index
, PPERFDATA
*lppData
)
1117 BOOL bSuccessful
= FALSE
;
1119 EnterCriticalSection(&PerfDataCriticalSection
);
1120 if (Index
< ProcessCount
)
1122 *lppData
= pPerfData
+ Index
;
1125 LeaveCriticalSection(&PerfDataCriticalSection
);