Leaked source code of windows server 2003
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.

229 lines
9.3 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. idlemon.c
  5. Abstract:
  6. Little program for recording the various idle states of a machine
  7. Author:
  8. John Vert (jvert) 1/14/2000
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <stdio.h>
  15. #include <conio.h>
  16. #define DISPLAY_TOTAL 0
  17. #define DISPLAY_DELTA 1
  18. #define DISPLAY_RAW 2
  19. int Display=DISPLAY_TOTAL;
  20. int ShowTransitions=FALSE;
  21. #define printtime(_x_) { \
  22. ULONGLONG ms = (_x_)/10000; \
  23. ULONG hours, minutes, seconds; \
  24. hours = (ULONG)ms/(1000*60*60); \
  25. if (hours) printf("%3d:",(ULONG)(ms/(1000*60*60))); \
  26. ms=ms%(1000*60*60); \
  27. minutes = (ULONG)ms/(1000*60); \
  28. if (minutes || hours) printf("%02d:",(ULONG)(ms/(1000*60))); \
  29. ms=ms%(1000*60); \
  30. seconds = (ULONG)ms/1000; \
  31. printf("%02d.",seconds); \
  32. ms=ms%1000; \
  33. printf("%03d",(ULONG)ms); \
  34. }
  35. __cdecl
  36. main (argc, argv)
  37. int argc;
  38. char *argv[];
  39. {
  40. CHAR Buff[sizeof(SYSTEM_PROCESSOR_IDLE_INFORMATION)*MAXIMUM_PROCESSORS];
  41. PSYSTEM_PROCESSOR_IDLE_INFORMATION IdleInfo = (PSYSTEM_PROCESSOR_IDLE_INFORMATION)Buff;
  42. ULONG Length;
  43. NTSTATUS Status;
  44. ULONGLONG LastIdleTime[MAXIMUM_PROCESSORS];
  45. ULONGLONG LastC1Time[MAXIMUM_PROCESSORS], LastC2Time[MAXIMUM_PROCESSORS], LastC3Time[MAXIMUM_PROCESSORS];
  46. ULONG LastC1Transitions[MAXIMUM_PROCESSORS], LastC2Transitions[MAXIMUM_PROCESSORS], LastC3Transitions[MAXIMUM_PROCESSORS];
  47. ULONG i,NumProc;
  48. LARGE_INTEGER Delay;
  49. ULONGLONG DeltaTime;
  50. ULONGLONG Diff;
  51. ULONG Delta;
  52. for (i=0;i<MAXIMUM_PROCESSORS;i++) {
  53. LastIdleTime[i] = LastC1Time[i] = LastC2Time[i] = LastC3Time[i] = 0;
  54. LastC1Transitions[i] = LastC2Transitions[i] = LastC3Transitions[i] = 0;
  55. }
  56. Delay.QuadPart = -5 * 1000 * 1000 * 10;
  57. printf("TOT IDLE TOTC1 TOTC2 TOTC3 DELTA IDLE DELTAC1 DELTAC2 DELTAC3\n");
  58. while (1) {
  59. if (_kbhit()) {
  60. int Char=_getch();
  61. switch (toupper(Char)) {
  62. case 'T':
  63. Display = DISPLAY_TOTAL;
  64. break;
  65. case 'D':
  66. Display = DISPLAY_DELTA;
  67. break;
  68. case 'R':
  69. Display = DISPLAY_RAW;
  70. break;
  71. case 'C':
  72. ShowTransitions = !ShowTransitions;
  73. break;
  74. case 'P':
  75. printf("Hit a key to continue\n");
  76. _getch();
  77. break;
  78. case 'Q':
  79. return;
  80. default:
  81. printf("Type :\n");
  82. printf("\t'T' - display Total\n");
  83. printf("\t'D' - display Delta\n");
  84. printf("\t'R' - display Raw\n");
  85. printf("\t'C' - toggle between transition Counts and time\n");
  86. printf("\t'P' - Pause\n");
  87. printf("\t'Q' - quit\n");
  88. }
  89. }
  90. Status = NtQuerySystemInformation(SystemProcessorIdleInformation,
  91. IdleInfo,
  92. sizeof(Buff),
  93. &Length);
  94. if (!NT_SUCCESS(Status)) {
  95. fprintf(stderr, "NtQuerySystemInformation failed: %lx\n",Status);
  96. return(Status);
  97. }
  98. NumProc = Length/sizeof(SYSTEM_PROCESSOR_IDLE_INFORMATION);
  99. for (i=0;i<NumProc;i++) {
  100. if (NumProc > 1) {
  101. printf("%2d>",i);
  102. } else {
  103. printf(" ");
  104. }
  105. if (ShowTransitions) {
  106. switch (Display) {
  107. case DISPLAY_TOTAL:
  108. printf("Idle ");
  109. printtime(IdleInfo[i].IdleTime);
  110. printf(" C1 %d (%d us)",
  111. IdleInfo[i].C1Transitions,
  112. IdleInfo[i].C1Transitions ?
  113. (ULONG)(IdleInfo[i].C1Time*10/IdleInfo[i].C1Transitions):0);
  114. printf(" C2 %d (%d us)",
  115. IdleInfo[i].C2Transitions,
  116. IdleInfo[i].C2Transitions ?
  117. (ULONG)(IdleInfo[i].C2Time*10/IdleInfo[i].C2Transitions):0);
  118. printf(" C3 %d (%d us)",
  119. IdleInfo[i].C3Transitions,
  120. IdleInfo[i].C3Transitions ?
  121. (ULONG)(IdleInfo[i].C3Time*10/IdleInfo[i].C3Transitions):0);
  122. break;
  123. case DISPLAY_DELTA:
  124. printf("Idle ");
  125. DeltaTime = IdleInfo[i].IdleTime-LastIdleTime[i];
  126. printtime(DeltaTime);
  127. DeltaTime = IdleInfo[i].C1Time-LastC1Time[i];
  128. Delta = IdleInfo[i].C1Transitions-LastC1Transitions[i];
  129. printf(" C1 %d (%d us)",
  130. Delta,
  131. (Delta == 0) ? 0 : (ULONG)(DeltaTime/10/Delta));
  132. DeltaTime = IdleInfo[i].C2Time-LastC2Time[i];
  133. Delta = IdleInfo[i].C2Transitions-LastC2Transitions[i];
  134. printf(" C2 %d (%d us)",
  135. Delta,
  136. (Delta == 0) ? 0 : (ULONG)(DeltaTime/10/Delta));
  137. DeltaTime = IdleInfo[i].C3Time-LastC3Time[i];
  138. Delta = IdleInfo[i].C3Transitions-LastC3Transitions[i];
  139. printf(" C3 %d (%d us)",
  140. Delta,
  141. (Delta == 0) ? 0 : (ULONG)(DeltaTime/10/Delta));
  142. break;
  143. case DISPLAY_RAW:
  144. printf("Idle %I64X C1 %d C2 %d C3 %d",
  145. IdleInfo[i].IdleTime,
  146. IdleInfo[i].C1Transitions,
  147. IdleInfo[i].C2Transitions,
  148. IdleInfo[i].C3Transitions);
  149. break;
  150. }
  151. } else {
  152. switch (Display) {
  153. case DISPLAY_TOTAL:
  154. printf("Idle ");
  155. printtime(IdleInfo[i].IdleTime);
  156. printf(" C1 ");
  157. printtime(IdleInfo[i].C1Time);
  158. printf("(%2d%%) C2 ",IdleInfo[i].C1Time*100/IdleInfo[i].IdleTime);
  159. printtime(IdleInfo[i].C2Time);
  160. printf("(%2d%%) C3 ",IdleInfo[i].C2Time*100/IdleInfo[i].IdleTime);
  161. printtime(IdleInfo[i].C3Time);
  162. printf("(%2d%%) ",IdleInfo[i].C3Time*100/IdleInfo[i].IdleTime);
  163. break;
  164. case DISPLAY_DELTA:
  165. printf("Idle ");
  166. printtime(IdleInfo[i].IdleTime-LastIdleTime[i]);
  167. printf(" C1 ");
  168. printtime(IdleInfo[i].C1Time-LastC1Time[i]);
  169. DeltaTime = IdleInfo[i].C1Time-LastC1Time[i];
  170. Diff = IdleInfo[i].IdleTime - LastIdleTime[i];
  171. printf("(%2d%%) C2 ",(Diff == 0 ? 0 : (DeltaTime * 100 / Diff) ) );
  172. DeltaTime = IdleInfo[i].C2Time-LastC2Time[i];
  173. printtime(IdleInfo[i].C2Time-LastC2Time[i]);
  174. printf("(%2d%%) C3 ",(Diff == 0 ? 0 : (DeltaTime * 100/ Diff) ) );
  175. DeltaTime = IdleInfo[i].C3Time-LastC3Time[i];
  176. printtime(IdleInfo[i].C3Time-LastC3Time[i]);
  177. printf("(%2d%%) ",(Diff == 0 ? 0 : (DeltaTime * 100 / Diff) ) );
  178. break;
  179. case DISPLAY_RAW:
  180. printf("Idle %I64X C1 %I64X C2 %I64X C3 %I64X",
  181. IdleInfo[i].IdleTime,
  182. IdleInfo[i].C1Time,
  183. IdleInfo[i].C2Time,
  184. IdleInfo[i].C3Time);
  185. break;
  186. }
  187. }
  188. LastIdleTime[i] = IdleInfo[i].IdleTime;
  189. LastC1Time[i] = IdleInfo[i].C1Time;
  190. LastC2Time[i] = IdleInfo[i].C2Time;
  191. LastC3Time[i] = IdleInfo[i].C3Time;
  192. LastC1Transitions[i] = IdleInfo[i].C1Transitions;
  193. LastC2Transitions[i] = IdleInfo[i].C2Transitions;
  194. LastC3Transitions[i] = IdleInfo[i].C3Transitions;
  195. printf("\n");
  196. }
  197. NtDelayExecution(FALSE, &Delay);
  198. }
  199. return 0;
  200. }