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