diff options
Diffstat (limited to 'analysis/rtt/src/logger.c')
| -rw-r--r-- | analysis/rtt/src/logger.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/analysis/rtt/src/logger.c b/analysis/rtt/src/logger.c new file mode 100644 index 0000000..ed5f6e5 --- /dev/null +++ b/analysis/rtt/src/logger.c | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | |||
| 2 | #include "logger.h" | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include <sys/time.h> | ||
| 7 | #include <time.h> | ||
| 8 | |||
| 9 | #define WINDOW_SEC 1 | ||
| 10 | #define MAX_TIMES 1000 | ||
| 11 | |||
| 12 | static FILE *csv_file = NULL; | ||
| 13 | static double msg_times[MAX_TIMES]; | ||
| 14 | static int msg_count = 0; | ||
| 15 | |||
| 16 | static long long current_time_ms() { | ||
| 17 | struct timeval tv; | ||
| 18 | gettimeofday(&tv, NULL); | ||
| 19 | return ((long long)tv.tv_sec * 1000) + (tv.tv_usec / 1000); | ||
| 20 | } | ||
| 21 | |||
| 22 | static double current_time_sec() { | ||
| 23 | struct timeval tv; | ||
| 24 | gettimeofday(&tv, NULL); | ||
| 25 | return (double)tv.tv_sec + (tv.tv_usec / 1e6); | ||
| 26 | } | ||
| 27 | |||
| 28 | static int update_throughput_window(double now) { | ||
| 29 | int i, new_count = 0; | ||
| 30 | for (i = 0; i < msg_count; i++) { | ||
| 31 | if (now - msg_times[i] < WINDOW_SEC) { | ||
| 32 | msg_times[new_count++] = msg_times[i]; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | msg_times[new_count++] = now; | ||
| 36 | msg_count = new_count; | ||
| 37 | return msg_count; | ||
| 38 | } | ||
| 39 | |||
| 40 | void logger_init(const char *filename) { | ||
| 41 | csv_file = fopen(filename, "w"); | ||
| 42 | if (!csv_file) { | ||
| 43 | perror("Cannot open CSV file"); | ||
| 44 | exit(EXIT_FAILURE); | ||
| 45 | } | ||
| 46 | fprintf(csv_file, | ||
| 47 | "timestamp,sent_ms,received_ms,rtt_ms,throughput_msg_per_s\n"); | ||
| 48 | fflush(csv_file); | ||
| 49 | } | ||
| 50 | |||
| 51 | void logger_cleanup() { | ||
| 52 | if (csv_file) { | ||
| 53 | fclose(csv_file); | ||
| 54 | csv_file = NULL; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | void logger_handle_message(const void *payload, int payloadlen) { | ||
| 59 | char *payload_str = malloc(payloadlen + 1); | ||
| 60 | if (!payload_str) | ||
| 61 | return; | ||
| 62 | memcpy(payload_str, payload, payloadlen); | ||
| 63 | payload_str[payloadlen] = '\0'; | ||
| 64 | |||
| 65 | long long sent_ms = atoll(payload_str); | ||
| 66 | free(payload_str); | ||
| 67 | |||
| 68 | long long received_ms = current_time_ms(); | ||
| 69 | long long rtt = received_ms - sent_ms; | ||
| 70 | |||
| 71 | double now_sec = current_time_sec(); | ||
| 72 | int throughput = update_throughput_window(now_sec); | ||
| 73 | |||
| 74 | time_t now = time(NULL); | ||
| 75 | struct tm *tm_info = localtime(&now); | ||
| 76 | char iso_time[32]; | ||
| 77 | strftime(iso_time, sizeof(iso_time), "%Y-%m-%dT%H:%M:%S", tm_info); | ||
| 78 | |||
| 79 | if (csv_file) { | ||
| 80 | fprintf(csv_file, "%s,%lld,%lld,%lld,%d\n", iso_time, sent_ms, received_ms, | ||
| 81 | rtt, throughput); | ||
| 82 | fflush(csv_file); | ||
| 83 | } | ||
| 84 | |||
| 85 | printf("RTT: %lld ms | Throughput: %d msg/s\n", rtt, throughput); | ||
| 86 | } | ||
