PX4 Firmware
PX4 Autopilot Software http://px4.io
resources.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2015 PX4 Development Team. All rights reserved.
4  * Author: David Sidrane<david_s5@nscdg.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * 3. Neither the name PX4 nor the names of its contributors may be
17  * used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  ****************************************************************************/
34 
35 #include <px4_platform_common/px4_config.h>
36 #include <nuttx/progmem.h>
37 #include <nuttx/compiler.h>
38 #include <stdlib.h>
39 #include <syslog.h>
40 
41 #include <nuttx/arch.h>
42 
43 #include <systemlib/cpuload.h>
44 #include "resources.hpp"
45 
46 #if !defined(CONFIG_NSH_LIBRARY)
47 
48 static const char *
49 tstate_name(const tstate_t s)
50 {
51  switch (s) {
52  case TSTATE_TASK_INVALID: return "init";
53 
54  case TSTATE_TASK_PENDING: return "PEND";
55 
56  case TSTATE_TASK_READYTORUN: return "READY";
57 
58  case TSTATE_TASK_RUNNING: return "RUN";
59 
60  case TSTATE_TASK_INACTIVE: return "inact";
61 
62  case TSTATE_WAIT_SEM: return "w:sem";
63 #ifndef CONFIG_DISABLE_SIGNALS
64 
65  case TSTATE_WAIT_SIG: return "w:sig";
66 #endif
67 #ifndef CONFIG_DISABLE_MQUEUE
68 
69  case TSTATE_WAIT_MQNOTEMPTY: return "w:mqe";
70 
71  case TSTATE_WAIT_MQNOTFULL: return "w:mqf";
72 #endif
73 #ifdef CONFIG_PAGING
74 
75  case TSTATE_WAIT_PAGEFILL: return "w:pgf";
76 #endif
77 
78  default:
79  return "ERROR";
80  }
81 }
82 
83 void stack_check(void)
84 {
85 
86  for (int i = 0; i < CONFIG_MAX_TASKS; i++) {
87 
88  if (system_load.tasks[i].tcb) {
89  size_t stack_size = system_load.tasks[i].tcb->adj_stack_size;
90  ssize_t stack_free = 0;
91 
92 #if CONFIG_ARCH_INTERRUPTSTACK > 3
93 
94  if (system_load.tasks[i].tcb->pid == 0) {
95  stack_size = (CONFIG_ARCH_INTERRUPTSTACK & ~3);
96  stack_free = up_check_intstack_remain();
97 
98  } else {
99 #endif
100  stack_free = up_check_tcbstack_remain(system_load.tasks[i].tcb);
101 #if CONFIG_ARCH_INTERRUPTSTACK > 3
102  }
103 
104 #endif
105  ::syslog(LOG_INFO, "%4d %*-s %8lld %5u/%5u %3u (%3u) ",
106  system_load.tasks[i].tcb->pid,
107  CONFIG_TASK_NAME_SIZE, system_load.tasks[i].tcb->name,
108  (system_load.tasks[i].total_runtime / 1000),
109  stack_size - stack_free,
110  stack_size,
111  system_load.tasks[i].tcb->sched_priority,
112  system_load.tasks[i].tcb->base_priority);
113 
114 #if CONFIG_RR_INTERVAL > 0
115  /* print scheduling info with RR time slice */
116  ::syslog(LOG_INFO, " %6d\n", system_load.tasks[i].tcb->timeslice);
117 #else
118  /* print task state instead */
119  ::syslog(LOG_INFO, " %-6s\n", tstate_name((tstate_t)system_load.tasks[i].tcb->task_state));
120 #endif
121  }
122  }
123 }
124 
125 static void free_getprogmeminfo(struct mallinfo *mem)
126 {
127  size_t page = 0, stpage = 0xFFFF;
128  size_t pagesize = 0;
129  ssize_t status;
130 
131  mem->arena = 0;
132  mem->fordblks = 0;
133  mem->uordblks = 0;
134  mem->mxordblk = 0;
135 
136  for (status = 0, page = 0; status >= 0; page++) {
137  status = up_progmem_ispageerased(page);
138  pagesize = up_progmem_pagesize(page);
139 
140  mem->arena += pagesize;
141 
142  /* Is this beginning of new free space section */
143 
144  if (status == 0) {
145  if (stpage == 0xFFFF) { stpage = page; }
146 
147  mem->fordblks += pagesize;
148 
149  } else if (status != 0) {
150  mem->uordblks += pagesize;
151 
152  if (stpage != 0xFFFF && up_progmem_isuniform()) {
153  stpage = page - stpage;
154 
155  if (stpage > (size_t) mem->mxordblk) {
156  mem->mxordblk = stpage;
157  }
158 
159  stpage = 0xFFFF;
160  }
161  }
162  }
163 
164  mem->mxordblk *= pagesize;
165 }
166 
167 void free_check(void)
168 {
169  struct mallinfo data;
170  struct mallinfo prog;
171 
172 #ifdef CONFIG_CAN_PASS_STRUCTS
173  data = mallinfo();
174 #else
175  (void)mallinfo(&data);
176 #endif
177 
178  free_getprogmeminfo(&prog);
179 
180 
181  ::syslog(LOG_INFO, " total used free largest\n");
182 
183  ::syslog(LOG_INFO, "Data: %11d%11d%11d%11d\n",
184  data.arena, data.uordblks, data.fordblks, data.mxordblk);
185 
186  ::syslog(LOG_INFO, "Prog: %11d%11d%11d%11d\n",
187  prog.arena, prog.uordblks, prog.fordblks, prog.mxordblk);
188 }
189 #endif /* CONFIG_NSH_LIBRARY */
static struct vehicle_status_s status
Definition: Commander.cpp:138
void free_check(void)
Definition: resources.cpp:167
#define CONFIG_MAX_TASKS
Definition: printload.h:49
static void free_getprogmeminfo(struct mallinfo *mem)
Definition: resources.cpp:125
void stack_check(void)
Definition: resources.cpp:83
static const char * tstate_name(const tstate_t s)
Definition: resources.cpp:49