Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
rtos_mon_arch.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Gautier Hattenberger <gautier.hattenberger@enac.fr>
3 * 2020 Gautier Hattenberger, Alexandre Bustico
4 *
5 * This file is part of paparazzi
6 *
7 * paparazzi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * paparazzi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with paparazzi; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
20 */
29#include <ch.h>
30
31#if !CH_DBG_STATISTICS
32#error CH_DBG_STATISTICS should be defined to TRUE to use this monitoring tool
33#endif
34
36
37static uint16_t get_stack_free(const thread_t *tp);
38
39#if USE_SHELL
40#include "modules/core/shell.h"
41#include "mcu_periph/sys_time.h"
42#include "printf.h"
43#include "string.h"
44
45typedef struct _ThreadCpuInfo {
47 float cpu[RTOS_MON_MAX_THREADS];
48 float totalTicks;
49 float totalISRTicks;
51
52
54{
55 const thread_t *tp = chRegFirstThread();
56 uint32_t idx = 0;
57
58 ti->totalTicks = 0;
59 do {
60 ti->ticks[idx] = (float) tp->stats.cumulative;
61 ti->totalTicks += ti->ticks[idx];
63 idx++;
64 } while ((tp != NULL) && (idx < RTOS_MON_MAX_THREADS));
65 ti->totalISRTicks = ch0.kernel_stats.m_crit_isr.cumulative;
66 ti->totalTicks += ti->totalISRTicks;
68 idx = 0;
69 do {
70 ti->cpu[idx] = (ti->ticks[idx] * 100.f) / ti->totalTicks;
72 idx++;
73 } while ((tp != NULL) && (idx < RTOS_MON_MAX_THREADS));
74}
75
76static float stampThreadGetCpuPercent(const ThreadCpuInfo *ti, const uint32_t idx)
77{
79 return -1.f;
80 }
81
82 return ti->cpu[idx];
83}
84
85static float stampISRGetCpuPercent(const ThreadCpuInfo *ti)
86{
87 return ti->totalISRTicks * 100.0f / ti->totalTicks;
88}
89
90static void cmd_threads(BaseSequentialStream *lchp, int argc, const char *const argv[])
91{
92 static const char *states[] = {CH_STATE_NAMES};
94 (void)argv;
95 (void)argc;
96 float totalTicks = 0;
97 float idleTicks = 0;
98
100 .ticks = {[0 ... RTOS_MON_MAX_THREADS - 1] = 0.f},
101 .cpu = {[0 ... RTOS_MON_MAX_THREADS - 1] = -1.f},
102 .totalTicks = 0.f,
103 .totalISRTicks = 0.f
104 };
105
107
108 chprintf(lchp, " addr stack frestk prio refs state time \t percent name\r\n");
109 uint32_t idx = 0;
110 do {
111 chprintf(lchp, "%.8lx %.8lx %6lu %4lu %4lu %9s %9lu %.2f%% \t%s\r\n",
112 (uint32_t)tp, (uint32_t)tp->ctx.sp,
114 (uint32_t)tp->hdr.pqueue.prio, (uint32_t)(tp->refs - 1),
115 states[tp->state],
116 (uint32_t)RTC2MS(STM32_SYSCLK, tp->stats.cumulative),
119
120 totalTicks += (float)tp->stats.cumulative;
121 if (strcmp(chRegGetThreadNameX(tp), "idle") == 0) {
122 idleTicks = (float)tp->stats.cumulative;
123 }
125 idx++;
126 } while (tp != NULL);
127
128 totalTicks += ch0.kernel_stats.m_crit_isr.cumulative;
129 const float idlePercent = (idleTicks * 100.f) / totalTicks;
130 const float cpuPercent = 100.f - idlePercent;
131 chprintf(lchp, "Interrupt Service Routine \t\t %9lu %.2f%% \tISR\r\n",
132 (uint32_t)RTC2MS(STM32_SYSCLK, threadCpuInfo.totalISRTicks),
134 chprintf(lchp, "\r\ncpu load = %.2f%%\r\n", cpuPercent);
135}
136
137static void cmd_rtos_mon(shell_stream_t *sh, int argc, const char *const argv[])
138{
139 (void) argv;
140 if (argc > 0) {
141 chprintf(sh, "Usage: rtos_mon\r\n");
142 return;
143 }
144
145 chprintf(sh, "Data reported in the RTOS_MON message:\r\n");
146 chprintf(sh, " core free mem: %u\r\n", rtos_mon.core_free_memory);
147 chprintf(sh, " heap free mem: %u\r\n", rtos_mon.heap_free_memory);
148 chprintf(sh, " heap fragments: %u\r\n", rtos_mon.heap_fragments);
149 chprintf(sh, " heap largest: %u\r\n", rtos_mon.heap_largest);
150 chprintf(sh, " CPU load: %d \%\r\n", rtos_mon.cpu_load);
151 chprintf(sh, " number of threads: %d\r\n", rtos_mon.thread_counter);
152 chprintf(sh, " thread names: %s\r\n", rtos_mon.thread_names);
153 for (int i = 0; i < rtos_mon.thread_counter; i++) {
154 chprintf(sh, " thread %d load: %0.1f, free stack: %d\r\n", i,
155 (float)rtos_mon.thread_load[i] / 10.f, rtos_mon.thread_free_stack[i]);
156 }
157 chprintf(sh, " CPU time: %.2f\r\n", rtos_mon.cpu_time);
158}
159#endif
160
162{
163#if USE_SHELL
164 shell_add_entry("rtos_mon", cmd_rtos_mon);
165 shell_add_entry("threads", cmd_threads);
166#endif
167}
168
169// Fill data structure
171{
172 int i;
177
183
184 // loop threads to find idle thread
185 // store info on other threads
186 thread_t *tp;
187 float idle_counter = 0.f;
188 float sum = 0.f;
191 do {
192 // add beginning of thread name to buffer
193 for (i = 0; i < RTOS_MON_NAME_LEN - 1 && tp->name[i] != '\0'; i++) {
195 }
197
198 // store free stack for this thread
200
201 // store time spend in thread
202 thread_p_time[rtos_mon.thread_counter] = tp->stats.cumulative;
203 sum += (float)(tp->stats.cumulative);
204
205 // if current thread is 'idle' thread, store its value separately
206 if (tp == chSysGetIdleThreadX()) {
207 idle_counter = (float)tp->stats.cumulative;
208 }
209 // get next thread
211 // increment thread counter
214 // sum the time spent in ISR
215 sum += ch0.kernel_stats.m_crit_isr.cumulative;
216 // store individual thread load (as centi-percent integer, i.e. (th_time/sum)*10*100)
217 for (i = 0; i < rtos_mon.thread_counter; i ++) {
218 rtos_mon.thread_load[i] = (uint16_t)(1000.f * (float)thread_p_time[i] / sum);
219 }
220
221 // assume we call the counter once a second
222 // so the difference in seconds is always one
223 // NOTE: not perfectly precise, +-5% on average so take it into consideration
224 rtos_mon.cpu_load = (uint8_t)((1.f - (idle_counter / sum)) * 100.f);
225}
226
228{
229 int32_t index = 0;
230 extern const uint8_t __ram0_end__;
231 unsigned long long *stkAdr = (unsigned long long *)((uint8_t *) tp->wabase);
232 while ((stkAdr[index] == 0x5555555555555555) && (((uint8_t *) & (stkAdr[index])) < &__ram0_end__)) {
233 index++;
234 }
235 const int32_t freeBytes = index * (int32_t) sizeof(long long);
236 return (uint16_t)freeBytes;
237}
238
static uint32_t thread_p_time[RTOS_MON_MAX_THREADS]
void rtos_mon_periodic_arch(void)
void rtos_mon_init_arch(void)
static uint16_t get_stack_free(const thread_t *tp)
static struct nodeState states[UWB_SERIAL_COMM_DIST_NUM_NODES]
#define STM32_SYSCLK
int ticks
Definition gps_sirf.c:193
void chprintf(BaseSequentialStream *lchp, const char *fmt,...)
Definition printf.c:395
uint16_t foo
Definition main_demo5.c:58
static uint32_t idx
Mini printf-like functionality.
struct rtos_monitoring rtos_mon
Definition rtos_mon.c:30
void shell_add_entry(char *cmd_name, shell_cmd_t *cmd)
Add dynamic entry.
Definition shell_arch.c:92
BaseSequentialStream shell_stream_t
Definition shell_arch.h:31
System monitoring for RTOS targets return cpu load, average exec time, ...
uint8_t cpu_load
global CPU/MCU load in %
char thread_names[RTOS_MON_THREAD_NAMES+1]
string of thread names / identifiers
uint32_t core_free_memory
core free memory in bytes
uint16_t thread_free_stack[RTOS_MON_MAX_THREADS]
individual thread free stack in bytes
uint32_t heap_free_memory
Total fragmented free memory in the heap.
uint16_t thread_load[RTOS_MON_MAX_THREADS]
individual thread load in centi-percent (10*%)
uint8_t thread_name_idx
length of the string in thread_names buffer
uint8_t thread_counter
number of threads
#define RTOS_MON_MAX_THREADS
uint32_t heap_fragments
Number of fragments in the heap.
#define RTOS_MON_NAME_LEN
uint32_t heap_largest
Largest free block in the heap.
Architecture independent timing functions.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.