Bench: opt-in nRF52 instrument for MCU awake time and CPU time per received packet

Changelog: -
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-30 12:30:27 +01:00
1 parent 22700192bd
commit 7a7c894e2d
4 files changed
+85 -1

No files matched your search

+17
View File
@@ -6,6 +6,16 @@
#include <math.h>
#ifdef PWR_DUTY_INSTRUMENT
// CUSTOM (TeTeHacko): bench instrument. Pure CPU time spent processing a received
// packet (dedup + crypto + routing), independent of BLE/USB background wakes --
// unlike the sleep-duty measure. This is the work a radio-side filter could skip
// for a dropped packet. Printed by NRF52Board::sleep() (extern below).
#include <Arduino.h>
uint64_t g_pkt_proc_us = 0;
uint32_t g_pkt_proc_count = 0;
#endif
namespace mesh {
#define MAX_RX_DELAY_MILLIS 32000 // 32 seconds
@@ -273,6 +283,9 @@ void Dispatcher::checkRecv() {
}
void Dispatcher::processRecvPacket(Packet* pkt) {
#ifdef PWR_DUTY_INSTRUMENT
uint32_t _pwr_t0 = micros();
#endif
DispatcherAction action = onRecvPacket(pkt);
if (action == ACTION_RELEASE) {
_mgr->free(pkt);
@@ -284,6 +297,10 @@ void Dispatcher::processRecvPacket(Packet* pkt) {
_mgr->queueOutbound(pkt, priority, futureMillis(_delay));
}
#ifdef PWR_DUTY_INSTRUMENT
g_pkt_proc_us += (uint32_t)(micros() - _pwr_t0);
g_pkt_proc_count++;
#endif
}
void Dispatcher::checkSend() {
+39 -1
View File
@@ -328,11 +328,27 @@ bool NRF52Board::isExternalPowered() {
}
}
#ifdef PWR_DUTY_INSTRUMENT
// CUSTOM (TeTeHacko): bench instrument (opt-in, -D PWR_DUTY_INSTRUMENT). Measures
// what share of wall time the MCU is awake: the loop calls sleep() every pass, so
// the gap between a sleep() return and the next sleep() entry is CPU-awake time,
// and the WFE span inside is asleep. Combined with `recv` from stats-packets it
// gives the CPU cost per received packet (the part a radio-side filter could cut).
// Prints one line to Serial every ~10 s. No effect unless the flag is set.
static uint64_t s_awake_us = 0, s_asleep_us = 0;
static uint32_t s_last_exit_us = 0, s_wakes = 0, s_report_us = 0;
static bool s_have_exit = false;
// Pure per-packet CPU time, measured in Dispatcher::processRecvPacket (immune to
// BLE/USB background wakes, unlike the awake/asleep duty above).
extern uint64_t g_pkt_proc_us;
extern uint32_t g_pkt_proc_count;
#endif
void NRF52Board::sleep(uint32_t secs) {
// Clear FPU interrupt flags to avoid insomnia
// see errata 87 for details https://docs.nordicsemi.com/bundle/errata_nRF52840_Rev3/page/ERR/nRF52840/Rev3/latest/anomaly_840_87.html
#if (__FPU_USED == 1)
__set_FPSCR(__get_FPSCR() & ~(0x0000009F));
__set_FPSCR(__get_FPSCR() & ~(0x0000009F));
(void) __get_FPSCR();
NVIC_ClearPendingIRQ(FPU_IRQn);
#endif
@@ -350,6 +366,11 @@ void NRF52Board::sleep(uint32_t secs) {
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
#ifdef PWR_DUTY_INSTRUMENT
uint32_t t_enter = micros();
if (s_have_exit) s_awake_us += (uint32_t)(t_enter - s_last_exit_us);
#endif
if (sd_enabled) {
// first call processes pending softdevice events, second call sleeps.
sd_app_evt_wait();
@@ -360,6 +381,23 @@ void NRF52Board::sleep(uint32_t secs) {
__WFE();
__WFE();
}
#ifdef PWR_DUTY_INSTRUMENT
uint32_t t_exit = micros();
s_asleep_us += (uint32_t)(t_exit - t_enter);
s_last_exit_us = t_exit;
s_have_exit = true;
s_wakes++;
if ((uint32_t)(t_exit - s_report_us) >= 10000000UL) {
s_report_us = t_exit;
uint64_t tot = s_awake_us + s_asleep_us;
uint32_t permille = tot ? (uint32_t)((s_awake_us * 1000) / tot) : 0;
Serial.printf("PWRDUTY awake_ms=%lu asleep_ms=%lu wakes=%lu duty_permille=%lu pkt_us=%lu pkt_n=%lu\r\n",
(unsigned long)(s_awake_us / 1000), (unsigned long)(s_asleep_us / 1000),
(unsigned long)s_wakes, (unsigned long)permille,
(unsigned long)(g_pkt_proc_us), (unsigned long)g_pkt_proc_count);
}
#endif
}
// Temperature from NRF52 MCU