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.

190 lines
4.0 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <windows.h>
  7. #include <string.h>
  8. typedef DWORD ERESOURCE;
  9. #include "\nt\private\ntos\inc\heap.h"
  10. #define ITERATIONS 1000
  11. #define SIZES 10
  12. DWORD Sizes[SIZES] = {
  13. 100,
  14. 10,
  15. 128,
  16. 256,
  17. 256,
  18. 100,
  19. 32,
  20. 64,
  21. 64,
  22. 100
  23. };
  24. VOID
  25. LaThread(PVOID h)
  26. {
  27. int i,j;
  28. PVOID p;
  29. WaitForSingleObject((HANDLE)h,INFINITE);
  30. for(i=0;i<ITERATIONS;i++){
  31. for(j=0;j<SIZES;j++) {
  32. if ( j & 1 ) {
  33. p = LocalAlloc(LMEM_FIXED,Sizes[j]);
  34. }
  35. else {
  36. p = LocalAlloc(LMEM_ZEROINIT,Sizes[j]);
  37. }
  38. if ( p ) {
  39. LocalFree(p);
  40. }
  41. else {
  42. printf("Alloc In Thread Failed\n");
  43. ExitProcess(1);
  44. }
  45. }
  46. }
  47. }
  48. VOID
  49. StartTest(
  50. DWORD SpinCount,
  51. DWORD ThreadCount
  52. )
  53. {
  54. PHANDLE Threads,p;
  55. HANDLE hStartEvent;
  56. DWORD Id;
  57. DWORD i;
  58. PHEAP ProcessHeap;
  59. PRTL_CRITICAL_SECTION HeapLock;
  60. DWORD Start,End;
  61. ProcessHeap = (PHEAP)NtCurrentPeb()->ProcessHeap;
  62. HeapLock = &ProcessHeap->LockVariable->Lock.CriticalSection;
  63. if ( HeapLock ) {
  64. HeapLock->DebugInfo->ContentionCount = 0;
  65. if ( SpinCount != 0xffffffff) {
  66. HeapLock->SpinCount = SpinCount;
  67. }
  68. }
  69. hStartEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  70. if ( !hStartEvent ) {
  71. printf("CreateEvent Failed %d\n",GetLastError());
  72. ExitProcess(1);
  73. }
  74. if ( ThreadCount < 2 ) {
  75. ThreadCount = 2;
  76. }
  77. if ( ThreadCount > 64 ) {
  78. ThreadCount = 64;
  79. }
  80. Threads = LocalAlloc(LMEM_ZEROINIT,sizeof(HANDLE)*ThreadCount);
  81. if ( !Threads ) {
  82. printf("Alloc Threads Failed\n");
  83. ExitProcess(1);
  84. }
  85. for(i=0,p=Threads;i<ThreadCount;i++,p++){
  86. *p = CreateThread(
  87. NULL,
  88. 0,
  89. (LPTHREAD_START_ROUTINE)LaThread,
  90. (PVOID)hStartEvent,
  91. 0,
  92. &Id
  93. );
  94. if ( !*p ) {
  95. printf("CreateThread Failed %d\n",GetLastError());
  96. ExitProcess(1);
  97. }
  98. }
  99. Start = GetTickCount();
  100. SetEvent(hStartEvent);
  101. if ( WaitForMultipleObjects(ThreadCount,Threads,TRUE,INFINITE) == WAIT_FAILED ) {
  102. printf("WaitMultiple Failed %d\n",GetLastError());
  103. ExitProcess(1);
  104. }
  105. End = GetTickCount();
  106. for(i=0,p=Threads;i<ThreadCount;i++,p++){
  107. CloseHandle(*p);
  108. }
  109. CloseHandle(hStartEvent);
  110. if ( !HeapLock ) {
  111. HeapLock = &ProcessHeap->LockVariable->Lock.CriticalSection;
  112. if ( !HeapLock ) {
  113. printf("No Heap Lock found\n");
  114. ExitProcess(1);
  115. }
  116. }
  117. printf("Heap Contention Threads %2d SpinCount %5d -> Contention %7d TickCount %d\n",ThreadCount,SpinCount,HeapLock->DebugInfo->ContentionCount,End-Start);
  118. }
  119. DWORD
  120. _cdecl
  121. main(
  122. int argc,
  123. char *argv[],
  124. char *envp[]
  125. )
  126. {
  127. StartTest(0xffffffff,2);
  128. StartTest(0,2);
  129. StartTest(1000,2);
  130. StartTest(3000,2);
  131. StartTest(4000,2);
  132. StartTest(5000,2);
  133. StartTest(10000,2);
  134. printf("\n");
  135. StartTest(0,4);
  136. StartTest(1000,4);
  137. StartTest(3000,4);
  138. StartTest(4000,4);
  139. StartTest(5000,4);
  140. StartTest(10000,4);
  141. printf("\n");
  142. StartTest(0,5);
  143. StartTest(1000,5);
  144. StartTest(3000,5);
  145. StartTest(4000,5);
  146. StartTest(5000,5);
  147. StartTest(10000,5);
  148. printf("\n");
  149. StartTest(0,6);
  150. StartTest(1000,6);
  151. StartTest(3000,6);
  152. StartTest(4000,6);
  153. StartTest(5000,6);
  154. StartTest(10000,6);
  155. printf("\n");
  156. StartTest(0,8);
  157. StartTest(1000,8);
  158. StartTest(3000,8);
  159. StartTest(4000,8);
  160. StartTest(5000,8);
  161. StartTest(10000,8);
  162. }