Debian Codegraph @

src/log.c

raw symbols SCIP
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) 2020-2025 Linutronix GmbH
4 * Author Kurt Kanzenbach <[email protected]>
5 */
6 #include <errno.h>
7 #include <inttypes.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13
14 #include "config.h"
15 #include "log.h"
16 #include "stat.h"
17 #include "thread.h"
18 #include "utils.h"
19
20 static struct statistics global_statistics[NUM_FRAME_TYPES];
21 static struct ring_buffer *global_log_ring_buffer;
22 static enum log_level current_log_level;
23 static FILE *file_tracing_on;
24
25 int log_init(void)
26 {
27 global_log_ring_buffer = ring_buffer_allocate(LOG_BUFFER_SIZE);
28
29 if (!global_log_ring_buffer)
30 return -ENOMEM;
31
32 /* Default */
33 current_log_level = LOG_LEVEL_DEBUG;
34
35 if (!strcmp(app_config.log_level, "Debug"))
36 current_log_level = LOG_LEVEL_DEBUG;
37 if (!strcmp(app_config.log_level, "Info"))
38 current_log_level = LOG_LEVEL_INFO;
39 if (!strcmp(app_config.log_level, "Warning"))
40 current_log_level = LOG_LEVEL_WARNING;
41 if (!strcmp(app_config.log_level, "Error"))
42 current_log_level = LOG_LEVEL_ERROR;
43
44 if (app_config.debug_stop_trace_on_error) {
45 file_tracing_on = fopen("/sys/kernel/debug/tracing/tracing_on", "w");
46 if (!file_tracing_on)
47 return -errno;
48 }
49
50 return 0;
51 }
52
53 void log_free(void)
54 {
55 ring_buffer_free(global_log_ring_buffer);
56
57 if (app_config.debug_stop_trace_on_error)
58 fclose(file_tracing_on);
59 }
60
61 static const char *log_level_to_string(enum log_level level)
62 {
63 if (level == LOG_LEVEL_DEBUG)
64 return "DEBUG";
65 if (level == LOG_LEVEL_INFO)
66 return "INFO";
67 if (level == LOG_LEVEL_WARNING)
68 return "WARNING";
69 if (level == LOG_LEVEL_ERROR)
70 return "ERROR";
71
72 return NULL;
73 }
74
75 void log_message(enum log_level level, const char *format, ...)
76 {
77 unsigned char buffer[4096];
78 int written, len, ret;
79 struct timespec time;
80 va_list args;
81 char *p;
82
83 /* Stop trace on error if desired. */
84 if (level == LOG_LEVEL_ERROR && app_config.debug_stop_trace_on_error)
85 fprintf(file_tracing_on, "0\n");
86
87 /* Log message only if log level fulfilled. */
88 if (level > current_log_level)
89 return;
90
91 /* Log each message with time stamps. */
92 ret = clock_gettime(app_config.application_clock_id, &time);
93 if (ret)
94 memset(&time, '\0', sizeof(time));
95
96 len = sizeof(buffer) - 1;
97 p = (char *)buffer;
98
99 written = snprintf(p, len, "[%8ld.%9ld]: [%s]: ", time.tv_sec, time.tv_nsec,
100 log_level_to_string(level));
101 p += written;
102 len -= written;
103
104 va_start(args, format);
105 written += vsnprintf(p, len, format, args);
106 va_end(args);
107
108 ring_buffer_add(global_log_ring_buffer, buffer, written);
109 }
110
111 static void log_add_traffic_class(const char *name, enum stat_frame_type frame_type, char **buffer,
112 size_t *length)
113 {
114 const struct statistics *stat = &global_statistics[frame_type];
115 int written;
116
117 written = snprintf(
118 *buffer, *length,
119 "%sSent=%" PRIu64 " | %sReceived=%" PRIu64 " | %sRttMin=%" PRIu64
120 " [us] | %sRttMax=%" PRIu64 " [us] | %sRttAvg=%lf [us] | %sOnewayMin=%" PRIu64
121 " [us] | %sOnewayMax=%" PRIu64 " [us] | %sOnewayAvg=%lf [us] | ",
122 name, stat->frames_sent, name, stat->frames_received, name, stat->round_trip_min,
123 name, stat->round_trip_max, name, stat->round_trip_avg, name, stat->oneway_min,
124 name, stat->oneway_max, name, stat->oneway_avg);
125
126 *buffer += written;
127 *length -= written;
128
129 if (app_config.classes[frame_type].tx_hwtstamp_enabled && config_have_rx_timestamp() &&
130 app_config.classes[frame_type].xdp_enabled && stat->proc_first_count > 0) {
131 written = snprintf(*buffer, *length,
132 "%sProcFirstMin=%" PRIu64 " [us] | %sProcFirstMax=%" PRIu64
133 " [us] | %sProcFirstAvg=%lf [us] | ",
134 name, stat->proc_first_min, name, stat->proc_first_max, name,
135 stat->proc_first_avg);
136 *buffer += written;
137 *length -= written;
138 }
139
140 if (app_config.classes[frame_type].tx_hwtstamp_enabled && config_have_rx_timestamp() &&
141 app_config.classes[frame_type].xdp_enabled && stat->proc_batch_count > 0) {
142 written = snprintf(*buffer, *length,
143 "%sProcBatchMin=%" PRIu64 " [us] | %sProcBatchMax=%" PRIu64
144 " [us] | %sProcBatchAvg=%lf [us] | ",
145 name, stat->proc_batch_min, name, stat->proc_batch_max, name,
146 stat->proc_batch_avg);
147 *buffer += written;
148 *length -= written;
149 }
150
151 if (config_have_rx_timestamp() && app_config.classes[frame_type].xdp_enabled) {
152 written = snprintf(*buffer, *length,
153 "%sRxMin=%" PRIu64 " [us] | %sRxMax=%" PRIu64
154 " [us] | %sRxAvg=%lf [us] | "
155 "%sRxHw2XdpMin=%" PRIu64 " [us] | %sRxHw2XdpMax=%" PRIu64
156 " [us] | %sRxHw2XdpAvg=%lf [us] | "
157 "%sRxXdp2AppMin=%" PRIu64 " [us] | %sRxXdp2AppMax=%" PRIu64
158 " [us] | %sRxXdp2AppAvg=%lf [us] | ",
159 name, stat->rx_min, name, stat->rx_max, name, stat->rx_avg, name,
160 stat->rx_hw2xdp_min, name, stat->rx_hw2xdp_max, name,
161 stat->rx_hw2xdp_avg, name, stat->rx_xdp2app_min, name,
162 stat->rx_xdp2app_max, name, stat->rx_xdp2app_avg);
163
164 *buffer += written;
165 *length -= written;
166 }
167
168 if (app_config.classes[frame_type].tx_hwtstamp_enabled &&
169 app_config.classes[frame_type].xdp_enabled) {
170 written =
171 snprintf(*buffer, *length,
172 "%sTxMin=%" PRIu64 " [us] | %sTxMax=%" PRIu64
173 " [us] | %sTxAvg=%lf [us] | %sTxHwTimestampMissing=%" PRIu64 " | ",
174 name, stat->tx_min, name, stat->tx_max, name, stat->tx_avg, name,
175 stat->tx_hw_timestamp_missing);
176 *buffer += written;
177 *length -= written;
178 }
179
180 if (stat_frame_type_is_real_time(frame_type)) {
181 written = snprintf(*buffer, *length,
182 "%sRttOutliers=%" PRIu64 " | %sOnewayOutliers=%" PRIu64 " | ",
183 name, stat->round_trip_outliers, name, stat->oneway_outliers);
184 *buffer += written;
185 *length -= written;
186 }
187
188 if (app_config.classes[frame_type].rx_workload_enabled) {
189 written = snprintf(*buffer, *length,
190 "%sRxWorkloadMin=%" PRIu64 " [us] | "
191 "%sRxWorkloadMax=%" PRIu64 " [us] | "
192 "%sRxWorkloadAvg=%lf [us] | "
193 "%sRxWorkloadOutliers=%" PRIu64 " | ",
194 name, stat->rx_workload_min, name, stat->rx_workload_max, name,
195 stat->rx_workload_avg, name, stat->rx_workload_outliers);
196
197 *buffer += written;
198 *length -= written;
199 }
200 }
201
202 static void *log_thread_routine(void *data)
203 {
204 struct log_thread_context *log_context = data;
205 uint64_t period = app_config.stats_collection_interval_ns;
206 struct timespec time;
207 int ret;
208
209 /*
210 * Write the content of the LogBuffer periodically to disk. This thread can run with low
211 * priority to not influence to Application Tasks that much.
212 */
213 ret = clock_gettime(app_config.application_clock_id, &time);
214 if (ret) {
215 fprintf(stderr, "Log: clock_gettime() failed: %s!", strerror(errno));
216 return NULL;
217 }
218
219 while (!log_context->stop) {
220 size_t log_data_len, stat_message_length;
221 char stat_message[4096] = {}, *p;
222 int i;
223
224 /* Wait until next period */
225 increment_period(&time, period);
226 ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, &time, NULL);
227 if (ret) {
228 pthread_error(ret, "clock_nanosleep() failed");
229 return NULL;
230 }
231
232 /* Get latest statistics data */
233 stat_get_global_stats(global_statistics, sizeof(global_statistics));
234
235 /* Log statistics once per logging period. */
236 p = stat_message;
237 stat_message_length = sizeof(stat_message) - 1;
238
239 for (i = 0; i < NUM_FRAME_TYPES; i++) {
240 if (config_is_traffic_class_active(stat_frame_type_to_string(i))) {
241 const char *name =
242 i == GENERICL2_FRAME_TYPE
243 ? app_config.classes[GENERICL2_FRAME_TYPE].name
244 : stat_frame_type_to_string(i);
245
246 log_add_traffic_class(name, i, &p, &stat_message_length);
247 }
248 }
249
250 log_message(LOG_LEVEL_INFO, "%s\n", stat_message);
251
252 /* Fetch data */
253 ring_buffer_fetch(log_context->log_ring_buffer, log_context->log_data,
254 LOG_BUFFER_SIZE, &log_data_len);
255
256 /* Write down to disk */
257 if (log_data_len > 0) {
258 fwrite(log_context->log_data, sizeof(char), log_data_len,
259 log_context->file_handle);
260 fflush(log_context->file_handle);
261 }
262 }
263
264 return NULL;
265 }
266
267 struct log_thread_context *log_thread_create(void)
268 {
269 struct log_thread_context *log_context;
270 int ret;
271
272 log_context = calloc(1, sizeof(*log_context));
273 if (!log_context)
274 return NULL;
275
276 log_context->log_data = calloc(LOG_BUFFER_SIZE, sizeof(char));
277 if (!log_context->log_data)
278 goto err_log_data;
279
280 log_context->log_ring_buffer = global_log_ring_buffer;
281
282 log_context->file_handle = fopen(app_config.log_file, "w+");
283 if (!log_context->file_handle)
284 goto err_fopen;
285
286 ret = create_rt_thread(&log_context->log_task_id, "Logger", app_config.log_thread_priority,
287 app_config.log_thread_cpu, log_thread_routine, log_context);
288 if (ret)
289 goto err_thread;
290
291 return log_context;
292
293 err_thread:
294 fclose(log_context->file_handle);
295 err_fopen:
296 free(log_context->log_data);
297 err_log_data:
298 free(log_context);
299
300 return NULL;
301 }
302
303 void log_thread_stop(struct log_thread_context *thread_context)
304 {
305 if (!thread_context)
306 return;
307
308 thread_context->stop = 1;
309 pthread_join(thread_context->log_task_id, NULL);
310 }
311
312 void log_thread_free(struct log_thread_context *thread_context)
313 {
314 if (!thread_context)
315 return;
316
317 free(thread_context->log_data);
318 fclose(thread_context->file_handle);
319 free(thread_context);
320 }
321
322 void log_thread_wait_for_finish(struct log_thread_context *thread_context)
323 {
324 if (!thread_context)
325 return;
326
327 pthread_join(thread_context->log_task_id, NULL);
328 }