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.

413 lines
9.6 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. // Create a job object and assign it to itself. This will help
  75. // terminating baby consumer processes. However the assign will
  76. // fail if the consumer is already inside a job object (e.g. dks
  77. // scheduler).
  78. //
  79. Job = CreateJobObject (0, 0);
  80. Result = AssignProcessToJobObject (Job, GetCurrentProcess());
  81. if (Job && Result) {
  82. Message ("Successfully assigned process to a job object ...");
  83. }
  84. //
  85. // Figure out if a time out parameter has been specified.
  86. //
  87. {
  88. char * * Option;
  89. Option = SearchCmdlineOption ("-time", argv);
  90. if (Option && *(Option + 1)) {
  91. TimeOut = atoi (*(Option + 1));
  92. Message ("Time out after %u seconds.", TimeOut);
  93. TimeOut *= 1000;
  94. }
  95. }
  96. //
  97. // Parse command line. For every command we execute the consumption
  98. // scenario and then we sleep forever with the resource hold.
  99. //
  100. if (SearchCmdlineOption ("-disk-space", argv)) {
  101. ConsumeAllDiskSpace ();
  102. SleepSomeTime (TimeOut, Job);
  103. }
  104. else if (SearchCmdlineOption ("-cpu-time", argv)) {
  105. ConsumeAllCpuTime ();
  106. SleepSomeTime (TimeOut, Job);
  107. }
  108. else if (SearchCmdlineOption ("-kernel-pool", argv)) {
  109. CreateKernelPoolConsumers ();
  110. SleepSomeTime (TimeOut, Job);
  111. }
  112. else if (SearchCmdlineOption ("-physical-memory", argv)) {
  113. CreatePhysicalMemoryConsumers ();
  114. SleepSomeTime (TimeOut, Job);
  115. }
  116. else if (SearchCmdlineOption ("-page-file", argv)) {
  117. CreatePageFileConsumers ();
  118. SleepSomeTime (TimeOut, Job);
  119. }
  120. else if (SearchCmdlineOption ("-physical-memory-worker", argv)) {
  121. ConsumeAllPhysicalMemory ();
  122. SleepSomeTime (TimeOut, Job);
  123. }
  124. else if (SearchCmdlineOption ("-page-file-worker", argv)) {
  125. ConsumeAllPageFile ();
  126. SleepSomeTime (TimeOut, Job);
  127. }
  128. else if (SearchCmdlineOption ("-kernel-pool-worker", argv)) {
  129. ConsumeAllNonpagedPool ();
  130. SleepSomeTime (TimeOut, Job);
  131. }
  132. else {
  133. Help ();
  134. }
  135. }
  136. //
  137. // Function:
  138. //
  139. // SleepSomeTime
  140. //
  141. // Description:
  142. //
  143. // Sleeps forever.
  144. //
  145. static void
  146. SleepSomeTime (
  147. DWORD TimeOut,
  148. HANDLE Job)
  149. {
  150. Message ("Sleeping ...");
  151. fflush (stdout);
  152. if (TimeOut == INFINITE) {
  153. while (1) {
  154. Sleep (10000);
  155. }
  156. }
  157. else {
  158. Sleep (TimeOut);
  159. if (Job) {
  160. TerminateJobObject (Job, 0xAABBBBAA);
  161. }
  162. }
  163. }
  164. //
  165. // Function:
  166. //
  167. // Help
  168. //
  169. // Description:
  170. //
  171. // Prints help information to stdout.
  172. //
  173. static void
  174. Help ()
  175. {
  176. printf (
  177. "Universal Resource Consumer - Just an innocent stress program, v 0.1.0 \n"
  178. "Copyright (c) 1998, 1999, Microsoft Corporation \n"
  179. " \n"
  180. " consume RESOURCE [-time SECONDS] \n"
  181. " \n"
  182. "RESOURCE can be one of the following: \n"
  183. " \n"
  184. " -physical-memory \n"
  185. " -page-file \n"
  186. " -disk-space \n"
  187. " -cpu-time \n"
  188. " -kernel-pool \n"
  189. " \n");
  190. exit (1);
  191. }
  192. //
  193. // Function:
  194. //
  195. // SearchCmdlineOption
  196. //
  197. // Description:
  198. //
  199. // Helper function for cmdline parsing.
  200. //
  201. static char * *
  202. SearchCmdlineOption (
  203. char * Search,
  204. char * * Options)
  205. {
  206. for ( ; *Options; Options++) {
  207. if (_stricmp (Search, *Options) == 0) {
  208. return Options;
  209. }
  210. }
  211. return NULL;
  212. }
  213. //////////////////////////////////////////////////////////////////////
  214. /////////////////////////////////////////////// Baby consumer creation
  215. //////////////////////////////////////////////////////////////////////
  216. //
  217. // Function:
  218. //
  219. // CreateBabyConsumer
  220. //
  221. // Description:
  222. //
  223. // This function calls CreateProcess() with the command line
  224. // specified. This is used by some consumers that cannot eat
  225. // completely a resource from only one process. Typical examples
  226. // are physical memory and page file. Essentially in one process
  227. // you can consume up to 2Gb therefore we need more processes
  228. // for machines that have more than 2Gb of RAM.
  229. //
  230. BOOL
  231. CreateBabyConsumer (
  232. LPTSTR CommandLine)
  233. {
  234. BOOL Result;
  235. TCHAR CmdLine [MAX_PATH];
  236. STARTUPINFO StartInfo;
  237. PROCESS_INFORMATION ProcessInfo;
  238. strcpy (CmdLine, CommandLine);
  239. ZeroMemory (&StartInfo, sizeof StartInfo);
  240. ZeroMemory (&ProcessInfo, sizeof ProcessInfo);
  241. StartInfo.cb = sizeof StartInfo;
  242. Result = CreateProcess (
  243. NULL,
  244. CmdLine,
  245. NULL,
  246. NULL,
  247. 0,
  248. CREATE_NEW_CONSOLE,
  249. NULL,
  250. NULL,
  251. & StartInfo,
  252. & ProcessInfo);
  253. CloseHandle (ProcessInfo.hThread);
  254. CloseHandle (ProcessInfo.hProcess);
  255. return Result;
  256. }
  257. //
  258. // Function:
  259. //
  260. // CreatePhysicalMemoryConsumers
  261. //
  262. // Description:
  263. //
  264. // This function launches enough physical memory
  265. // consumer processes to insure that the whole physical
  266. // memory gets used.
  267. //
  268. VOID
  269. CreatePhysicalMemoryConsumers ()
  270. {
  271. MEMORYSTATUSEX MemoryInfo;
  272. DWORD Consumers;
  273. DWORD Index;
  274. ZeroMemory (&MemoryInfo, sizeof MemoryInfo);
  275. MemoryInfo.dwLength = sizeof MemoryInfo;
  276. GlobalMemoryStatusEx (&MemoryInfo);
  277. //
  278. // We will attempt to create a consumer for every 256Mb of physical
  279. // memory.
  280. //
  281. Consumers = 1 + (DWORD)(MemoryInfo.ullTotalPhys / SIZE_1_GB) * 4;
  282. Message ("Total physical memory: %I64X", MemoryInfo.ullTotalPhys);
  283. Message ("Available physical memory: %I64X", MemoryInfo.ullAvailPhys);
  284. Message ("Will attempt to create %u baby consumers ...", Consumers);
  285. for (Index = 0; Index < Consumers; Index++)
  286. if (CreateBabyConsumer ("consume -physical-memory-worker") == FALSE)
  287. Warning ("Cannot create baby consumer `-physical-memory-worker'");
  288. }
  289. //
  290. // Function:
  291. //
  292. // CreatePageFileConsumers
  293. //
  294. // Description:
  295. //
  296. // This function launches enough page file
  297. // consumer processes to insure that the whole page file
  298. // gets used.
  299. //
  300. VOID
  301. CreatePageFileConsumers ()
  302. {
  303. MEMORYSTATUSEX MemoryInfo;
  304. DWORD Consumers;
  305. DWORD Index;
  306. ZeroMemory (&MemoryInfo, sizeof MemoryInfo);
  307. MemoryInfo.dwLength = sizeof MemoryInfo;
  308. GlobalMemoryStatusEx (&MemoryInfo);
  309. //
  310. // We will attempt to create a consumer for every 256Mb of page file
  311. //
  312. Consumers = 1 + (DWORD)(MemoryInfo.ullTotalPageFile / SIZE_1_GB) * 4;
  313. Message ("Total page file: %I64X", MemoryInfo.ullTotalPageFile);
  314. Message ("Available page file: %I64X", MemoryInfo.ullAvailPageFile);
  315. Message ("Will attempt to create %u baby consumers ...", Consumers);
  316. for (Index = 0; Index < Consumers; Index++)
  317. if (CreateBabyConsumer ("consume -page-file-worker") == FALSE)
  318. Warning ("Cannot create baby consumer `-page-file-worker'");
  319. }
  320. //
  321. // Function:
  322. //
  323. // CreateKernelPoolConsumers
  324. //
  325. // Description:
  326. //
  327. // This function launches enough kernel pool
  328. // consumer processes to insure that the whole
  329. // non paged pool gets used.
  330. //
  331. VOID
  332. CreateKernelPoolConsumers ()
  333. {
  334. DWORD Consumers;
  335. DWORD Index;
  336. //
  337. // We will attempt to create 4 consumers
  338. //
  339. Consumers = 4;
  340. for (Index = 0; Index < Consumers; Index++)
  341. if (CreateBabyConsumer ("consume -kernel-pool-worker") == FALSE)
  342. Warning ("Cannot create baby consumer `-kernel-pool-worker'");
  343. }
  344. //
  345. // end of module: consume.cxx
  346. //