diff --git a/dist/tools/zep_dispatch/main.c b/dist/tools/zep_dispatch/main.c index deafe6ef13..7764618804 100644 --- a/dist/tools/zep_dispatch/main.c +++ b/dist/tools/zep_dispatch/main.c @@ -133,15 +133,18 @@ static topology_t topology; static const char *graphviz_file = "example.gv"; static void _info_handler(int signal) { - if (signal != SIGUSR1) { - return; - } - - if (topology_print(graphviz_file, &topology)) { - fprintf(stderr, "can't open %s\n", graphviz_file); - } - else { - printf("graph written to %s\n", graphviz_file); + switch (signal) { + case SIGUSR1: + if (topology_print(graphviz_file, &topology)) { + fprintf(stderr, "can't open %s\n", graphviz_file); + } + else { + printf("graph written to %s\n", graphviz_file); + } + break; + case SIGUSR2: + topology_print_stats(&topology, true); + break; } } @@ -257,6 +260,7 @@ int main(int argc, char **argv) if (graphviz_file) { signal(SIGUSR1, _info_handler); } + signal(SIGUSR2, _info_handler); struct addrinfo *server_addr; int res = getaddrinfo(argv[0], argv[1], diff --git a/dist/tools/zep_dispatch/topology.c b/dist/tools/zep_dispatch/topology.c index 2fee4e6c2b..9f3c39f050 100644 --- a/dist/tools/zep_dispatch/topology.c +++ b/dist/tools/zep_dispatch/topology.c @@ -26,6 +26,8 @@ struct node { char name[NODE_NAME_MAX_LEN]; uint8_t mac[HW_ADDR_MAX_LEN]; struct sockaddr_in6 addr; + uint32_t num_tx; + uint32_t num_rx; uint8_t mac_len; }; @@ -182,6 +184,26 @@ int topology_print(const char *file, const topology_t *t) return 0; } +void topology_print_stats(const topology_t *t, bool reset) +{ + uint32_t tx_total = 0; + + puts("{ nodes: ["); + for (list_node_t *node = t->nodes.next; node; node = node->next) { + struct node *super = container_of(node, struct node, next); + + tx_total += super->num_tx; + + printf("\t{ name: %s, tx: %u, rx: %u }%c\n", + super->name, super->num_tx, super->num_rx, node->next ? ',' : ' '); + if (reset) { + super->num_tx = 0; + super->num_rx = 0; + } + } + printf("], tx_total: %u }\n", tx_total); +} + int topology_parse(const char *file, topology_t *out) { FILE *in; @@ -217,6 +239,8 @@ void topology_send(const topology_t *t, int sock, const struct sockaddr_in6 *src_addr, void *buffer, size_t len) { + struct node *sender = NULL; + if (t->has_sniffer) { sendto(sock, buffer, len, 0, (struct sockaddr *)&t->sniffer_addr, sizeof(t->sniffer_addr)); @@ -230,6 +254,11 @@ void topology_send(const topology_t *t, int sock, } if (memcmp(&super->a->addr, src_addr, sizeof(*src_addr)) == 0) { + if (sender == NULL) { + sender = super->a; + sender->num_tx++; + } + /* packet loss */ if (random() > super->weight_a_b * RAND_MAX) { continue; @@ -238,8 +267,14 @@ void topology_send(const topology_t *t, int sock, sendto(sock, buffer, len, 0, (struct sockaddr *)&super->b->addr, sizeof(super->b->addr)); + super->b->num_rx++; } else if (memcmp(&super->b->addr, src_addr, sizeof(*src_addr)) == 0) { + if (sender == NULL) { + sender = super->b; + sender->num_tx++; + } + /* packet loss */ if (random() > super->weight_b_a * RAND_MAX) { continue; @@ -248,6 +283,7 @@ void topology_send(const topology_t *t, int sock, sendto(sock, buffer, len, 0, (struct sockaddr *)&super->a->addr, sizeof(super->a->addr)); + super->a->num_rx++; } } } diff --git a/dist/tools/zep_dispatch/topology.h b/dist/tools/zep_dispatch/topology.h index 9b3d61d989..16733a29e2 100644 --- a/dist/tools/zep_dispatch/topology.h +++ b/dist/tools/zep_dispatch/topology.h @@ -48,6 +48,13 @@ int topology_parse(const char *file, topology_t *out); */ int topology_print(const char *file_out, const topology_t *t); +/** + * @brief Print send / receive statistics + * + * @param[in] t The topology to render + */ +void topology_print_stats(const topology_t *t, bool reset); + /** * @brief Populate a spot in the topology with a connected node *