From: Michael Tremer Date: Fri, 29 Jan 2021 15:50:56 +0000 (+0000) Subject: server: Show how many bytes were received in total X-Git-Tag: 0.1.0~33 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=044b792bc412034cdec2c65e915142af46f58d6e;p=fireperf.git server: Show how many bytes were received in total Signed-off-by: Michael Tremer --- diff --git a/src/server.c b/src/server.c index 92c4ff3..cbaf708 100644 --- a/src/server.c +++ b/src/server.c @@ -19,6 +19,8 @@ #############################################################################*/ #include +#include +#include #include #include #include @@ -37,8 +39,37 @@ struct fireperf_server_stats { // Total number of open connections unsigned int connections; + + size_t total_bytes_received; }; +static char* format_size(ssize_t size) { + const char* suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL }; + + const char** suffix; + char* retval = NULL; + + // Convert into double + double s = size; + + for (suffix = suffixes; *suffix; suffix++) { + if (abs(s) < 1024) + break; + + s /= 1024; + } + + if (!*suffix) + return NULL; + + // Format the output string + int r = asprintf(&retval, "%.02f %s", s, *suffix); + if (r < 0) + return NULL; + + return retval; +} + static char __timespec[20]; static const char* format_timespec(const struct timespec* t) { @@ -77,13 +108,21 @@ static int dump_stats(struct fireperf_config* conf, struct fireperf_server_stats // Format timestamp const char* timestamp = format_timespec(&now); + // Format total bytes received + char* total_bytes_received = format_size(stats->total_bytes_received); + INFO( conf, "--- %s --------------------\n", timestamp); DEBUG(conf, " %-20s: %19.4fs\n", "Delta", delta); INFO( conf, " %-20s: %20u\n", "Open Connection(s)", stats->connections); + INFO( conf, " %-20s: %20s\n", "Total Bytes Received", total_bytes_received); // Remember when this was printed last stats->last_printed = now; + // Cleanup + if (total_bytes_received) + free(total_bytes_received); + return 0; } @@ -145,7 +184,8 @@ static int accept_connection(struct fireperf_config* conf, int sockfd) { return fd; } -static int handle_io_on_connection(struct fireperf_config* conf, int fd) { +static int handle_io_on_connection(struct fireperf_config* conf, + struct fireperf_server_stats* stats, int fd) { char buffer[BUFFER_SIZE]; ssize_t bytes_read; @@ -162,11 +202,14 @@ static int handle_io_on_connection(struct fireperf_config* conf, int fd) { DEBUG(conf, "Read %zu bytes from socket %d\n", bytes_read, fd); + // Update statistics + stats->total_bytes_received += bytes_read; + return 0; } int fireperf_server(struct fireperf_config* conf) { - struct fireperf_server_stats stats; + struct fireperf_server_stats stats = { 0 }; DEBUG(conf, "Launching " PACKAGE_NAME " in server mode\n"); @@ -302,7 +345,7 @@ int fireperf_server(struct fireperf_config* conf) { // One of the connections had IO if (ev.events & EPOLLIN) { - r = handle_io_on_connection(conf, fd); + r = handle_io_on_connection(conf, &stats, fd); if (r < 0) goto ERROR; }