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.

234 lines
4.8 KiB

  1. //
  2. // Universal Resource Consumer: Just an innocent stress program
  3. // Copyright (c) Microsoft Corporation, 1997, 1998, 1999
  4. //
  5. //
  6. // module: cputime.cxx
  7. // author: silviuc
  8. // created: Wed Jun 17 18:48:56 1998
  9. //
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include <windows.h>
  14. #include "error.hxx"
  15. #include "cputime.hxx"
  16. #include "consume.hxx"
  17. //
  18. // Function:
  19. //
  20. // ThreadVirtualMemoryStuff
  21. //
  22. // Description:
  23. //
  24. // This function is run by some consumer threads.
  25. // It is supposed to perform alloc/free cycles in order
  26. // to keep the memory manager busy.
  27. //
  28. #if _MSC_FULL_VER >= 13008827
  29. #pragma warning(push)
  30. #pragma warning(disable:4715) // Not all control paths return (due to infinite loop)
  31. #endif
  32. static DWORD
  33. ThreadVirtualMemoryStuff (LPVOID)
  34. {
  35. PVOID Address;
  36. for ( ; ; )
  37. {
  38. //
  39. // Allocate a normal space 64K chunk, touch it in
  40. // one page and then free it.
  41. //
  42. Address = (PVOID)(UINT_PTR)(( rand() << 16) | rand());
  43. try
  44. {
  45. Address = VirtualAlloc (
  46. Address,
  47. 0x10000,
  48. MEM_RESERVE | MEM_COMMIT,
  49. PAGE_READWRITE);
  50. if (Address) {
  51. *((DWORD *)Address) = 0xAABBCCDD;
  52. VirtualFree (
  53. Address,
  54. 0,
  55. MEM_RELEASE);
  56. }
  57. }
  58. catch (...)
  59. {
  60. Message ("VirtualAlloc/Free scenario raised exception");
  61. }
  62. } // for ( ; ; )
  63. //
  64. // Make compiler happy.
  65. //
  66. return 0;
  67. }
  68. #if _MSC_FULL_VER >= 13008827
  69. #pragma warning(pop)
  70. #endif
  71. //
  72. // Function:
  73. //
  74. // ThreadVolumeInformationStuff
  75. //
  76. // Description:
  77. //
  78. // This function is run by some consumer threads.
  79. // It is supposed to call GetVolumeInformation in a loop.
  80. // This makes some calls inside the I/O manager.
  81. //
  82. #if _MSC_FULL_VER >= 13008827
  83. #pragma warning(push)
  84. #pragma warning(disable:4715) // Not all control paths return (due to infinite loop)
  85. #endif
  86. static DWORD
  87. ThreadVolumeInformationStuff (LPVOID)
  88. {
  89. for ( ; ; )
  90. {
  91. TCHAR Name [MAX_PATH];
  92. DWORD SerialNumber;
  93. DWORD MaxNameLength;
  94. DWORD FileSystemFlags;
  95. TCHAR FileSystemName [MAX_PATH];
  96. try {
  97. GetVolumeInformation (
  98. NULL,
  99. Name,
  100. MAX_PATH,
  101. & SerialNumber,
  102. & MaxNameLength,
  103. & FileSystemFlags,
  104. FileSystemName,
  105. MAX_PATH);
  106. }
  107. catch (...) {
  108. Message ("GetVolumeInformation scenario raised exception");
  109. }
  110. }
  111. //
  112. // Make compile rhappy.
  113. //
  114. return 0;
  115. }
  116. #if _MSC_FULL_VER >= 13008827
  117. #pragma warning(pop)
  118. #endif
  119. //
  120. // Global:
  121. //
  122. // ThreadFunction
  123. //
  124. // Description:
  125. //
  126. // This is a vector containing all thread functions available.
  127. // We will pick at random from here when we want to create
  128. // a thread.
  129. //
  130. static LPTHREAD_START_ROUTINE
  131. ThreadFunction [] =
  132. {
  133. ThreadVirtualMemoryStuff,
  134. ThreadVolumeInformationStuff
  135. };
  136. //
  137. // Function:
  138. //
  139. // ConsumeAllCpuTime
  140. //
  141. // Description:
  142. //
  143. // This function will try to swamp the system with threads.
  144. // The "swamp" calculation is very simple. For every processor
  145. // we create 128 threads having normal priority.
  146. //
  147. void ConsumeAllCpuTime ()
  148. {
  149. DWORD MaxThreadCount;
  150. DWORD MaxThreadFunction;
  151. SYSTEM_INFO SystemInfo;
  152. DWORD Index;
  153. //
  154. // Decide how many threads we should attempt to start.
  155. // Every processor will add 128 threads.
  156. //
  157. GetSystemInfo (& SystemInfo);
  158. MaxThreadCount = 128 * SystemInfo.dwNumberOfProcessors;
  159. Message ("Attempting to start %u threads ...", MaxThreadCount);
  160. //
  161. // Create all threads required. If we fail to create a thread due
  162. // to low memory conditions we will wait in a tight loop for better
  163. // conditions.
  164. //
  165. MaxThreadFunction = (sizeof ThreadFunction) / (sizeof ThreadFunction[0]);
  166. for (Index = 0; Index < MaxThreadCount; Index++)
  167. {
  168. HANDLE Thread;
  169. DWORD ThreadId;
  170. DWORD FunctionIndex;
  171. do
  172. {
  173. try {
  174. FunctionIndex = rand() % MaxThreadFunction;
  175. // Message ("Index function %u", FunctionIndex);
  176. Thread = CreateThread (
  177. NULL,
  178. 0,
  179. ThreadFunction [FunctionIndex],
  180. NULL,
  181. 0,
  182. & ThreadId);
  183. CloseHandle (Thread);
  184. }
  185. catch (...) {
  186. Message ("CreateThread() raised exception");
  187. }
  188. }
  189. while (Thread == NULL);
  190. printf (".");
  191. }
  192. printf("\n");
  193. }
  194. //
  195. // end of module: cputime.cxx
  196. //