Paparazzi UAS  v6.1.0_stable
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 "printf.h"
42 #include "string.h"
43 
44 typedef struct _ThreadCpuInfo {
46  float cpu[RTOS_MON_MAX_THREADS];
47  float totalTicks;
48  float totalISRTicks;
49 } ThreadCpuInfo ;
50 
51 
52 static void stampThreadCpuInfo (ThreadCpuInfo *ti)
53 {
54  const thread_t *tp = chRegFirstThread();
55  uint32_t idx=0;
56 
57  ti->totalTicks =0;
58  do {
59  ti->ticks[idx] = (float) tp->stats.cumulative;
60  ti->totalTicks += ti->ticks[idx];
61  tp = chRegNextThread ((thread_t *)tp);
62  idx++;
63  } while ((tp != NULL) && (idx < RTOS_MON_MAX_THREADS));
64  ti->totalISRTicks = ch.kernel_stats.m_crit_isr.cumulative;
65  ti->totalTicks += ti->totalISRTicks;
66  tp = chRegFirstThread();
67  idx=0;
68  do {
69  ti->cpu[idx] = (ti->ticks[idx]*100.f) / ti->totalTicks;
70  tp = chRegNextThread ((thread_t *)tp);
71  idx++;
72  } while ((tp != NULL) && (idx < RTOS_MON_MAX_THREADS));
73 }
74 
75 static float stampThreadGetCpuPercent (const ThreadCpuInfo *ti, const uint32_t idx)
76 {
78  return -1.f;
79 
80  return ti->cpu[idx];
81 }
82 
83 static float stampISRGetCpuPercent (const ThreadCpuInfo *ti)
84 {
85  return ti->totalISRTicks * 100.0f / ti->totalTicks;
86 }
87 
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();
91  (void)argv;
92  (void)argc;
93  float totalTicks=0;
94  float idleTicks=0;
95 
96  static ThreadCpuInfo threadCpuInfo = {
97  .ticks = {[0 ... RTOS_MON_MAX_THREADS-1] = 0.f},
98  .cpu = {[0 ... RTOS_MON_MAX_THREADS-1] =-1.f},
99  .totalTicks = 0.f,
100  .totalISRTicks = 0.f
101  };
102 
103  stampThreadCpuInfo (&threadCpuInfo);
104 
105  chprintf (lchp, " addr stack frestk prio refs state time \t percent name\r\n");
106  uint32_t idx=0;
107  do {
108  chprintf (lchp, "%.8lx %.8lx %6lu %4lu %4lu %9s %9lu %.2f%% \t%s\r\n",
109  (uint32_t)tp, (uint32_t)tp->ctx.sp,
110  get_stack_free (tp),
111  (uint32_t)tp->hdr.pqueue.prio, (uint32_t)(tp->refs - 1),
112  states[tp->state],
113  (uint32_t)RTC2MS(STM32_SYSCLK, tp->stats.cumulative),
114  stampThreadGetCpuPercent (&threadCpuInfo, idx),
115  chRegGetThreadNameX(tp));
116 
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);
121  idx++;
122  } while (tp != NULL);
123 
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);
131 }
132 
133 static void cmd_rtos_mon(shell_stream_t *sh, int argc, const char * const argv[])
134 {
135  (void) argv;
136  if (argc > 0) {
137  chprintf(sh, "Usage: rtos_mon\r\n");
138  return;
139  }
140 
141  chprintf(sh, "Data reported in the RTOS_MON message:\r\n");
142  chprintf(sh, " core free mem: %u\r\n", rtos_mon.core_free_memory);
143  chprintf(sh, " heap free mem: %u\r\n", rtos_mon.heap_free_memory);
144  chprintf(sh, " heap fragments: %u\r\n", rtos_mon.heap_fragments);
145  chprintf(sh, " heap largest: %u\r\n", rtos_mon.heap_largest);
146  chprintf(sh, " CPU load: %d \%\r\n", rtos_mon.cpu_load);
147  chprintf(sh, " number of threads: %d\r\n", rtos_mon.thread_counter);
148  chprintf(sh, " thread names: %s\r\n", rtos_mon.thread_names);
149  for (int i = 0; i < rtos_mon.thread_counter; i++) {
150  chprintf(sh, " thread %d load: %0.1f, free stack: %d\r\n", i,
151  (float)rtos_mon.thread_load[i] / 10.f, rtos_mon.thread_free_stack[i]);
152  }
153  chprintf(sh, " CPU time: %.2f\r\n", rtos_mon.cpu_time);
154 }
155 #endif
156 
158 {
159 #if USE_SHELL
160  shell_add_entry("rtos_mon", cmd_rtos_mon);
161  shell_add_entry("threads", cmd_threads);
162 #endif
163 }
164 
165 // Fill data structure
167 {
168  int 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);
171 
172  rtos_mon.core_free_memory = chCoreGetStatusX();
173  rtos_mon.heap_fragments = total_fragments;
174  rtos_mon.heap_largest = largest_free_block;
175  rtos_mon.heap_free_memory = total_fragmented_free_space;
177 
178  // loop threads to find idle thread
179  // store info on other threads
180  thread_t *tp;
181  float idle_counter = 0.f;
182  float sum = 0.f;
184  tp = chRegFirstThread();
185  do {
186  // add beginning of thread name to buffer
187  for (i = 0; i < RTOS_MON_NAME_LEN-1 && tp->name[i] != '\0'; i++) {
189  }
191 
192  // store free stack for this thread
194 
195  // store time spend in thread
196  thread_p_time[rtos_mon.thread_counter] = tp->stats.cumulative;
197  sum += (float)(tp->stats.cumulative);
198 
199  // if current thread is 'idle' thread, store its value separately
200  if (tp == chSysGetIdleThreadX()) {
201  idle_counter = (float)tp->stats.cumulative;
202  }
203  // get next thread
204  tp = chRegNextThread(tp);
205  // increment thread counter
207  } while (tp != NULL && rtos_mon.thread_counter < RTOS_MON_MAX_THREADS);
208  // sum the time spent in ISR
209  sum += ch.kernel_stats.m_crit_isr.cumulative;
210  // store individual thread load (as centi-percent integer, i.e. (th_time/sum)*10*100)
211  for (i = 0; i < rtos_mon.thread_counter; i ++) {
212  rtos_mon.thread_load[i] = (uint16_t)(1000.f * (float)thread_p_time[i] / sum);
213  }
214 
215  // assume we call the counter once a second
216  // so the difference in seconds is always one
217  // NOTE: not perfectly precise, +-5% on average so take it into consideration
218  rtos_mon.cpu_load = (uint8_t)((1.f - (idle_counter / sum)) * 100.f);
219 }
220 
221 static uint16_t get_stack_free(const thread_t *tp)
222 {
223  int32_t index = 0;
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__))
227  index++;
228  const int32_t freeBytes = index * (int32_t) sizeof(long long);
229  return (uint16_t)freeBytes;
230 }
231 
uint32_t
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
uint8_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
states
static struct nodeState states[UWB_SERIAL_COMM_DIST_NUM_NODES]
Definition: decawave_anchorless_communication.c:83
rtos_monitoring::thread_counter
uint8_t thread_counter
number of threads
Definition: sys_mon_rtos.h:52
rtos_monitoring::thread_load
uint16_t thread_load[RTOS_MON_MAX_THREADS]
individual thread load in centi-percent (10*%)
Definition: sys_mon_rtos.h:53
chprintf
void chprintf(BaseSequentialStream *lchp, const char *fmt,...)
Definition: printf.c:395
rtos_monitoring::heap_largest
uint32_t heap_largest
Largest free block in the heap.
Definition: sys_mon_rtos.h:50
rtos_monitoring::thread_name_idx
uint8_t thread_name_idx
length of the string in thread_names buffer
Definition: sys_mon_rtos.h:56
thread_p_time
static uint32_t thread_p_time[RTOS_MON_MAX_THREADS]
Definition: rtos_mon_arch.c:35
rtos_monitoring::thread_names
char thread_names[RTOS_MON_THREAD_NAMES+1]
string of thread names / identifiers
Definition: sys_mon_rtos.h:55
idx
static uint32_t idx
Definition: nps_radio_control_spektrum.c:105
shell_add_entry
void shell_add_entry(char *cmd_name, shell_cmd_t *cmd)
Add dynamic entry.
Definition: shell_arch.c:85
printf.h
Mini printf-like functionality.
rtos_mon
struct rtos_monitoring rtos_mon
Definition: rtos_mon.c:30
RTOS_MON_NAME_LEN
#define RTOS_MON_NAME_LEN
Definition: sys_mon_rtos.h:40
if
if(GpsFixValid() &&e_identification_started)
Definition: e_identification_fr.c:159
shell_stream_t
BaseSequentialStream shell_stream_t
Definition: shell_arch.h:31
f
uint16_t f
Camera baseline, in meters (i.e. horizontal distance between the two cameras of the stereo setup)
Definition: wedgebug.c:204
rtos_mon_periodic_arch
void rtos_mon_periodic_arch(void)
Definition: rtos_mon_arch.c:166
rtos_monitoring::thread_free_stack
uint16_t thread_free_stack[RTOS_MON_MAX_THREADS]
individual thread free stack in bytes
Definition: sys_mon_rtos.h:54
get_stack_free
static uint16_t get_stack_free(const thread_t *tp)
Definition: rtos_mon_arch.c:221
rtos_monitoring::cpu_time
float cpu_time
Definition: sys_mon_rtos.h:57
int32_t
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
rtos_monitoring::heap_free_memory
uint32_t heap_free_memory
Total fragmented free memory in the heap.
Definition: sys_mon_rtos.h:48
sys_mon_rtos.h
rtos_monitoring::core_free_memory
uint32_t core_free_memory
core free memory in bytes
Definition: sys_mon_rtos.h:47
ticks
int ticks
Definition: gps_sirf.c:193
rtos_monitoring::heap_fragments
uint32_t heap_fragments
Number of fragments in the heap.
Definition: sys_mon_rtos.h:49
uint16_t
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
rtos_mon_init_arch
void rtos_mon_init_arch(void)
Definition: rtos_mon_arch.c:157
shell.h
rtos_monitoring::cpu_load
uint8_t cpu_load
global CPU/MCU load in %
Definition: sys_mon_rtos.h:51
RTOS_MON_MAX_THREADS
#define RTOS_MON_MAX_THREADS
Definition: sys_mon_rtos.h:36