mirror of
https://gitee.com/xiaohuolufeihua/bizhang_-obav.git
synced 2026-05-21 01:12:11 +00:00
NuttX cpuload monitoring optimization
- Nuttx only process all suspend & resume scheduling notes when top is running, otherwise only track IDLE - convert cpuload and print load to c++ - delete unused fields from print_load struct - update hrt_store_absolute_time (previous unused)
This commit is contained in:
@@ -55,6 +55,8 @@ add_library(px4_layer
|
||||
px4_init.cpp
|
||||
lib_crc32.c
|
||||
drv_hrt.cpp
|
||||
cpuload.cpp
|
||||
print_load.cpp
|
||||
${SHMEM_SRCS}
|
||||
)
|
||||
target_compile_definitions(px4_layer PRIVATE MODULE_NAME="px4")
|
||||
|
||||
65
platforms/posix/src/px4/common/cpuload.cpp
Normal file
65
platforms/posix/src/px4/common/cpuload.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file cpuload.cpp
|
||||
*
|
||||
* Measurement of CPU load of each individual task.
|
||||
*
|
||||
* @author Lorenz Meier <lorenz@px4.io>
|
||||
* @author Petri Tanskanen <petri.tanskanen@inf.ethz.ch>
|
||||
*/
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <px4_platform/cpuload.h>
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
static px4::atomic_int cpuload_monitor_all_count{0};
|
||||
|
||||
void cpuload_monitor_start()
|
||||
{
|
||||
cpuload_monitor_all_count.fetch_add(1);
|
||||
}
|
||||
|
||||
void cpuload_monitor_stop()
|
||||
{
|
||||
if (cpuload_monitor_all_count.fetch_sub(1) <= 1) {
|
||||
// don't all the count to go negative
|
||||
cpuload_monitor_all_count.store(0);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
@@ -176,13 +176,11 @@ hrt_abstime hrt_elapsed_time_atomic(const volatile hrt_abstime *then)
|
||||
*
|
||||
* This function ensures that the timestamp cannot be seen half-written by an interrupt handler.
|
||||
*/
|
||||
hrt_abstime hrt_store_absolute_time(volatile hrt_abstime *now)
|
||||
void hrt_store_absolute_time(volatile hrt_abstime *t)
|
||||
{
|
||||
hrt_abstime ts = hrt_absolute_time();
|
||||
return ts;
|
||||
*t = hrt_absolute_time();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* If this returns true, the entry has been invoked and removed from the callout list,
|
||||
* or it has never been entered.
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
__EXPORT void cpuload_monitor_start(void);
|
||||
__EXPORT void cpuload_monitor_stop(void);
|
||||
|
||||
__END_DECLS
|
||||
182
platforms/posix/src/px4/common/print_load.cpp
Normal file
182
platforms/posix/src/px4/common/print_load.cpp
Normal file
@@ -0,0 +1,182 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2015-2020 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file print_load.cpp
|
||||
*
|
||||
* Print the current system load.
|
||||
*
|
||||
* @author Lorenz Meier <lorenz@px4.io>
|
||||
*/
|
||||
|
||||
#include <px4_platform_common/posix.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <px4_platform_common/printload.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
|
||||
#ifdef __PX4_DARWIN
|
||||
#include <mach/mach.h>
|
||||
#endif
|
||||
|
||||
#ifdef __PX4_QURT
|
||||
// dprintf is not available on QURT. Use the usual output to mini-dm.
|
||||
#define dprintf(_fd, _text, ...) ((_fd) == 1 ? PX4_INFO((_text), ##__VA_ARGS__) : (void)(_fd))
|
||||
#endif
|
||||
|
||||
extern struct system_load_s system_load;
|
||||
|
||||
#define CL "\033[K" // clear line
|
||||
|
||||
void init_print_load(struct print_load_s *s)
|
||||
{
|
||||
s->total_user_time = 0;
|
||||
|
||||
s->running_count = 0;
|
||||
s->blocked_count = 0;
|
||||
|
||||
s->new_time = hrt_absolute_time();
|
||||
s->interval_start_time = s->new_time;
|
||||
|
||||
for (int i = 0; i < CONFIG_MAX_TASKS; i++) {
|
||||
s->last_times[i] = 0;
|
||||
}
|
||||
|
||||
s->interval_time_us = 0.f;
|
||||
}
|
||||
|
||||
void print_load(int fd, struct print_load_s *print_state)
|
||||
{
|
||||
char clear_line[] = CL;
|
||||
|
||||
/* print system information */
|
||||
if (fd == 1) {
|
||||
dprintf(fd, "\033[H"); /* move cursor home and clear screen */
|
||||
|
||||
} else {
|
||||
memset(clear_line, 0, sizeof(clear_line));
|
||||
}
|
||||
|
||||
#if defined(__PX4_LINUX) || defined(__PX4_CYGWIN) || defined(__PX4_QURT)
|
||||
dprintf(fd, "%sTOP NOT IMPLEMENTED ON LINUX, QURT, WINDOWS (ONLY ON NUTTX, APPLE)\n", clear_line);
|
||||
|
||||
#elif defined(__PX4_DARWIN)
|
||||
pid_t pid = getpid(); //-- this is the process id you need info for
|
||||
task_t task_handle;
|
||||
task_for_pid(mach_task_self(), pid, &task_handle);
|
||||
|
||||
task_info_data_t tinfo;
|
||||
mach_msg_type_number_t th_info_cnt;
|
||||
|
||||
th_info_cnt = TASK_INFO_MAX;
|
||||
kern_return_t kr = task_info(task_handle, TASK_BASIC_INFO, (task_info_t)tinfo, &th_info_cnt);
|
||||
|
||||
if (kr != KERN_SUCCESS) {
|
||||
return;
|
||||
}
|
||||
|
||||
thread_array_t thread_list;
|
||||
mach_msg_type_number_t th_cnt;
|
||||
|
||||
thread_info_data_t th_info_data;
|
||||
mach_msg_type_number_t thread_info_count;
|
||||
|
||||
thread_basic_info_t basic_info_th;
|
||||
uint32_t stat_thread = 0;
|
||||
|
||||
// get all threads of the PX4 main task
|
||||
kr = task_threads(task_handle, &thread_list, &th_cnt);
|
||||
|
||||
if (kr != KERN_SUCCESS) {
|
||||
PX4_WARN("ERROR getting thread list");
|
||||
return;
|
||||
}
|
||||
|
||||
if (th_cnt > 0) {
|
||||
stat_thread += th_cnt;
|
||||
}
|
||||
|
||||
long tot_sec = 0;
|
||||
long tot_usec = 0;
|
||||
long tot_cpu = 0;
|
||||
|
||||
dprintf(fd, "%sThreads: %d total\n",
|
||||
clear_line,
|
||||
th_cnt);
|
||||
|
||||
for (unsigned j = 0; j < th_cnt; j++) {
|
||||
thread_info_count = THREAD_INFO_MAX;
|
||||
kr = thread_info(thread_list[j], THREAD_BASIC_INFO,
|
||||
(thread_info_t)th_info_data, &thread_info_count);
|
||||
|
||||
if (kr != KERN_SUCCESS) {
|
||||
PX4_WARN("ERROR getting thread info");
|
||||
continue;
|
||||
}
|
||||
|
||||
basic_info_th = (thread_basic_info_t)th_info_data;
|
||||
|
||||
if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {
|
||||
tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
|
||||
tot_usec = tot_usec + basic_info_th->system_time.microseconds + basic_info_th->system_time.microseconds;
|
||||
tot_cpu = tot_cpu + basic_info_th->cpu_usage;
|
||||
}
|
||||
|
||||
// char tname[128];
|
||||
|
||||
// int ret = pthread_getname_np(pthread_t *thread,
|
||||
// const char *name, size_t len);
|
||||
|
||||
dprintf(fd, "thread %d\t\t %d\n", j, basic_info_th->cpu_usage);
|
||||
}
|
||||
|
||||
kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list,
|
||||
th_cnt * sizeof(thread_t));
|
||||
|
||||
if (kr != KERN_SUCCESS) {
|
||||
PX4_WARN("ERROR cleaning up thread info");
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void print_load_buffer(char *buffer, int buffer_length, print_load_callback_f cb, void *user,
|
||||
struct print_load_s *print_state)
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user