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.

339 lines
9.7 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. perfthrd.c
  5. Abstract:
  6. This file implements an Performance Object that presents
  7. Thread performance object data
  8. Created:
  9. Bob Watson 22-Oct-1996
  10. Revision History
  11. --*/
  12. //
  13. // Include Files
  14. //
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include <windows.h>
  19. #include <assert.h>
  20. #include <winperf.h>
  21. #include <ntprfctr.h>
  22. #include <perfutil.h>
  23. #include "perfsprc.h"
  24. #include "perfmsg.h"
  25. #include "datathrd.h"
  26. extern DWORD PerfSprc_dwThreadNameFormat;
  27. DWORD APIENTRY
  28. CollectThreadObjectData (
  29. IN OUT LPVOID *lppData,
  30. IN OUT LPDWORD lpcbTotalBytes,
  31. IN OUT LPDWORD lpNumObjectTypes
  32. )
  33. /*++
  34. Routine Description:
  35. This routine will return the data for the processor object
  36. Arguments:
  37. IN OUT LPVOID *lppData
  38. IN: pointer to the address of the buffer to receive the completed
  39. PerfDataBlock and subordinate structures. This routine will
  40. append its data to the buffer starting at the point referenced
  41. by *lppData.
  42. OUT: points to the first byte after the data structure added by this
  43. routine. This routine updated the value at lppdata after appending
  44. its data.
  45. IN OUT LPDWORD lpcbTotalBytes
  46. IN: the address of the DWORD that tells the size in bytes of the
  47. buffer referenced by the lppData argument
  48. OUT: the number of bytes added by this routine is writted to the
  49. DWORD pointed to by this argument
  50. IN OUT LPDWORD NumObjectTypes
  51. IN: the address of the DWORD to receive the number of objects added
  52. by this routine
  53. OUT: the number of objects added by this routine is writted to the
  54. DWORD pointed to by this argument
  55. Returns:
  56. 0 if successful, else Win 32 error code of failure
  57. --*/
  58. {
  59. LONG lReturn = ERROR_SUCCESS;
  60. DWORD TotalLen; // Length of the total return block
  61. THREAD_DATA_DEFINITION *pThreadDataDefinition;
  62. PERF_INSTANCE_DEFINITION *pPerfInstanceDefinition;
  63. PTHREAD_COUNTER_DATA pTCD;
  64. THREAD_COUNTER_DATA tcdTotal;
  65. PSYSTEM_PROCESS_INFORMATION ProcessInfo;
  66. PSYSTEM_THREAD_INFORMATION ThreadInfo;
  67. ULONG ProcessNumber;
  68. ULONG NumThreadInstances;
  69. ULONG ThreadNumber;
  70. ULONG ProcessBufferOffset;
  71. BOOLEAN NullProcess;
  72. BOOL bMoreThreads;
  73. // total thread accumulator variables
  74. UNICODE_STRING ThreadName;
  75. WCHAR ThreadNameBuffer[MAX_THREAD_NAME_LENGTH+1];
  76. pThreadDataDefinition = (THREAD_DATA_DEFINITION *) *lppData;
  77. //
  78. // Check for sufficient space for Thread object type definition
  79. //
  80. TotalLen = sizeof(THREAD_DATA_DEFINITION) +
  81. sizeof(PERF_INSTANCE_DEFINITION) +
  82. sizeof(THREAD_COUNTER_DATA);
  83. if ( *lpcbTotalBytes < TotalLen ) {
  84. *lpcbTotalBytes = (DWORD) 0;
  85. *lpNumObjectTypes = (DWORD) 0;
  86. return ERROR_MORE_DATA;
  87. }
  88. //
  89. // Define Thread data block
  90. //
  91. ThreadName.Length =
  92. ThreadName.MaximumLength = (MAX_THREAD_NAME_LENGTH + 1) * sizeof(WCHAR);
  93. ThreadName.Buffer = ThreadNameBuffer;
  94. memcpy(pThreadDataDefinition,
  95. &ThreadDataDefinition,
  96. sizeof(THREAD_DATA_DEFINITION));
  97. pThreadDataDefinition->ThreadObjectType.PerfTime = PerfTime;
  98. ProcessBufferOffset = 0;
  99. // Now collect data for each Thread
  100. ProcessNumber = 0;
  101. NumThreadInstances = 0;
  102. ProcessInfo = (PSYSTEM_PROCESS_INFORMATION)pProcessBuffer;
  103. pPerfInstanceDefinition =
  104. (PPERF_INSTANCE_DEFINITION)&pThreadDataDefinition[1];
  105. TotalLen = sizeof(THREAD_DATA_DEFINITION);
  106. // clear total accumulator
  107. memset (&tcdTotal, 0, sizeof (tcdTotal));
  108. bMoreThreads = FALSE;
  109. if (ProcessInfo) {
  110. if (ProcessInfo->NextEntryOffset != 0) {
  111. bMoreThreads = TRUE;
  112. }
  113. }
  114. while ( bMoreThreads && (ProcessInfo != NULL)) {
  115. if ( ProcessInfo->ImageName.Buffer != NULL ||
  116. ProcessInfo->NumberOfThreads > 0 ) {
  117. NullProcess = FALSE;
  118. } else {
  119. NullProcess = TRUE;
  120. }
  121. ThreadNumber = 0; // Thread number of this process
  122. ThreadInfo = (PSYSTEM_THREAD_INFORMATION)(ProcessInfo + 1);
  123. while ( !NullProcess &&
  124. ThreadNumber < ProcessInfo->NumberOfThreads ) {
  125. TotalLen += sizeof(PERF_INSTANCE_DEFINITION) +
  126. (MAX_THREAD_NAME_LENGTH+1+sizeof(DWORD))*
  127. sizeof(WCHAR) +
  128. sizeof (THREAD_COUNTER_DATA);
  129. if ( *lpcbTotalBytes < TotalLen ) {
  130. *lpcbTotalBytes = (DWORD) 0;
  131. *lpNumObjectTypes = (DWORD) 0;
  132. return ERROR_MORE_DATA;
  133. }
  134. if (PerfSprc_dwThreadNameFormat == NAME_FORMAT_ID) {
  135. PerfIntegerToWString(
  136. HandleToUlong(ThreadInfo->ClientId.UniqueThread),
  137. 10,
  138. MAX_THREAD_NAME_LENGTH+1,
  139. ThreadNameBuffer);
  140. }
  141. else {
  142. // The only name we've got is the thread number
  143. RtlIntegerToUnicodeString(ThreadNumber,
  144. 10,
  145. &ThreadName);
  146. }
  147. MonBuildInstanceDefinition(pPerfInstanceDefinition,
  148. (PVOID *) &pTCD,
  149. PROCESS_OBJECT_TITLE_INDEX,
  150. ProcessNumber,
  151. (DWORD)-1,
  152. ThreadName.Buffer);
  153. // test structure for Quadword Alignment
  154. assert (((DWORD)(pTCD) & 0x00000007) == 0);
  155. //
  156. //
  157. // Format and collect Thread data
  158. //
  159. pTCD->CounterBlock.ByteLength = sizeof(THREAD_COUNTER_DATA);
  160. //
  161. // Convert User time from 100 nsec units to counter
  162. // frequency.
  163. //
  164. tcdTotal.ProcessorTime +=
  165. pTCD->ProcessorTime = ThreadInfo->KernelTime.QuadPart +
  166. ThreadInfo->UserTime.QuadPart;
  167. tcdTotal.UserTime +=
  168. pTCD->UserTime = ThreadInfo->UserTime.QuadPart;
  169. tcdTotal.KernelTime +=
  170. pTCD->KernelTime = ThreadInfo->KernelTime.QuadPart;
  171. tcdTotal.ContextSwitches +=
  172. pTCD->ContextSwitches = ThreadInfo->ContextSwitches;
  173. pTCD->ThreadElapsedTime = ThreadInfo->CreateTime.QuadPart;
  174. pTCD->ThreadPriority = (ThreadInfo->ClientId.UniqueProcess == 0) ?
  175. 0 : ThreadInfo->Priority;
  176. pTCD->ThreadBasePriority = ThreadInfo->BasePriority;
  177. pTCD->ThreadStartAddr = ThreadInfo->StartAddress;
  178. pTCD->ThreadState =
  179. (DWORD)((ThreadInfo->ThreadState > 7) ?
  180. 7 : ThreadInfo->ThreadState);
  181. pTCD->WaitReason = (DWORD)ThreadInfo->WaitReason;
  182. // now stuff in the process and thread id's
  183. pTCD->ProcessId = HandleToUlong(ThreadInfo->ClientId.UniqueProcess);
  184. pTCD->ThreadId = HandleToUlong(ThreadInfo->ClientId.UniqueThread);
  185. pPerfInstanceDefinition = (PERF_INSTANCE_DEFINITION *)&pTCD[1];
  186. NumThreadInstances++;
  187. ThreadNumber++;
  188. ThreadInfo++;
  189. }
  190. if ( !NullProcess ) {
  191. ProcessNumber++;
  192. }
  193. if (ProcessInfo->NextEntryOffset == 0) {
  194. bMoreThreads = FALSE;
  195. continue;
  196. }
  197. ProcessBufferOffset += ProcessInfo->NextEntryOffset;
  198. ProcessInfo = (PSYSTEM_PROCESS_INFORMATION)
  199. &pProcessBuffer[ProcessBufferOffset];
  200. }
  201. if (NumThreadInstances > 0) {
  202. // See if the total instance will fit
  203. TotalLen += sizeof(PERF_INSTANCE_DEFINITION) +
  204. (MAX_THREAD_NAME_LENGTH+1+sizeof(DWORD))*
  205. sizeof(WCHAR) +
  206. sizeof (THREAD_COUNTER_DATA);
  207. if ( *lpcbTotalBytes < TotalLen ) {
  208. *lpcbTotalBytes = (DWORD) 0;
  209. *lpNumObjectTypes = (DWORD) 0;
  210. return ERROR_MORE_DATA;
  211. }
  212. // set the Total Elapsed Time to be the current time so that it will
  213. // show up as 0 when displayed.
  214. tcdTotal.ThreadElapsedTime = pThreadDataDefinition->ThreadObjectType.PerfTime.QuadPart;
  215. // use the "total" for this instance
  216. MonBuildInstanceDefinition(pPerfInstanceDefinition,
  217. (PVOID *) &pTCD,
  218. PROCESS_OBJECT_TITLE_INDEX,
  219. ProcessNumber,
  220. (DWORD)-1,
  221. wszTotal);
  222. // test structure for Quadword Alignment
  223. assert (((DWORD)(pTCD) & 0x00000007) == 0);
  224. //
  225. //
  226. // Format and collect Thread data
  227. //
  228. memcpy (pTCD, &tcdTotal, sizeof(tcdTotal));
  229. pTCD->CounterBlock.ByteLength = sizeof(THREAD_COUNTER_DATA);
  230. pPerfInstanceDefinition = (PERF_INSTANCE_DEFINITION *)&pTCD[1];
  231. NumThreadInstances++;
  232. }
  233. // Note number of Thread instances
  234. pThreadDataDefinition->ThreadObjectType.NumInstances =
  235. NumThreadInstances;
  236. //
  237. // Now we know how large an area we used for the
  238. // Thread definition, so we can update the offset
  239. // to the next object definition
  240. //
  241. *lpcbTotalBytes =
  242. pThreadDataDefinition->ThreadObjectType.TotalByteLength =
  243. (DWORD)((PCHAR) pPerfInstanceDefinition -
  244. (PCHAR) pThreadDataDefinition);
  245. #if DBG
  246. if (*lpcbTotalBytes > TotalLen ) {
  247. DbgPrint ("\nPERFPROC: Thread Perf Ctr. Instance Size Underestimated:");
  248. DbgPrint ("\nPERFPROC: Estimated size: %d, Actual Size: %d", TotalLen, *lpcbTotalBytes);
  249. }
  250. #endif
  251. *lppData = (LPVOID)pPerfInstanceDefinition;
  252. *lpNumObjectTypes = 1;
  253. return lReturn;
  254. }