]> git.ipfire.org Git - fireperf.git/commitdiff
stats: Refactor stats calculation
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Sep 2024 11:39:59 +0000 (11:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Sep 2024 11:39:59 +0000 (11:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/ctx.c
src/ctx.h
src/main.c
src/stats.c
src/stats.h

index f7a24af6b30c8a6717aaa0cf224c917dde573afe..501d7502ccc68e1f1f89a96729157e9febefc89d 100644 (file)
--- a/src/ctx.c
+++ b/src/ctx.c
@@ -270,6 +270,9 @@ int fireperf_ctx_create(struct fireperf_ctx** ctx, int argc, char* argv[]) {
                        goto ERROR;
        }
 
+       // Initialize the stats
+       c->stats = fireperf_ctx_get_stats(c);
+
        // Return the context
        *ctx = c;
 
@@ -298,8 +301,6 @@ static struct fireperf_worker* fireperf_ctx_find_idle_worker(struct fireperf_ctx
                if (!stats)
                        continue;
 
-               printf("C %lu\n", stats->open_connections);
-
                // If the worker has no open connections, we can use it
                if (stats->open_connections == 0)
                        return ctx->workers[i];
@@ -385,11 +386,19 @@ ERROR:
 
 struct fireperf_stats fireperf_ctx_get_stats(struct fireperf_ctx* ctx) {
        const struct fireperf_stats* s = NULL;
+       int r;
 
        struct fireperf_stats stats = {};
 
+       // Fetch the time
+       r = clock_gettime(CLOCK_REALTIME, &stats.t);
+       if (r) {
+               ERROR(ctx, "Could not fetch the time: %m\n");
+               return stats;
+       }
+
        // Add up everything from all workers
-       for (unsigned int i = 0; i < MAX_WORKERS; i++) {
+       for (unsigned int i = 0; i < ctx->num_workers; i++) {
                if (!ctx->workers[i])
                        continue;
 
@@ -410,3 +419,21 @@ struct fireperf_stats fireperf_ctx_get_stats(struct fireperf_ctx* ctx) {
 
        return stats;
 }
+
+int fireperf_ctx_dump_stats(struct fireperf_ctx* ctx) {
+       struct fireperf_stats stats = {};
+       int r;
+
+       // Fetch the current stats
+       stats = fireperf_ctx_get_stats(ctx);
+
+       // Dump the stats
+       r = fireperf_stats_dump(ctx, &ctx->stats, &stats);
+       if (r)
+               return r;
+
+       // Replace the stats for the next iteration
+       ctx->stats = stats;
+
+       return 0;
+}
index 23a8d136bc56c19cbfbbf4b9c9d6912d337d5bdd..e8fceb104e0864c6d290f505ab050fec96514c75 100644 (file)
--- a/src/ctx.h
+++ b/src/ctx.h
@@ -24,6 +24,7 @@
 #include <netinet/in.h>
 
 #include "constants.h"
+#include "stats.h"
 
 // Forward declarations
 struct fireperf_worker;
@@ -55,6 +56,9 @@ struct fireperf_ctx {
        struct fireperf_worker* workers[MAX_WORKERS];
        unsigned int num_workers;
        unsigned int max_workers;
+
+       // Stats
+       struct fireperf_stats stats;
 };
 
 #include "main.h"
@@ -66,4 +70,6 @@ void fireperf_ctx_free(struct fireperf_ctx* ctx);
 struct fireperf_worker* fireperf_ctx_fetch_worker(struct fireperf_ctx* ctx);
 struct fireperf_stats fireperf_ctx_get_stats(struct fireperf_ctx* ctx);
 
+int fireperf_ctx_dump_stats(struct fireperf_ctx* ctx);
+
 #endif /* FIREPERF_CTX_H */
index a8ba5707a569186aa84faac7b533eaa3bc1bcccc..d03017bfc4e126fbb5ef876c0ae583bcbdad5473 100644 (file)
@@ -94,7 +94,6 @@ static int set_limits(struct fireperf_ctx* ctx) {
 
 int main(int argc, char* argv[]) {
        struct fireperf_ctx* ctx = NULL;
-       struct fireperf_stats stats = { 0 };
        uint64_t expirations = 0;
        void* data = NULL;
        int epollfd = -1;
@@ -183,11 +182,8 @@ int main(int argc, char* argv[]) {
                                        goto ERROR;
                                }
 
-                               // Cumulate stats
-                               stats = fireperf_ctx_get_stats(ctx);
-
                                // Print the stats
-                               r = fireperf_dump_stats(ctx, &stats, ctx->mode);
+                               r = fireperf_ctx_dump_stats(ctx);
                                if (r)
                                        goto ERROR;
 
index d7a80f5800fc4e4b3b34f8e3b0f4901046bd3a64..f9629d36db2d6ff2ad84633ff05008cca5bb9bb7 100644 (file)
 #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);
+int fireperf_stats_dump(struct fireperf_ctx* ctx,
+               struct fireperf_stats* old, struct fireperf_stats* new) {
+       // Compute the delta since the last dump
+       double delta = timespec_delta(&new->t, &old->t);
 
        // Called too soon again?
        if (delta < 0.1)
                return 0;
 
+       // Compute the changes
+       const struct fireperf_stats stats = {
+               .t                    = new->t,
+               .open_connections     = new->open_connections,
+               .connections          = new->connections - old->connections,
+               .total_bytes_received = new->total_bytes_received,
+               .bytes_received       = new->total_bytes_received - old->total_bytes_received,
+               .total_bytes_sent     = new->total_bytes_sent,
+               .bytes_sent           = new->total_bytes_sent - old->total_bytes_sent,
+
+       };
+
        // Format timestamp
-       const char* timestamp = format_timespec(&now);
+       const char* timestamp = format_timespec(&stats.t);
 
        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);
+       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);
+       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);
@@ -66,8 +71,8 @@ int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats,
        }
 
        // 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);
+       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);
@@ -81,13 +86,5 @@ int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats,
        // 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;
 }
index 21c2449e95cb613937de420736afc20d451f9cd6..273fbdeba2d5b36688275e122c11a2b8869a35da 100644 (file)
@@ -25,7 +25,7 @@
 
 // Struct to collect statistics
 struct fireperf_stats {
-       struct timespec last_printed;
+       struct timespec t;
 
        // Total number of open connections
        unsigned int open_connections;
@@ -40,8 +40,10 @@ struct fireperf_stats {
        size_t total_bytes_sent;
 };
 
-#include "ctx.h"
+// Forward declararion
+struct fireperf_ctx;
 
-int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats, int mode);
+int fireperf_stats_dump(struct fireperf_ctx* ctx,
+       struct fireperf_stats* old, struct fireperf_stats* new);
 
 #endif /* FIREPERF_STATS_H */