1 /* syskernel.c */
2 
3 /*
4  * Copyright (c) 1997-2010, 2012-2014 Wind River Systems, Inc.
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/kernel.h>
10 #include <zephyr/tc_util.h>
11 
12 #include "syskernel.h"
13 
14 #include <string.h>
15 
16 K_THREAD_STACK_DEFINE(thread_stack1, STACK_SIZE);
17 K_THREAD_STACK_DEFINE(thread_stack2, STACK_SIZE);
18 struct k_thread thread_data1;
19 struct k_thread thread_data2;
20 
21 char Msg[256];
22 
23 FILE *output_file;
24 
25 const char sz_success[] = "SUCCESSFUL";
26 const char sz_partial[] = "PARTIAL";
27 const char sz_fail[] = "FAILED";
28 
29 /* time necessary to read the time */
30 uint32_t tm_off;
31 
32 /* Holds the loop count that need to be carried out. */
33 uint32_t number_of_loops;
34 
35 /**
36  *
37  * @brief Get the time ticks before test starts
38  *
39  * Routine does necessary preparations for the test to start
40  *
41  */
begin_test(void)42 void begin_test(void)
43 {
44 	/*
45 	 * Invoke bench_test_start in order to be able to use
46 	 * timestamp_check static variable.
47 	 */
48 	bench_test_start();
49 }
50 
51 /**
52  *
53  * @brief Checks number of tests and calculate average time
54  *
55  * @return 1 if success and 0 on failure
56  *
57  * @param i   Number of tests.
58  * @param t   Time in ticks for the whole test.
59  */
check_result(int i,uint32_t t)60 int check_result(int i, uint32_t t)
61 {
62 	/*
63 	 * bench_test_end checks timestamp_check static variable.
64 	 * bench_test_start modifies it
65 	 */
66 	if (bench_test_end() != 0) {
67 		fprintf(output_file, sz_case_result_fmt, sz_fail);
68 		fprintf(output_file, sz_case_details_fmt,
69 				"timer tick happened. Results are inaccurate");
70 		fprintf(output_file, sz_case_end_fmt);
71 		return 0;
72 	}
73 	if (i != number_of_loops) {
74 		fprintf(output_file, sz_case_result_fmt, sz_fail);
75 		fprintf(output_file, sz_case_details_fmt, "loop counter = ");
76 		fprintf(output_file, "%i !!!", i);
77 		fprintf(output_file, sz_case_end_fmt);
78 		return 0;
79 	}
80 	fprintf(output_file, sz_case_result_fmt, sz_success);
81 	fprintf(output_file, sz_case_details_fmt,
82 			"Average time for 1 iteration: ");
83 	fprintf(output_file, sz_case_timing_fmt,
84 			SYS_CLOCK_HW_CYCLES_TO_NS_AVG(t, number_of_loops));
85 
86 	fprintf(output_file, sz_case_end_fmt);
87 	return 1;
88 }
89 
90 /**
91  *
92  * @brief Prepares the test output
93  *
94  * @param continuously   Run test till the user presses the key.
95  *
96  */
97 
init_output(int * continuously)98 void init_output(int *continuously)
99 {
100 	ARG_UNUSED(continuously);
101 
102 	/*
103 	 * send all printf and fprintf to console
104 	 */
105 	output_file = stdout;
106 }
107 
108 
109 /**
110  *
111  * @brief Close output for the test
112  *
113  */
output_close(void)114 void output_close(void)
115 {
116 }
117 
118 /**
119  *
120  * @brief Perform all selected benchmarks
121  *
122  */
main(void)123 int main(void)
124 {
125 	int	    continuously = 0;
126 	int	    test_result;
127 
128 	number_of_loops = NUMBER_OF_LOOPS;
129 
130 	/* The following code is needed to make the benchmarking run on
131 	 * slower platforms.
132 	 */
133 	uint64_t time_stamp = sys_clock_tick_get();
134 
135 	k_sleep(K_MSEC(1));
136 
137 	uint64_t time_stamp_2 = sys_clock_tick_get();
138 
139 	if (time_stamp_2 - time_stamp > 1) {
140 		number_of_loops = 10U;
141 	}
142 
143 	init_output(&continuously);
144 	bench_test_init();
145 
146 
147 
148 	do {
149 		fprintf(output_file, sz_module_title_fmt,
150 			"kernel API test");
151 		fprintf(output_file, sz_kernel_ver_fmt,
152 			sys_kernel_version_get());
153 		fprintf(output_file,
154 			"\n\nEach test below is repeated %d times;\n"
155 			"average time for one iteration is displayed.",
156 			number_of_loops);
157 
158 		test_result = 0;
159 
160 		test_result += sema_test();
161 		test_result += lifo_test();
162 		test_result += fifo_test();
163 		test_result += stack_test();
164 		test_result += mem_slab_test();
165 
166 		if (test_result) {
167 			/* sema/lifo/fifo/stack/mem_slab account for 14 tests in total */
168 			if (test_result == 14) {
169 				fprintf(output_file, sz_module_result_fmt,
170 					sz_success);
171 			} else {
172 				fprintf(output_file, sz_module_result_fmt,
173 					sz_partial);
174 			}
175 		} else {
176 			fprintf(output_file, sz_module_result_fmt, sz_fail);
177 		}
178 		TC_PRINT_RUNID;
179 
180 	} while (continuously);
181 
182 	output_close();
183 	return 0;
184 }
185