Debian Codegraph @

src/hist.c

raw symbols SCIP
1 // SPDX-License-Identifier: BSD-2-Clause
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"
16
17 #include "app_config.h"
18 #include "config.h"
19 #include "stat.h"
20
21 static struct histogram histograms[NUM_FRAME_TYPES];
22 static FILE *hist_file;
23
24 int histogram_init(void)
25 {
26 int i;
27
28 if (!app_config.stats_histogram_enabled)
29 return 0;
30
31 for (i = 0; i < NUM_FRAME_TYPES; i++) {
32 struct histogram *hist = &histograms[i];
33
34 hist->min = app_config.stats_histogram_minimum_ns;
35 hist->max = app_config.stats_histogram_maximum_ns;
36
37 /* RTT is recorded in us */
38 hist->min /= 1000;
39 hist->max /= 1000;
40
41 hist->data = calloc(hist->max - hist->min, sizeof(uint64_t));
42 if (!hist->data)
43 return -ENOMEM;
44 }
45
46 hist_file = fopen(app_config.stats_histogram_file, "w");
47 if (!hist_file) {
48 fprintf(stderr, "Failed to open file '%s': %s!\n", app_config.stats_histogram_file,
49 strerror(errno));
50 return -EINVAL;
51 }
52
53 return 0;
54 }
55
56 void histogram_free(void)
57 {
58 int i;
59
60 if (!app_config.stats_histogram_enabled)
61 return;
62
63 for (i = 0; i < NUM_FRAME_TYPES; i++) {
64 struct histogram *hist = &histograms[i];
65
66 free(hist->data);
67 }
68 }
69
70 void histogram_update(enum stat_frame_type frame_type, uint64_t rtt)
71 {
72 struct histogram *hist = &histograms[frame_type];
73 uint64_t sample = rtt - hist->min;
74
75 /* Overflow */
76 if (rtt >= hist->max) {
77 hist->overflow++;
78 return;
79 }
80
81 /* Underflow */
82 if (rtt < hist->min) {
83 hist->underflow++;
84 return;
85 }
86
87 /* Update sample */
88 hist->data[sample]++;
89 }
90
91 static void hist_print_kernel_info(void)
92 {
93 struct utsname name;
94 int ret;
95
96 ret = uname(&name);
97 if (ret) {
98 fprintf(stderr, "uname() failed: %s\n", strerror(errno));
99 fprintf(stderr, "Could not get system information!\n");
100 return;
101 }
102
103 fprintf(hist_file, "Kernel: %s %s %s %s %s\n", name.sysname, name.nodename, name.release,
104 name.version, name.machine);
105 }
106
107 static void hist_print_kernel_cmdline(void)
108 {
109 char buf[1024];
110 FILE *f;
111
112 f = fopen("/proc/cmdline", "r");
113 if (!f) {
114 fprintf(stderr, "Failed to open '/proc/cmdline': %s\n", strerror(errno));
115 fprintf(stderr, "Could not get the kernel command line!\n");
116 return;
117 }
118
119 if (!fgets(buf, sizeof(buf), f))
120 goto out;
121
122 fprintf(hist_file, "Kernel Command Line: %s", buf);
123
124 out:
125 fclose(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_write(void)
136 {
137 uint64_t rtt;
138 int i;
139
140 if (!app_config.stats_histogram_enabled)
141 return 0;
142
143 fprintf(hist_file, "Testbench: Version '%s'\n", VERSION);
144 hist_print_kernel_info();
145 hist_print_kernel_cmdline();
146 fprintf(hist_file, "Testbench RTT Histogram: %s %s %s %s %s %s %s %s %s\n",
147 stat_frame_type_to_string(TSN_HIGH_FRAME_TYPE),
148 stat_frame_type_to_string(TSN_LOW_FRAME_TYPE),
149 stat_frame_type_to_string(RTC_FRAME_TYPE),
150 stat_frame_type_to_string(RTA_FRAME_TYPE),
151 stat_frame_type_to_string(DCP_FRAME_TYPE),
152 stat_frame_type_to_string(LLDP_FRAME_TYPE),
153 stat_frame_type_to_string(UDP_HIGH_FRAME_TYPE),
154 stat_frame_type_to_string(UDP_LOW_FRAME_TYPE),
155 stat_frame_type_to_string(GENERICL2_FRAME_TYPE));
156 for (rtt = histograms[0].min; rtt < histograms[0].max; rtt++) {
157 bool print = false;
158
159 for (i = 0; i < NUM_FRAME_TYPES; i++) {
160 struct histogram *hist = &histograms[i];
161
162 if (hist->data[rtt - hist->min]) {
163 print = true;
164 break;
165 }
166 }
167
168 if (!print)
169 continue;
170
171 fprintf(hist_file, "%08" PRIu64 ": ", rtt);
172 for (i = 0; i < NUM_FRAME_TYPES; i++) {
173 struct histogram *hist = &histograms[i];
174
175 fprintf(hist_file, "%08" PRIu64 " ", hist->data[rtt - hist->min]);
176 }
177 fprintf(hist_file, "\n");
178 }
179
180 fprintf(hist_file, "Overflow: ");
181 for (i = 0; i < NUM_FRAME_TYPES; i++) {
182 struct histogram *hist = &histograms[i];
183
184 fprintf(hist_file, "%08" PRIu64 " ", hist->overflow);
185 }
186 fprintf(hist_file, "\n");
187
188 fprintf(hist_file, "Underflow: ");
189 for (i = 0; i < NUM_FRAME_TYPES; i++) {
190 struct histogram *hist = &histograms[i];
191
192 fprintf(hist_file, "%08" PRIu64 " ", hist->underflow);
193 }
194 fprintf(hist_file, "\n");
195
196 fclose(hist_file);
197
198 return 0;
199 }