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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
|
/*
* Copyright 2012 Red Hat Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Authors: Ben Skeggs
*/
#include <curses.h>
#include <signal.h>
#include <ctype.h>
#include <menu.h>
#include <form.h>
#include <nvif/client.h>
#include <nvif/device.h>
#include <nvif/class.h>
#include <nvif/ioctl.h>
#include <sys/time.h>
static struct nvif_device *device;
static char **signals;
static int nr_signals;
#define SEC_US 1000000
#define REFRESH_PERIOD SEC_US
/*******************************************************************************
*
******************************************************************************/
enum ui_colour {
UI_DEFAULT = 0,
UI_BLACK_GREEN
};
struct ui_window {
WINDOW *win;
enum ui_colour colour;
const char *status;
int (*create)(struct ui_window *, int c, int r);
void (*destroy)(struct ui_window *);
void (*redraw)(struct ui_window *);
bool (*driver)(struct ui_window *, int k);
};
/*******************************************************************************
*
******************************************************************************/
struct ui_table {
struct ui_window w;
FIELD **data;
FORM *form;
int y, r, c;
char *(*layout)(struct ui_table *);
void (*change)(struct ui_table *, FIELD *, int x, int y);
void (*action)(struct ui_table *, FIELD *, int x, int y);
void (*redraw)(struct ui_table *);
bool (*driver)(struct ui_table *, int k);
};
static void
ui_field_change(FORM *f)
{
struct ui_table *t = form_userptr(f);
if (t->change) {
FIELD *f = current_field(t->form);
int i = field_index(f);
int y = i / t->c;
int x = i % t->c;
t->change(t, f, x, y);
}
}
static void
ui_table_destroy(struct ui_window *w)
{
struct ui_table *t = (void *)w;
int i = -1;
unpost_form(t->form);
free_form(t->form);
while (t->data[++i])
free_field(t->data[i]);
free(t->data);
}
static int
ui_table_create(struct ui_window *win, int c, int r)
{
struct ui_table *t = (void *)win;
char *layout = t->layout(t);
int f = layout[0];
int s = layout[1];
int l = layout[2];
int x, y, n, p;
FIELD **v;
if (s) {
n = c - (f + 1) - 1;
n /= (s + 1) + l;
l *= n;
} else {
l = max(l, c - (f + 1));
n = 0;
}
t->c = 2 + n;
t->r = r;
t->data = malloc(sizeof(*t->data) * ((t->c * t->r) + 1));
for (y = 0; y < t->r; y++) {
t->data[(y * t->c) + 0] = new_field(1, f, y, 0, 0, 0);
for (x = 1, p = (f + 1); x < n + 1; x++, p += (s + 1))
t->data[(y * t->c) + x] = new_field(1, s, y, p, 0, 0);
t->data[(y * t->c) + x] = new_field(1, l, y, p, 0, 0);
}
t->data[y * t->c] = NULL;
v = t->data - 1;
while (*(++v))
field_opts_off(*v, O_ACTIVE);
t->form = new_form(t->data);
set_form_sub(t->form, win->win);
set_form_userptr(t->form, t);
set_field_term(t->form, ui_field_change);
post_form(t->form);
return 0;
}
static void
ui_table_redraw(struct ui_window *w)
{
struct ui_table *t = (void *)w;
t->redraw(t);
}
static bool
ui_table_driver(struct ui_window *w, int k)
{
struct ui_table *t = (void *)w;
FIELD *v = current_field(t->form);
int i = field_index(v);
int y = i / t->c;
int x = i % t->c;
switch (k) {
case ' ':
t->y = 0;
return true;
case KEY_UP:
// XXX: buggy until we move the initial cursor to the correct location
if (y == 2) {
if (t->y > 0)
t->y--;
return true;
}
form_driver(t->form, REQ_UP_FIELD);
break;
case KEY_PPAGE:
t->y -= (t->r - 2);
if (t->y < 0)
t->y = 0;
return true;
case KEY_HOME:
// TODO: move cursor to top entry
t->y = 0;
return true;
case KEY_DOWN:
if ((t->y + (x - 2)) >= (nr_signals - 1))
break;
if (y == (t->r - 1)) {
t->y++;
return true;
}
form_driver(t->form, REQ_DOWN_FIELD);
break;
case KEY_NPAGE:
if ((t->y + (x - 2) + (t->r - 2)) > (nr_signals - 1))
break;
t->y += (t->r - 2);
return true;
case KEY_END:
// TODO: move cursor to top entry
t->y = nr_signals - 1;
return true;
case KEY_LEFT:
form_driver(t->form, REQ_LEFT_FIELD);
break;
case KEY_RIGHT:
form_driver(t->form, REQ_RIGHT_FIELD);
break;
case KEY_ENTER:
case '\r':
case '\n':
if (t->action)
t->action(t, v, x, y);
break;
default:
if (!t->driver || t->driver(t, k))
form_driver(t->form, k);
break;
}
return false;
}
/*******************************************************************************
*
******************************************************************************/
static void
ui_menu_redraw(struct ui_window *w)
{
const char *name = "nVPerfMon";
mvwaddstr(w->win, 0, getmaxx(w->win) - strlen(name) - 1, name);
}
static struct ui_window
ui_menu_win = {
.colour = UI_BLACK_GREEN,
.redraw = ui_menu_redraw,
.driver = NULL,
};
/*******************************************************************************
*
******************************************************************************/
struct ui_main {
struct list_head head;
u32 handle;
struct nvif_object object;
const char *name;
u32 clk;
u32 ctr;
u64 incr;
};
static struct list_head ui_main_list = LIST_HEAD_INIT(ui_main_list);
static u32 ui_main_handle = 0xc0000000;
static void
ui_main_remove(struct ui_main *item)
{
nvif_object_fini(&item->object);
list_del(&item->head);
free(item);
}
static void
ui_main_select(void)
{
struct ui_main *item, *temp;
int ret, i;
list_for_each_entry_safe(item, temp, &ui_main_list, head) {
ui_main_remove(item);
}
for (i = 0; i < nr_signals; i++) {
struct nvif_perfctr_v0 args = {
.logic_op = 0xaaaa,
};
item = calloc(1, sizeof(*item));
item->handle = ui_main_handle++;
item->name = signals[i];
item->incr = 0;
strncpy(args.name[0], item->name, sizeof(args.name[0]));
ret = nvif_object_init(nvif_object(device), NULL, item->handle,
NVIF_IOCTL_NEW_V0_PERFCTR, &args,
sizeof(args), &item->object);
assert(ret == 0);
list_add_tail(&item->head, &ui_main_list);
}
}
static void
ui_main_alarm_handler(int signal)
{
struct ui_main *item;
bool sampled = false;
if (list_empty(&ui_main_list))
ui_main_select();
list_for_each_entry(item, &ui_main_list, head) {
struct nvif_perfctr_read_v0 args = {};
int ret;
if (!sampled) {
struct nvif_perfctr_sample args = {};
ret = nvif_mthd(&item->object, NVIF_PERFCTR_V0_SAMPLE, &args, sizeof(args));
assert(ret == 0);
sampled = true;
}
ret = nvif_mthd(&item->object, NVIF_PERFCTR_V0_READ, &args, sizeof(args));
assert(ret == 0 || ret == -EAGAIN);
if (ret == 0) {
item->clk = args.clk;
item->ctr = args.ctr;
}
item->incr += item->ctr;
}
}
static struct sigaction
ui_main_alarm = {
.sa_handler = ui_main_alarm_handler,
};
static char *
ui_main_layout(struct ui_table *t)
{
return "\x10\x00\x01";
}
static void
ui_main_action(struct ui_table *t, FIELD *f, int x, int y)
{
void *priv = field_userptr(f);
(void)priv;
}
static void
ui_main_redraw(struct ui_table *t)
{
struct ui_main *item;
FIELD **f = t->data;
char b[128];
int y;
set_field_buffer(f[1], 0, " Samples Count % Total");
f += t->c * 2;
// TODO: move cursor to top entry
y = -1;
list_for_each_entry(item, &ui_main_list, head) {
if (++y >= t->y)
break;
}
y = 2;
list_for_each_entry_from(item, &ui_main_list, head) {
set_field_buffer(f[0], 0, item->name);
set_field_userptr(f[0], item);
field_opts_on(f[0], O_VISIBLE | O_ACTIVE);
snprintf(b, sizeof(b), "%10u %10u %6.2f %16"PRIu64,
item->clk, item->ctr,
(float)item->ctr * 100.0 / item->clk, item->incr);
set_field_buffer(f[1], 0, b);
field_opts_on(f[1], O_VISIBLE);
if (++y == t->r)
break;
f += t->c;
}
for (; y < t->r; y++) {
field_opts_off(f[0], O_VISIBLE | O_ACTIVE);
field_opts_off(f[1], O_VISIBLE);
set_field_userptr(f[0], NULL);
f += t->c;
}
}
static bool
ui_main_driver(struct ui_window *w, int k)
{
struct ui_table *t = (void *)w;
struct ui_main *item, *temp;
FIELD *f;
switch (k) {
case KEY_DC:
case KEY_DL:
f = current_field(t->form);
item = field_userptr(f);
if (item) {
ui_main_remove(item);
return true;
}
break;
case 'x':
list_for_each_entry_safe(item, temp, &ui_main_list, head) {
if (item->incr == 0)
ui_main_remove(item);
}
break;
default:
return ui_table_driver(w, k);
}
return false;
}
static int
ui_main_create(struct ui_window *win, int c, int r)
{
int ret = ui_table_create(win, c, r);
if (ret == 0) {
sigaction(SIGALRM, &ui_main_alarm, 0);
ualarm(1, 999999);
}
return ret;
}
static struct ui_table
ui_main_f = {
.w.status = "main",
.w.colour = UI_DEFAULT,
.w.create = ui_main_create,
.w.destroy = ui_table_destroy,
.w.redraw = ui_table_redraw,
.w.driver = ui_main_driver,
.layout = ui_main_layout,
.action = ui_main_action,
.redraw = ui_main_redraw,
};
/*******************************************************************************
*
******************************************************************************/
static void
ui_stat_redraw(struct ui_window *w)
{
}
static struct ui_window
ui_stat_win = {
.colour = UI_BLACK_GREEN,
.redraw = ui_stat_redraw,
.driver = NULL,
};
/*******************************************************************************
*
******************************************************************************/
static struct ui_layout {
struct ui_window *w;
int parent;
int horiz;
int percent;
int adjust;
int border;
} ui[] = {
{ &ui_menu_win, -1, 0, 0, +1, 0 },
{ &ui_main_f.w, -1, 0, 100, -2, 0 },
{ &ui_stat_win, -1, 0, 0, +1, 0 },
{}
}, *active = &ui[1];
static void
ui_redraw_win(struct ui_window *w)
{
wclear(w->win);
if (w->redraw)
w->redraw(w);
wrefresh(w->win);
}
static void
ui_active(int adjust)
{
do {
if ((active += adjust)->w == NULL) {
active = &ui[0];
adjust = 1;
}
} while(!active->w->driver);
wrefresh(active->w->win);
}
static void
ui_redraw(void)
{
struct ui_layout *l = ui - 1;
struct ui_window *w;
clear();
refresh();
while ((w = (++l)->w))
ui_redraw_win(w);
ui_active(0);
}
static void
ui_create(void)
{
struct ui_layout *l = ui - 1;
struct ui_window *w;
WINDOW *p = stdscr;
int mc = COLS, mr = LINES;
int ac = 0, ar = 0;
int x, y, c, r;
int lp = -1;
refresh();
while ((w = (++l)->w)) {
if (l->parent != lp) {
p = ui[l->parent].w->win;
mc = getmaxx(p);
mr = getmaxy(p);
lp = l->parent;
ac = ar = 0;
}
if (l->horiz) {
if ((c = l->percent)) {
if ((l + 1)->parent == l->parent)
c = ((mc * l->percent) / 100);
else
c = mc - ac;
}
c = c + l->adjust;
r = mr + (l->adjust * l->border);
y = 0;
x = ac; ac += c;
} else {
c = mc + (l->adjust * l->border);
if ((r = l->percent)) {
if ((l + 1)->parent == l->parent)
r = ((mr * l->percent) / 100);
else
r = mr - ar;
}
r = r + l->adjust;
x = 0;
y = ar; ar += r;
}
x += l->border;
y += l->border;
w->win = derwin(p, r, c, y, x);
wbkgd(w->win, COLOR_PAIR(w->colour));
if (w->create)
w->create(w, c, r);
}
ui_redraw();
}
static void
ui_destroy(void)
{
struct ui_layout *l = ui + ARRAY_SIZE(ui);
struct ui_window *w;
while ((w = (--l)->w)) {
if (w->destroy)
w->destroy(w);
delwin(w->win);
w->win = NULL;
}
}
static void
ui_resize(void)
{
ui_destroy();
ui_create();
}
int
main(int argc, char **argv)
{
const char *drv = NULL;
const char *cfg = NULL;
const char *dbg = "error";
u64 dev = ~0ULL;
struct nvif_perfctr_query_v0 args = {};
struct nvif_client *client;
struct nvif_object object;
int ret, c, k;
int scan = 0;
while ((c = getopt(argc, argv, "-a:b:c:d:s")) != -1) {
switch (c) {
case 'a': dev = strtoull(optarg, NULL, 0); break;
case 'b': drv = optarg; break;
case 'c': cfg = optarg; break;
case 'd': dbg = optarg; break;
case 's':
scan = 1;
break;
case 1:
return -EINVAL;
}
}
ret = nvif_client_new(drv, argv[0], dev, cfg, dbg, &client);
if (ret)
return ret;
ret = nvif_device_new(nvif_object(client), 0x00000000,
NV_DEVICE, &(struct nv_device_v0) {
.device = ~0ULL,
.disable = ~(NV_DEVICE_V0_DISABLE_MMIO |
NV_DEVICE_V0_DISABLE_VBIOS |
NV_DEVICE_V0_DISABLE_CORE |
NV_DEVICE_V0_DISABLE_IDENTIFY),
.debug0 = ~((1ULL << NVDEV_SUBDEV_TIMER) |
(1ULL << NVDEV_ENGINE_PERFMON)),
}, sizeof(struct nv_device_v0), &device);
nvif_client_ref(NULL, &client);
if (ret)
return ret;
if (scan) {
fprintf(stderr, "unimplemented\n");
return 1;
}
ret = nvif_object_init(nvif_object(device), NULL, 0xdeadbeef,
NVIF_IOCTL_NEW_V0_PERFCTR,
&(struct nvif_perfctr_v0) {
}, sizeof(struct nvif_perfctr_v0), &object);
assert(ret == 0);
do {
u32 prev_iter = args.iter;
args.name[0] = '\0';
ret = nvif_mthd(&object, NVIF_PERFCTR_V0_QUERY, &args, sizeof(args));
assert(ret == 0);
if (prev_iter) {
nr_signals++;
signals = realloc(signals, nr_signals * sizeof(char*));
signals[nr_signals - 1] = malloc(sizeof(args.name));
args.iter = prev_iter;
strncpy(signals[nr_signals - 1], args.name,
sizeof(args.name));
ret = nvif_mthd(&object, NVIF_PERFCTR_V0_QUERY, &args, sizeof(args));
assert(ret == 0);
}
} while (args.iter != 0xffffffff);
nvif_object_fini(&object);
initscr();
keypad(stdscr, TRUE);
nonl();
cbreak();
noecho();
if (has_colors())
start_color();
init_pair(UI_BLACK_GREEN, COLOR_BLACK, COLOR_GREEN);
ui_create();
while ((k = getch()) != '\x1b') {
switch (k) {
case KEY_RESIZE:
ui_resize();
break;
default:
active->w->driver(active->w, k);
break;
}
active->w->redraw(active->w);
}
ui_destroy();
endwin();
while (nr_signals--)
free(signals[nr_signals]);
free(signals);
nvif_device_ref(NULL, &device);
return 0;
}
|