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.

257 lines
6.8 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. pfctrl.c
  5. Abstract:
  6. This module builds a console test program to control various
  7. parameters of the prefetcher maintenance service.
  8. The quality of the code for the test programs is as such.
  9. Author:
  10. Cenk Ergan (cenke)
  11. Environment:
  12. User Mode
  13. --*/
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <aclapi.h>
  21. #include "prefetch.h"
  22. #include "idletask.h"
  23. #include "..\..\pfsvc.h"
  24. WCHAR *PfCtrlUsage =
  25. L" \n"
  26. L"Usage: pfctrl [-override_idle=[0|1]] [-process_all] \n"
  27. L" Controls the prefetcher maintenance service. \n"
  28. L" \n"
  29. L"Arguments: \n"
  30. L" -override_idle=[0|1] - Whether to wait for system to be idle before \n"
  31. L" processing prefetcher traces. \n"
  32. L" -process_all - Sets the override-idle event and waits for all \n"
  33. L" current traces to be processed. \n"
  34. L" \n"
  35. ;
  36. int
  37. __cdecl
  38. main(int argc, char* argv[])
  39. {
  40. WCHAR *CommandLine;
  41. WCHAR *Argument;
  42. DWORD ErrorCode;
  43. ULONG OverrideIdle;
  44. BOOLEAN EventIsSet;
  45. HANDLE OverrideIdleEvent;
  46. HANDLE ProcessingCompleteEvent;
  47. DWORD WaitResult;
  48. BOOLEAN ResetOverrideIdleEvent;
  49. //
  50. // Initialize locals.
  51. //
  52. OverrideIdleEvent = NULL;
  53. ProcessingCompleteEvent = NULL;
  54. CommandLine = GetCommandLine();
  55. if (Argument = wcsstr(CommandLine, L"-override_idle=")) {
  56. swscanf(Argument, L"-override_idle=%d", &OverrideIdle);
  57. //
  58. // Open the override idle processing event.
  59. //
  60. OverrideIdleEvent = OpenEvent(EVENT_ALL_ACCESS,
  61. FALSE,
  62. PFSVC_OVERRIDE_IDLE_EVENT_NAME);
  63. if (!OverrideIdleEvent) {
  64. ErrorCode = GetLastError();
  65. wprintf(L"Could not open override-idle-processing event: %x\n", ErrorCode);
  66. goto cleanup;
  67. }
  68. //
  69. // Determine the current status of the event.
  70. //
  71. WaitResult = WaitForSingleObject(OverrideIdleEvent,
  72. 0);
  73. if (WaitResult == WAIT_OBJECT_0) {
  74. EventIsSet = TRUE;
  75. } else {
  76. EventIsSet = FALSE;
  77. }
  78. //
  79. // Do what we are asked to do:
  80. //
  81. if (OverrideIdle) {
  82. if (EventIsSet) {
  83. wprintf(L"Override event is already set!\n");
  84. ErrorCode = ERROR_SUCCESS;
  85. goto cleanup;
  86. } else {
  87. wprintf(L"Setting the override idle processing event.\n");
  88. SetEvent(OverrideIdleEvent);
  89. ErrorCode = ERROR_SUCCESS;
  90. goto cleanup;
  91. }
  92. } else {
  93. if (!EventIsSet) {
  94. wprintf(L"Override event is already cleared!\n");
  95. ErrorCode = ERROR_SUCCESS;
  96. goto cleanup;
  97. } else {
  98. wprintf(L"Clearing the override idle processing event.\n");
  99. ResetEvent(OverrideIdleEvent);
  100. ErrorCode = ERROR_SUCCESS;
  101. goto cleanup;
  102. }
  103. }
  104. } else if (Argument = wcsstr(CommandLine, L"-process_all")) {
  105. //
  106. // Open the override-idle-processing and processing-complete
  107. // events.
  108. //
  109. OverrideIdleEvent = OpenEvent(EVENT_ALL_ACCESS,
  110. FALSE,
  111. PFSVC_OVERRIDE_IDLE_EVENT_NAME);
  112. if (!OverrideIdleEvent) {
  113. ErrorCode = GetLastError();
  114. wprintf(L"Could not open override-idle-processing event: %x\n", ErrorCode);
  115. goto cleanup;
  116. }
  117. ProcessingCompleteEvent = OpenEvent(EVENT_ALL_ACCESS,
  118. FALSE,
  119. PFSVC_PROCESSING_COMPLETE_EVENT_NAME);
  120. if (!ProcessingCompleteEvent) {
  121. ErrorCode = GetLastError();
  122. wprintf(L"Could not open processing-complete event: %x\n", ErrorCode);
  123. goto cleanup;
  124. }
  125. //
  126. // Determine the current status of the override-idle event.
  127. //
  128. WaitResult = WaitForSingleObject(OverrideIdleEvent,
  129. 0);
  130. if (WaitResult == WAIT_OBJECT_0) {
  131. EventIsSet = TRUE;
  132. } else {
  133. EventIsSet = FALSE;
  134. }
  135. //
  136. // Set the override-idle event to force processing of traces
  137. // right away.
  138. //
  139. if (!EventIsSet) {
  140. wprintf(L"Setting override idle event.\n");
  141. SetEvent(OverrideIdleEvent);
  142. ResetOverrideIdleEvent = TRUE;
  143. } else {
  144. wprintf(L"WARNING: Override-idle event is already set. "
  145. L"It won't be reset.\n");
  146. ResetOverrideIdleEvent = FALSE;
  147. }
  148. //
  149. // Wait for processing complete event to get signaled.
  150. //
  151. wprintf(L"Waiting for all traces to be processed... ");
  152. WaitResult = WaitForSingleObject(ProcessingCompleteEvent, INFINITE);
  153. if (WaitResult != WAIT_OBJECT_0) {
  154. ErrorCode = GetLastError();
  155. wprintf(L"There was an error: %x\n", ErrorCode);
  156. goto cleanup;
  157. }
  158. wprintf(L"Done!\n");
  159. //
  160. // Reset the override idle event if necessary.
  161. //
  162. if (ResetOverrideIdleEvent) {
  163. wprintf(L"Resetting override-idle-processing event.\n");
  164. ResetEvent(OverrideIdleEvent);
  165. }
  166. ErrorCode = ERROR_SUCCESS;
  167. goto cleanup;
  168. } else {
  169. wprintf(PfCtrlUsage);
  170. ErrorCode = ERROR_INVALID_PARAMETER;
  171. goto cleanup;
  172. }
  173. //
  174. // We should not come here.
  175. //
  176. PFSVC_ASSERT(FALSE);
  177. ErrorCode = ERROR_GEN_FAILURE;
  178. cleanup:
  179. if (OverrideIdleEvent) {
  180. CloseHandle(OverrideIdleEvent);
  181. }
  182. if (ProcessingCompleteEvent) {
  183. CloseHandle(ProcessingCompleteEvent);
  184. }
  185. return ErrorCode;
  186. }