From 01713bbe20d2cf5aafbe5eb32721d3e4fc5823d8 Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Fri, 5 Sep 2025 03:30:24 +0200 Subject: Standarize the project directory for monorepo-like developer experience Move the clang formatter to the root of the three so all nested projects could use it Provide README for all other projects Refactor the code in rtt agregator Signed-off-by: Filip Wandzio --- analysis/rtt/src/logger.c | 151 +++++++++++++++++++++++++++++----------------- 1 file changed, 96 insertions(+), 55 deletions(-) (limited to 'analysis/rtt/src/logger.c') diff --git a/analysis/rtt/src/logger.c b/analysis/rtt/src/logger.c index ed5f6e5..e1dd2f3 100644 --- a/analysis/rtt/src/logger.c +++ b/analysis/rtt/src/logger.c @@ -1,4 +1,3 @@ - #include "logger.h" #include #include @@ -9,78 +8,120 @@ #define WINDOW_SEC 1 #define MAX_TIMES 1000 +#define ISO_TIME_BUF_SIZE 32 + +#define MICROSECONDS_IN_SECOND 1000000 +#define MICROSECONDS_IN_MILLISECOND 1000 + +#define CSV_HEADER "timestamp,sent_ms,received_ms,rtt_ms,throughput_msg_per_s\n" +#define CSV_LINE_FORMAT "%s,%lld,%lld,%lld,%d\n" + static FILE *csv_file = NULL; static double msg_times[MAX_TIMES]; static int msg_count = 0; -static long long current_time_ms() { - struct timeval tv; - gettimeofday(&tv, NULL); - return ((long long)tv.tv_sec * 1000) + (tv.tv_usec / 1000); +/** + * @brief Returns the current time in milliseconds since the Epoch. + * + * @return Current time in milliseconds (int64). + */ +static long long current_time_ms(void); + +/** + * @brief Returns the current time in seconds (with microsecond precision) since + * the Epoch. + * + * @return Current time in seconds (double). + */ +static double current_time_sec(void); + +/** + * @brief Updates the sliding window of message timestamps and returns the count + * of messages within WINDOW_SEC. + * + * @param now Current timestamp in seconds. + * @return Number of messages in the last WINDOW_SEC seconds. + */ +static int update_throughput_window(double now); + +static long long current_time_ms(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return ((long long)tv.tv_sec * 1000) + + (tv.tv_usec / MICROSECONDS_IN_MILLISECOND); } -static double current_time_sec() { - struct timeval tv; - gettimeofday(&tv, NULL); - return (double)tv.tv_sec + (tv.tv_usec / 1e6); +static double current_time_sec(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (double)tv.tv_sec + + (tv.tv_usec / (double)MICROSECONDS_IN_SECOND); } -static int update_throughput_window(double now) { - int i, new_count = 0; - for (i = 0; i < msg_count; i++) { - if (now - msg_times[i] < WINDOW_SEC) { - msg_times[new_count++] = msg_times[i]; - } - } - msg_times[new_count++] = now; - msg_count = new_count; - return msg_count; +static int update_throughput_window(double current_time) +{ + int msg_index, window_count = 0; + for (msg_index = 0; msg_index < msg_count; msg_index++) { + if (current_time - msg_times[msg_index] < WINDOW_SEC) + msg_times[window_count++] = msg_times[msg_index]; + } + msg_times[window_count++] = current_time; + msg_count = window_count; + return msg_count; } -void logger_init(const char *filename) { - csv_file = fopen(filename, "w"); - if (!csv_file) { - perror("Cannot open CSV file"); - exit(EXIT_FAILURE); - } - fprintf(csv_file, - "timestamp,sent_ms,received_ms,rtt_ms,throughput_msg_per_s\n"); - fflush(csv_file); +void logger_init(const char *filename) +{ + csv_file = fopen(filename, "w"); + if (!csv_file) { + perror("Cannot open CSV file"); + exit(EXIT_FAILURE); + } + fprintf(csv_file, CSV_HEADER); + fflush(csv_file); } -void logger_cleanup() { - if (csv_file) { - fclose(csv_file); - csv_file = NULL; - } +void logger_cleanup(void) +{ + if (csv_file) { + fclose(csv_file); + csv_file = NULL; + } } -void logger_handle_message(const void *payload, int payloadlen) { - char *payload_str = malloc(payloadlen + 1); - if (!payload_str) - return; - memcpy(payload_str, payload, payloadlen); - payload_str[payloadlen] = '\0'; +void logger_handle_message(const void *payload, int payloadlen) +{ + if (payloadlen <= 0) + return; + + char *payload_str = malloc(payloadlen + 1); + if (!payload_str) + return; + + memcpy(payload_str, payload, payloadlen); + payload_str[payloadlen] = '\0'; - long long sent_ms = atoll(payload_str); - free(payload_str); + long long sent_ms = atoll(payload_str); + free(payload_str); - long long received_ms = current_time_ms(); - long long rtt = received_ms - sent_ms; + long long received_ms = current_time_ms(); + long long rtt = received_ms - sent_ms; - double now_sec = current_time_sec(); - int throughput = update_throughput_window(now_sec); + double now_sec = current_time_sec(); + int throughput = update_throughput_window(now_sec); - time_t now = time(NULL); - struct tm *tm_info = localtime(&now); - char iso_time[32]; - strftime(iso_time, sizeof(iso_time), "%Y-%m-%dT%H:%M:%S", tm_info); + time_t now = time(NULL); + struct tm *tm_info = localtime(&now); + char iso_time[ISO_TIME_BUF_SIZE]; + strftime(iso_time, sizeof(iso_time), "%Y-%m-%dT%H:%M:%S", tm_info); - if (csv_file) { - fprintf(csv_file, "%s,%lld,%lld,%lld,%d\n", iso_time, sent_ms, received_ms, - rtt, throughput); - fflush(csv_file); - } + if (csv_file) { + fprintf(csv_file, CSV_LINE_FORMAT, iso_time, sent_ms, + received_ms, rtt, throughput); + fflush(csv_file); + } - printf("RTT: %lld ms | Throughput: %d msg/s\n", rtt, throughput); + printf("RTT: %lld ms | Throughput: %d msg/s\n", rtt, throughput); } -- cgit v1.2.3