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.

424 lines
10 KiB

  1. //
  2. // Universal Resource Consumer: Just an innocent stress program
  3. // Copyright (c) Microsoft Corporation, 1997, 1998, 1999
  4. //
  5. //
  6. // module: consume.cxx
  7. // author: silviuc
  8. // created: Fri Apr 10 14:32:17 1998
  9. //
  10. // history:
  11. // johnfu added/modfied -paged-pool and -nonpaged-pool
  12. // removed -paged-pool-bad and -nonpaged-pool-bad
  13. //
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <time.h>
  18. #include <windows.h>
  19. #include "error.hxx"
  20. #include "physmem.hxx"
  21. #include "pagefile.hxx"
  22. #include "pool.hxx"
  23. #include "disk.hxx"
  24. #include "cputime.hxx"
  25. #include "consume.hxx"
  26. #define Main main
  27. VOID CreatePhysicalMemoryConsumers ();
  28. VOID CreatePageFileConsumers ();
  29. VOID CreateKernelPoolConsumers ();
  30. //
  31. // Table of contents (local functions)
  32. //
  33. static void SleepSomeTime (DWORD TimeOut, HANDLE Job);
  34. static void Help ();
  35. static char * *
  36. SearchCmdlineOption (
  37. char * Search,
  38. char * * Options);
  39. #define SIZE_1_GB 0x40000000
  40. //
  41. // Function:
  42. //
  43. // Main
  44. //
  45. // Description:
  46. //
  47. // Main function.
  48. //
  49. void _cdecl
  50. Main (
  51. int argc,
  52. char *argv [])
  53. {
  54. DWORD TimeOut = INFINITE;
  55. HANDLE Job = NULL;
  56. BOOL Result;
  57. //
  58. // Is help requested?
  59. //
  60. if (argc == 1
  61. || (argc == 2 && strcmp (argv[1], "?") == 0)
  62. || (argc == 2 && strcmp (argv[1], "/?") == 0)
  63. || (argc == 2 && strcmp (argv[1], "-?") == 0)
  64. || (argc == 2 && strcmp (argv[1], "-h") == 0)
  65. || (argc == 2 && strcmp (argv[1], "/h") == 0)
  66. || (argc == 2 && strcmp (argv[1], "-help") == 0)) {
  67. Help ();
  68. }
  69. //
  70. // Randomize the seed.
  71. //
  72. srand ((unsigned)time(0));
  73. //
  74. // Figure out if a time out parameter has been specified.
  75. //
  76. {
  77. char * * Option;
  78. Option = SearchCmdlineOption ("-time", argv);
  79. if (Option && *(Option + 1)) {
  80. TimeOut = atoi (*(Option + 1));
  81. Message ("Time out after %u seconds.", TimeOut);
  82. TimeOut *= 1000;
  83. }
  84. }
  85. //
  86. // Create a job object and assign it to itself. This will help
  87. // terminating baby consumer processes. However the assign will
  88. // fail if the consumer is already inside a job object (e.g. dks
  89. // scheduler).
  90. //
  91. // DMihai:
  92. // We are attaching the current process to a job object only
  93. // if a timeout was specified in the command line. Otherwise we
  94. // don't need the job object at all. The dks scheduler
  95. // had a race where it would allow this app to assign itself to a
  96. // job object before the scheduler assigned it to another job.
  97. // So the scheduler failed to kill this app when its time came.
  98. //
  99. if (TimeOut != INFINITE) {
  100. Job = CreateJobObject (0, 0);
  101. Result = AssignProcessToJobObject (Job, GetCurrentProcess());
  102. if (Job && Result) {
  103. Message ("Successfully assigned process to a job object ...");
  104. }
  105. }
  106. //
  107. // Parse command line. For every command we execute the consumption
  108. // scenario and then we sleep forever with the resource hold.
  109. //
  110. if (SearchCmdlineOption ("-disk-space", argv)) {
  111. ConsumeAllDiskSpace ();
  112. SleepSomeTime (TimeOut, Job);
  113. }
  114. else if (SearchCmdlineOption ("-cpu-time", argv)) {
  115. ConsumeAllCpuTime ();
  116. SleepSomeTime (TimeOut, Job);
  117. }
  118. else if (SearchCmdlineOption ("-kernel-pool", argv)) {
  119. CreateKernelPoolConsumers ();
  120. SleepSomeTime (TimeOut, Job);
  121. }
  122. else if (SearchCmdlineOption ("-physical-memory", argv)) {
  123. CreatePhysicalMemoryConsumers ();
  124. SleepSomeTime (TimeOut, Job);
  125. }
  126. else if (SearchCmdlineOption ("-page-file", argv)) {
  127. CreatePageFileConsumers ();
  128. SleepSomeTime (TimeOut, Job);
  129. }
  130. else if (SearchCmdlineOption ("-physical-memory-worker", argv)) {
  131. ConsumeAllPhysicalMemory ();
  132. SleepSomeTime (TimeOut, Job);
  133. }
  134. else if (SearchCmdlineOption ("-page-file-worker", argv)) {
  135. ConsumeAllPageFile ();
  136. SleepSomeTime (TimeOut, Job);
  137. }
  138. else if (SearchCmdlineOption ("-kernel-pool-worker", argv)) {
  139. ConsumeAllNonpagedPool ();
  140. SleepSomeTime (TimeOut, Job);
  141. }
  142. else {
  143. Help ();
  144. }
  145. }
  146. //
  147. // Function:
  148. //
  149. // SleepSomeTime
  150. //
  151. // Description:
  152. //
  153. // Sleeps forever.
  154. //
  155. static void
  156. SleepSomeTime (
  157. DWORD TimeOut,
  158. HANDLE Job)
  159. {
  160. Message ("Sleeping ...");
  161. fflush (stdout);
  162. if (TimeOut == INFINITE) {
  163. while (1) {
  164. Sleep (10000);
  165. }
  166. }
  167. else {
  168. Sleep (TimeOut);
  169. if (Job) {
  170. TerminateJobObject (Job, 0xAABBBBAA);
  171. }
  172. }
  173. }
  174. //
  175. // Function:
  176. //
  177. // Help
  178. //
  179. // Description:
  180. //
  181. // Prints help information to stdout.
  182. //
  183. static void
  184. Help ()
  185. {
  186. printf (
  187. "Universal Resource Consumer - Just an innocent stress program, v 0.1.0 \n"
  188. "Copyright (c) 1998, 1999, Microsoft Corporation \n"
  189. " \n"
  190. " consume RESOURCE [-time SECONDS] \n"
  191. " \n"
  192. "RESOURCE can be one of the following: \n"
  193. " \n"
  194. " -physical-memory \n"
  195. " -page-file \n"
  196. " -disk-space \n"
  197. " -cpu-time \n"
  198. " -kernel-pool \n"
  199. " \n");
  200. exit (1);
  201. }
  202. //
  203. // Function:
  204. //
  205. // SearchCmdlineOption
  206. //
  207. // Description:
  208. //
  209. // Helper function for cmdline parsing.
  210. //
  211. static char * *
  212. SearchCmdlineOption (
  213. char * Search,
  214. char * * Options)
  215. {
  216. for ( ; *Options; Options++) {
  217. if (_stricmp (Search, *Options) == 0) {
  218. return Options;
  219. }
  220. }
  221. return NULL;
  222. }
  223. //////////////////////////////////////////////////////////////////////
  224. /////////////////////////////////////////////// Baby consumer creation
  225. //////////////////////////////////////////////////////////////////////
  226. //
  227. // Function:
  228. //
  229. // CreateBabyConsumer
  230. //
  231. // Description:
  232. //
  233. // This function calls CreateProcess() with the command line
  234. // specified. This is used by some consumers that cannot eat
  235. // completely a resource from only one process. Typical examples
  236. // are physical memory and page file. Essentially in one process
  237. // you can consume up to 2Gb therefore we need more processes
  238. // for machines that have more than 2Gb of RAM.
  239. //
  240. BOOL
  241. CreateBabyConsumer (
  242. LPTSTR CommandLine)
  243. {
  244. BOOL Result;
  245. TCHAR CmdLine [MAX_PATH];
  246. STARTUPINFO StartInfo;
  247. PROCESS_INFORMATION ProcessInfo;
  248. strcpy (CmdLine, CommandLine);
  249. ZeroMemory (&StartInfo, sizeof StartInfo);
  250. ZeroMemory (&ProcessInfo, sizeof ProcessInfo);
  251. StartInfo.cb = sizeof StartInfo;
  252. Result = CreateProcess (
  253. NULL,
  254. CmdLine,
  255. NULL,
  256. NULL,
  257. 0,
  258. CREATE_NEW_CONSOLE,
  259. NULL,
  260. NULL,
  261. & StartInfo,
  262. & ProcessInfo);
  263. CloseHandle (ProcessInfo.hThread);
  264. CloseHandle (ProcessInfo.hProcess);
  265. return Result;
  266. }
  267. //
  268. // Function:
  269. //
  270. // CreatePhysicalMemoryConsumers
  271. //
  272. // Description:
  273. //
  274. // This function launches enough physical memory
  275. // consumer processes to insure that the whole physical
  276. // memory gets used.
  277. //
  278. VOID
  279. CreatePhysicalMemoryConsumers ()
  280. {
  281. MEMORYSTATUSEX MemoryInfo;
  282. DWORD Consumers;
  283. DWORD Index;
  284. ZeroMemory (&MemoryInfo, sizeof MemoryInfo);
  285. MemoryInfo.dwLength = sizeof MemoryInfo;
  286. GlobalMemoryStatusEx (&MemoryInfo);
  287. //
  288. // We will attempt to create a consumer for every 256Mb of physical
  289. // memory.
  290. //
  291. Consumers = 1 + (DWORD)(MemoryInfo.ullTotalPhys / SIZE_1_GB) * 4;
  292. Message ("Total physical memory: %I64X", MemoryInfo.ullTotalPhys);
  293. Message ("Available physical memory: %I64X", MemoryInfo.ullAvailPhys);
  294. Message ("Will attempt to create %u baby consumers ...", Consumers);
  295. for (Index = 0; Index < Consumers; Index++)
  296. if (CreateBabyConsumer ("consume -physical-memory-worker") == FALSE)
  297. Warning ("Cannot create baby consumer `-physical-memory-worker'");
  298. }
  299. //
  300. // Function:
  301. //
  302. // CreatePageFileConsumers
  303. //
  304. // Description:
  305. //
  306. // This function launches enough page file
  307. // consumer processes to insure that the whole page file
  308. // gets used.
  309. //
  310. VOID
  311. CreatePageFileConsumers ()
  312. {
  313. MEMORYSTATUSEX MemoryInfo;
  314. DWORD Consumers;
  315. DWORD Index;
  316. ZeroMemory (&MemoryInfo, sizeof MemoryInfo);
  317. MemoryInfo.dwLength = sizeof MemoryInfo;
  318. GlobalMemoryStatusEx (&MemoryInfo);
  319. //
  320. // We will attempt to create a consumer for every 256Mb of page file
  321. //
  322. Consumers = 1 + (DWORD)(MemoryInfo.ullTotalPageFile / SIZE_1_GB) * 4;
  323. Message ("Total page file: %I64X", MemoryInfo.ullTotalPageFile);
  324. Message ("Available page file: %I64X", MemoryInfo.ullAvailPageFile);
  325. Message ("Will attempt to create %u baby consumers ...", Consumers);
  326. for (Index = 0; Index < Consumers; Index++)
  327. if (CreateBabyConsumer ("consume -page-file-worker") == FALSE)
  328. Warning ("Cannot create baby consumer `-page-file-worker'");
  329. }
  330. //
  331. // Function:
  332. //
  333. // CreateKernelPoolConsumers
  334. //
  335. // Description:
  336. //
  337. // This function launches enough kernel pool
  338. // consumer processes to insure that the whole
  339. // non paged pool gets used.
  340. //
  341. VOID
  342. CreateKernelPoolConsumers ()
  343. {
  344. DWORD Consumers;
  345. DWORD Index;
  346. //
  347. // We will attempt to create 4 consumers
  348. //
  349. Consumers = 4;
  350. for (Index = 0; Index < Consumers; Index++)
  351. if (CreateBabyConsumer ("consume -kernel-pool-worker") == FALSE)
  352. Warning ("Cannot create baby consumer `-kernel-pool-worker'");
  353. }
  354. //
  355. // end of module: consume.cxx
  356. //