From 15ab58aa139dc6c810f1eeaa202716f9eabdec7a Mon Sep 17 00:00:00 2001 From: jfreegman Date: Mon, 31 Jan 2022 12:45:35 -0500 Subject: [PATCH] Add run time, avg. kb/s and packets/s to netprof log dump --- src/netprof.c | 12 +++++++++++- src/netprof.h | 4 +++- src/toxic.c | 21 ++++++++++++++++++++- src/windows.h | 5 +++-- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/netprof.c b/src/netprof.c index 8b28021..1778658 100644 --- a/src/netprof.c +++ b/src/netprof.c @@ -230,7 +230,7 @@ void dump_packet_bytes_totals(const Tox *m, FILE *fp, const uint64_t total_bytes fprintf(fp, "\n\n"); } -void netprof_log_dump(const Tox *m, FILE *fp) +void netprof_log_dump(const Tox *m, FILE *fp, time_t run_time) { if (fp == NULL) { fprintf(stderr, "Failed to dump network statistics: null file pointer\n"); @@ -257,6 +257,16 @@ void netprof_log_dump(const Tox *m, FILE *fp) const uint64_t total_count = UDP_count_sent + UDP_count_recv + TCP_count_sent + TCP_count_recv; const uint64_t total_bytes = UDP_bytes_sent + UDP_bytes_recv + TCP_bytes_sent + TCP_bytes_recv; + fprintf(fp, "--- Tox network profile log dump ---\n"); + fprintf(fp, "Run time: %lu seconds\n", run_time); + + if (run_time && total_count && total_bytes) { + fprintf(fp, "Average kilobytes per second: %.2f\n", ((float)total_bytes / run_time) / 1000.0); + fprintf(fp, "Average packets per second: %lu\n", total_count / run_time); + fprintf(fp, "Average packet size: %lu bytes\n", total_bytes / total_count); + fprintf(fp, "\n"); + } + dump_packet_count_totals(m, fp, total_count, UDP_count_sent, UDP_count_recv, TCP_count_sent, TCP_count_recv); dump_packet_bytes_totals(m, fp, total_bytes, UDP_bytes_sent, UDP_bytes_recv, TCP_bytes_sent, TCP_bytes_recv); dump_packet_id_counts(m, fp, total_count, TOX_NETPROF_PACKET_TYPE_TCP); diff --git a/src/netprof.h b/src/netprof.h index 88de7eb..7d0f8f8 100644 --- a/src/netprof.h +++ b/src/netprof.h @@ -23,8 +23,10 @@ #ifndef TOXIC_NETPROF #define TOXIC_NETPROF +#include +#include #include -void netprof_log_dump(const Tox *m, FILE *fp); +void netprof_log_dump(const Tox *m, FILE *fp, time_t run_time); #endif // TOXIC_NETPROF diff --git a/src/toxic.c b/src/toxic.c index 5965106..1802528 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -195,7 +195,7 @@ void free_global_data(void) void exit_toxic_success(Tox *m) { if (arg_opts.netprof_log_dump) { - netprof_log_dump(m, arg_opts.netprof_fp); + netprof_log_dump(m, arg_opts.netprof_fp, get_unix_time() - arg_opts.netprof_start_time); } store_data(m, DATA_FILE); @@ -1455,6 +1455,25 @@ static void parse_args(int argc, char *argv[]) break; } + case 's': { + if (optarg == NULL) { + queue_init_message("Invalid argument for option: %d", opt); + break; + } + + arg_opts.netprof_fp = fopen(optarg, "w"); + + if (arg_opts.netprof_fp != NULL) { + queue_init_message("Network profile logging enabled. Logging to file: '%s'", optarg); + arg_opts.netprof_log_dump = true; + arg_opts.netprof_start_time = time(NULL); + } else { + queue_init_message("Failed to open file '%s' for network profile logging.", optarg); + } + + break; + } + case 'u': { arg_opts.unencrypt_data = 1; break; diff --git a/src/windows.h b/src/windows.h index e8f0a0e..1f78493 100644 --- a/src/windows.h +++ b/src/windows.h @@ -142,8 +142,9 @@ struct arg_opts { bool logging; FILE *log_fp; - bool netprof_log_dump; - FILE *netprof_fp; + bool netprof_log_dump; + time_t netprof_start_time; + FILE *netprof_fp; char proxy_address[256]; uint8_t proxy_type;