-
Notifications
You must be signed in to change notification settings - Fork 8
/
bqlmon.c
651 lines (535 loc) · 13.5 KB
/
bqlmon.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
* BQLmon
*
* Copyright (C) 2014, Florian Fainelli <[email protected]>
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include <curses.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/timerfd.h>
#include <linux/sockios.h>
#include "bqlmon.h"
#define DEFAULT_SCALING (1024) // This is a lousy way to do scaling
/* Locate the network interface and count its number of transmit queues */
static int bql_sysfs_init(struct bql_ctx *ctx)
{
struct dirent **namelist;
char path[PATH_MAX];
struct utsname uts;
int maj, min, rel;
int n;
n = uname(&uts);
if (n < 0)
goto try_file;
if (strcmp(uts.sysname, "Linux")) {
fprintf(stderr, "Unsupported OS: %s\n", uts.sysname);
return -1;
}
n = sscanf(uts.release, "%d.%d.%d", &maj, &min, &rel);
if (n < 3)
return -1;
if (maj < 3 && min < 3) {
fprintf(stderr, "Kernel too old, requires 3.3 for BQL\n");
return -1;
}
try_file:
n = snprintf(path, sizeof(path), "/sys/class/net/%s/queues/",
ctx->iface);
if (n <= 0)
return -1;
n = scandir(path, &namelist, NULL, alphasort);
if (n < 0) {
perror("scandir");
return n;
}
while (n--) {
if (!strncmp(namelist[n]->d_name, "tx-", 3))
ctx->num_queues++;
free(namelist[n]);
}
free(namelist);
if (ctx->num_queues == 0) {
fprintf(stderr, "Kernel too old, or invalid network device\n");
return -1;
}
return 0;
}
static int bql_get_drv_info(struct bql_ctx *ctx)
{
struct ifreq ifr;
int fd, ret;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
return fd;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ctx->iface);
ctx->info.cmd = ETHTOOL_GDRVINFO;
ifr.ifr_data = (void *)&ctx->info;
ret = ioctl(fd, SIOCETHTOOL, &ifr);
close(fd);
if (ret < 0)
return ret;
return 0;
}
static int bql_sysfs_file_init(struct bql_sysfs_attr *s, const char *dir,
const char *name)
{
char path[PATH_MAX];
size_t n;
n = snprintf(path, sizeof(path), "%s/%s", dir, name);
if (n <= 0)
return -1;
s->fd = open(path, O_RDONLY);
if (s->fd < 0) {
perror("open");
return s->fd;
}
return 0;
}
static int bql_sysfs_file_read(struct bql_sysfs_attr *s)
{
ssize_t n;
off_t r;
/* Rewind to the beginning */
r = lseek(s->fd, 0, SEEK_SET);
if (r < 0) {
perror("lseek");
return r;
}
n = read(s->fd, s->s_val, sizeof(s->s_val));
if (n < 0) {
perror("read");
return n;
}
n = sscanf(s->s_val, "%d", &s->value);
if (n < 1)
return -1;
return 0;
}
static const char *attr_names[] = {
"hold_time", "inflight", "limit", "limit_max", "limit_min" };
#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
static int bql_queue_init(struct bql_q_ctx *q)
{
char path[PATH_MAX];
size_t n;
unsigned int i;
int ret;
n = snprintf(path, sizeof(path),
"/sys/class/net/%s/queues/tx-%d/byte_queue_limits",
q->g->iface, q->queue_num);
if (n <= 0)
return -1;
for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
ret = bql_sysfs_file_init(&q->attrs[i], path, attr_names[i]);
if (ret)
return ret;
}
return 0;
}
static void bql_queue_exit(struct bql_q_ctx *q)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
if (q->attrs[i].fd >= 0) {
close(q->attrs[i].fd);
q->attrs[i].fd = -1;
}
}
}
static inline unsigned int bql_poll_one_queue(struct bql_q_ctx *ctx)
{
bql_sysfs_file_read(&ctx->attrs[INFLIGHT]);
bql_sysfs_file_read(&ctx->attrs[LIMIT]);
return ctx->attrs[INFLIGHT].value;
}
static void bql_queues_destroy(struct bql_ctx *ctx)
{
unsigned int i;
for (i = 0; i < ctx->num_queues; i++)
bql_queue_exit(&ctx->queues[i]);
free(ctx->queues);
}
static int bql_queues_create(struct bql_ctx *ctx)
{
struct bql_q_ctx *tctx;
unsigned int i;
size_t size;
int ret;
size = sizeof(struct bql_q_ctx) * ctx->num_queues;
ctx->queues = malloc(size);
if (!ctx->queues)
return -ENOMEM;
memset(ctx->queues, 0, size);
for (i = 0; i < ctx->num_queues; i++) {
tctx = &ctx->queues[i];
tctx->queue_num = i;
tctx->g = ctx;
ret = bql_queue_init(tctx);
if (ret) {
fprintf(stderr, "failed to initialize queue %d\n", i);
return ret;
}
}
return ret;
}
static int get_color_thresh(unsigned int val, unsigned int limit)
{
if (val <= limit / 3)
return 2;
else if (val <= (limit * 2) / 3)
return 3;
else if (val <= limit)
return 1;
else
return 6;
}
static void bql_draw_arrows(struct bql_ctx *ctx, unsigned int q,
unsigned int limit)
{
unsigned int i;
chtype ch;
int y, x;
x = q * QUEUE_SPACING + QUEUE_VAL_X - ctx->x_start;
y = ctx->rows - QUEUE_VAL_Y - limit - QUEUE_ARROW_Y;
if (q == ctx->vq_start && q != 0) {
for (i = 0; i < 3; i++) {
if (i == 0)
ch = ACS_LARROW;
else
ch = ACS_HLINE;
mvwaddch(ctx->w, y, x + i, ch);
}
}
if (q == ctx->vq_end - 1 && q != ctx->num_queues - 1) {
for (i = 3; i-- > 0; ) {
if (i == 0)
ch = ACS_RARROW;
else
ch = ACS_HLINE;
mvwaddch(ctx->w, y, x - i, ch);
}
}
}
static int bql_output_one(struct bql_ctx *ctx, unsigned int q)
{
struct bql_q_ctx *qctx = &ctx->queues[q];
return(bql_poll_one_queue(qctx));
}
static void bql_draw_one(struct bql_ctx *ctx, unsigned int q)
{
struct bql_q_ctx *qctx = &ctx->queues[q];
unsigned int val, limit;
unsigned int i;
int color, rows;
char buf[128];
int x;
rows = ctx->rows;
val = bql_poll_one_queue(qctx)/ctx->scaling;
limit = ctx->queues[q].attrs[LIMIT].value/ctx->scaling;
x = q * QUEUE_SPACING + QUEUE_VAL_X - ctx->x_start;
snprintf(buf, sizeof(buf), "%02u", q);
/* Draw the queue number */
wattron(ctx->w, COLOR_PAIR(5));
wattron(ctx->w, A_BOLD);
mvwaddstr(ctx->w, rows - QUEUE_NUM_Y, q * QUEUE_SPACING + QUEUE_SEP_X - ctx->x_start, buf);
wattroff(ctx->w, A_BOLD);
wattroff(ctx->w, COLOR_PAIR(5));
/* Draw the queue value as a histogram */
for (i = 0; i < val; i++) {
color = get_color_thresh(i, limit);
wattron(ctx->w, COLOR_PAIR(color));
mvwaddch(ctx->w, rows - QUEUE_VAL_Y - i, x, QUEUE_CHAR | A_BOLD);
wattroff(ctx->w, COLOR_PAIR(color));
}
/* Display the queue limit value */
mvwaddch(ctx->w, rows - QUEUE_VAL_Y - limit, x, ACS_BLOCK);
/* Display the arrows to indicate there is something */
bql_draw_arrows(ctx, q, limit);
}
static void bql_draw_main_items(struct bql_ctx *ctx)
{
int y = PARAMS_Y;
box(ctx->w, 0, 0);
wmove(ctx->w, y, PARAMS_X);
waddstr(ctx->w, "Interface: ");
wattron(ctx->w, A_BOLD);
waddstr(ctx->w, ctx->iface);
wattroff(ctx->w, A_BOLD);
wmove(ctx->w, ++y, PARAMS_X);
waddstr(ctx->w, "Frequency: ");
wattron(ctx->w, A_BOLD);
wprintw(ctx->w, "%d (msecs)", ctx->poll_freq);
wattroff(ctx->w, A_BOLD);
wmove(ctx->w, ++y, PARAMS_X);
waddstr(ctx->w, "Driver: ");
wattron(ctx->w, A_BOLD);
wprintw(ctx->w, "%s (%s)", ctx->info.driver, ctx->info.version);
wattroff(ctx->w, A_BOLD);
/* Draw the separation line between queue number and values */
wmove(ctx->w, ++y, PARAMS_X);
mvwhline(ctx->w, ctx->rows - QUEUE_SEP_Y, QUEUE_SEP_X, ACS_HLINE,
ctx->h_line_val);
y = PARAMS_Y;
wattron(ctx->w, A_BOLD);
mvwaddstr(ctx->w, y, ctx->version_x_pos, "BQLmon");
wattroff(ctx->w, A_BOLD);
wmove(ctx->w, ++y, ctx->version_x_pos);
waddstr(ctx->w, "Version: ");
wattron(ctx->w, A_BOLD);
wprintw(ctx->w, "%s", VERSION);
wattroff(ctx->w, A_BOLD);
wattron(ctx->w, A_BOLD);
mvwaddstr(ctx->w, ++y, ctx->version_x_pos, "Q to exit");
wattroff(ctx->w, A_BOLD);
}
static void bql_recalc_visible_queues(struct bql_ctx *ctx)
{
unsigned int val;
/* Get the number of times we need to repeat the ACS_HLINE */
val = ctx->num_queues * QUEUE_SPACING;
if (val >= ctx->cols)
val = ctx->cols - 2 * QUEUE_SEP_Y;
ctx->h_line_val = val;
ctx->vq_start = ctx->x_start / QUEUE_SPACING;
ctx->vq_end = ctx->cols / QUEUE_SPACING + ctx->vq_start - 1;
if (ctx->vq_end >= ctx->num_queues)
ctx->vq_end = ctx->num_queues;
}
static void ts_add(struct timespec *t, uint64_t ns)
{
t->tv_nsec = t->tv_nsec + ns;
if(t->tv_nsec >= 1000000000L) {
t->tv_sec++;
t->tv_nsec = t->tv_nsec - 1000000000L ;
}
}
static void bql_output_loop(struct bql_ctx *ctx)
{
unsigned int q, exit = 0;
uint64_t expirations = 0;
uint64_t totals = 0;
struct timespec time;
read(ctx->timer,&expirations,sizeof(uint64_t));
clock_gettime(CLOCK_REALTIME, &time);
q = ctx->monitor;
while (!exit) {
fprintf(ctx->fd,"%ld.%ld", time.tv_sec, time.tv_nsec);
if(q >= 0 ) {
int x = bql_output_one(ctx,q);
fprintf(ctx->fd, " %d %d",
ctx->queues[q].attrs[LIMIT].value, x);
fprintf(ctx->fd,"\n");
} else {
for (q = ctx->vq_start; q <= ctx->vq_end; q++) {
int x = bql_output_one(ctx,q);
fprintf(ctx->fd, " %d %d",
ctx->queues[q].attrs[LIMIT].value, x);
}
q = -1;
fprintf(ctx->fd,"\n");
}
read(ctx->timer,&expirations,sizeof(uint64_t));
ts_add(&time, expirations * ctx->poll_freq * 1000000);
if(ctx->count > 0) {
totals += expirations;
if(totals >= ctx->count)
exit = 1;
}
}
}
static void bql_draw_loop(struct bql_ctx *ctx)
{
unsigned int q, exit = 0;
int ch;
uint64_t expirations;
struct timespec time;
read(ctx->timer,&expirations,sizeof(uint64_t));
clock_gettime(CLOCK_REALTIME, &time);
while (!exit) {
wclear(ctx->w);
bql_draw_main_items(ctx);
for (q = ctx->vq_start; q < ctx->vq_end; q++)
bql_draw_one(ctx, q);
ch = wgetch(ctx->w);
switch (ch) {
case 'Q':
case 'q':
exit = 1;
break;
case KEY_LEFT:
if (ctx->x_start >= QUEUE_SPACING)
ctx->x_start -= QUEUE_SPACING;
break;
case KEY_RIGHT:
if (ctx->vq_end < ctx->num_queues)
ctx->x_start += QUEUE_SPACING;
break;
case KEY_RESIZE:
endwin();
getmaxyx(stdscr, ctx->rows, ctx->cols);
break;
default:
read(ctx->timer,&expirations,sizeof(uint64_t));
ts_add(&time, expirations * ctx->poll_freq * 1000000);
break;
}
bql_recalc_visible_queues(ctx);
wrefresh(ctx->w);
}
}
static int bql_init_term(struct bql_ctx *ctx)
{
int rows, cols;
initscr();
getmaxyx(stdscr, rows, cols);
cbreak();
noecho();
curs_set(0);
ctx->rows = rows;
ctx->cols = cols;
ctx->x_start = 0;
ctx->w = newwin(rows, cols, 0, 0);
if (!ctx->w) {
fprintf(stderr, "failed to create window\n");
return 1;
}
keypad(ctx->w, TRUE);
nodelay(ctx->w, TRUE);
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_WHITE, COLOR_BLACK);
init_pair(5, COLOR_WHITE, COLOR_BLUE);
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
bql_recalc_visible_queues(ctx);
ctx->version_x_pos = ctx->cols - strlen("Version: ") -
strlen(VERSION) - QUEUE_SPACING;
return 0;
}
static void usage(const char *pname)
{
fprintf(stderr, "Usage: %s [options]\n"
"-i: interface\n"
"-f: poll frequency (msecs)\n"
"-s: scaling (higher means shorter bars, default 1024\n"
"-o: output file or - for stdout\n"
"-c: count (run for count * frequency) \n"
"-Q: queue monitor specific BQL queue \n"
"-h: this help\n", pname);
}
int main(int argc, char **argv)
{
struct bql_ctx *ctx;
int opt, ret;
struct itimerspec interval;
ctx = calloc(sizeof(*ctx),1);
if (!ctx)
return -ENOMEM;
ctx->monitor = -1;
ctx->scaling = DEFAULT_SCALING;
while ((opt = getopt(argc, argv, "c:Q:o:i:f:s:h")) > 0) {
switch (opt) {
case 'i':
ctx->iface = optarg;
break;
case 'f':
ctx->poll_freq = strtoul(optarg, 0, 10);
break;
case 's':
ctx->scaling = strtoul(optarg, 0, 10);
break;
case 'c':
ctx->count = strtoul(optarg, 0, 10);
break;
case 'o':
ctx->filename = optarg;
break;
case 'Q':
ctx->monitor = strtoul(optarg, 0, 10);
break;
default:
usage(argv[0]);
exit(EXIT_SUCCESS);
break;
}
}
argc -= optind;
argv += optind;
if (!ctx->iface)
ctx->iface = "eth0";
if (!ctx->poll_freq)
ctx->poll_freq = 10;
if (ctx->filename) {
if(strcmp(ctx->filename,"-") == 0)
ctx->fd = stdout;
else
ctx->fd = fopen(ctx->filename,"w");
if(!ctx->fd) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
ret = bql_get_drv_info(ctx);
if (ret)
goto out;
ret = bql_sysfs_init(ctx);
if (ret)
goto out;
if (ctx->monitor >= 1 && ctx->num_queues - 1 > ctx->monitor) {
fprintf(stderr, "Queue %d out of range\n", ctx->monitor);
goto out;
}
ret = bql_queues_create(ctx);
if (ret)
goto out;
interval.it_interval.tv_sec = 0;
interval.it_interval.tv_nsec = ctx->poll_freq * 1000000;
interval.it_value.tv_sec = 0;
interval.it_value.tv_nsec = ctx->poll_freq * 1000000;
ctx->timer = timerfd_create(CLOCK_MONOTONIC, 0);
if (ctx->timer) {
if (timerfd_settime(ctx->timer, 0, &interval, NULL) == -1)
fprintf(stderr,("timerfd_settime"));
}
if(!ctx->filename) {
ret = bql_init_term(ctx);
if (ret)
goto out_win;
bql_draw_loop(ctx);
} else {
bql_output_loop(ctx);
}
out_win:
endwin();
out:
bql_queues_destroy(ctx);
free(ctx);
return 0;
}