twork.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. twork.c
  9. Abstract:
  10. This module implements the kernel work item 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_WORK_DEFAULT_ITERATIONS 500000
  26. #define KTEST_WORK_DEFAULT_THREAD_COUNT 20
  27. #define KTEST_WORK_DEFAULT_ALLOCATION_SIZE 512
  28. //
  29. // ------------------------------------------------------ Data Type Definitions
  30. //
  31. typedef struct _KTEST_WORK_ITEM_CONTEXT {
  32. PKTEST_PARAMETERS Parameters;
  33. PKEVENT Event;
  34. } KTEST_WORK_ITEM_CONTEXT, *PKTEST_WORK_ITEM_CONTEXT;
  35. //
  36. // ----------------------------------------------- Internal Function Prototypes
  37. //
  38. VOID
  39. KTestWorkStressRoutine (
  40. PVOID Parameter
  41. );
  42. VOID
  43. KTestWorkStressWorkRoutine (
  44. PVOID Parameter
  45. );
  46. //
  47. // -------------------------------------------------------------------- Globals
  48. //
  49. //
  50. // ------------------------------------------------------------------ Functions
  51. //
  52. KSTATUS
  53. KTestWorkStressStart (
  54. PKTEST_START_TEST Command,
  55. PKTEST_ACTIVE_TEST Test
  56. )
  57. /*++
  58. Routine Description:
  59. This routine starts a new invocation of the work item stress test.
  60. Arguments:
  61. Command - Supplies a pointer to the start command.
  62. Test - Supplies a pointer to the active test structure to initialize.
  63. Return Value:
  64. Status code.
  65. --*/
  66. {
  67. PKTEST_PARAMETERS Parameters;
  68. KSTATUS Status;
  69. ULONG ThreadIndex;
  70. Parameters = &(Test->Parameters);
  71. RtlCopyMemory(Parameters, &(Command->Parameters), sizeof(KTEST_PARAMETERS));
  72. if (Parameters->Iterations == 0) {
  73. Parameters->Iterations = KTEST_WORK_DEFAULT_ITERATIONS;
  74. }
  75. if (Parameters->Threads == 0) {
  76. Parameters->Threads = KTEST_WORK_DEFAULT_THREAD_COUNT;
  77. }
  78. if (Parameters->Parameters[0] == 0) {
  79. Parameters->Parameters[0] = KTEST_WORK_DEFAULT_ALLOCATION_SIZE;
  80. }
  81. Test->Total = Test->Parameters.Iterations;
  82. Test->Results.Status = STATUS_SUCCESS;
  83. Test->Results.Failures = 0;
  84. for (ThreadIndex = 0;
  85. ThreadIndex < Test->Parameters.Threads;
  86. ThreadIndex += 1) {
  87. Status = PsCreateKernelThread(KTestWorkStressRoutine,
  88. Test,
  89. "KTestWorkStressRoutine");
  90. if (!KSUCCESS(Status)) {
  91. goto WorkStressStartEnd;
  92. }
  93. }
  94. Status = STATUS_SUCCESS;
  95. WorkStressStartEnd:
  96. return Status;
  97. }
  98. //
  99. // --------------------------------------------------------- Internal Functions
  100. //
  101. VOID
  102. KTestWorkStressRoutine (
  103. PVOID Parameter
  104. )
  105. /*++
  106. Routine Description:
  107. This routine implements the work item stress test.
  108. Arguments:
  109. Parameter - Supplies a pointer to the thread parameter, which in this
  110. case is a pointer to the active test structure.
  111. Return Value:
  112. None.
  113. --*/
  114. {
  115. ULONG Failures;
  116. PKTEST_ACTIVE_TEST Information;
  117. ULONG Iteration;
  118. PKTEST_PARAMETERS Parameters;
  119. KSTATUS Status;
  120. ULONG ThreadNumber;
  121. KTEST_WORK_ITEM_CONTEXT WorkContext;
  122. Failures = 0;
  123. Information = Parameter;
  124. Parameters = &(Information->Parameters);
  125. RtlZeroMemory(&WorkContext, sizeof(KTEST_WORK_ITEM_CONTEXT));
  126. WorkContext.Parameters = Parameters;
  127. WorkContext.Event = KeCreateEvent(NULL);
  128. if (WorkContext.Event == NULL) {
  129. Failures += 1;
  130. Status = STATUS_INSUFFICIENT_RESOURCES;
  131. goto TestWorkStressRoutineEnd;
  132. }
  133. ThreadNumber = RtlAtomicAdd32(&(Information->ThreadsStarted), 1);
  134. for (Iteration = 0; Iteration < Parameters->Iterations; Iteration += 1) {
  135. if (Information->Cancel != FALSE) {
  136. Status = STATUS_SUCCESS;
  137. goto TestWorkStressRoutineEnd;
  138. }
  139. KeSignalEvent(WorkContext.Event, SignalOptionUnsignal);
  140. Status = KeCreateAndQueueWorkItem(NULL,
  141. WorkPriorityNormal,
  142. KTestWorkStressWorkRoutine,
  143. &WorkContext);
  144. if (!KSUCCESS(Status)) {
  145. Failures += 1;
  146. goto TestWorkStressRoutineEnd;
  147. }
  148. Status = KeWaitForEvent(WorkContext.Event,
  149. FALSE,
  150. WAIT_TIME_INDEFINITE);
  151. if (!KSUCCESS(Status)) {
  152. goto TestWorkStressRoutineEnd;
  153. }
  154. if (ThreadNumber == 0) {
  155. Information->Progress += 1;
  156. }
  157. }
  158. Status = STATUS_SUCCESS;
  159. TestWorkStressRoutineEnd:
  160. KeDestroyEvent(WorkContext.Event);
  161. WorkContext.Event = NULL;
  162. //
  163. // Save the results.
  164. //
  165. if (!KSUCCESS(Status)) {
  166. Information->Results.Status = Status;
  167. }
  168. Information->Results.Failures += Failures;
  169. RtlAtomicAdd32(&(Information->ThreadsFinished), 1);
  170. return;
  171. }
  172. VOID
  173. KTestWorkStressWorkRoutine (
  174. PVOID Parameter
  175. )
  176. /*++
  177. Routine Description:
  178. This routine implements the work item routine for the work item stress
  179. test.
  180. Arguments:
  181. Parameter - Supplies a pointer to the parameter, which in this
  182. case is a pointer to the work item context.
  183. Return Value:
  184. None.
  185. --*/
  186. {
  187. PUCHAR Allocation;
  188. ULONG AllocationSize;
  189. ULONG ByteIndex;
  190. PKTEST_WORK_ITEM_CONTEXT WorkContext;
  191. WorkContext = Parameter;
  192. //
  193. // Allocate and scribble on some memory to make it seem like some work is
  194. // being done.
  195. //
  196. AllocationSize = (KTestGetRandomValue() %
  197. WorkContext->Parameters->Parameters[0]) + 1;
  198. Allocation = MmAllocatePagedPool(AllocationSize, KTEST_ALLOCATION_TAG);
  199. if (Allocation == NULL) {
  200. goto TestWorkStressWorkRoutineEnd;
  201. }
  202. for (ByteIndex = 0; ByteIndex < AllocationSize; ByteIndex += 1) {
  203. Allocation[ByteIndex] = 0xB0 + ByteIndex;
  204. }
  205. TestWorkStressWorkRoutineEnd:
  206. if (Allocation != NULL) {
  207. MmFreePagedPool(Allocation);
  208. }
  209. KeSignalEvent(WorkContext->Event, SignalOptionSignalAll);
  210. return;
  211. }