PX4 Firmware
PX4 Autopilot Software http://px4.io
print_load_posix.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015-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 print_load_posix.c
36  *
37  * Print the current system load.
38  *
39  * @author Lorenz Meier <lorenz@px4.io>
40  */
41 
42 #include <px4_platform_common/posix.h>
43 
44 #include <unistd.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <stdbool.h>
48 
49 #include <systemlib/cpuload.h>
50 #include <systemlib/printload.h>
51 #include <drivers/drv_hrt.h>
52 
53 #ifdef __PX4_DARWIN
54 #include <mach/mach.h>
55 #endif
56 
57 #ifdef __PX4_QURT
58 // dprintf is not available on QURT. Use the usual output to mini-dm.
59 #define dprintf(_fd, _text, ...) ((_fd) == 1 ? PX4_INFO((_text), ##__VA_ARGS__) : (void)(_fd))
60 #endif
61 
62 extern struct system_load_s system_load;
63 
64 #define CL "\033[K" // clear line
65 
66 void init_print_load_s(uint64_t t, struct print_load_s *s)
67 {
68 
69  s->total_user_time = 0;
70 
71  s->running_count = 0;
72  s->blocked_count = 0;
73 
74  s->new_time = t;
75  s->interval_start_time = t;
76 
77  for (int i = 0; i < CONFIG_MAX_TASKS; i++) {
78  s->last_times[i] = 0;
79  }
80 
81  s->interval_time_ms_inv = 0.f;
82 }
83 
84 void print_load(uint64_t t, int fd, struct print_load_s *print_state)
85 {
86  char *clear_line = "";
87 
88  /* print system information */
89  if (fd == 1) {
90  dprintf(fd, "\033[H"); /* move cursor home and clear screen */
91  clear_line = CL;
92  }
93 
94 #if defined(__PX4_LINUX) || defined(__PX4_CYGWIN) || defined(__PX4_QURT)
95  dprintf(fd, "%sTOP NOT IMPLEMENTED ON LINUX, QURT, WINDOWS (ONLY ON NUTTX, APPLE)\n", clear_line);
96 
97 #elif defined(__PX4_DARWIN)
98  pid_t pid = getpid(); //-- this is the process id you need info for
99  task_t task_handle;
100  task_for_pid(mach_task_self(), pid, &task_handle);
101 
102  task_info_data_t tinfo;
103  mach_msg_type_number_t th_info_cnt;
104 
105  th_info_cnt = TASK_INFO_MAX;
106  kern_return_t kr = task_info(task_handle, TASK_BASIC_INFO, (task_info_t)tinfo, &th_info_cnt);
107 
108  if (kr != KERN_SUCCESS) {
109  return;
110  }
111 
112  thread_array_t thread_list;
113  mach_msg_type_number_t th_cnt;
114 
115  thread_info_data_t th_info_data;
116  mach_msg_type_number_t thread_info_count;
117 
118  thread_basic_info_t basic_info_th;
119  uint32_t stat_thread = 0;
120 
121  // get all threads of the PX4 main task
122  kr = task_threads(task_handle, &thread_list, &th_cnt);
123 
124  if (kr != KERN_SUCCESS) {
125  PX4_WARN("ERROR getting thread list");
126  return;
127  }
128 
129  if (th_cnt > 0) {
130  stat_thread += th_cnt;
131  }
132 
133  long tot_sec = 0;
134  long tot_usec = 0;
135  long tot_cpu = 0;
136 
137  dprintf(fd, "%sThreads: %d total\n",
138  clear_line,
139  th_cnt);
140 
141  for (unsigned j = 0; j < th_cnt; j++) {
142  thread_info_count = THREAD_INFO_MAX;
143  kr = thread_info(thread_list[j], THREAD_BASIC_INFO,
144  (thread_info_t)th_info_data, &thread_info_count);
145 
146  if (kr != KERN_SUCCESS) {
147  PX4_WARN("ERROR getting thread info");
148  continue;
149  }
150 
151  basic_info_th = (thread_basic_info_t)th_info_data;
152 
153  if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {
154  tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
155  tot_usec = tot_usec + basic_info_th->system_time.microseconds + basic_info_th->system_time.microseconds;
156  tot_cpu = tot_cpu + basic_info_th->cpu_usage;
157  }
158 
159  // char tname[128];
160 
161  // int ret = pthread_getname_np(pthread_t *thread,
162  // const char *name, size_t len);
163 
164  dprintf(fd, "thread %d\t\t %d\n", j, basic_info_th->cpu_usage);
165  }
166 
167  kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list,
168  th_cnt * sizeof(thread_t));
169 
170  if (kr != KERN_SUCCESS) {
171  PX4_WARN("ERROR cleaning up thread info");
172  return;
173  }
174 
175 #endif
176 }
177 
178 void print_load_buffer(uint64_t t, char *buffer, int buffer_length, print_load_callback_f cb, void *user,
179  struct print_load_s *print_state)
180 {
181 
182 }
183 
uint64_t new_time
Definition: printload.h:58
High-resolution timer with callouts and timekeeping.
void(* print_load_callback_f)(void *user)
Definition: printload.h:71
Print the current system load.
#define CONFIG_MAX_TASKS
Definition: printload.h:49
int blocked_count
Definition: printload.h:56
int fd
Definition: dataman.cpp:146
float interval_time_ms_inv
Definition: printload.h:61
uint64_t interval_start_time
Definition: printload.h:59
uint64_t total_user_time
Definition: printload.h:53
uint32_t last_times[CONFIG_MAX_TASKS]
Definition: printload.h:60
int running_count
Definition: printload.h:55