]> git.ipfire.org Git - fireperf.git/commitdiff
Move dumping stats into main.c
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Feb 2021 14:46:04 +0000 (14:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Feb 2021 14:46:04 +0000 (14:46 +0000)
The code is very common between client and server and therefore we can
share it better.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/client.c
src/main.c
src/main.h
src/server.c

index 57a0d1ff83b132acc2a70481445b9e6fa5c7a01b..7770c5c42c239bbe3ebab4b46bc2365e01ebfcb9 100644 (file)
@@ -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;
 
index c6b8e6dfc9d5649a044ef865c23f7d3b781f2f89..5fbeaa091a130a4cbe389cc5046f6d244cb18751 100644 (file)
@@ -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;
+}
index bb736aacf141efbc6bd5cf5026964f23d59d66bb..cbcebab8aa58a32fc3924db00eb4da9b66cfafe1 100644 (file)
@@ -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 */
index 9133a0d1624debaa3f3490aa60ce59d78bc86a3f..e2585c36e6b4b3cc41ebd2b28a301d1c5005b15c 100644 (file)
 
 #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;