Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
37 static 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 
45 typedef struct _ThreadCpuInfo {
47  float cpu[RTOS_MON_MAX_THREADS];
48  float totalTicks;
49  float totalISRTicks;
50 } ThreadCpuInfo ;
51 
52 
53 static void stampThreadCpuInfo(ThreadCpuInfo *ti)
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];
62  tp = chRegNextThread((thread_t *)tp);
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;
67  tp = chRegFirstThread();
68  idx = 0;
69  do {
70  ti->cpu[idx] = (ti->ticks[idx] * 100.f) / ti->totalTicks;
71  tp = chRegNextThread((thread_t *)tp);
72  idx++;
73  } while ((tp != NULL) && (idx < RTOS_MON_MAX_THREADS));
74 }
75 
76 static float stampThreadGetCpuPercent(const ThreadCpuInfo *ti, const uint32_t idx)
77 {
78  if (idx >= RTOS_MON_MAX_THREADS) {
79  return -1.f;
80  }
81 
82  return ti->cpu[idx];
83 }
84 
85 static float stampISRGetCpuPercent(const ThreadCpuInfo *ti)
86 {
87  return ti->totalISRTicks * 100.0f / ti->totalTicks;
88 }
89 
90 static void cmd_threads(BaseSequentialStream *lchp, int argc, const char *const argv[])
91 {
92  static const char *states[] = {CH_STATE_NAMES};
93  thread_t *tp = chRegFirstThread();
94  (void)argv;
95  (void)argc;
96  float totalTicks = 0;
97  float idleTicks = 0;
98 
99  static ThreadCpuInfo threadCpuInfo = {
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 
106  stampThreadCpuInfo(&threadCpuInfo);
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,
113  get_stack_free(tp),
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),
117  stampThreadGetCpuPercent(&threadCpuInfo, idx),
118  chRegGetThreadNameX(tp));
119 
120  totalTicks += (float)tp->stats.cumulative;
121  if (strcmp(chRegGetThreadNameX(tp), "idle") == 0) {
122  idleTicks = (float)tp->stats.cumulative;
123  }
124  tp = chRegNextThread((thread_t *)tp);
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),
133  stampISRGetCpuPercent(&threadCpuInfo));
134  chprintf(lchp, "\r\ncpu load = %.2f%%\r\n", cpuPercent);
135 }
136 
137 static 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;
173  size_t total_fragments, total_fragmented_free_space, largest_free_block;
174  memory_area_t area;
175  total_fragments = chHeapStatus(NULL, &total_fragmented_free_space, &largest_free_block);
176  chCoreGetStatusX(&area);
177 
178  rtos_mon.core_free_memory = area.size;
179  rtos_mon.heap_fragments = total_fragments;
180  rtos_mon.heap_largest = largest_free_block;
181  rtos_mon.heap_free_memory = total_fragmented_free_space;
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;
190  tp = chRegFirstThread();
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
210  tp = chRegNextThread(tp);
211  // increment thread counter
213  } while (tp != NULL && rtos_mon.thread_counter < RTOS_MON_MAX_THREADS);
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 
227 static uint16_t get_stack_free(const thread_t *tp)
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]
Definition: rtos_mon_arch.c:35
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
if(GpsFixValid() &&e_identification_started)
int ticks
Definition: gps_sirf.c:193
void chprintf(BaseSequentialStream *lchp, const char *fmt,...)
Definition: printf.c:395
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 %
Definition: sys_mon_rtos.h:51
char thread_names[RTOS_MON_THREAD_NAMES+1]
string of thread names / identifiers
Definition: sys_mon_rtos.h:55
uint32_t core_free_memory
core free memory in bytes
Definition: sys_mon_rtos.h:47
uint16_t thread_free_stack[RTOS_MON_MAX_THREADS]
individual thread free stack in bytes
Definition: sys_mon_rtos.h:54
uint32_t heap_free_memory
Total fragmented free memory in the heap.
Definition: sys_mon_rtos.h:48
uint16_t thread_load[RTOS_MON_MAX_THREADS]
individual thread load in centi-percent (10*%)
Definition: sys_mon_rtos.h:53
uint8_t thread_name_idx
length of the string in thread_names buffer
Definition: sys_mon_rtos.h:56
uint8_t thread_counter
number of threads
Definition: sys_mon_rtos.h:52
#define RTOS_MON_MAX_THREADS
Definition: sys_mon_rtos.h:36
uint32_t heap_fragments
Number of fragments in the heap.
Definition: sys_mon_rtos.h:49
#define RTOS_MON_NAME_LEN
Definition: sys_mon_rtos.h:40
uint32_t heap_largest
Largest free block in the heap.
Definition: sys_mon_rtos.h:50
Architecture independent timing functions.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98