]> git.ipfire.org Git - fireperf.git/commitdiff
server: Compute delta in seconds between dump_stats() calls
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Jan 2021 15:33:03 +0000 (15:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Jan 2021 15:33:03 +0000 (15:33 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/server.c

index c6672f166cc5ae45a1f27eb3efc6f12ac58b0586..92c4ff304a444180018e85a30733a53ad8ce01e7 100644 (file)
@@ -33,6 +33,8 @@
 
 // Struct to collect server statistics
 struct fireperf_server_stats {
+       struct timespec last_printed;
+
        // Total number of open connections
        unsigned int connections;
 };
@@ -51,7 +53,16 @@ static const char* format_timespec(const struct timespec* t) {
        return NULL;
 }
 
-static int dump_stats(struct fireperf_config* conf, const struct fireperf_server_stats* stats) {
+static unsigned long timespec_delta(struct timespec* t1, struct timespec* t2) {
+       // Compute delta in milliseconds
+       return (
+               ((t1->tv_sec * 1000) + (t1->tv_nsec / 1000000))
+               -
+               ((t2->tv_sec * 1000) + (t2->tv_nsec / 1000000))
+       ) / 1000.0;
+}
+
+static int dump_stats(struct fireperf_config* conf, struct fireperf_server_stats* stats) {
        struct timespec now;
 
        // Fetch the time
@@ -61,11 +72,17 @@ static int dump_stats(struct fireperf_config* conf, const struct fireperf_server
                return 1;
        }
 
+       double delta = timespec_delta(&now, &stats->last_printed);
+
        // Format timestamp
        const char* timestamp = format_timespec(&now);
 
-       INFO(conf, "--- %s --------------------\n", timestamp);
-       INFO(conf, "  %-20s: %20u\n", "Open Connection(s)", stats->connections);
+       INFO( conf, "--- %s --------------------\n", timestamp);
+       DEBUG(conf, "  %-20s: %19.4fs\n", "Delta", delta);
+       INFO( conf, "  %-20s: %20u\n", "Open Connection(s)", stats->connections);
+
+       // Remember when this was printed last
+       stats->last_printed = now;
 
        return 0;
 }