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