ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

perfdata.c
Go to the documentation of this file.
00001 /*
00002  *  ReactOS Task Manager
00003  *
00004  *  perfdata.c
00005  *
00006  *  Copyright (C) 1999 - 2001  Brian Palmer  <brianp@reactos.org>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  */
00022 
00023 #include <precomp.h>
00024 
00025 CRITICAL_SECTION                           PerfDataCriticalSection;
00026 PPERFDATA                                  pPerfDataOld = NULL;    /* Older perf data (saved to establish delta values) */
00027 PPERFDATA                                  pPerfData = NULL;    /* Most recent copy of perf data */
00028 ULONG                                      ProcessCountOld = 0;
00029 ULONG                                      ProcessCount = 0;
00030 double                                     dbIdleTime;
00031 double                                     dbKernelTime;
00032 double                                     dbSystemTime;
00033 LARGE_INTEGER                              liOldIdleTime = {{0,0}};
00034 double                                     OldKernelTime = 0;
00035 LARGE_INTEGER                              liOldSystemTime = {{0,0}};
00036 SYSTEM_PERFORMANCE_INFORMATION             SystemPerfInfo;
00037 SYSTEM_BASIC_INFORMATION                   SystemBasicInfo;
00038 SYSTEM_FILECACHE_INFORMATION               SystemCacheInfo;
00039 SYSTEM_HANDLE_INFORMATION                  SystemHandleInfo;
00040 PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION  SystemProcessorTimeInfo = NULL;
00041 PSID                                       SystemUserSid = NULL;
00042 
00043 typedef struct _SIDTOUSERNAME
00044 {
00045     LIST_ENTRY List;
00046     LPWSTR pszName;
00047     BYTE Data[0];
00048 } SIDTOUSERNAME, *PSIDTOUSERNAME;
00049 
00050 static LIST_ENTRY SidToUserNameHead = {&SidToUserNameHead, &SidToUserNameHead};
00051 
00052 BOOL PerfDataInitialize(void)
00053 {
00054     SID_IDENTIFIER_AUTHORITY NtSidAuthority = {SECURITY_NT_AUTHORITY};
00055     NTSTATUS    status;
00056 
00057     InitializeCriticalSection(&PerfDataCriticalSection);
00058 
00059     /*
00060      * Get number of processors in the system
00061      */
00062     status = NtQuerySystemInformation(SystemBasicInformation, &SystemBasicInfo, sizeof(SystemBasicInfo), NULL);
00063     if (!NT_SUCCESS(status))
00064         return FALSE;
00065 
00066     /*
00067      * Create the SYSTEM Sid
00068      */
00069     AllocateAndInitializeSid(&NtSidAuthority, 1, SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, &SystemUserSid);
00070     return TRUE;
00071 }
00072 
00073 void PerfDataUninitialize(void)
00074 {
00075     PLIST_ENTRY pCur;
00076     PSIDTOUSERNAME pEntry;
00077 
00078     if (pPerfData != NULL)
00079         HeapFree(GetProcessHeap(), 0, pPerfData);
00080 
00081     DeleteCriticalSection(&PerfDataCriticalSection);
00082 
00083     if (SystemUserSid != NULL)
00084     {
00085         FreeSid(SystemUserSid);
00086         SystemUserSid = NULL;
00087     }
00088 
00089     /* Free user names cache list */
00090     pCur = SidToUserNameHead.Flink;
00091     while (pCur != &SidToUserNameHead)
00092     {
00093         pEntry = CONTAINING_RECORD(pCur, SIDTOUSERNAME, List);
00094         pCur = pCur->Flink;
00095         HeapFree(GetProcessHeap(), 0, pEntry);
00096     }
00097 }
00098 
00099 static void SidToUserName(PSID Sid, LPWSTR szBuffer, DWORD BufferSize)
00100 {
00101     static WCHAR szDomainNameUnused[255];
00102     DWORD DomainNameLen = sizeof(szDomainNameUnused) / sizeof(szDomainNameUnused[0]);
00103     SID_NAME_USE Use;
00104 
00105     if (Sid != NULL)
00106         LookupAccountSidW(NULL, Sid, szBuffer, &BufferSize, szDomainNameUnused, &DomainNameLen, &Use);
00107 }
00108 
00109 VOID
00110 WINAPI
00111 CachedGetUserFromSid(
00112     PSID pSid,
00113     LPWSTR pUserName,
00114     PULONG pcwcUserName)
00115 {
00116     PLIST_ENTRY pCur;
00117     PSIDTOUSERNAME pEntry;
00118     ULONG cbSid, cwcUserName;
00119 
00120     cwcUserName = *pcwcUserName;
00121 
00122     /* Walk through the list */
00123     for(pCur = SidToUserNameHead.Flink;
00124         pCur != &SidToUserNameHead;
00125         pCur = pCur->Flink)
00126     {
00127         pEntry = CONTAINING_RECORD(pCur, SIDTOUSERNAME, List);
00128         if (EqualSid((PSID)&pEntry->Data, pSid))
00129         {
00130             wcsncpy(pUserName, pEntry->pszName, cwcUserName);
00131             *pcwcUserName = cwcUserName;
00132             return;
00133         }
00134     }
00135 
00136     /* We didn't find the SID in the list, get the name conventional */
00137     SidToUserName(pSid, pUserName, cwcUserName);
00138 
00139     /* Allocate a new entry */
00140     *pcwcUserName = wcslen(pUserName);
00141     cwcUserName = *pcwcUserName + 1;
00142     cbSid = GetLengthSid(pSid);
00143     pEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(SIDTOUSERNAME) + cbSid + cwcUserName * sizeof(WCHAR));
00144 
00145     /* Copy the Sid and name to our entry */
00146     CopySid(cbSid, (PSID)&pEntry->Data, pSid);
00147     pEntry->pszName = (LPWSTR)(pEntry->Data + cbSid);
00148     wcsncpy(pEntry->pszName, pUserName, cwcUserName);
00149 
00150     /* Insert the new entry */
00151     pEntry->List.Flink = &SidToUserNameHead;
00152     pEntry->List.Blink = SidToUserNameHead.Blink;
00153     SidToUserNameHead.Blink->Flink = &pEntry->List;
00154     SidToUserNameHead.Blink = &pEntry->List;
00155 
00156     return;
00157 }
00158 
00159 void PerfDataRefresh(void)
00160 {
00161     ULONG                                      ulSize;
00162     NTSTATUS                                   status;
00163     LPBYTE                                     pBuffer;
00164     ULONG                                      BufferSize;
00165     PSYSTEM_PROCESS_INFORMATION                pSPI;
00166     PPERFDATA                                  pPDOld;
00167     ULONG                                      Idx, Idx2;
00168     HANDLE                                     hProcess;
00169     HANDLE                                     hProcessToken;
00170     SYSTEM_PERFORMANCE_INFORMATION             SysPerfInfo;
00171     SYSTEM_TIMEOFDAY_INFORMATION               SysTimeInfo;
00172     SYSTEM_FILECACHE_INFORMATION               SysCacheInfo;
00173     LPBYTE                                     SysHandleInfoData;
00174     PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION  SysProcessorTimeInfo;
00175     double                                     CurrentKernelTime;
00176     PSECURITY_DESCRIPTOR                       ProcessSD;
00177     PSID                                       ProcessUser;
00178     ULONG                                      Buffer[64]; /* must be 4 bytes aligned! */
00179     ULONG                                      cwcUserName;
00180 
00181     /* Get new system time */
00182     status = NtQuerySystemInformation(SystemTimeOfDayInformation, &SysTimeInfo, sizeof(SysTimeInfo), NULL);
00183     if (!NT_SUCCESS(status))
00184         return;
00185 
00186     /* Get new CPU's idle time */
00187     status = NtQuerySystemInformation(SystemPerformanceInformation, &SysPerfInfo, sizeof(SysPerfInfo), NULL);
00188     if (!NT_SUCCESS(status))
00189         return;
00190 
00191     /* Get system cache information */
00192     status = NtQuerySystemInformation(SystemFileCacheInformation, &SysCacheInfo, sizeof(SysCacheInfo), NULL);
00193     if (!NT_SUCCESS(status))
00194         return;
00195 
00196     /* Get processor time information */
00197     SysProcessorTimeInfo = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)HeapAlloc(GetProcessHeap(), 0, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberOfProcessors);
00198     status = NtQuerySystemInformation(SystemProcessorPerformanceInformation, SysProcessorTimeInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberOfProcessors, &ulSize);
00199 
00200     if (!NT_SUCCESS(status))
00201     {
00202         if (SysProcessorTimeInfo != NULL)
00203             HeapFree(GetProcessHeap(), 0, SysProcessorTimeInfo);
00204         return;
00205     }
00206 
00207     /* Get handle information
00208      * We don't know how much data there is so just keep
00209      * increasing the buffer size until the call succeeds
00210      */
00211     BufferSize = 0;
00212     do
00213     {
00214         BufferSize += 0x10000;
00215         SysHandleInfoData = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, BufferSize);
00216 
00217         status = NtQuerySystemInformation(SystemHandleInformation, SysHandleInfoData, BufferSize, &ulSize);
00218 
00219         if (status == STATUS_INFO_LENGTH_MISMATCH) {
00220             HeapFree(GetProcessHeap(), 0, SysHandleInfoData);
00221         }
00222 
00223     } while (status == STATUS_INFO_LENGTH_MISMATCH);
00224 
00225     /* Get process information
00226      * We don't know how much data there is so just keep
00227      * increasing the buffer size until the call succeeds
00228      */
00229     BufferSize = 0;
00230     do
00231     {
00232         BufferSize += 0x10000;
00233         pBuffer = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, BufferSize);
00234 
00235         status = NtQuerySystemInformation(SystemProcessInformation, pBuffer, BufferSize, &ulSize);
00236 
00237         if (status == STATUS_INFO_LENGTH_MISMATCH) {
00238             HeapFree(GetProcessHeap(), 0, pBuffer);
00239         }
00240 
00241     } while (status == STATUS_INFO_LENGTH_MISMATCH);
00242 
00243     EnterCriticalSection(&PerfDataCriticalSection);
00244 
00245     /*
00246      * Save system performance info
00247      */
00248     memcpy(&SystemPerfInfo, &SysPerfInfo, sizeof(SYSTEM_PERFORMANCE_INFORMATION));
00249 
00250     /*
00251      * Save system cache info
00252      */
00253     memcpy(&SystemCacheInfo, &SysCacheInfo, sizeof(SYSTEM_FILECACHE_INFORMATION));
00254 
00255     /*
00256      * Save system processor time info
00257      */
00258     if (SystemProcessorTimeInfo) {
00259         HeapFree(GetProcessHeap(), 0, SystemProcessorTimeInfo);
00260     }
00261     SystemProcessorTimeInfo = SysProcessorTimeInfo;
00262 
00263     /*
00264      * Save system handle info
00265      */
00266     memcpy(&SystemHandleInfo, SysHandleInfoData, sizeof(SYSTEM_HANDLE_INFORMATION));
00267     HeapFree(GetProcessHeap(), 0, SysHandleInfoData);
00268 
00269     for (CurrentKernelTime=0, Idx=0; Idx<(ULONG)SystemBasicInfo.NumberOfProcessors; Idx++) {
00270         CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].KernelTime);
00271         CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].DpcTime);
00272         CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].InterruptTime);
00273     }
00274 
00275     /* If it's a first call - skip idle time calcs */
00276     if (liOldIdleTime.QuadPart != 0) {
00277         /*  CurrentValue = NewValue - OldValue */
00278         dbIdleTime = Li2Double(SysPerfInfo.IdleProcessTime) - Li2Double(liOldIdleTime);
00279         dbKernelTime = CurrentKernelTime - OldKernelTime;
00280         dbSystemTime = Li2Double(SysTimeInfo.CurrentTime) - Li2Double(liOldSystemTime);
00281 
00282         /*  CurrentCpuIdle = IdleTime / SystemTime */
00283         dbIdleTime = dbIdleTime / dbSystemTime;
00284         dbKernelTime = dbKernelTime / dbSystemTime;
00285 
00286         /*  CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors */
00287         dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
00288         dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
00289     }
00290 
00291     /* Store new CPU's idle and system time */
00292     liOldIdleTime = SysPerfInfo.IdleProcessTime;
00293     liOldSystemTime = SysTimeInfo.CurrentTime;
00294     OldKernelTime = CurrentKernelTime;
00295 
00296     /* Determine the process count
00297      * We loop through the data we got from NtQuerySystemInformation
00298      * and count how many structures there are (until RelativeOffset is 0)
00299      */
00300     ProcessCountOld = ProcessCount;
00301     ProcessCount = 0;
00302     pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
00303     while (pSPI) {
00304         ProcessCount++;
00305         if (pSPI->NextEntryOffset == 0)
00306             break;
00307         pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->NextEntryOffset);
00308     }
00309 
00310     /* Now alloc a new PERFDATA array and fill in the data */
00311     if (pPerfDataOld) {
00312         HeapFree(GetProcessHeap(), 0, pPerfDataOld);
00313     }
00314     pPerfDataOld = pPerfData;
00315     /* Clear out process perf data structures with HEAP_ZERO_MEMORY flag: */
00316     pPerfData = (PPERFDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PERFDATA) * ProcessCount);
00317     pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
00318     for (Idx=0; Idx<ProcessCount; Idx++) {
00319         /* Get the old perf data for this process (if any) */
00320         /* so that we can establish delta values */
00321         pPDOld = NULL;
00322         for (Idx2=0; Idx2<ProcessCountOld; Idx2++) {
00323             if (pPerfDataOld[Idx2].ProcessId == pSPI->UniqueProcessId) {
00324                 pPDOld = &pPerfDataOld[Idx2];
00325                 break;
00326             }
00327         }
00328 
00329         if (pSPI->ImageName.Buffer) {
00330             /* Don't assume a UNICODE_STRING Buffer is zero terminated: */
00331             int len = pSPI->ImageName.Length / 2;
00332             /* Check against max size and allow for terminating zero (already zeroed): */
00333             if(len >= MAX_PATH)len=MAX_PATH - 1;
00334             wcsncpy(pPerfData[Idx].ImageName, pSPI->ImageName.Buffer, len);
00335         } else {
00336             LoadStringW(hInst, IDS_IDLE_PROCESS, pPerfData[Idx].ImageName,
00337                        sizeof(pPerfData[Idx].ImageName) / sizeof(pPerfData[Idx].ImageName[0]));
00338         }
00339 
00340         pPerfData[Idx].ProcessId = pSPI->UniqueProcessId;
00341 
00342         if (pPDOld)    {
00343             double    CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
00344             double    OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
00345             double    CpuTime = (CurTime - OldTime) / dbSystemTime;
00346             CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
00347             pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
00348         }
00349         pPerfData[Idx].CPUTime.QuadPart = pSPI->UserTime.QuadPart + pSPI->KernelTime.QuadPart;
00350         pPerfData[Idx].WorkingSetSizeBytes = pSPI->WorkingSetSize;
00351         pPerfData[Idx].PeakWorkingSetSizeBytes = pSPI->PeakWorkingSetSize;
00352         if (pPDOld)
00353             pPerfData[Idx].WorkingSetSizeDelta = labs((LONG)pSPI->WorkingSetSize - (LONG)pPDOld->WorkingSetSizeBytes);
00354         else
00355             pPerfData[Idx].WorkingSetSizeDelta = 0;
00356         pPerfData[Idx].PageFaultCount = pSPI->PageFaultCount;
00357         if (pPDOld)
00358             pPerfData[Idx].PageFaultCountDelta = labs((LONG)pSPI->PageFaultCount - (LONG)pPDOld->PageFaultCount);
00359         else
00360             pPerfData[Idx].PageFaultCountDelta = 0;
00361         pPerfData[Idx].VirtualMemorySizeBytes = pSPI->VirtualSize;
00362         pPerfData[Idx].PagedPoolUsagePages = pSPI->QuotaPeakPagedPoolUsage;
00363         pPerfData[Idx].NonPagedPoolUsagePages = pSPI->QuotaPeakNonPagedPoolUsage;
00364         pPerfData[Idx].BasePriority = pSPI->BasePriority;
00365         pPerfData[Idx].HandleCount = pSPI->HandleCount;
00366         pPerfData[Idx].ThreadCount = pSPI->NumberOfThreads;
00367         pPerfData[Idx].SessionId = pSPI->SessionId;
00368         pPerfData[Idx].UserName[0] = L'\0';
00369         pPerfData[Idx].USERObjectCount = 0;
00370         pPerfData[Idx].GDIObjectCount = 0;
00371         ProcessUser = SystemUserSid;
00372         ProcessSD = NULL;
00373 
00374         if (pSPI->UniqueProcessId != NULL) {
00375             hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | READ_CONTROL, FALSE, PtrToUlong(pSPI->UniqueProcessId));
00376             if (hProcess) {
00377                 /* don't query the information of the system process. It's possible but
00378                    returns Administrators as the owner of the process instead of SYSTEM */
00379                 if (pSPI->UniqueProcessId != (HANDLE)0x4)
00380                 {
00381                     if (OpenProcessToken(hProcess, TOKEN_QUERY, &hProcessToken))
00382                     {
00383                         DWORD RetLen = 0;
00384                         BOOL Ret;
00385 
00386                         Ret = GetTokenInformation(hProcessToken, TokenUser, (LPVOID)Buffer, sizeof(Buffer), &RetLen);
00387                         CloseHandle(hProcessToken);
00388 
00389                         if (Ret)
00390                             ProcessUser = ((PTOKEN_USER)Buffer)->User.Sid;
00391                         else
00392                             goto ReadProcOwner;
00393                     }
00394                     else
00395                     {
00396 ReadProcOwner:
00397                         GetSecurityInfo(hProcess, SE_KERNEL_OBJECT, OWNER_SECURITY_INFORMATION, &ProcessUser, NULL, NULL, NULL, &ProcessSD);
00398                     }
00399 
00400                     pPerfData[Idx].USERObjectCount = GetGuiResources(hProcess, GR_USEROBJECTS);
00401                     pPerfData[Idx].GDIObjectCount = GetGuiResources(hProcess, GR_GDIOBJECTS);
00402                 }
00403 
00404                 GetProcessIoCounters(hProcess, &pPerfData[Idx].IOCounters);
00405                 CloseHandle(hProcess);
00406             } else {
00407                 goto ClearInfo;
00408             }
00409         } else {
00410 ClearInfo:
00411             /* clear information we were unable to fetch */
00412             ZeroMemory(&pPerfData[Idx].IOCounters, sizeof(IO_COUNTERS));
00413         }
00414 
00415         cwcUserName = sizeof(pPerfData[0].UserName) / sizeof(pPerfData[0].UserName[0]);
00416         CachedGetUserFromSid(ProcessUser, pPerfData[Idx].UserName, &cwcUserName);
00417 
00418         if (ProcessSD != NULL)
00419         {
00420             LocalFree((HLOCAL)ProcessSD);
00421         }
00422 
00423         pPerfData[Idx].UserTime.QuadPart = pSPI->UserTime.QuadPart;
00424         pPerfData[Idx].KernelTime.QuadPart = pSPI->KernelTime.QuadPart;
00425         pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->NextEntryOffset);
00426     }
00427     HeapFree(GetProcessHeap(), 0, pBuffer);
00428     LeaveCriticalSection(&PerfDataCriticalSection);
00429 }
00430 
00431 ULONG PerfDataGetProcessIndex(ULONG pid)
00432 {
00433     ULONG idx;
00434 
00435     EnterCriticalSection(&PerfDataCriticalSection);
00436 
00437     for (idx = 0; idx < ProcessCount; idx++)
00438     {
00439         if (PtrToUlong(pPerfData[idx].ProcessId) == pid)
00440         {
00441             break;
00442         }
00443     }
00444 
00445     LeaveCriticalSection(&PerfDataCriticalSection);
00446 
00447     if (idx == ProcessCount)
00448     {
00449         return -1;
00450     }
00451     return idx;
00452 }
00453 
00454 ULONG PerfDataGetProcessCount(void)
00455 {
00456     return ProcessCount;
00457 }
00458 
00459 ULONG PerfDataGetProcessorUsage(void)
00460 {
00461     return (ULONG)dbIdleTime;
00462 }
00463 
00464 ULONG PerfDataGetProcessorSystemUsage(void)
00465 {
00466     return (ULONG)dbKernelTime;
00467 }
00468 
00469 BOOL PerfDataGetImageName(ULONG Index, LPWSTR lpImageName, int nMaxCount)
00470 {
00471     BOOL  bSuccessful;
00472 
00473     EnterCriticalSection(&PerfDataCriticalSection);
00474 
00475     if (Index < ProcessCount) {
00476         wcsncpy(lpImageName, pPerfData[Index].ImageName, nMaxCount);
00477         bSuccessful = TRUE;
00478     } else {
00479         bSuccessful = FALSE;
00480     }
00481     LeaveCriticalSection(&PerfDataCriticalSection);
00482     return bSuccessful;
00483 }
00484 
00485 ULONG PerfDataGetProcessId(ULONG Index)
00486 {
00487     ULONG  ProcessId;
00488 
00489     EnterCriticalSection(&PerfDataCriticalSection);
00490 
00491     if (Index < ProcessCount)
00492         ProcessId = PtrToUlong(pPerfData[Index].ProcessId);
00493     else
00494         ProcessId = 0;
00495 
00496     LeaveCriticalSection(&PerfDataCriticalSection);
00497 
00498     return ProcessId;
00499 }
00500 
00501 BOOL PerfDataGetUserName(ULONG Index, LPWSTR lpUserName, int nMaxCount)
00502 {
00503     BOOL  bSuccessful;
00504 
00505     EnterCriticalSection(&PerfDataCriticalSection);
00506 
00507     if (Index < ProcessCount) {
00508         wcsncpy(lpUserName, pPerfData[Index].UserName, nMaxCount);
00509         bSuccessful = TRUE;
00510     } else {
00511         bSuccessful = FALSE;
00512     }
00513 
00514     LeaveCriticalSection(&PerfDataCriticalSection);
00515 
00516     return bSuccessful;
00517 }
00518 
00519 ULONG PerfDataGetSessionId(ULONG Index)
00520 {
00521     ULONG  SessionId;
00522 
00523     EnterCriticalSection(&PerfDataCriticalSection);
00524 
00525     if (Index < ProcessCount)
00526         SessionId = pPerfData[Index].SessionId;
00527     else
00528         SessionId = 0;
00529 
00530     LeaveCriticalSection(&PerfDataCriticalSection);
00531 
00532     return SessionId;
00533 }
00534 
00535 ULONG PerfDataGetCPUUsage(ULONG Index)
00536 {
00537     ULONG  CpuUsage;
00538 
00539     EnterCriticalSection(&PerfDataCriticalSection);
00540 
00541     if (Index < ProcessCount)
00542         CpuUsage = pPerfData[Index].CPUUsage;
00543     else
00544         CpuUsage = 0;
00545 
00546     LeaveCriticalSection(&PerfDataCriticalSection);
00547 
00548     return CpuUsage;
00549 }
00550 
00551 LARGE_INTEGER PerfDataGetCPUTime(ULONG Index)
00552 {
00553     LARGE_INTEGER  CpuTime = {{0,0}};
00554 
00555     EnterCriticalSection(&PerfDataCriticalSection);
00556 
00557     if (Index < ProcessCount)
00558         CpuTime = pPerfData[Index].CPUTime;
00559 
00560     LeaveCriticalSection(&PerfDataCriticalSection);
00561 
00562     return CpuTime;
00563 }
00564 
00565 ULONG PerfDataGetWorkingSetSizeBytes(ULONG Index)
00566 {
00567     ULONG  WorkingSetSizeBytes;
00568 
00569     EnterCriticalSection(&PerfDataCriticalSection);
00570 
00571     if (Index < ProcessCount)
00572         WorkingSetSizeBytes = pPerfData[Index].WorkingSetSizeBytes;
00573     else
00574         WorkingSetSizeBytes = 0;
00575 
00576     LeaveCriticalSection(&PerfDataCriticalSection);
00577 
00578     return WorkingSetSizeBytes;
00579 }
00580 
00581 ULONG PerfDataGetPeakWorkingSetSizeBytes(ULONG Index)
00582 {
00583     ULONG  PeakWorkingSetSizeBytes;
00584 
00585     EnterCriticalSection(&PerfDataCriticalSection);
00586 
00587     if (Index < ProcessCount)
00588         PeakWorkingSetSizeBytes = pPerfData[Index].PeakWorkingSetSizeBytes;
00589     else
00590         PeakWorkingSetSizeBytes = 0;
00591 
00592     LeaveCriticalSection(&PerfDataCriticalSection);
00593 
00594     return PeakWorkingSetSizeBytes;
00595 }
00596 
00597 ULONG PerfDataGetWorkingSetSizeDelta(ULONG Index)
00598 {
00599     ULONG  WorkingSetSizeDelta;
00600 
00601     EnterCriticalSection(&PerfDataCriticalSection);
00602 
00603     if (Index < ProcessCount)
00604         WorkingSetSizeDelta = pPerfData[Index].WorkingSetSizeDelta;
00605     else
00606         WorkingSetSizeDelta = 0;
00607 
00608     LeaveCriticalSection(&PerfDataCriticalSection);
00609 
00610     return WorkingSetSizeDelta;
00611 }
00612 
00613 ULONG PerfDataGetPageFaultCount(ULONG Index)
00614 {
00615     ULONG  PageFaultCount;
00616 
00617     EnterCriticalSection(&PerfDataCriticalSection);
00618 
00619     if (Index < ProcessCount)
00620         PageFaultCount = pPerfData[Index].PageFaultCount;
00621     else
00622         PageFaultCount = 0;
00623 
00624     LeaveCriticalSection(&PerfDataCriticalSection);
00625 
00626     return PageFaultCount;
00627 }
00628 
00629 ULONG PerfDataGetPageFaultCountDelta(ULONG Index)
00630 {
00631     ULONG  PageFaultCountDelta;
00632 
00633     EnterCriticalSection(&PerfDataCriticalSection);
00634 
00635     if (Index < ProcessCount)
00636         PageFaultCountDelta = pPerfData[Index].PageFaultCountDelta;
00637     else
00638         PageFaultCountDelta = 0;
00639 
00640     LeaveCriticalSection(&PerfDataCriticalSection);
00641 
00642     return PageFaultCountDelta;
00643 }
00644 
00645 ULONG PerfDataGetVirtualMemorySizeBytes(ULONG Index)
00646 {
00647     ULONG  VirtualMemorySizeBytes;
00648 
00649     EnterCriticalSection(&PerfDataCriticalSection);
00650 
00651     if (Index < ProcessCount)
00652         VirtualMemorySizeBytes = pPerfData[Index].VirtualMemorySizeBytes;
00653     else
00654         VirtualMemorySizeBytes = 0;
00655 
00656     LeaveCriticalSection(&PerfDataCriticalSection);
00657 
00658     return VirtualMemorySizeBytes;
00659 }
00660 
00661 ULONG PerfDataGetPagedPoolUsagePages(ULONG Index)
00662 {
00663     ULONG  PagedPoolUsage;
00664 
00665     EnterCriticalSection(&PerfDataCriticalSection);
00666 
00667     if (Index < ProcessCount)
00668         PagedPoolUsage = pPerfData[Index].PagedPoolUsagePages;
00669     else
00670         PagedPoolUsage = 0;
00671 
00672     LeaveCriticalSection(&PerfDataCriticalSection);
00673 
00674     return PagedPoolUsage;
00675 }
00676 
00677 ULONG PerfDataGetNonPagedPoolUsagePages(ULONG Index)
00678 {
00679     ULONG  NonPagedPoolUsage;
00680 
00681     EnterCriticalSection(&PerfDataCriticalSection);
00682 
00683     if (Index < ProcessCount)
00684         NonPagedPoolUsage = pPerfData[Index].NonPagedPoolUsagePages;
00685     else
00686         NonPagedPoolUsage = 0;
00687 
00688     LeaveCriticalSection(&PerfDataCriticalSection);
00689 
00690     return NonPagedPoolUsage;
00691 }
00692 
00693 ULONG PerfDataGetBasePriority(ULONG Index)
00694 {
00695     ULONG  BasePriority;
00696 
00697     EnterCriticalSection(&PerfDataCriticalSection);
00698 
00699     if (Index < ProcessCount)
00700         BasePriority = pPerfData[Index].BasePriority;
00701     else
00702         BasePriority = 0;
00703 
00704     LeaveCriticalSection(&PerfDataCriticalSection);
00705 
00706     return BasePriority;
00707 }
00708 
00709 ULONG PerfDataGetHandleCount(ULONG Index)
00710 {
00711     ULONG  HandleCount;
00712 
00713     EnterCriticalSection(&PerfDataCriticalSection);
00714 
00715     if (Index < ProcessCount)
00716         HandleCount = pPerfData[Index].HandleCount;
00717     else
00718         HandleCount = 0;
00719 
00720     LeaveCriticalSection(&PerfDataCriticalSection);
00721 
00722     return HandleCount;
00723 }
00724 
00725 ULONG PerfDataGetThreadCount(ULONG Index)
00726 {
00727     ULONG  ThreadCount;
00728 
00729     EnterCriticalSection(&PerfDataCriticalSection);
00730 
00731     if (Index < ProcessCount)
00732         ThreadCount = pPerfData[Index].ThreadCount;
00733     else
00734         ThreadCount = 0;
00735 
00736     LeaveCriticalSection(&PerfDataCriticalSection);
00737 
00738     return ThreadCount;
00739 }
00740 
00741 ULONG PerfDataGetUSERObjectCount(ULONG Index)
00742 {
00743     ULONG  USERObjectCount;
00744 
00745     EnterCriticalSection(&PerfDataCriticalSection);
00746 
00747     if (Index < ProcessCount)
00748         USERObjectCount = pPerfData[Index].USERObjectCount;
00749     else
00750         USERObjectCount = 0;
00751 
00752     LeaveCriticalSection(&PerfDataCriticalSection);
00753 
00754     return USERObjectCount;
00755 }
00756 
00757 ULONG PerfDataGetGDIObjectCount(ULONG Index)
00758 {
00759     ULONG  GDIObjectCount;
00760 
00761     EnterCriticalSection(&PerfDataCriticalSection);
00762 
00763     if (Index < ProcessCount)
00764         GDIObjectCount = pPerfData[Index].GDIObjectCount;
00765     else
00766         GDIObjectCount = 0;
00767 
00768     LeaveCriticalSection(&PerfDataCriticalSection);
00769 
00770     return GDIObjectCount;
00771 }
00772 
00773 BOOL PerfDataGetIOCounters(ULONG Index, PIO_COUNTERS pIoCounters)
00774 {
00775     BOOL  bSuccessful;
00776 
00777     EnterCriticalSection(&PerfDataCriticalSection);
00778 
00779     if (Index < ProcessCount)
00780     {
00781         memcpy(pIoCounters, &pPerfData[Index].IOCounters, sizeof(IO_COUNTERS));
00782         bSuccessful = TRUE;
00783     }
00784     else
00785         bSuccessful = FALSE;
00786 
00787     LeaveCriticalSection(&PerfDataCriticalSection);
00788 
00789     return bSuccessful;
00790 }
00791 
00792 ULONG PerfDataGetCommitChargeTotalK(void)
00793 {
00794     ULONG  Total;
00795     ULONG  PageSize;
00796 
00797     EnterCriticalSection(&PerfDataCriticalSection);
00798 
00799     Total = SystemPerfInfo.CommittedPages;
00800     PageSize = SystemBasicInfo.PageSize;
00801 
00802     LeaveCriticalSection(&PerfDataCriticalSection);
00803 
00804     Total = Total * (PageSize / 1024);
00805 
00806     return Total;
00807 }
00808 
00809 ULONG PerfDataGetCommitChargeLimitK(void)
00810 {
00811     ULONG  Limit;
00812     ULONG  PageSize;
00813 
00814     EnterCriticalSection(&PerfDataCriticalSection);
00815 
00816     Limit = SystemPerfInfo.CommitLimit;
00817     PageSize = SystemBasicInfo.PageSize;
00818 
00819     LeaveCriticalSection(&PerfDataCriticalSection);
00820 
00821     Limit = Limit * (PageSize / 1024);
00822 
00823     return Limit;
00824 }
00825 
00826 ULONG PerfDataGetCommitChargePeakK(void)
00827 {
00828     ULONG  Peak;
00829     ULONG  PageSize;
00830 
00831     EnterCriticalSection(&PerfDataCriticalSection);
00832 
00833     Peak = SystemPerfInfo.PeakCommitment;
00834     PageSize = SystemBasicInfo.PageSize;
00835 
00836     LeaveCriticalSection(&PerfDataCriticalSection);
00837 
00838     Peak = Peak * (PageSize / 1024);
00839 
00840     return Peak;
00841 }
00842 
00843 ULONG PerfDataGetKernelMemoryTotalK(void)
00844 {
00845     ULONG  Total;
00846     ULONG  Paged;
00847     ULONG  NonPaged;
00848     ULONG  PageSize;
00849 
00850     EnterCriticalSection(&PerfDataCriticalSection);
00851 
00852     Paged = SystemPerfInfo.PagedPoolPages;
00853     NonPaged = SystemPerfInfo.NonPagedPoolPages;
00854     PageSize = SystemBasicInfo.PageSize;
00855 
00856     LeaveCriticalSection(&PerfDataCriticalSection);
00857 
00858     Paged = Paged * (PageSize / 1024);
00859     NonPaged = NonPaged * (PageSize / 1024);
00860 
00861     Total = Paged + NonPaged;
00862 
00863     return Total;
00864 }
00865 
00866 ULONG PerfDataGetKernelMemoryPagedK(void)
00867 {
00868     ULONG  Paged;
00869     ULONG  PageSize;
00870 
00871     EnterCriticalSection(&PerfDataCriticalSection);
00872 
00873     Paged = SystemPerfInfo.PagedPoolPages;
00874     PageSize = SystemBasicInfo.PageSize;
00875 
00876     LeaveCriticalSection(&PerfDataCriticalSection);
00877 
00878     Paged = Paged * (PageSize / 1024);
00879 
00880     return Paged;
00881 }
00882 
00883 ULONG PerfDataGetKernelMemoryNonPagedK(void)
00884 {
00885     ULONG  NonPaged;
00886     ULONG  PageSize;
00887 
00888     EnterCriticalSection(&PerfDataCriticalSection);
00889 
00890     NonPaged = SystemPerfInfo.NonPagedPoolPages;
00891     PageSize = SystemBasicInfo.PageSize;
00892 
00893     LeaveCriticalSection(&PerfDataCriticalSection);
00894 
00895     NonPaged = NonPaged * (PageSize / 1024);
00896 
00897     return NonPaged;
00898 }
00899 
00900 ULONG PerfDataGetPhysicalMemoryTotalK(void)
00901 {
00902     ULONG  Total;
00903     ULONG  PageSize;
00904 
00905     EnterCriticalSection(&PerfDataCriticalSection);
00906 
00907     Total = SystemBasicInfo.NumberOfPhysicalPages;
00908     PageSize = SystemBasicInfo.PageSize;
00909 
00910     LeaveCriticalSection(&PerfDataCriticalSection);
00911 
00912     Total = Total * (PageSize / 1024);
00913 
00914     return Total;
00915 }
00916 
00917 ULONG PerfDataGetPhysicalMemoryAvailableK(void)
00918 {
00919     ULONG  Available;
00920     ULONG  PageSize;
00921 
00922     EnterCriticalSection(&PerfDataCriticalSection);
00923 
00924     Available = SystemPerfInfo.AvailablePages;
00925     PageSize = SystemBasicInfo.PageSize;
00926 
00927     LeaveCriticalSection(&PerfDataCriticalSection);
00928 
00929     Available = Available * (PageSize / 1024);
00930 
00931     return Available;
00932 }
00933 
00934 ULONG PerfDataGetPhysicalMemorySystemCacheK(void)
00935 {
00936     ULONG  SystemCache;
00937     ULONG  PageSize;
00938 
00939     EnterCriticalSection(&PerfDataCriticalSection);
00940 
00941     PageSize = SystemBasicInfo.PageSize;
00942     SystemCache = SystemCacheInfo.CurrentSizeIncludingTransitionInPages * PageSize;
00943 
00944     LeaveCriticalSection(&PerfDataCriticalSection);
00945 
00946     return SystemCache / 1024;
00947 }
00948 
00949 ULONG PerfDataGetSystemHandleCount(void)
00950 {
00951     ULONG  HandleCount;
00952 
00953     EnterCriticalSection(&PerfDataCriticalSection);
00954 
00955     HandleCount = SystemHandleInfo.NumberOfHandles;
00956 
00957     LeaveCriticalSection(&PerfDataCriticalSection);
00958 
00959     return HandleCount;
00960 }
00961 
00962 ULONG PerfDataGetTotalThreadCount(void)
00963 {
00964     ULONG  ThreadCount = 0;
00965     ULONG  i;
00966 
00967     EnterCriticalSection(&PerfDataCriticalSection);
00968 
00969     for (i=0; i<ProcessCount; i++)
00970     {
00971         ThreadCount += pPerfData[i].ThreadCount;
00972     }
00973 
00974     LeaveCriticalSection(&PerfDataCriticalSection);
00975 
00976     return ThreadCount;
00977 }
00978 
00979 BOOL PerfDataGet(ULONG Index, PPERFDATA *lppData)
00980 {
00981     BOOL  bSuccessful = FALSE;
00982 
00983     EnterCriticalSection(&PerfDataCriticalSection);
00984     if (Index < ProcessCount)
00985     {
00986         *lppData = pPerfData + Index;
00987         bSuccessful = TRUE;
00988     }
00989     LeaveCriticalSection(&PerfDataCriticalSection);
00990     return bSuccessful;
00991 }
00992 

Generated on Sat May 26 2012 04:16:28 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.