33#error CH_DBG_STATISTICS should be defined to TRUE to use this monitoring tool
59typedef struct _ThreadCpuInfo {
67static void stampThreadCpuInfo(ThreadCpuInfo *ti)
69 const thread_t *tp = chRegFirstThread();
74 ti->ticks[
idx] = (float) tp->stats.cumulative;
75 ti->totalTicks += ti->ticks[
idx];
76 tp = chRegNextThread((thread_t *)tp);
79 ti->totalISRTicks = currcore->kernel_stats.m_crit_isr.cumulative;
80 ti->totalTicks += ti->totalISRTicks;
81 tp = chRegFirstThread();
84 ti->cpu[
idx] = (ti->totalTicks > 0.f) ? ((ti->ticks[
idx] * 100.f) / ti->totalTicks) : 0.f;
85 tp = chRegNextThread((thread_t *)tp);
90static float stampThreadGetCpuPercent(
const ThreadCpuInfo *ti,
const uint32_t idx)
99static float stampISRGetCpuPercent(
const ThreadCpuInfo *ti)
101 return (ti->totalTicks > 0.f) ? (ti->totalISRTicks * 100.0f / ti->totalTicks) : 0.f;
104static void cmd_threads(BaseSequentialStream *lchp,
int argc,
const char *
const argv[])
106 static const char *
states[] = {CH_STATE_NAMES};
107 thread_t *tp = chRegFirstThread();
110 float totalTicks = 0;
113 static ThreadCpuInfo threadCpuInfo = {
120 stampThreadCpuInfo(&threadCpuInfo);
122 chprintf(lchp,
" addr stack frestk prio refs state time \t percent name\r\n");
125 chprintf(lchp,
"%.8lx %.8lx %6lu %4lu %4lu %9s %9lu %.2f%% \t%s\r\n",
130 (
uint32_t)RTC2MS(STM32_SYSCLK, tp->stats.cumulative),
131 stampThreadGetCpuPercent(&threadCpuInfo,
idx),
132 chRegGetThreadNameX(tp));
134 totalTicks += (float)tp->stats.cumulative;
135 if (strcmp(chRegGetThreadNameX(tp),
"idle") == 0) {
136 idleTicks = (float)tp->stats.cumulative;
138 tp = chRegNextThread((thread_t *)tp);
140 }
while (tp != NULL);
142 totalTicks += currcore->kernel_stats.m_crit_isr.cumulative;
143 const float idlePercent = (totalTicks > 0.f) ? ((idleTicks * 100.f) / totalTicks) : 0.f;
144 const float cpuPercent = 100.f - idlePercent;
145 chprintf(lchp,
"Interrupt Service Routine \t\t %9lu %.2f%% \tISR\r\n",
146 (
uint32_t)RTC2MS(STM32_SYSCLK, threadCpuInfo.totalISRTicks),
147 stampISRGetCpuPercent(&threadCpuInfo));
148 chprintf(lchp,
"\r\ncpu load = %.2f%%\r\n", cpuPercent);
151static void cmd_rtos_mon(
shell_stream_t *sh,
int argc,
const char *
const argv[])
155 chprintf(sh,
"Usage: rtos_mon\r\n");
159 chprintf(sh,
"Data reported in the RTOS_MON message:\r\n");
167 for (
int i = 0; i <
rtos_mon.thread_counter; i++) {
168 chprintf(sh,
" thread %d load: %0.1f, free stack: %d\r\n", i,
187 size_t total_fragments, total_fragmented_free_space, largest_free_block;
188 total_fragments = chHeapStatus(NULL, &total_fragmented_free_space, &largest_free_block);
190 rtos_mon.core_free_memory = chCoreGetStatusX();
191 rtos_mon.heap_fragments = total_fragments;
192 rtos_mon.heap_largest = largest_free_block;
193 rtos_mon.heap_free_memory = total_fragmented_free_space;
199 rttime_t idle_counter = 0;
201 rttime_t isr_time = currcore->kernel_stats.m_crit_isr.cumulative;
203 tp = chRegFirstThread();
205 const char *name = chRegGetThreadNameX(tp);
225 if (tp == chSysGetIdleThreadX()) {
226 idle_counter = thread_delta;
230 tp = chRegNextThread(tp);
241 for (i = 0; i <
rtos_mon.thread_counter; i ++) {
257 extern const uint8_t __ram0_end__;
258 unsigned long long *stkAdr = (
unsigned long long *)((
uint8_t *) tp->wabase);
259 while ((stkAdr[index] == 0x5555555555555555) && (((
uint8_t *) & (stkAdr[index])) < &__ram0_end__)) {
276 return current_time -
cpu_window.prev_thread_time[i];
287 if (total_ticks == 0) {
298 if (total_ticks == 0) {
void rtos_mon_periodic_arch(void)
void rtos_mon_init_arch(void)
static uint16_t get_thread_load_x10(rttime_t thread_ticks, rttime_t total_ticks)
thread_t * thread_ref[RTOS_MON_MAX_THREADS]
Current thread identities for matching samples.
static uint8_t get_cpu_load_percent(rttime_t idle_ticks, rttime_t total_ticks)
static uint16_t get_stack_free(const thread_t *tp)
thread_t * prev_thread_ref[RTOS_MON_MAX_THREADS]
Previous thread identities for delta lookup.
static rttime_t get_thread_delta(const thread_t *tp, rttime_t current_time)
rttime_t thread_time[RTOS_MON_MAX_THREADS]
Current cumulative thread counters.
uint8_t prev_stats_valid
True after the first full sampling pass.
uint8_t prev_thread_count
Number of valid entries in previous arrays.
static struct rtos_mon_cpu_window cpu_window
rttime_t prev_isr_time
Previous cumulative ISR counter.
rttime_t prev_thread_time[RTOS_MON_MAX_THREADS]
Previous cumulative thread counters.
static struct nodeState states[UWB_SERIAL_COMM_DIST_NUM_NODES]
void chprintf(BaseSequentialStream *lchp, const char *fmt,...)
Mini printf-like functionality.
struct rtos_monitoring rtos_mon
void shell_add_entry(char *cmd_name, shell_cmd_t *cmd)
Add dynamic entry.
BaseSequentialStream shell_stream_t
System monitoring for RTOS targets return cpu load, average exec time, ...
#define RTOS_MON_MAX_THREADS
#define RTOS_MON_NAME_LEN
Architecture independent timing functions.
Implementation of system time functions for ChibiOS arch.
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 long long uint64_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.