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 /**   Interrupt Preemption Processing Test                                */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /**************************************************************************/
24 /*                                                                        */
25 /*  FUNCTION                                               RELEASE        */
26 /*                                                                        */
27 /*    tm_interrupt_preemption_processing_test             PORTABLE C      */
28 /*                                                           6.1.7        */
29 /*  AUTHOR                                                                */
30 /*                                                                        */
31 /*    William E. Lamie, Microsoft Corporation                             */
32 /*                                                                        */
33 /*  DESCRIPTION                                                           */
34 /*                                                                        */
35 /*    This file defines the preemptive scheduling test.                   */
36 /*                                                                        */
37 /*  RELEASE HISTORY                                                       */
38 /*                                                                        */
39 /*    DATE              NAME                      DESCRIPTION             */
40 /*                                                                        */
41 /*  10-15-2021     William E. Lamie         Initial Version 6.1.7         */
42 /*                                                                        */
43 /**************************************************************************/
44 
45 #include "tm_api.h"
46 
47 
48 /* Define the counters used in the demo application...  */
49 
50 unsigned long   tm_interrupt_preemption_thread_0_counter;
51 unsigned long   tm_interrupt_preemption_thread_1_counter;
52 unsigned long   tm_interrupt_preemption_handler_counter;
53 
54 
55 /* Define the test thread prototypes.  */
56 
57 void            tm_interrupt_preemption_thread_0_entry(void);
58 void            tm_interrupt_preemption_thread_1_entry(void);
59 void            tm_interrupt_preemption_handler_entry(void);
60 
61 
62 /* Define the reporting thread prototype.  */
63 
64 void            tm_interrupt_preemption_thread_report(void);
65 
66 
67 /* Define the interrupt handler.  This must be called from the RTOS.  */
68 
69 void            tm_interrupt_preemption_handler(void);
70 
71 
72 /* Define the initialization prototype.  */
73 
74 void            tm_interrupt_preemption_processing_initialize(void);
75 
76 
77 /* Define main entry point.  */
78 
tm_main()79 void tm_main()
80 {
81 
82     /* Initialize the test.  */
83     tm_initialize(tm_interrupt_preemption_processing_initialize);
84 }
85 
86 
87 /* Define the interrupt processing test initialization.  */
88 
tm_interrupt_preemption_processing_initialize(void)89 void  tm_interrupt_preemption_processing_initialize(void)
90 {
91 
92     /* Create interrupt thread at priority 3.  */
93     tm_thread_create(0, 3, tm_interrupt_preemption_thread_0_entry);
94 
95     /* Create thread that generates the interrupt at priority 10.  */
96     tm_thread_create(1, 10, tm_interrupt_preemption_thread_1_entry);
97 
98     /* Resume just thread 1.  */
99     tm_thread_resume(1);
100 
101     /* Create the reporting thread. It will preempt the other
102        threads and print out the test results.  */
103     tm_thread_create(5, 2, tm_interrupt_preemption_thread_report);
104     tm_thread_resume(5);
105 }
106 
107 
108 /* Define the interrupt thread.  This thread is resumed from the
109    interrupt handler.  It runs and suspends.  */
tm_interrupt_preemption_thread_0_entry(void)110 void  tm_interrupt_preemption_thread_0_entry(void)
111 {
112 
113     while(1)
114     {
115 
116         /* Increment this thread's counter.  */
117         tm_interrupt_preemption_thread_0_counter++;
118 
119         /* Suspend. This will allow the thread generating the
120            interrupt to run again.  */
121         tm_thread_suspend(0);
122     }
123 }
124 
125 /* Define the thread that generates the interrupt.  */
tm_interrupt_preemption_thread_1_entry(void)126 void  tm_interrupt_preemption_thread_1_entry(void)
127 {
128 
129     while(1)
130     {
131 
132         /* Force an interrupt. The underlying RTOS must see that the
133            the interrupt handler is called from the appropriate software
134            interrupt or trap. */
135         TM_CAUSE_INTERRUPT
136 
137         /* We won't get back here until the interrupt processing is complete,
138            including the execution of the higher priority thread made ready
139            by the interrupt.  */
140 
141         /* Increment this thread's counter.  */
142         tm_interrupt_preemption_thread_1_counter++;
143     }
144 }
145 
146 
147 /* Define the interrupt handler.  This must be called from the RTOS trap handler.
148    To be fair, it must behave just like a processor interrupt, i.e. it must save
149    the full context of the interrupted thread during the preemption processing. */
tm_interrupt_preemption_handler(void)150 void  tm_interrupt_preemption_handler(void)
151 {
152 
153     /* Increment the interrupt count.  */
154     tm_interrupt_preemption_handler_counter++;
155 
156     /* Resume the higher priority thread from the ISR.  */
157     tm_thread_resume(0);
158 }
159 
160 
161 /* Define the interrupt test reporting thread.  */
tm_interrupt_preemption_thread_report(void)162 void  tm_interrupt_preemption_thread_report(void)
163 {
164 
165 unsigned long   total;
166 unsigned long   relative_time;
167 unsigned long   last_total;
168 unsigned long   average;
169 
170 
171     /* Initialize the last total.  */
172     last_total =  0;
173 
174     /* Initialize the relative time.  */
175     relative_time =  0;
176 
177     while(1)
178     {
179 
180         /* Sleep to allow the test to run.  */
181         tm_thread_sleep(TM_TEST_DURATION);
182 
183         /* Increment the relative time.  */
184         relative_time =  relative_time + TM_TEST_DURATION;
185 
186         /* Print results to the stdio window.  */
187         printf("**** Thread-Metric Interrupt Preemption Processing Test **** Relative Time: %lu\n", relative_time);
188 
189         /* Calculate the total of all the counters.  */
190         total =  tm_interrupt_preemption_thread_0_counter + tm_interrupt_preemption_thread_1_counter + tm_interrupt_preemption_handler_counter;
191 
192         /* Calculate the average of all the counters.  */
193         average =  total/3;
194 
195         /* See if there are any errors.  */
196         if ((tm_interrupt_preemption_thread_0_counter < (average - 1)) ||
197             (tm_interrupt_preemption_thread_0_counter > (average + 1)) ||
198             (tm_interrupt_preemption_thread_1_counter < (average - 1)) ||
199             (tm_interrupt_preemption_thread_1_counter > (average + 1)) ||
200             (tm_interrupt_preemption_handler_counter < (average - 1)) ||
201             (tm_interrupt_preemption_handler_counter > (average + 1)))
202         {
203 
204             printf("ERROR: Invalid counter value(s). Interrupt processing test has failed!\n");
205         }
206 
207         /* Show the total interrupts for the time period.  */
208         printf("Time Period Total:  %lu\n\n", tm_interrupt_preemption_handler_counter - last_total);
209 
210         /* Save the last total number of interrupts.  */
211         last_total =  tm_interrupt_preemption_handler_counter;
212     }
213 }
214