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 /**   Basic Processing Test                                               */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 
22 /**************************************************************************/
23 /*                                                                        */
24 /*  FUNCTION                                               RELEASE        */
25 /*                                                                        */
26 /*    tm_basic_processing_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 basic test for determining board processing   */
35 /*    capabilities                                                        */
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 #include "tm_api.h"
45 
46 
47 /* Define the counters used in the demo application...  */
48 
49 unsigned long   tm_basic_processing_counter;
50 
51 
52 /* Test array.  We will just do a series of calculations on the
53    test array to eat up processing bandwidth. The idea is that
54    all RTOSes should produce the same metric here if everything
55    else is equal, e.g. processor speed, memory speed, etc.  */
56 
57 volatile unsigned long   tm_basic_processing_array[1024];
58 
59 
60 /* Define the test thread prototypes.  */
61 
62 void            tm_basic_processing_thread_0_entry(void);
63 
64 
65 /* Define the reporting thread prototype.  */
66 
67 void            tm_basic_processing_thread_report(void);
68 
69 
70 /* Define the initialization prototype.  */
71 
72 void            tm_basic_processing_initialize(void);
73 
74 
75 /* Define main entry point.  */
76 
tm_main()77 void tm_main()
78 {
79 
80     /* Initialize the test.  */
81     tm_initialize(tm_basic_processing_initialize);
82 }
83 
84 
85 /* Define the basic processing test initialization.  */
86 
tm_basic_processing_initialize(void)87 void  tm_basic_processing_initialize(void)
88 {
89   /* Create thread 0 at priority 10.  */
90     tm_thread_create(0, 10, tm_basic_processing_thread_0_entry);
91 
92     /* Resume thread 0.  */
93     tm_thread_resume(0);
94 
95     /* Create the reporting thread. It will preempt the other
96        threads and print out the test results.  */
97     tm_thread_create(5, 2, tm_basic_processing_thread_report);
98     tm_thread_resume(5);
99 }
100 
101 
102 /* Define the basic processing thread.  */
tm_basic_processing_thread_0_entry(void)103 void  tm_basic_processing_thread_0_entry(void)
104 {
105 
106 int     i;
107 
108     /* Initialize the test array.   */
109     for (i = 0; i < 1024; i++)
110     {
111 
112         /* Clear the basic processing array.  */
113         tm_basic_processing_array[i] =  0;
114     }
115 
116     while(1)
117     {
118 
119         /* Loop through the basic processing array, add the previous
120            contents with the contents of the tm_basic_processing_counter
121            and xor the result with the previous value...   just to eat
122            up some time.  */
123         for (i = 0; i < 1024; i++)
124         {
125 
126             /* Update each array entry.  */
127             tm_basic_processing_array[i] =  (tm_basic_processing_array[i] + tm_basic_processing_counter) ^ tm_basic_processing_array[i];
128         }
129 
130         /* Increment the basic processing counter.  */
131         tm_basic_processing_counter++;
132     }
133 }
134 
135 
136 /* Define the basic processing reporting thread.  */
tm_basic_processing_thread_report(void)137 void  tm_basic_processing_thread_report(void)
138 {
139 
140 unsigned long   last_counter;
141 unsigned long   relative_time;
142 
143 
144     /* Initialize the last counter.  */
145     last_counter =  0;
146 
147     /* Initialize the relative time.  */
148     relative_time =  0;
149 
150     while(1)
151     {
152 
153         /* Sleep to allow the test to run.  */
154         tm_thread_sleep(TM_TEST_DURATION);
155 
156         /* Increment the relative time.  */
157         relative_time =  relative_time + TM_TEST_DURATION;
158 
159         /* Print results to the stdio window.  */
160         printf("**** Thread-Metric Basic Single Thread Processing Test **** Relative Time: %lu\n", relative_time);
161 
162         /* See if there are any errors.  */
163         if (tm_basic_processing_counter == last_counter)
164         {
165 
166             printf("ERROR: Invalid counter value(s). Basic processing thread died!\n");
167         }
168 
169         /* Show the time period total.  */
170         printf("Time Period Total:  %lu\n\n", tm_basic_processing_counter - last_counter);
171 
172         /* Save the last counter.  */
173         last_counter =  tm_basic_processing_counter;
174     }
175 }
176