6#include "porting_assistant.h"
14#define HIGH_PERF_EXECUTION_TIME_THRESHOLD_MS 600
15#define LOW_PERF_EXECUTION_TIME_THRESHOLD_MS 8000
18#define HIGH_PERF_EXECUTION_TIME_X_CPU_CLOCK_THRESHOLD 60000
19#define LOW_PERF_EXECUTION_TIME_X_CPU_CLOCK_THRESHOLD 500000
22#define LONG_DEEP_FUNC_DEPTH 10
26#define LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s) \
27 memcpy(buf, s, sizeof(s)); \
31 c = c + (b >> 1) - a; \
33 acc += a + b - c * d; \
34 acc ^= (acc << 3) | (acc >> 5); \
35 acc += (a * b) ^ (c + d); \
36 acc -= (b ^ c) + (d - a); \
37 acc *= (c | d) ^ (a & b); \
40 v = v - ((v >> 1) & 0x55555555); \
41 v = (v & 0x33333333) + ((v >> 2) & 0x33333333); \
42 v = (((v + (v >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; \
46#define LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s) \
47 LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s) \
48 LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s) \
49 LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s) \
50 LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s) \
51 LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s) \
52 LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s) \
53 LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s) \
54 LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s) \
55 LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s) \
56 LONG_DEEP_FUNC_BODY(a, b, c, d, acc, buf, s)
58#define LONG_DEEP_FUNC_BODY100(a, b, c, d, acc, buf, s) \
59 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s) \
60 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s) \
61 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s) \
62 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s) \
63 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s) \
64 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s) \
65 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s) \
66 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s) \
67 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s) \
68 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc, buf, s)
70enum cpu_mem_benchmark_result
72 CPU_MEM_BENCHMARK_GOOD,
73 CPU_MEM_BENCHMARK_MEDIUM,
74 CPU_MEM_BENCHMARK_POOR,
75 CPU_MEM_BENCHMARK_UNKNOWN
78static void execute_long_and_deep_func_inner(uint32_t depth)
81 static const char description[] =
"Simulate computations";
82 char buf[
sizeof(description)];
85 volatile uint32_t a = depth, b = depth * 2, c = depth * 3, d = depth * 4;
86 volatile uint32_t acc = 0;
94 for (
int i = 0; i < 100; i++)
96 LONG_DEEP_FUNC_BODY100(a, b, c, d, acc,
buf, description)
97 LONG_DEEP_FUNC_BODY100(a, b, c, d, acc,
buf, description)
98 LONG_DEEP_FUNC_BODY100(a, b, c, d, acc,
buf, description)
99 LONG_DEEP_FUNC_BODY100(a, b, c, d, acc,
buf, description)
100 LONG_DEEP_FUNC_BODY100(a, b, c, d, acc,
buf, description)
108 execute_long_and_deep_func_inner(--depth);
112 LONG_DEEP_FUNC_BODY10(a, b, c, d, acc,
buf, description)
116static void execute_long_and_deep_func(
void)
118 execute_long_and_deep_func_inner(LONG_DEEP_FUNC_DEPTH);
121static const char *benchmark_result_to_str(
enum cpu_mem_benchmark_result result)
125 case CPU_MEM_BENCHMARK_GOOD:
128 case CPU_MEM_BENCHMARK_MEDIUM:
131 case CPU_MEM_BENCHMARK_POOR:
139extern const uint32_t mmhal_system_clock;
141static enum cpu_mem_benchmark_result evaluate_benchmark_result(uint32_t execution_time)
143 if (execution_time <= HIGH_PERF_EXECUTION_TIME_THRESHOLD_MS)
146 return CPU_MEM_BENCHMARK_GOOD;
148 else if (execution_time > LOW_PERF_EXECUTION_TIME_THRESHOLD_MS)
151 return CPU_MEM_BENCHMARK_POOR;
155 uint32_t cpu_speed_mhz = mmhal_system_clock / 1000000;
158 return CPU_MEM_BENCHMARK_UNKNOWN;
161 uint32_t execution_time_x_cpu_speed = execution_time * cpu_speed_mhz;
162 if (execution_time_x_cpu_speed <= HIGH_PERF_EXECUTION_TIME_X_CPU_CLOCK_THRESHOLD)
165 return CPU_MEM_BENCHMARK_GOOD;
167 else if (execution_time_x_cpu_speed > LOW_PERF_EXECUTION_TIME_X_CPU_CLOCK_THRESHOLD)
170 return CPU_MEM_BENCHMARK_POOR;
174 return CPU_MEM_BENCHMARK_MEDIUM;
180 execute_long_and_deep_func();
183 enum cpu_mem_benchmark_result result = evaluate_benchmark_result(execution_time);
185 TEST_LOG_APPEND(
"\tExecution time: %lu ms\t(%s, mmhal_system_clock = %lu MHz)\n",
187 benchmark_result_to_str(result),
188 (mmhal_system_clock / 1000000));
190 if (result == CPU_MEM_BENCHMARK_POOR)
192 TEST_LOG_APPEND(
"\n\tRelatively slow compared to CPU speed. Please check system bus and "
193 "memory performance.\n");
196 else if (result == CPU_MEM_BENCHMARK_UNKNOWN)
198 TEST_LOG_APPEND(
"\n\tPlease check whether 'mmhal_system_clock' is valid\n");
uint32_t mmosal_get_time_ms(void)
Get the system time in milliseconds.
static unsigned char buf[1024]
Statically allocated buffer for MQTT.
const struct test_step test_step_benchmark_cpu_mem_stack_perf
Test definition.