1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11 /**************************************************************************/
12 /**************************************************************************/
13 /** */
14 /** Thread-Metric Component */
15 /** */
16 /** Cooperative Scheduling Test */
17 /** */
18 /**************************************************************************/
19 /**************************************************************************/
20
21 /**************************************************************************/
22 /* */
23 /* FUNCTION RELEASE */
24 /* */
25 /* tm_cooperative_scheduling_test PORTABLE C */
26 /* 6.1.7 */
27 /* AUTHOR */
28 /* */
29 /* William E. Lamie, Microsoft Corporation */
30 /* */
31 /* DESCRIPTION */
32 /* */
33 /* This file defines the cooperative scheduling test. */
34 /* */
35 /* RELEASE HISTORY */
36 /* */
37 /* DATE NAME DESCRIPTION */
38 /* */
39 /* 10-15-2021 William E. Lamie Initial Version 6.1.7 */
40 /* */
41 /**************************************************************************/
42 #include "tm_api.h"
43
44
45 /* Define the counters used in the demo application... */
46
47 unsigned long tm_cooperative_thread_0_counter;
48 unsigned long tm_cooperative_thread_1_counter;
49 unsigned long tm_cooperative_thread_2_counter;
50 unsigned long tm_cooperative_thread_3_counter;
51 unsigned long tm_cooperative_thread_4_counter;
52
53
54 /* Define the test thread prototypes. */
55
56 void tm_cooperative_thread_0_entry(void);
57 void tm_cooperative_thread_1_entry(void);
58 void tm_cooperative_thread_2_entry(void);
59 void tm_cooperative_thread_3_entry(void);
60 void tm_cooperative_thread_4_entry(void);
61
62
63 /* Define the reporting thread prototype. */
64
65 void tm_cooperative_thread_report(void);
66
67
68 /* Define the initialization prototype. */
69
70 void tm_cooperative_scheduling_initialize(void);
71
72
73 /* Define main entry point. */
74
tm_main()75 void tm_main()
76 {
77
78 /* Initialize the test. */
79 tm_initialize(tm_cooperative_scheduling_initialize);
80 }
81
82
83 /* Define the cooperative scheduling test initialization. */
84
tm_cooperative_scheduling_initialize(void)85 void tm_cooperative_scheduling_initialize(void)
86 {
87
88 /* Create all 5 threads at priority 3. */
89 tm_thread_create(0, 3, tm_cooperative_thread_0_entry);
90 tm_thread_create(1, 3, tm_cooperative_thread_1_entry);
91 tm_thread_create(2, 3, tm_cooperative_thread_2_entry);
92 tm_thread_create(3, 3, tm_cooperative_thread_3_entry);
93 tm_thread_create(4, 3, tm_cooperative_thread_4_entry);
94
95 /* Resume all 5 threads. */
96 tm_thread_resume(0);
97 tm_thread_resume(1);
98 tm_thread_resume(2);
99 tm_thread_resume(3);
100 tm_thread_resume(4);
101
102 /* Create the reporting thread. It will preempt the other
103 threads and print out the test results. */
104 tm_thread_create(5, 2, tm_cooperative_thread_report);
105 tm_thread_resume(5);
106 }
107
108
109 /* Define the first cooperative thread. */
tm_cooperative_thread_0_entry(void)110 void tm_cooperative_thread_0_entry(void)
111 {
112
113 while(1)
114 {
115
116 /* Relinquish to all other threads at same priority. */
117 tm_thread_relinquish();
118
119 /* Increment this thread's counter. */
120 tm_cooperative_thread_0_counter++;
121 }
122 }
123
124 /* Define the second cooperative thread. */
tm_cooperative_thread_1_entry(void)125 void tm_cooperative_thread_1_entry(void)
126 {
127
128 while(1)
129 {
130
131 /* Relinquish to all other threads at same priority. */
132 tm_thread_relinquish();
133
134 /* Increment this thread's counter. */
135 tm_cooperative_thread_1_counter++;
136 }
137 }
138
139 /* Define the third cooperative thread. */
tm_cooperative_thread_2_entry(void)140 void tm_cooperative_thread_2_entry(void)
141 {
142
143 while(1)
144 {
145
146 /* Relinquish to all other threads at same priority. */
147 tm_thread_relinquish();
148
149 /* Increment this thread's counter. */
150 tm_cooperative_thread_2_counter++;
151 }
152 }
153
154
155 /* Define the fourth cooperative thread. */
tm_cooperative_thread_3_entry(void)156 void tm_cooperative_thread_3_entry(void)
157 {
158
159 while(1)
160 {
161
162 /* Relinquish to all other threads at same priority. */
163 tm_thread_relinquish();
164
165 /* Increment this thread's counter. */
166 tm_cooperative_thread_3_counter++;
167 }
168 }
169
170
171 /* Define the fifth cooperative thread. */
tm_cooperative_thread_4_entry(void)172 void tm_cooperative_thread_4_entry(void)
173 {
174
175 while(1)
176 {
177
178 /* Relinquish to all other threads at same priority. */
179 tm_thread_relinquish();
180
181 /* Increment this thread's counter. */
182 tm_cooperative_thread_4_counter++;
183 }
184 }
185
186
187 /* Define the cooperative test reporting thread. */
tm_cooperative_thread_report(void)188 void tm_cooperative_thread_report(void)
189 {
190
191 unsigned long total;
192 unsigned long relative_time;
193 unsigned long last_total;
194 unsigned long average;
195
196 /* Initialize the last total. */
197 last_total = 0;
198
199 /* Initialize the relative time. */
200 relative_time = 0;
201
202 while(1)
203 {
204
205 /* Sleep to allow the test to run. */
206 tm_thread_sleep(TM_TEST_DURATION);
207
208 /* Increment the relative time. */
209 relative_time = relative_time + TM_TEST_DURATION;
210
211 /* Print results to the stdio window. */
212 printf("**** Thread-Metric Cooperative Scheduling Test **** Relative Time: %lu\n", relative_time);
213
214 /* Calculate the total of all the counters. */
215 total = tm_cooperative_thread_0_counter + tm_cooperative_thread_1_counter + tm_cooperative_thread_2_counter
216 + tm_cooperative_thread_3_counter + tm_cooperative_thread_4_counter;
217
218 /* Calculate the average of all the counters. */
219 average = total/5;
220
221 /* WCC - integrity check */
222 printf("tm_cooperative_thread_0_counter: %d\n", tm_cooperative_thread_0_counter);
223 printf("tm_cooperative_thread_1_counter: %d\n", tm_cooperative_thread_1_counter);
224 printf("tm_cooperative_thread_2_counter: %d\n", tm_cooperative_thread_2_counter);
225 printf("tm_cooperative_thread_3_counter: %d\n", tm_cooperative_thread_3_counter);
226 printf("tm_cooperative_thread_4_counter: %d\n", tm_cooperative_thread_4_counter);
227
228 /* See if there are any errors. */
229 if ((tm_cooperative_thread_0_counter < (average - 1)) ||
230 (tm_cooperative_thread_0_counter > (average + 1)) ||
231 (tm_cooperative_thread_1_counter < (average - 1)) ||
232 (tm_cooperative_thread_1_counter > (average + 1)) ||
233 (tm_cooperative_thread_2_counter < (average - 1)) ||
234 (tm_cooperative_thread_2_counter > (average + 1)) ||
235 (tm_cooperative_thread_3_counter < (average - 1)) ||
236 (tm_cooperative_thread_3_counter > (average + 1)) ||
237 (tm_cooperative_thread_4_counter < (average - 1)) ||
238 (tm_cooperative_thread_4_counter > (average + 1)))
239 {
240
241 printf("ERROR: Invalid counter value(s). Cooperative counters should not be more that 1 different than the average!\n");
242 }
243
244 /* Show the time period total. */
245 printf("Time Period Total: %lu\n\n", total - last_total);
246
247 /* Save the last total. */
248 last_total = total;
249 }
250 }
251