From: Michael Tremer Date: Thu, 4 Feb 2021 14:46:04 +0000 (+0000) Subject: Move dumping stats into main.c X-Git-Tag: 0.1.0~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ee4deba7213d95c46eac00d559a5328a7f86e50f;p=fireperf.git Move dumping stats into main.c The code is very common between client and server and therefore we can share it better. Signed-off-by: Michael Tremer --- diff --git a/src/client.c b/src/client.c index 57a0d1f..7770c5c 100644 --- a/src/client.c +++ b/src/client.c @@ -96,53 +96,6 @@ static const char* fireperf_random_pool_get_slice(struct fireperf_random_pool* p return pool->data + offset; } -static int dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats) { - struct timespec now; - - // Fetch the time - int r = clock_gettime(CLOCK_REALTIME, &now); - if (r) { - ERROR(conf, "Could not fetch the time: %s\n", strerror(errno)); - return 1; - } - - double delta = timespec_delta(&now, &stats->last_printed); - - // Called too soon again? - if (delta < 0.1) - return 0; - - // Format timestamp - const char* timestamp = format_timespec(&now); - - // Format total bytes sent - char* total_bytes_sent = format_size(stats->total_bytes_sent, FIREPERF_FORMAT_BYTES); - - // Calculate bandwidth - char* bps = format_size(stats->bytes_sent * 8 / delta, FIREPERF_FORMAT_BITS); - - INFO(conf, "--- %s --------------------\n", timestamp); - INFO(conf, " %-20s: %16.2f/s\n", "New Connections", stats->connections / delta); - INFO(conf, " %-20s: %18s/s\n", "Current Bandwidth", bps); - INFO(conf, " %-20s: %20s\n", "Total Bytes Sent", total_bytes_sent); - - // Remember when this was printed last - stats->last_printed = now; - - // Reset statistics - stats->connections = 0; - stats->bytes_sent = 0; - - // Cleanup - if (bps) - free(bps); - - if (total_bytes_sent) - free(total_bytes_sent); - - return 0; -} - static int open_connection(struct fireperf_config* conf) { // Open a new socket int fd = socket(AF_INET6, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); @@ -339,7 +292,7 @@ int fireperf_client(struct fireperf_config* conf, struct fireperf_stats* stats, goto ERROR; } - r = dump_stats(conf, stats); + r = fireperf_dump_stats(conf, stats, FIREPERF_MODE_CLIENT); if (r) goto ERROR; diff --git a/src/main.c b/src/main.c index c6b8e6d..5fbeaa0 100644 --- a/src/main.c +++ b/src/main.c @@ -36,6 +36,7 @@ #include "main.h" #include "logging.h" #include "server.h" +#include "util.h" static int parse_address(const char* string, struct in6_addr* address6) { // Try parsing this address @@ -323,3 +324,73 @@ ERROR: return r; } + + +int fireperf_dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats, int mode) { + struct timespec now; + + // Fetch the time + int r = clock_gettime(CLOCK_REALTIME, &now); + if (r) { + ERROR(conf, "Could not fetch the time: %s\n", strerror(errno)); + return 1; + } + + double delta = timespec_delta(&now, &stats->last_printed); + + // Called too soon again? + if (delta < 0.1) + return 0; + + // Format timestamp + const char* timestamp = format_timespec(&now); + + INFO( conf, "--- %s --------------------\n", timestamp); + DEBUG(conf, " %-20s: %19.4fs\n", "Delta", delta); + INFO( conf, " %-20s: %20u\n", "Open Connection(s)", stats->open_connections); + INFO( conf, " %-20s: %18.2f/s\n", "New Connections", stats->connections / delta); + + // Show current bandwidth + char* bps = NULL; + switch (mode) { + case FIREPERF_MODE_CLIENT: + bps = format_size(stats->bytes_sent * 8 / delta, FIREPERF_FORMAT_BITS); + break; + + case FIREPERF_MODE_SERVER: + bps = format_size(stats->bytes_received * 8 / delta, FIREPERF_FORMAT_BITS); + break; + } + + if (bps) { + INFO( conf, " %-20s: %18s/s\n", "Current Bandwidth", bps); + free(bps); + } + + // Total bytes sent/received + char* total_bytes = NULL; + switch (mode) { + case FIREPERF_MODE_CLIENT: + total_bytes = format_size(stats->total_bytes_sent, FIREPERF_FORMAT_BYTES); + INFO(conf, " %-20s: %20s\n", "Total Bytes Sent", total_bytes); + break; + + case FIREPERF_MODE_SERVER: + total_bytes = format_size(stats->total_bytes_received, FIREPERF_FORMAT_BYTES); + INFO(conf, " %-20s: %20s\n", "Total Bytes Received", total_bytes); + break; + } + + // Remember when this was printed last + stats->last_printed = now; + + // Reset statistics + stats->connections = 0; + stats->bytes_received = 0; + + // Cleanup + if (total_bytes) + free(total_bytes); + + return 0; +} diff --git a/src/main.h b/src/main.h index bb736aa..cbcebab 100644 --- a/src/main.h +++ b/src/main.h @@ -80,4 +80,6 @@ struct fireperf_stats { size_t total_bytes_sent; }; +int fireperf_dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats, int mode); + #endif /* FIREPERF_MAIN_H */ diff --git a/src/server.c b/src/server.c index 9133a0d..e2585c3 100644 --- a/src/server.c +++ b/src/server.c @@ -33,55 +33,6 @@ #define SOCKET_BACKLOG 1024 -static int dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats) { - struct timespec now; - - // Fetch the time - int r = clock_gettime(CLOCK_REALTIME, &now); - if (r) { - ERROR(conf, "Could not fetch the time: %s\n", strerror(errno)); - return 1; - } - - double delta = timespec_delta(&now, &stats->last_printed); - - // Called too soon again? - if (delta < 0.1) - return 0; - - // Format timestamp - const char* timestamp = format_timespec(&now); - - // Format total bytes received - char* total_bytes_received = format_size(stats->total_bytes_received, FIREPERF_FORMAT_BYTES); - - // Calculate bandwidth - char* bps = format_size(stats->bytes_received * 8 / delta, FIREPERF_FORMAT_BITS); - - INFO( conf, "--- %s --------------------\n", timestamp); - DEBUG(conf, " %-20s: %19.4fs\n", "Delta", delta); - INFO( conf, " %-20s: %20u\n", "Open Connection(s)", stats->open_connections); - INFO( conf, " %-20s: %16.2f/s\n", "New Connections", stats->connections / delta); - INFO( conf, " %-20s: %18s/s\n", "Current Bandwidth", bps); - INFO( conf, " %-20s: %20s\n", "Total Bytes Received", total_bytes_received); - - // Remember when this was printed last - stats->last_printed = now; - - // Reset statistics - stats->connections = 0; - stats->bytes_received = 0; - - // Cleanup - if (bps) - free(bps); - - if (total_bytes_received) - free(total_bytes_received); - - return 0; -} - static int create_socket(struct fireperf_config* conf, int i) { int r; @@ -261,7 +212,7 @@ int fireperf_server(struct fireperf_config* conf, struct fireperf_stats* stats, goto ERROR; } - r = dump_stats(conf, stats); + r = fireperf_dump_stats(conf, stats, FIREPERF_MODE_SERVER); if (r) goto ERROR;