1 // Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string.h>
16 #include "unity.h"
17 #include "esp_log.h"
18 #include <stdlib.h>
19 
20 #include "perfmon.h"
21 
22 // These includes required only for the tests
23 #include "xtensa-debug-module.h"
24 #include "eri.h"
25 
26 static const char *TAG = "perfmon";
27 
28 
29 TEST_CASE("Perfomance counter dump", "[perfmon]")
30 {
31 
32     xtensa_perfmon_dump();
33     xtensa_perfmon_stop();
34     xtensa_perfmon_dump();
35     xtensa_perfmon_init(0, 0, 0xffff, 0, 6);
36     xtensa_perfmon_dump();
37     xtensa_perfmon_reset(0);
38     xtensa_perfmon_start();
39     int pm_data[10];
40     for (int i = 0 ; i < 10 ; i++) {
41         if (i == 4) {
42             xtensa_perfmon_reset(0);
43             xtensa_perfmon_start();
44         }
45         if (i == 6) {
46             xtensa_perfmon_stop();
47         }
48         if (i == 8) {
49             xtensa_perfmon_start();
50         }
51         pm_data[i] = eri_read(ERI_PERFMON_PM0);
52     }
53     for (int i = 0 ; i < 10 ; i++) {
54         ESP_LOGI(TAG, "pm_data[%i]= %08x", i, pm_data[i]);
55     }
56     if (pm_data[4] > pm_data[3]) {
57         ESP_LOGE(TAG, "The functions xtensa_perfmon_reset and xtensa_perfmon_start are not working correct.");
58         ESP_LOGW(TAG, "pm_data[3]= %i, must be > pm_data[4]= %i", pm_data[3], pm_data[4]);
59         TEST_ESP_OK(ESP_FAIL);
60     }
61     if ( pm_data[6] !=  pm_data[7]) {
62         ESP_LOGE(TAG, "The xtensa_perfmon_stop functions is not working correct.");
63         ESP_LOGW(TAG, "pm_data[6]= %i, must be == pm_data[7]= %i", pm_data[6], pm_data[7]);
64         TEST_ESP_OK(ESP_FAIL);
65     }
66     if ( pm_data[7] ==  pm_data[8]) {
67         ESP_LOGE(TAG, "The xtensa_perfmon_start functions is not working correct.");
68         ESP_LOGW(TAG, "pm_data[7]= %i, must be < pm_data[8]= %i", pm_data[7], pm_data[8]);
69         TEST_ESP_OK(ESP_FAIL);
70     }
71 
72     xtensa_perfmon_stop();
73 }
74 
test_call(void * params)75 static void test_call(void* params)
76 {
77     for (int i = 0 ; i < 1000 ; i++) {
78         __asm__ __volatile__("   nop");
79     }
80 }
81 
82 static bool callback_called = false;
83 static int callback_call_count = 0;
test_callback(void * params,uint32_t select,uint32_t mask,uint32_t value)84 static void test_callback(void *params, uint32_t select, uint32_t mask, uint32_t value)
85 {
86     ESP_LOGI("test", "test_callback select = %i,  mask = %i, value = %i", select, mask, value);
87     callback_called = true;
88     callback_call_count++;
89 }
90 
91 TEST_CASE("Performacnce test callback", "[perfmon]")
92 {
93     ESP_LOGI(TAG, "Initialize performance structure");
94     xtensa_perfmon_config_t pm_config = {};
95     pm_config.counters_size = sizeof(xtensa_perfmon_select_mask_all) / sizeof(uint32_t) / 2;
96     pm_config.select_mask = xtensa_perfmon_select_mask_all;
97     pm_config.repeat_count = 200;
98     pm_config.max_deviation = 1;
99     pm_config.call_function = test_call;
100     pm_config.callback = test_callback;
101     pm_config.callback_params = stdout;
102     pm_config.tracelevel = -1; // Trace all events
103     callback_called = false;
104     callback_call_count = 0;
105     xtensa_perfmon_exec(&pm_config);
106 
107     ESP_LOGI(TAG, "Callback count = %i", callback_call_count);
108     if (callback_call_count != pm_config.counters_size) {
109         ESP_LOGE(TAG, "The callback count is not correct.");
110         ESP_LOGW(TAG, "callback_call_count= %i, must be == pm_config.counters_size= %i", callback_call_count, pm_config.counters_size);
111         TEST_ESP_OK(ESP_FAIL);
112     }
113     if (ESP_OK != xtensa_perfmon_overflow(0))
114     {
115         ESP_LOGE(TAG, "Perfmon 0 overflow detected!");
116         TEST_ESP_OK(ESP_FAIL);
117     }
118     if (ESP_OK != xtensa_perfmon_overflow(1))
119     {
120         ESP_LOGE(TAG, "Perfmon 1 overflow detected!");
121         TEST_ESP_OK(ESP_FAIL);
122     }
123     if (false == callback_called) {
124         TEST_ESP_OK(ESP_FAIL);
125     }
126 }
127 
exec_callback(void * params)128 static void exec_callback(void *params)
129 {
130     for (int i = 0 ; i < 100 ; i++) {
131         __asm__ __volatile__("   nop");
132     }
133 }
134 
135 static const uint32_t test_dsp_table[] = {
136     XTPERF_CNT_CYCLES, XTPERF_MASK_CYCLES, // total cycles
137     XTPERF_CNT_INSN, XTPERF_MASK_INSN_ALL, // total instructions
138     XTPERF_CNT_D_LOAD_U1, XTPERF_MASK_D_LOAD_LOCAL_MEM, // Mem read
139     XTPERF_CNT_D_STORE_U1, XTPERF_MASK_D_STORE_LOCAL_MEM, // Mem write
140     XTPERF_CNT_BUBBLES, XTPERF_MASK_BUBBLES_ALL &(~XTPERF_MASK_BUBBLES_R_HOLD_REG_DEP),  // wait for other reasons
141     XTPERF_CNT_BUBBLES, XTPERF_MASK_BUBBLES_R_HOLD_REG_DEP,           // Wait for register dependency
142     XTPERF_CNT_OVERFLOW, XTPERF_MASK_OVERFLOW,               // Last test cycle
143 };
144 
145 TEST_CASE("Performance test for Empty callback", "[perfmon]")
146 {
147     for (int i = 5 ; i < 10 ; i++) {
148         exec_callback(NULL);
149         ESP_LOGD(TAG, "Empty call passed.");
150     }
151 
152     ESP_LOGI(TAG, "Start first test");
153     xtensa_perfmon_config_t pm_config = {};
154     pm_config.counters_size = sizeof(xtensa_perfmon_select_mask_all) / sizeof(uint32_t) / 2;
155     pm_config.select_mask = xtensa_perfmon_select_mask_all;
156     pm_config.repeat_count = 200;
157     pm_config.max_deviation = 1;
158     pm_config.call_function = exec_callback;
159     pm_config.callback = xtensa_perfmon_view_cb;
160     pm_config.callback_params = stdout;
161     pm_config.tracelevel = -1;
162 
163     xtensa_perfmon_exec(&pm_config);
164     callback_call_count = 0;
165     ESP_LOGI(TAG, "Start second test");
166     pm_config.counters_size = sizeof(test_dsp_table) / sizeof(uint32_t) / 2;
167     pm_config.select_mask = test_dsp_table;
168     pm_config.repeat_count = 200;
169     pm_config.max_deviation = 1;
170     pm_config.call_function = exec_callback;
171     pm_config.callback = xtensa_perfmon_view_cb;
172     pm_config.callback_params = stdout;
173     pm_config.tracelevel = -1;
174 
175     xtensa_perfmon_exec(&pm_config);
176     callback_call_count = 0;
177     ESP_LOGI(TAG, "Start third test");
178     pm_config.counters_size = sizeof(test_dsp_table) / sizeof(uint32_t) / 2;
179     pm_config.select_mask = test_dsp_table;
180     pm_config.repeat_count = 200;
181     pm_config.max_deviation = 1;
182     pm_config.call_function = exec_callback;
183     pm_config.callback = test_callback;
184     pm_config.callback_params = stdout;
185     pm_config.tracelevel = -1;
186 
187     xtensa_perfmon_exec(&pm_config);
188     if (callback_call_count != pm_config.counters_size) {
189         TEST_ESP_OK(ESP_FAIL);
190     }
191     ESP_LOGI(TAG, "All tests passed.");
192 }
193