PX4 Firmware
PX4 Autopilot Software http://px4.io
cpuload.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2016 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file cpuload.c
36  *
37  * Measurement of CPU load of each individual task.
38  *
39  * @author Lorenz Meier <lorenz@px4.io>
40  * @author Petri Tanskanen <petri.tanskanen@inf.ethz.ch>
41  */
42 #include <px4_platform_common/px4_config.h>
43 
44 #include <sys/types.h>
45 #include <stdint.h>
46 #include <stdbool.h>
47 
48 //#include <arch/arch.h>
49 
50 //#include <debug.h>
51 
52 #include <sys/time.h>
53 
54 #include <arch/board/board.h>
55 #include <drivers/drv_hrt.h>
56 
57 #include "cpuload.h"
58 
59 #ifdef CONFIG_SCHED_INSTRUMENTATION
60 
61 #ifdef __PX4_NUTTX
62 
63 # include <nuttx/sched_note.h>
64 
65 void sched_note_suspend(FAR struct tcb_s *tcb);
66 void sched_note_resume(FAR struct tcb_s *tcb);
67 
68 __EXPORT void sched_note_switch(FAR struct tcb_s *pFromTcb, FAR struct tcb_s *pToTcb);
69 
70 __EXPORT struct system_load_s system_load;
71 
72 extern FAR struct tcb_s *sched_gettcb(pid_t pid);
73 
74 void cpuload_initialize_once()
75 {
76  system_load.start_time = hrt_absolute_time();
77  int i;
78 
79  for (i = 0; i < CONFIG_MAX_TASKS; i++) {
80  system_load.tasks[i].valid = false;
81  }
82 
83  int static_tasks_count = 2; // there are at least 2 threads that should be initialized statically - "idle" and "init"
84 
85 #ifdef CONFIG_PAGING
86  static_tasks_count++; // include paging thread in initialization
87 #endif /* CONFIG_PAGING */
88 #if CONFIG_SCHED_WORKQUEUE
89  static_tasks_count++; // include high priority work0 thread in initialization
90 #endif /* CONFIG_SCHED_WORKQUEUE */
91 #if CONFIG_SCHED_LPWORK
92  static_tasks_count++; // include low priority work1 thread in initialization
93 #endif /* CONFIG_SCHED_WORKQUEUE */
94 
95  // perform static initialization of "system" threads
96  for (system_load.total_count = 0; system_load.total_count < static_tasks_count; system_load.total_count++) {
97  system_load.tasks[system_load.total_count].total_runtime = 0;
98  system_load.tasks[system_load.total_count].curr_start_time = 0;
99  system_load.tasks[system_load.total_count].tcb = sched_gettcb(
100  system_load.total_count); // it is assumed that these static threads have consecutive PIDs
101  system_load.tasks[system_load.total_count].valid = true;
102  }
103 
104  system_load.initialized = true;
105 }
106 
107 void sched_note_start(FAR struct tcb_s *tcb)
108 {
109  /* search first free slot */
110  int i;
111 
112  if (system_load.initialized) {
113  for (i = 1; i < CONFIG_MAX_TASKS; i++) {
114  if (!system_load.tasks[i].valid) {
115  /* slot is available */
116  system_load.tasks[i].total_runtime = 0;
117  system_load.tasks[i].curr_start_time = 0;
118  system_load.tasks[i].tcb = tcb;
119  system_load.tasks[i].valid = true;
120  system_load.total_count++;
121  break;
122  }
123  }
124  }
125 }
126 
127 void sched_note_stop(FAR struct tcb_s *tcb)
128 {
129  int i;
130 
131  if (system_load.initialized) {
132  for (i = 1; i < CONFIG_MAX_TASKS; i++) {
133  if (system_load.tasks[i].tcb != 0 && system_load.tasks[i].tcb->pid == tcb->pid) {
134  /* mark slot as fee */
135  system_load.tasks[i].valid = false;
136  system_load.tasks[i].total_runtime = 0;
137  system_load.tasks[i].curr_start_time = 0;
138  system_load.tasks[i].tcb = NULL;
139  system_load.total_count--;
140  break;
141  }
142  }
143  }
144 }
145 
146 void sched_note_suspend(FAR struct tcb_s *tcb)
147 {
148 
149  if (system_load.initialized) {
150  uint64_t new_time = hrt_absolute_time();
151 
152  for (int i = 0; i < CONFIG_MAX_TASKS; i++) {
153  /* Task ending its current scheduling run */
154  if (system_load.tasks[i].valid && system_load.tasks[i].tcb != 0 && system_load.tasks[i].tcb->pid == tcb->pid) {
155  system_load.tasks[i].total_runtime += new_time - system_load.tasks[i].curr_start_time;
156  break;
157  }
158  }
159  }
160 }
161 
162 void sched_note_resume(FAR struct tcb_s *tcb)
163 {
164 
165  if (system_load.initialized) {
166  uint64_t new_time = hrt_absolute_time();
167 
168  for (int i = 0; i < CONFIG_MAX_TASKS; i++) {
169  if (system_load.tasks[i].valid && system_load.tasks[i].tcb->pid == tcb->pid) {
170  // curr_start_time is accessed from an IRQ handler (in logger), so we need
171  // to make the update atomic
172  irqstate_t irq_state = px4_enter_critical_section();
173  system_load.tasks[i].curr_start_time = new_time;
174  px4_leave_critical_section(irq_state);
175  break;
176  }
177  }
178  }
179 
180 }
181 
182 #else
183 __EXPORT struct system_load_s system_load;
184 #endif
185 #endif /* CONFIG_SCHED_INSTRUMENTATION */
Definition: I2C.hpp:51
High-resolution timer with callouts and timekeeping.
#define CONFIG_MAX_TASKS
Definition: printload.h:49
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).