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:
Daniel Agar
2020-09-05 14:35:50 -04:00
committed by GitHub
parent a9798454cd
commit 023f6d3983
25 changed files with 339 additions and 240 deletions
@@ -0,0 +1,79 @@
/****************************************************************************
*
* Copyright (c) 2015 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 printload.h
*
* Print the current system load.
*
* @author Lorenz Meier <lorenz@px4.io>
*/
#pragma once
#include <px4_platform_common/px4_config.h>
#include <stdint.h>
#ifndef CONFIG_MAX_TASKS
#define CONFIG_MAX_TASKS 64
#endif
struct print_load_s {
uint64_t total_user_time{0};
int running_count{0};
int blocked_count{0};
uint64_t new_time{0};
uint64_t interval_start_time{0};
uint64_t last_times[CONFIG_MAX_TASKS] {};
float interval_time_us{0.f};
};
__BEGIN_DECLS
__EXPORT void init_print_load(struct print_load_s *s);
__EXPORT void print_load(int fd, struct print_load_s *print_state);
typedef void (*print_load_callback_f)(void *user);
/**
* Print load to a buffer, and call cb after each written line (buffer will not include '\n')
*/
void print_load_buffer(char *buffer, int buffer_length, print_load_callback_f cb, void *user,
struct print_load_s *print_state);
__END_DECLS
@@ -39,7 +39,9 @@ if(NOT PX4_BOARD MATCHES "px4_io")
board_dma_alloc.c
board_fat_dma_alloc.c
console_buffer.cpp
cpuload.cpp
gpio.c
print_load.cpp
tasks.cpp
px4_nuttx_impl.cpp
px4_init.cpp
+204
View File
@@ -0,0 +1,204 @@
/****************************************************************************
*
* Copyright (c) 2012-2016 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.c
*
* 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>
#if defined(__PX4_NUTTX) && defined(CONFIG_SCHED_INSTRUMENTATION)
# include <nuttx/sched_note.h>
void sched_note_suspend(FAR struct tcb_s *tcb);
void sched_note_resume(FAR struct tcb_s *tcb);
__EXPORT void sched_note_switch(FAR struct tcb_s *pFromTcb, FAR struct tcb_s *pToTcb);
__EXPORT struct system_load_s system_load;
extern FAR struct tcb_s *sched_gettcb(pid_t pid);
static px4::atomic_int cpuload_monitor_all_count{0};
void cpuload_monitor_start()
{
if (cpuload_monitor_all_count.fetch_add(1) == 0) {
// if the count was previously 0 (idle thread only) then clear any existing runtime data
sched_lock();
system_load.start_time = hrt_absolute_time();
for (int i = 1; i < CONFIG_MAX_TASKS; i++) {
system_load.tasks[i].total_runtime = 0;
system_load.tasks[i].curr_start_time = 0;
}
sched_unlock();
}
}
void cpuload_monitor_stop()
{
if (cpuload_monitor_all_count.fetch_sub(1) <= 1) {
// don't allow the count to go negative
cpuload_monitor_all_count.store(0);
}
}
void cpuload_initialize_once()
{
for (auto &task : system_load.tasks) {
task.valid = false;
}
int static_tasks_count = 2; // there are at least 2 threads that should be initialized statically - "idle" and "init"
#ifdef CONFIG_PAGING
static_tasks_count++; // include paging thread in initialization
#endif /* CONFIG_PAGING */
#if CONFIG_SCHED_WORKQUEUE
static_tasks_count++; // include high priority work0 thread in initialization
#endif /* CONFIG_SCHED_WORKQUEUE */
#if CONFIG_SCHED_LPWORK
static_tasks_count++; // include low priority work1 thread in initialization
#endif /* CONFIG_SCHED_WORKQUEUE */
// perform static initialization of "system" threads
for (system_load.total_count = 0; system_load.total_count < static_tasks_count; system_load.total_count++) {
system_load.tasks[system_load.total_count].total_runtime = 0;
system_load.tasks[system_load.total_count].curr_start_time = 0;
system_load.tasks[system_load.total_count].tcb = sched_gettcb(
system_load.total_count); // it is assumed that these static threads have consecutive PIDs
system_load.tasks[system_load.total_count].valid = true;
}
system_load.initialized = true;
}
void sched_note_start(FAR struct tcb_s *tcb)
{
// find first free slot
if (system_load.initialized) {
for (auto &task : system_load.tasks) {
if (!task.valid) {
// slot is available
task.total_runtime = 0;
task.curr_start_time = 0;
task.tcb = tcb;
task.valid = true;
system_load.total_count++;
break;
}
}
}
}
void sched_note_stop(FAR struct tcb_s *tcb)
{
if (system_load.initialized) {
for (auto &task : system_load.tasks) {
if (task.tcb && task.tcb->pid == tcb->pid) {
// mark slot as free
task.valid = false;
task.total_runtime = 0;
task.curr_start_time = 0;
task.tcb = nullptr;
system_load.total_count--;
break;
}
}
}
}
void sched_note_suspend(FAR struct tcb_s *tcb)
{
if (system_load.initialized) {
if (tcb->pid == 0) {
system_load.tasks[0].total_runtime += hrt_elapsed_time(&system_load.tasks[0].curr_start_time);
return;
} else {
if (cpuload_monitor_all_count.load() == 0) {
return;
}
}
for (auto &task : system_load.tasks) {
// Task ending its current scheduling run
if (task.valid && (task.curr_start_time > 0)
&& task.tcb && task.tcb->pid == tcb->pid) {
task.total_runtime += hrt_elapsed_time(&task.curr_start_time);
break;
}
}
}
}
void sched_note_resume(FAR struct tcb_s *tcb)
{
if (system_load.initialized) {
if (tcb->pid == 0) {
hrt_store_absolute_time(&system_load.tasks[0].curr_start_time);
return;
} else {
if (cpuload_monitor_all_count.load() == 0) {
return;
}
}
for (auto &task : system_load.tasks) {
if (task.valid && task.tcb && task.tcb->pid == tcb->pid) {
// curr_start_time is accessed from an IRQ handler (in logger), so we need
// to make the update atomic
hrt_store_absolute_time(&task.curr_start_time);
break;
}
}
}
}
#endif // PX4_NUTTX && CONFIG_SCHED_INSTRUMENTATION
@@ -0,0 +1,68 @@
/****************************************************************************
*
* Copyright (c) 2012, 2013 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
#ifdef CONFIG_SCHED_INSTRUMENTATION
#include <sched.h>
#include <stdint.h>
#include <stdbool.h>
struct system_load_taskinfo_s {
uint64_t total_runtime{0}; ///< Runtime since start (start_time - total_runtime)/(start_time - current_time) = load
uint64_t curr_start_time{0}; ///< Start time of the current scheduling slot
struct tcb_s *tcb {nullptr};
bool valid{false}; ///< Task is currently active / valid
};
struct system_load_s {
uint64_t start_time{0};
system_load_taskinfo_s tasks[CONFIG_MAX_TASKS] {};
int total_count{0};
int running_count{0};
bool initialized{false};
};
__BEGIN_DECLS
__EXPORT extern struct system_load_s system_load;
__EXPORT void cpuload_initialize_once(void);
__EXPORT void cpuload_monitor_start(void);
__EXPORT void cpuload_monitor_stop(void);
__END_DECLS
#endif
@@ -0,0 +1,405 @@
/****************************************************************************
*
* 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 <string.h>
#include <stdio.h>
#include <px4_platform/cpuload.h>
#include <px4_platform_common/printload.h>
#include <drivers/drv_hrt.h>
#if defined(BOARD_DMA_ALLOC_POOL_SIZE)
#include <px4_platform/board_dma_alloc.h>
#endif /* BOARD_DMA_ALLOC_POOL_SIZE */
#if defined(CONFIG_SCHED_INSTRUMENTATION)
#if !defined(CONFIG_TASK_NAME_SIZE)
#error print_load_nuttx requires CONFIG_TASK_NAME_SIZE
#endif
#if !defined(CONFIG_STACK_COLORATION)
#error print_load_nuttx requires CONFIG_STACK_COLORATION
#endif
extern struct system_load_s system_load;
#define CL "\033[K" // clear line
void init_print_load(struct print_load_s *s)
{
cpuload_monitor_start();
s->total_user_time = 0;
s->running_count = 0;
s->blocked_count = 0;
s->new_time = system_load.start_time;
s->interval_start_time = system_load.start_time;
sched_lock();
// special case for IDLE thread
s->last_times[0] = system_load.tasks[0].total_runtime;
sched_unlock();
for (int i = 1; i < CONFIG_MAX_TASKS; i++) {
s->last_times[i] = 0;
}
s->interval_time_us = 0.f;
}
static constexpr const char *tstate_name(const tstate_t s)
{
switch (s) {
case TSTATE_TASK_INVALID:
return "init";
case TSTATE_TASK_PENDING:
return "PEND";
case TSTATE_TASK_READYTORUN:
return "READY";
case TSTATE_TASK_RUNNING:
return "RUN";
case TSTATE_TASK_INACTIVE:
return "inact";
case TSTATE_WAIT_SEM:
return "w:sem";
#ifndef CONFIG_DISABLE_SIGNALS
case TSTATE_WAIT_SIG:
return "w:sig";
#endif
#ifndef CONFIG_DISABLE_MQUEUE
case TSTATE_WAIT_MQNOTEMPTY:
return "w:mqe";
case TSTATE_WAIT_MQNOTFULL:
return "w:mqf";
#endif
#ifdef CONFIG_PAGING
case TSTATE_WAIT_PAGEFILL:
return "w:pgf";
#endif
default:
return "ERROR";
}
}
void print_load_buffer(char *buffer, int buffer_length, print_load_callback_f cb, void *user,
struct print_load_s *print_state)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat" // NuttX uses a different printf format
#pragma GCC diagnostic ignored "-Wformat-extra-args"
float idle_load = 0.f;
// create a copy of the runtimes because this could be updated during the print output
uint64_t total_runtime[CONFIG_MAX_TASKS] {};
sched_lock();
print_state->new_time = hrt_absolute_time();
for (int i = 0; i < CONFIG_MAX_TASKS; i++) {
if (system_load.tasks[i].valid) {
total_runtime[i] = system_load.tasks[i].total_runtime;
}
}
sched_unlock();
if (print_state->new_time > print_state->interval_start_time) {
print_state->interval_time_us = print_state->new_time - print_state->interval_start_time;
/* header for task list */
snprintf(buffer, buffer_length, "%4s %-*s %8s %6s %11s %10s %-5s %2s",
"PID",
CONFIG_TASK_NAME_SIZE, "COMMAND",
"CPU(ms)",
"CPU(%)",
"USED/STACK",
"PRIO(BASE)",
#if CONFIG_RR_INTERVAL > 0
"TSLICE",
#else
"STATE",
#endif
"FD"
);
cb(user);
}
print_state->running_count = 0;
print_state->blocked_count = 0;
print_state->total_user_time = 0;
for (int i = 0; i < CONFIG_MAX_TASKS; i++) {
sched_lock(); // need to lock the tcb access (but make it as short as possible)
if (!system_load.tasks[i].valid) {
sched_unlock();
continue;
}
unsigned tcb_pid = system_load.tasks[i].tcb->pid;
size_t stack_size = system_load.tasks[i].tcb->adj_stack_size;
ssize_t stack_free = 0;
char tcb_name[CONFIG_TASK_NAME_SIZE + 1];
strncpy(tcb_name, system_load.tasks[i].tcb->name, CONFIG_TASK_NAME_SIZE + 1);
#if CONFIG_ARCH_INTERRUPTSTACK > 3
if (system_load.tasks[i].tcb->pid == 0) {
stack_size = (CONFIG_ARCH_INTERRUPTSTACK & ~3);
stack_free = up_check_intstack_remain();
} else {
stack_free = up_check_tcbstack_remain(system_load.tasks[i].tcb);
}
#else
stack_free = up_check_tcbstack_remain(system_load.tasks[i].tcb);
#endif
#if CONFIG_ARCH_BOARD_SIM || !defined(CONFIG_PRIORITY_INHERITANCE)
#else
unsigned tcb_base_priority = system_load.tasks[i].tcb->base_priority;
#endif
#if CONFIG_RR_INTERVAL > 0
unsigned tcb_timeslice = system_load.tasks[i].tcb->timeslice;
#endif
tstate_t tcb_task_state = (tstate_t)system_load.tasks[i].tcb->task_state;
uint8_t tcb_sched_priority = system_load.tasks[i].tcb->sched_priority;
unsigned int tcb_num_used_fds = 0; // number of used file descriptors
#if CONFIG_NFILE_DESCRIPTORS > 0
FAR struct task_group_s *group = system_load.tasks[i].tcb->group;
if (group) {
for (int fd_index = 0; fd_index < CONFIG_NFILE_DESCRIPTORS; ++fd_index) {
if (group->tg_filelist.fl_files[fd_index].f_inode) {
++tcb_num_used_fds;
}
}
}
#endif
sched_unlock();
switch (tcb_task_state) {
case TSTATE_TASK_PENDING:
case TSTATE_TASK_READYTORUN:
case TSTATE_TASK_RUNNING:
print_state->running_count++;
break;
#ifndef CONFIG_DISABLE_SIGNALS
case TSTATE_WAIT_SIG:
#endif
#ifndef CONFIG_DISABLE_MQUEUE
case TSTATE_WAIT_MQNOTEMPTY:
case TSTATE_WAIT_MQNOTFULL:
#endif
#ifdef CONFIG_PAGING
case TSTATE_WAIT_PAGEFILL:
#endif
case TSTATE_TASK_INVALID:
case TSTATE_TASK_INACTIVE:
case TSTATE_WAIT_SEM:
print_state->blocked_count++;
break;
case TSTATE_TASK_STOPPED:
// DO NOTHING
break;
case NUM_TASK_STATES:
// DO NOTHING
break;
}
float current_load = 0.f;
if (total_runtime[i] > print_state->last_times[i]) {
const uint64_t interval_runtime = total_runtime[i] - print_state->last_times[i];
current_load = interval_runtime / print_state->interval_time_us;
if (tcb_pid == 0) {
idle_load = current_load;
} else {
print_state->total_user_time += interval_runtime;
}
}
print_state->last_times[i] = total_runtime[i];
if (print_state->new_time <= print_state->interval_start_time) {
continue; // not enough data yet
}
// print output
int print_len = snprintf(buffer, buffer_length, "%4d %-*s %8d %6.3f %5u/%5u %3u (%3u) ",
tcb_pid,
CONFIG_TASK_NAME_SIZE, tcb_name,
total_runtime[i] / 1000, // us -> ms
(double)(current_load * 100.f),
stack_size - stack_free,
stack_size,
tcb_sched_priority,
#if CONFIG_ARCH_BOARD_SIM || !defined(CONFIG_PRIORITY_INHERITANCE)
0);
#else
tcb_base_priority);
#endif
#if CONFIG_RR_INTERVAL > 0
/* print scheduling info with RR time slice */
snprintf(buffer + print_len, buffer_length - print_len, " %5d %2d", tcb_timeslice, tcb_num_used_fds);
(void)tstate_name(TSTATE_TASK_INVALID); // Stop not used warning
#else
// print task state instead
snprintf(buffer + print_len, buffer_length - print_len, " %-5s %2d", tstate_name(tcb_task_state), tcb_num_used_fds);
#endif
cb(user);
}
if (print_state->new_time <= print_state->interval_start_time) {
// first run, not enough data yet
return;
}
// Print footer
buffer[0] = 0;
cb(user);
float task_load = (float)(print_state->total_user_time) / print_state->interval_time_us;
/* this can happen if one tasks total runtime was not computed
correctly by the scheduler instrumentation TODO */
if (task_load > (1.f - idle_load)) {
task_load = (1.f - idle_load);
}
const float sched_load = 1.f - idle_load - task_load;
snprintf(buffer, buffer_length, "Processes: %d total, %d running, %d sleeping, max FDs: %d",
system_load.total_count,
print_state->running_count,
print_state->blocked_count,
CONFIG_NFILE_DESCRIPTORS);
cb(user);
snprintf(buffer, buffer_length, "CPU usage: %.2f%% tasks, %.2f%% sched, %.2f%% idle",
(double)(task_load * 100.f),
(double)(sched_load * 100.f),
(double)(idle_load * 100.f));
cb(user);
#if defined(BOARD_DMA_ALLOC_POOL_SIZE)
uint16_t dma_total;
uint16_t dma_used;
uint16_t dma_peak_used;
if (board_get_dma_usage(&dma_total, &dma_used, &dma_peak_used) >= 0) {
snprintf(buffer, buffer_length, "DMA Memory: %d total, %d used %d peak",
dma_total,
dma_used,
dma_peak_used);
cb(user);
}
#endif
snprintf(buffer, buffer_length, "Uptime: %.3fs total, %.3fs idle",
(double)print_state->new_time / 1e6, (double)total_runtime[0] / 1e6);
cb(user);
print_state->interval_start_time = print_state->new_time;
#pragma GCC diagnostic pop
}
struct print_load_callback_data_s {
int fd;
char buffer[140];
};
static void print_load_callback(void *user)
{
char clear_line[] {CL};
struct print_load_callback_data_s *data = (struct print_load_callback_data_s *)user;
if (data->fd != STDOUT_FILENO) {
clear_line[0] = '\0';
}
dprintf(data->fd, "%s%s\n", clear_line, data->buffer);
}
void print_load(int fd, struct print_load_s *print_state)
{
// print system information
if (fd == STDOUT_FILENO) {
// move cursor home and clear screen
dprintf(fd, "\033[H");
}
print_load_callback_data_s data{};
data.fd = fd;
print_load_buffer(data.buffer, sizeof(data.buffer), print_load_callback, &data, print_state);
}
#endif // if CONFIG_SCHED_INSTRUMENTATION
+1 -1
View File
@@ -38,7 +38,7 @@
#include <drivers/drv_hrt.h>
#include <lib/parameters/param.h>
#include <px4_platform_common/px4_work_queue/WorkQueueManager.hpp>
#include <systemlib/cpuload.h>
#include <px4_platform/cpuload.h>
#include <fcntl.h>
+3 -7
View File
@@ -604,16 +604,12 @@ hrt_elapsed_time_atomic(const volatile hrt_abstime *then)
/**
* Store the absolute time in an interrupt-safe fashion
*/
hrt_abstime
hrt_store_absolute_time(volatile hrt_abstime *now)
void
hrt_store_absolute_time(volatile hrt_abstime *t)
{
irqstate_t flags = px4_enter_critical_section();
hrt_abstime ts = hrt_absolute_time();
*t = hrt_absolute_time();
px4_leave_critical_section(flags);
return ts;
}
/**
@@ -616,16 +616,12 @@ hrt_elapsed_time_atomic(const volatile hrt_abstime *then)
/**
* Store the absolute time in an interrupt-safe fashion
*/
hrt_abstime
hrt_store_absolute_time(volatile hrt_abstime *now)
void
hrt_store_absolute_time(volatile hrt_abstime *t)
{
irqstate_t flags = px4_enter_critical_section();
hrt_abstime ts = hrt_absolute_time();
*t = hrt_absolute_time();
px4_leave_critical_section(flags);
return ts;
}
/**
@@ -604,16 +604,12 @@ hrt_elapsed_time_atomic(const volatile hrt_abstime *then)
/**
* Store the absolute time in an interrupt-safe fashion
*/
hrt_abstime
hrt_store_absolute_time(volatile hrt_abstime *now)
void
hrt_store_absolute_time(volatile hrt_abstime *t)
{
irqstate_t flags = px4_enter_critical_section();
hrt_abstime ts = hrt_absolute_time();
*t = hrt_absolute_time();
px4_leave_critical_section(flags);
return ts;
}
/**
@@ -737,16 +737,12 @@ hrt_elapsed_time_atomic(const volatile hrt_abstime *then)
/**
* Store the absolute time in an interrupt-safe fashion
*/
hrt_abstime
hrt_store_absolute_time(volatile hrt_abstime *now)
void
hrt_store_absolute_time(volatile hrt_abstime *t)
{
irqstate_t flags = px4_enter_critical_section();
hrt_abstime ts = hrt_absolute_time();
*t = hrt_absolute_time();
px4_leave_critical_section(flags);
return ts;
}
/**
@@ -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")
@@ -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
+2 -4
View File
@@ -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
@@ -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)
{
}
+2 -4
View File
@@ -156,13 +156,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.