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.

169 lines
3.4 KiB

  1. //
  2. // Universal Resource Consumer: Just an innocent stress program
  3. // Copyright (c) Microsoft Corporation, 1997, 1998, 1999
  4. //
  5. //
  6. // module: physmem.cxx
  7. // author: silviuc
  8. // created: Fri Apr 10 14:05:05 1998
  9. //
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include <windows.h>
  14. #include "error.hxx"
  15. #include "physmem.hxx"
  16. #include "consume.hxx"
  17. //
  18. // Local functions
  19. //
  20. BOOL
  21. CheckMemoryRegion (
  22. LPVOID Address,
  23. SIZE_T Size);
  24. //
  25. // Function:
  26. //
  27. // ConsumeAllPhysicalMemory
  28. //
  29. // Description:
  30. //
  31. // This routine will consume all physical memory.
  32. // It will do so by increasing the working set size and locking the pages
  33. // in memory.
  34. //
  35. void ConsumeAllPhysicalMemory ()
  36. {
  37. BOOL Result;
  38. SIZE_T MinWsSize;
  39. SIZE_T MaxWsSize;
  40. SIZE_T RegionSize;
  41. LPVOID Region;
  42. //
  43. // For ever loop in which we try to grab as much physical memory
  44. // as possible.
  45. //
  46. for (RegionSize = 0x100000; ; )
  47. {
  48. //
  49. // Eat more ...
  50. //
  51. Region = VirtualAlloc (NULL, RegionSize, MEM_COMMIT, PAGE_READWRITE);
  52. if (Region == NULL) {
  53. Sleep (1000);
  54. //
  55. // We've got an allocation error.
  56. // Switch to 64K chunks and try again.
  57. //
  58. RegionSize = 0x10000;
  59. continue;
  60. }
  61. if (VirtualLock (Region, RegionSize) == FALSE) {
  62. VirtualFree (Region, 0, MEM_RELEASE);
  63. //
  64. // We have to increase the working set if possible.
  65. //
  66. Result = GetProcessWorkingSetSize(
  67. GetCurrentProcess(),
  68. &MinWsSize,
  69. &MaxWsSize);
  70. if (Result == FALSE)
  71. {
  72. Sleep (1000);
  73. continue;
  74. }
  75. MinWsSize += 0x10000;
  76. MaxWsSize += 0x10000;
  77. Result = SetProcessWorkingSetSize(
  78. GetCurrentProcess(),
  79. MinWsSize,
  80. MaxWsSize);
  81. if (Result == FALSE) {
  82. Sleep (1000);
  83. }
  84. continue;
  85. }
  86. else {
  87. //
  88. // Memory is allocated and locked. Touch it.
  89. //
  90. assert_ (Region != NULL);
  91. CheckMemoryRegion (Region, RegionSize);
  92. }
  93. printf (".");
  94. }
  95. }
  96. //
  97. // Function:
  98. //
  99. // CheckMemoryRegion
  100. //
  101. // Description:
  102. //
  103. // This function checks that the memory region is zeroed as
  104. // it should be when a region has just been allocated and
  105. // then reads and writes work properly.
  106. //
  107. BOOL
  108. CheckMemoryRegion (
  109. LPVOID Address,
  110. SIZE_T Size)
  111. {
  112. LPBYTE Start, End, Current;
  113. Current = Start = (LPBYTE) Address;
  114. End = Start + Size;
  115. for ( ; Current < End; Current += 1024) {
  116. if (*Current != 0 || (End - Current >= 1024 && *(Current + 1023) != 0)) {
  117. Message ("Page at %p not zeroed \n");
  118. return FALSE;
  119. }
  120. *Current = *(Current + 1023) = 0xAB;
  121. if (*Current != 0xAB || (End - Current >= 1024 && *(Current + 1023) != 0xAB)) {
  122. Message ("Page at %p has R/W error \n");
  123. return FALSE;
  124. }
  125. }
  126. return TRUE;
  127. }
  128. //
  129. // end of module: physmem.cxx
  130. //