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.

245 lines
7.1 KiB

  1. #include <ntos.h>
  2. #include <nturtl.h>
  3. #include <windows.h>
  4. #include <dbghelp.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <wtypes.h>
  8. #include <ntstatus.dbg>
  9. #include <winerror.dbg>
  10. #include <netevent.h>
  11. #include <netevent.dbg>
  12. #define MAX_TYPE_NAMES 32
  13. ULONG NumberOfTypeNames;
  14. UNICODE_STRING TypeNames[ MAX_TYPE_NAMES ];
  15. PCHAR LargeBuffer1[ 8192 ];
  16. ULONG CompareField;
  17. #define SORT_BY_TYPE_NAME 0
  18. #define SORT_BY_OBJECT_COUNT 1
  19. #define SORT_BY_HANDLE_COUNT 2
  20. #define SORT_BY_PAGED_POOL 3
  21. #define SORT_BY_NONPAGED_POOL 4
  22. #define SORT_BY_NAME_USAGE 5
  23. int __cdecl
  24. CompareTypeInfo(
  25. const void *e1,
  26. const void *e2
  27. );
  28. int __cdecl
  29. CompareTypeInfo(
  30. const void *e1,
  31. const void *e2
  32. )
  33. {
  34. POBJECT_TYPE_INFORMATION p1, p2;
  35. p1 = (POBJECT_TYPE_INFORMATION)e1;
  36. p2 = (POBJECT_TYPE_INFORMATION)e2;
  37. switch (CompareField) {
  38. case SORT_BY_TYPE_NAME:
  39. return RtlCompareUnicodeString( &p1->TypeName, &p2->TypeName, TRUE );
  40. case SORT_BY_OBJECT_COUNT:
  41. return p2->TotalNumberOfObjects - p1->TotalNumberOfObjects;
  42. case SORT_BY_HANDLE_COUNT:
  43. return p2->TotalNumberOfHandles - p1->TotalNumberOfHandles;
  44. case SORT_BY_PAGED_POOL:
  45. return p2->TotalPagedPoolUsage - p1->TotalPagedPoolUsage;
  46. case SORT_BY_NONPAGED_POOL:
  47. return p2->TotalNonPagedPoolUsage - p1->TotalNonPagedPoolUsage;
  48. case SORT_BY_NAME_USAGE:
  49. return p2->TotalNamePoolUsage - p1->TotalNamePoolUsage;
  50. default:
  51. return 0;
  52. }
  53. }
  54. int
  55. _cdecl
  56. main(
  57. int argc,
  58. char *argv[]
  59. )
  60. {
  61. BOOL fShowUsage;
  62. ANSI_STRING AnsiString;
  63. PCHAR s;
  64. NTSTATUS Status;
  65. POBJECT_TYPES_INFORMATION TypesInfo;
  66. POBJECT_TYPE_INFORMATION TypeInfo;
  67. POBJECT_TYPE_INFORMATION TypeInfo1;
  68. ULONG Size, i, j;
  69. ULONG Totals[ 8 ];
  70. fShowUsage = FALSE;
  71. CompareField = SORT_BY_TYPE_NAME;
  72. NumberOfTypeNames = 0;
  73. while (--argc) {
  74. s = *++argv;
  75. if (*s == '-' || *s == '/') {
  76. while (*++s) {
  77. switch( toupper( *s ) ) {
  78. case 'C':
  79. CompareField = SORT_BY_OBJECT_COUNT;
  80. break;
  81. case 'H':
  82. CompareField = SORT_BY_HANDLE_COUNT;
  83. break;
  84. case 'P':
  85. CompareField = SORT_BY_PAGED_POOL;
  86. break;
  87. case 'N':
  88. CompareField = SORT_BY_NONPAGED_POOL;
  89. break;
  90. case 'M':
  91. CompareField = SORT_BY_NAME_USAGE;
  92. break;
  93. default:
  94. fprintf( stderr, "OBJMON: Invalid flag - '%c'\n", *s );
  95. case '?':
  96. fShowUsage = TRUE;
  97. break;
  98. }
  99. }
  100. }
  101. else
  102. if (fShowUsage) {
  103. break;
  104. }
  105. else {
  106. if (NumberOfTypeNames >= MAX_TYPE_NAMES) {
  107. fprintf( stderr, "OBJMON: Too many type names specified.\n" );
  108. fShowUsage = TRUE;
  109. break;
  110. }
  111. RtlInitAnsiString( &AnsiString, s );
  112. RtlAnsiStringToUnicodeString( &TypeNames[ NumberOfTypeNames++ ],
  113. &AnsiString,
  114. TRUE
  115. );
  116. }
  117. }
  118. if (fShowUsage) {
  119. fprintf( stderr, "usage: OBJMON [-h] [Type Names to display]\n" );
  120. fprintf( stderr, "where: -? - displays this help message.\n" );
  121. fprintf( stderr, " -c - sort by number of objects.\n" );
  122. fprintf( stderr, " -h - sort by number of handles.\n" );
  123. fprintf( stderr, " -p - sort by paged pool usage.\n" );
  124. fprintf( stderr, " -n - sort by nonpaged pool usage.\n" );
  125. fprintf( stderr, " -m - sort by object name usage.\n" );
  126. return 1;
  127. }
  128. TypesInfo = (POBJECT_TYPES_INFORMATION)LargeBuffer1;
  129. Size = sizeof( LargeBuffer1 );
  130. Status = NtQueryObject( NULL,
  131. ObjectTypesInformation,
  132. TypesInfo,
  133. Size,
  134. &Size
  135. );
  136. if (!NT_SUCCESS( Status )) {
  137. fprintf( stderr, "OBJMON: Unable to query type information - %x\n", Status );
  138. return 1;
  139. }
  140. TypeInfo = (POBJECT_TYPE_INFORMATION)malloc( TypesInfo->NumberOfTypes * sizeof( *TypeInfo ) );
  141. TypeInfo1 = (POBJECT_TYPE_INFORMATION)(((PUCHAR)TypesInfo) + ALIGN_UP( sizeof(*TypesInfo), ULONG_PTR ));
  142. for (i=0; i<TypesInfo->NumberOfTypes; i++) {
  143. TypeInfo[ i ] = *TypeInfo1;
  144. TypeInfo1 = (POBJECT_TYPE_INFORMATION)
  145. ((PCHAR)TypeInfo1 + sizeof( *TypeInfo1 ) + ALIGN_UP( TypeInfo1->TypeName.MaximumLength, ULONG_PTR ));
  146. }
  147. qsort( (void *)TypeInfo, TypesInfo->NumberOfTypes, sizeof( *TypeInfo ), CompareTypeInfo );
  148. memset( Totals, 0, sizeof( Totals ) );
  149. printf( "Object Type Count Handles\n" );
  150. for (i=0; i<TypesInfo->NumberOfTypes; i++) {
  151. for (j=0; j<NumberOfTypeNames; j++) {
  152. if (RtlEqualUnicodeString( &TypeInfo[ i ].TypeName,
  153. &TypeNames[ j ],
  154. TRUE
  155. )
  156. ) {
  157. break;
  158. }
  159. }
  160. if (NumberOfTypeNames == 0 || j < NumberOfTypeNames) {
  161. printf( "%-14wZ %5u %7u\n",
  162. &TypeInfo[ i ].TypeName,
  163. TypeInfo[ i ].TotalNumberOfObjects,
  164. TypeInfo[ i ].TotalNumberOfHandles
  165. );
  166. Totals[ 0 ] += TypeInfo[ i ].TotalNumberOfObjects;
  167. Totals[ 1 ] += TypeInfo[ i ].TotalNumberOfHandles;
  168. }
  169. }
  170. printf( "%-14s %5u %7u\n",
  171. "Totals",
  172. Totals[ 0 ],
  173. Totals[ 1 ]
  174. );
  175. memset( Totals, 0, sizeof( Totals ) );
  176. printf( "\n\nHigh Water marks for above totals.\n" );
  177. printf( "Object Type Count Handles\n" );
  178. for (i=0; i<TypesInfo->NumberOfTypes; i++) {
  179. for (j=0; j<NumberOfTypeNames; j++) {
  180. if (RtlEqualUnicodeString( &TypeInfo[ i ].TypeName,
  181. &TypeNames[ j ],
  182. TRUE
  183. )
  184. ) {
  185. break;
  186. }
  187. }
  188. if (NumberOfTypeNames == 0 || j < NumberOfTypeNames) {
  189. printf( "%-14wZ %5u %7u\n",
  190. &TypeInfo[ i ].TypeName,
  191. TypeInfo[ i ].HighWaterNumberOfObjects,
  192. TypeInfo[ i ].HighWaterNumberOfHandles
  193. );
  194. Totals[ 0 ] += TypeInfo[ i ].HighWaterNumberOfObjects;
  195. Totals[ 1 ] += TypeInfo[ i ].HighWaterNumberOfHandles;
  196. }
  197. }
  198. printf( "%-14s %5u %7u\n",
  199. "Totals",
  200. Totals[ 0 ],
  201. Totals[ 1 ]
  202. );
  203. return 0;
  204. }