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.

421 lines
7.2 KiB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <string.h>
  5. #define FIVEK 5000
  6. #define TENK 10000
  7. #define ONEHUNK 100000
  8. #define ONEMIL 1000000
  9. #define MEMORY_TEST 200000000
  10. VOID
  11. _fastcall
  12. MemToReg (
  13. IN ULONG Count,
  14. IN PULONG Address
  15. );
  16. VOID
  17. _fastcall
  18. RegToMem (
  19. IN ULONG Count,
  20. IN PULONG Address
  21. );
  22. //
  23. // Define local types.
  24. //
  25. typedef struct _PERFINFO {
  26. DWORD StartTime;
  27. DWORD StopTime;
  28. LPSTR Title;
  29. DWORD Iterations;
  30. } PERFINFO, *PPERFINFO;
  31. VOID
  32. FinishBenchMark (
  33. IN PPERFINFO PerfInfo
  34. )
  35. {
  36. DWORD ContextSwitches;
  37. DWORD Duration;
  38. DWORD Performance;
  39. //
  40. // Print results and announce end of test.
  41. //
  42. PerfInfo->StopTime = GetTickCount();
  43. Duration = PerfInfo->StopTime - PerfInfo->StartTime;
  44. printf(" Test time in milliseconds %d\n", Duration);
  45. printf(" Number of iterations %d\n", PerfInfo->Iterations);
  46. Performance = PerfInfo->Iterations * 1000 / Duration;
  47. printf(" Iterations per second %d\n", Performance);
  48. printf("*** End of Test ***\n\n");
  49. return;
  50. }
  51. VOID
  52. StartBenchMark (
  53. IN PCHAR Title,
  54. IN DWORD Iterations,
  55. IN PPERFINFO PerfInfo
  56. )
  57. {
  58. //
  59. // Announce start of test and the number of iterations.
  60. //
  61. printf("*** Start of test ***\n %s\n", Title);
  62. PerfInfo->Title = Title;
  63. PerfInfo->Iterations = Iterations;
  64. PerfInfo->StartTime = GetTickCount();
  65. return;
  66. }
  67. VOID
  68. VqTest(
  69. VOID
  70. )
  71. {
  72. PERFINFO PerfInfo;
  73. int i;
  74. PVOID Pv;
  75. DWORD dw;
  76. MEMORY_BASIC_INFORMATION BasicInfo;
  77. //
  78. // Reserve 64k and commit one page
  79. //
  80. Pv = VirtualAlloc(NULL,65536,MEM_RESERVE,PAGE_READWRITE);
  81. if ( !Pv ) {
  82. printf("Virtual Alloc(a) Failed %d\n",GetLastError());
  83. ExitProcess(1);
  84. }
  85. Pv = VirtualAlloc(Pv,4096,MEM_COMMIT,PAGE_READWRITE);
  86. if ( !Pv ) {
  87. printf("Virtual Alloc(b) Failed %d\n",GetLastError());
  88. ExitProcess(1);
  89. }
  90. SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
  91. StartBenchMark(
  92. "Virtual Query Test",
  93. 5*ONEHUNK,
  94. &PerfInfo
  95. );
  96. for ( i=0;i<5*ONEHUNK;i++) {
  97. dw = VirtualQuery(Pv,&BasicInfo,sizeof(BasicInfo));
  98. }
  99. FinishBenchMark(&PerfInfo);
  100. SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
  101. }
  102. VOID
  103. MulDivTest(
  104. VOID
  105. )
  106. {
  107. PERFINFO PerfInfo;
  108. int i;
  109. SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
  110. StartBenchMark(
  111. "MulDiv(4,2,5) Test",
  112. 5*ONEMIL,
  113. &PerfInfo
  114. );
  115. for ( i=0;i<5*ONEMIL;i++) {
  116. MulDiv(4,2,5);
  117. }
  118. FinishBenchMark(&PerfInfo);
  119. StartBenchMark(
  120. "MulDiv(-4,2,5) Test",
  121. 5*ONEMIL,
  122. &PerfInfo
  123. );
  124. for ( i=0;i<5*ONEMIL;i++) {
  125. MulDiv(-4,2,5);
  126. }
  127. FinishBenchMark(&PerfInfo);
  128. StartBenchMark(
  129. "MulDiv(4,-2,5) Test",
  130. 5*ONEMIL,
  131. &PerfInfo
  132. );
  133. for ( i=0;i<5*ONEMIL;i++) {
  134. MulDiv(4,-2,5);
  135. }
  136. FinishBenchMark(&PerfInfo);
  137. StartBenchMark(
  138. "MulDiv(-4,-2,5) Test",
  139. 5*ONEMIL,
  140. &PerfInfo
  141. );
  142. for ( i=0;i<5*ONEMIL;i++) {
  143. MulDiv(-4,-2,5);
  144. }
  145. FinishBenchMark(&PerfInfo);
  146. StartBenchMark(
  147. "MulDiv(0x10,0x400,0) Test",
  148. 5*ONEMIL,
  149. &PerfInfo
  150. );
  151. for ( i=0;i<5*ONEMIL;i++) {
  152. MulDiv(0x10,0x400,0);
  153. }
  154. FinishBenchMark(&PerfInfo);
  155. StartBenchMark(
  156. "MulDiv(0x10,0x40000000,2) Test",
  157. 5*ONEMIL,
  158. &PerfInfo
  159. );
  160. for ( i=0;i<5*ONEMIL;i++) {
  161. MulDiv(0x10,0x40000000,2);
  162. }
  163. FinishBenchMark(&PerfInfo);
  164. SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
  165. }
  166. VOID
  167. GfaTest(
  168. LPSTR Str
  169. )
  170. {
  171. PERFINFO PerfInfo;
  172. int i;
  173. StartBenchMark(
  174. "GetFileAttributes Test",
  175. FIVEK,
  176. &PerfInfo
  177. );
  178. for ( i=0;i<FIVEK;i++) {
  179. GetFileAttributes(Str);
  180. }
  181. FinishBenchMark(&PerfInfo);
  182. }
  183. VOID
  184. GmhTest(
  185. VOID
  186. )
  187. {
  188. PERFINFO PerfInfo;
  189. int i;
  190. StartBenchMark(
  191. "GetModuleHandle Test",
  192. ONEHUNK,
  193. &PerfInfo
  194. );
  195. for ( i=0;i<ONEHUNK;i++) {
  196. GetModuleHandle("kernel32.dll");
  197. }
  198. FinishBenchMark(&PerfInfo);
  199. }
  200. LPVOID Fibers[2];
  201. VOID
  202. FiberRoutine1(
  203. LPVOID lpParameter
  204. )
  205. {
  206. for(;;) {
  207. SwitchToFiber(Fibers[0]);
  208. }
  209. }
  210. VOID
  211. FiberRoutine0(
  212. LPVOID lpParameter
  213. )
  214. {
  215. PERFINFO PerfInfo;
  216. int i;
  217. StartBenchMark(
  218. "Fiber Switch Test",
  219. ONEMIL,
  220. &PerfInfo
  221. );
  222. for ( i=0;i<ONEMIL;i++) {
  223. SwitchToFiber(Fibers[1]);
  224. }
  225. FinishBenchMark(&PerfInfo);
  226. }
  227. VOID
  228. FibTst(
  229. VOID
  230. )
  231. {
  232. Fibers[0] = ConvertThreadToFiber((LPVOID)0);
  233. Fibers[1] = CreateFiber(0,FiberRoutine1,(LPVOID)1);
  234. FiberRoutine0((LPVOID)1);
  235. }
  236. VOID
  237. MemoryTest (
  238. VOID
  239. )
  240. {
  241. PULONG Address;
  242. CHAR Buffer[512];
  243. ULONG Count;
  244. PERFINFO PerfInfo;
  245. //
  246. // Memory to register - aligned.
  247. //
  248. Address = (PULONG)(((ULONG)(&Buffer[128]) + 3) & ~3);
  249. StartBenchMark("Memory To Register Aligned Test",
  250. MEMORY_TEST,
  251. &PerfInfo);
  252. MemToReg(MEMORY_TEST, Address);
  253. FinishBenchMark(&PerfInfo);
  254. //
  255. // Memory to register - unaligned within cache line
  256. //
  257. Address = (PULONG)((((ULONG)(&Buffer[256]) + 127) & ~127) + 1);
  258. StartBenchMark("Memory To Register Unaligned Within Cache Line Test",
  259. MEMORY_TEST,
  260. &PerfInfo);
  261. MemToReg(MEMORY_TEST, Address);
  262. FinishBenchMark(&PerfInfo);
  263. //
  264. // Memory to register - unaligned across cache line
  265. //
  266. Address = (PULONG)((((ULONG)(&Buffer[256]) + 127) & ~127) - 1);
  267. StartBenchMark("Memory To Register Unaligned Across Cache Line Test",
  268. MEMORY_TEST / 2,
  269. &PerfInfo);
  270. MemToReg(MEMORY_TEST, Address);
  271. FinishBenchMark(&PerfInfo);
  272. //
  273. // Register to memory - aligned.
  274. //
  275. Address = (PULONG)(((ULONG)(&Buffer[256]) + 3) & ~3);
  276. StartBenchMark("Register To Memory Aligned Test",
  277. MEMORY_TEST,
  278. &PerfInfo);
  279. RegToMem(MEMORY_TEST, Address);
  280. FinishBenchMark(&PerfInfo);
  281. //
  282. // Register to Memory - unaligned within cache line
  283. //
  284. Address = (PULONG)((((ULONG)(&Buffer[256]) + 127) & ~127) + 1);
  285. StartBenchMark("Register To Memory Unaligned Within Cache Line Test",
  286. MEMORY_TEST,
  287. &PerfInfo);
  288. RegToMem(MEMORY_TEST, Address);
  289. FinishBenchMark(&PerfInfo);
  290. //
  291. // Register to Memory - unaligned across cache line
  292. //
  293. Address = (PULONG)((((ULONG)(&Buffer[256]) + 127) & ~127) - 1);
  294. StartBenchMark("Register To Memory Unaligned Across Cache Line Test",
  295. MEMORY_TEST / 2,
  296. &PerfInfo);
  297. RegToMem(MEMORY_TEST, Address);
  298. FinishBenchMark(&PerfInfo);
  299. return;
  300. }
  301. DWORD
  302. _cdecl
  303. main(
  304. int argc,
  305. char *argv[],
  306. char *envp[]
  307. )
  308. {
  309. // VqTest();
  310. // MulDivTest();
  311. // GmhTest();
  312. // if ( argc > 1 ) {
  313. // GfaTest(argv[1]);
  314. // }
  315. // FibTst();
  316. MemoryTest();
  317. return 0;
  318. }