]> git.ipfire.org Git - fireperf.git/commitdiff
server: Show how many bytes were received in total
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Jan 2021 15:50:56 +0000 (15:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Jan 2021 15:50:56 +0000 (15:50 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/server.c

index 92c4ff304a444180018e85a30733a53ad8ce01e7..cbaf7089714f1962ef3ac332915eb3d185b06f54 100644 (file)
@@ -19,6 +19,8 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <sys/epoll.h>
 #include <sys/timerfd.h>
@@ -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;
                                }