-
Notifications
You must be signed in to change notification settings - Fork 3
/
esp32_htop.c
258 lines (219 loc) · 6.38 KB
/
esp32_htop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "esp_err.h"
#include "esp32_htop.h"
// Store vaules for each port away.
static TOL task_order_list[TASKS_LISTS];
uint8_t task_total = 0;
/***********************************************************************
*
* stats_task
*
***********************************************************************/
void stats_task(void *arg)
{
printf("%s", HOME_SCREEN);
for (uint16_t x = 0; x < 30; x++)
{
printf(" \r\n");
}
// Clear Vars
for(uint8_t x = 0 ; x < TASKS_LISTS ; x++)
{
bzero(task_order_list[x].task_name, sizeof(task_order_list[x].task_name));
task_order_list[x].percentage_time = 0;
}
//Print real time stats periodically
for(; ;)
{
vTaskDelay(pdMS_TO_TICKS(1000));
if (print_real_time_stats(STATS_TICKS) == ESP_OK)
{
char temp[TASKS_NAME_LENGTH + 1];
for (int i = 0; i < task_total; i++)
{
for (int j = i + 1; j < task_total; j++)
{
if (strcmp(task_order_list[i].task_name, task_order_list[j].task_name) > 0)
{
strcpy(temp, task_order_list[i].task_name);
uint32_t pt = task_order_list[i].percentage_time;
strcpy(task_order_list[i].task_name, task_order_list[j].task_name);
task_order_list[i].percentage_time = task_order_list[j].percentage_time;
strcpy(task_order_list[j].task_name, temp);
task_order_list[j].percentage_time = pt;
}
}
}
// Set cursor to home screen
printf("%s", HOME_SCREEN);
printf("Real time stats over %d ticks\r\n", STATS_TICKS);
printf("Total Tasks %d\r\n", task_total);
printf("\x1b[31m-----------------------------------------\x1b[0m\r\n");
// If arg is 0 then we just want cpu0 & cpu1 stats
if(arg == 0)
{
task_total = 2;
}
for (uint8_t j = 0; j < task_total; j++)
{
uint32_t cpu_used = 100 - task_order_list[j].percentage_time;
uint8_t bars = ((int)(((float)cpu_used / 100) * 40)) + 1;
// Rename IDLE0 & IDLE1 to PRO & App cpu's These are the 2 cores.
if(strcmp(task_order_list[j].task_name, "IDLE0") == 0)
{
strcpy(task_order_list[j].task_name, "CPU 0 (Pro)");
}
else if(strcmp(task_order_list[j].task_name, "IDLE1") == 0)
{
strcpy(task_order_list[j].task_name, "CPU 1 (App)");
}
else
{
cpu_used = task_order_list[j].percentage_time;
bars = ((int)(((float)task_order_list[j].percentage_time / 100) * 40)) + 1;
}
// Lets make names all evenly spaced.
int g = strlen(task_order_list[j].task_name);
printf("%s%s", COLOR_CYAN, task_order_list[j].task_name);
for (int l = g; l < 15; l++)
{
printf(" ");
}
// Start Bracket
printf("%s", START_BRACKET);
for (uint8_t s = 0; s < 40; s++)
{
if (s < bars)
{
if (s < CPU_MID_RANGE)
{
printf("%s|", COLOR_GREEN);
}
else if ((s >= CPU_MID_RANGE) && (s < CPU_HIGH_RANGE))
{
printf("%s|", COLOR_YELLOW);
}
else if ((s >= CPU_HIGH_RANGE) && (s < bars))
{
printf("%s|", COLOR_RED);
}
}
else
{
printf(" ");
}
}
printf("%03d%%", cpu_used);
// End Bracket
printf("%s\r\n", END_BRACKET);
}
}
else
{
printf("Error getting real time stats\r\n");
}
}
}
/********************************************************************
*
*
* print_real_time_stats()
*
*
********************************************************************/
esp_err_t print_real_time_stats(TickType_t xTicksToWait)
{
TaskStatus_t *start_array = NULL, *end_array = NULL;
UBaseType_t start_array_size, end_array_size;
uint32_t start_run_time, end_run_time;
esp_err_t ret;
//Allocate array to store current task states
start_array_size = uxTaskGetNumberOfTasks() + ARRAY_SIZE_OFFSET;
start_array = malloc(sizeof(TaskStatus_t) * start_array_size);
if (start_array == NULL)
{
ret = ESP_ERR_NO_MEM;
goto exit;
}
//Get current task states
start_array_size = uxTaskGetSystemState(start_array, start_array_size, &start_run_time);
if (start_array_size == 0)
{
ret = ESP_ERR_INVALID_SIZE;
goto exit;
}
vTaskDelay(xTicksToWait);
//Allocate array to store tasks states post delay
end_array_size = uxTaskGetNumberOfTasks() + ARRAY_SIZE_OFFSET;
end_array = malloc(sizeof(TaskStatus_t) * end_array_size);
if (end_array == NULL)
{
ret = ESP_ERR_NO_MEM;
goto exit;
}
//Get post delay task states
end_array_size = uxTaskGetSystemState(end_array, end_array_size, &end_run_time);
if (end_array_size == 0)
{
ret = ESP_ERR_INVALID_SIZE;
goto exit;
}
//Calculate total_elapsed_time in units of run time stats clock period.
uint32_t total_elapsed_time = (end_run_time - start_run_time);
if (total_elapsed_time == 0)
{
ret = ESP_ERR_INVALID_STATE;
goto exit;
}
task_total = uxTaskGetNumberOfTasks();
//Match each task in start_array to those in the end_array
for(int i = 0 ; i < start_array_size ; i++)
{
int k = -1;
for (int j = 0; j < end_array_size; j++)
{
if (start_array[i].xHandle == end_array[j].xHandle)
{
k = j;
//Mark that task have been matched by overwriting their handles
start_array[i].xHandle = NULL;
end_array[j].xHandle = NULL;
break;
}
}
//Check if matching task found
// and save the values
if(k >= 0)
{
uint32_t task_elapsed_time = end_array[k].ulRunTimeCounter - start_array[i].ulRunTimeCounter;
uint32_t percentage_time = (task_elapsed_time * 100UL) / total_elapsed_time;
strcpy(task_order_list[i].task_name, start_array[i].pcTaskName);
task_order_list[i].percentage_time = percentage_time;
}
}
//Print unmatched tasks
for(int i = 0 ; i < start_array_size ; i++)
{
if (start_array[i].xHandle != NULL)
{
printf("| %s | Deleted\n", start_array[i].pcTaskName);
bzero(task_order_list[i].task_name, sizeof(task_order_list[i].task_name));
task_order_list[i].percentage_time = 0;
}
}
for (int i = 0; i < end_array_size; i++)
{
if (end_array[i].xHandle != NULL)
{
printf("| %s | Created\n", end_array[i].pcTaskName);
}
}
ret = ESP_OK;
exit:
//Common return path
free(start_array);
free(end_array);
return ret;
}