tthread.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. tthread.c
  9. Abstract:
  10. This module implements the kernel thread test.
  11. Author:
  12. Evan Green 11-Nov-2013
  13. Environment:
  14. Kernel
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <minoca/kernel/driver.h>
  20. #include "ktestdrv.h"
  21. #include "testsup.h"
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. #define KTEST_THREAD_DEFAULT_ITERATIONS 30000
  26. #define KTEST_THREAD_DEFAULT_THREAD_COUNT 20
  27. //
  28. // ------------------------------------------------------ Data Type Definitions
  29. //
  30. //
  31. // ----------------------------------------------- Internal Function Prototypes
  32. //
  33. VOID
  34. KTestThreadStressRoutine (
  35. PVOID Parameter
  36. );
  37. VOID
  38. KTestThreadStressThread (
  39. PVOID Parameter
  40. );
  41. //
  42. // -------------------------------------------------------------------- Globals
  43. //
  44. //
  45. // ------------------------------------------------------------------ Functions
  46. //
  47. KSTATUS
  48. KTestThreadStressStart (
  49. PKTEST_START_TEST Command,
  50. PKTEST_ACTIVE_TEST Test
  51. )
  52. /*++
  53. Routine Description:
  54. This routine starts a new invocation of the thread stress test.
  55. Arguments:
  56. Command - Supplies a pointer to the start command.
  57. Test - Supplies a pointer to the active test structure to initialize.
  58. Return Value:
  59. Status code.
  60. --*/
  61. {
  62. PKTEST_PARAMETERS Parameters;
  63. KSTATUS Status;
  64. ULONG ThreadIndex;
  65. Parameters = &(Test->Parameters);
  66. RtlCopyMemory(Parameters, &(Command->Parameters), sizeof(KTEST_PARAMETERS));
  67. if (Parameters->Iterations == 0) {
  68. Parameters->Iterations = KTEST_THREAD_DEFAULT_ITERATIONS;
  69. }
  70. if (Parameters->Threads == 0) {
  71. Parameters->Threads = KTEST_THREAD_DEFAULT_THREAD_COUNT;
  72. }
  73. Test->Total = Test->Parameters.Iterations;
  74. Test->Results.Status = STATUS_SUCCESS;
  75. Test->Results.Failures = 0;
  76. for (ThreadIndex = 0;
  77. ThreadIndex < Test->Parameters.Threads;
  78. ThreadIndex += 1) {
  79. Status = PsCreateKernelThread(KTestThreadStressRoutine,
  80. Test,
  81. "KTestThreadStressRoutine");
  82. if (!KSUCCESS(Status)) {
  83. goto ThreadStressStartEnd;
  84. }
  85. }
  86. Status = STATUS_SUCCESS;
  87. ThreadStressStartEnd:
  88. return Status;
  89. }
  90. //
  91. // --------------------------------------------------------- Internal Functions
  92. //
  93. VOID
  94. KTestThreadStressRoutine (
  95. PVOID Parameter
  96. )
  97. /*++
  98. Routine Description:
  99. This routine implements the thread stress test.
  100. Arguments:
  101. Parameter - Supplies a pointer to the thread parameter, which in this
  102. case is a pointer to the active test structure.
  103. Return Value:
  104. None.
  105. --*/
  106. {
  107. PKEVENT Event;
  108. ULONG Failures;
  109. PKTEST_ACTIVE_TEST Information;
  110. ULONG Iteration;
  111. PKTEST_PARAMETERS Parameters;
  112. KSTATUS Status;
  113. ULONG ThreadNumber;
  114. Failures = 0;
  115. Information = Parameter;
  116. Parameters = &(Information->Parameters);
  117. ThreadNumber = RtlAtomicAdd32(&(Information->ThreadsStarted), 1);
  118. Event = KeCreateEvent(NULL);
  119. if (Event == NULL) {
  120. Status = STATUS_INSUFFICIENT_RESOURCES;
  121. goto TestWorkStressRoutineEnd;
  122. }
  123. for (Iteration = 0; Iteration < Parameters->Iterations; Iteration += 1) {
  124. if (Information->Cancel != FALSE) {
  125. Status = STATUS_SUCCESS;
  126. goto TestWorkStressRoutineEnd;
  127. }
  128. KeSignalEvent(Event, SignalOptionUnsignal);
  129. Status = PsCreateKernelThread(KTestThreadStressThread,
  130. Event,
  131. "KTestThreadStressThread");
  132. if (!KSUCCESS(Status)) {
  133. Failures += 1;
  134. } else {
  135. Status = KeWaitForEvent(Event, FALSE, WAIT_TIME_INDEFINITE);
  136. if (!KSUCCESS(Status)) {
  137. Failures += 1;
  138. }
  139. }
  140. if (ThreadNumber == 0) {
  141. Information->Progress += 1;
  142. }
  143. }
  144. Status = STATUS_SUCCESS;
  145. TestWorkStressRoutineEnd:
  146. if (Event != FALSE) {
  147. KeDestroyEvent(Event);
  148. }
  149. //
  150. // Save the results.
  151. //
  152. if (!KSUCCESS(Status)) {
  153. Information->Results.Status = Status;
  154. }
  155. Information->Results.Failures += Failures;
  156. RtlAtomicAdd32(&(Information->ThreadsFinished), 1);
  157. return;
  158. }
  159. VOID
  160. KTestThreadStressThread (
  161. PVOID Parameter
  162. )
  163. /*++
  164. Routine Description:
  165. This routine implements the thread test thread, which does nothing but exit.
  166. Arguments:
  167. Parameter - Supplies a pointer to the parameter, which in this
  168. case is a pointer to the work item context.
  169. Return Value:
  170. None.
  171. --*/
  172. {
  173. KeSignalEvent(Parameter, SignalOptionSignalAll);
  174. return;
  175. }