diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-27 18:15:26 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-27 18:15:26 -0600 |
| commit | 83e8fbc692b36be5ac9011ccf26b1b232d560008 (patch) | |
| tree | ab62a06c6a49b343b33d41b502487b233128ad80 /src/ztop.c | |
| parent | 4c95e6490d189b47cad050744de9768465e0307b (diff) | |
| parent | b5bb63d4760f6f6e0282fc9f7eec36b41705458c (diff) | |
| download | ztop-main.tar.gz | |
views: PS, FS and LOG, and a DEC-graphics frame
Diffstat (limited to 'src/ztop.c')
| -rw-r--r-- | src/ztop.c | 1435 |
1 files changed, 1162 insertions, 273 deletions
@@ -6,7 +6,7 @@ * view is built on libtui, which knows nothing about z/OS: ztop supplies * the model, the messages and the tables. */ -#include <stdbool.h> +#include <ctype.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> @@ -17,11 +17,31 @@ #include <unistd.h> #include "sys.h" +#include "mdb.h" +#include "uss.h" #include "tui.h" #include "vsm.h" +#include "zmcs.h" -#define ZTOP_VERSION "0.3" -#define WORK_SIZE (1024u * 1024u) +static const char ZTOP_VERSION[] = "0.3"; + +/* The console is opt-in: opening it calls MODESET, and an unauthorized + * program does not get a return code from that, it gets S047. */ +static bool console_opt; + +/* The stub in src/zmcs.s addresses these fields by offset, and those are + * the -m32 offsets: a pointer is 4 bytes there, so the same struct on a + * 64-bit host has a different shape and the check would only be noise. */ +#if defined(__MVS__) +static_assert(offsetof(zmcs_req, save) == 40, "zmcs_req.save"); +static_assert(offsetof(zmcs_req, csa) == 184, "zmcs_req.csa"); +static_assert(offsetof(zmcs_req, alet) == 32, "zmcs_req.alet"); +static_assert(offsetof(zmcs_req, ecb) == 28, "zmcs_req.ecb"); +static_assert(offsetof(zmcs_req, rsn) == 24, "zmcs_req.rsn"); +/* The end of the last field, not sizeof: save[] forces 8-byte alignment, + * so sizeof is 192 on -m32 and the padding is not part of the interface. */ +static_assert(offsetof(zmcs_req, csa) + 4 == 188, "zmcs_req.csa: runs into the next object"); +#endif #if defined(__MVS__) /* HLASM upcases the ESD name of the CSECT, so the assembler side is @@ -32,7 +52,7 @@ extern int zvslist(void *) __asm__("ZVSLIST"); /* 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 { +typedef struct zreq { int area; unsigned len; void *wk; @@ -40,11 +60,12 @@ struct zreq { 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"); +} zreq; +static_assert(offsetof(zreq, self) == 16, "zreq.self"); +static_assert(offsetof(zreq, save) == 24, "zreq.save"); +static_assert(offsetof(zreq, space) == 168, "zreq.space"); +static constexpr unsigned WORK_SIZE = 1024 * 1024; static unsigned char work[WORK_SIZE] __attribute__((aligned(8))); #endif @@ -78,18 +99,18 @@ static void hexdump(const char *tag, const unsigned char *p, unsigned int len, } } -static void dump_work(const struct vsm_info *v, const unsigned char *workp, +static void dump_work(const 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) + if (data != nullptr && len > 0) hexdump("data", data, len, 256); } -static bool fetch_one(struct vsm_info *v, int area, int space, bool dump) { +static bool fetch_one(vsm_info *v, int area, int space, bool dump) { int calls = 0; const unsigned char *data; unsigned int len; @@ -97,7 +118,7 @@ static bool fetch_one(struct vsm_info *v, int area, int space, bool dump) { 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}; + zreq req = {0}; bool ok; if (++calls > 64) { @@ -133,7 +154,7 @@ static bool fetch_one(struct vsm_info *v, int area, int space, bool dump) { } } #else -static bool fetch_one(struct vsm_info *v, int area, int space, bool dump) { +static bool fetch_one(vsm_info *v, int area, int space, bool dump) { (void)area; (void)space; (void)dump; @@ -142,11 +163,11 @@ static bool fetch_one(struct vsm_info *v, int area, int space, bool dump) { } #endif -static bool collect_area(struct vsm_info *v, int area, bool dump) { +static bool collect_area(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; + vsm_info u; if (fetch_one(&u, area, VSM_UNALLOC, dump)) { v->unalloc = u.unalloc; @@ -164,11 +185,11 @@ static bool collect_area(struct vsm_info *v, int area, bool dump) { /** @brief Fraction of the mapped area in use, or -1 when there is no total. */ /* Only CSA and PVT get an unallocated pass, so only they have a total. */ -static bool has_total(const struct vsm_info *v) { +static bool has_total(const vsm_info *v) { return (v->area == VSM_CSA || v->area == VSM_PVT) && v->unalloc_valid; } -static int pct(const struct vsm_info *v) { +static int pct(const vsm_info *v) { uint64_t total; if (!has_total(v)) @@ -179,15 +200,18 @@ static int pct(const struct vsm_info *v) { return (int)((v->used * 100u + total / 2) / total); } -static void print_area(const struct vsm_info *v) { +static void print_area(const 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); + (void)printf("area %s: used %llu (below 16M %llu above 16M %llu) free %llu " + "mapped %llu blocks %u", + name, (unsigned long long)v->used, + (unsigned long long)v->below, (unsigned long long)v->above, + (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, @@ -205,7 +229,7 @@ static void print_area(const struct vsm_info *v) { "blks", "used", "free", "mapped", "largest", "address"); for (i = 0; i < v->nsub; i++) { - const struct vsm_subpool *s = &v->sub[i]; + const vsm_subpool *s = &v->sub[i]; char addr[32]; if (s->blocks == 0) @@ -246,7 +270,7 @@ static void print_area(const struct vsm_info *v) { (void)printf("warning: %s\n", v->err); } -static void print_system(const struct zsys *z) { +static void print_system(const 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); @@ -257,7 +281,7 @@ static void print_system(const struct zsys *z) { (unsigned long long)z->real_k); } -static void print_page(const struct zpage *p) { +static void print_page(const zpage *p) { (void)printf("aux storage common %s%s plpa %s%s local %s vio %s" ", journal %u\n", p->common_pds ? "yes" : "no", p->common_full ? " full" : "", @@ -266,7 +290,7 @@ static void print_page(const struct zpage *p) { p->vio_journaling ? "on" : "off", p->vio_ci); } -static void print_jobs(const struct zsys *z) { +static void print_jobs(const zsys *z) { int i; (void)printf("%4s %-8s %8s\n", "asid", "job", "ascb"); @@ -275,14 +299,26 @@ static void print_jobs(const struct zsys *z) { (unsigned int)z->job[i].ascb); } -struct app { - struct vsm_info info[4]; +typedef struct pvt_row { + uint32_t tcb; + unsigned nsub; + uint64_t used; + uint64_t below; + uint64_t above; +} pvt_row; + +/* The console ring: 256 lines of 200 columns is 51K inside the model, + * and it never grows. */ +enum { LOG_LINES = 256, LOG_COLS = 200 }; + +typedef struct app { + vsm_info info[4]; bool have[4]; char err[4][160]; - struct zsys sys; + zsys sys; bool have_sys; char sys_err[160]; - struct zpage page; + zpage page; bool have_page; char page_err[160]; int interval; @@ -291,16 +327,50 @@ struct app { bool help; int tab; int sub; - struct tui_table storage; - struct tui_table jobs; - struct tui_app *tapp; + tui_table storage; + tui_table jobs; + tui_app *tapp; char host[65]; int sub_sort, sub_dir; int job_sort, job_dir; int sub_order[VSM_MAX_SUB]; int job_order[ZJOB_MAX]; bool popup; -}; + bool pvt_fold; + bool tcb_popup; + bool inspector; + char filter[32]; + tui_input fin; + tui_input cin; + bool filtering; + bool commanding; + int sub_rows; + int job_rows; + unsigned char trend[4][32]; /* per-area use%, newest last */ + int ntrend[4]; + tui_viewport pvt_vp; + pvt_row pvt[VSM_MAX_SUB]; + int npvt; + uss_ps_list ps; + uss_fs_list fs; + bool have_ps; + bool have_fs; + tui_table pst; + tui_table fst; + /* The console log: one request block, one ring of lines, both in the + * model, because a refresh must not allocate. */ + zmcs_req creq; + char creq_name[8]; + unsigned creq_consid; + char mdb[ZMCS_MDB_MAX]; + int mdb_len; + char log[LOG_LINES][LOG_COLS]; + int nlog; + int log_next; + bool log_on; + char log_err[160]; + tui_viewport log_vp; +} app; enum { SS_SP, SS_KEY, SS_BLOCKS, SS_USED, SS_FREE, SS_LARGEST, SS_COUNT }; enum { JS_ASID, JS_JOB, JS_ADDR, JS_COUNT }; @@ -312,13 +382,141 @@ static const char *const job_sort_names[JS_COUNT] = {"asid", "job", "ascb"}; /* qsort has no context argument; these are set right before each call and * the application is single-threaded. */ -static const struct vsm_info *sort_info; -static const struct zsys *sort_sys; +static const vsm_info *sort_info; +static const zsys *sort_sys; static int sort_key, sort_dir; static int cmp_u64(uint64_t a, uint64_t b) { return (a > b) - (a < b); } +/* ---- the folded PVT view: one row per TCB ---- */ + +/* Defined with the filter, needed by the folded table and by collect. */ +static bool match(const char *hay, const char *pat); +static void order_storage(app *a); + +/* Defined below; the tick and the command bar use them. */ +static void log_start(app *a); +static void log_poll(app *a); +static void log_stop(app *a); + +/* The console is named after the caller, so two people (or two sessions) + * do not collide on one name and the RACF profile can be that user's + * alone. The service checks MVS.MCSOPER.<name>, so the name is not a + * secret: an unauthorised user choosing someone else's name is refused. */ +static void console_name(char *name) { + const char *from = getenv("LOGNAME"); + + if (from == nullptr || *from == '\0') + from = getenv("USER"); + (void)memset(name, ' ', 8); + if (from == nullptr) + from = "ZTOP"; + for (int i = 0; i < 8 && from[i] != '\0'; i++) { + unsigned char c = (unsigned char)from[i]; + + if (c >= 'a' && c <= 'z') + c = (unsigned char)(c - 'a' + 'A'); + name[i] = + (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ? (char)c : '_'; + } +} + +static int tcb_order[VSM_MAX_SUB]; + +static int cmp_tcb(const void *pa, const void *pb) { + const vsm_subpool *a = &sort_info->sub[*(const int *)pa]; + const vsm_subpool *b = &sort_info->sub[*(const int *)pb]; + + return (a->tcb > b->tcb) - (a->tcb < b->tcb); +} + +/* Group the subpools by owning TCB. Sorted first, so the grouping is a + * linear pass and the row table needs no allocation. */ +static void build_pvt(app *a) { + const vsm_info *v = &a->info[3]; + + a->npvt = 0; + if (!a->have[3]) + return; + for (int i = 0; i < v->nsub; i++) + tcb_order[i] = i; + sort_info = v; + qsort(tcb_order, (size_t)v->nsub, sizeof tcb_order[0], cmp_tcb); + for (int i = 0; i < v->nsub; i++) { + const vsm_subpool *s = &v->sub[tcb_order[i]]; + pvt_row *r; + + if (a->npvt > 0 && a->pvt[a->npvt - 1].tcb == s->tcb) { + r = &a->pvt[a->npvt - 1]; + } else { + if (a->npvt >= VSM_MAX_SUB) + break; + r = &a->pvt[a->npvt++]; + *r = (pvt_row){s->tcb, 0, 0, 0, 0}; + } + r->nsub++; + r->used += s->used; + r->below += s->below; + r->above += s->above; + } + { + int n = 0; + + for (int i = 0; i < a->npvt; i++) { + char key[16]; + + (void)snprintf(key, sizeof key, "%08X", + (unsigned int)a->pvt[i].tcb); + if (match(key, a->filter)) + a->pvt[n++] = a->pvt[i]; + } + a->npvt = n; + } +} + +/* The viewport and row count the keys act on, so a folded PVT and the JOBS + * table never need their own copy of the movement code. */ +static bool folded(const app *a) { + return a->tab == 0 && a->sub == 3 && a->pvt_fold; +} + +static tui_viewport *vp_of(app *a) { + if (folded(a)) + return &a->pvt_vp; + if (a->tab == 2) + return &a->jobs.vp; + if (a->tab == 4) + return &a->pst.vp; + if (a->tab == 5) + return &a->fst.vp; + if (a->tab == 6) + return &a->log_vp; + return &a->storage.vp; +} + +static int vrows(const app *a) { + if (folded(a)) + return a->npvt; + if (a->tab == 2) + return a->job_rows; + if (a->tab == 4) + return a->ps.nproc; + if (a->tab == 5) + return a->fs.nfs; + if (a->tab == 6) + return a->nlog; + return a->sub_rows; +} + +static int vheight(const app *a, int h) { + if (a->tab == 0) + return h - 8; + if (a->tab == 2 || a->tab == 4 || a->tab == 5 || a->tab == 6) + return h - 6; + return 0; +} + static int cmp_u32(uint32_t a, uint32_t b) { return (a > b) - (a < b); } @@ -326,8 +524,8 @@ static int cmp_u32(uint32_t a, uint32_t b) { static int cmp_sub(const void *pa, const void *pb) { int ia = *(const int *)pa; int ib = *(const int *)pb; - const struct vsm_subpool *a = &sort_info->sub[ia]; - const struct vsm_subpool *b = &sort_info->sub[ib]; + const vsm_subpool *a = &sort_info->sub[ia]; + const vsm_subpool *b = &sort_info->sub[ib]; int r = 0; switch (sort_key) { @@ -356,8 +554,8 @@ static int cmp_sub(const void *pa, const void *pb) { static int cmp_job(const void *pa, const void *pb) { int ia = *(const int *)pa; int ib = *(const int *)pb; - const struct zjob *a = &sort_sys->job[ia]; - const struct zjob *b = &sort_sys->job[ib]; + const zjob *a = &sort_sys->job[ia]; + const zjob *b = &sort_sys->job[ib]; int r = 0; switch (sort_key) { @@ -374,28 +572,68 @@ static int cmp_job(const void *pa, const void *pb) { return r != 0 ? sort_dir * r : ia - ib; } -static const struct tui_col storage_cols[] = { +static const tui_col storage_cols[] = { {"SP", 3, true}, {"KY", 3, true}, {"BLKS", 8, true}, {"USED", 10, true}, {"FREE", 10, true}, {"MAPPED", 10, true}, {"LARGEST", 10, true}, {"ADDRESS", 17, false}, }; -static const struct tui_col pvt_cols[] = { +static const tui_col pvt_cols[] = { {"SP", 3, true}, {"KY", 3, true}, {"BLKS", 7, true}, {"USED", 10, true}, {"FREE", 10, true}, {"MAPPED", 10, true}, {"LARGEST", 10, true}, {"TCB", 9, false}, }; -static const struct tui_col job_cols[] = { +/* The folded PVT table: one row per TCB, with the 16M split and which side + * of the line the TCB's storage is on. */ +static const tui_col pvt_fold_cols[] = { + {"TCB", 9, false}, {"SUBS", 5, true}, {"USED", 10, true}, + {"<16M", 10, true}, {">16M", 10, true}, {"LINE", 5, false}, +}; + +static const tui_col job_cols[] = { {"ASID", 6, true}, {"JOB", 9, false}, {"ASCB", 10, false}, }; +static void pvt_cell(void *ctx, int row, int col, char *buf, size_t len) { + const app *a = ctx; + const pvt_row *r = &a->pvt[row]; + char u[16], b[16], ab[16]; + + switch (col) { + case 0: + (void)snprintf(buf, len, "%08X", (unsigned int)r->tcb); + break; + case 1: + (void)snprintf(buf, len, "%u", r->nsub); + break; + case 2: + human(r->used, u, sizeof u); + (void)snprintf(buf, len, "%s", u); + break; + case 3: + human(r->below, b, sizeof b); + (void)snprintf(buf, len, "%s", b); + break; + case 4: + human(r->above, ab, sizeof ab); + (void)snprintf(buf, len, "%s", ab); + break; + default: + (void)snprintf(buf, len, "%s", + r->below != 0 && r->above != 0 ? "BOTH" + : r->above != 0 ? ">16M" + : "<16M"); + break; + } +} + static void storage_cell(void *ctx, int row, int col, char *buf, size_t len) { - const struct app *a = ctx; - int i = row < a->info[a->sub].nsub ? a->sub_order[row] : row; - const struct vsm_subpool *s = &a->info[a->sub].sub[i]; + const app *a = ctx; + int i = row < a->sub_rows ? a->sub_order[row] : row; + const vsm_subpool *s = &a->info[a->sub].sub[i]; char u[16], f[16], m[16], l[16]; switch (col) { @@ -437,9 +675,9 @@ static void storage_cell(void *ctx, int row, int col, char *buf, size_t len) { } static void job_cell(void *ctx, int row, int col, char *buf, size_t len) { - const struct app *a = ctx; - int i = row < a->sys.njobs ? a->job_order[row] : row; - const struct zjob *j = &a->sys.job[i]; + const app *a = ctx; + int i = row < a->job_rows ? a->job_order[row] : row; + const zjob *j = &a->sys.job[i]; switch (col) { case 0: @@ -454,8 +692,8 @@ static void job_cell(void *ctx, int row, int col, char *buf, size_t len) { } } -static void sys_read(struct app *a) { - struct zsys z; +static void sys_read(app *a) { + zsys z; struct utsname u; (void)memcpy(&z, &a->sys, sizeof z); @@ -475,13 +713,28 @@ static void sys_read(struct app *a) { a->sys_err[0] = '\0'; } -static void collect(struct app *a) { +/* Keep the last 32 samples of an area's use%, so the storage view can draw + * its recent shape without keeping history anywhere else. */ +static void push_trend(app *a, int i, int p) { + unsigned char *t = a->trend[i]; + int *n = &a->ntrend[i]; + + if (p < 0 || p > 100) + return; + if (*n == (int)sizeof a->trend[i]) { + (void)memmove(t, t + 1, sizeof a->trend[i] - 1); + (*n)--; + } + t[(*n)++] = (unsigned char)p; +} + +static void collect(app *a) { struct timespec t0, t1; int i; (void)clock_gettime(CLOCK_MONOTONIC, &t0); for (i = 0; i < 4; i++) { - struct vsm_info v; + vsm_info v; if (collect_area(&v, i + 1, false)) { a->info[i] = v; @@ -496,37 +749,89 @@ static void collect(struct app *a) { if (!a->have_page) (void)snprintf(a->page_err, sizeof a->page_err, "ASMVT is only available on z/OS"); + build_pvt(a); + order_storage(a); + if (a->tab == 4) + a->have_ps = uss_ps_read(&a->ps); + else if (a->tab == 5) + a->have_fs = uss_fs_read(&a->fs); + log_poll(a); + for (i = 0; i < 4; i++) + if (a->have[i]) + push_trend(a, i, pct(&a->info[i])); (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 const struct tui_hint view_bar[] = { - {"q", "quit"}, - {"TAB", "view"}, - {"r", "refresh"}, - {"?", "help"}, +/* Tab bit for the KeyMap's per-view mask. */ +enum { + TABBITS_STORAGE = 1u << 0, + TABBITS_SYSTEM = 1u << 1, + TABBITS_JOBS = 1u << 2, + TABBITS_PAGING = 1u << 3, + TABBITS_PS = 1u << 4, + TABBITS_FS = 1u << 5, + TABBITS_LOG = 1u << 6, + TABBITS_ALL = 0x7Fu, + TABBITS_ROWS = TABBITS_STORAGE | TABBITS_JOBS | TABBITS_PS | TABBITS_FS | + TABBITS_LOG, }; -static const struct tui_hint storage_bar[] = { - {"q", "quit"}, - {"TAB", "view"}, - {"s", "sort"}, - {"Enter", "details"}, +/* One table drives the footer hint and the ? overlay, so a key that moves + * cannot be documented in one place and forgotten in the other. */ +static const tui_binding keymap[] = { + /* views */ + {"TAB", "next view", "views", TABBITS_ALL, true}, + {"S-TAB", "previous view", "views", TABBITS_ALL, false}, + {"1-7", "area, or view", "views", TABBITS_ALL, false}, + {"click", "a tab", "views", TABBITS_ALL, false}, + {"/", "filter", "views", TABBITS_STORAGE | TABBITS_JOBS, false}, + {":", "command", "views", TABBITS_ALL, true}, + /* moving */ + {"j k", "one row", "moving", TABBITS_ROWS, false}, + {"PgUp PgDn", "a page", "moving", TABBITS_ROWS, false}, + {"g G", "first, last", "moving", TABBITS_ROWS, false}, + {"wheel", "scroll", "moving", TABBITS_ROWS, false}, + {"Enter", "details", "moving", TABBITS_STORAGE, false}, + {"i", "inspector", "moving", TABBITS_STORAGE, false}, + {"t", "fold TCBs", "moving", TABBITS_STORAGE, false}, + {"s S", "sort, reverse", "moving", TABBITS_STORAGE | TABBITS_JOBS, true}, + /* the screen */ + {"q", "quit", "the screen", TABBITS_ALL, true}, + {"p", "pause", "the screen", TABBITS_ALL, false}, + {"r", "refresh now", "the screen", TABBITS_ALL, false}, + {"+ -", "1..60 s", "the screen", TABBITS_ALL, false}, + {"?", "help", "the screen", TABBITS_ALL, true}, }; +enum { KEYMAP_COUNT = (int)(sizeof keymap / sizeof keymap[0]) }; -static void draw_header(struct tui *t, const struct app *a) { +/* Drawn and hit-tested from the same array, so a click lands on the tab it + * names. */ +static const char *const tab_labels[7] = { + "1 STORAGE", "2 SYSTEM", "3 JOBS", "4 PAGING", "5 PS", "6 FS", "7 LOG", +}; + +static void draw_header(tui *t, const app *a) { char now[16]; char right[64]; - time_t clock_now = time(NULL); + char ident[96]; + time_t clock_now = time(nullptr); 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_BAR); tui_printf(t, 1, 0, 7, TUI_KEYCAP, " ztop "); - tui_printf(t, 7, 0, 8, TUI_META, " %s", ZTOP_VERSION); - tui_printf(t, 15, 0, t->w - 40, TUI_PLAIN, " %s", a->host); + tui_printf(t, 7, 0, 8, TUI_META, "%s |", ZTOP_VERSION); + /* The identity stays on screen in every view: who you are looking at + * should not need a tab. */ + if (a->have_sys) + (void)snprintf(ident, sizeof ident, "%s | %s | %s", a->sys.sysname, + a->sys.os, a->sys.machine); + else + (void)snprintf(ident, sizeof ident, "%s", a->host); + tui_printf(t, 14, 0, t->w - 40, TUI_PLAIN, "%s", ident); if (a->collect_ms > 0) (void)snprintf(right, sizeof right, "collect %dms %s", a->collect_ms, now); @@ -539,32 +844,115 @@ static void draw_header(struct tui *t, const struct app *a) { " PAUSED "); } -static void draw_tabs(struct tui *t, const struct app *a) { - static const char *const labels[4] = {"1 STORAGE", "2 SYSTEM", "3 JOBS", - "4 PAGING"}; - int x = tui_tabs_draw(t, 0, 1, labels, 4, a->tab); +static void draw_tabs(tui *t, const app *a) { + int x = tui_tabs_draw(t, 0, 1, tab_labels, 7, a->tab); tui_printf(t, x + 1, 1, t->w - x - 2, TUI_META, "refresh %ds +/-", a->interval); } -static void order_storage(struct app *a) { - const struct vsm_info *v = &a->info[a->sub]; +/** @brief A line of text set into the panel's bottom border, like its + * title. */ +static void footer_line(tui *t, const char *text, tui_style st) { + tui_printf(t, 3, t->h - 2, t->w - 6, st, " %s ", text); +} - for (int i = 0; i < v->nsub; i++) - a->sub_order[i] = i; +/** @brief A dim label and its value, one blank apart. */ +static void kv(tui *t, int x, int y, int maxw, const char *label, + const char *value, tui_style st) { + int ll = (int)strlen(label); + + if (ll >= maxw - 1) + return; + tui_printf(t, x, y, ll, TUI_META, "%s", label); + tui_printf(t, x + ll + 1, y, maxw - ll - 1, st, "%s", value); +} + +/** @brief kv on one row, abandoned when it would cross @p limit; returns the + * column after it. */ +static int pair(tui *t, int x, int y, const char *label, const char *value, + tui_style st, int limit) { + int n = (int)strlen(label) + 1 + (int)strlen(value); + + if (x + n > limit) + return x; + kv(t, x, y, n, label, value, st); + return x + n; +} + +/* Case-folded substring match, so /csa finds CSAPOOL. */ +static bool match(const char *hay, const char *pat) { + size_t n = strlen(pat); + + if (n == 0) + return true; + for (; *hay != '\0'; hay++) { + size_t i = 0; + + while (i < n && hay[i] != '\0' && + tolower((unsigned char)hay[i]) == tolower((unsigned char)pat[i])) + i++; + if (i == n) + return true; + } + return false; +} + +/* The filter narrows the order array, so every reader of sub_order sees the + * filtered list and the sort applies to what is left. */ +static void order_storage(app *a) { + const vsm_info *v = &a->info[a->sub]; + int n = 0; + + for (int i = 0; i < v->nsub; i++) { + if (a->filter[0] != '\0') { + char key[24]; + + if (a->sub == 3) + (void)snprintf(key, sizeof key, "%u %08X", v->sub[i].id, + (unsigned int)v->sub[i].tcb); + else + (void)snprintf(key, sizeof key, "%u", v->sub[i].id); + if (!match(key, a->filter)) + continue; + } + a->sub_order[n++] = i; + } + a->sub_rows = n; sort_info = v; sort_key = a->sub_sort; sort_dir = a->sub_dir; - qsort(a->sub_order, (size_t)v->nsub, sizeof a->sub_order[0], cmp_sub); + qsort(a->sub_order, (size_t)n, sizeof a->sub_order[0], cmp_sub); +} + +/* Bodies live with the other detail drawing; the storage view places them + * either inside a popup or in the inspector pane. */ +static void subpool_box(tui *t, const app *a, tui_rect r, int idx); +static void tcb_box(tui *t, const app *a, tui_rect r, int row); + +/* One place for the row layout: the views draw from this rect and the + * mouse hit-tests against the same one, so a header change cannot leave a + * click target behind. STORAGE keeps its area tabs on the row above. */ +static tui_rect table_rect(int w, int h, int tab) { + int y = tab == 0 ? 5 : 3; + + return (tui_rect){1, y, w - 2, h - y - 2}; } -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}; +static int table_rows(int w, int h, int tab) { + int n = table_rect(w, h, tab).h - 1; + + return n > 0 ? n : 1; +} + +static void draw_storage(tui *t, app *a) { + const vsm_info *v = &a->info[a->sub]; + tui_rect r = table_rect(t->w, t->h, 0); + tui_rect pane = {0, 0, 0, 0}; + bool side = a->inspector && t->w >= 110; char used[16], freed[16], mapped[16], total[16], pcbuf[8]; char head[24]; - struct tui_col cols[8]; + tui_col cols[8]; static const int col_of[SS_COUNT] = {0, 1, 2, 3, 4, 6}; int p = pct(v); @@ -574,10 +962,16 @@ static void draw_storage(struct tui *t, struct app *a) { a->sub_dir < 0 ? "^" : "v"); cols[col_of[a->sub_sort]].title = head; a->storage.cols = cols; - tui_printf(t, 1, 3, 6, a->sub == 0 ? TUI_TAB_ON : TUI_TAB_OFF, " CSA "); - tui_printf(t, 7, 3, 6, a->sub == 1 ? TUI_TAB_ON : TUI_TAB_OFF, " SQA "); - tui_printf(t, 13, 3, 7, a->sub == 2 ? TUI_TAB_ON : TUI_TAB_OFF, " LSQA "); - tui_printf(t, 20, 3, 6, a->sub == 3 ? TUI_TAB_ON : TUI_TAB_OFF, " PVT "); + tui_printf(t, 1, r.y - 2, 6, a->sub == 0 ? TUI_TAB_ON : TUI_TAB_OFF, + " CSA "); + tui_printf(t, 7, r.y - 2, 6, a->sub == 1 ? TUI_TAB_ON : TUI_TAB_OFF, + " SQA "); + tui_printf(t, 13, r.y - 2, 7, a->sub == 2 ? TUI_TAB_ON : TUI_TAB_OFF, + " LSQA "); + tui_printf(t, 20, r.y - 2, 6, a->sub == 3 ? TUI_TAB_ON : TUI_TAB_OFF, + " PVT "); + if (side) + tui_split_h(r, r.w - 46, &r, &pane); if (!a->have[a->sub]) { tui_printf(t, 1, 6, t->w - 2, TUI_BAD, "%s: %s", area_upper[a->sub], @@ -594,54 +988,71 @@ static void draw_storage(struct tui *t, struct app *a) { (void)snprintf(total, sizeof total, "-"); (void)snprintf(pcbuf, sizeof pcbuf, "use -"); } - tui_printf(t, 1, 4, t->w - 16, TUI_PLAIN, - "used %s free %s mapped %s total %s", used, freed, mapped, - total); - tui_printf(t, t->w - 15 - (int)strlen(pcbuf), 4, (int)strlen(pcbuf), + { + int gx = t->w - 20 - (int)strlen(pcbuf); + int x = 2; + + x = pair(t, x, 4, "used", used, TUI_ACCENT, gx); + x = pair(t, x + 2, 4, "free", freed, TUI_PLAIN, gx); + x = pair(t, x + 2, 4, "mapped", mapped, TUI_PLAIN, gx); + (void)pair(t, x + 2, 4, "total", total, TUI_PLAIN, gx); + } + tui_printf(t, t->w - 19 - (int)strlen(pcbuf), 4, (int)strlen(pcbuf), p < 0 ? TUI_META : p < 70 ? TUI_GOOD : p < 85 ? TUI_WARN : TUI_BAD, "%s", pcbuf); if (p >= 0) - tui_progress(t, t->w - 14, 4, 12, p); - tui_table_draw(t, r, &a->storage, v->nsub, storage_cell, NULL, a); + tui_progress(t, t->w - 18, 4, 16, p); + if (folded(a)) { + a->storage.cols = pvt_fold_cols; + a->storage.vp = a->pvt_vp; + tui_table_draw(t, r, &a->storage, a->npvt, pvt_cell, nullptr, a); + } else { + tui_table_draw(t, r, &a->storage, a->sub_rows, storage_cell, nullptr, + a); + } + if (side) { + if (folded(a) && a->pvt_vp.sel < a->npvt) + tcb_box(t, a, pane, a->pvt_vp.sel); + else if (!folded(a) && a->sub_rows > 0) + subpool_box(t, a, pane, a->sub_order[a->storage.vp.sel]); + } - 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) { - char ua[16], ul[16]; + if (a->err[a->sub][0] != '\0') { + footer_line(t, a->err[a->sub], TUI_BAD); + } else if (v->unalloc_valid) { + char ua[16], ul[16], note[96]; human(v->unalloc, ua, sizeof ua); human(v->largest_unalloc, ul, sizeof ul); - tui_printf(t, 1, t->h - 2, t->w - 2, TUI_META, - "unallocated %s in %u blocks, largest %s", ua, - v->unalloc_blocks, ul); + (void)snprintf(note, sizeof note, + "unallocated %s in %u blocks, largest %s", ua, + v->unalloc_blocks, ul); + footer_line(t, note, TUI_META); } -} + if (a->ntrend[a->sub] > 0 && t->w >= 100) { + int n = a->ntrend[a->sub]; + int x = t->w - 4 - n; -/** @brief A dim label and its value, one blank apart. */ -static void kv(struct tui *t, int x, int y, int maxw, const char *label, - const char *value, tui_style st) { - int ll = (int)strlen(label); - - if (ll >= maxw - 1) - return; - tui_printf(t, x, y, ll, TUI_META, "%s", label); - tui_printf(t, x + ll + 1, y, maxw - ll - 1, st, "%s", value); + tui_printf(t, x - 5, t->h - 2, 4, TUI_META, "used"); + tui_sparkline(t, x, t->h - 2, a->trend[a->sub], n, TUI_ACCENT); + } } -/** @brief An accent heading with a dim rule filling the row. */ -static void section(struct tui *t, int y, const char *name) { +/** @brief An accent heading with a dim rule filling the row, stopped at the + * panel border. */ +static void section(tui *t, int y, const char *name) { int n = (int)strlen(name); tui_printf(t, 1, y, n, TUI_ACCENT, "%s", name); - if (n + 2 < t->w) - tui_fill(t, n + 2, y, t->w - n - 2, 1, '-', TUI_META); + if (n + 2 < t->w - 1) + tui_fill(t, n + 2, y, t->w - n - 3, 1, '-', TUI_META); } -static void draw_system(struct tui *t, const struct app *a) { - const struct zsys *z = &a->sys; +static void draw_system(tui *t, const app *a) { + const zsys *z = &a->sys; char b1[32], b2[32]; int half = t->w / 2; @@ -681,8 +1092,8 @@ static void draw_system(struct tui *t, const struct app *a) { } } -static void draw_paging(struct tui *t, const struct app *a) { - const struct zpage *p = &a->page; +static void draw_paging(tui *t, const app *a) { + const zpage *p = &a->page; char b[48]; int half = t->w / 2; @@ -721,19 +1132,27 @@ static void draw_paging(struct tui *t, const struct app *a) { kv(t, half, 12, t->w - half - 1, "control intervals", b, TUI_PLAIN); } -static void draw_jobs(struct tui *t, struct app *a) { - tui_rect r = {0, 3, t->w, t->h - 5}; +static void draw_jobs(tui *t, app *a) { + tui_rect r = table_rect(t->w, t->h, 2); char head[24]; - struct tui_col cols[3]; + tui_col cols[3]; if (a->have_sys) { - for (int i = 0; i < a->sys.njobs; i++) - a->job_order[i] = i; + int n = 0; + + for (int i = 0; i < a->sys.njobs; i++) { + char key[24]; + + (void)snprintf(key, sizeof key, "%u %s", a->sys.job[i].asid, + a->sys.job[i].name); + if (match(key, a->filter)) + a->job_order[n++] = i; + } + a->job_rows = n; sort_sys = &a->sys; sort_key = a->job_sort; sort_dir = a->job_dir; - qsort(a->job_order, (size_t)a->sys.njobs, sizeof a->job_order[0], - cmp_job); + qsort(a->job_order, (size_t)n, sizeof a->job_order[0], cmp_job); (void)memcpy(cols, job_cols, sizeof cols); (void)snprintf(head, sizeof head, "%s %s", job_sort_names[a->job_sort], a->job_dir < 0 ? "^" : "v"); @@ -744,93 +1163,461 @@ static void draw_jobs(struct tui *t, struct app *a) { 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); + tui_table_draw(t, r, &a->jobs, a->job_rows, job_cell, nullptr, a); + { + char note[64]; + + (void)snprintf(note, sizeof note, "%d address spaces, %u slots", + a->sys.njobs, a->sys.slots); + footer_line(t, note, TUI_LABEL); + } } /** @brief Everything about the selected subpool, over the table. */ -static void draw_subpool(struct tui *t, const struct app *a) { - const struct vsm_info *v = &a->info[a->sub]; - int i = v->nsub ? a->sub_order[a->storage.vp.sel] : 0; - const struct vsm_subpool *s = &v->sub[i]; - int w = 46; - int h = 12; - int x = (t->w - w) / 2; - int y = (t->h - h) / 2; - char u[16], f[16], m[16], l[16], b[32]; +/* One subpool, painted into whatever rectangle is handed in: the popup and + * the inspector pane share this body. */ +static void subpool_box(tui *t, const app *a, tui_rect r, int idx) { + const vsm_info *v = &a->info[a->sub]; + const vsm_subpool *s = &v->sub[idx]; + char key[16], blocks[16], used[16], freed[16], mapped[16], largest[16]; + char below[16], above[16], where[32], title[32]; + const char *k[9]; + const char *val[9]; + int n = 0; - if (x < 0) - x = 0; - if (y < 0) - y = 0; - human(s->used, u, sizeof u); - human(s->free, f, sizeof f); - human(s->foot, m, sizeof m); - human(s->largest, l, sizeof l); - tui_fill(t, x, y, w, h, ' ', TUI_PLAIN); - (void)snprintf(b, sizeof b, "subpool %u", s->id); - tui_box_frame(t, x, y, w, h, TUI_ACCENT, b, TUI_ACCENT); - (void)snprintf(b, sizeof b, "%u", s->key); - kv(t, x + 2, y + 2, w - 4, "key", b, TUI_PLAIN); - (void)snprintf(b, sizeof b, "%u", s->blocks); - kv(t, x + 2, y + 3, w - 4, "blocks", b, TUI_PLAIN); - kv(t, x + 2, y + 4, w - 4, "used", u, TUI_PLAIN); - kv(t, x + 2, y + 5, w - 4, "free", f, TUI_PLAIN); - kv(t, x + 2, y + 6, w - 4, "mapped", m, TUI_PLAIN); - kv(t, x + 2, y + 7, w - 4, "largest", l, TUI_PLAIN); - if (v->area == VSM_PVT) { - (void)snprintf(b, sizeof b, "%08X", (unsigned int)s->tcb); - kv(t, x + 2, y + 8, w - 4, "tcb", b, TUI_PLAIN); - } else { - (void)snprintf(b, sizeof b, "%08X-%08X", (unsigned int)s->lo, + human(s->used, used, sizeof used); + human(s->free, freed, sizeof freed); + human(s->foot, mapped, sizeof mapped); + human(s->largest, largest, sizeof largest); + human(s->below, below, sizeof below); + human(s->above, above, sizeof above); + (void)snprintf(key, sizeof key, "%u", s->key); + (void)snprintf(blocks, sizeof blocks, "%u", s->blocks); + if (v->area == VSM_PVT) + (void)snprintf(where, sizeof where, "%08X", (unsigned int)s->tcb); + else + (void)snprintf(where, sizeof where, "%08X-%08X", (unsigned int)s->lo, (unsigned int)s->hi); - kv(t, x + 2, y + 8, w - 4, "address", b, TUI_PLAIN); + k[n] = "key", val[n++] = key; + k[n] = "blocks", val[n++] = blocks; + k[n] = "used", val[n++] = used; + k[n] = "below 16M", val[n++] = below; + k[n] = "above 16M", val[n++] = above; + k[n] = "free", val[n++] = freed; + k[n] = "mapped", val[n++] = mapped; + k[n] = "largest", val[n++] = largest; + k[n] = v->area == VSM_PVT ? "tcb" : "address", val[n++] = where; + + (void)snprintf(title, sizeof title, "subpool %u", s->id); + tui_fill(t, r.x, r.y, r.w, r.h, ' ', TUI_PLAIN); + tui_box_frame(t, r.x, r.y, r.w, r.h, TUI_FRAME, title, TUI_ACCENT); + for (int i = 0; i < n && r.y + 2 + i < r.y + r.h - 1; i++) + kv(t, r.x + 2, r.y + 2 + i, r.w - 4, k[i], val[i], TUI_PLAIN); + if (r.h >= 4) + tui_printf(t, r.x + 2, r.y + r.h - 2, r.w - 4, TUI_META, + "any key closes"); +} + +/* One folded TCB row: its totals, then the subpools that make them up. */ +static void tcb_box(tui *t, const app *a, tui_rect r, int row) { + const vsm_info *v = &a->info[3]; + const pvt_row *p = &a->pvt[row]; + char used[16], below[16], above[16], subs[16], title[40]; + int line; + + human(p->used, used, sizeof used); + human(p->below, below, sizeof below); + human(p->above, above, sizeof above); + (void)snprintf(subs, sizeof subs, "%u", p->nsub); + (void)snprintf(title, sizeof title, "TCB %08X", (unsigned int)p->tcb); + tui_fill(t, r.x, r.y, r.w, r.h, ' ', TUI_PLAIN); + tui_box_frame(t, r.x, r.y, r.w, r.h, TUI_FRAME, title, TUI_ACCENT); + kv(t, r.x + 2, r.y + 2, r.w - 4, "subpools", subs, TUI_PLAIN); + kv(t, r.x + 2, r.y + 3, r.w - 4, "used", used, TUI_PLAIN); + kv(t, r.x + 2, r.y + 4, r.w - 4, "below 16M", below, TUI_PLAIN); + kv(t, r.x + 2, r.y + 5, r.w - 4, "above 16M", above, TUI_PLAIN); + line = r.y + 7; + if (line < r.y + r.h - 1) + tui_printf(t, r.x + 2, line++, r.w - 4, TUI_LABEL, "subpools"); + for (int i = 0; i < v->nsub && line < r.y + r.h - 1; i++) { + const vsm_subpool *s = &v->sub[i]; + char u[16], b[16], ab[16]; + + if (s->tcb != p->tcb) + continue; + human(s->used, u, sizeof u); + human(s->below, b, sizeof b); + human(s->above, ab, sizeof ab); + tui_printf(t, r.x + 2, line++, r.w - 4, TUI_PLAIN, "%2u %8s %s%s", + s->id, u, b[0] == '0' ? "" : "<16M ", + ab[0] == '0' ? "" : ">16M"); } - tui_printf(t, x + 2, y + h - 2, w - 4, TUI_META, "any key closes"); + if (r.h >= 4) + tui_printf(t, r.x + 2, r.y + r.h - 2, r.w - 4, TUI_META, + "any key closes"); } -static void draw_help(struct tui *t) { - static const struct tui_hint views[] = { - {"TAB", "next view"}, - {"Shift-TAB", "previous view"}, - {"1-4", "CSA SQA LSQA PVT"}, - {"click", "a tab"}, - }; - static const struct tui_hint moving[] = { - {"j k", "one row"}, {"PgUp PgDn", "a page"}, {"g G", "first, last"}, - {"wheel", "scroll"}, {"Enter", "details"}, {"s S", "sort, reverse"}, - }; - static const struct tui_hint screen[] = { - {"p", "pause"}, {"r", "refresh now"}, {"+ -", "interval 1..60 s"}, - {"?", "help"}, {"q", "quit"}, - }; - int w = t->w < 64 ? t->w - 2 : 62; - int h = 15; - int x = (t->w - w) / 2; - int y = (t->h - h) / 2; +static void draw_popup(tui *t, app *a) { + tui_rect r = {(t->w - 46) / 2, (t->h - 13) / 2, 46, 13}; + const vsm_info *v = &a->info[a->sub]; + + if (r.x < 0) + r.x = 0; + if (r.y < 0) + r.y = 0; + if (a->tcb_popup) { + if (a->pvt_vp.sel < a->npvt) + tcb_box(t, a, r, a->pvt_vp.sel); + } else if (v->nsub > 0) { + subpool_box(t, a, r, a->sub_order[a->storage.vp.sel]); + } +} - if (x < 0) - x = 0; - if (y < 0) - y = 0; - tui_fill(t, x, y, w, h, ' ', TUI_PLAIN); - tui_box_frame(t, x, y, w, h, TUI_ACCENT, "keys", TUI_ACCENT); - tui_printf(t, x + 2, y + 2, w - 4, TUI_LABEL, "views"); - tui_hints(t, x + 2, y + 3, w - 4, views, 4); - tui_printf(t, x + 2, y + 5, w - 4, TUI_LABEL, "moving"); - tui_hints(t, x + 2, y + 6, w - 4, moving, 3); - tui_hints(t, x + 2, y + 7, w - 4, moving + 3, 3); - tui_printf(t, x + 2, y + 9, w - 4, TUI_LABEL, "the screen"); - tui_hints(t, x + 2, y + 10, w - 4, screen, 5); +static void draw_help(tui *t) { + tui_keymap_help(t, keymap, KEYMAP_COUNT, "keys"); +} + +/* A filter edit re-runs the orders, because every reader indexes through + * them: the table, the detail line, the popup and the mouse. */ +static void filter_changed(app *a) { + (void)snprintf(a->filter, sizeof a->filter, "%s", a->fin.buf); + order_storage(a); + build_pvt(a); + a->storage.vp.sel = 0; + a->storage.vp.top = 0; + a->pvt_vp.sel = 0; + a->pvt_vp.top = 0; + a->jobs.vp.sel = 0; + a->jobs.vp.top = 0; +} + +static void run_command(app *a, const char *cmd) { + char c[32]; + char *p = c; + size_t n; + + (void)snprintf(c, sizeof c, "%s", cmd); + for (char *q = c; *q != '\0'; q++) + *q = (char)tolower((unsigned char)*q); + while (*p == ' ') + p++; + n = strlen(p); + while (n > 0 && p[n - 1] == ' ') + p[--n] = '\0'; + + if (strcmp(p, "csa") == 0 || strcmp(p, "sqa") == 0 || + strcmp(p, "lsqa") == 0 || strcmp(p, "pvt") == 0) { + a->tab = 0; + if (p[0] == 'c') + a->sub = 0; + else if (p[0] == 'l') + a->sub = 2; + else if (p[0] == 'p') + a->sub = 3; + else + a->sub = 1; + } else if (strcmp(p, "sys") == 0) { + a->tab = 1; + } else if (strcmp(p, "jobs") == 0 || strcmp(p, "da") == 0) { + a->tab = 2; + } else if (strcmp(p, "page") == 0) { + a->tab = 3; + } else if (strcmp(p, "ps") == 0) { + a->tab = 4; + a->have_ps = uss_ps_read(&a->ps); + } else if (strcmp(p, "fs") == 0) { + a->tab = 5; + a->have_fs = uss_fs_read(&a->fs); + } else if (strcmp(p, "fold") == 0) { + a->tab = 0; + a->sub = 3; + a->pvt_fold = !a->pvt_fold; + } else if (strcmp(p, "log") == 0) { + a->tab = 6; + if (!a->log_on) + log_start(a); + log_poll(a); + } else if (strcmp(p, "pause") == 0) { + a->paused = !a->paused; + } else if (strcmp(p, "help") == 0) { + a->help = true; + } else if (strcmp(p, "quit") == 0 || strcmp(p, "q") == 0) { + a->tapp->quit = true; + } +} + +static const tui_col ps_cols[] = { + {"PID", 9, true}, {"PPID", 9, true}, {"UID", 7, true}, + {"ST", 3, false}, {"CPU", 9, true}, {"COMMAND", 44, false}, +}; + +static const tui_col fs_cols[] = { + {"MOUNT", 28, false}, {"TYPE", 9, false}, {"SIZE", 9, true}, + {"FREE", 9, true}, {"USED", 5, true}, {"RO", 2, false}, + {"FS", 45, false}, +}; + +static void ps_cell(void *ctx, int row, int col, char *buf, size_t len) { + const app *a = ctx; + const uss_proc *p; + + if (row >= a->ps.nproc) { + buf[0] = '\0'; + return; + } + p = &a->ps.proc[row]; + switch (col) { + case 0: + (void)snprintf(buf, len, "%d", p->pid); + break; + case 1: + (void)snprintf(buf, len, "%d", p->ppid); + break; + case 2: + (void)snprintf(buf, len, "%d", p->uid); + break; + case 3: + (void)snprintf(buf, len, "%c%s", p->st, p->swapped ? "S" : ""); + break; + case 4: + (void)snprintf(buf, len, "%lu.%02lu", p->cpu_ms / 1000, + (p->cpu_ms % 1000) / 10); + break; + default: + (void)snprintf(buf, len, "%s", p->cmd); + break; + } +} + +static void fs_cell(void *ctx, int row, int col, char *buf, size_t len) { + const app *a = ctx; + const uss_fs *f; + char b[16]; + + if (row >= a->fs.nfs) { + buf[0] = '\0'; + return; + } + f = &a->fs.fs[row]; + switch (col) { + case 0: + (void)snprintf(buf, len, "%s", f->mount); + break; + case 1: + (void)snprintf(buf, len, "%s", f->type); + break; + case 2: + human(f->kb_total * 1024, b, sizeof b); + (void)snprintf(buf, len, "%s", f->ok ? b : "-"); + break; + case 3: + human(f->kb_free * 1024, b, sizeof b); + (void)snprintf(buf, len, "%s", f->ok ? b : "-"); + break; + case 4: + if (f->pct < 0) + (void)snprintf(buf, len, "-"); + else + (void)snprintf(buf, len, "%d%%", f->pct); + break; + case 5: + (void)snprintf(buf, len, "%s", f->rdonly ? "ro" : "rw"); + break; + default: + (void)snprintf(buf, len, "%s", f->fsname); + break; + } +} + +static void draw_ps(tui *t, app *a) { + tui_rect r = table_rect(t->w, t->h, 4); + char note[64]; + + if (!a->have_ps) { + tui_printf(t, 1, 4, t->w - 2, TUI_BAD, "%s", a->ps.err); + return; + } + tui_table_draw(t, r, &a->pst, a->ps.nproc, ps_cell, nullptr, a); + (void)snprintf(note, sizeof note, "%d processes%s", a->ps.nproc, + a->ps.truncated ? ", more not shown" : ""); + footer_line(t, note, TUI_LABEL); +} + +static void draw_fs(tui *t, app *a) { + tui_rect r = table_rect(t->w, t->h, 5); + char note[96]; + + if (!a->have_fs) { + tui_printf(t, 1, 4, t->w - 2, TUI_BAD, "%s", a->fs.err); + return; + } + tui_table_draw(t, r, &a->fst, a->fs.nfs, fs_cell, nullptr, a); + (void)snprintf(note, sizeof note, "%d file systems%s", a->fs.nfs, + a->fs.truncated ? ", more not shown" : ""); + footer_line(t, note, TUI_LABEL); +} + +/* ---- the console log ---- */ + +/* One line of the ring. A line that arrives when the ring is full + * overwrites the oldest, which is what a console does. */ +static void log_put(app *a, const char *line, int len) { + char *dst = a->log[a->log_next]; + + if (len > LOG_COLS - 1) + len = LOG_COLS - 1; + if (len < 0) + len = 0; + (void)memcpy(dst, line, (size_t)len); + dst[len] = '\0'; + a->log_next = (a->log_next + 1) % LOG_LINES; + if (a->nlog < LOG_LINES) + a->nlog++; +} + +/* Line i, counted from the oldest kept. */ +static const char *log_at(const app *a, int i) { + return a->log[(a->log_next + LOG_LINES - a->nlog + i) % LOG_LINES]; +} + +/* The object walk lives in src/mdb.c: it parses bytes from another + * address space and has a test of its own. */ +static void log_mdb(app *a) { + char job[16]; + char text[MDB_TEXT_MAX]; + char line[LOG_COLS]; + int n; + + if (!mdb_line(a->mdb, a->mdb_len, job, sizeof job, text, sizeof text)) + return; + n = snprintf(line, sizeof line, "%-8s %s", job, text); + if (n < 0) + return; + if (n > (int)sizeof line - 1) + n = (int)sizeof line - 1; + log_put(a, line, n); +} + +/* Take the console down. Called on quit, and safe to call when it was + * never up. */ +static void log_stop(app *a) { + if (!a->log_on) + return; + a->creq.op = ZMCS_DEACTIVATE; + a->creq.consid = &a->creq_consid; + (void)zmcs(&a->creq); + a->log_on = false; +} + +/* Open the console. A refusal is not fatal: the other views do not need + * it, so the reason is kept and shown on the tab. */ +/* The console stub keeps the MCSOPER control blocks in its own CSECT, + * which is read-only in this link: it has to be built as a 31-bit module + * and thunked to (src/zmcs.s says why). Until that module exists, -c + * reports it instead of taking a 0C4 on the first privileged call. */ +static bool console_built(void) { + return false; +} + +static void log_start(app *a) { + if (!console_opt) { + (void)snprintf(a->log_err, sizeof a->log_err, + "console off: ztop -c after make auth"); + return; + } + if (!console_built()) { + (void)snprintf(a->log_err, sizeof a->log_err, + "console: the stub is not a 31-bit module yet"); + return; + } + a->creq = (zmcs_req){}; + console_name(a->creq_name); + a->creq.op = ZMCS_ACTIVATE; + a->creq.name = a->creq_name; + a->creq.consid = &a->creq_consid; + (void)zmcs(&a->creq); + if (a->creq.rc > 4) { + if (a->creq.rc == 12) + (void)snprintf(a->log_err, sizeof a->log_err, + "console needs APF authorization: MODESET rc %d", + a->creq.rc); + else + (void)snprintf(a->log_err, sizeof a->log_err, + "MCSOPER activate: rc %d rsn %d", a->creq.rc, + a->creq.rsn); + return; + } + a->log_on = true; + a->log_err[0] = '\0'; +} + +/* Drain what is queued. Bounded: a console burst must not stall a frame, + * and one call per tick is the steady state, which costs a single + * MCSOPMSG. */ +static void log_poll(app *a) { + int i; + + if (!a->log_on) + return; + /* QLIMIT bounds the queue at the console end; this bounds the work per + * refresh, so a burst cannot stall a redraw. */ + a->creq.consid = &a->creq_consid; + a->creq.buf = a->mdb; + a->creq.len = &a->mdb_len; + for (i = 0; i < 200; i++) { + a->mdb_len = (int)sizeof a->mdb; + a->creq.op = ZMCS_GETMSG; + (void)zmcs(&a->creq); + if (a->creq.rc == 8) /* nothing queued */ + return; + if (a->creq.rc != 0 && a->creq.rc != 4) { + (void)snprintf(a->log_err, sizeof a->log_err, + "MCSOPMSG: rc %d rsn %d", a->creq.rc, a->creq.rsn); + log_stop(a); + return; + } + log_mdb(a); + } +} + +static void draw_log(tui *t, app *a) { + tui_rect r = table_rect(t->w, t->h, 6); + int rows = table_rows(t->w, t->h, 6); + char note[64]; + + if (!a->log_on) { + tui_printf(t, 1, 4, t->w - 2, TUI_BAD, "%s", + a->log_err[0] != '\0' ? a->log_err + : "the console is not open"); + return; + } + tui_viewport_clamp(&a->log_vp, a->nlog, rows - 1); + for (int i = 0; i < rows - 1 && a->log_vp.top + i < a->nlog; i++) { + int line = a->log_vp.top + i; + + tui_printf(t, r.x, r.y + 1 + i, r.w, + line == a->log_vp.sel ? TUI_TAB_ON : TUI_PLAIN, "%s", + log_at(a, line)); + } + (void)snprintf(note, sizeof note, "%d messages%s", a->nlog, + a->nlog >= LOG_LINES ? ", oldest dropped" : ""); + footer_line(t, note, TUI_LABEL); } /** @brief The right half of the status line: what is under the cursor. */ -static void detail(struct app *a, char *buf, size_t len) { +static void detail(app *a, char *buf, size_t len) { if (a->paused) { (void)snprintf(buf, len, "PAUSED"); - } else if (a->tab == 0 && a->have[a->sub] && a->info[a->sub].nsub > 0) { - const struct vsm_subpool *sp = &a->info[a->sub].sub[a->storage.vp.sel]; + } else if (folded(a) && a->pvt_vp.sel < a->npvt) { + const pvt_row *p = &a->pvt[a->pvt_vp.sel]; + char u[16]; + + human(p->used, u, sizeof u); + (void)snprintf(buf, len, "TCB %08X %u subpools %s", + (unsigned int)p->tcb, p->nsub, u); + } else if (a->tab == 0 && a->have[a->sub] && a->sub_rows > 0) { + const vsm_info *v = &a->info[a->sub]; + const vsm_subpool *sp = &v->sub[a->sub_order[vp_of(a)->sel]]; if (a->sub == 3) (void)snprintf(buf, len, "sp %u tcb %08X", sp->id, @@ -840,43 +1627,86 @@ static void detail(struct app *a, char *buf, size_t len) { else (void)snprintf(buf, len, "sp %u %08X-%08X", sp->id, (unsigned int)sp->lo, (unsigned int)sp->hi); - } else if (a->tab == 2 && a->have_sys && a->sys.njobs > 0) { - const struct zjob *j = &a->sys.job[a->jobs.vp.sel]; + } else if (a->tab == 2 && a->have_sys && a->job_rows > 0) { + const zjob *j = &a->sys.job[a->job_order[a->jobs.vp.sel]]; (void)snprintf(buf, len, "asid %u %s", j->asid, j->name); + } else if (a->tab == 4 && a->have_ps && a->ps.nproc > 0) { + const uss_proc *p = &a->ps.proc[a->pst.vp.sel]; + + (void)snprintf(buf, len, "pid %d uid %d %s", p->pid, p->uid, p->cmd); + } else if (a->tab == 5 && a->have_fs && a->fs.nfs > 0) { + const uss_fs *f = &a->fs.fs[a->fst.vp.sel]; + + if (f->ok) + (void)snprintf(buf, len, "%s %llu%% used", f->mount, + (unsigned long long)(f->pct < 0 ? 0 : f->pct)); + else + (void)snprintf(buf, len, "%s statvfs errno %d", f->mount, f->eno); } else { buf[0] = '\0'; } } -static void view(void *model, struct tui *t) { - struct app *a = model; +static void view(void *model, tui *t) { + static const char *const title[7] = { + "STORAGE", "SYSTEM", "JOBS", "PAGING", + "PROCESSES", "FILE SYSTEMS", "CONSOLE", + }; + app *a = model; char right[48]; 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)); + /* The tables draw a clamped copy, so the model's selection is brought + * into range here, before the detail lines read it. */ + tui_viewport_clamp(&a->storage.vp, a->info[a->sub].nsub, + table_rows(t->w, t->h, 0)); + tui_viewport_clamp(&a->jobs.vp, a->sys.njobs, table_rows(t->w, t->h, 2)); + tui_viewport_clamp(&a->pst.vp, a->ps.nproc, table_rows(t->w, t->h, 4)); + tui_viewport_clamp(&a->fst.vp, a->fs.nfs, table_rows(t->w, t->h, 5)); + tui_viewport_clamp(&a->log_vp, a->nlog, table_rows(t->w, t->h, 6)); + tui_viewport_clamp(&a->pvt_vp, a->npvt, table_rows(t->w, t->h, 0)); + if (t->w >= 70 && t->h >= 16) + tui_box_frame(t, 0, 2, t->w, t->h - 3, TUI_FRAME, title[a->tab], + TUI_ACCENT); - if (t->w < 70 || t->h < 14) { - tui_printf(t, 1, 3, 0, TUI_BAD, "terminal too small: need 70x14"); + if (t->w < 70 || t->h < 16) { + tui_printf(t, 1, 3, 0, TUI_BAD, "terminal too small: need 70x16"); } else if (a->tab == 0) { draw_storage(t, a); } else if (a->tab == 1) { draw_system(t, a); } else if (a->tab == 2) { draw_jobs(t, a); + } else if (a->tab == 4) { + draw_ps(t, a); + } else if (a->tab == 5) { + draw_fs(t, a); + } else if (a->tab == 6) { + draw_log(t, a); } else { draw_paging(t, a); } if (a->popup) - draw_subpool(t, a); + draw_popup(t, a); if (a->help) draw_help(t); detail(a, right, sizeof right); - tui_statusbar(t, NULL, right); - tui_hints(t, 0, t->h - 1, t->w - 20, a->tab == 0 ? storage_bar : view_bar, - 4); + tui_statusbar(t, nullptr, right); + if (a->filtering) { + tui_put(t, 0, t->h - 1, "/", 1, TUI_KEYCAP); + tui_input_draw(t, (tui_rect){1, t->h - 1, t->w - 1, 1}, &a->fin, + TUI_BAR); + } else if (a->commanding) { + tui_put(t, 0, t->h - 1, ":", 1, TUI_KEYCAP); + tui_input_draw(t, (tui_rect){1, t->h - 1, t->w - 1, 1}, &a->cin, + TUI_BAR); + } else { + tui_keymap_footer(t, 0, t->h - 1, t->w - 20, keymap, KEYMAP_COUNT, + a->tab); + } } static bool parse_areas(const char *s, unsigned int *mask) { @@ -916,71 +1746,63 @@ static void usage(FILE *f) { " -p auxiliary storage: page data sets, local paging, VIO journal\n" " -x dump the VSMLIST work and data areas (one-shot only)\n" " -d force the interactive view (the default on a terminal)\n" + " -c open the MCS console for the LOG view (needs APF, see\n" + " make auth; an unauthorized program ends with S047)\n" " -h this text\n"); } -static void wheel(struct app *a, int dir, int height) { - if (a->tab == 0) - tui_viewport_move(&a->storage.vp, dir, a->info[a->sub].nsub, height); - else if (a->tab == 2) - tui_viewport_move(&a->jobs.vp, dir, a->sys.njobs, height); +static void wheel(app *a, int dir, int height) { + if (a->tab == 0 || a->tab == 2 || a->tab == 6) + tui_viewport_move(vp_of(a), dir, vrows(a), height); } /** @brief Tabs, sub-tabs, table rows and the wheel. */ -static bool mouse_event(struct tui_app *app, const struct tui_msg *m) { - static const char *const views[3] = {"1 STORAGE", "2 SYSTEM", "3 JOBS"}; - struct app *a = app->model; - const struct tui_mouse *mo = &m->mouse; - int idx, row, height; +static bool mouse_event(tui_app *ui, const tui_msg *m) { + app *a = ui->model; + const tui_mouse *mo = &m->mouse; + tui_rect r = table_rect(m->w, m->h, a->tab); + int idx, row; - height = (a->tab == 0 ? m->h - 8 : m->h - 6); - if (height < 1) - height = 1; if (mo->button == 64 || mo->button == 65) { - wheel(a, mo->button == 64 ? -1 : 1, height); + wheel(a, mo->button == 64 ? -1 : 1, table_rows(m->w, m->h, a->tab)); return true; } if (mo->release || mo->button != 0) return false; if (mo->y == 1) { - idx = tui_tabs_hit(mo->x, views, 3); + idx = tui_tabs_hit(mo->x, tab_labels, 7); if (idx < 0) return false; a->tab = idx; return true; } - if (a->tab == 0 && mo->y == 3) { - if (mo->x >= 1 && mo->x <= 5) - a->sub = 0; - else if (mo->x >= 7 && mo->x <= 11) - a->sub = 1; - else if (mo->x >= 13 && mo->x <= 18) - a->sub = 2; - else if (mo->x >= 20 && mo->x <= 24) - a->sub = 3; - else - return false; - return true; + if (a->tab == 0 && mo->y == r.y - 2) { + /* The drawn labels, without their trailing space. */ + static const tui_rect area[4] = { + {1, 0, 5, 1}, {7, 0, 5, 1}, {13, 0, 6, 1}, {20, 0, 5, 1}}; + + for (int i = 0; i < 4; i++) + if (tui_rect_hit((tui_rect){area[i].x, area[i].y + r.y - 2, + area[i].w, area[i].h}, + mo->x, mo->y)) { + a->sub = i; + return true; + } + return false; } - if (a->tab == 0 && mo->y >= 6) { - row = a->storage.vp.top + (mo->y - 6); - if (row < a->info[a->sub].nsub) { - a->storage.vp.sel = row; - return true; - } - } else if (a->tab == 2 && mo->y >= 4) { - row = a->jobs.vp.top + (mo->y - 4); - if (row < a->sys.njobs) { - a->jobs.vp.sel = row; - return true; - } + if (!tui_rect_hit(r, mo->x, mo->y) || mo->y == r.y) + return false; + row = vp_of(a)->top + (mo->y - r.y - 1); + if (row < vrows(a)) { + vp_of(a)->sel = row; + return true; } return false; } -static bool update(struct tui_app *app, const struct tui_msg *m) { - struct app *a = app->model; - int rows, height; +static bool update(tui_app *ui, const tui_msg *m) { + app *a = ui->model; + int height; if (a->help) { if (m->type == TUI_MSG_KEY && (m->key == '?' || m->key == TUI_KEY_ESC || @@ -989,8 +1811,10 @@ static bool update(struct tui_app *app, const struct tui_msg *m) { return true; } if (a->popup) { - if (m->type == TUI_MSG_KEY) + if (m->type == TUI_MSG_KEY) { a->popup = false; + a->tcb_popup = false; + } return true; } if (m->type == TUI_MSG_TICK) { @@ -1001,12 +1825,50 @@ static bool update(struct tui_app *app, const struct tui_msg *m) { if (m->type == TUI_MSG_RESIZE) return true; if (m->type == TUI_MSG_MOUSE) - return mouse_event(app, m); + return mouse_event(ui, m); if (m->type != TUI_MSG_KEY) return false; + if (a->filtering || a->commanding) { + tui_input *in = a->filtering ? &a->fin : &a->cin; + + if (m->key == TUI_KEY_ESC || m->key == 0x03) { + if (a->filtering) { + a->fin = (tui_input){}; + filter_changed(a); + } + a->cin = (tui_input){}; + a->filtering = false; + a->commanding = false; + } else if (m->key == '\r' || m->key == '\n') { + if (a->commanding) + run_command(a, a->cin.buf); + a->cin = (tui_input){}; + a->filtering = false; + a->commanding = false; + } else { + (void)tui_input_key(in, m->key); + if (a->filtering) + filter_changed(a); + } + return true; + } + + if (m->key == '/' || m->key == ':') { + tui_input *in = m->key == '/' ? &a->fin : &a->cin; + + if (m->key == '/') + a->filtering = true; + else + a->commanding = true; + *in = (tui_input){}; + in->focus = true; + return true; + } + if (m->key == 'q' || m->key == TUI_KEY_ESC || m->key == 0x03) { - app->quit = true; + log_stop(a); + ui->quit = true; return false; } if (m->key == '?') { @@ -1017,6 +1879,14 @@ static bool update(struct tui_app *app, const struct tui_msg *m) { a->paused = !a->paused; return true; } + if (m->key == 't' && a->tab == 0 && a->sub == 3) { + a->pvt_fold = !a->pvt_fold; + return true; + } + if (m->key == 'i') { + a->inspector = !a->inspector; + return true; + } if (m->key == 'r') { collect(a); return true; @@ -1033,20 +1903,35 @@ static bool update(struct tui_app *app, const struct tui_msg *m) { return true; } if (m->key == TUI_KEY_TAB) { - a->tab = (a->tab + 1) % 4; + a->tab = (a->tab + 1) % 7; return true; } if (m->key == TUI_KEY_BACKTAB) { - a->tab = (a->tab + 3) % 4; + a->tab = (a->tab + 6) % 7; return true; } - if (a->tab == 0 && m->key >= '1' && m->key <= '4') { - a->sub = m->key - '1'; - a->storage.vp.sel = 0; - a->storage.vp.top = 0; + if (m->key >= '1' && m->key <= '7') { + if (a->tab == 0 && m->key <= '4') { /* 1-4 are the areas here */ + a->sub = m->key - '1'; + a->storage.vp.sel = 0; + a->storage.vp.top = 0; + a->pvt_vp.sel = 0; + a->pvt_vp.top = 0; + return true; + } + a->tab = m->key - '1'; + if (a->tab == 4) + a->have_ps = uss_ps_read(&a->ps); + else if (a->tab == 5) + a->have_fs = uss_fs_read(&a->fs); + else if (a->tab == 6) { + if (!a->log_on) + log_start(a); + log_poll(a); + } return true; } - if ((m->key == 's' || m->key == 'S') && a->tab != 1) { + if ((m->key == 's' || m->key == 'S') && (a->tab == 0 || a->tab == 2)) { int *key = a->tab == 0 ? &a->sub_sort : &a->job_sort; int *dir = a->tab == 0 ? &a->sub_dir : &a->job_dir; int n = a->tab == 0 ? SS_COUNT : JS_COUNT; @@ -1057,24 +1942,21 @@ static bool update(struct tui_app *app, const struct tui_msg *m) { *dir = -*dir; return true; } - if (a->tab == 0 && (m->key == '\r' || m->key == '\n') && - a->info[a->sub].nsub > 0) { + if (a->tab == 0 && (m->key == '\r' || m->key == '\n') && vrows(a) > 0) { a->popup = true; + a->tcb_popup = folded(a); return true; } - rows = a->tab == 0 ? a->info[a->sub].nsub : a->sys.njobs; - height = a->tab == 0 ? m->h - 8 : m->h - 6; + height = vheight(a, m->h); 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); + if (a->tab == 0 || a->tab == 2 || a->tab == 6) + return tui_viewport_key(vp_of(a), m->key, vrows(a), height); return false; } -static void sys_fill(struct zsys *z, const char *host) { +static void sys_fill(zsys *z, const char *host) { struct utsname u; if (uname(&u) == 0) { @@ -1085,9 +1967,9 @@ static void sys_fill(struct zsys *z, const char *host) { } static void run_tui(int interval) { - static struct app a; - struct tui t; - struct tui_app ta; + static app a; + tui t; + tui_app ta; memset(&a, 0, sizeof a); if (gethostname(a.host, sizeof a.host - 1) != 0) @@ -1101,6 +1983,10 @@ static void run_tui(int interval) { a.storage.ncols = 8; a.jobs.cols = job_cols; a.jobs.ncols = 3; + a.pst.cols = ps_cols; + a.pst.ncols = 6; + a.fst.cols = fs_cols; + a.fst.ncols = 7; if (!tui_open(&t)) { (void)fprintf(stderr, "ztop: -d needs a terminal\n"); @@ -1115,6 +2001,7 @@ static void run_tui(int interval) { ta.tick_ms = interval * 1000; a.tapp = &ta; tui_run(&t, &ta); + log_stop(&a); /* a signal can end the loop without the q path */ tui_close(&t); } @@ -1126,7 +2013,7 @@ static void run_oneshot(unsigned mask, bool want_sys, bool want_jobs, int i; for (i = 0; i < 4; i++) { - struct vsm_info v; + vsm_info v; if ((mask & (1u << i)) == 0) continue; @@ -1141,7 +2028,7 @@ static void run_oneshot(unsigned mask, bool want_sys, bool want_jobs, printed = true; } if (want_page) { - struct zpage p; + zpage p; if (!zpage_read(&p)) { (void)fprintf(stderr, "ztop: paging is only available on z/OS\n"); @@ -1154,7 +2041,7 @@ static void run_oneshot(unsigned mask, bool want_sys, bool want_jobs, } } if (want_sys || want_jobs) { - struct zsys z; + zsys z; if (gethostname(host, sizeof host - 1) != 0) (void)snprintf(host, sizeof host, "?"); @@ -1211,11 +2098,13 @@ int main(int argc, char **argv) { usage(stderr); return 2; } + } else if (strcmp(argv[i], "-c") == 0) { + console_opt = true; } else if (strcmp(argv[i], "-d") == 0) { interactive = true; if (i + 1 < argc && argv[i + 1][0] >= '0' && argv[i + 1][0] <= '9') { - long secs = strtol(argv[++i], NULL, 10); + long secs = strtol(argv[++i], nullptr, 10); interval = secs < 1 ? 1 : secs > 60 ? 60 : (int)secs; } |
