Debian Codegraph @

src/stat.c

raw symbols SCIP
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) 2021-2025 Linutronix GmbH
4 * Author Kurt Kanzenbach <[email protected]>
5 */
6
7 #include <errno.h>
8 #include <inttypes.h>
9 #include <memory.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <time.h>
14
15 #include "config.h"
16 #include "hist.h"
17 #include "log.h"
18 #include "log_mqtt.h"
19 #include "stat.h"
20 #include "thread.h"
21 #include "utils.h"
22
23 /*
24 * This is used by all real time Tx and Rx threads. However, all threads have their own portion
25 * within that struct so that it can be accessed without taking any locks. That is a must, because
26 * Tx and Rx are hot paths and we do not want to wait for logging or such.
27 */
28 static struct statistics global_statistics[NUM_FRAME_TYPES];
29
30 /*
31 * Once per stat collection interval the global_statistics are copied to global_statistics_for_log
32 * for the log threads. That is used for file Log at the moment.
33 */
34 static struct statistics global_statistics_for_log[NUM_FRAME_TYPES];
35 static pthread_mutex_t global_statistics_mutex;
36
37 /*
38 * These structs contains only the statistics for current stat interval. This is used by MQTT.
39 */
40 static struct statistics statistics_per_period[NUM_FRAME_TYPES];
41 static struct statistics statistics_per_period_for_log[NUM_FRAME_TYPES];
42
43 struct round_trip_context round_trip_contexts[NUM_FRAME_TYPES];
44 static uint64_t rtt_expected_rt_limit;
45 int log_stat_user_selected;
46 static FILE *file_tracing_on;
47 static FILE *file_trace_marker;
48
49 const char *stat_frame_type_names[NUM_FRAME_TYPES] = {
50 "TsnHigh", "TsnLow", "Rtc", "Rta", "Dcp", "Lldp", "UdpHigh", "UdpLow", "GenericL2"};
51
52 /*
53 * Keep 1024 periods of backlog available. If a frame is received later than 1024 periods after
54 * sending, it's a bug in any case.
55 *
56 * E.g. A period of 500us results in a backlog of 500ms.
57 */
58 #define STAT_MAX_BACKLOG 1024
59
60 /*
61 * If SIGUSR1 signal is received, reset the global statistics.
62 */
63 volatile sig_atomic_t reset_stats = 0;
64
65 static void stat_reset(struct statistics *stats)
66 {
67 memset(stats, 0, sizeof(struct statistics));
68 stats->round_trip_min = UINT64_MAX;
69 stats->oneway_min = UINT64_MAX;
70 stats->rx_min = UINT64_MAX;
71 stats->rx_hw2xdp_min = UINT64_MAX;
72 stats->rx_xdp2app_min = UINT64_MAX;
73 stats->rx_workload_min = UINT64_MAX;
74 stats->tx_min = UINT64_MAX;
75 stats->proc_first_min = UINT64_MAX;
76 stats->proc_batch_min = UINT64_MAX;
77 }
78
79 int stat_init(enum log_stat_options log_selection)
80 {
81 int i;
82
83 if (log_selection >= LOG_NUM_OPTIONS)
84 return -EINVAL;
85
86 init_mutex(&global_statistics_mutex);
87
88 if (log_selection == LOG_REFERENCE || log_selection == LOG_TX_TIMESTAMPS) {
89 bool allocation_error = false;
90
91 for (i = 0; i < NUM_FRAME_TYPES; i++) {
92 bool needs_backlog = (log_selection == LOG_REFERENCE) ||
93 (log_selection == LOG_TX_TIMESTAMPS &&
94 app_config.classes[i].tx_hwtstamp_enabled);
95
96 if (needs_backlog) {
97 struct round_trip_context *current_context =
98 &round_trip_contexts[i];
99
100 current_context->backlog_len =
101 STAT_MAX_BACKLOG *
102 app_config.classes[i].num_frames_per_cycle;
103
104 current_context->backlog = calloc(current_context->backlog_len,
105 sizeof(struct rtt_entry));
106 allocation_error |= !current_context->backlog;
107 }
108 }
109
110 if (allocation_error)
111 return -ENOMEM;
112 }
113
114 for (i = 0; i < NUM_FRAME_TYPES; i++) {
115 stat_reset(&global_statistics[i]);
116 stat_reset(&statistics_per_period[i]);
117 }
118
119 if (app_config.debug_stop_trace_on_outlier) {
120 file_tracing_on = fopen("/sys/kernel/debug/tracing/tracing_on", "w");
121 if (!file_tracing_on)
122 return -errno;
123 file_trace_marker = fopen("/sys/kernel/debug/tracing/trace_marker", "w");
124 if (!file_trace_marker) {
125 fclose(file_tracing_on);
126 return -errno;
127 }
128 }
129
130 /*
131 * The expected round trip limit for RT traffic classes is below < 2 * cycle time.
132 * Stored in us.
133 */
134 rtt_expected_rt_limit = app_config.application_base_cycle_time_ns * 2;
135 rtt_expected_rt_limit /= 1000;
136
137 log_stat_user_selected = log_selection;
138
139 return 0;
140 }
141
142 void stat_free(void)
143 {
144
145 for (int i = 0; i < NUM_FRAME_TYPES; i++)
146 free(round_trip_contexts[i].backlog);
147
148 if (app_config.debug_stop_trace_on_outlier) {
149 fclose(file_tracing_on);
150 fclose(file_trace_marker);
151 }
152 }
153
154 /*
155 * This function will be called once per cycle after all Tx threads have been finished. At this
156 * point in time no one touches global_statistics, because all networking is done.
157 *
158 * It copies all *statistics to *statistics_for_log.
159 */
160 void stat_update(void)
161 {
162 static uint64_t last_ts = 0;
163 uint64_t elapsed, curr_time;
164 bool proceed = false;
165 struct timespec now;
166
167 clock_gettime(app_config.application_clock_id, &now);
168 curr_time = ts_to_ns(&now);
169
170 if (!last_ts)
171 last_ts = curr_time;
172
173 elapsed = curr_time - last_ts;
174 if (elapsed >= app_config.stats_collection_interval_ns) {
175 proceed = true;
176 last_ts = curr_time;
177 }
178
179 if (!proceed)
180 return;
181
182 /* Update stats for logging facilities. */
183 pthread_mutex_lock(&global_statistics_mutex);
184 memcpy(&global_statistics_for_log, &global_statistics, sizeof(global_statistics));
185 #if defined(WITH_MQTT)
186 memcpy(&statistics_per_period_for_log, &statistics_per_period,
187 sizeof(statistics_per_period));
188 for (int i = 0; i < NUM_FRAME_TYPES; i++)
189 stat_reset(&statistics_per_period[i]);
190 #endif
191 pthread_mutex_unlock(&global_statistics_mutex);
192
193 /* Perform reset of global statistics if requested by user. */
194 if (reset_stats) {
195 for (int i = 0; i < NUM_FRAME_TYPES; i++)
196 stat_reset(&global_statistics[i]);
197 reset_stats = 0;
198 }
199 }
200
201 void stat_get_global_stats(struct statistics *stats, size_t len)
202 {
203 if (len < sizeof(global_statistics_for_log))
204 return;
205
206 pthread_mutex_lock(&global_statistics_mutex);
207 memcpy(stats, &global_statistics_for_log, sizeof(global_statistics_for_log));
208 pthread_mutex_unlock(&global_statistics_mutex);
209 }
210
211 void stat_get_stats_per_period(struct statistics *stats, size_t len)
212 {
213 if (len < sizeof(statistics_per_period_for_log))
214 return;
215
216 pthread_mutex_lock(&global_statistics_mutex);
217 memcpy(stats, &statistics_per_period_for_log, sizeof(statistics_per_period_for_log));
218 pthread_mutex_unlock(&global_statistics_mutex);
219 }
220
221 static inline void stat_update_min_max(uint64_t new_value, uint64_t *min, uint64_t *max)
222 {
223 *max = (new_value > *max) ? new_value : *max;
224 *min = (new_value < *min) ? new_value : *min;
225 }
226
227 static inline size_t get_first_frame_backlog_idx(uint64_t cycle_number,
228 enum stat_frame_type frame_type,
229 size_t backlog_len)
230 {
231 uint64_t first_frame_in_cycle =
232 (cycle_number / app_config.classes[frame_type].num_frames_per_cycle) *
233 app_config.classes[frame_type].num_frames_per_cycle;
234 return first_frame_in_cycle % backlog_len;
235 }
236
237 static bool stat_frame_received_common(struct statistics *stat, enum stat_frame_type frame_type,
238 uint64_t rt_time, uint64_t oneway_time, bool out_of_order,
239 bool payload_mismatch, bool frame_id_mismatch,
240 uint64_t rx_hw2app_time, uint64_t rx_hw2xdp_time,
241 uint64_t rx_xdp2app_time)
242 {
243 bool outlier = false;
244
245 if (log_stat_user_selected == LOG_REFERENCE) {
246 if (stat_frame_type_is_real_time(frame_type) && rt_time > rtt_expected_rt_limit) {
247 stat->round_trip_outliers++;
248 outlier = true;
249 }
250
251 stat_update_min_max(rt_time, &stat->round_trip_min, &stat->round_trip_max);
252
253 stat->round_trip_count++;
254 stat->round_trip_sum += rt_time;
255 stat->round_trip_avg = stat->round_trip_sum / (double)stat->round_trip_count;
256 }
257
258 stat_update_min_max(oneway_time, &stat->oneway_min, &stat->oneway_max);
259
260 if (stat_frame_type_is_real_time(frame_type) && oneway_time > rtt_expected_rt_limit / 2) {
261 stat->oneway_outliers++;
262 outlier = true;
263 }
264 stat->oneway_count++;
265 stat->oneway_sum += oneway_time;
266 stat->oneway_avg = stat->oneway_sum / (double)stat->oneway_count;
267
268 if (rx_hw2app_time != 0 && rx_hw2xdp_time != 0 && rx_xdp2app_time != 0) {
269 stat_update_min_max(rx_hw2app_time, &stat->rx_min, &stat->rx_max);
270 stat->rx_sum += rx_hw2app_time;
271 stat->rx_avg = stat->rx_sum / (double)stat->oneway_count;
272
273 stat_update_min_max(rx_hw2xdp_time, &stat->rx_hw2xdp_min, &stat->rx_hw2xdp_max);
274 stat->rx_hw2xdp_sum += rx_hw2xdp_time;
275 stat->rx_hw2xdp_avg = stat->rx_hw2xdp_sum / (double)stat->oneway_count;
276
277 stat_update_min_max(rx_xdp2app_time, &stat->rx_xdp2app_min, &stat->rx_xdp2app_max);
278 stat->rx_xdp2app_sum += rx_xdp2app_time;
279 stat->rx_xdp2app_avg = stat->rx_xdp2app_sum / (double)stat->oneway_count;
280 }
281
282 stat->frames_received++;
283 stat->out_of_order_errors += out_of_order;
284 stat->payload_errors += payload_mismatch;
285 stat->frame_id_errors += frame_id_mismatch;
286
287 return outlier;
288 }
289
290 #ifdef TX_TIMESTAMP
291 static void stat_frame_sent_latency_common(struct statistics *stat, enum stat_frame_type frame_type,
292 uint64_t tx_latency_us)
293 {
294 stat_update_min_max(tx_latency_us, &stat->tx_min, &stat->tx_max);
295
296 stat->tx_count++;
297 stat->tx_sum += tx_latency_us;
298 stat->tx_avg = stat->tx_sum / (double)stat->tx_count;
299 }
300
301 static void stat_frame_proc_first_common(struct statistics *stat, enum stat_frame_type frame_type,
302 uint64_t proc_first_us)
303 {
304 stat_update_min_max(proc_first_us, &stat->proc_first_min, &stat->proc_first_max);
305 stat->proc_first_count++;
306 stat->proc_first_sum += proc_first_us;
307 stat->proc_first_avg = stat->proc_first_sum / (double)stat->proc_first_count;
308 }
309
310 static void stat_frame_proc_batch_common(struct statistics *stat, enum stat_frame_type frame_type,
311 uint64_t proc_batch_us)
312 {
313 stat_update_min_max(proc_batch_us, &stat->proc_batch_min, &stat->proc_batch_max);
314 stat->proc_batch_count++;
315 stat->proc_batch_sum += proc_batch_us;
316 stat->proc_batch_avg = stat->proc_batch_sum / (double)stat->proc_batch_count;
317 }
318 #endif
319
320 #if defined(WITH_MQTT)
321 static void stat_frame_received_per_period(enum stat_frame_type frame_type, uint64_t curr_time,
322 uint64_t rt_time, uint64_t oneway_time,
323 bool out_of_order, bool payload_mismatch,
324 bool frame_id_mismatch, uint64_t rx_hw2app_time,
325 uint64_t rx_hw2xdp_time, uint64_t rx_xdp2app_time)
326 {
327 struct statistics *stat_per_period = &statistics_per_period[frame_type];
328
329 stat_per_period->time_stamp = curr_time;
330 stat_frame_received_common(stat_per_period, frame_type, rt_time, oneway_time, out_of_order,
331 payload_mismatch, frame_id_mismatch, rx_hw2app_time,
332 rx_hw2xdp_time, rx_xdp2app_time);
333 }
334
335 static void stat_frame_sent_per_period(enum stat_frame_type frame_type)
336 {
337 struct statistics *stat_per_period = &statistics_per_period[frame_type];
338
339 /* Just increment the Tx counter. The reset per period is done by the Rx part. */
340 stat_per_period->frames_sent++;
341 }
342
343 static void stat_frame_workload_per_period(enum stat_frame_type frame_type, uint64_t workload_time)
344 {
345 struct statistics *stat_per_period = &statistics_per_period[frame_type];
346
347 if (stat_per_period->rx_workload_count >
348 app_config.classes[frame_type].rx_workload_skip_count) {
349 stat_update_min_max(workload_time, &stat_per_period->rx_workload_min,
350 &stat_per_period->rx_workload_max);
351 }
352
353 stat_per_period->rx_workload_count++;
354 stat_per_period->rx_workload_sum += workload_time;
355 stat_per_period->rx_workload_avg =
356 stat_per_period->rx_workload_sum / (double)stat_per_period->rx_workload_count;
357 }
358
359 #ifdef TX_TIMESTAMP
360 static void stat_frame_sent_latency_per_period(enum stat_frame_type frame_type,
361 uint64_t tx_latency_us)
362 {
363 struct statistics *stat_per_period = &statistics_per_period[frame_type];
364
365 stat_frame_sent_latency_common(stat_per_period, frame_type, tx_latency_us);
366 }
367
368 static void stat_frame_proc_first_per_period(enum stat_frame_type frame_type,
369 uint64_t proc_first_us)
370 {
371 struct statistics *stat_per_period = &statistics_per_period[frame_type];
372
373 stat_frame_proc_first_common(stat_per_period, frame_type, proc_first_us);
374 }
375
376 static void stat_frame_proc_batch_per_period(enum stat_frame_type frame_type,
377 uint64_t proc_batch_us)
378 {
379 struct statistics *stat_per_period = &statistics_per_period[frame_type];
380
381 stat_frame_proc_batch_common(stat_per_period, frame_type, proc_batch_us);
382 }
383 #endif
384 #else
385 static void stat_frame_received_per_period(enum stat_frame_type frame_type, uint64_t curr_time,
386 uint64_t rt_time, bool out_of_order,
387 bool payload_mismatch, bool frame_id_mismatch,
388 uint64_t tx_timestamp, uint64_t rx_hw2app_time,
389 uint64_t rx_hw2xdp_time, uint64_t rx_xdp2app_time)
390 {
391 }
392
393 static void stat_frame_sent_per_period(enum stat_frame_type frame_type)
394 {
395 }
396
397 static void stat_frame_workload_per_period(enum stat_frame_type frame_type, uint64_t workload_time)
398 {
399 }
400 #ifdef TX_TIMESTAMP
401 static void stat_frame_sent_latency_per_period(enum stat_frame_type frame_type,
402 uint64_t tx_latency_us)
403 {
404 }
405
406 static void stat_frame_proc_first_per_period(enum stat_frame_type frame_type,
407 uint64_t proc_first_us)
408 {
409 }
410
411 static void stat_frame_proc_batch_per_period(enum stat_frame_type frame_type,
412 uint64_t proc_batch_us)
413 {
414 }
415 #endif
416 #endif
417
418 void stat_frame_sent(enum stat_frame_type frame_type, uint64_t cycle_number)
419 {
420 /* Single frame is just a batch operation with count=1 */
421 stat_frames_sent_batch(frame_type, cycle_number, 1);
422 }
423
424 void stat_frames_sent_batch(enum stat_frame_type frame_type, uint64_t cycle_number,
425 uint64_t frame_count)
426 {
427 struct round_trip_context *rtt = &round_trip_contexts[frame_type];
428 struct statistics *stat = &global_statistics[frame_type];
429 size_t idx = cycle_number % rtt->backlog_len;
430 struct timespec tx_time = {};
431
432 if (frame_count == 1) {
433 log_message(LOG_LEVEL_DEBUG, "%s: frame[%" PRIu64 "] sent\n",
434 stat_frame_type_to_string(frame_type), cycle_number);
435 } else {
436 log_message(LOG_LEVEL_DEBUG, "%s: %" PRIu64 " frames[%" PRIu64 "] sent\n",
437 stat_frame_type_to_string(frame_type), frame_count, cycle_number);
438 }
439
440 if ((log_stat_user_selected == LOG_REFERENCE ||
441 log_stat_user_selected == LOG_TX_TIMESTAMPS) &&
442 rtt->backlog) {
443 /* Record Tx SW timestamp for the first frame in the cycle */
444 clock_gettime(app_config.application_clock_id, &tx_time);
445 rtt->backlog[idx].sw_ts = ts_to_ns(&tx_time);
446 }
447
448 /* Increment stats by frame_count */
449 for (uint64_t i = 0; i < frame_count; i++) {
450 stat_frame_sent_per_period(frame_type);
451 }
452 stat->frames_sent += frame_count;
453 }
454
455 #ifdef TX_TIMESTAMP
456 void stat_frame_sent_latency(enum stat_frame_type frame_type, uint64_t seq)
457 {
458 bool hwtstamp_enabled = app_config.classes[frame_type].tx_hwtstamp_enabled;
459 struct statistics *stat_per_period = &statistics_per_period[frame_type];
460 struct round_trip_context *rtt = &round_trip_contexts[frame_type];
461 struct statistics *stat = &global_statistics[frame_type];
462 size_t idx = seq % rtt->backlog_len;
463 uint64_t sw_ts = rtt->backlog[idx].sw_ts;
464 uint64_t hw_ts = rtt->backlog[idx].hw_ts;
465
466 if (!hwtstamp_enabled) {
467 log_message(LOG_LEVEL_INFO,
468 "TxLatency [%s] HW timestamping disabled — skipping Tx latency stat\n",
469 stat_frame_type_to_string(frame_type));
470 return;
471 }
472
473 /* Check if we've already processed this timestamp (hw_ts was cleared) */
474 if (sw_ts != 0 && hw_ts == 0) {
475 log_message(LOG_LEVEL_DEBUG,
476 "TxLatency [%s] Seq %" PRIu64 ": Already processed, skipping\n",
477 stat_frame_type_to_string(frame_type), seq);
478 return;
479 }
480
481 if (hw_ts > sw_ts) {
482 int64_t latency = (int64_t)(hw_ts - sw_ts);
483
484 latency /= 1000;
485
486 log_message(LOG_LEVEL_DEBUG,
487 "TxLatency [%s] Seq %" PRIu64
488 ": SW %llu ns, HW %llu ns, latency %lld us, idx=%zu\n",
489 stat_frame_type_to_string(frame_type), seq, (unsigned long long)sw_ts,
490 (unsigned long long)hw_ts, (long long)latency, idx);
491
492 /* Update global stats */
493 stat_frame_sent_latency_common(stat, frame_type, latency);
494
495 /* Update stats per collection interval */
496 stat_frame_sent_latency_per_period(frame_type, latency);
497
498 /* Clear HW timestamp to prevent double processing */
499 rtt->backlog[idx].hw_ts = 0;
500
501 } else {
502 /* If HW timestamp isn't available after 1 cycle, consider it a miss */
503 stat->tx_hw_timestamp_missing++;
504 stat_per_period->tx_hw_timestamp_missing++;
505
506 log_message(LOG_LEVEL_DEBUG,
507 "TxLatency [%s] Seq %" PRIu64
508 ": No HW timestamp available after 1 cycle (SW %llu ns), idx=%zu\n",
509 stat_frame_type_to_string(frame_type), seq, (unsigned long long)sw_ts,
510 idx);
511 }
512 }
513 #endif
514
515 void stat_frame_received(enum stat_frame_type frame_type, uint64_t cycle_number, bool out_of_order,
516 bool payload_mismatch, bool frame_id_mismatch, uint64_t tx_timestamp,
517 uint64_t rx_hw_timestamp, uint64_t rx_sw_timestamp)
518 {
519 struct round_trip_context *rtt = &round_trip_contexts[frame_type];
520 const bool histogram = app_config.stats_histogram_enabled;
521 struct statistics *stat = &global_statistics[frame_type];
522 uint64_t rt_time = 0, curr_time, oneway_time, rx_hw2app_time, rx_hw2xdp_time,
523 rx_xdp2app_time;
524 struct timespec rx_time = {};
525 bool outlier = false;
526
527 log_message(LOG_LEVEL_DEBUG, "%s: frame[%" PRIu64 "] received\n",
528 stat_frame_type_to_string(frame_type), cycle_number);
529
530 /* Record Rx timestamp in us */
531 clock_gettime(app_config.application_clock_id, &rx_time);
532 curr_time = ts_to_ns(&rx_time);
533
534 /* Store RX HW timestamp for ProFirst and ProBatch latency measurement at Mirror */
535 if (log_stat_user_selected == LOG_TX_TIMESTAMPS &&
536 app_config.classes[frame_type].tx_hwtstamp_enabled && config_have_rx_timestamp() &&
537 rx_hw_timestamp != 0 && rtt->backlog) {
538 /* Check if this is the first frame of the cycle */
539 uint64_t frame_in_cycle =
540 cycle_number % app_config.classes[frame_type].num_frames_per_cycle;
541 if (frame_in_cycle == 0) {
542 size_t idx = get_first_frame_backlog_idx(cycle_number, frame_type,
543 rtt->backlog_len);
544 rtt->backlog[idx].rx_hw_ts = rx_hw_timestamp;
545
546 log_message(LOG_LEVEL_DEBUG,
547 "%s: Stored RX HW timestamp %" PRIu64 " for cycle %" PRIu64
548 ", idx=%zu\n",
549 stat_frame_type_to_string(frame_type), rx_hw_timestamp,
550 cycle_number, idx);
551 }
552 }
553
554 if (log_stat_user_selected == LOG_REFERENCE) {
555 uint64_t tx_sw_ts;
556 size_t backlog_idx;
557
558 /* Determine which timestamp to use based on TX HW timestamp configuration */
559 if (app_config.classes[frame_type].tx_hwtstamp_enabled) {
560 /*
561 * When TX HW timestamping is enabled, only first frame of each cycle has
562 * timestamp
563 */
564 backlog_idx = get_first_frame_backlog_idx(cycle_number, frame_type,
565 rtt->backlog_len);
566 tx_sw_ts = rtt->backlog[backlog_idx].sw_ts;
567 } else {
568 /* When TX HW timestamping is disabled, each frame has its own timestamp */
569 backlog_idx = cycle_number % rtt->backlog_len;
570 tx_sw_ts = rtt->backlog[backlog_idx].sw_ts;
571 }
572
573 /* Calc Round Trip Time */
574 if (tx_sw_ts != 0) {
575 rt_time = curr_time - tx_sw_ts;
576 rt_time /= 1000;
577 } else {
578 rt_time = 0;
579 }
580
581 /* Update histogram */
582 if (histogram)
583 histogram_update(frame_type, rt_time);
584 }
585
586 /* Calc Oneway Time */
587 oneway_time = curr_time - tx_timestamp;
588 oneway_time /= 1000;
589
590 if (rx_hw_timestamp != 0 && rx_sw_timestamp != 0) {
591 /* Calculate Rx times */
592 rx_hw2app_time = curr_time - rx_hw_timestamp;
593 rx_hw2app_time /= 1000;
594 rx_hw2xdp_time = rx_sw_timestamp - rx_hw_timestamp;
595 rx_hw2xdp_time /= 1000;
596 rx_xdp2app_time = curr_time - rx_sw_timestamp;
597 rx_xdp2app_time /= 1000;
598 } else {
599 /* If one of the timestamp is not available, set them to zero */
600 rx_hw2app_time = 0;
601 rx_hw2xdp_time = 0;
602 rx_xdp2app_time = 0;
603 }
604
605 /* Update global stats */
606 outlier = stat_frame_received_common(stat, frame_type, rt_time, oneway_time, out_of_order,
607 payload_mismatch, frame_id_mismatch, rx_hw2app_time,
608 rx_hw2xdp_time, rx_xdp2app_time);
609
610 /* Update stats per collection interval */
611 stat_frame_received_per_period(frame_type, curr_time, rt_time, oneway_time, out_of_order,
612 payload_mismatch, frame_id_mismatch, rx_hw2app_time,
613 rx_hw2xdp_time, rx_xdp2app_time);
614
615 /* Stop tracing after certain amount of time */
616 if (app_config.debug_stop_trace_on_outlier && outlier) {
617 fprintf(file_trace_marker,
618 "Outlier hit: %" PRIu64 " [us] -- Type: %s -- Cycle Counter: %" PRIu64 "\n",
619 rt_time ? rt_time : oneway_time, stat_frame_type_to_string(frame_type),
620 cycle_number);
621 fprintf(file_tracing_on, "0\n");
622 fprintf(stderr,
623 "Outlier hit: %" PRIu64 " [us] -- Type: %s -- Cycle Counter: %" PRIu64 "\n",
624 rt_time ? rt_time : oneway_time, stat_frame_type_to_string(frame_type),
625 cycle_number);
626 fclose(file_trace_marker);
627 fclose(file_tracing_on);
628 exit(EXIT_SUCCESS);
629 }
630 }
631
632 #ifdef TX_TIMESTAMP
633 void stat_proc_first_latency(enum stat_frame_type frame_type, uint64_t cycle_number,
634 uint64_t tx_hw_timestamp)
635 {
636 struct round_trip_context *rtt = &round_trip_contexts[frame_type];
637 struct statistics *stat = &global_statistics[frame_type];
638 size_t idx = get_first_frame_backlog_idx(cycle_number, frame_type, rtt->backlog_len);
639 uint64_t rx_hw_ts = rtt->backlog[idx].rx_hw_ts;
640
641 /* Ensure both RX and TX hardware timestamps are enabled */
642 if (!config_have_rx_timestamp() || !app_config.classes[frame_type].tx_hwtstamp_enabled) {
643 log_message(LOG_LEVEL_DEBUG,
644 "ProcFirst [%s] Cycle %" PRIu64
645 ": Hardware timestamping not fully enabled (RX: %s, TX: %s)\n",
646 stat_frame_type_to_string(frame_type), cycle_number,
647 config_have_rx_timestamp() ? "enabled" : "disabled",
648 app_config.classes[frame_type].tx_hwtstamp_enabled ? "enabled"
649 : "disabled");
650 return;
651 }
652
653 if (rx_hw_ts == 0 || tx_hw_timestamp == 0) {
654 log_message(LOG_LEVEL_DEBUG,
655 "ProcFirst [%s] Cycle %" PRIu64 ": Missing timestamp (RX HW: %" PRIu64
656 ", TX HW: %" PRIu64 ")\n",
657 stat_frame_type_to_string(frame_type), cycle_number, rx_hw_ts,
658 tx_hw_timestamp);
659 return;
660 }
661
662 if (tx_hw_timestamp > rx_hw_ts) {
663 uint64_t proc_first_latency = (tx_hw_timestamp - rx_hw_ts) / 1000;
664
665 log_message(LOG_LEVEL_DEBUG,
666 "ProcFirst [%s] Cycle %" PRIu64 ": %" PRIu64 " us (RX HW: %" PRIu64
667 ", TX HW: %" PRIu64 ")\n",
668 stat_frame_type_to_string(frame_type), cycle_number, proc_first_latency,
669 rx_hw_ts, tx_hw_timestamp);
670
671 /* Update global stats */
672 stat_frame_proc_first_common(stat, frame_type, proc_first_latency);
673
674 /* Update stats per collection interval */
675 stat_frame_proc_first_per_period(frame_type, proc_first_latency);
676
677 /* Not clearing RX HW timestamp here - it will be cleared by ProcBatch */
678 } else {
679 log_message(LOG_LEVEL_DEBUG,
680 "ProcFirst [%s] Cycle %" PRIu64 ": TX HW timestamp (%" PRIu64
681 ") <= RX HW timestamp (%" PRIu64 ")\n",
682 stat_frame_type_to_string(frame_type), cycle_number, tx_hw_timestamp,
683 rx_hw_ts);
684 }
685 }
686
687 void stat_proc_batch_latency(enum stat_frame_type frame_type, uint64_t cycle_number,
688 uint64_t last_tx_hw_timestamp)
689 {
690 struct round_trip_context *rtt = &round_trip_contexts[frame_type];
691 struct statistics *stat = &global_statistics[frame_type];
692 size_t idx = get_first_frame_backlog_idx(cycle_number, frame_type, rtt->backlog_len);
693 uint64_t rx_hw_ts = rtt->backlog[idx].rx_hw_ts;
694
695 /* Clear RX HW timestamp after both ProcFirst and ProcBatch are processed. */
696 rtt->backlog[idx].rx_hw_ts = 0;
697
698 /* Ensure both RX and TX hardware timestamps are enabled */
699 if (!config_have_rx_timestamp() || !app_config.classes[frame_type].tx_hwtstamp_enabled) {
700 log_message(LOG_LEVEL_DEBUG,
701 "ProcBatch [%s] Cycle %" PRIu64
702 ": Hardware timestamping not fully enabled (RX: %s, TX: %s)\n",
703 stat_frame_type_to_string(frame_type), cycle_number,
704 config_have_rx_timestamp() ? "enabled" : "disabled",
705 app_config.classes[frame_type].tx_hwtstamp_enabled ? "enabled"
706 : "disabled");
707 return;
708 }
709
710 if (rx_hw_ts == 0 || last_tx_hw_timestamp == 0) {
711 log_message(LOG_LEVEL_DEBUG,
712 "ProcBatch [%s] Cycle %" PRIu64 ": Missing timestamp (RX HW: %" PRIu64
713 ", Last TX HW: %" PRIu64 ")\n",
714 stat_frame_type_to_string(frame_type), cycle_number, rx_hw_ts,
715 last_tx_hw_timestamp);
716 return;
717 }
718
719 if (last_tx_hw_timestamp > rx_hw_ts) {
720 uint64_t proc_batch_latency = (last_tx_hw_timestamp - rx_hw_ts) / 1000;
721
722 log_message(LOG_LEVEL_DEBUG,
723 "ProcBatch [%s] Cycle %" PRIu64 ": %" PRIu64 " us (1st RX HW: %" PRIu64
724 ", Last TX HW: %" PRIu64 ")\n",
725 stat_frame_type_to_string(frame_type), cycle_number, proc_batch_latency,
726 rx_hw_ts, last_tx_hw_timestamp);
727
728 /* Update global stats */
729 stat_frame_proc_batch_common(stat, frame_type, proc_batch_latency);
730
731 /* Update stats per collection interval */
732 stat_frame_proc_batch_per_period(frame_type, proc_batch_latency);
733 } else {
734 log_message(LOG_LEVEL_DEBUG,
735 "ProcBatch [%s] Cycle %" PRIu64 ": Last TX HW timestamp (%" PRIu64
736 ") <= RX HW timestamp (%" PRIu64 ")\n",
737 stat_frame_type_to_string(frame_type), cycle_number,
738 last_tx_hw_timestamp, rx_hw_ts);
739 }
740 }
741 #endif
742
743 void stat_frame_workload(enum stat_frame_type frame_type, uint64_t cycle_number,
744 struct timespec start_ts)
745 {
746 struct statistics *stat = &global_statistics[frame_type];
747 uint64_t workload_time = 0, curr_time, start_time;
748 struct timespec clk_time = {};
749
750 log_message(LOG_LEVEL_DEBUG, "%s: frame[%" PRIu64 "] workload_complete\n",
751 stat_frame_type_to_string(frame_type), cycle_number);
752
753 clock_gettime(app_config.application_clock_id, &clk_time);
754 curr_time = ts_to_ns(&clk_time);
755 start_time = ts_to_ns(&start_ts);
756
757 workload_time = curr_time - start_time;
758 workload_time /= 1000;
759
760 if (stat->rx_workload_count > app_config.classes[frame_type].rx_workload_skip_count) {
761 stat_update_min_max(workload_time, &stat->rx_workload_min, &stat->rx_workload_max);
762 }
763
764 stat->rx_workload_count++;
765 stat->rx_workload_sum += workload_time;
766 stat->rx_workload_avg = stat->rx_workload_sum / (double)stat->rx_workload_count;
767
768 /* Update stats per collection interval */
769 stat_frame_workload_per_period(frame_type, workload_time);
770 }
771
772 void stat_inc_workload_outlier(enum stat_frame_type frame_type)
773 {
774 struct statistics *stat = &global_statistics[frame_type];
775
776 stat->rx_workload_outliers++;
777 }
778
779 static int json_err_handling(char **buffer, size_t *len, int ret)
780 {
781 /* Error? */
782 if (ret < 0)
783 return -EINVAL;
784
785 /* Buffer too small? */
786 if (ret >= *len)
787 return -EINVAL;
788
789 /* All good */
790 *buffer += ret;
791 *len -= ret;
792
793 return 0;
794 }
795
796 static int append_jlog_u64(char **buffer, size_t *len, const char *stat, uint64_t value)
797 {
798 int ret;
799
800 ret = snprintf(*buffer, *len, "\"%s\": %" PRIu64 ",\n", stat, value);
801
802 return json_err_handling(buffer, len, ret);
803 }
804
805 static int last_jlog_u64(char **buffer, size_t *len, const char *stat, uint64_t value)
806 {
807 int ret;
808
809 ret = snprintf(*buffer, *len, "\"%s\": %" PRIu64 "\n", stat, value);
810
811 return json_err_handling(buffer, len, ret);
812 }
813
814 static int append_jlog_float(char **buffer, size_t *len, const char *stat, float value)
815 {
816 int ret;
817
818 ret = snprintf(*buffer, *len, "\"%s\": %lf,\n", stat, value);
819
820 return json_err_handling(buffer, len, ret);
821 }
822
823 int stat_to_json(char *json, size_t len, const struct statistics *stat, const char *tc,
824 const char *measurement)
825 {
826 int ret;
827
828 /* JSON header */
829 ret = snprintf(json, len,
830 "{\"reference\" :\n"
831 "{\"Timestamp\" : %" PRIu64 ",\n"
832 "\"MeasurementName\" : \"%s\",\n"
833 "\"stats\" : \n"
834 "{\n"
835 "\"TCName\" : \"%s\",\n",
836 stat->time_stamp, measurement, tc);
837 ret = json_err_handling(&json, &len, ret);
838 if (ret)
839 return ret;
840
841 /* JSON statistics */
842 ret = append_jlog_u64(&json, &len, "FramesSent", stat->frames_sent);
843 if (ret)
844 return ret;
845
846 ret = append_jlog_u64(&json, &len, "FramesReceived", stat->frames_received);
847 if (ret)
848 return ret;
849
850 ret = append_jlog_u64(&json, &len, "RoundTripTimeMin", stat->round_trip_min);
851 if (ret)
852 return ret;
853
854 ret = append_jlog_u64(&json, &len, "RoundTripMax", stat->round_trip_max);
855 if (ret)
856 return ret;
857
858 ret = append_jlog_float(&json, &len, "RoundTripAv", stat->round_trip_avg);
859 if (ret)
860 return ret;
861
862 ret = append_jlog_u64(&json, &len, "OnewayMin", stat->oneway_min);
863 if (ret)
864 return ret;
865
866 ret = append_jlog_u64(&json, &len, "OnewayMax", stat->oneway_max);
867 if (ret)
868 return ret;
869
870 ret = append_jlog_float(&json, &len, "OnewayAv", stat->oneway_avg);
871 if (ret)
872 return ret;
873
874 ret = append_jlog_u64(&json, &len, "ProcFirstMin", stat->proc_first_min);
875 if (ret)
876 return ret;
877
878 ret = append_jlog_u64(&json, &len, "ProcFirstMax", stat->proc_first_max);
879 if (ret)
880 return ret;
881
882 ret = append_jlog_float(&json, &len, "ProcFirstAv", stat->proc_first_avg);
883 if (ret)
884 return ret;
885
886 ret = append_jlog_u64(&json, &len, "ProcBatchMin", stat->proc_batch_min);
887 if (ret)
888 return ret;
889
890 ret = append_jlog_u64(&json, &len, "ProcBatchMax", stat->proc_batch_max);
891 if (ret)
892 return ret;
893
894 ret = append_jlog_float(&json, &len, "ProcBatchAv", stat->proc_batch_avg);
895 if (ret)
896 return ret;
897
898 ret = append_jlog_u64(&json, &len, "RxMin", stat->rx_min);
899 if (ret)
900 return ret;
901
902 ret = append_jlog_u64(&json, &len, "RxMax", stat->rx_max);
903 if (ret)
904 return ret;
905
906 ret = append_jlog_float(&json, &len, "RxAv", stat->rx_avg);
907 if (ret)
908 return ret;
909
910 ret = append_jlog_u64(&json, &len, "RxHw2XdpMin", stat->rx_hw2xdp_min);
911 if (ret)
912 return ret;
913
914 ret = append_jlog_u64(&json, &len, "RxHw2XdpMax", stat->rx_hw2xdp_max);
915 if (ret)
916 return ret;
917
918 ret = append_jlog_float(&json, &len, "RxHw2XdpAv", stat->rx_hw2xdp_avg);
919 if (ret)
920 return ret;
921
922 ret = append_jlog_u64(&json, &len, "RxXdp2AppMin", stat->rx_xdp2app_min);
923 if (ret)
924 return ret;
925
926 ret = append_jlog_u64(&json, &len, "RxXdp2AppMax", stat->rx_xdp2app_max);
927 if (ret)
928 return ret;
929
930 ret = append_jlog_float(&json, &len, "RxXdp2AppAv", stat->rx_xdp2app_avg);
931 if (ret)
932 return ret;
933
934 ret = append_jlog_u64(&json, &len, "RxWorkloadMin", stat->rx_workload_min);
935 if (ret)
936 return ret;
937
938 ret = append_jlog_u64(&json, &len, "RxWorkloadMax", stat->rx_workload_max);
939 if (ret)
940 return ret;
941
942 ret = append_jlog_float(&json, &len, "RxWorkloadAv", stat->rx_workload_avg);
943 if (ret)
944 return ret;
945
946 ret = append_jlog_u64(&json, &len, "TxMin", stat->tx_min);
947 if (ret)
948 return ret;
949
950 ret = append_jlog_u64(&json, &len, "TxMax", stat->tx_max);
951 if (ret)
952 return ret;
953
954 ret = append_jlog_float(&json, &len, "TxAv", stat->tx_avg);
955 if (ret)
956 return ret;
957
958 ret = append_jlog_u64(&json, &len, "TxHwTimestampMissing", stat->tx_hw_timestamp_missing);
959 if (ret)
960 return ret;
961
962 ret = append_jlog_u64(&json, &len, "OutofOrderErrors", stat->out_of_order_errors);
963 if (ret)
964 return ret;
965
966 ret = append_jlog_u64(&json, &len, "FrameIdErrors", stat->frame_id_errors);
967 if (ret)
968 return ret;
969
970 ret = append_jlog_u64(&json, &len, "PayloadErrors", stat->payload_errors);
971 if (ret)
972 return ret;
973
974 ret = append_jlog_u64(&json, &len, "RoundTripOutliers", stat->round_trip_outliers);
975 if (ret)
976 return ret;
977
978 ret = last_jlog_u64(&json, &len, "OnewayOutliers", stat->oneway_outliers);
979 if (ret)
980 return ret;
981
982 /* JSON footer */
983 ret = snprintf(json, len, "}\n}\n}\n");
984
985 return json_err_handling(&json, &len, ret);
986 }