goto ERROR;
}
+ // Initialize the stats
+ c->stats = fireperf_ctx_get_stats(c);
+
// Return the context
*ctx = c;
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];
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;
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;
+}
#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);
}
// 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);
// 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;
}