]> git.ipfire.org Git - fireperf.git/commitdiff
stats: Move them into their own file
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Sep 2024 09:08:21 +0000 (09:08 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Sep 2024 09:08:21 +0000 (09:08 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/main.c
src/main.h
src/stats.c [new file with mode: 0644]
src/stats.h [new file with mode: 0644]

index 3a6550362b8489ab9a5b029673ca8434a74b0054..cda51372cf02517b4dd10ac1479ac0a20f730a59 100644 (file)
@@ -73,6 +73,8 @@ fireperf_SOURCES = \
        src/random.h \
        src/server.c \
        src/server.h \
+       src/stats.c \
+       src/stats.h \
        src/util.c \
        src/util.h \
        src/worker.c \
index a7468be94120e9bba6064e0da3dd08861d915d81..6ab10e2250ccbf0187642dee35fbeeed6d1768ce 100644 (file)
@@ -26,7 +26,6 @@
 #include <sys/resource.h>
 #include <sys/time.h>
 #include <sys/timerfd.h>
-#include <time.h>
 #include <unistd.h>
 
 #include "client.h"
@@ -34,7 +33,7 @@
 #include "logging.h"
 #include "random.h"
 #include "server.h"
-#include "util.h"
+#include "stats.h"
 
 static int set_limits(struct fireperf_ctx* ctx) {
        struct rlimit limit;
@@ -144,70 +143,6 @@ ERROR:
        return r;
 }
 
-int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats, int mode) {
-       struct timespec now;
-
-       // Fetch the time
-       int r = clock_gettime(CLOCK_REALTIME, &now);
-       if (r) {
-               ERROR(ctx, "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(ctx, "--- %s -------------------------\n", timestamp);
-       INFO(ctx, "                      : %12s %12s\n", "RX", "TX");
-       INFO(ctx, "  %-20s: %25u\n", "Open Connection(s)", stats->open_connections);
-       INFO(ctx, "  %-20s: %23.2f/s\n", "New Connections", stats->connections / delta);
-
-       // Show current bandwidth
-       char* bps_received = format_size(stats->bytes_received * 8 / delta, FIREPERF_FORMAT_BITS);
-       char* bps_sent     = format_size(stats->bytes_sent * 8 / delta, FIREPERF_FORMAT_BITS);
-
-       if (bps_received || bps_sent) {
-               INFO(ctx, "  %-20s: %10s/s %10s/s\n", "Current Bandwidth", bps_received, bps_sent);
-
-               if (bps_received)
-                       free(bps_received);
-               if (bps_sent)
-                       free(bps_sent);
-       }
-
-       // Total bytes
-       char* total_bytes_received = format_size(stats->total_bytes_received, FIREPERF_FORMAT_BYTES);
-       char* total_bytes_sent     = format_size(stats->total_bytes_sent, FIREPERF_FORMAT_BYTES);
-
-       if (total_bytes_received || total_bytes_sent) {
-               INFO(ctx, "  %-20s: %12s %12s\n", "Total Bytes", total_bytes_received, total_bytes_sent);
-
-               if (total_bytes_received)
-                       free(total_bytes_received);
-               if (total_bytes_sent)
-                       free(total_bytes_sent);
-       }
-
-       // Empty line
-       INFO(ctx, "\n");
-
-       // Remember when this was printed last
-       stats->last_printed = now;
-
-       // Reset statistics
-       stats->connections = 0;
-       stats->bytes_received = 0;
-       stats->bytes_sent = 0;
-
-       return 0;
-}
-
 int set_socket_buffer_sizes(struct fireperf_ctx* ctx, int fd) {
        int r;
 
index 203d9f127196b1799c8e36ba2e25bdae482e5a9c..f469144102f1764b82c51c0fe4e9732504513b54 100644 (file)
 #ifndef FIREPERF_MAIN_H
 #define FIREPERF_MAIN_H
 
-#include <time.h>
-
 #include "ctx.h"
-
-// Struct to collect statistics
-struct fireperf_stats {
-       struct timespec last_printed;
-
-       // Total number of open connections
-       unsigned int open_connections;
-
-       // New connections
-       unsigned int connections;
-
-       // Total transferred data
-       size_t bytes_received;
-       size_t total_bytes_received;
-       size_t bytes_sent;
-       size_t total_bytes_sent;
-};
-
-int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats, int mode);
+#include "stats.h"
 
 int set_socket_buffer_sizes(struct fireperf_ctx* ctx, int fd);
 
diff --git a/src/stats.c b/src/stats.c
new file mode 100644 (file)
index 0000000..d7a80f5
--- /dev/null
@@ -0,0 +1,93 @@
+/*#############################################################################
+#                                                                             #
+# fireperf - A network benchmarking tool                                      #
+# Copyright (C) 2024 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "ctx.h"
+#include "logging.h"
+#include "stats.h"
+#include "util.h"
+
+int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats, int mode) {
+       struct timespec now;
+
+       // Fetch the time
+       int r = clock_gettime(CLOCK_REALTIME, &now);
+       if (r) {
+               ERROR(ctx, "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(ctx, "--- %s -------------------------\n", timestamp);
+       INFO(ctx, "                      : %12s %12s\n", "RX", "TX");
+       INFO(ctx, "  %-20s: %25u\n", "Open Connection(s)", stats->open_connections);
+       INFO(ctx, "  %-20s: %23.2f/s\n", "New Connections", stats->connections / delta);
+
+       // Show current bandwidth
+       char* bps_received = format_size(stats->bytes_received * 8 / delta, FIREPERF_FORMAT_BITS);
+       char* bps_sent     = format_size(stats->bytes_sent * 8 / delta, FIREPERF_FORMAT_BITS);
+
+       if (bps_received || bps_sent) {
+               INFO(ctx, "  %-20s: %10s/s %10s/s\n", "Current Bandwidth", bps_received, bps_sent);
+
+               if (bps_received)
+                       free(bps_received);
+               if (bps_sent)
+                       free(bps_sent);
+       }
+
+       // Total bytes
+       char* total_bytes_received = format_size(stats->total_bytes_received, FIREPERF_FORMAT_BYTES);
+       char* total_bytes_sent     = format_size(stats->total_bytes_sent, FIREPERF_FORMAT_BYTES);
+
+       if (total_bytes_received || total_bytes_sent) {
+               INFO(ctx, "  %-20s: %12s %12s\n", "Total Bytes", total_bytes_received, total_bytes_sent);
+
+               if (total_bytes_received)
+                       free(total_bytes_received);
+               if (total_bytes_sent)
+                       free(total_bytes_sent);
+       }
+
+       // Empty line
+       INFO(ctx, "\n");
+
+       // Remember when this was printed last
+       stats->last_printed = now;
+
+       // Reset statistics
+       stats->connections = 0;
+       stats->bytes_received = 0;
+       stats->bytes_sent = 0;
+
+       return 0;
+}
diff --git a/src/stats.h b/src/stats.h
new file mode 100644 (file)
index 0000000..21c2449
--- /dev/null
@@ -0,0 +1,47 @@
+/*#############################################################################
+#                                                                             #
+# fireperf - A network benchmarking tool                                      #
+# Copyright (C) 2024 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef FIREPERF_STATS_H
+#define FIREPERF_STATS_H
+
+#include <time.h>
+
+// Struct to collect statistics
+struct fireperf_stats {
+       struct timespec last_printed;
+
+       // Total number of open connections
+       unsigned int open_connections;
+
+       // New connections
+       unsigned int connections;
+
+       // Total transferred data
+       size_t bytes_received;
+       size_t total_bytes_received;
+       size_t bytes_sent;
+       size_t total_bytes_sent;
+};
+
+#include "ctx.h"
+
+int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats, int mode);
+
+#endif /* FIREPERF_STATS_H */