| 1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
| 2 | /* |
| 3 | * Copyright (C) 2021-2025 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #ifndef _STAT_H_ |
| 8 | #define _STAT_H_ |
| 9 | |
| 10 | #include <signal.h> |
| 11 | #include <stdbool.h> |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | |
| 15 | enum log_stat_options { |
| 16 | LOG_REFERENCE = 0, |
| 17 | LOG_MIRROR, |
| 18 | /* Mirror mode with TX timestamping enabled */ |
| 19 | LOG_TX_TIMESTAMPS, |
| 20 | LOG_NUM_OPTIONS |
| 21 | }; |
| 22 | |
| 23 | enum stat_frame_type { |
| 24 | TSN_HIGH_FRAME_TYPE = 0, |
| 25 | TSN_LOW_FRAME_TYPE, |
| 26 | RTC_FRAME_TYPE, |
| 27 | RTA_FRAME_TYPE, |
| 28 | DCP_FRAME_TYPE, |
| 29 | LLDP_FRAME_TYPE, |
| 30 | UDP_HIGH_FRAME_TYPE, |
| 31 | UDP_LOW_FRAME_TYPE, |
| 32 | GENERICL2_FRAME_TYPE, |
| 33 | NUM_FRAME_TYPES, |
| 34 | }; |
| 35 | |
| 36 | static inline bool stat_frame_type_is_real_time(enum stat_frame_type frame_type) |
| 37 | { |
| 38 | switch (frame_type) { |
| 39 | case TSN_HIGH_FRAME_TYPE: |
| 40 | case TSN_LOW_FRAME_TYPE: |
| 41 | case RTC_FRAME_TYPE: |
| 42 | case GENERICL2_FRAME_TYPE: |
| 43 | return true; |
| 44 | default: |
| 45 | return false; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | extern const char *stat_frame_type_names[NUM_FRAME_TYPES]; |
| 50 | |
| 51 | static inline const char *stat_frame_type_to_string(enum stat_frame_type frame_type) |
| 52 | { |
| 53 | return stat_frame_type_names[frame_type]; |
| 54 | } |
| 55 | |
| 56 | struct statistics { |
| 57 | uint64_t time_stamp; |
| 58 | uint64_t frames_sent; |
| 59 | uint64_t frames_received; |
| 60 | uint64_t out_of_order_errors; |
| 61 | uint64_t frame_id_errors; |
| 62 | uint64_t payload_errors; |
| 63 | uint64_t round_trip_min; |
| 64 | uint64_t round_trip_max; |
| 65 | uint64_t round_trip_count; |
| 66 | uint64_t round_trip_outliers; |
| 67 | double round_trip_sum; |
| 68 | double round_trip_avg; |
| 69 | uint64_t oneway_min; |
| 70 | uint64_t oneway_max; |
| 71 | uint64_t oneway_count; |
| 72 | uint64_t oneway_outliers; |
| 73 | double oneway_sum; |
| 74 | double oneway_avg; |
| 75 | /* First-frame processing latency at Mirror (1st Rx HW to 1st Tx HW timestamp) */ |
| 76 | uint64_t proc_first_min; |
| 77 | uint64_t proc_first_max; |
| 78 | uint64_t proc_first_count; |
| 79 | double proc_first_sum; |
| 80 | double proc_first_avg; |
| 81 | /* Batch processing latency at Mirror (1st Rx HW to Last Tx HW timestamp) */ |
| 82 | uint64_t proc_batch_min; |
| 83 | uint64_t proc_batch_max; |
| 84 | uint64_t proc_batch_count; |
| 85 | double proc_batch_sum; |
| 86 | double proc_batch_avg; |
| 87 | /* Rx latency from NIC Rx Hw timestamp to user space timestamp */ |
| 88 | uint64_t rx_min; |
| 89 | uint64_t rx_max; |
| 90 | double rx_sum; |
| 91 | double rx_avg; |
| 92 | /* Rx latency from NIC Rx Hw timestamp to Xdp prog timestamp */ |
| 93 | uint64_t rx_hw2xdp_min; |
| 94 | uint64_t rx_hw2xdp_max; |
| 95 | double rx_hw2xdp_sum; |
| 96 | double rx_hw2xdp_avg; |
| 97 | /* Rx latency from Xdp prog timestamp to user space timestamp */ |
| 98 | uint64_t rx_xdp2app_min; |
| 99 | uint64_t rx_xdp2app_max; |
| 100 | double rx_xdp2app_sum; |
| 101 | double rx_xdp2app_avg; |
| 102 | /* Workload statistics */ |
| 103 | uint64_t rx_workload_min; |
| 104 | uint64_t rx_workload_max; |
| 105 | uint64_t rx_workload_count; |
| 106 | double rx_workload_sum; |
| 107 | double rx_workload_avg; |
| 108 | uint64_t rx_workload_outliers; |
| 109 | /* Tx latency from user space (at send) to NIC Tx HW timestamp */ |
| 110 | uint64_t tx_min; |
| 111 | uint64_t tx_max; |
| 112 | uint64_t tx_count; |
| 113 | uint64_t tx_hw_timestamp_missing; |
| 114 | double tx_sum; |
| 115 | double tx_avg; |
| 116 | }; |
| 117 | |
| 118 | struct rtt_entry { |
| 119 | uint64_t sw_ts; |
| 120 | uint64_t hw_ts; |
| 121 | uint64_t rx_hw_ts; |
| 122 | }; |
| 123 | |
| 124 | struct round_trip_context { |
| 125 | struct rtt_entry *backlog; |
| 126 | size_t backlog_len; |
| 127 | }; |
| 128 | |
| 129 | int stat_init(enum log_stat_options log_selection); |
| 130 | void stat_free(void); |
| 131 | const char *stat_frame_type_to_string(enum stat_frame_type frame_type); |
| 132 | void stat_frame_sent(enum stat_frame_type frame_type, uint64_t cycle_number); |
| 133 | void stat_frames_sent_batch(enum stat_frame_type frame_type, uint64_t cycle_number, |
| 134 | uint64_t frame_count); |
| 135 | void stat_frame_received(enum stat_frame_type frame_type, uint64_t cycle_number, bool out_of_order, |
| 136 | bool payload_mismatch, bool frame_id_mismatch, uint64_t tx_timestamp, |
| 137 | uint64_t rx_hw_timestamp, uint64_t rx_sw_timestamp); |
| 138 | void stat_update(void); |
| 139 | void stat_get_global_stats(struct statistics *stats, size_t len); |
| 140 | void stat_get_stats_per_period(struct statistics *stats, size_t len); |
| 141 | void stat_frame_workload(enum stat_frame_type, uint64_t cycle_number, struct timespec start_ts); |
| 142 | void stat_inc_workload_outlier(enum stat_frame_type frame_type); |
| 143 | void stat_frame_sent_latency(enum stat_frame_type frame_type, uint64_t seq); |
| 144 | void stat_proc_first_latency(enum stat_frame_type frame_type, uint64_t cycle_number, |
| 145 | uint64_t tx_hw_timestamp); |
| 146 | void stat_proc_batch_latency(enum stat_frame_type frame_type, uint64_t cycle_number, |
| 147 | uint64_t last_tx_hw_timestamp); |
| 148 | int stat_to_json(char *json, size_t len, const struct statistics *stat, const char *tc, |
| 149 | const char *measurement); |
| 150 | |
| 151 | extern volatile sig_atomic_t reset_stats; |
| 152 | extern struct round_trip_context round_trip_contexts[NUM_FRAME_TYPES]; |
| 153 | extern int log_stat_user_selected; |
| 154 | |
| 155 | #endif /* _STAT_H_ */ |