| 1 | // SPDX-License-Identifier: BSD-2-Clausecxx . . $ `<file>/src/hist.c`/ |
| 2 | /* |
| 3 | * Copyright (C) 2024 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <stdbool.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | #include <sys/utsname.h> |
| 14 | |
| 15 | #include "hist.h"cxx . . $ `<file>/src/hist.h`/ |
| 16 | |
| 17 | #include "app_config.h"cxx . . $ `<file>/build/app_config.h`/ |
| 18 | #include "config.h"cxx . . $ `<file>/src/config.h`/ |
| 19 | #include "stat.h"cxx . . $ `<file>/src/stat.h`/ |
| 20 | |
| 21 | static struct histogramcxx . . $ histogram# histogramscxx . . $ histograms.[NUM_FRAME_TYPEScxx . . $ NUM_FRAME_TYPES.]; |
| 22 | static FILEcxx . . $ FILE# *hist_filecxx . . $ hist_file.; |
| 23 | |
| 24 | int histogram_initcxx . . $ histogram_init(b126dc7c1de90089).(void) |
| 25 | { |
| 26 | int i; |
| 27 | |
| 28 | if (!app_configcxx . . $ app_config..stats_histogram_enabledcxx . . $ application_config#stats_histogram_enabled.) |
| 29 | return 0; |
| 30 | |
| 31 | for (i = 0; i < NUM_FRAME_TYPEScxx . . $ NUM_FRAME_TYPES.; i++) { |
| 32 | struct histogramcxx . . $ histogram# *hist = &histogramscxx . . $ histograms.[i]; |
| 33 | |
| 34 | hist->mincxx . . $ histogram#min. = app_configcxx . . $ app_config..stats_histogram_minimum_nscxx . . $ application_config#stats_histogram_minimum_ns.; |
| 35 | hist->maxcxx . . $ histogram#max. = app_configcxx . . $ app_config..stats_histogram_maximum_nscxx . . $ application_config#stats_histogram_maximum_ns.; |
| 36 | |
| 37 | /* RTT is recorded in us */ |
| 38 | hist->mincxx . . $ histogram#min. /= 1000; |
| 39 | hist->maxcxx . . $ histogram#max. /= 1000; |
| 40 | |
| 41 | hist->datacxx . . $ histogram#data. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(hist->maxcxx . . $ histogram#max. - hist->mincxx . . $ histogram#min., sizeof(uint64_tcxx . . $ uint64_t#)); |
| 42 | if (!hist->datacxx . . $ histogram#data.) |
| 43 | return -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 44 | } |
| 45 | |
| 46 | hist_filecxx . . $ hist_file. = fopencxx . . $ fopen(82ec0787cd37e0a3).(app_configcxx . . $ app_config..stats_histogram_filecxx . . $ application_config#stats_histogram_file., "w"); |
| 47 | if (!hist_filecxx . . $ hist_file.) { |
| 48 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to open file '%s': %s!\n", app_configcxx . . $ app_config..stats_histogram_filecxx . . $ application_config#stats_histogram_file., |
| 49 | strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 50 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | void histogram_freecxx . . $ histogram_free(49f6e7a06ebc5aa8).(void) |
| 57 | { |
| 58 | int i; |
| 59 | |
| 60 | if (!app_configcxx . . $ app_config..stats_histogram_enabledcxx . . $ application_config#stats_histogram_enabled.) |
| 61 | return; |
| 62 | |
| 63 | for (i = 0; i < NUM_FRAME_TYPEScxx . . $ NUM_FRAME_TYPES.; i++) { |
| 64 | struct histogramcxx . . $ histogram# *hist = &histogramscxx . . $ histograms.[i]; |
| 65 | |
| 66 | freecxx . . $ free(5d22bdacc48458e8).(hist->datacxx . . $ histogram#data.); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void histogram_updatecxx . . $ histogram_update(ca48c151307d73b6).(enum stat_frame_typecxx . . $ stat_frame_type# frame_type, uint64_tcxx . . $ uint64_t# rtt) |
| 71 | { |
| 72 | struct histogramcxx . . $ histogram# *hist = &histogramscxx . . $ histograms.[frame_type]; |
| 73 | uint64_tcxx . . $ uint64_t# sample = rtt - hist->mincxx . . $ histogram#min.; |
| 74 | |
| 75 | /* Overflow */ |
| 76 | if (rtt >= hist->maxcxx . . $ histogram#max.) { |
| 77 | hist->overflowcxx . . $ histogram#overflow.++; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | /* Underflow */ |
| 82 | if (rtt < hist->mincxx . . $ histogram#min.) { |
| 83 | hist->underflowcxx . . $ histogram#underflow.++; |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | /* Update sample */ |
| 88 | hist->datacxx . . $ histogram#data.[sample]++; |
| 89 | } |
| 90 | |
| 91 | static void hist_print_kernel_infocxx . . $ hist_print_kernel_info(49f6e7a06ebc5aa8).(void) |
| 92 | { |
| 93 | struct utsnamecxx . . $ utsname# name; |
| 94 | int ret; |
| 95 | |
| 96 | ret = unamecxx . . $ uname(c69a16225c55e184).(&name); |
| 97 | if (ret) { |
| 98 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "uname() failed: %s\n", strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 99 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Could not get system information!\n"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "Kernel: %s %s %s %s %s\n", name.sysnamecxx . . $ utsname#sysname., name.nodenamecxx . . $ utsname#nodename., name.releasecxx . . $ utsname#release., |
| 104 | name.versioncxx . . $ utsname#version., name.machinecxx . . $ utsname#machine.); |
| 105 | } |
| 106 | |
| 107 | static void hist_print_kernel_cmdlinecxx . . $ hist_print_kernel_cmdline(49f6e7a06ebc5aa8).(void) |
| 108 | { |
| 109 | char buf[1024]; |
| 110 | FILEcxx . . $ FILE# *f; |
| 111 | |
| 112 | f = fopencxx . . $ fopen(82ec0787cd37e0a3).("/proc/cmdline", "r"); |
| 113 | if (!f) { |
| 114 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to open '/proc/cmdline': %s\n", strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 115 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Could not get the kernel command line!\n"); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | if (!fgetscxx . . $ fgets(efb0aaba7ac866ae).(buf, sizeof(buf), f)) |
| 120 | goto out; |
| 121 | |
| 122 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "Kernel Command Line: %s", buf); |
| 123 | |
| 124 | out: |
| 125 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(f); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Write histogram to disk in form like this: |
| 130 | * RTT HistMin + 0: TsnHighSampleCount TsnLowSampleCount ... |
| 131 | * RTT HistMin + 1: TsnHighSampleCount TsnLowSampleCount ... |
| 132 | * [...] |
| 133 | * RTT HistMax - 1: TsnHighSampleCount TsnLowSampleCount ... |
| 134 | */ |
| 135 | int histogram_writecxx . . $ histogram_write(b126dc7c1de90089).(void) |
| 136 | { |
| 137 | uint64_tcxx . . $ uint64_t# rtt; |
| 138 | int i; |
| 139 | |
| 140 | if (!app_configcxx . . $ app_config..stats_histogram_enabledcxx . . $ application_config#stats_histogram_enabled.) |
| 141 | return 0; |
| 142 | |
| 143 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "Testbench: Version '%s'\n", VERSIONcxx . . $ `build/app_config.h:10:9`!); |
| 144 | hist_print_kernel_infocxx . . $ hist_print_kernel_info(49f6e7a06ebc5aa8).(); |
| 145 | hist_print_kernel_cmdlinecxx . . $ hist_print_kernel_cmdline(49f6e7a06ebc5aa8).(); |
| 146 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "Testbench RTT Histogram: %s %s %s %s %s %s %s %s %s\n", |
| 147 | stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(TSN_HIGH_FRAME_TYPEcxx . . $ TSN_HIGH_FRAME_TYPE.), |
| 148 | stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(TSN_LOW_FRAME_TYPEcxx . . $ TSN_LOW_FRAME_TYPE.), |
| 149 | stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(RTC_FRAME_TYPEcxx . . $ RTC_FRAME_TYPE.), |
| 150 | stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(RTA_FRAME_TYPEcxx . . $ RTA_FRAME_TYPE.), |
| 151 | stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(DCP_FRAME_TYPEcxx . . $ DCP_FRAME_TYPE.), |
| 152 | stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(LLDP_FRAME_TYPEcxx . . $ LLDP_FRAME_TYPE.), |
| 153 | stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(UDP_HIGH_FRAME_TYPEcxx . . $ UDP_HIGH_FRAME_TYPE.), |
| 154 | stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(UDP_LOW_FRAME_TYPEcxx . . $ UDP_LOW_FRAME_TYPE.), |
| 155 | stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(GENERICL2_FRAME_TYPEcxx . . $ GENERICL2_FRAME_TYPE.)); |
| 156 | for (rtt = histogramscxx . . $ histograms.[0].mincxx . . $ histogram#min.; rtt < histogramscxx . . $ histograms.[0].maxcxx . . $ histogram#max.; rtt++) { |
| 157 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! print = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 158 | |
| 159 | for (i = 0; i < NUM_FRAME_TYPEScxx . . $ NUM_FRAME_TYPES.; i++) { |
| 160 | struct histogramcxx . . $ histogram# *hist = &histogramscxx . . $ histograms.[i]; |
| 161 | |
| 162 | if (hist->datacxx . . $ histogram#data.[rtt - hist->mincxx . . $ histogram#min.]) { |
| 163 | print = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (!print) |
| 169 | continue; |
| 170 | |
| 171 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "%08" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! ": ", rtt); |
| 172 | for (i = 0; i < NUM_FRAME_TYPEScxx . . $ NUM_FRAME_TYPES.; i++) { |
| 173 | struct histogramcxx . . $ histogram# *hist = &histogramscxx . . $ histograms.[i]; |
| 174 | |
| 175 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "%08" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! " ", hist->datacxx . . $ histogram#data.[rtt - hist->mincxx . . $ histogram#min.]); |
| 176 | } |
| 177 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "\n"); |
| 178 | } |
| 179 | |
| 180 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "Overflow: "); |
| 181 | for (i = 0; i < NUM_FRAME_TYPEScxx . . $ NUM_FRAME_TYPES.; i++) { |
| 182 | struct histogramcxx . . $ histogram# *hist = &histogramscxx . . $ histograms.[i]; |
| 183 | |
| 184 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "%08" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! " ", hist->overflowcxx . . $ histogram#overflow.); |
| 185 | } |
| 186 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "\n"); |
| 187 | |
| 188 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "Underflow: "); |
| 189 | for (i = 0; i < NUM_FRAME_TYPEScxx . . $ NUM_FRAME_TYPES.; i++) { |
| 190 | struct histogramcxx . . $ histogram# *hist = &histogramscxx . . $ histograms.[i]; |
| 191 | |
| 192 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "%08" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! " ", hist->underflowcxx . . $ histogram#underflow.); |
| 193 | } |
| 194 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(hist_filecxx . . $ hist_file., "\n"); |
| 195 | |
| 196 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(hist_filecxx . . $ hist_file.); |
| 197 | |
| 198 | return 0; |
| 199 | } |