diff --git a/docs/power.md b/docs/power.md index eca8d576..ea63c4e8 100644 --- a/docs/power.md +++ b/docs/power.md @@ -171,6 +171,34 @@ a half-drained `rxlog`/`pktlog` page would otherwise wait for the next connection event — the bridge drains those on a 2-minute duty cycle, so a slower drain directly costs collected frames. +### CPU time per received packet (`PWR_DUTY_INSTRUMENT`) + +A bench build with `-D PWR_DUTY_INSTRUMENT` (nRF52 only, nothing changes +without the flag) prints one line to the USB console every ~10 s: + +``` +PWRDUTY awake_ms=… asleep_ms=… wakes=… duty_permille=… pkt_us=… pkt_n=… +``` + +- `awake_ms`, `asleep_ms` and `duty_permille` are the time between a return + from `NRF52Board::sleep()` and the next entry into it, against the WFE span + inside it. This needs `powersaving on`: without it `sleep()` is never called. +- `wakes` counts the passes through `sleep()`. BLE and USB events wake the + core too, so the duty is not packet cost alone. +- `pkt_us` and `pkt_n` are the CPU time spent in + `Dispatcher::processRecvPacket()` (dedup, crypto, routing) and the number of + packets. They do not depend on background wakes. This is the work a filter + on the radio side could skip for a packet that is dropped anyway. + +All counters are cumulative since boot, so take the difference between two +lines. + +Measured on the XIAO x4, 24. 11. 2026, live mesh, about 440 packets/h (all +flood adverts): about **6.8 ms CPU per received packet**, mostly the Ed25519 +check of the advert signature. The duty went from 0.05 % at rest to 0.13 %. +The awake time is a small part of a node's current; the radio's RX and the +SoftDevice dominate. + ## A mast that runs flat: bootlock and cutoff A panel that gets no direct sun in winter (remote-mast faces east, 11/2026: diff --git a/docs/tth-commands.md b/docs/tth-commands.md index b9674c78..d5afe427 100644 --- a/docs/tth-commands.md +++ b/docs/tth-commands.md @@ -210,6 +210,7 @@ The ones that change what a node does; set in `platformio.ini` / | `LOGIN_FAIL_MAX`, `LOGIN_FAIL_SECS` | `4`, `180` (s) | after that many wrong passwords in the window no password is checked until it ends (an admin already in the ACL always gets in) | | `NRF52_POWER_MANAGEMENT`, `PWRMGT_RUNTIME` | upstream: off; on in `SenseCap_Solar_repeater` (the masts) | boot protection, and with `PWRMGT_RUNTIME` the runtime `pwr.bootlock` / `pwr.cutoff` above | | `PWRMGT_BOOTLOCK_DEFAULT`, `PWRMGT_CUTOFF_DEFAULT` | `PWRMGT_VOLTAGE_BOOTLOCK` (SenseCAP 3300), `0` (mV) | thresholds for a node without `/pwr_guard` | +| `PWR_DUTY_INSTRUMENT` | unset | bench only, nRF52: every ~10 s a `PWRDUTY` line on the USB console with the time the MCU was awake and asleep, its wakes, and the CPU time spent on received packets. See [power.md](power.md#cpu-time-per-received-packet-pwr_duty_instrument) | Prefs saved on the node override the build defaults: a reflash keeps what a node already has; only a new node or one after `erase` takes them. diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index 14ad28d9..31dbae00 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -6,6 +6,16 @@ #include +#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 +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() { diff --git a/src/helpers/NRF52Board.cpp b/src/helpers/NRF52Board.cpp index bc817d67..8457f21f 100644 --- a/src/helpers/NRF52Board.cpp +++ b/src/helpers/NRF52Board.cpp @@ -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