From b774736f35ba12b2ff1e39f0fe3c8b6451e85dbe Mon Sep 17 00:00:00 2001 From: Atis Elsts Date: Sun, 9 Sep 2018 21:43:40 +0100 Subject: [PATCH] Add link stat packet counters --- os/net/link-stats.c | 59 ++++++++++++++++++++++++++++++++++++++++----- os/net/link-stats.h | 26 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/os/net/link-stats.c b/os/net/link-stats.c index 8ec372f4e..c11365b9a 100644 --- a/os/net/link-stats.c +++ b/os/net/link-stats.c @@ -37,12 +37,10 @@ #include "net/link-stats.h" #include -#define DEBUG 0 -#if DEBUG -#define PRINTF(...) printf(__VA_ARGS__) -#else -#define PRINTF(...) -#endif +/* Log configuration */ +#include "sys/log.h" +#define LOG_MODULE "Link Stats" +#define LOG_LEVEL LOG_LEVEL_MAC /* Maximum value for the Tx count counter */ #define TX_COUNT_MAX 32 @@ -82,6 +80,13 @@ link_stats_from_lladdr(const linkaddr_t *lladdr) return nbr_table_get_from_lladdr(link_stats, lladdr); } /*---------------------------------------------------------------------------*/ +/* Returns the neighbor's address given a link stats item */ +const linkaddr_t * +link_stats_get_lladdr(const struct link_stats *stat) +{ + return nbr_table_get_lladdr(link_stats, stat); +} +/*---------------------------------------------------------------------------*/ /* Are the statistics fresh? */ int link_stats_is_fresh(const struct link_stats *stats) @@ -156,6 +161,14 @@ link_stats_packet_sent(const linkaddr_t *lladdr, int status, int numtx) stats->last_tx_time = clock_time(); stats->freshness = MIN(stats->freshness + numtx, FRESHNESS_MAX); +#if LINK_STATS_PACKET_COUNTERS + /* Update paket counters */ + stats->cnt_current.num_packets_tx += numtx; + if(status == MAC_TX_OK) { + stats->cnt_current.num_packets_acked++; + } +#endif + /* Add penalty in case of no-ACK */ if(status == MAC_TX_NOACK) { numtx += ETX_NOACK_PENALTY; @@ -219,8 +232,38 @@ link_stats_input_callback(const linkaddr_t *lladdr) /* Update RSSI EWMA */ stats->rssi = ((int32_t)stats->rssi * (EWMA_SCALE - EWMA_ALPHA) + (int32_t)packet_rssi * EWMA_ALPHA) / EWMA_SCALE; + +#if LINK_STATS_PACKET_COUNTERS + stats->cnt_current.num_packets_rx++; +#endif } /*---------------------------------------------------------------------------*/ +#if LINK_STATS_PACKET_COUNTERS +/*---------------------------------------------------------------------------*/ +static void +print_and_update_counters(void) +{ + struct link_stats *stats; + + for(stats = nbr_table_head(link_stats); stats != NULL; + stats = nbr_table_next(link_stats, stats)) { + + struct link_packet_counter *c = &stats->cnt_current; + + LOG_INFO("num packets: tx=%u ack=%u rx=%u to=", + c->num_packets_tx, c->num_packets_acked, c->num_packets_rx); + LOG_INFO_LLADDR(link_stats_get_lladdr(stats)); + LOG_INFO_("\n"); + + stats->cnt_total.num_packets_tx += stats->cnt_current.num_packets_tx; + stats->cnt_total.num_packets_acked += stats->cnt_current.num_packets_acked; + stats->cnt_total.num_packets_rx += stats->cnt_current.num_packets_rx; + memset(&stats->cnt_current, 0, sizeof(stats->cnt_current)); + } +} +/*---------------------------------------------------------------------------*/ +#endif /* LINK_STATS_PACKET_COUNTERS */ +/*---------------------------------------------------------------------------*/ /* Periodic timer called at a period of FRESHNESS_HALF_LIFE */ static void periodic(void *ptr) @@ -231,6 +274,10 @@ periodic(void *ptr) for(stats = nbr_table_head(link_stats); stats != NULL; stats = nbr_table_next(link_stats, stats)) { stats->freshness >>= 1; } + +#if LINK_STATS_PACKET_COUNTERS + print_and_update_counters(); +#endif } /*---------------------------------------------------------------------------*/ /* Resets link-stats module */ diff --git a/os/net/link-stats.h b/os/net/link-stats.h index cca15fd32..51f1d285b 100644 --- a/os/net/link-stats.h +++ b/os/net/link-stats.h @@ -56,6 +56,25 @@ #define LINK_STATS_ETX_FROM_PACKET_COUNT 0 #endif /* LINK_STATS_ETX_FROM_PACKET_COUNT */ +/* Store and periodically print packet counters? */ +#ifdef LINK_STATS_CONF_PACKET_COUNTERS +#define LINK_STATS_PACKET_COUNTERS LINK_STATS_CONF_PACKET_COUNTERS +#else /* LINK_STATS_CONF_PACKET_COUNTERS */ +#define LINK_STATS_PACKET_COUNTERS 0 +#endif /* LINK_STATS_PACKET_COUNTERS */ + +typedef uint16_t link_packet_stat_t; + +struct link_packet_counter { + /* total attempts to transmit unicast packets */ + link_packet_stat_t num_packets_tx; + /* total ACKs for unicast packets */ + link_packet_stat_t num_packets_acked; + /* total number of unicast and broadcast packets received */ + link_packet_stat_t num_packets_rx; +}; + + /* All statistics of a given link */ struct link_stats { clock_time_t last_tx_time; /* Last Tx timestamp */ @@ -66,10 +85,17 @@ struct link_stats { uint8_t tx_count; /* Tx count, used for ETX calculation */ uint8_t ack_count; /* ACK count, used for ETX calculation */ #endif /* LINK_STATS_ETX_FROM_PACKET_COUNT */ + +#if LINK_STATS_PACKET_COUNTERS + struct link_packet_counter cnt_current; /* packets in the current period */ + struct link_packet_counter cnt_total; /* packets in total */ +#endif }; /* Returns the neighbor's link statistics */ const struct link_stats *link_stats_from_lladdr(const linkaddr_t *lladdr); +/* Returns the address of the neighbor */ +const linkaddr_t *link_stats_get_lladdr(const struct link_stats *); /* Are the statistics fresh? */ int link_stats_is_fresh(const struct link_stats *stats); /* Resets link-stats module */