Browse Source
This is to guard against busy-looping tasks, so that we at least have a log. In case the watchdog is triggered, it also logs CPU load of all tasks.sbg
7 changed files with 285 additions and 0 deletions
@ -0,0 +1,168 @@
@@ -0,0 +1,168 @@
|
||||
/****************************************************************************
|
||||
* |
||||
* Copyright (c) 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. |
||||
* |
||||
****************************************************************************/ |
||||
|
||||
#include "watchdog.h" |
||||
|
||||
#include <px4_log.h> |
||||
|
||||
using namespace time_literals; |
||||
|
||||
namespace px4 |
||||
{ |
||||
namespace logger |
||||
{ |
||||
|
||||
bool watchdog_update(watchdog_data_t &watchdog_data) |
||||
{ |
||||
|
||||
#ifdef __PX4_NUTTX |
||||
|
||||
if (system_load.initialized && watchdog_data.logger_main_task_index >= 0 |
||||
&& watchdog_data.logger_writer_task_index >= 0) { |
||||
const hrt_abstime now = hrt_absolute_time(); |
||||
const system_load_taskinfo_s &log_writer_task = system_load.tasks[watchdog_data.logger_writer_task_index]; |
||||
|
||||
if (log_writer_task.valid) { |
||||
// Trigger the watchdog if the log writer task has been ready to run for a
|
||||
// minimum duration and it has not been scheduled during that time.
|
||||
// When the writer is waiting for an SD transfer, it is not in ready state, thus a long dropout
|
||||
// will not trigger it. The longest period in ready state I measured was around 70ms,
|
||||
// after a param change.
|
||||
// We only check the log writer because it runs at lower priority than the main thread.
|
||||
// No need to lock the tcb access, since we are in IRQ context
|
||||
|
||||
// update the timestamp if it has been scheduled recently
|
||||
if (log_writer_task.curr_start_time > watchdog_data.ready_to_run_timestamp) { |
||||
watchdog_data.ready_to_run_timestamp = log_writer_task.curr_start_time; |
||||
} |
||||
|
||||
// update the timestamp if not ready to run or if transitioned into ready to run
|
||||
uint8_t current_state = log_writer_task.tcb->task_state; |
||||
|
||||
if (current_state != TSTATE_TASK_READYTORUN |
||||
|| (watchdog_data.last_state != TSTATE_TASK_READYTORUN && current_state == TSTATE_TASK_READYTORUN)) { |
||||
watchdog_data.ready_to_run_timestamp = now; |
||||
} |
||||
|
||||
watchdog_data.last_state = current_state; |
||||
|
||||
#if 0 // for debugging
|
||||
// test code that prints the maximum time in ready state
|
||||
static uint64_t max_time = 0; |
||||
|
||||
if (now - watchdog_data.ready_to_run_timestamp > max_time) { |
||||
max_time = now - watchdog_data.ready_to_run_timestamp; |
||||
} |
||||
|
||||
static int counter = 0; |
||||
|
||||
if (++counter > 300) { |
||||
PX4_ERR("max time in ready: %i ms", (int)max_time / 1000); |
||||
counter = 0; |
||||
max_time = 0; |
||||
} |
||||
|
||||
#endif |
||||
|
||||
if (now - watchdog_data.ready_to_run_timestamp > 1_s) { |
||||
PX4_ERR("watchdog triggered!"); // this will most likely not be logged due to dropouts
|
||||
|
||||
// boost the priority to make sure the logger continues to write to the log
|
||||
sched_param param{}; |
||||
param.sched_priority = SCHED_PRIORITY_MAX; |
||||
int ret; |
||||
|
||||
if (system_load.tasks[watchdog_data.logger_main_task_index].valid) { |
||||
ret = sched_setparam(system_load.tasks[watchdog_data.logger_main_task_index].tcb->pid, ¶m); |
||||
|
||||
if (ret < 0) { |
||||
PX4_ERR("sched_reprioritize failed (%i)", ret); |
||||
} |
||||
} |
||||
|
||||
ret = sched_setparam(log_writer_task.tcb->pid, ¶m); |
||||
|
||||
if (ret < 0) { |
||||
PX4_ERR("sched_reprioritize failed (%i)", ret); |
||||
} |
||||
|
||||
// make sure we won't trigger again
|
||||
watchdog_data.logger_main_task_index = -1; |
||||
return true; |
||||
} |
||||
|
||||
} else { |
||||
// should never happen
|
||||
watchdog_data.logger_main_task_index = -1; |
||||
} |
||||
} |
||||
|
||||
#endif /* __PX4_NUTTX */ |
||||
|
||||
return false; |
||||
|
||||
} |
||||
|
||||
void watchdog_initialize(const pid_t pid_logger_main, const pid_t pid_logger_writer, watchdog_data_t &watchdog_data) |
||||
{ |
||||
#ifdef __PX4_NUTTX |
||||
|
||||
sched_lock(); // need to lock the tcb access
|
||||
|
||||
for (int i = 0; i < CONFIG_MAX_TASKS; i++) { |
||||
if (system_load.tasks[i].valid) { |
||||
if (system_load.tasks[i].tcb->pid == pid_logger_writer) { |
||||
watchdog_data.logger_writer_task_index = i; |
||||
} |
||||
|
||||
if (system_load.tasks[i].tcb->pid == pid_logger_main) { |
||||
watchdog_data.logger_main_task_index = i; |
||||
} |
||||
} |
||||
} |
||||
|
||||
sched_unlock(); |
||||
|
||||
if (watchdog_data.logger_writer_task_index == -1 || |
||||
watchdog_data.logger_main_task_index == -1) { |
||||
// If we land here it means the NuttX implementation changed
|
||||
// and one of our assumptions is not valid anymore
|
||||
PX4_ERR("watchdog init failed"); |
||||
} |
||||
|
||||
#endif /* __PX4_NUTTX */ |
||||
} |
||||
|
||||
|
||||
} // namespace logger
|
||||
} // namespace px4
|
@ -0,0 +1,78 @@
@@ -0,0 +1,78 @@
|
||||
/****************************************************************************
|
||||
* |
||||
* Copyright (c) 2018 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 |
||||
|
||||
#include <drivers/drv_hrt.h> |
||||
|
||||
#ifdef __PX4_NUTTX |
||||
#include <sched.h> |
||||
#include <systemlib/cpuload.h> |
||||
#endif /* __PX4_NUTTX */ |
||||
|
||||
namespace px4 |
||||
{ |
||||
namespace logger |
||||
{ |
||||
|
||||
struct watchdog_data_t { |
||||
#ifdef __PX4_NUTTX |
||||
int logger_main_task_index = -1; |
||||
int logger_writer_task_index = -1; |
||||
hrt_abstime ready_to_run_timestamp = hrt_absolute_time(); |
||||
uint8_t last_state = TSTATE_TASK_INVALID; |
||||
#endif /* __PX4_NUTTX */ |
||||
}; |
||||
|
||||
|
||||
/**
|
||||
* Initialize the watchdog, fill in watchdog_data. |
||||
*/ |
||||
void watchdog_initialize(const pid_t pid_logger_main, const pid_t pid_logger_writer, watchdog_data_t &watchdog_data); |
||||
|
||||
/**
|
||||
* Update the watchdog and trigger it if necessary. It is triggered when the log writer task is in |
||||
* ready state for a certain period of time, but did not get scheduled. It means that most likely |
||||
* some other higher-prio task runs busy. |
||||
* When the watchdog triggers, it boosts the priority of the logger's main & writer tasks to maximum, so |
||||
* that they get scheduled again. |
||||
* |
||||
* Expected to be called from IRQ context. |
||||
* |
||||
* @param watchdog_data |
||||
* @return true if watchdog is triggered, false otherwise |
||||
*/ |
||||
bool watchdog_update(watchdog_data_t &watchdog_data); |
||||
|
||||
} //namespace logger
|
||||
} //namespace px4
|
Loading…
Reference in new issue