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.

269 lines
7.8 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. perftdet.c
  5. Abstract:
  6. This file implements an Performance Object that presents
  7. Thread details 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 <winperf.h>
  20. #include <ntprfctr.h>
  21. #include <perfutil.h>
  22. #include "perfsprc.h"
  23. #include "perfmsg.h"
  24. #include "datatdet.h"
  25. DWORD APIENTRY
  26. CollectThreadDetailsObjectData (
  27. IN OUT LPVOID *lppData,
  28. IN OUT LPDWORD lpcbTotalBytes,
  29. IN OUT LPDWORD lpNumObjectTypes
  30. )
  31. /*++
  32. Routine Description:
  33. This routine will return the data for the processor object
  34. Arguments:
  35. IN OUT LPVOID *lppData
  36. IN: pointer to the address of the buffer to receive the completed
  37. PerfDataBlock and subordinate structures. This routine will
  38. append its data to the buffer starting at the point referenced
  39. by *lppData.
  40. OUT: points to the first byte after the data structure added by this
  41. routine. This routine updated the value at lppdata after appending
  42. its data.
  43. IN OUT LPDWORD lpcbTotalBytes
  44. IN: the address of the DWORD that tells the size in bytes of the
  45. buffer referenced by the lppData argument
  46. OUT: the number of bytes added by this routine is writted to the
  47. DWORD pointed to by this argument
  48. IN OUT LPDWORD NumObjectTypes
  49. IN: the address of the DWORD to receive the number of objects added
  50. by this routine
  51. OUT: the number of objects added by this routine is writted to the
  52. DWORD pointed to by this argument
  53. Returns:
  54. 0 if successful, else Win 32 error code of failure
  55. --*/
  56. {
  57. DWORD TotalLen; // Length of the total return block
  58. PTHREAD_DETAILS_DATA_DEFINITION pThreadDetailDataDefinition;
  59. PPERF_INSTANCE_DEFINITION pPerfInstanceDefinition;
  60. PTHREAD_DETAILS_COUNTER_DATA pTDCD;
  61. PSYSTEM_PROCESS_INFORMATION ProcessInfo;
  62. PSYSTEM_THREAD_INFORMATION ThreadInfo = NULL;
  63. ULONG ProcessNumber;
  64. ULONG NumThreadInstances;
  65. ULONG ThreadNumber = 0;
  66. ULONG ProcessBufferOffset;
  67. BOOLEAN NullProcess;
  68. NTSTATUS Status; // return from Nt Calls
  69. LONGLONG llPcValue; // value of current thread PC
  70. OBJECT_ATTRIBUTES Obja; // object attributes for thread context
  71. HANDLE hThread; // handle to current thread
  72. CONTEXT ThreadContext; // current thread context struct
  73. UNICODE_STRING ThreadName;
  74. WCHAR ThreadNameBuffer[MAX_THREAD_NAME_LENGTH+1];
  75. BOOL bMoreThreads;
  76. pThreadDetailDataDefinition = (THREAD_DETAILS_DATA_DEFINITION *) *lppData;
  77. //
  78. // Check for sufficient space for Thread object type definition
  79. //
  80. TotalLen = sizeof(THREAD_DETAILS_DATA_DEFINITION) +
  81. sizeof(PERF_INSTANCE_DEFINITION) +
  82. sizeof(THREAD_DETAILS_COUNTER_DATA);
  83. if ( *lpcbTotalBytes < TotalLen ) {
  84. *lpcbTotalBytes = 0;
  85. *lpNumObjectTypes = 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 (pThreadDetailDataDefinition,
  95. &ThreadDetailsDataDefinition,
  96. sizeof(THREAD_DETAILS_DATA_DEFINITION));
  97. ProcessBufferOffset = 0;
  98. // Now collect data for each Thread
  99. ProcessNumber = 0;
  100. NumThreadInstances = 0;
  101. ProcessInfo = (PSYSTEM_PROCESS_INFORMATION)pProcessBuffer;
  102. pPerfInstanceDefinition = (PERF_INSTANCE_DEFINITION *)
  103. &pThreadDetailDataDefinition[1];
  104. TotalLen = sizeof (THREAD_DETAILS_DATA_DEFINITION);
  105. bMoreThreads = FALSE;
  106. if (ProcessInfo) {
  107. if (ProcessInfo->NextEntryOffset != 0) {
  108. bMoreThreads = TRUE;
  109. }
  110. }
  111. while ( bMoreThreads && (ProcessInfo != NULL)) {
  112. if ( ProcessInfo->ImageName.Buffer != NULL ||
  113. ProcessInfo->NumberOfThreads > 0 ) {
  114. NullProcess = FALSE;
  115. ThreadNumber = 0; // Thread number of this process
  116. ThreadInfo = (PSYSTEM_THREAD_INFORMATION)(ProcessInfo + 1);
  117. } else {
  118. NullProcess = TRUE;
  119. }
  120. while ( !NullProcess &&
  121. ThreadNumber < ProcessInfo->NumberOfThreads ) {
  122. TotalLen += sizeof(PERF_INSTANCE_DEFINITION) +
  123. (MAX_THREAD_NAME_LENGTH+1+sizeof(DWORD))*
  124. sizeof(WCHAR) +
  125. sizeof (THREAD_DETAILS_COUNTER_DATA);
  126. if ( *lpcbTotalBytes < TotalLen ) {
  127. *lpcbTotalBytes = 0;
  128. *lpNumObjectTypes = 0;
  129. return ERROR_MORE_DATA;
  130. }
  131. // Get Thread Context Information for Current PC field
  132. llPcValue = 0;
  133. InitializeObjectAttributes(&Obja, NULL, 0, NULL, NULL);
  134. Status = NtOpenThread(
  135. &hThread,
  136. THREAD_GET_CONTEXT,
  137. &Obja,
  138. &ThreadInfo->ClientId
  139. );
  140. if ( NT_SUCCESS(Status) ) {
  141. ThreadContext.ContextFlags = CONTEXT_CONTROL;
  142. Status = NtGetContextThread(hThread,&ThreadContext);
  143. NtClose(hThread);
  144. if ( NT_SUCCESS(Status) ) {
  145. llPcValue = (LONGLONG)CONTEXT_TO_PROGRAM_COUNTER(&ThreadContext);
  146. } else {
  147. llPcValue = 0; // an error occured so send back 0 PC
  148. }
  149. } else {
  150. llPcValue = 0; // an error occured so send back 0 PC
  151. }
  152. // The only name we've got is the thread number
  153. RtlIntegerToUnicodeString(ThreadNumber,
  154. 10,
  155. &ThreadName);
  156. MonBuildInstanceDefinition(pPerfInstanceDefinition,
  157. (PVOID *) &pTDCD,
  158. EXPROCESS_OBJECT_TITLE_INDEX,
  159. ProcessNumber,
  160. (DWORD)-1,
  161. ThreadName.Buffer);
  162. //
  163. //
  164. // Format and collect Thread data
  165. //
  166. pTDCD->CounterBlock.ByteLength = sizeof (THREAD_DETAILS_COUNTER_DATA);
  167. pTDCD->UserPc = llPcValue;
  168. pPerfInstanceDefinition = (PERF_INSTANCE_DEFINITION *)&pTDCD[1];
  169. NumThreadInstances++;
  170. ThreadNumber++;
  171. ThreadInfo++;
  172. }
  173. if (ProcessInfo->NextEntryOffset == 0) {
  174. // no more entries so bail out of the loop
  175. bMoreThreads = FALSE;
  176. continue;
  177. }
  178. ProcessBufferOffset += ProcessInfo->NextEntryOffset;
  179. ProcessInfo = (PSYSTEM_PROCESS_INFORMATION)
  180. &pProcessBuffer[ProcessBufferOffset];
  181. if ( !NullProcess ) {
  182. ProcessNumber++;
  183. }
  184. }
  185. // Note number of Thread instances
  186. pThreadDetailDataDefinition->ThreadDetailsObjectType.NumInstances =
  187. NumThreadInstances;
  188. //
  189. // Now we know how large an area we used for the
  190. // Thread definition, so we can update the offset
  191. // to the next object definition
  192. //
  193. *lpcbTotalBytes =
  194. pThreadDetailDataDefinition->ThreadDetailsObjectType.TotalByteLength =
  195. (DWORD)((PCHAR) pPerfInstanceDefinition -
  196. (PCHAR) pThreadDetailDataDefinition);
  197. #if DBG
  198. if (*lpcbTotalBytes > TotalLen ) {
  199. DbgPrint ("\nPERFPROC: Thread Details Perf Ctr. Instance Size Underestimated:");
  200. DbgPrint ("\nPERFPROC: Estimated size: %d, Actual Size: %d", TotalLen, *lpcbTotalBytes);
  201. }
  202. #endif
  203. *lppData = (LPVOID) pPerfInstanceDefinition;
  204. *lpNumObjectTypes = 1;
  205. return ERROR_SUCCESS;
  206. }