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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
|
/**
* @file ztop.c
* @brief What z/OS has where: storage by subpool, the system, and jobs.
*
* One-shot reports for scripts and a full-screen view for people. The
* view is built on libtui, which knows nothing about z/OS: ztop supplies
* the model, the messages and the tables.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include <time.h>
#include <unistd.h>
#include "sys.h"
#include "tui.h"
#include "vsm.h"
#define ZTOP_VERSION "0.3"
#define WORK_SIZE (1024u * 1024u)
#if defined(__MVS__)
/* HLASM upcases the ESD name of the CSECT, so the assembler side is
* exported as ZVSLIST whatever case the source is written in. */
extern int zvslist(void *) __asm__("ZVSLIST");
#pragma linkage(zvslist, OS)
/* The 31-bit request block. R13 is saved by the stub, so the save area
* has to live in storage the C compiler gives us, not in the read-only
* CSECT. */
struct zreq {
int area;
unsigned len;
void *wk;
int rc;
void *self;
unsigned long long save[18];
int space;
};
_Static_assert(offsetof(struct zreq, self) == 16, "zreq.self");
_Static_assert(offsetof(struct zreq, save) == 24, "zreq.save");
_Static_assert(offsetof(struct zreq, space) == 168, "zreq.space");
static unsigned char work[WORK_SIZE] __attribute__((aligned(8)));
#endif
static const char *const area_name[] = {"csa", "sqa", "lsqa", "pvt"};
static const char *const area_upper[] = {"CSA", "SQA", "LSQA", "PVT"};
/** @brief A byte count in K, M or G, whichever fits best. */
static void human(uint64_t n, char *buf, size_t len) {
if (n >= (1ULL << 30))
(void)snprintf(buf, len, "%.1fG", (double)n / (double)(1ULL << 30));
else if (n >= (1ULL << 20))
(void)snprintf(buf, len, "%.1fM", (double)n / (double)(1ULL << 20));
else if (n >= (1ULL << 10))
(void)snprintf(buf, len, "%.1fK", (double)n / (double)(1ULL << 10));
else
(void)snprintf(buf, len, "%llu", (unsigned long long)n);
}
#if defined(__MVS__)
static void hexdump(const char *tag, const unsigned char *p, unsigned int len,
unsigned int max) {
unsigned int i, j;
if (len > max)
len = max;
for (i = 0; i < len; i += 16) {
(void)fprintf(stderr, "%s %04X:", tag, i);
for (j = 0; j < 16 && i + j < len; j++)
(void)fprintf(stderr, " %02X", p[i + j]);
(void)fputc('\n', stderr);
}
}
static void dump_work(const struct vsm_info *v, const unsigned char *workp,
const unsigned char *data, unsigned int len, int rc) {
(void)fprintf(stderr, "ztop: VSMLIST %s %s rc=%d data=%08X len=%u\n",
area_name[v->area - 1],
v->space == VSM_UNALLOC ? "UNALLOC" : "FREE", rc,
(unsigned int)(uintptr_t)data, len);
hexdump("work", workp, 32, 32);
if (data != NULL && len > 0)
hexdump("data", data, len, 256);
}
static bool fetch_one(struct vsm_info *v, int area, int space, bool dump) {
int calls = 0;
const unsigned char *data;
unsigned int len;
vsm_reset(v, (unsigned int)area, (unsigned int)space);
memset(work, 0, 4); /* the return code must start at 0 */
for (;;) {
struct zreq req = {0};
bool ok;
if (++calls > 64) {
(void)snprintf(v->err, sizeof v->err,
"VSMLIST did not finish after 64 calls");
return false;
}
req.area = area;
req.space = space;
req.len = WORK_SIZE;
req.wk = work;
req.rc = -1;
(void)zvslist(&req);
if (req.rc != 0 && req.rc != 4) {
(void)snprintf(v->err, sizeof v->err, "VSMLIST returned %d",
req.rc);
return false;
}
if (!vsm_work_data(work, WORK_SIZE, &data, &len)) {
(void)snprintf(v->err, sizeof v->err,
"VSMLIST work area has an unusable data pointer");
return false;
}
if (dump)
dump_work(v, work, data, len, req.rc);
ok = space == VSM_UNALLOC ? vsm_parse_unalloc(v, data, len)
: vsm_parse_alloc(v, data, len);
if (!ok)
return false;
if (req.rc == 0)
return true;
/* rc 4: the service wants the same request and work area again. */
}
}
#else
static bool fetch_one(struct vsm_info *v, int area, int space, bool dump) {
(void)area;
(void)space;
(void)dump;
(void)snprintf(v->err, sizeof v->err, "VSMLIST is only available on z/OS");
return false;
}
#endif
static bool collect_area(struct vsm_info *v, int area, bool dump) {
if (!fetch_one(v, area, VSM_FREE, dump))
return false;
if (area == VSM_CSA || area == VSM_PVT) {
struct vsm_info u;
if (fetch_one(&u, area, VSM_UNALLOC, dump)) {
v->unalloc = u.unalloc;
v->unalloc_blocks = u.unalloc_blocks;
v->largest_unalloc = u.largest_unalloc;
v->nregion = u.nregion;
memcpy(v->region, u.region, sizeof v->region);
v->unalloc_valid = true;
} else {
(void)snprintf(v->err, sizeof v->err, "%s", u.err);
}
}
return true;
}
/** @brief Fraction of the mapped area in use, or -1 when there is no total. */
static int pct(const struct vsm_info *v) {
uint64_t total;
if (!v->unalloc_valid || v->area != VSM_CSA)
return -1;
total = v->foot + v->unalloc;
if (total == 0)
return 0;
return (int)((v->used * 100u + total / 2) / total);
}
static void print_area(const struct vsm_info *v) {
const char *name = area_name[v->area - 1];
bool pvt = v->area == VSM_PVT;
int p = pct(v);
int i;
(void)printf("area %s: used %llu free %llu mapped %llu blocks %u", name,
(unsigned long long)v->used, (unsigned long long)v->free,
(unsigned long long)v->foot, v->blocks);
if (v->area == VSM_CSA && v->unalloc_valid)
(void)printf(" unallocated %llu total %llu use %d%%",
(unsigned long long)v->unalloc,
(unsigned long long)(v->foot + v->unalloc), p);
if (pvt)
(void)printf(" tcbs %u", v->tcbs);
(void)printf("\n");
if (pvt)
(void)printf("%3s %3s %8s %5s %10s %10s %10s %10s %8s\n", "sp", "key",
"tcb", "blks", "used", "free", "mapped", "largest",
"addr");
else
(void)printf("%3s %3s %6s %10s %10s %10s %10s %17s\n", "sp", "key",
"blks", "used", "free", "mapped", "largest", "address");
for (i = 0; i < v->nsub; i++) {
const struct vsm_subpool *s = &v->sub[i];
char addr[32];
if (s->blocks == 0)
(void)snprintf(addr, sizeof addr, "-");
else if (pvt)
(void)snprintf(addr, sizeof addr, "%08X", (unsigned int)s->lo);
else
(void)snprintf(addr, sizeof addr, "%08X-%08X", (unsigned int)s->lo,
(unsigned int)s->hi);
if (pvt)
(void)printf("%3u %3u %08X %5u %10llu %10llu %10llu %10llu %s\n",
s->id, s->key, (unsigned int)s->tcb, s->blocks,
(unsigned long long)s->used,
(unsigned long long)s->free,
(unsigned long long)s->foot,
(unsigned long long)s->largest, addr);
else
(void)printf("%3u %3u %6u %10llu %10llu %10llu %10llu %s\n", s->id,
s->key, s->blocks, (unsigned long long)s->used,
(unsigned long long)s->free,
(unsigned long long)s->foot,
(unsigned long long)s->largest, addr);
}
if (v->truncated)
(void)printf("(subpool list truncated at %d rows)\n", VSM_MAX_SUB);
if (v->unalloc_valid) {
(void)printf("unallocated %llu bytes in %u blocks, largest %llu",
(unsigned long long)v->unalloc, v->unalloc_blocks,
(unsigned long long)v->largest_unalloc);
for (i = 0; i < v->nregion && i < VSM_MAX_REGION; i++)
(void)printf("%s %08X+%08X", i == 0 ? " regions" : ",",
(unsigned int)v->region[i].addr,
(unsigned int)v->region[i].len);
(void)printf("\n");
}
if (v->err[0] != '\0')
(void)printf("warning: %s\n", v->err);
}
static void print_system(const struct zsys *z) {
(void)printf(
"system %s host %s os %s machine %s address spaces %d/%u\n",
z->sysname, z->host, z->os, z->machine, z->njobs, z->slots);
}
static void print_jobs(const struct zsys *z) {
int i;
(void)printf("%4s %-8s %8s\n", "asid", "job", "ascb");
for (i = 0; i < z->njobs; i++)
(void)printf("%4u %-8s %08X\n", z->job[i].asid, z->job[i].name,
(unsigned int)z->job[i].ascb);
}
struct app {
struct vsm_info info[4];
bool have[4];
char err[4][160];
struct zsys sys;
bool have_sys;
char sys_err[160];
int interval;
int collect_ms;
bool paused;
bool help;
int tab;
int sub;
struct tui_table storage;
struct tui_table jobs;
struct tui_app *tapp;
char host[65];
};
static const struct tui_col storage_cols[] = {
{"SP", 3, true}, {"KY", 3, true}, {"BLKS", 6, true},
{"USED", 10, true}, {"FREE", 10, true}, {"MAPPED", 10, true},
{"LARGEST", 10, true}, {"ADDRESS", 17, false},
};
static const struct tui_col pvt_cols[] = {
{"SP", 3, true}, {"KY", 3, true}, {"BLKS", 5, true},
{"USED", 10, true}, {"FREE", 10, true}, {"MAPPED", 10, true},
{"LARGEST", 10, true}, {"TCB", 9, false},
};
static const struct tui_col job_cols[] = {
{"ASID", 4, true},
{"JOB", 8, false},
{"ASCB", 10, false},
};
static void storage_cell(void *ctx, int row, int col, char *buf, size_t len) {
const struct app *a = ctx;
const struct vsm_subpool *s = &a->info[a->sub].sub[row];
char u[16], f[16], m[16], l[16];
switch (col) {
case 0:
(void)snprintf(buf, len, "%u", s->id);
break;
case 1:
(void)snprintf(buf, len, "%u", s->key);
break;
case 2:
(void)snprintf(buf, len, "%u", s->blocks);
break;
case 3:
human(s->used, u, sizeof u);
(void)snprintf(buf, len, "%s", u);
break;
case 4:
human(s->free, f, sizeof f);
(void)snprintf(buf, len, "%s", f);
break;
case 5:
human(s->foot, m, sizeof m);
(void)snprintf(buf, len, "%s", m);
break;
case 6:
human(s->largest, l, sizeof l);
(void)snprintf(buf, len, "%s", l);
break;
default:
if (a->sub == 3)
(void)snprintf(buf, len, "%08X", (unsigned int)s->tcb);
else if (s->blocks == 0)
(void)snprintf(buf, len, "-");
else
(void)snprintf(buf, len, "%08X-%08X", (unsigned int)s->lo,
(unsigned int)s->hi);
break;
}
}
static void job_cell(void *ctx, int row, int col, char *buf, size_t len) {
const struct app *a = ctx;
const struct zjob *j = &a->sys.job[row];
switch (col) {
case 0:
(void)snprintf(buf, len, "%u", j->asid);
break;
case 1:
(void)snprintf(buf, len, "%s", j->name);
break;
default:
(void)snprintf(buf, len, "%08X", (unsigned int)j->ascb);
break;
}
}
static void sys_read(struct app *a) {
struct zsys z;
struct utsname u;
(void)memcpy(&z, &a->sys, sizeof z);
if (!zsys_read(&z)) {
(void)snprintf(a->sys_err, sizeof a->sys_err,
"CVT/ASVT are only available on z/OS");
a->have_sys = false;
return;
}
if (uname(&u) == 0) {
(void)snprintf(z.os, sizeof z.os, "%s %s", u.sysname, u.release);
(void)snprintf(z.machine, sizeof z.machine, "%s", u.machine);
}
(void)snprintf(z.host, sizeof z.host, "%s", a->host);
a->sys = z;
a->have_sys = true;
a->sys_err[0] = '\0';
}
static void collect(struct app *a) {
struct timespec t0, t1;
int i;
(void)clock_gettime(CLOCK_MONOTONIC, &t0);
for (i = 0; i < 4; i++) {
struct vsm_info v;
if (collect_area(&v, i + 1, false)) {
a->info[i] = v;
a->have[i] = true;
a->err[i][0] = '\0';
} else {
(void)snprintf(a->err[i], sizeof a->err[i], "%s", v.err);
}
}
sys_read(a);
(void)clock_gettime(CLOCK_MONOTONIC, &t1);
a->collect_ms = (int)((t1.tv_sec - t0.tv_sec) * 1000 +
(t1.tv_nsec - t0.tv_nsec) / 1000000);
}
static void draw_header(struct tui *t, const struct app *a) {
char now[16];
time_t clock_now = time(NULL);
struct tm tm;
(void)localtime_r(&clock_now, &tm);
(void)strftime(now, sizeof now, "%H:%M:%S", &tm);
tui_fill(t, 0, 0, t->w, 1, ' ', TUI_TITLE);
tui_printf(t, 1, 0, t->w - 30, TUI_TITLE, " ztop %s %s %s collect %dms ",
ZTOP_VERSION, a->host, now, a->collect_ms);
if (a->paused)
tui_put(t, t->w - 9, 0, " PAUSED ", 8, TUI_WARN);
}
static void draw_tabs(struct tui *t, const struct app *a) {
static const char *const labels[3] = {"1 STORAGE", "2 SYSTEM", "3 JOBS"};
int x = tui_tabs_draw(t, 0, 1, labels, 3, a->tab);
tui_printf(t, x + 1, 1, t->w - x - 2, TUI_LABEL, "refresh %ds +/-",
a->interval);
}
static void draw_storage(struct tui *t, struct app *a) {
const struct vsm_info *v = &a->info[a->sub];
tui_rect r = {0, 5, t->w, t->h - 7};
char used[16], freed[16], mapped[16], total[16], pcbuf[8];
int p = pct(v);
a->storage.cols = a->sub == 3 ? pvt_cols : storage_cols;
tui_printf(t, 1, 3, 6, a->sub == 0 ? TUI_TITLE : TUI_LABEL, " CSA ");
tui_printf(t, 7, 3, 6, a->sub == 1 ? TUI_TITLE : TUI_LABEL, " SQA ");
tui_printf(t, 13, 3, 6, a->sub == 2 ? TUI_TITLE : TUI_LABEL, " LSQA ");
tui_printf(t, 20, 3, 6, a->sub == 3 ? TUI_TITLE : TUI_LABEL, " PVT ");
if (!a->have[a->sub]) {
tui_printf(t, 1, 6, t->w - 2, TUI_BAD, "%s: %s", area_upper[a->sub],
a->err[a->sub][0] ? a->err[a->sub] : "no data");
return;
}
human(v->used, used, sizeof used);
human(v->free, freed, sizeof freed);
human(v->foot, mapped, sizeof mapped);
if (v->area == VSM_CSA && v->unalloc_valid) {
human(v->foot + v->unalloc, total, sizeof total);
(void)snprintf(pcbuf, sizeof pcbuf, "%d%%", p);
} else {
(void)snprintf(total, sizeof total, "-");
(void)snprintf(pcbuf, sizeof pcbuf, "-");
}
tui_printf(t, 1, 4, t->w - 2, TUI_PLAIN,
"used %s free %s mapped %s total %s use %s blocks %u", used,
freed, mapped, total, pcbuf, v->blocks);
tui_table_draw(t, r, &a->storage, v->nsub, storage_cell, NULL, a);
if (a->err[a->sub][0] != '\0')
tui_printf(t, 1, t->h - 2, t->w - 2, TUI_BAD, "! %s", a->err[a->sub]);
else if (v->unalloc_valid)
tui_printf(t, 1, t->h - 2, t->w - 2, TUI_LABEL,
"unallocated %llu in %u blocks, largest %llu",
(unsigned long long)v->unalloc, v->unalloc_blocks,
(unsigned long long)v->largest_unalloc);
}
static void draw_system(struct tui *t, const struct app *a) {
const struct zsys *z = &a->sys;
if (!a->have_sys) {
tui_printf(t, 1, 4, t->w - 2, TUI_BAD, "%s", a->sys_err);
return;
}
tui_printf(t, 1, 4, t->w - 2, TUI_LABEL, "system %s", z->sysname);
tui_printf(t, 1, 5, t->w - 2, TUI_PLAIN, "host %s", z->host);
tui_printf(t, 1, 6, t->w - 2, TUI_PLAIN, "os %s", z->os);
tui_printf(t, 1, 7, t->w - 2, TUI_PLAIN, "machine %s", z->machine);
tui_printf(t, 1, 8, t->w - 2, TUI_PLAIN, "spaces %d used, %u slots",
z->njobs, z->slots);
tui_printf(
t, 1, 10, t->w - 2, TUI_LABEL,
"storage CSA %llu unallocated, PVT %llu",
(unsigned long long)(a->info[0].unalloc_valid ? a->info[0].unalloc : 0),
(unsigned long long)(a->info[3].unalloc_valid ? a->info[3].unalloc
: 0));
}
static void draw_jobs(struct tui *t, struct app *a) {
tui_rect r = {0, 3, t->w, t->h - 5};
if (!a->have_sys) {
tui_printf(t, 1, 4, t->w - 2, TUI_BAD, "%s", a->sys_err);
return;
}
tui_table_draw(t, r, &a->jobs, a->sys.njobs, job_cell, NULL, a);
tui_printf(t, 1, t->h - 2, t->w - 2, TUI_LABEL,
"%d address spaces, %u slots", a->sys.njobs, a->sys.slots);
}
static void draw_help(struct tui *t) {
int w = t->w < 56 ? t->w - 2 : 54;
int h = 16;
int x = (t->w - w) / 2;
int y = (t->h - h) / 2;
tui_fill(t, x, y, w, h, ' ', TUI_PLAIN);
tui_box(t, x, y, w, h, TUI_LABEL);
tui_printf(t, x + 2, y + 1, w - 4, TUI_TITLE, "ztop keys");
tui_printf(t, x + 2, y + 3, w - 4, TUI_PLAIN, "q quit");
tui_printf(t, x + 2, y + 4, w - 4, TUI_PLAIN, "TAB next view");
tui_printf(t, x + 2, y + 5, w - 4, TUI_PLAIN,
"1-3 storage, system, jobs");
tui_printf(t, x + 2, y + 6, w - 4, TUI_PLAIN, "1-4 CSA, SQA, LSQA, PVT");
tui_printf(t, x + 2, y + 7, w - 4, TUI_PLAIN, "p pause refresh");
tui_printf(t, x + 2, y + 8, w - 4, TUI_PLAIN, "r refresh now");
tui_printf(t, x + 2, y + 9, w - 4, TUI_PLAIN, "+ - interval up / down");
tui_printf(t, x + 2, y + 10, w - 4, TUI_PLAIN, "j k move the selection");
tui_printf(t, x + 2, y + 11, w - 4, TUI_PLAIN, "up dn same");
tui_printf(t, x + 2, y + 12, w - 4, TUI_PLAIN, "g G first / last");
tui_printf(t, x + 2, y + 13, w - 4, TUI_PLAIN, "? close help");
tui_printf(t, x + 2, y + 14, w - 4, TUI_PLAIN, "1-4 inside storage");
}
static void view(void *model, struct tui *t) {
struct app *a = model;
tui_clear(t);
draw_header(t, a);
draw_tabs(t, a);
tui_fill(t, 0, 2, t->w, 1, '-', TUI_STYLE(7, 0, TUI_DIM));
if (t->w < 70 || t->h < 14) {
tui_printf(t, 1, 3, 0, TUI_BAD, "terminal too small: need 70x14");
} else if (a->tab == 0) {
draw_storage(t, a);
} else if (a->tab == 1) {
draw_system(t, a);
} else {
draw_jobs(t, a);
}
if (a->help)
draw_help(t);
tui_statusbar(t, "q quit TAB view j/k move r refresh p pause ? help",
a->paused ? "PAUSED" : "");
}
static bool parse_areas(const char *s, unsigned int *mask) {
unsigned int m = 0;
while (*s != '\0') {
size_t n = strcspn(s, ",");
if (n == 3 && strncmp(s, "csa", 3) == 0)
m |= 1u << 0;
else if (n == 3 && strncmp(s, "sqa", 3) == 0)
m |= 1u << 1;
else if (n == 4 && strncmp(s, "lsqa", 4) == 0)
m |= 1u << 2;
else if (n == 3 && strncmp(s, "pvt", 3) == 0)
m |= 1u << 3;
else if (n == 3 && strncmp(s, "all", 3) == 0)
m |= 0xFu;
else
return false;
s += n;
if (*s == ',')
s++;
}
*mask = m;
return m != 0;
}
static void usage(FILE *f) {
(void)fprintf(
f,
"usage: ztop [-a csa|sqa|lsqa|pvt|all] [-s] [-j] [-x] [-d [seconds]]\n"
" -a storage areas to report (default csa); comma-separate or all\n"
" -s system name, host, release and address-space count\n"
" -j one line per address space\n"
" -x dump the VSMLIST work and data areas (one-shot only)\n"
" -d interactive view, refreshed every 2 seconds by default\n"
" -h this text\n");
}
static bool update(struct tui_app *app, const struct tui_msg *m) {
struct app *a = app->model;
int rows, height;
if (a->help) {
if (m->type == TUI_MSG_KEY && (m->key == '?' || m->key == TUI_KEY_ESC ||
m->key == 'q' || m->key == 0x03))
a->help = false;
return true;
}
if (m->type == TUI_MSG_TICK) {
if (!a->paused)
collect(a);
return true;
}
if (m->type == TUI_MSG_RESIZE)
return true;
if (m->type != TUI_MSG_KEY)
return false;
if (m->key == 'q' || m->key == TUI_KEY_ESC || m->key == 0x03) {
app->quit = true;
return false;
}
if (m->key == '?') {
a->help = true;
return true;
}
if (m->key == 'p') {
a->paused = !a->paused;
return true;
}
if (m->key == 'r') {
collect(a);
return true;
}
if (m->key == '+' || m->key == '=') {
if (a->interval < 60)
a->interval++;
a->tapp->tick_ms = a->interval * 1000;
return true;
}
if (m->key == '-' && a->interval > 1) {
a->interval--;
a->tapp->tick_ms = a->interval * 1000;
return true;
}
if (m->key == TUI_KEY_TAB) {
a->tab = (a->tab + 1) % 3;
return true;
}
if (m->key == TUI_KEY_BACKTAB) {
a->tab = (a->tab + 2) % 3;
return true;
}
if (a->tab == 0 && m->key >= '1' && m->key <= '4') {
a->sub = m->key - '1';
return true;
}
rows = a->tab == 0 ? a->info[a->sub].nsub : a->sys.njobs;
height = a->tab == 0 ? m->h - 8 : m->h - 6;
if (height < 1)
height = 1;
if (a->tab == 0)
return tui_viewport_key(&a->storage.vp, m->key, rows, height);
if (a->tab == 2)
return tui_viewport_key(&a->jobs.vp, m->key, rows, height);
return false;
}
static void sys_fill(struct zsys *z, const char *host) {
struct utsname u;
if (uname(&u) == 0) {
(void)snprintf(z->os, sizeof z->os, "%s %s", u.sysname, u.release);
(void)snprintf(z->machine, sizeof z->machine, "%s", u.machine);
}
(void)snprintf(z->host, sizeof z->host, "%s", host);
}
static void run_tui(int interval) {
static struct app a;
struct tui t;
struct tui_app ta;
memset(&a, 0, sizeof a);
if (gethostname(a.host, sizeof a.host - 1) != 0)
(void)snprintf(a.host, sizeof a.host, "?");
a.interval = interval;
a.storage.cols = storage_cols;
a.storage.ncols = 8;
a.jobs.cols = job_cols;
a.jobs.ncols = 3;
if (!tui_open(&t)) {
(void)fprintf(stderr, "ztop: -d needs a terminal\n");
exit(1);
}
collect(&a);
memset(&ta, 0, sizeof ta);
ta.model = &a;
ta.update = update;
ta.view = view;
ta.tick_ms = interval * 1000;
a.tapp = &ta;
tui_run(&t, &ta);
tui_close(&t);
}
static void run_oneshot(unsigned mask, bool want_sys, bool want_jobs,
bool dump) {
char host[65];
bool printed = false;
int rc = 0;
int i;
for (i = 0; i < 4; i++) {
struct vsm_info v;
if ((mask & (1u << i)) == 0)
continue;
if (!collect_area(&v, i + 1, dump)) {
(void)fprintf(stderr, "ztop: %s: %s\n", area_name[i], v.err);
rc = 1;
continue;
}
if (printed)
(void)printf("\n");
print_area(&v);
printed = true;
}
if (want_sys || want_jobs) {
struct zsys z;
if (gethostname(host, sizeof host - 1) != 0)
(void)snprintf(host, sizeof host, "?");
host[sizeof host - 1] = '\0';
if (!zsys_read(&z)) {
(void)fprintf(stderr,
"ztop: system and jobs are only available on z/OS\n");
rc = 1;
} else {
sys_fill(&z, host);
if (printed)
(void)printf("\n");
if (want_sys)
print_system(&z);
if (want_jobs) {
if (want_sys)
(void)printf("\n");
print_jobs(&z);
}
}
}
exit(rc);
}
int main(int argc, char **argv) {
unsigned mask = 0;
bool dump = false;
bool interactive = false;
bool want_sys = false;
bool want_jobs = false;
int interval = 0;
int i;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0) {
usage(stdout);
return 0;
} else if (strcmp(argv[i], "-x") == 0) {
dump = true;
} else if (strcmp(argv[i], "-s") == 0) {
want_sys = true;
} else if (strcmp(argv[i], "-j") == 0) {
want_jobs = true;
} else if (strcmp(argv[i], "-a") == 0) {
if (i + 1 >= argc || !parse_areas(argv[++i], &mask)) {
usage(stderr);
return 2;
}
} else if (strncmp(argv[i], "-a", 2) == 0 && argv[i][2] != '\0') {
if (!parse_areas(argv[i] + 2, &mask)) {
usage(stderr);
return 2;
}
} else if (strcmp(argv[i], "-d") == 0) {
interactive = true;
if (i + 1 < argc && argv[i + 1][0] >= '0' && argv[i + 1][0] <= '9')
interval = atoi(argv[++i]);
} else {
usage(stderr);
return 2;
}
}
if (interactive) {
if (dump)
(void)fprintf(stderr, "ztop: -x is ignored with -d\n");
if (interval < 1)
interval = 2;
if (interval > 60)
interval = 60;
run_tui(interval);
} else {
if (mask == 0 && !want_sys && !want_jobs)
mask = 1u;
run_oneshot(mask, want_sys, want_jobs, dump);
}
return 0;
}
|