-
Notifications
You must be signed in to change notification settings - Fork 6
/
result.c
425 lines (351 loc) · 10.2 KB
/
result.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* 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 "plget_args.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/ethtool.h>
#define MEASUREMENTS_NUM 5
#define NSEC_PER_USEC 1000ULL
static void res_print_clock_info(int clock, char *clock_name)
{
struct timespec res[MEASUREMENTS_NUM];
struct timespec ts1, ts2;
int res_is_avarage;
double aver_res;
int i, j, k;
__u64 val;
int ret;
ret = clock_getres(clock, &ts1);
if (ret < 0)
perror("clock_getres");
printf("-----------------------------------------\n");
printf("%s info:\n", clock_name);
printf("Declared resolution: %ldns\n", ts1.tv_nsec);
clock_gettime(clock, &ts1);
val = NSEC_PER_SEC * ts1.tv_sec + ts1.tv_nsec;
printf("Current time: %lluns\n", val);
for (j =0; j < MEASUREMENTS_NUM; j++) {
for (i = 0, k = 0; k < 8;) {
clock_gettime(clock, &ts1);
clock_gettime(clock, &ts2);
if (ts1.tv_nsec != ts2.tv_nsec) {
if (i > 1)
break;
else {
k++;
continue;
}
}
i++;
}
ts_sub(&ts2, &ts1, &res[j]);
if (res[j].tv_sec)
plget_fail("too long time diff");
}
/* check if res is real or average */
aver_res = res[0].tv_nsec;
for (i = 1; i < MEASUREMENTS_NUM; i++) {
if (res[i].tv_nsec != res[i-1].tv_nsec)
res_is_avarage = 1;
aver_res += res[i].tv_nsec;
}
if (res_is_avarage) {
aver_res /= MEASUREMENTS_NUM;
printf("Average \"userspace\" access resolution: %gns\n", aver_res);
} else {
printf("Real resolution: %ldns\n", res->tv_nsec);
}
/* Count in 1sec*/
clock_gettime(clock, &ts1);
printf("%s count:", clock_name);
for (i = 0; i < 3;) {
clock_gettime(clock, &ts2);
ts_sub(&ts2, &ts1, res);
if (res->tv_sec >= 1) {
ts1 = ts2;
printf(" %d", ++i);
fflush(stdout);
}
}
printf("\n");
}
static int res_tx_lat_print(void)
{
struct timespec *rtime;
int print_flags;
int n = 0;
print_flags = plget->flags & PLF_PLAIN_FORMAT ? STATS_PLAIN_OUTPUT : 0;
if (plget->flags & PLF_LATENCY_STAT) {
stats_diff(&tx_hw_v, &tx_sw_v, &temp);
n |= stats_print("\ndma + NIC tx latency, us (not complete "
"driver latency, driver s/w ts -> wire)" ,
&temp, print_flags, NULL);
stats_diff(&tx_sw_v, &tx_app_v, &temp);
n |= stats_print("\nstack + packet scheduler + part of "
"driver tx latency, us (app -> some place in the "
"NIC driver, app -> driver s/w ts)",
&temp, print_flags, NULL);
stats_diff(&tx_hw_v, &tx_app_v, &temp);
n |= stats_print("\ncomplete tx latency, us (driver latency + "
"stack latency, app -> wire)",
&temp, print_flags, NULL);
}
if (plget->flags & PLF_SCHED_STAT) {
struct stats *v;
int i;
v = tx_sch_v;
stats_diff(v, &tx_app_v, &temp);
n |= stats_print("\nstack tx latency, us (based on s/w "
"timestamps, app -> packet scheduler)",
&temp, print_flags, NULL);
for (i = 1; i < plget->dev_deep; i++) {
printf("psched%d -> psched%d\n", i, i + 1);
stats_diff(&tx_sch_v[i], v, &temp);
n |= stats_print("\nbetween device (sched) tx latency, "
"us (based on s/w timestamps, psched "
"-> psched)",
&temp, print_flags, NULL);
v = &tx_sch_v[i];
}
stats_diff(&tx_sw_v, v, &temp);
n |= stats_print("\npacket scheduler + part of driver tx "
"latency, us (packet scheduler -> driver s/w "
"ts)", &temp, print_flags, NULL);
stats_diff(&tx_hw_v, v, &temp);
n |= stats_print("\ndriver + packet scheduler tx latency, "
"us (packet scheduler entrance -> wire)",
&temp, print_flags, NULL);
}
if (plget->flags & PLF_HW_STAT) {
if (plget->flags & PLF_RTIME) {
rtime = &plget->rtime;
} else {
rtime = plget->mod == ECHO_LAT ? rx_hw_v.start_ts :
tx_hw_v.start_ts;
}
n |= stats_print("\nhw tx time, us", &tx_hw_v, print_flags,
rtime);
}
if (plget->flags & PLF_IPGAP_STAT) {
if (plget->flags & PLF_DIS_HW_TS)
n |= stats_print("\ngap of sw tx time, us", &tx_sw_v,
print_flags | STATS_GAP_DATA, NULL);
else
n |= stats_print("\ngap of hw tx time, us", &tx_hw_v,
print_flags | STATS_GAP_DATA, NULL);
}
return n;
}
static int res_rx_lat_print(void)
{
struct timespec *rtime;
int print_flags;
int n = 0;
print_flags = plget->flags & PLF_PLAIN_FORMAT ? STATS_PLAIN_OUTPUT : 0;
if (plget->flags & PLF_HW_STAT) {
if (plget->flags & PLF_RTIME)
rtime = &plget->rtime;
else
rtime = plget->mod == RTT_MOD ? tx_hw_v.start_ts :
rx_hw_v.start_ts;
n |= stats_print("\nhw rx time, us", &rx_hw_v, print_flags,
rtime);
}
if (plget->flags & PLF_IPGAP_STAT) {
if (plget->flags & PLF_DIS_HW_TS)
n |= stats_print("\ngap of sw rx time, us", &rx_sw_v,
print_flags | STATS_GAP_DATA, NULL);
else
n |= stats_print("\ngap of hw rx time, us", &rx_hw_v,
print_flags | STATS_GAP_DATA, NULL);
}
if (plget->flags & PLF_LATENCY_STAT) {
stats_diff(&rx_sw_v, &rx_hw_v, &temp);
n |= stats_print("\ndriver rx latency, us (no stack latency, "
"wire -> net subsystem)",
&temp, print_flags, NULL);
stats_diff(&rx_app_v, &rx_sw_v, &temp);
n |= stats_print("\nstack rx latency, us (no driver latency, "
"net subsystem -> app)",
&temp, print_flags, NULL);
stats_diff(&rx_app_v, &rx_hw_v, &temp);
n |= stats_print("\ncomplete rx latency, us (driver latency + "
"stack latency, wire -> app)",
&temp, print_flags, NULL);
}
return n;
}
static void res_rtt_print(void)
{
struct stats *a_stat, *b_stat;
char *a_ts_base, *b_ts_base;
int print_flags;
print_flags = plget->flags & PLF_PLAIN_FORMAT ? STATS_PLAIN_OUTPUT : 0;
if (ts_correct(tx_hw_v.start_ts)) {
a_stat = &tx_hw_v;
a_ts_base = "hw";
} else if (ts_correct(tx_sw_v.start_ts)) {
a_stat = &tx_sw_v;
a_ts_base = "sw";
} else {
a_stat = &tx_app_v;
a_ts_base = "app";
}
if (ts_correct(rx_hw_v.start_ts)) {
b_stat = &rx_hw_v;
b_ts_base = "hw";
} else if (ts_correct(rx_sw_v.start_ts)) {
b_stat = &rx_sw_v;
b_ts_base = "sw";
} else {
b_stat = &rx_app_v;
b_ts_base = "app";
}
printf("RTT (round trip time) for this HOST based on "
"tx %s and rx %s timestamps\n", a_ts_base, b_ts_base);
stats_diff(b_stat, a_stat, &temp);
stats_print("\nRTT (no rx/tx latencies of this HOST, us",
&temp, print_flags, NULL);
}
static struct stats *res_best_rx_vect(void)
{
if (ts_correct(rx_hw_v.start_ts))
return &rx_hw_v;
else if (ts_correct(rx_sw_v.start_ts))
return &rx_sw_v;
else
return &rx_app_v;
}
static struct stats *res_best_tx_vect(void)
{
if (ts_correct(tx_hw_v.start_ts))
return &tx_hw_v;
else if (ts_correct(tx_sw_v.start_ts))
return &tx_sw_v;
else
return &tx_app_v;
}
void res_title_print(void)
{
struct timespec ts1, ts2, res;
int ptp_fd, phc_idx;
char phc_addr[20];
if (!(plget->flags & PLF_TITLE))
return;
res_print_clock_info(CLOCK_REALTIME, "CLOCK_REALTIME");
phc_idx = (plget->phc_idx == -1) ? 0 : plget->phc_idx;
snprintf(phc_addr, sizeof(phc_addr), "/dev/ptp%u", phc_idx);
if (access(phc_addr, 0)) {
printf("PHC %s is not registered\n", phc_addr);
goto ptp_err;
}
ptp_fd = open(phc_addr, O_RDWR);
if (ptp_fd == -1) {
perror("open PHC");
goto ptp_err;
}
snprintf(phc_addr, sizeof(phc_addr), "PHC %u", phc_idx);
res_print_clock_info(ptp_fd, phc_addr);
printf("-----------------------------------------\n");
/* Check roughly if timeline is same for Sys and PHC */
clock_gettime(ptp_fd, &ts1);
clock_gettime(CLOCK_MONOTONIC, &ts2);
ts_sub(&ts2, &ts1, &res);
int usec_diff = res.tv_nsec / NSEC_PER_USEC;
if (res.tv_sec || usec_diff > 5000)
printf("You need to run phc2sys in order to align timeline\n");
printf("PHC vs CLOCK MONOTONIC = %lus %luns\n", res.tv_sec, res.tv_nsec);
close(ptp_fd);
ptp_err:
printf("-----------------------------------------\n");
}
void res_print_time(void)
{
struct timespec time;
__u64 val;
clock_gettime(CLOCK_REALTIME, &time);
val = NSEC_PER_SEC * time.tv_sec + time.tv_nsec;
printf("%llu\n", val);
}
int res_get_intf_speed(void)
{
struct ethtool_cmd edata;
struct ifreq ifr;
int ret;
memset(&ifr, 0, sizeof(ifr));
memset(&edata, 0, sizeof(edata));
strncpy(ifr.ifr_name, plget->if_name, sizeof(ifr.ifr_name));
ifr.ifr_data = (char *)&edata;
edata.cmd = ETHTOOL_GSET;
ret = ioctl(plget->sfd, SIOCETHTOOL, &ifr);
if (ret < 0) {
printf("SIOCETHTOOL failed, supposing 100Mbs: %s\n", strerror(errno));
return SPEED_100;
}
return ethtool_cmd_speed(&edata);
}
void res_stats_print(void)
{
unsigned long long int ftt;
int mod = plget->mod;
int rx_tx_lat = mod == ECHO_LAT || mod == RTT_MOD;
int print_rx_lat = mod == RX_LAT || rx_tx_lat;
int print_tx_lat = mod == TX_LAT || rx_tx_lat;
int n = 0, n2 = 0;
int header_size;
int pnum, speed;
printf("\n");
if (print_tx_lat)
n2 = res_tx_lat_print();
if (print_rx_lat)
n = res_rx_lat_print();
if (mod == ECHO_LAT || mod == RTT_MOD) {
if (n != n2)
printf("rx ts num != tx ts num: %d != %d\n", n, n2);
pnum = n > n2 ? n2 : n;
if (mod == RTT_MOD)
res_rtt_print();
} else if (mod == PKT_GEN) {
pnum = plget->pkt_num;
} else {
pnum = n | n2;
}
if (mod == RX_LAT || mod == ECHO_LAT) {
header_size = (plget->pkt_type == PKT_RAW ||
plget->pkt_type == PKT_XDP) ? 0 : ETH_HLEN;
if (plget->pkt_type == PKT_UDP)
header_size += 28;
plget->frame_size = header_size + plget->sk_payload_size;
}
if (plget->frame_size) {
speed = res_get_intf_speed();
printf("Interface speed returned: %dMbps\n", speed);
printf("Frame size: %d\n", plget->frame_size);
if (speed > 0) {
ftt = (double)(plget->frame_size * 8 * 1000) / speed;
printf("Frame transmission time: %lluns (%gus)\n", ftt,
ftt / 1000.0);
}
}
printf("number of packets: %d\n", pnum);
if (mod == TX_LAT || mod == RTT_MOD)
stats_vrate_print(res_best_tx_vect(), plget->frame_size);
if (mod == RX_LAT || mod == ECHO_LAT)
stats_vrate_print(res_best_rx_vect(), plget->frame_size);
printf("\n");
}