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.

275 lines
6.2 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. vmstress.c
  5. Abstract:
  6. Test stress program for virtual memory.
  7. Author:
  8. Lou Perazzoli (LouP) 26-Jul-91
  9. Revision History:
  10. --*/
  11. #include <assert.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <memory.h>
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include <windows.h>
  19. typedef struct _INIT_ARG {
  20. PULONG_PTR Va;
  21. SIZE_T Size;
  22. } INIT_ARG, *PINITARG;
  23. VOID
  24. VmRandom1 (
  25. LPVOID ThreadParameter
  26. );
  27. VOID
  28. VmRandom2 (
  29. LPVOID ThreadParameter
  30. );
  31. VOID
  32. VmRandom1 (
  33. LPVOID ThreadParameter
  34. )
  35. {
  36. PINITARG InitialArg;
  37. ULONG Seed = 8373833;
  38. SIZE_T size;
  39. PULONG_PTR startva0;
  40. PULONG_PTR Va;
  41. ULONG i,j;
  42. InitialArg = (PINITARG)ThreadParameter;
  43. startva0 = InitialArg->Va;
  44. size = InitialArg->Size;
  45. // printf("starting random references in thread1\n");
  46. for (j = 1; j < 10; j++) {
  47. for (i = 1 ; i < 2500; i++) {
  48. RtlRandom (&Seed);
  49. Va = startva0 + (Seed % (size / sizeof(ULONG_PTR)));
  50. if (*Va == (((ULONG_PTR)Va + 1))) {
  51. *Va = (ULONG_PTR)Va;
  52. } else {
  53. if (*Va != (ULONG_PTR)Va) {
  54. printf("bad random value in cell %p was %p\n", Va, (void *)*Va);
  55. }
  56. }
  57. }
  58. Sleep (150);
  59. }
  60. // printf("terminating thread1\n");
  61. ExitThread(0);
  62. }
  63. VOID
  64. VmRandom2 (
  65. LPVOID ThreadParameter
  66. )
  67. {
  68. PINITARG InitialArg;
  69. ULONG Seed = 8373839;
  70. SIZE_T size;
  71. PULONG_PTR startva0;
  72. PULONG_PTR Va;
  73. ULONG i,j;
  74. InitialArg = (PINITARG)ThreadParameter;
  75. startva0 = InitialArg->Va;
  76. size = InitialArg->Size;
  77. // printf("starting random references in thread2\n");
  78. for (j = 1; j < 10; j++) {
  79. for (i = 1 ; i < 2500; i++) {
  80. RtlRandom (&Seed);
  81. Va = startva0 + (Seed % (size / sizeof(ULONG_PTR)));
  82. if (*Va == (((ULONG_PTR)Va + 1))) {
  83. *Va = (ULONG_PTR)Va;
  84. } else {
  85. if (*Va != (ULONG_PTR)Va) {
  86. printf("bad random value in cell %p was %lx\n", Va, *Va);
  87. }
  88. }
  89. }
  90. Sleep (150);
  91. }
  92. // printf("terminating thread2\n");
  93. ExitThread(0);
  94. }
  95. DWORD
  96. __cdecl main(
  97. int argc,
  98. char *argv[],
  99. char *envp[]
  100. )
  101. {
  102. HANDLE Objects[2];
  103. MEMORYSTATUS MemStatus;
  104. INIT_ARG InitialArg;
  105. PULONG_PTR Va;
  106. PULONG_PTR EndVa;
  107. SIZE_T size;
  108. PULONG_PTR startva0;
  109. NTSTATUS status;
  110. DWORD ThreadId1, ThreadId2;
  111. ULONG count = 0;
  112. printf("Starting virtual memory stress test\n");
  113. for (;;) {
  114. //
  115. // Create a region of private memory based on the number of
  116. // available pages on this system.
  117. //
  118. GlobalMemoryStatus(&MemStatus);
  119. size = MemStatus.dwAvailPhys;
  120. if (size == 0) {
  121. size = 4096;
  122. }
  123. else {
  124. size -= 4*4096;
  125. }
  126. while (size != 0) {
  127. startva0 = NULL;
  128. status = NtAllocateVirtualMemory (NtCurrentProcess(),
  129. (PVOID *)&startva0,
  130. 0,
  131. &size,
  132. MEM_COMMIT | MEM_RESERVE,
  133. PAGE_READWRITE);
  134. if (NT_SUCCESS(status)) {
  135. break;
  136. }
  137. else {
  138. //
  139. // Try for less memory.
  140. //
  141. size -= 4096;
  142. }
  143. }
  144. printf("created vm status, startva, size, %lX %p %p\n",
  145. status, startva0, (void *)size);
  146. if (!NT_SUCCESS(status)) {
  147. ExitProcess (0);
  148. }
  149. InitialArg.Va = startva0;
  150. InitialArg.Size = size;
  151. //
  152. // Set all memory to know values (not zeroes).
  153. //
  154. printf("initializing memory\n");
  155. EndVa = (PULONG_PTR)startva0 + (size/sizeof(ULONG_PTR));
  156. Va = startva0;
  157. while (Va < EndVa) {
  158. *Va = (ULONG_PTR)Va + 1;
  159. Va += 1;
  160. }
  161. do {
  162. Objects[0] = CreateThread(NULL,
  163. 0L,
  164. (LPTHREAD_START_ROUTINE)VmRandom1,
  165. (LPVOID)&InitialArg,
  166. 0,
  167. &ThreadId1);
  168. //
  169. // Must have run out of memory, wait a while and then try again.
  170. //
  171. if (Objects[0] == (HANDLE)0) {
  172. Sleep (3000);
  173. }
  174. } while (Objects[0] == (HANDLE)0);
  175. do {
  176. Objects[1] = CreateThread(NULL,
  177. 0L,
  178. (LPTHREAD_START_ROUTINE)VmRandom2,
  179. (LPVOID)&InitialArg,
  180. 0,
  181. &ThreadId2);
  182. //
  183. // Must have run out of memory, wait a while and then try again.
  184. //
  185. if (Objects[1] == (HANDLE)0) {
  186. Sleep (3000);
  187. }
  188. } while (Objects[1] == (HANDLE)0);
  189. WaitForMultipleObjects (2,
  190. Objects,
  191. TRUE,
  192. -1);
  193. count += 1;
  194. printf("stress test pass number %ld complete\n",count);
  195. CloseHandle (Objects[0]);
  196. CloseHandle (Objects[1]);
  197. printf("freeing vm startva, size, %p %p\n",
  198. startva0, (void *)size);
  199. status = NtFreeVirtualMemory (NtCurrentProcess(),
  200. (PVOID *)&startva0,
  201. &size,
  202. MEM_RELEASE);
  203. if (!NT_SUCCESS(status)) {
  204. ExitProcess (0);
  205. }
  206. Sleep (1000);
  207. }
  208. return 0;
  209. }