return pool->data + offset;
}
-static int dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats) {
- struct timespec now;
-
- // Fetch the time
- int r = clock_gettime(CLOCK_REALTIME, &now);
- if (r) {
- ERROR(conf, "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);
-
- // Format total bytes sent
- char* total_bytes_sent = format_size(stats->total_bytes_sent, FIREPERF_FORMAT_BYTES);
-
- // Calculate bandwidth
- char* bps = format_size(stats->bytes_sent * 8 / delta, FIREPERF_FORMAT_BITS);
-
- INFO(conf, "--- %s --------------------\n", timestamp);
- INFO(conf, " %-20s: %16.2f/s\n", "New Connections", stats->connections / delta);
- INFO(conf, " %-20s: %18s/s\n", "Current Bandwidth", bps);
- INFO(conf, " %-20s: %20s\n", "Total Bytes Sent", total_bytes_sent);
-
- // Remember when this was printed last
- stats->last_printed = now;
-
- // Reset statistics
- stats->connections = 0;
- stats->bytes_sent = 0;
-
- // Cleanup
- if (bps)
- free(bps);
-
- if (total_bytes_sent)
- free(total_bytes_sent);
-
- return 0;
-}
-
static int open_connection(struct fireperf_config* conf) {
// Open a new socket
int fd = socket(AF_INET6, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
goto ERROR;
}
- r = dump_stats(conf, stats);
+ r = fireperf_dump_stats(conf, stats, FIREPERF_MODE_CLIENT);
if (r)
goto ERROR;
#include "main.h"
#include "logging.h"
#include "server.h"
+#include "util.h"
static int parse_address(const char* string, struct in6_addr* address6) {
// Try parsing this address
return r;
}
+
+
+int fireperf_dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats, int mode) {
+ struct timespec now;
+
+ // Fetch the time
+ int r = clock_gettime(CLOCK_REALTIME, &now);
+ if (r) {
+ ERROR(conf, "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( conf, "--- %s --------------------\n", timestamp);
+ DEBUG(conf, " %-20s: %19.4fs\n", "Delta", delta);
+ INFO( conf, " %-20s: %20u\n", "Open Connection(s)", stats->open_connections);
+ INFO( conf, " %-20s: %18.2f/s\n", "New Connections", stats->connections / delta);
+
+ // Show current bandwidth
+ char* bps = NULL;
+ switch (mode) {
+ case FIREPERF_MODE_CLIENT:
+ bps = format_size(stats->bytes_sent * 8 / delta, FIREPERF_FORMAT_BITS);
+ break;
+
+ case FIREPERF_MODE_SERVER:
+ bps = format_size(stats->bytes_received * 8 / delta, FIREPERF_FORMAT_BITS);
+ break;
+ }
+
+ if (bps) {
+ INFO( conf, " %-20s: %18s/s\n", "Current Bandwidth", bps);
+ free(bps);
+ }
+
+ // Total bytes sent/received
+ char* total_bytes = NULL;
+ switch (mode) {
+ case FIREPERF_MODE_CLIENT:
+ total_bytes = format_size(stats->total_bytes_sent, FIREPERF_FORMAT_BYTES);
+ INFO(conf, " %-20s: %20s\n", "Total Bytes Sent", total_bytes);
+ break;
+
+ case FIREPERF_MODE_SERVER:
+ total_bytes = format_size(stats->total_bytes_received, FIREPERF_FORMAT_BYTES);
+ INFO(conf, " %-20s: %20s\n", "Total Bytes Received", total_bytes);
+ break;
+ }
+
+ // Remember when this was printed last
+ stats->last_printed = now;
+
+ // Reset statistics
+ stats->connections = 0;
+ stats->bytes_received = 0;
+
+ // Cleanup
+ if (total_bytes)
+ free(total_bytes);
+
+ return 0;
+}
size_t total_bytes_sent;
};
+int fireperf_dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats, int mode);
+
#endif /* FIREPERF_MAIN_H */
#define SOCKET_BACKLOG 1024
-static int dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats) {
- struct timespec now;
-
- // Fetch the time
- int r = clock_gettime(CLOCK_REALTIME, &now);
- if (r) {
- ERROR(conf, "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);
-
- // Format total bytes received
- char* total_bytes_received = format_size(stats->total_bytes_received, FIREPERF_FORMAT_BYTES);
-
- // Calculate bandwidth
- char* bps = format_size(stats->bytes_received * 8 / delta, FIREPERF_FORMAT_BITS);
-
- INFO( conf, "--- %s --------------------\n", timestamp);
- DEBUG(conf, " %-20s: %19.4fs\n", "Delta", delta);
- INFO( conf, " %-20s: %20u\n", "Open Connection(s)", stats->open_connections);
- INFO( conf, " %-20s: %16.2f/s\n", "New Connections", stats->connections / delta);
- INFO( conf, " %-20s: %18s/s\n", "Current Bandwidth", bps);
- INFO( conf, " %-20s: %20s\n", "Total Bytes Received", total_bytes_received);
-
- // Remember when this was printed last
- stats->last_printed = now;
-
- // Reset statistics
- stats->connections = 0;
- stats->bytes_received = 0;
-
- // Cleanup
- if (bps)
- free(bps);
-
- if (total_bytes_received)
- free(total_bytes_received);
-
- return 0;
-}
-
static int create_socket(struct fireperf_config* conf, int i) {
int r;
goto ERROR;
}
- r = dump_stats(conf, stats);
+ r = fireperf_dump_stats(conf, stats, FIREPERF_MODE_SERVER);
if (r)
goto ERROR;