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.

236 lines
3.9 KiB

  1. // TestTrust.cpp : Defines the entry point for the console application.
  2. //
  3. #include <windows.h>
  4. #include <tchar.h>
  5. #include <logging.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. //#include <malloc.h> // use _alloc
  9. const TCHAR TestQuitEvent[] = _T("B4715050-ED35-4a7c-894A-1DF04F4F7F27");
  10. LOG_Process;
  11. HANDLE g_QuitEvent = NULL;
  12. long g_Cnt = 0;
  13. void Randomize()
  14. {
  15. srand(GetTickCount());
  16. }
  17. int RandomNum(int _max)
  18. {
  19. int n = rand();
  20. float f = (float)n / RAND_MAX;
  21. n = (int) (f * _max);
  22. return n;
  23. }
  24. inline bool ShouldQuit()
  25. {
  26. return (WaitForSingleObject(g_QuitEvent, 0) == WAIT_OBJECT_0);
  27. }
  28. void RandomGenLogs(int _max)
  29. {
  30. LOG_Block("WriteLog");
  31. Randomize();
  32. for (int i = 0; i < RandomNum(_max); i++)
  33. {
  34. int n = RandomNum(10);
  35. if (n >= 5)
  36. {
  37. if (RandomNum(2) > 1)
  38. {
  39. LOG_Error(_T("Error: %d"), RandomNum(100));
  40. }
  41. else
  42. {
  43. LOG_ErrorMsg(RandomNum(128));
  44. }
  45. }
  46. else
  47. {
  48. switch (n)
  49. {
  50. case 0:
  51. LOG_XML(_T("XML Error"));
  52. break;
  53. case 1:
  54. LOG_Driver(_T("Driver Log sample"));
  55. break;
  56. case 2:
  57. LOG_Internet(_T("Internet related log"));
  58. break;
  59. case 3:
  60. LOG_Software(_T("Software related log"));
  61. break;
  62. case 4:
  63. LOG_Trust(_T("Trust related log"));
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. DWORD TestLog(int nDepth)
  70. {
  71. int i;
  72. char szTitle[16];
  73. wsprintfA(szTitle, "TestLog(%d)", nDepth);
  74. LOG_Block(szTitle);
  75. Randomize();
  76. if (ShouldQuit())
  77. return 0;
  78. if (nDepth <= 0)
  79. {
  80. RandomGenLogs(10);
  81. return 0;
  82. }
  83. RandomGenLogs(4);
  84. for (i = 0; i < RandomNum(2); i++)
  85. {
  86. if (ShouldQuit())
  87. {
  88. LOG_Out(_T("Got quit signal!"));
  89. return 0;
  90. }
  91. TestLog(nDepth - 1);
  92. }
  93. RandomGenLogs(6);
  94. return 0;
  95. }
  96. DWORD WINAPI ThreadProc(LPVOID Param)
  97. {
  98. LOG_Block("ThreadProc");
  99. InterlockedIncrement(&g_Cnt);
  100. int n = (int) Param;
  101. printf("\t\tThread %d starts with depth %d\n", GetCurrentThreadId(), n);
  102. while (!ShouldQuit())
  103. TestLog(n);
  104. LOG_Out(_T("ThreadProc:::::::::Got quit signal!"));
  105. printf("\t\tThread %d quits\n", GetCurrentThreadId());
  106. InterlockedDecrement(&g_Cnt);
  107. return 0;
  108. }
  109. void StartThreadTesting(int nTotalThreads)
  110. {
  111. LOG_Block("StartThreadTesting()");
  112. DWORD dwThreadId;
  113. int Num, i;
  114. HANDLE* pHandles = (HANDLE*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nTotalThreads * sizeof(HANDLE));
  115. Randomize();
  116. for (i = 0; i < nTotalThreads; i++)
  117. {
  118. Num = RandomNum(6);
  119. //LOG_Out(_T("Generate thread #%d with depth %d"), i, Num);
  120. printf("\tGenerate thread #%d with depth %d\n", i, Num);
  121. if (pHandles != NULL)
  122. pHandles[i] = CreateThread(NULL, 0, ThreadProc, (LPVOID) Num, CREATE_SUSPENDED, &dwThreadId);
  123. else
  124. CreateThread(NULL, 0, ThreadProc, (LPVOID) Num, 0, &dwThreadId);
  125. }
  126. if (pHandles != NULL)
  127. {
  128. //
  129. // start all threads
  130. //
  131. for (i = 0; i < nTotalThreads; i++)
  132. {
  133. ResumeThread(pHandles[i]);
  134. }
  135. HeapFree(GetProcessHeap(), 0, (LPVOID)pHandles);
  136. }
  137. }
  138. int main(int argc, char* argv[])
  139. {
  140. LOG_Block("main");
  141. int nWaitSeconds = 5 * 1000;;
  142. int nThreads= 20;
  143. if (argc > 1)
  144. {
  145. nWaitSeconds = abs(atoi(argv[1])) * 1000;
  146. }
  147. LOG_Out(_T("Found timing %d seconds"), nWaitSeconds/1000);
  148. if (argc > 2)
  149. {
  150. nThreads = abs(atoi(argv[2]));
  151. }
  152. LOG_Out(_T("Found number of threads: %d"), nThreads);
  153. g_QuitEvent = CreateEvent(NULL, TRUE, FALSE, TestQuitEvent);
  154. printf("Start threading ....\n");
  155. StartThreadTesting(nThreads);
  156. printf("Finished threading\n");
  157. int iStart = (int)GetTickCount();
  158. int nNow = (int)GetTickCount();
  159. while (nNow < iStart + nWaitSeconds)
  160. {
  161. printf("Wait for %d more seconds...\n", (nWaitSeconds - (nNow - iStart))/1000);
  162. Sleep(2000);
  163. nNow = (int)GetTickCount();
  164. }
  165. //
  166. // tell that we need to quit
  167. //
  168. SetEvent(g_QuitEvent);
  169. //
  170. // wait for threads to quit
  171. //
  172. int nCnt = g_Cnt;
  173. iStart = (int)GetTickCount();
  174. while (nCnt > 0)
  175. {
  176. Sleep(1000);
  177. printf("Seconds: %d, Threads: %d\n", ((int)GetTickCount() - iStart)/1000, nCnt);
  178. nCnt = g_Cnt;
  179. }
  180. //
  181. // quit
  182. //
  183. return 0;
  184. }