|
| 1 | +#include "RoutingStatsModule.h" |
| 2 | +#include "MeshService.h" |
| 3 | +#include "NodeDB.h" |
| 4 | +#include "airtime.h" |
| 5 | + |
| 6 | +RoutingStatsModule *routingStats; |
| 7 | + |
| 8 | +#define PRINT_STATS_INTERVAL_MS (60 * 1000) // Print our own stats every 60 seconds |
| 9 | + |
| 10 | +/** |
| 11 | + * Log a routing event |
| 12 | + */ |
| 13 | +void RoutingStatsModule::logEvent(RoutingEvent event, meshtastic_MeshPacket *p, uint32_t detail) |
| 14 | +{ |
| 15 | + switch (event) { |
| 16 | + case RoutingEvent::TX_OK: |
| 17 | + stats.tx_total++; |
| 18 | + stats.tx_total_ms += detail; |
| 19 | + if (isFromUs(p)) { |
| 20 | + stats.tx_mine++; |
| 21 | + if (!p->hop_start) |
| 22 | + stats.tx_zero++; |
| 23 | + } else |
| 24 | + stats.tx_relayed++; |
| 25 | + break; |
| 26 | + case RoutingEvent::TX_DROP: |
| 27 | + stats.tx_dropped++; |
| 28 | + break; |
| 29 | + case RoutingEvent::TX_DEFER: |
| 30 | + stats.tx_deferred++; |
| 31 | + break; |
| 32 | + case RoutingEvent::TX_HWM: |
| 33 | + if (detail > stats.tx_hwm) |
| 34 | + stats.tx_hwm = detail; |
| 35 | + break; |
| 36 | + case RoutingEvent::RX_OK: |
| 37 | + stats.rx_total++; |
| 38 | + stats.rx_total_ms += detail; |
| 39 | + if (p) { |
| 40 | + if (p->hop_limit == p->hop_start) { |
| 41 | + if (!p->hop_start) |
| 42 | + stats.rx_zero++; |
| 43 | + else |
| 44 | + stats.rx_direct++; |
| 45 | + } else if (!p->hop_limit) |
| 46 | + stats.rx_eol++; |
| 47 | + } |
| 48 | + break; |
| 49 | + case RoutingEvent::RX_BAD: |
| 50 | + stats.rx_bad++; |
| 51 | + stats.rx_total_ms += detail; |
| 52 | + break; |
| 53 | + default: |
| 54 | + LOG_WARN("Unknown routing event %d", static_cast<int>(event)); |
| 55 | + break; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Print routing stats to the console |
| 61 | + */ |
| 62 | +void RoutingStatsModule::printStats(meshtastic_RoutingStats *stats, NodeNum src) |
| 63 | +{ |
| 64 | + const meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(src); |
| 65 | + const char *short_name = (node && node->has_user) ? node->user.short_name : "????"; |
| 66 | + |
| 67 | + LOG_INFO("Routing stats from %s (0x%08x), last %lu seconds", short_name, src, stats->window_secs); |
| 68 | + LOG_INFO(" Airtime: chutil=%lu%% duty=%lu%% rx=%lums tx=%lums", stats->channel_util_pct, stats->tx_duty_pct, |
| 69 | + stats->rx_total_ms, stats->tx_total_ms); |
| 70 | + LOG_INFO(" TX: packets=%lu relayed=%lu mine=%lu zero-hop=%lu dropped=%lu hwm=%lu", stats->tx_total, stats->tx_relayed, |
| 71 | + stats->tx_mine, stats->tx_zero, stats->tx_dropped, stats->tx_hwm); |
| 72 | + LOG_INFO(" RX: packets=%lu bad=%lu direct=%lu zero-hop=%lu eol=%lu", stats->rx_total, stats->rx_bad, stats->rx_direct, |
| 73 | + stats->rx_zero, stats->rx_eol); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Handle an incoming routing stats protobuf |
| 78 | + */ |
| 79 | +bool RoutingStatsModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_RoutingStats *decoded) |
| 80 | +{ |
| 81 | + printStats(decoded, getFrom(&mp)); |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Periodic thread wakeup |
| 87 | + */ |
| 88 | +int32_t RoutingStatsModule::runOnce() |
| 89 | +{ |
| 90 | + unsigned long now = millis(); |
| 91 | + unsigned long next_tx_millis = config.device.routing_stats_broadcast_secs |
| 92 | + ? (last_tx_millis + config.device.routing_stats_broadcast_secs * 1000) |
| 93 | + : INT32_MAX; |
| 94 | + unsigned long next_print_millis = last_print_millis + PRINT_STATS_INTERVAL_MS; |
| 95 | + |
| 96 | + // Update 'now' fields |
| 97 | + stats.window_secs = (now - last_tx_millis) / 1000; |
| 98 | + stats.channel_util_pct = airTime->channelUtilizationPercent(); |
| 99 | + stats.tx_duty_pct = airTime->utilizationTXPercent(); |
| 100 | + |
| 101 | + if (now >= next_print_millis) { |
| 102 | + printStats(&stats, nodeDB->getNodeNum()); |
| 103 | + last_print_millis = now; |
| 104 | + next_print_millis = last_print_millis + PRINT_STATS_INTERVAL_MS; |
| 105 | + } |
| 106 | + |
| 107 | + if (next_tx_millis > now) |
| 108 | + return (next_tx_millis > next_print_millis ? next_print_millis : next_tx_millis) - now; |
| 109 | + |
| 110 | + LOG_DEBUG("Broadcast routing stats for last %d seconds", stats.window_secs); |
| 111 | + meshtastic_MeshPacket *p = allocDataProtobuf(stats); |
| 112 | + p->to = NODENUM_BROADCAST; |
| 113 | + service->sendToMesh(p); |
| 114 | + |
| 115 | + stats = {}; |
| 116 | + last_tx_millis = now; |
| 117 | + next_tx_millis = last_tx_millis + config.device.routing_stats_broadcast_secs * 1000; |
| 118 | + |
| 119 | + return (next_tx_millis > next_print_millis ? next_print_millis : next_tx_millis) - now; |
| 120 | +} |
0 commit comments