#############################################################################*/
#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
// Total number of open connections
unsigned int connections;
+
+ size_t total_bytes_received;
};
+static char* format_size(ssize_t size) {
+ const char* suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL };
+
+ const char** suffix;
+ char* retval = NULL;
+
+ // Convert into double
+ double s = size;
+
+ for (suffix = suffixes; *suffix; suffix++) {
+ if (abs(s) < 1024)
+ break;
+
+ s /= 1024;
+ }
+
+ if (!*suffix)
+ return NULL;
+
+ // Format the output string
+ int r = asprintf(&retval, "%.02f %s", s, *suffix);
+ if (r < 0)
+ return NULL;
+
+ return retval;
+}
+
static char __timespec[20];
static const char* format_timespec(const struct timespec* t) {
// Format timestamp
const char* timestamp = format_timespec(&now);
+ // Format total bytes received
+ char* total_bytes_received = format_size(stats->total_bytes_received);
+
INFO( conf, "--- %s --------------------\n", timestamp);
DEBUG(conf, " %-20s: %19.4fs\n", "Delta", delta);
INFO( conf, " %-20s: %20u\n", "Open Connection(s)", stats->connections);
+ INFO( conf, " %-20s: %20s\n", "Total Bytes Received", total_bytes_received);
// Remember when this was printed last
stats->last_printed = now;
+ // Cleanup
+ if (total_bytes_received)
+ free(total_bytes_received);
+
return 0;
}
return fd;
}
-static int handle_io_on_connection(struct fireperf_config* conf, int fd) {
+static int handle_io_on_connection(struct fireperf_config* conf,
+ struct fireperf_server_stats* stats, int fd) {
char buffer[BUFFER_SIZE];
ssize_t bytes_read;
DEBUG(conf, "Read %zu bytes from socket %d\n", bytes_read, fd);
+ // Update statistics
+ stats->total_bytes_received += bytes_read;
+
return 0;
}
int fireperf_server(struct fireperf_config* conf) {
- struct fireperf_server_stats stats;
+ struct fireperf_server_stats stats = { 0 };
DEBUG(conf, "Launching " PACKAGE_NAME " in server mode\n");
// One of the connections had IO
if (ev.events & EPOLLIN) {
- r = handle_io_on_connection(conf, fd);
+ r = handle_io_on_connection(conf, &stats, fd);
if (r < 0)
goto ERROR;
}