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.

453 lines
13 KiB

  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <memory.h>
  5. #include <windows.h>
  6. #define SEG_1_SIZE 1024 * 1024
  7. #define SEG_X_SIZE 1024 * 1024 * 64
  8. #define W32_MEMMAN_ITERATIONS 150
  9. #define W32_MEMMAN_ITERATIONS2 20000
  10. #define W32_MMST04_ITERATIONS 100
  11. #define MemManSubtest5Count 200
  12. int TotalBenchMarks = 0;
  13. #define MAX_BENCHMARKS 32
  14. char *BenchMarkNames[ MAX_BENCHMARKS ];
  15. ULONG BenchMarkRates[ MAX_BENCHMARKS ];
  16. ULONG BenchMarkFracs[ MAX_BENCHMARKS ];
  17. typedef struct _PERFINFO {
  18. DWORD StartTime;
  19. DWORD StopTime;
  20. PCHAR Title;
  21. ULONG Iterations;
  22. } PERFINFO, *PPERFINFO;
  23. int
  24. StartBenchMark(
  25. PCHAR Title,
  26. ULONG Iterations,
  27. PPERFINFO PerfInfo
  28. );
  29. VOID
  30. FinishBenchMark(
  31. PPERFINFO PerfInfo
  32. );
  33. __cdecl
  34. main(
  35. )
  36. {
  37. PULONG_PTR p1, p2, p3, p4; // pointers into new segment
  38. PCHAR pa[MemManSubtest5Count]; // array for section pointers
  39. PULONG_PTR u1;
  40. ULONG actual; // actual xfer count for read
  41. ULONG ssize; // section allocation size var
  42. ULONG ii, ix; // loop index variables
  43. PERFINFO PerfInfo;
  44. ULONG Seg1Size;
  45. ULONG SegxSize;
  46. ULONG CommitSize;
  47. HANDLE CurrentProcessHandle, Section1;
  48. LARGE_INTEGER SectionSize;
  49. ULONG Size;
  50. ULONG ViewSize;
  51. printf("Win32 Memory Management test\n");
  52. Size = 1024 * 1024;
  53. p1 = NULL;
  54. p1 = VirtualAlloc(NULL, Size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  55. if (p1 == NULL) {
  56. printf("failed first created vm status %X start %p size %lx\n",
  57. GetLastError(),
  58. p1,
  59. Size);
  60. }
  61. for (p2 = p1; p2 < (p1 + (Size / sizeof(ULONG_PTR))); p2 += 1) {
  62. *p2 = (ULONG_PTR)p2;
  63. }
  64. SectionSize.QuadPart = 1024 * 1024;
  65. Section1 = CreateFileMapping(INVALID_HANDLE_VALUE,
  66. NULL,
  67. PAGE_READWRITE | SEC_COMMIT,
  68. SectionSize.HighPart,
  69. SectionSize.LowPart,
  70. NULL);
  71. if (!Section1) {
  72. printf("failed create big section status %ld\n", GetLastError());
  73. }
  74. p3 = NULL;
  75. ViewSize = 0;
  76. p3 = MapViewOfFile(Section1, FILE_MAP_WRITE, 0, 0, 0);
  77. if (p3 == NULL) {
  78. printf("service failed mapview - status %X\n", GetLastError());
  79. }
  80. //
  81. // Memory Management sub-test 1
  82. //
  83. // Create a 1 MB segment with commitment of the pages,
  84. // then touch each page, which should cause a fault and
  85. // a demand zero page to be allocated.
  86. //
  87. //
  88. StartBenchMark("Win32 MemMan0 - 1 Meg Copy", 150, &PerfInfo);
  89. for (ii = 0; ii < 150; ii += 1) {
  90. MoveMemory (p3, p1, Size);
  91. }
  92. FinishBenchMark(&PerfInfo);
  93. CloseHandle(Section1);
  94. if (!UnmapViewOfFile(p3)) {
  95. printf("unmap view service failed - status %X\n", GetLastError());
  96. }
  97. //
  98. // Memory Management sub-test 1
  99. //
  100. // Create a 1 MB segment with commitment of the pages,
  101. // then touch each page, which should cause a fault and
  102. // a demand zero page to be allocated.
  103. //
  104. //
  105. StartBenchMark("Win32 MemMan01 - create 1mb section, copy 1mb, delete",
  106. 150,
  107. &PerfInfo);
  108. for (ii = 0; ii < 150; ii += 1) {
  109. Section1 = CreateFileMapping(INVALID_HANDLE_VALUE,
  110. NULL,
  111. PAGE_READWRITE | SEC_COMMIT,
  112. SectionSize.HighPart,
  113. SectionSize.LowPart,
  114. NULL);
  115. if (!Section1) {
  116. printf("failed create big section status %X\n", GetLastError());
  117. }
  118. p3 = MapViewOfFile(Section1, FILE_MAP_WRITE, 0, 0, 0);
  119. if (p3 == NULL) {
  120. printf("service failed mapview - status %X\n", GetLastError());
  121. }
  122. MoveMemory(p3, p1, Size);
  123. p4 = MapViewOfFile(Section1, FILE_MAP_WRITE, 0, 0, 0);
  124. if (p4 == NULL) {
  125. printf("service failed mapview - status %X\n", GetLastError());
  126. }
  127. CloseHandle(Section1);
  128. if (!UnmapViewOfFile(p3)) {
  129. printf("unmap view service failed - status %X\n", GetLastError());
  130. }
  131. if (!UnmapViewOfFile(p4)) {
  132. printf("unmap view service failed - status %X\n", GetLastError());
  133. }
  134. }
  135. FinishBenchMark(&PerfInfo);
  136. //
  137. // Memory Management sub-test 1
  138. //
  139. // Create a 1 MB segment with commitment of the pages,
  140. // then touch each page, which should cause a fault and
  141. // a demand zero page to be allocated.
  142. //
  143. //
  144. StartBenchMark("Win32 MemMan02 - alloc 1mb vm, copy 1mb, delete",
  145. 150,
  146. &PerfInfo);
  147. for (ii = 0; ii < 150; ii++) {
  148. Size = 1024 * 1024;
  149. p3 = VirtualAlloc(NULL, Size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  150. if (p3 == NULL) {
  151. printf("service failed allocvm - status %ld\n", GetLastError());
  152. }
  153. MoveMemory (p3, p1, Size);
  154. if (!VirtualFree(p3, 0, MEM_RELEASE)) {
  155. printf("service failed freevm1 - status %X\n", GetLastError());
  156. }
  157. }
  158. FinishBenchMark(&PerfInfo);
  159. if (!VirtualFree (p1, 0, MEM_RELEASE)) {
  160. printf("service failed freevm2 - status %X\n", GetLastError());
  161. }
  162. //
  163. // Memory Management sub-test 1
  164. //
  165. // Create a 1 MB segment with commitment of the pages,
  166. // then touch each page, which should cause a fault and
  167. // a demand zero page to be allocated.
  168. //
  169. //
  170. StartBenchMark("Win32 MemMan1 - 1 Meg Seg, Create, Commit & Touch",
  171. W32_MEMMAN_ITERATIONS,
  172. &PerfInfo);
  173. for (ii = 0; ii < W32_MEMMAN_ITERATIONS; ii += 1) {
  174. Seg1Size = SEG_1_SIZE;
  175. p1 = VirtualAlloc(NULL, Seg1Size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  176. if (p1 == NULL) {
  177. printf("service failed - status %X\n", GetLastError());
  178. }
  179. for (p2 = p1; p2 < (p1 + (Seg1Size / sizeof(ULONG_PTR))); p2 += 4096 /sizeof(ULONG_PTR)) {
  180. *p2 = 99;
  181. }
  182. if (!VirtualFree(p1, 0, MEM_RELEASE)) {
  183. printf("service failed freevm3 - status %X\n", GetLastError());
  184. }
  185. }
  186. FinishBenchMark(&PerfInfo);
  187. //
  188. // Memory Management sub-test 1
  189. //
  190. // Create a 1 MB segment with commitment of the pages,
  191. // then touch each page, which should cause a fault and
  192. // a demand zero page to be allocated.
  193. //
  194. //
  195. StartBenchMark("Win32 MemMan1.5 - 1 Meg Seg, Create, reserve Commit & Touch",
  196. W32_MEMMAN_ITERATIONS,
  197. &PerfInfo);
  198. for (ii = 0; ii < W32_MEMMAN_ITERATIONS; ii += 1) {
  199. Seg1Size = SEG_1_SIZE;
  200. p1 = VirtualAlloc(NULL, Seg1Size, MEM_RESERVE | MEM_COMMIT, PAGE_READONLY);
  201. if (p1 == NULL) {
  202. printf("service failed - status %X\n", GetLastError());
  203. }
  204. if (!VirtualProtect(p1, Seg1Size, PAGE_READWRITE, &CommitSize)) {
  205. printf("service failed (ntprotect)- status %X\n", GetLastError());
  206. }
  207. for (p2 = p1; p2 < (p1 + (Seg1Size / sizeof(ULONG_PTR))); p2 += (4096 / sizeof(ULONG_PTR))) {
  208. *p2 = 99;
  209. }
  210. if (!VirtualFree(p1, 0, MEM_RELEASE)) {
  211. printf("service failed freevm4 - status %X\n", GetLastError());
  212. }
  213. }
  214. FinishBenchMark(&PerfInfo);
  215. //
  216. // Memory Management sub-test 2
  217. //
  218. // Create a 1 MB segment with commitment of the pages,
  219. // but never use the segment.
  220. //
  221. StartBenchMark("Win32 MemMan2 - 1 Meg Seg, Create & Commit Only",
  222. W32_MEMMAN_ITERATIONS2,
  223. &PerfInfo);
  224. for (ii = 0; ii < W32_MEMMAN_ITERATIONS2; ii += 1) {
  225. p1 = NULL;
  226. Seg1Size = SEG_1_SIZE;
  227. p1 = VirtualAlloc(NULL, Seg1Size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  228. if (p1 == NULL) {
  229. printf("service failed - status %X\n", GetLastError());
  230. }
  231. if (!VirtualFree(p1, 0, MEM_RELEASE)) {
  232. printf("service failed freevm5 - status %X\n", GetLastError());
  233. }
  234. }
  235. FinishBenchMark(&PerfInfo);
  236. //
  237. // Memory Management sub-test 3
  238. //
  239. // Create a 1 MB segment without commitment of the pages,
  240. // but never use or commit the segment.
  241. //
  242. StartBenchMark("Win32 MemMan3 - 1 Meg Seg Create Only",
  243. W32_MEMMAN_ITERATIONS2,
  244. &PerfInfo);
  245. for (ii = 0; ii < W32_MEMMAN_ITERATIONS2; ii += 1) {
  246. p1 = NULL;
  247. Seg1Size = SEG_1_SIZE;
  248. p1 = VirtualAlloc(NULL, Seg1Size, MEM_RESERVE, PAGE_READWRITE);
  249. if (p1 == NULL) {
  250. printf("service failed - status %X\n", GetLastError());
  251. }
  252. if (!VirtualFree(p1, 0, MEM_RELEASE)) {
  253. printf("service failed freevm6 - status %X\n", GetLastError());
  254. }
  255. }
  256. FinishBenchMark(&PerfInfo);
  257. //
  258. // Memory Management sub-test 4
  259. //
  260. // Create a 64 MB segment without committing the pages,
  261. // then commit and touch at 128 KB intervals.
  262. //
  263. //
  264. StartBenchMark("Win32 MemMan4 - 64 Meg Seg, Commit Sparse",
  265. W32_MMST04_ITERATIONS,
  266. &PerfInfo);
  267. for (ii = 0; ii < W32_MMST04_ITERATIONS; ii += 1) {
  268. p1 = NULL;
  269. SegxSize = SEG_X_SIZE;
  270. p1 = VirtualAlloc(NULL, SegxSize, MEM_RESERVE, PAGE_READWRITE);
  271. if (p1 == NULL) {
  272. printf("service failed - status %X\n", GetLastError());
  273. }
  274. CommitSize = 4;
  275. for (p2 = p1; p2 < (p1 + (SegxSize / sizeof(ULONG_PTR))); p2 += (256 * 1024 / sizeof(ULONG_PTR))) {
  276. p2 = VirtualAlloc(p2, CommitSize, MEM_COMMIT, PAGE_READWRITE);
  277. if (p2 == NULL) {
  278. printf("service failed - status %X\n", GetLastError());
  279. }
  280. if (*p2 != 0) {
  281. printf("%p = %lx\n", p2, *p2);
  282. }
  283. }
  284. if (!VirtualFree(p1, 0, MEM_RELEASE)) {
  285. printf("service failed freevm7 - status %X\n", GetLastError());
  286. }
  287. }
  288. FinishBenchMark(&PerfInfo);
  289. //
  290. // Memory Management sub-test 5
  291. //
  292. // Create a alternatively 232k and 112 k memory sections.
  293. // For every 2 created, delete 1. Do this for MemManSubtest5Count times.
  294. //
  295. //
  296. StartBenchMark("Win32 MemMan5 - Sparse Section Create/Delete Benchmark",
  297. W32_MEMMAN_ITERATIONS,
  298. &PerfInfo);
  299. for (ii = 0; ii < W32_MEMMAN_ITERATIONS; ii += 1) {
  300. for (ix = 0; ix < MemManSubtest5Count; ix += 1) {
  301. //
  302. // determine if even or odd allocation, if even and not 0,
  303. // delete a section
  304. //
  305. ssize = (112 * 1024); //assume ODD allocation
  306. if ((ix & 1) == 0) { //if it is an even one
  307. ssize = (232 * 1024); //allocate 232 K on even passes
  308. if (ix) { //except on pass 0
  309. if (!VirtualFree(pa[ix/2], 0, MEM_RELEASE)) {
  310. printf("service failed freevm8 - status %X\n", GetLastError());
  311. }
  312. pa[ix / 2] = 0; //remember this one is gone
  313. }
  314. }
  315. pa[ix] = VirtualAlloc(NULL, ssize, MEM_RESERVE, PAGE_READWRITE);
  316. if (pa[ix] == NULL) {
  317. printf("service failed - status %X\n", GetLastError());
  318. }
  319. }
  320. //
  321. // Now free up the memory used in this test
  322. //
  323. for (ix = 0; ix < MemManSubtest5Count; ix += 1) {
  324. if (pa[ix] != 0) {
  325. if (!VirtualFree(pa[ix], 0, MEM_RELEASE)) {
  326. printf("service failed freevm9 - status %X\n", GetLastError());
  327. }
  328. }
  329. }
  330. }
  331. FinishBenchMark(&PerfInfo);
  332. printf("that's all\n");
  333. return (TRUE);
  334. }
  335. int
  336. StartBenchMark(
  337. PCHAR Title,
  338. ULONG Iterations,
  339. PPERFINFO PerfInfo
  340. )
  341. {
  342. printf("*** Start %s : %d\n",
  343. PerfInfo->Title = Title,
  344. PerfInfo->Iterations = Iterations);
  345. PerfInfo->StartTime = GetCurrentTime();
  346. return TRUE;
  347. }
  348. VOID
  349. FinishBenchMark(
  350. PPERFINFO PerfInfo
  351. )
  352. {
  353. ULONG TotalMilliSeconds;
  354. ULONG IterationsPerSecond;
  355. ULONG IterationFractions;
  356. DWORD Delta;
  357. PerfInfo->StopTime = GetCurrentTime();
  358. TotalMilliSeconds = PerfInfo->StopTime - PerfInfo->StartTime;
  359. IterationsPerSecond = (1000 * PerfInfo->Iterations) / TotalMilliSeconds;
  360. IterationFractions = (1000 * PerfInfo->Iterations) % TotalMilliSeconds;
  361. IterationFractions = (1000 * IterationFractions) / TotalMilliSeconds;
  362. if (1) {
  363. printf(" iterations - %9d\n", PerfInfo->Iterations );
  364. printf(" milliseconds - %9d\n", TotalMilliSeconds );
  365. printf(" iterations/sec - %5d.%3d\n\n",
  366. IterationsPerSecond,
  367. IterationFractions);
  368. }
  369. BenchMarkNames[TotalBenchMarks] = PerfInfo->Title;
  370. BenchMarkRates[TotalBenchMarks] = IterationsPerSecond;
  371. BenchMarkFracs[TotalBenchMarks] = IterationFractions;
  372. TotalBenchMarks++;
  373. }