31 #if !CH_DBG_THREADS_PROFILING
32 #error CH_DBG_THREADS_PROFILING should be defined to TRUE to use this monitoring tool
44 #define MAX_CPU_INFO_ENTRIES 20
46 typedef struct _ThreadCpuInfo {
47 float ticks[MAX_CPU_INFO_ENTRIES];
48 float cpu[MAX_CPU_INFO_ENTRIES];
53 static void stampThreadCpuInfo (ThreadCpuInfo *ti)
55 const thread_t *tp = chRegFirstThread();
59 totalTicks+= (float) tp->time;
60 ti->cpu[
idx] = (
float) tp->time - ti->ticks[
idx];;
61 ti->ticks[
idx] = (float) tp->time;
62 tp = chRegNextThread ((thread_t *)tp);
64 }
while ((tp != NULL) && (
idx < MAX_CPU_INFO_ENTRIES));
66 const float diffTotal = totalTicks- ti->totalTicks;
67 ti->totalTicks = totalTicks;
68 tp = chRegFirstThread();
71 ti->cpu[
idx] = (ti->cpu[
idx]*100.f)/diffTotal;
72 tp = chRegNextThread ((thread_t *)tp);
74 }
while ((tp != NULL) && (
idx < MAX_CPU_INFO_ENTRIES));
77 static float stampThreadGetCpuPercent (
const ThreadCpuInfo *ti,
const uint32_t idx)
79 if (
idx >= MAX_CPU_INFO_ENTRIES)
84 static void cmd_threads(
shell_stream_t *lchp,
int argc,
const char*
const argv[]) {
85 static const char *
states[] = {CH_STATE_NAMES};
86 thread_t *tp = chRegFirstThread();
92 static ThreadCpuInfo threadCpuInfo = {
93 .ticks = {[0 ... MAX_CPU_INFO_ENTRIES-1] = 0.f},
94 .cpu = {[0 ... MAX_CPU_INFO_ENTRIES-1] =-1.f},
97 stampThreadCpuInfo (&threadCpuInfo);
99 chprintf (lchp,
" addr stack frestk prio refs state time \t percent name\r\n");
102 chprintf (lchp,
"%.8lx %.8lx %6lu %4lu %4lu %9s %9lu %.1f \t%s\r\n",
107 stampThreadGetCpuPercent (&threadCpuInfo,
idx),
108 chRegGetThreadNameX(tp));
109 totalTicks+= (float) tp->time;
110 if (strcmp (chRegGetThreadNameX(tp),
"idle") == 0)
111 idleTicks = (
float) tp->time;
112 tp = chRegNextThread ((thread_t *)tp);
114 }
while (tp != NULL);
115 const float idlePercent = (idleTicks*100.f)/totalTicks;
116 const float cpuPercent = 100.f - idlePercent;
117 chprintf (lchp,
"\r\ncpu load = %.2f%%\r\n", cpuPercent);
120 static void cmd_rtos_mon(
shell_stream_t *sh,
int argc,
const char *
const argv[])
124 chprintf(sh,
"Usage: rtos_mon\r\n");
128 chprintf(sh,
"Data reported in the RTOS_MON message:\r\n");
137 chprintf(sh,
" thread %d load: %0.1f, free stack: %d\r\n", i,
159 size_t total_fragments, total_fragmented_free_space, largest_free_block;
160 total_fragments = chHeapStatus(NULL, &total_fragmented_free_space, &largest_free_block);
173 tp = chRegFirstThread();
186 sum += (float)(tp->time);
189 if (tp == chSysGetIdleThreadX()) {
193 tp = chRegNextThread(tp);
213 extern const uint8_t __ram0_end__;
214 unsigned long long *stkAdr = (
unsigned long long *) ((
uint8_t *) tp->wabase);
215 while ((stkAdr[index] == 0x5555555555555555) && ( ((
uint8_t *) &(stkAdr[index])) < &__ram0_end__))