31 #if !CH_DBG_STATISTICS
32 #error CH_DBG_STATISTICS should be defined to TRUE to use this monitoring tool
44 typedef struct _ThreadCpuInfo {
52 static void stampThreadCpuInfo (ThreadCpuInfo *ti)
54 const thread_t *tp = chRegFirstThread();
59 ti->ticks[
idx] = (float) tp->stats.cumulative;
60 ti->totalTicks += ti->ticks[
idx];
61 tp = chRegNextThread ((thread_t *)tp);
64 ti->totalISRTicks = ch.kernel_stats.m_crit_isr.cumulative;
65 ti->totalTicks += ti->totalISRTicks;
66 tp = chRegFirstThread();
69 ti->cpu[
idx] = (ti->ticks[
idx]*100.f) / ti->totalTicks;
70 tp = chRegNextThread ((thread_t *)tp);
75 static float stampThreadGetCpuPercent (
const ThreadCpuInfo *ti,
const uint32_t idx)
83 static float stampISRGetCpuPercent (
const ThreadCpuInfo *ti)
85 return ti->totalISRTicks * 100.0f / ti->totalTicks;
88 static void cmd_threads(BaseSequentialStream *lchp,
int argc,
const char *
const argv[]) {
89 static const char *
states[] = {CH_STATE_NAMES};
90 thread_t *tp = chRegFirstThread();
96 static ThreadCpuInfo threadCpuInfo = {
103 stampThreadCpuInfo (&threadCpuInfo);
105 chprintf (lchp,
" addr stack frestk prio refs state time \t percent name\r\n");
108 chprintf (lchp,
"%.8lx %.8lx %6lu %4lu %4lu %9s %9lu %.2f%% \t%s\r\n",
113 (
uint32_t)RTC2MS(STM32_SYSCLK, tp->stats.cumulative),
114 stampThreadGetCpuPercent (&threadCpuInfo,
idx),
115 chRegGetThreadNameX(tp));
117 totalTicks+= (float)tp->stats.cumulative;
118 if (strcmp(chRegGetThreadNameX(tp),
"idle") == 0)
119 idleTicks = (
float)tp->stats.cumulative;
120 tp = chRegNextThread ((thread_t *)tp);
122 }
while (tp != NULL);
124 totalTicks += ch.kernel_stats.m_crit_isr.cumulative;
125 const float idlePercent = (idleTicks*100.f)/totalTicks;
126 const float cpuPercent = 100.f - idlePercent;
127 chprintf (lchp,
"Interrupt Service Routine \t\t %9lu %.2f%% \tISR\r\n",
128 (
uint32_t)RTC2MS(STM32_SYSCLK,threadCpuInfo.totalISRTicks),
129 stampISRGetCpuPercent(&threadCpuInfo));
130 chprintf (lchp,
"\r\ncpu load = %.2f%%\r\n", cpuPercent);
133 static void cmd_rtos_mon(
shell_stream_t *sh,
int argc,
const char *
const argv[])
137 chprintf(sh,
"Usage: rtos_mon\r\n");
141 chprintf(sh,
"Data reported in the RTOS_MON message:\r\n");
150 chprintf(sh,
" thread %d load: %0.1f, free stack: %d\r\n", i,
169 size_t total_fragments, total_fragmented_free_space, largest_free_block;
170 total_fragments = chHeapStatus(NULL, &total_fragmented_free_space, &largest_free_block);
181 float idle_counter = 0.f;
184 tp = chRegFirstThread();
197 sum += (float)(tp->stats.cumulative);
200 if (tp == chSysGetIdleThreadX()) {
201 idle_counter = (float)tp->stats.cumulative;
204 tp = chRegNextThread(tp);
209 sum += ch.kernel_stats.m_crit_isr.cumulative;
224 extern const uint8_t __ram0_end__;
225 unsigned long long *stkAdr = (
unsigned long long *) ((
uint8_t *) tp->wabase);
226 while ((stkAdr[index] == 0x5555555555555555) && ( ((
uint8_t *) &(stkAdr[index])) < &__ram0_end__))