1*b23bdf5aSs.makeev_local // The MIT License (MIT)
2*b23bdf5aSs.makeev_local //
3*b23bdf5aSs.makeev_local // 	Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev
4*b23bdf5aSs.makeev_local //
5*b23bdf5aSs.makeev_local // 	Permission is hereby granted, free of charge, to any person obtaining a copy
6*b23bdf5aSs.makeev_local // 	of this software and associated documentation files (the "Software"), to deal
7*b23bdf5aSs.makeev_local // 	in the Software without restriction, including without limitation the rights
8*b23bdf5aSs.makeev_local // 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9*b23bdf5aSs.makeev_local // 	copies of the Software, and to permit persons to whom the Software is
10*b23bdf5aSs.makeev_local // 	furnished to do so, subject to the following conditions:
11*b23bdf5aSs.makeev_local //
12*b23bdf5aSs.makeev_local //  The above copyright notice and this permission notice shall be included in
13*b23bdf5aSs.makeev_local // 	all copies or substantial portions of the Software.
14*b23bdf5aSs.makeev_local //
15*b23bdf5aSs.makeev_local // 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*b23bdf5aSs.makeev_local // 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*b23bdf5aSs.makeev_local // 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18*b23bdf5aSs.makeev_local // 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*b23bdf5aSs.makeev_local // 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20*b23bdf5aSs.makeev_local // 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21*b23bdf5aSs.makeev_local // 	THE SOFTWARE.
22*b23bdf5aSs.makeev_local 
23*b23bdf5aSs.makeev_local #include "Tests.h"
24*b23bdf5aSs.makeev_local #include <UnitTest++.h>
25*b23bdf5aSs.makeev_local #include <MTScheduler.h>
26*b23bdf5aSs.makeev_local 
27*b23bdf5aSs.makeev_local /*
28*b23bdf5aSs.makeev_local 
29*b23bdf5aSs.makeev_local Check that every worker thread executed tasks in specified priority. HIGH/NORMAL/LOW
30*b23bdf5aSs.makeev_local 
31*b23bdf5aSs.makeev_local */
SUITE(PriorityTests)32*b23bdf5aSs.makeev_local SUITE(PriorityTests)
33*b23bdf5aSs.makeev_local {
34*b23bdf5aSs.makeev_local 	static const uint32 TASK_COUNT = 512;
35*b23bdf5aSs.makeev_local 
36*b23bdf5aSs.makeev_local 	MT::Atomic32<int32> switchCountToNormal;
37*b23bdf5aSs.makeev_local 	MT::Atomic32<int32> switchCountToLow;
38*b23bdf5aSs.makeev_local 
39*b23bdf5aSs.makeev_local 
40*b23bdf5aSs.makeev_local 	struct ThreadState
41*b23bdf5aSs.makeev_local 	{
42*b23bdf5aSs.makeev_local 		uint32 taskPrio;
43*b23bdf5aSs.makeev_local 		uint32 highProcessed;
44*b23bdf5aSs.makeev_local 		uint32 normalProcessed;
45*b23bdf5aSs.makeev_local 		uint32 lowProcessed;
46*b23bdf5aSs.makeev_local 		byte cacheLine[64];
47*b23bdf5aSs.makeev_local 
48*b23bdf5aSs.makeev_local 		ThreadState()
49*b23bdf5aSs.makeev_local 		{
50*b23bdf5aSs.makeev_local 			Reset();
51*b23bdf5aSs.makeev_local 		}
52*b23bdf5aSs.makeev_local 
53*b23bdf5aSs.makeev_local 		void Reset()
54*b23bdf5aSs.makeev_local 		{
55*b23bdf5aSs.makeev_local 			taskPrio = 0;
56*b23bdf5aSs.makeev_local 			highProcessed = 0;
57*b23bdf5aSs.makeev_local 			normalProcessed = 0;
58*b23bdf5aSs.makeev_local 			lowProcessed = 0;
59*b23bdf5aSs.makeev_local 		}
60*b23bdf5aSs.makeev_local 	};
61*b23bdf5aSs.makeev_local 
62*b23bdf5aSs.makeev_local 	ThreadState workerState[64];
63*b23bdf5aSs.makeev_local 
64*b23bdf5aSs.makeev_local 	struct TaskHigh
65*b23bdf5aSs.makeev_local 	{
66*b23bdf5aSs.makeev_local 		MT_DECLARE_TASK(TaskHigh, MT::StackRequirements::STANDARD, MT::TaskPriority::HIGH, MT::Color::Blue);
67*b23bdf5aSs.makeev_local 
68*b23bdf5aSs.makeev_local 		uint32 id;
69*b23bdf5aSs.makeev_local 
70*b23bdf5aSs.makeev_local 		TaskHigh(uint32 _id)
71*b23bdf5aSs.makeev_local 			: id(_id)
72*b23bdf5aSs.makeev_local 		{
73*b23bdf5aSs.makeev_local 		}
74*b23bdf5aSs.makeev_local 
75*b23bdf5aSs.makeev_local 		void Do(MT::FiberContext& ctx)
76*b23bdf5aSs.makeev_local 		{
77*b23bdf5aSs.makeev_local 			uint32 workerIndex = ctx.GetThreadContext()->workerIndex;
78*b23bdf5aSs.makeev_local 			MT_ASSERT(workerIndex < MT_ARRAY_SIZE(workerState), "Invalid worker index");
79*b23bdf5aSs.makeev_local 			ThreadState& state = workerState[workerIndex];
80*b23bdf5aSs.makeev_local 
81*b23bdf5aSs.makeev_local 			CHECK_EQUAL((uint32)0, state.normalProcessed);
82*b23bdf5aSs.makeev_local 			CHECK_EQUAL((uint32)0, state.lowProcessed);
83*b23bdf5aSs.makeev_local 
84*b23bdf5aSs.makeev_local 			state.highProcessed++;
85*b23bdf5aSs.makeev_local 
86*b23bdf5aSs.makeev_local 			// Check we in right state (executing HIGH priority tasks)
87*b23bdf5aSs.makeev_local 			CHECK_EQUAL((uint32)0, state.taskPrio);
88*b23bdf5aSs.makeev_local 
89*b23bdf5aSs.makeev_local 		}
90*b23bdf5aSs.makeev_local 	};
91*b23bdf5aSs.makeev_local 
92*b23bdf5aSs.makeev_local 
93*b23bdf5aSs.makeev_local 	struct TaskNormal
94*b23bdf5aSs.makeev_local 	{
95*b23bdf5aSs.makeev_local 		MT_DECLARE_TASK(TaskNormal, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue);
96*b23bdf5aSs.makeev_local 
97*b23bdf5aSs.makeev_local 		uint32 id;
98*b23bdf5aSs.makeev_local 
99*b23bdf5aSs.makeev_local 		TaskNormal(uint32 _id)
100*b23bdf5aSs.makeev_local 			: id(_id)
101*b23bdf5aSs.makeev_local 		{
102*b23bdf5aSs.makeev_local 		}
103*b23bdf5aSs.makeev_local 
104*b23bdf5aSs.makeev_local 		void Do(MT::FiberContext& ctx)
105*b23bdf5aSs.makeev_local 		{
106*b23bdf5aSs.makeev_local 			uint32 workerIndex = ctx.GetThreadContext()->workerIndex;
107*b23bdf5aSs.makeev_local 			MT_ASSERT(workerIndex < MT_ARRAY_SIZE(workerState), "Invalid worker index");
108*b23bdf5aSs.makeev_local 			ThreadState& state = workerState[workerIndex];
109*b23bdf5aSs.makeev_local 
110*b23bdf5aSs.makeev_local 			CHECK_EQUAL((uint32)0, state.lowProcessed);
111*b23bdf5aSs.makeev_local 
112*b23bdf5aSs.makeev_local 			state.normalProcessed++;
113*b23bdf5aSs.makeev_local 
114*b23bdf5aSs.makeev_local 			//if state is set to HIGH tasks, change state to NORMAL tasks
115*b23bdf5aSs.makeev_local 			if (state.taskPrio == 0)
116*b23bdf5aSs.makeev_local 			{
117*b23bdf5aSs.makeev_local 				state.taskPrio = 1;
118*b23bdf5aSs.makeev_local 				switchCountToNormal.IncFetch();
119*b23bdf5aSs.makeev_local 			}
120*b23bdf5aSs.makeev_local 
121*b23bdf5aSs.makeev_local 			// Check we in right state (executing NORMAL priority tasks)
122*b23bdf5aSs.makeev_local 			CHECK_EQUAL((uint32)1, state.taskPrio);
123*b23bdf5aSs.makeev_local 		}
124*b23bdf5aSs.makeev_local 	};
125*b23bdf5aSs.makeev_local 
126*b23bdf5aSs.makeev_local 	struct TaskLow
127*b23bdf5aSs.makeev_local 	{
128*b23bdf5aSs.makeev_local 		MT_DECLARE_TASK(TaskLow, MT::StackRequirements::STANDARD, MT::TaskPriority::LOW, MT::Color::Blue);
129*b23bdf5aSs.makeev_local 
130*b23bdf5aSs.makeev_local 		uint32 id;
131*b23bdf5aSs.makeev_local 
132*b23bdf5aSs.makeev_local 		TaskLow(uint32 _id)
133*b23bdf5aSs.makeev_local 			: id(_id)
134*b23bdf5aSs.makeev_local 		{
135*b23bdf5aSs.makeev_local 		}
136*b23bdf5aSs.makeev_local 
137*b23bdf5aSs.makeev_local 
138*b23bdf5aSs.makeev_local 		void Do(MT::FiberContext& ctx)
139*b23bdf5aSs.makeev_local 		{
140*b23bdf5aSs.makeev_local 			uint32 workerIndex = ctx.GetThreadContext()->workerIndex;
141*b23bdf5aSs.makeev_local 			MT_ASSERT(workerIndex < MT_ARRAY_SIZE(workerState), "Invalid worker index");
142*b23bdf5aSs.makeev_local 			ThreadState& state = workerState[workerIndex];
143*b23bdf5aSs.makeev_local 
144*b23bdf5aSs.makeev_local 			state.lowProcessed++;
145*b23bdf5aSs.makeev_local 
146*b23bdf5aSs.makeev_local 			//if state is set to NORMAL tasks, change state to LOW tasks
147*b23bdf5aSs.makeev_local 			if (state.taskPrio == 1)
148*b23bdf5aSs.makeev_local 			{
149*b23bdf5aSs.makeev_local 				state.taskPrio = 2;
150*b23bdf5aSs.makeev_local 				switchCountToLow.IncFetch();
151*b23bdf5aSs.makeev_local 			}
152*b23bdf5aSs.makeev_local 
153*b23bdf5aSs.makeev_local 			// Check we in right state (executing LOW priority tasks)
154*b23bdf5aSs.makeev_local 			CHECK_EQUAL((uint32)2, state.taskPrio);
155*b23bdf5aSs.makeev_local 		}
156*b23bdf5aSs.makeev_local 	};
157*b23bdf5aSs.makeev_local 
158*b23bdf5aSs.makeev_local 
159*b23bdf5aSs.makeev_local 
160*b23bdf5aSs.makeev_local 
161*b23bdf5aSs.makeev_local 	TEST(SimplePriorityTest)
162*b23bdf5aSs.makeev_local 	{
163*b23bdf5aSs.makeev_local 		MT::TaskPool<TaskLow, TASK_COUNT> lowPriorityTasksPool;
164*b23bdf5aSs.makeev_local 		MT::TaskPool<TaskNormal, TASK_COUNT> normalPriorityTasksPool;
165*b23bdf5aSs.makeev_local 		MT::TaskPool<TaskHigh, TASK_COUNT> highPriorityTasksPool;
166*b23bdf5aSs.makeev_local 
167*b23bdf5aSs.makeev_local 		// Disable task stealing (for testing purposes only)
168*b23bdf5aSs.makeev_local #ifdef MT_INSTRUMENTED_BUILD
169*b23bdf5aSs.makeev_local 		MT::TaskScheduler scheduler(0, nullptr, nullptr, MT::TaskStealingMode::DISABLED);
170*b23bdf5aSs.makeev_local #else
171*b23bdf5aSs.makeev_local 		MT::TaskScheduler scheduler(0, nullptr, MT::TaskStealingMode::DISABLED);
172*b23bdf5aSs.makeev_local #endif
173*b23bdf5aSs.makeev_local 
174*b23bdf5aSs.makeev_local 		// Use task handles to add multiple tasks with different priorities in one RunAsync call
175*b23bdf5aSs.makeev_local 		MT::TaskHandle taskHandles[TASK_COUNT*3];
176*b23bdf5aSs.makeev_local 
177*b23bdf5aSs.makeev_local 		uint32 index = 0;
178*b23bdf5aSs.makeev_local 		for(uint32 i = 0; i < TASK_COUNT; i++)
179*b23bdf5aSs.makeev_local 		{
180*b23bdf5aSs.makeev_local 			taskHandles[index] = lowPriorityTasksPool.Alloc(TaskLow(i));
181*b23bdf5aSs.makeev_local 			index++;
182*b23bdf5aSs.makeev_local 		}
183*b23bdf5aSs.makeev_local 
184*b23bdf5aSs.makeev_local 		for(uint32 i = 0; i < TASK_COUNT; i++)
185*b23bdf5aSs.makeev_local 		{
186*b23bdf5aSs.makeev_local 			taskHandles[index] = highPriorityTasksPool.Alloc(TaskHigh(i));
187*b23bdf5aSs.makeev_local 			index++;
188*b23bdf5aSs.makeev_local 		}
189*b23bdf5aSs.makeev_local 
190*b23bdf5aSs.makeev_local 		for(uint32 i = 0; i < TASK_COUNT; i++)
191*b23bdf5aSs.makeev_local 		{
192*b23bdf5aSs.makeev_local 			taskHandles[index] = normalPriorityTasksPool.Alloc(TaskNormal(i));
193*b23bdf5aSs.makeev_local 			index++;
194*b23bdf5aSs.makeev_local 		}
195*b23bdf5aSs.makeev_local 
196*b23bdf5aSs.makeev_local 		switchCountToNormal.Store(0);
197*b23bdf5aSs.makeev_local 		switchCountToLow.Store(0);
198*b23bdf5aSs.makeev_local 
199*b23bdf5aSs.makeev_local 		for(uint32 i = 0; i < MT_ARRAY_SIZE(workerState); i++)
200*b23bdf5aSs.makeev_local 		{
201*b23bdf5aSs.makeev_local 			workerState[i].Reset();
202*b23bdf5aSs.makeev_local 		}
203*b23bdf5aSs.makeev_local 
204*b23bdf5aSs.makeev_local 		scheduler.RunAsync(MT::TaskGroup::Default(), &taskHandles[0], MT_ARRAY_SIZE(taskHandles));
205*b23bdf5aSs.makeev_local 		CHECK(scheduler.WaitAll(2000));
206*b23bdf5aSs.makeev_local 
207*b23bdf5aSs.makeev_local 		int32 workersCount = scheduler.GetWorkersCount();
208*b23bdf5aSs.makeev_local 		float minTasksExecuted  = (float)TASK_COUNT / (float)workersCount;
209*b23bdf5aSs.makeev_local 		minTasksExecuted *= 0.95f;
210*b23bdf5aSs.makeev_local 		uint32 minTasksExecutedThreshold = (uint32)minTasksExecuted;
211*b23bdf5aSs.makeev_local 
212*b23bdf5aSs.makeev_local 		uint32 lowProcessedTotal = 0;
213*b23bdf5aSs.makeev_local 		uint32 normalProcessedTotal = 0;
214*b23bdf5aSs.makeev_local 		uint32 highProcessedTotal = 0;
215*b23bdf5aSs.makeev_local 
216*b23bdf5aSs.makeev_local 		for(int32 j = 0; j < workersCount; j++)
217*b23bdf5aSs.makeev_local 		{
218*b23bdf5aSs.makeev_local 			lowProcessedTotal += workerState[j].lowProcessed;
219*b23bdf5aSs.makeev_local 			normalProcessedTotal += workerState[j].normalProcessed;
220*b23bdf5aSs.makeev_local 			highProcessedTotal += workerState[j].highProcessed;
221*b23bdf5aSs.makeev_local 		}
222*b23bdf5aSs.makeev_local 
223*b23bdf5aSs.makeev_local 		CHECK_EQUAL(TASK_COUNT, lowProcessedTotal);
224*b23bdf5aSs.makeev_local 		CHECK_EQUAL(TASK_COUNT, normalProcessedTotal);
225*b23bdf5aSs.makeev_local 		CHECK_EQUAL(TASK_COUNT, highProcessedTotal);
226*b23bdf5aSs.makeev_local 
227*b23bdf5aSs.makeev_local 		for(int32 j = 0; j < workersCount; j++)
228*b23bdf5aSs.makeev_local 		{
229*b23bdf5aSs.makeev_local 			printf("worker #%d\n", j);
230*b23bdf5aSs.makeev_local 
231*b23bdf5aSs.makeev_local 			CHECK_EQUAL((uint32)2, workerState[j].taskPrio);
232*b23bdf5aSs.makeev_local 			CHECK(workerState[j].lowProcessed >= minTasksExecutedThreshold);
233*b23bdf5aSs.makeev_local 			CHECK(workerState[j].normalProcessed >= minTasksExecutedThreshold);
234*b23bdf5aSs.makeev_local 			CHECK(workerState[j].highProcessed >= minTasksExecutedThreshold);
235*b23bdf5aSs.makeev_local 
236*b23bdf5aSs.makeev_local 			printf("   low : %d\n", workerState[j].lowProcessed);
237*b23bdf5aSs.makeev_local 			printf("   normal : %d\n", workerState[j].normalProcessed);
238*b23bdf5aSs.makeev_local 			printf("   high : %d\n", workerState[j].highProcessed);
239*b23bdf5aSs.makeev_local 		}
240*b23bdf5aSs.makeev_local 
241*b23bdf5aSs.makeev_local 		//
242*b23bdf5aSs.makeev_local 		// Every worker thread can't change state more than once.
243*b23bdf5aSs.makeev_local 		//
244*b23bdf5aSs.makeev_local 		CHECK_EQUAL(workersCount, switchCountToNormal.Load());
245*b23bdf5aSs.makeev_local 		CHECK_EQUAL(workersCount, switchCountToLow.Load());
246*b23bdf5aSs.makeev_local 
247*b23bdf5aSs.makeev_local 
248*b23bdf5aSs.makeev_local 	}
249*b23bdf5aSs.makeev_local ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
250*b23bdf5aSs.makeev_local }
251*b23bdf5aSs.makeev_local 
252