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.

256 lines
9.7 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. procmon.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. #define DISPLAY_INFO 3
  20. #define DISPLAY_TRANS 4
  21. int Display=DISPLAY_TOTAL;
  22. int LastDisplay=DISPLAY_TOTAL;
  23. LONG DelayTime = 5;
  24. #define printtime(_x_) { \
  25. ULONGLONG ms = (_x_)/10000; \
  26. ULONG hours, minutes, seconds; \
  27. hours = (ULONG)ms/(1000*60*60); \
  28. if (hours) printf("%3d:",(ULONG)(ms/(1000*60*60))); \
  29. ms=ms%(1000*60*60); \
  30. minutes = (ULONG)ms/(1000*60); \
  31. if (minutes || hours) printf("%02d:",(ULONG)(ms/(1000*60))); \
  32. ms=ms%(1000*60); \
  33. seconds = (ULONG)ms/1000; \
  34. printf("%02d.",seconds); \
  35. ms=ms%1000; \
  36. printf("%03d",(ULONG)ms); \
  37. }
  38. __cdecl
  39. main (argc, argv)
  40. int argc;
  41. char *argv[];
  42. {
  43. CHAR Buff[sizeof(SYSTEM_PROCESSOR_POWER_INFORMATION)*MAXIMUM_PROCESSORS];
  44. PSYSTEM_PROCESSOR_POWER_INFORMATION PowerInfo = (PSYSTEM_PROCESSOR_POWER_INFORMATION)Buff;
  45. ULONG Length;
  46. NTSTATUS Status;
  47. UCHAR LastAdjustedBusyFrequency[MAXIMUM_PROCESSORS];
  48. UCHAR LastBusyFrequency[MAXIMUM_PROCESSORS];
  49. UCHAR LastC3Frequency[MAXIMUM_PROCESSORS];
  50. ULONGLONG LastFrequencyTime[MAXIMUM_PROCESSORS];
  51. ULONG PromotionCount[MAXIMUM_PROCESSORS];
  52. ULONG DemotionCount[MAXIMUM_PROCESSORS];
  53. ULONGLONG LastIdleTime[MAXIMUM_PROCESSORS];
  54. ULONGLONG LastKernelTime[MAXIMUM_PROCESSORS];
  55. ULONGLONG DeltaTime;
  56. ULONG i;
  57. ULONG NumProc;
  58. LARGE_INTEGER Delay;
  59. ULONG Delta;
  60. for (i = 0; i < MAXIMUM_PROCESSORS; i++) {
  61. LastFrequencyTime[i] = 0;
  62. PromotionCount[i] = DemotionCount[i] = 0;
  63. }
  64. while (1) {
  65. if (_kbhit()) {
  66. int Char=_getch();
  67. switch (toupper(Char)) {
  68. case 'T':
  69. LastDisplay = Display = DISPLAY_TOTAL;
  70. break;
  71. case 'E':
  72. LastDisplay = Display = DISPLAY_TRANS;
  73. break;
  74. case 'D':
  75. LastDisplay = Display = DISPLAY_DELTA;
  76. break;
  77. case 'R':
  78. LastDisplay = Display = DISPLAY_RAW;
  79. break;
  80. case 'I':
  81. LastDisplay = Display;
  82. Display = DISPLAY_INFO;
  83. break;
  84. case '+':
  85. DelayTime++;
  86. printf("New delay is %d seconds.\n",DelayTime);
  87. break;
  88. case '-':
  89. if (DelayTime > 2) {
  90. DelayTime--;
  91. printf("New delay is %d seconds.\n",DelayTime);
  92. } else {
  93. printf("Delay cannot drop below %d seconds.\n",DelayTime);
  94. }
  95. break;
  96. case 'Q':
  97. return 0;
  98. case 'P':
  99. printf("Hit a key to continue\n");
  100. _getch();
  101. break;
  102. default:
  103. printf("Type :\n");
  104. printf("\t'T' - display Total\n");
  105. printf("\t'E' - display Transitions\n");
  106. printf("\t'D' - display Delta\n");
  107. printf("\t'R' - display Raw\n");
  108. printf("\t'I' - display quick info\n");
  109. printf("\t'+'/'-' - increase/decrease time pause\n");
  110. printf("\t'Q' - Quit\n");
  111. printf("\t'P' - Pause\n");
  112. }
  113. }
  114. Status = NtQuerySystemInformation(
  115. SystemProcessorPowerInformation,
  116. PowerInfo,
  117. sizeof(Buff),
  118. &Length
  119. );
  120. if (!NT_SUCCESS(Status)) {
  121. fprintf(stderr, "NtQuerySystemInformation failed: %lx\n",Status);
  122. return(Status);
  123. }
  124. NumProc = Length/sizeof(SYSTEM_PROCESSOR_POWER_INFORMATION);
  125. for (i=0;i<NumProc;i++) {
  126. if (NumProc > 1) {
  127. printf("%2d>",i);
  128. }
  129. switch (Display) {
  130. case DISPLAY_TOTAL:
  131. printf("Freq %3d%% (%3d%% %3d%% %3d%%) ",
  132. PowerInfo[i].CurrentFrequency,
  133. PowerInfo[i].LastAdjustedBusyFrequency,
  134. PowerInfo[i].LastBusyFrequency,
  135. PowerInfo[i].LastC3Frequency
  136. );
  137. printtime(PowerInfo[i].CurrentFrequencyTime);
  138. printf(" Sys ");
  139. printtime(PowerInfo[i].CurrentProcessorTime);
  140. printf(" Idle ");
  141. printtime(PowerInfo[i].CurrentProcessorIdleTime);
  142. break;
  143. case DISPLAY_TRANS:
  144. printf("Freq %3d%% (%3d%% %3d%% %3d%%) ",
  145. PowerInfo[i].CurrentFrequency,
  146. PowerInfo[i].LastAdjustedBusyFrequency,
  147. PowerInfo[i].LastBusyFrequency,
  148. PowerInfo[i].LastC3Frequency
  149. );
  150. printf("#P: %d #D %d #E: %d #R: %d",
  151. PowerInfo[i].PromotionCount,
  152. PowerInfo[i].DemotionCount,
  153. PowerInfo[i].ErrorCount,
  154. PowerInfo[i].RetryCount
  155. );
  156. break;
  157. case DISPLAY_DELTA:
  158. printf("Freq %3d%% (%3d%% %3d%% %3d%%) ",
  159. PowerInfo[i].CurrentFrequency,
  160. (PowerInfo[i].LastAdjustedBusyFrequency - LastAdjustedBusyFrequency[i]),
  161. (PowerInfo[i].LastBusyFrequency - LastBusyFrequency[i]),
  162. (PowerInfo[i].LastC3Frequency - LastC3Frequency[i])
  163. );
  164. DeltaTime = PowerInfo[i].CurrentFrequencyTime - LastFrequencyTime[i];
  165. printtime(DeltaTime);
  166. printf(" Sys ");
  167. printtime((PowerInfo[i].CurrentProcessorTime - LastKernelTime[i]));
  168. printf(" Idle ");
  169. printtime((PowerInfo[i].CurrentProcessorIdleTime - LastIdleTime[i]));
  170. break;
  171. case DISPLAY_RAW:
  172. printf("Freq %3d%% (%3d%% %3d%% %3d%%) %I64X #E %8d #R %8d",
  173. PowerInfo[i].CurrentFrequency,
  174. PowerInfo[i].LastAdjustedBusyFrequency,
  175. PowerInfo[i].LastBusyFrequency,
  176. PowerInfo[i].LastC3Frequency,
  177. PowerInfo[i].CurrentFrequencyTime,
  178. PowerInfo[i].ErrorCount,
  179. PowerInfo[i].RetryCount
  180. );
  181. break;
  182. case DISPLAY_INFO:
  183. printf("Frequencies: Current =%3d%% MaxProc =%3d%% MinProc =%3d%%\n"
  184. "Percentages: Adjusted=%3d%% Busy =%3d%% C3 =%3d%%\n"
  185. "Limiters: Thermal =%3d%% Constant=%3d%% Degraded=%3d%%\n"
  186. "Counts: Promote =%4d Demote =%4d\n"
  187. "Status: Retries =%4d Errors =%4d",
  188. PowerInfo[i].CurrentFrequency,
  189. PowerInfo[i].ProcessorMaxThrottle,
  190. PowerInfo[i].ProcessorMinThrottle,
  191. PowerInfo[i].LastAdjustedBusyFrequency,
  192. PowerInfo[i].LastBusyFrequency,
  193. PowerInfo[i].LastC3Frequency,
  194. PowerInfo[i].ThermalLimitFrequency,
  195. PowerInfo[i].ConstantThrottleFrequency,
  196. PowerInfo[i].DegradedThrottleFrequency,
  197. PowerInfo[i].PromotionCount,
  198. PowerInfo[i].DemotionCount,
  199. PowerInfo[i].RetryCount,
  200. PowerInfo[i].ErrorCount
  201. );
  202. break;
  203. }
  204. LastAdjustedBusyFrequency[i] = PowerInfo[i].LastAdjustedBusyFrequency;
  205. LastBusyFrequency[i] = PowerInfo[i].LastBusyFrequency;
  206. LastC3Frequency[i] = PowerInfo[i].LastC3Frequency;
  207. LastFrequencyTime[i] = PowerInfo[i].CurrentFrequencyTime;
  208. PromotionCount[i] = PowerInfo[i].PromotionCount;
  209. DemotionCount[i] = PowerInfo[i].DemotionCount;
  210. LastIdleTime[i] = PowerInfo[i].CurrentProcessorIdleTime;
  211. LastKernelTime[i] = PowerInfo[i].CurrentProcessorTime;
  212. printf("\n");
  213. }
  214. //
  215. // Revert Back to whatever we were displaying before...
  216. //
  217. if (Display != LastDisplay) {
  218. Display = LastDisplay;
  219. }
  220. Delay.QuadPart = - DelayTime * 1000 * 1000 * 10;
  221. NtDelayExecution(FALSE, &Delay);
  222. }
  223. return 0;
  224. }