-
Notifications
You must be signed in to change notification settings - Fork 6
/
stat.c
340 lines (278 loc) · 6.93 KB
/
stat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (C) 2017
* Authors: Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation version 2.
*
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
* kind, whether express or implied; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdlib.h>
#include <stdio.h>
#include "stat.h"
#include "plget.h"
#include <math.h>
#include <string.h>
#define LOG_ENTRY_SIZE 15
#define LOG_BASE 8
#define LOG_LINE_SIZE (LOG_BASE * LOG_ENTRY_SIZE + 1)
/* res = a - b; */
void ts_sub(struct timespec *a, struct timespec *b, struct timespec *res)
{
typeof(res->tv_sec) sec;
typeof(res->tv_nsec) nsec;
sec = a->tv_sec - b->tv_sec;
if (a->tv_nsec < b->tv_nsec) {
nsec = NSEC_PER_SEC + a->tv_nsec - b->tv_nsec;
sec--;
} else {
nsec = a->tv_nsec - b->tv_nsec;
}
res->tv_sec = sec;
res->tv_nsec = nsec;
}
static inline __u64 stat_num(struct stats *ss)
{
return ss->next_ts - ss->start_ts;
}
static inline __u64 to_num(struct stats *ss, struct timespec *ts)
{
return ts - ss->start_ts;
}
static inline __u64 to_val(struct timespec *ts)
{
return NSEC_PER_SEC * ts->tv_sec + ts->tv_nsec;
}
void stats_push(struct stats *ss, struct timespec *ts)
{
ss->next_ts->tv_sec = ts->tv_sec;
ss->next_ts->tv_nsec = ts->tv_nsec;
ss->next_ts++;
}
void stats_push_id(struct stats *ss, struct timespec *ts, __u32 id)
{
if (id == ss->id) {
ss->next_ts->tv_sec = ts->tv_sec;
ss->next_ts->tv_nsec = ts->tv_nsec;
ss->next_ts++;
ss->id++;
return;
}
if (id > ss->id) {
ss->id = id + 1;
ss->next_ts += id - ss->id + 1;
}
(ss->start_ts + id)->tv_sec = ts->tv_sec;
(ss->start_ts + id)->tv_nsec = ts->tv_nsec;
}
int stats_correct_id(struct stats *ss, __u32 id)
{
return ts_correct(ss->start_ts + id);
}
static double stats_mean(struct stats *ss)
{
struct timespec *ts;
double mean = 0;
__u64 val;
__u64 n;
n = stat_num(ss);
for (ts = ss->start_ts; ts < ss->next_ts; ts++) {
val = to_val(ts);
mean += val / (n * 1000.0);
}
return mean;
}
static double stats_gap_mean(struct stats *ss)
{
struct timespec *ts, temp;
double mean = 0;
__u64 n;
n = stat_num(ss);
for (ts = ss->start_ts + 1; ts < ss->next_ts; ts++) {
ts_sub(ts, ts - 1, &temp);
mean += to_val(&temp) / ((n - 1) * 1000.0);
}
return mean;
}
static double stats_gap_dev(struct stats *ss, double mean)
{
struct timespec *ts, temp;
double dev = 0;
double mdif;
__u64 n;
n = stat_num(ss);
for (ts = ss->start_ts + 1; ts < ss->next_ts; ts++) {
ts_sub(ts, ts - 1, &temp);
mdif = mean - to_val(&temp) / 1000.0;
mdif *= mdif;
dev += mdif / (n - 1);
}
dev = sqrt(dev);
return dev;
}
static double stats_dev(struct stats *ss, double mean)
{
struct timespec *ts;
double dev = 0;
double mdif;
__u64 n;
n = stat_num(ss);
for (ts = ss->start_ts; ts < ss->next_ts; ts++) {
mdif = mean - to_val(ts) / 1000.0;
mdif *= mdif;
dev += mdif / n;
}
dev = sqrt(dev);
return dev;
}
void stats_diff(struct stats *a, struct stats *b, struct stats *res)
{
struct timespec *tsa, *tsb;
tsa = a->start_ts;
tsb = b->start_ts;
res->next_ts = res->start_ts;
while (tsa < a->next_ts && tsb < b->next_ts) {
if (ts_correct(tsa) && ts_correct(tsb)) {
ts_sub(tsa++, tsb++, res->next_ts++);
} else
break;
}
}
static void stats_print_log(struct stats *ss, int flags, struct timespec *rtime)
{
double min_val = 1000000, max_val = 0;
struct timespec *ts, temp;
char line[LOG_LINE_SIZE];
int max_n = 0, min_n = 0;
__u64 n, start;
double val;
int pad;
if (flags & STATS_LIN_DATA) {
start = to_val(rtime);
printf("relative abs time %llu ns\n", start);
start = to_val(ss->start_ts);
printf("first packet abs time %llu ns\n", start);
}
memset(line, '-', LOG_LINE_SIZE - 1);
line[LOG_LINE_SIZE - 1] = 0;
printf("%s", line);
if (flags & STATS_PLAIN_OUTPUT)
printf("\n");
n = stat_num(ss);
for (ts = ss->start_ts; ts < ss->next_ts; ts++) {
if (flags & STATS_LIN_DATA) {
ts_sub(ts, rtime, &temp);
val = to_val(&temp) / 1000.0;
} else if (flags & STATS_GAP_DATA) {
if (!to_num(ss, ts)) {
val = 0;
} else {
ts_sub(ts, ts - 1, &temp);
val = to_val(&temp) / 1000.0;
}
} else {
val = to_val(ts) / 1000.0;
}
if (flags & STATS_PLAIN_OUTPUT)
printf("%g\n", val);
else {
if (!(to_num(ss, ts) % LOG_BASE))
printf("\n");
printf(" %*g |", LOG_ENTRY_SIZE - 3, val);
}
if (flags & STATS_GAP_DATA) {
if (!to_num(ss, ts)) {
if (n == 1)
min_val = 0;
continue;
}
}
if (max_val < val) {
max_n = to_num(ss, ts);
max_val = val;
}
if (min_val > val) {
min_n = to_num(ss, ts);
min_val = val;
}
}
int pad_needed = !(flags & STATS_PLAIN_OUTPUT) &&
(to_num(ss, ts) % LOG_BASE);
if (pad_needed) {
pad = (LOG_BASE - to_num(ss, ts) % LOG_BASE) * LOG_ENTRY_SIZE;
printf("%*c", pad, '|');
}
printf("\n%s\n", line);
if (flags & STATS_LIN_DATA)
return;
printf("max val(#%d) = %.2fus\n", max_n, max_val);
printf("min val(#%d) = %.2fus\n", min_n, min_val);
printf("peak-to-peak = %.2fus\n", max_val - min_val);
}
int stats_reserve(struct stats *ss, int entry_num)
{
struct timespec *ts;
ts = malloc(entry_num * sizeof(*ts));
if (ts == NULL)
return -1;
ss->start_ts = ts;
ss->next_ts = ts;
return 0;
}
void stats_drate_print(struct timespec *interval, int pkt_num, int data_size)
{
__u64 val;
double rate, pps, period;
val = to_val(interval);
pps = (double)pkt_num * NSEC_PER_SEC / val;
rate = (double)data_size * 8 * USEC_PER_SEC / val;
period = val / (double)pkt_num;
printf("RAW RATE = %.2fkbps, PPS = %.1f\n", rate, pps);
printf("AVERAGE PERIOD = %.2fns (%.2fus)\n", period, period / 1000.0);
}
void stats_rate_print(struct timespec *interval, int pkt_num, int frame_size)
{
int dsize;
dsize = frame_size * pkt_num;
stats_drate_print(interval, pkt_num, dsize);
}
void stats_vrate_print(struct stats *ss, int frame_size)
{
struct timespec interval;
unsigned int pkt_num;
pkt_num = ss->next_ts - ss->start_ts;
if (pkt_num-- < 2 || !ts_correct(ss->start_ts))
return;
ts_sub(ss->next_ts - 1, ss->start_ts, &interval);
stats_rate_print(&interval, pkt_num, frame_size);
}
int stats_print(char *str, struct stats *ss, int flags, struct timespec *rtime)
{
double mean, dev;
__u64 n;
/* don't print if first entry is incorrect or no entries */
n = stat_num(ss);
if (!n || !ts_correct(ss->start_ts))
return 0;
printf("%s: packets %llu:\n", str, n);
if (rtime)
flags |= STATS_LIN_DATA;
stats_print_log(ss, flags, rtime);
if (flags & STATS_LIN_DATA)
goto out;
if (flags & STATS_GAP_DATA) {
mean = stats_gap_mean(ss);
dev = stats_gap_dev(ss, mean);
} else {
mean = stats_mean(ss);
dev = stats_dev(ss, mean);
}
printf("mean +- RMS = %.2f +- %.2f us\n", mean, dev);
out:
printf("\n");
return n;
}