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.h>
10 #include <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  * @return N/A
42  */
begin_test(void)43 void begin_test(void)
44 {
45 	/*
46 	 * Invoke bench_test_start in order to be able to use
47 	 * timestamp_check static variable.
48 	 */
49 	bench_test_start();
50 }
51 
52 /**
53  *
54  * @brief Checks number of tests and calculate average time
55  *
56  * @return 1 if success and 0 on failure
57  *
58  * @param i   Number of tests.
59  * @param t   Time in ticks for the whole test.
60  */
check_result(int i,uint32_t t)61 int check_result(int i, uint32_t t)
62 {
63 	/*
64 	 * bench_test_end checks timestamp_check static variable.
65 	 * bench_test_start modifies it
66 	 */
67 	if (bench_test_end() != 0) {
68 		fprintf(output_file, sz_case_result_fmt, sz_fail);
69 		fprintf(output_file, sz_case_details_fmt,
70 				"timer tick happened. Results are inaccurate");
71 		fprintf(output_file, sz_case_end_fmt);
72 		return 0;
73 	}
74 	if (i != number_of_loops) {
75 		fprintf(output_file, sz_case_result_fmt, sz_fail);
76 		fprintf(output_file, sz_case_details_fmt, "loop counter = ");
77 		fprintf(output_file, "%i !!!", i);
78 		fprintf(output_file, sz_case_end_fmt);
79 		return 0;
80 	}
81 	fprintf(output_file, sz_case_result_fmt, sz_success);
82 	fprintf(output_file, sz_case_details_fmt,
83 			"Average time for 1 iteration: ");
84 	fprintf(output_file, sz_case_timing_fmt,
85 			SYS_CLOCK_HW_CYCLES_TO_NS_AVG(t, number_of_loops));
86 
87 	fprintf(output_file, sz_case_end_fmt);
88 	return 1;
89 }
90 
91 /**
92  *
93  * @brief Prepares the test output
94  *
95  * @param continuously   Run test till the user presses the key.
96  *
97  * @return N/A
98  */
99 
init_output(int * continuously)100 void init_output(int *continuously)
101 {
102 	ARG_UNUSED(continuously);
103 
104 	/*
105 	 * send all printf and fprintf to console
106 	 */
107 	output_file = stdout;
108 }
109 
110 
111 /**
112  *
113  * @brief Close output for the test
114  *
115  * @return N/A
116  */
output_close(void)117 void output_close(void)
118 {
119 }
120 
121 /**
122  *
123  * @brief Perform all selected benchmarks
124  *
125  * @return N/A
126  */
main(void)127 void main(void)
128 {
129 	int	    continuously = 0;
130 	int	    test_result;
131 
132 	number_of_loops = NUMBER_OF_LOOPS;
133 
134 	/* The following code is needed to make the benchmakring run on
135 	 * slower platforms.
136 	 */
137 	uint64_t time_stamp = sys_clock_tick_get();
138 
139 	k_sleep(K_MSEC(1));
140 
141 	uint64_t time_stamp_2 = sys_clock_tick_get();
142 
143 	if (time_stamp_2 - time_stamp > 1) {
144 		number_of_loops = 10U;
145 	}
146 
147 	init_output(&continuously);
148 	bench_test_init();
149 
150 
151 
152 	do {
153 		fprintf(output_file, sz_module_title_fmt,
154 			"kernel API test");
155 		fprintf(output_file, sz_kernel_ver_fmt,
156 			sys_kernel_version_get());
157 		fprintf(output_file,
158 			"\n\nEach test below is repeated %d times;\n"
159 			"average time for one iteration is displayed.",
160 			number_of_loops);
161 
162 		test_result = 0;
163 
164 		test_result += sema_test();
165 		test_result += lifo_test();
166 		test_result += fifo_test();
167 		test_result += stack_test();
168 		test_result += mem_slab_test();
169 
170 		if (test_result) {
171 			/* sema/lifo/fifo/stack/mem_slab account for 14 tests in total */
172 			if (test_result == 14) {
173 				fprintf(output_file, sz_module_result_fmt,
174 					sz_success);
175 			} else {
176 				fprintf(output_file, sz_module_result_fmt,
177 					sz_partial);
178 			}
179 		} else {
180 			fprintf(output_file, sz_module_result_fmt, sz_fail);
181 		}
182 		TC_PRINT_RUNID;
183 
184 	} while (continuously);
185 
186 	output_close();
187 }
188