Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
7.4 KiB

  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <malloc.h>
  5. #include <tchar.h>
  6. typedef struct _PerfDataSectionHeader {
  7. DWORD dwEntriesInUse;
  8. DWORD dwMaxEntries;
  9. DWORD dwMissingEntries;
  10. DWORD dwInitSignature;
  11. BYTE reserved[112];
  12. } PerfDataSectionHeader, *pPerfDataSectionHeader;
  13. #define PDSH_INIT_SIG ((DWORD)0x01234567)
  14. #define PDSR_SERVICE_NAME_LEN 32
  15. typedef struct _PerfDataSectionRecord {
  16. WCHAR szServiceName[PDSR_SERVICE_NAME_LEN];
  17. LONGLONG llElapsedTime;
  18. DWORD dwCollectCount; // number of times Collect successfully called
  19. DWORD dwOpenCount; // number of Loads & opens
  20. DWORD dwCloseCount; // number of Unloads & closes
  21. DWORD dwLockoutCount; // count of lock timeouts
  22. DWORD dwErrorCount; // count of errors (other than timeouts)
  23. DWORD dwLastBufferSize; // size of the last buffer returned
  24. DWORD dwMaxBufferSize; // size of MAX buffer returned
  25. DWORD dwMaxBufferRejected; // size of largest buffer returned as too small
  26. BYTE Reserved[24]; // reserved to make structure 128 bytes
  27. } PerfDataSectionRecord, *pPerfDataSectionRecord;
  28. // performance data block entries
  29. TCHAR szPerflibSectionFile[MAX_PATH];
  30. TCHAR szPerflibSectionName[MAX_PATH];
  31. HANDLE hPerflibSectionFile = NULL;
  32. HANDLE hPerflibSectionMap = NULL;
  33. LPVOID lpPerflibSectionAddr = NULL;
  34. #define dwPerflibSectionMaxEntries 127L
  35. const DWORD dwPerflibSectionSize = (sizeof(PerfDataSectionHeader) + (sizeof(PerfDataSectionRecord) * dwPerflibSectionMaxEntries));
  36. // -----------------------------------------------------------------------
  37. // FUNCTION: _tmain
  38. //
  39. // The main function primarily splits off the main tasks of either
  40. // enumerating the performance objects and object items, or dumping
  41. // the performance data of the user specified counter.
  42. // -----------------------------------------------------------------------
  43. int __cdecl _tmain(int argc, TCHAR **argv)
  44. {
  45. if (argc < 2) {
  46. _tprintf ("Enter the process ID in decimal of the process to view");
  47. return 0;
  48. }
  49. if (hPerflibSectionFile == NULL) {
  50. TCHAR szTmpFileName[MAX_PATH];
  51. pPerfDataSectionHeader pHead;
  52. TCHAR szPID[32];
  53. DWORD dwPid;
  54. dwPid = _tcstoul(argv[1],NULL, 10);
  55. _stprintf (szPID, TEXT("%x"), dwPid);
  56. // create section name
  57. lstrcpy (szPerflibSectionName, (LPCTSTR)"Perflib_Perfdata_");
  58. //lstrcpy (szPID, argv[1]);
  59. lstrcat (szPerflibSectionName, szPID);
  60. // create filename
  61. lstrcpy (szTmpFileName, (LPCTSTR)"%windir%\\system32\\");
  62. lstrcat (szTmpFileName, szPerflibSectionName);
  63. lstrcat (szTmpFileName, (LPCTSTR)".dat");
  64. ExpandEnvironmentStrings (szTmpFileName, szPerflibSectionFile, MAX_PATH);
  65. /* hPerflibSectionFile = CreateFile (
  66. szPerflibSectionFile,
  67. GENERIC_READ,
  68. FILE_SHARE_READ,
  69. NULL,
  70. OPEN_EXISTING,
  71. FILE_ATTRIBUTE_TEMPORARY,
  72. NULL);
  73. if (hPerflibSectionFile != INVALID_HANDLE_VALUE) {
  74. */ // create file mapping object
  75. hPerflibSectionMap = OpenFileMapping (
  76. FILE_MAP_READ, FALSE,
  77. szPerflibSectionName);
  78. if (hPerflibSectionMap != NULL) {
  79. // map view of file
  80. lpPerflibSectionAddr = MapViewOfFile (
  81. hPerflibSectionMap,
  82. FILE_MAP_READ,
  83. 0,0, dwPerflibSectionSize);
  84. if (lpPerflibSectionAddr != NULL) {
  85. // init section if not already
  86. pHead = (pPerfDataSectionHeader)lpPerflibSectionAddr;
  87. if (pHead->dwInitSignature != PDSH_INIT_SIG) {
  88. // then not ready
  89. _tprintf ((LPCTSTR)"Data Section has not been initialized.\n", GetLastError());
  90. UnmapViewOfFile (lpPerflibSectionAddr);
  91. lpPerflibSectionAddr = NULL;
  92. CloseHandle (hPerflibSectionMap);
  93. hPerflibSectionMap = NULL;
  94. CloseHandle (hPerflibSectionFile);
  95. hPerflibSectionFile = NULL;
  96. } else {
  97. // already initialized so leave it
  98. }
  99. } else {
  100. // unable to map file so close
  101. _tprintf ((LPCTSTR)"Unable to map file for reading perf data (%d)\n", GetLastError());
  102. CloseHandle (hPerflibSectionMap);
  103. hPerflibSectionMap = NULL;
  104. CloseHandle (hPerflibSectionFile);
  105. hPerflibSectionFile = NULL;
  106. }
  107. } else {
  108. // unable to create file mapping so close file
  109. _tprintf ((LPCTSTR)"Unable to create file mapping object for reading perf data (%d)\n", GetLastError());
  110. CloseHandle (hPerflibSectionFile);
  111. hPerflibSectionFile = NULL;
  112. }
  113. /* } else {
  114. // unable to open file so no perf stats available
  115. _tprintf ((LPCTSTR)"Unable to open file for reading perf data (%d)\n", GetLastError());
  116. hPerflibSectionFile = NULL;
  117. }
  118. */ }
  119. if (lpPerflibSectionAddr != NULL) {
  120. pPerfDataSectionHeader pHead = (pPerfDataSectionHeader)lpPerflibSectionAddr;
  121. pPerfDataSectionRecord pEntry = (pPerfDataSectionRecord)lpPerflibSectionAddr;
  122. DWORD i;
  123. double dTime;
  124. double dFreq;
  125. LARGE_INTEGER liTimeBase;
  126. QueryPerformanceFrequency (&liTimeBase);
  127. dFreq = (double)liTimeBase.QuadPart;
  128. _tprintf ((LPCTSTR)"Perflib Performance Data for process %s\n", argv[1]);
  129. _tprintf ((LPCTSTR)"%d/%d entries in section\n", pHead->dwEntriesInUse, pHead->dwMaxEntries);
  130. _tprintf ((LPCTSTR)"%d services not logged\n", pHead->dwMissingEntries);
  131. _tprintf ((LPCTSTR)" Service Name Avg Coll. Ms ");
  132. _tprintf ((LPCTSTR)"Collects Opens Closes Lockout Errs Last Buf Max Buf Max Rej Buf\n");
  133. // then walk the structures present
  134. for (i = 1; i <= pHead->dwEntriesInUse; i++) {
  135. // compute average collect time
  136. if (pEntry[i].dwCollectCount > 0) {
  137. dTime = (double)pEntry[i].llElapsedTime;
  138. dTime /= (double)pEntry[i].dwCollectCount;
  139. dTime /= dFreq;
  140. dTime *= 1000.0; // convert to mSec
  141. } else {
  142. dTime = 0.0;
  143. }
  144. _tprintf ((LPCTSTR)" %32.32ls\t%12.6f %11d %6d %6d %6d %6d %11d %11d %11d\n",
  145. pEntry[i].szServiceName,
  146. dTime,
  147. pEntry[i].dwCollectCount,
  148. pEntry[i].dwOpenCount,
  149. pEntry[i].dwCloseCount,
  150. pEntry[i].dwLockoutCount,
  151. pEntry[i].dwErrorCount,
  152. pEntry[i].dwLastBufferSize,
  153. pEntry[i].dwMaxBufferSize,
  154. pEntry[i].dwMaxBufferRejected);
  155. }
  156. UnmapViewOfFile (lpPerflibSectionAddr);
  157. lpPerflibSectionAddr = NULL;
  158. CloseHandle (hPerflibSectionMap);
  159. hPerflibSectionMap = NULL;
  160. // CloseHandle (hPerflibSectionFile);
  161. // hPerflibSectionFile = NULL;
  162. }
  163. return 0;
  164. }