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.

223 lines
8.8 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. default:
  79. printf("Type :\n");
  80. printf("\t'T' - display Total\n");
  81. printf("\t'D' - display Delta\n");
  82. printf("\t'R' - display Raw\n");
  83. printf("\t'C' - toggle between transition Counts and time\n");
  84. printf("\t'P' - Pause\n");
  85. }
  86. }
  87. Status = NtQuerySystemInformation(SystemProcessorIdleInformation,
  88. IdleInfo,
  89. sizeof(Buff),
  90. &Length);
  91. if (!NT_SUCCESS(Status)) {
  92. fprintf(stderr, "NtQuerySystemInformation failed: %lx\n",Status);
  93. return(Status);
  94. }
  95. NumProc = Length/sizeof(SYSTEM_PROCESSOR_IDLE_INFORMATION);
  96. for (i=0;i<NumProc;i++) {
  97. if (NumProc > 1) {
  98. printf("%2d>",i);
  99. } else {
  100. printf(" ");
  101. }
  102. if (ShowTransitions) {
  103. switch (Display) {
  104. case DISPLAY_TOTAL:
  105. printf("Idle ");
  106. printtime(IdleInfo[i].IdleTime);
  107. printf(" C1 %d (%d us)",
  108. IdleInfo[i].C1Transitions,
  109. (ULONG)(IdleInfo[i].C1Time*10/IdleInfo[i].C1Transitions));
  110. printf(" C2 %d (%d us)",
  111. IdleInfo[i].C2Transitions,
  112. (ULONG)(IdleInfo[i].C2Time*10/IdleInfo[i].C2Transitions));
  113. printf(" C3 %d (%d us)",
  114. IdleInfo[i].C3Transitions,
  115. (ULONG)(IdleInfo[i].C3Time*10/IdleInfo[i].C3Transitions));
  116. break;
  117. case DISPLAY_DELTA:
  118. printf("Idle ");
  119. DeltaTime = IdleInfo[i].IdleTime-LastIdleTime[i];
  120. printtime(DeltaTime);
  121. DeltaTime = IdleInfo[i].C1Time-LastC1Time[i];
  122. Delta = IdleInfo[i].C1Transitions-LastC1Transitions[i];
  123. printf(" C1 %d (%d us)",
  124. Delta,
  125. (Delta == 0) ? 0 : (ULONG)(DeltaTime/10/Delta));
  126. DeltaTime = IdleInfo[i].C2Time-LastC2Time[i];
  127. Delta = IdleInfo[i].C2Transitions-LastC2Transitions[i];
  128. printf(" C2 %d (%d us)",
  129. Delta,
  130. (Delta == 0) ? 0 : (ULONG)(DeltaTime/10/Delta));
  131. DeltaTime = IdleInfo[i].C3Time-LastC3Time[i];
  132. Delta = IdleInfo[i].C3Transitions-LastC3Transitions[i];
  133. printf(" C3 %d (%d us)",
  134. Delta,
  135. (Delta == 0) ? 0 : (ULONG)(DeltaTime/10/Delta));
  136. break;
  137. case DISPLAY_RAW:
  138. printf("Idle %I64X C1 %d C2 %d C3 %d",
  139. IdleInfo[i].IdleTime,
  140. IdleInfo[i].C1Transitions,
  141. IdleInfo[i].C2Transitions,
  142. IdleInfo[i].C3Transitions);
  143. break;
  144. }
  145. } else {
  146. switch (Display) {
  147. case DISPLAY_TOTAL:
  148. printf("Idle ");
  149. printtime(IdleInfo[i].IdleTime);
  150. printf(" C1 ");
  151. printtime(IdleInfo[i].C1Time);
  152. printf("(%2d%%) C2 ",IdleInfo[i].C1Time*100/IdleInfo[i].IdleTime);
  153. printtime(IdleInfo[i].C2Time);
  154. printf("(%2d%%) C3 ",IdleInfo[i].C2Time*100/IdleInfo[i].IdleTime);
  155. printtime(IdleInfo[i].C3Time);
  156. printf("(%2d%%) ",IdleInfo[i].C3Time*100/IdleInfo[i].IdleTime);
  157. break;
  158. case DISPLAY_DELTA:
  159. printf("Idle ");
  160. printtime(IdleInfo[i].IdleTime-LastIdleTime[i]);
  161. printf(" C1 ");
  162. printtime(IdleInfo[i].C1Time-LastC1Time[i]);
  163. DeltaTime = IdleInfo[i].C1Time-LastC1Time[i];
  164. Diff = IdleInfo[i].IdleTime - LastIdleTime[i];
  165. printf("(%2d%%) C2 ",(Diff == 0 ? 0 : (DeltaTime * 100 / Diff) ) );
  166. DeltaTime = IdleInfo[i].C2Time-LastC2Time[i];
  167. printtime(IdleInfo[i].C2Time-LastC2Time[i]);
  168. printf("(%2d%%) C3 ",(Diff == 0 ? 0 : (DeltaTime * 100/ Diff) ) );
  169. DeltaTime = IdleInfo[i].C3Time-LastC3Time[i];
  170. printtime(IdleInfo[i].C3Time-LastC3Time[i]);
  171. printf("(%2d%%) ",(Diff == 0 ? 0 : (DeltaTime * 100 / Diff) ) );
  172. break;
  173. case DISPLAY_RAW:
  174. printf("Idle %I64X C1 %I64X C2 %I64X C3 %I64X",
  175. IdleInfo[i].IdleTime,
  176. IdleInfo[i].C1Time,
  177. IdleInfo[i].C2Time,
  178. IdleInfo[i].C3Time);
  179. break;
  180. }
  181. }
  182. LastIdleTime[i] = IdleInfo[i].IdleTime;
  183. LastC1Time[i] = IdleInfo[i].C1Time;
  184. LastC2Time[i] = IdleInfo[i].C2Time;
  185. LastC3Time[i] = IdleInfo[i].C3Time;
  186. LastC1Transitions[i] = IdleInfo[i].C1Transitions;
  187. LastC2Transitions[i] = IdleInfo[i].C2Transitions;
  188. LastC3Transitions[i] = IdleInfo[i].C3Transitions;
  189. printf("\n");
  190. }
  191. NtDelayExecution(FALSE, &Delay);
  192. }
  193. return 0;
  194. }