diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-27 18:10:43 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-27 18:10:43 -0600 |
| commit | bed906ea03f44454baa7089bb3cb975f94d77bcc (patch) | |
| tree | 1b2f1fb4a7be820454792832df76c17f1da8069a /src | |
| parent | 327bb5eef966394d487046d3bfe2420a66d50a99 (diff) | |
| download | ztop-bed906ea03f44454baa7089bb3cb975f94d77bcc.tar.gz | |
views: PS, FS and LOG, and a DEC-graphics frame
Two changes that share files -- the views, and the screen they are drawn
on -- so one commit; splitting them would mean splitting src/ztop.c and
tui/tui.h by hunk, which is not a split a reviewer could follow.
The views. Seven tabs instead of four: PS (w_getpsent), FS (w_getmntent
plus statvfs) and LOG, a read-only extended MCS console behind -c. LOG
needs supervisor state, so MCSOPER and MCSOPMSG live in src/zmcs.s: it
switches to supervisor state, calls the macro, switches back, and hands
the caller a copy of the message data block. src/mdb.c walks that MDB
(IEAVM105) for the job name and the text line, and is separate from
src/ztop.c because it is the one place ztop parses bytes written by
another address space, so it is worth a test that runs anywhere. The
small structs become C23 typedefs, src/vsm.c keeps the 16M line apart,
and README lists the seven views and the keys they moved.
The screen. The frame, the meters and the scrollbar were the last parts
that said "curses". Borders and meters are now the DEC Special Graphics
set, designated into G0 with ESC ( 0 and released with ESC ( B, with a
fallback to + - | # when TERM is not one we know; the meters are shade
fill over a horizontal rule instead of a [#### ] bar; table selection
is a background colour instead of reverse video, which is the tell almost
everyone recognises as curses; the tab row separates with a gap instead
of |; titles are a dim style and the frame is grey so the accent colour
means selection. The designation is tracked per cell, because an SGR
reset does not leave the graphics set, and only the cells that carry a
border glyph pay for it -- six bytes a row at 80 columns. Everything
stays 7-bit ASCII in the source, which is what .gitattributes now
enforces for the whole tree: one rule, ISO8859-1 in the working tree,
the same line the zopen git port uses, so a checkout on z/OS is tagged
ASCII for make, sh, awk and the compiler and no file needs a hand tag.
Verified on Linux: make test TESTCC=cc green (test_vsm, test_sys,
test_mdb, tui/test_tui, leakcheck), make tidy adds no finding to the ten
that predate this, src/ztop.c passes -fsyntax-only with the project's
warnings. The frame was checked against a painted screen with
tests/screen.py, which now understands ESC ( 0. Not yet built on z/OS:
the CBT build, make check and make leakcheck run after the push and the
PR carries their result.
Diffstat (limited to 'src')
| -rw-r--r-- | src/mdb.c | 50 | ||||
| -rw-r--r-- | src/mdb.h | 37 | ||||
| -rw-r--r-- | src/sys.c | 8 | ||||
| -rw-r--r-- | src/sys.h | 29 | ||||
| -rw-r--r-- | src/uss.c | 172 | ||||
| -rw-r--r-- | src/uss.h | 61 | ||||
| -rw-r--r-- | src/vsm.c | 53 | ||||
| -rw-r--r-- | src/vsm.h | 36 | ||||
| -rw-r--r-- | src/zmcs.c | 22 | ||||
| -rw-r--r-- | src/zmcs.h | 73 | ||||
| -rw-r--r-- | src/zmcs.s | 185 | ||||
| -rw-r--r-- | src/ztop.c | 1433 |
12 files changed, 1827 insertions, 332 deletions
diff --git a/src/mdb.c b/src/mdb.c new file mode 100644 index 0000000..4deb4a5 --- /dev/null +++ b/src/mdb.c @@ -0,0 +1,50 @@ +/** @file mdb.c + * @brief The MDB object walk. See src/mdb.h. + */ +#include "mdb.h" + +#include <string.h> + +/** Copy @p n bytes, stop at the first blank or NUL, terminate. */ +static void trim_copy(char *dst, size_t len, const char *src, int n) { + int i = 0; + + if (len == 0) + return; + while (i < n && src[i] != '\0' && src[i] != ' ') + i++; + if ((size_t)i >= len) + i = (int)len - 1; + (void)memcpy(dst, src, (size_t)i); + dst[i] = '\0'; +} + +bool mdb_line(const char *mdb, int len, char *job, size_t joblen, char *text, + size_t textlen) { + const unsigned char *m = (const unsigned char *)mdb; + int p; + int found = 0; + + if (joblen > 0) + job[0] = '\0'; + if (textlen > 0) + text[0] = '\0'; + if (mdb == nullptr || len <= 0) + return false; + /* The header is 12 bytes; the objects follow it. */ + for (p = 12; p + 4 <= len;) { + int olen = (m[p] << 8) | m[p + 1]; + int otype = (m[p + 2] << 8) | m[p + 3]; + + if (olen < 4 || p + olen > len) + break; /* a length that does not fit is not a length */ + if (otype == 1 && olen >= 56) { + trim_copy(job, joblen, (const char *)m + p + 48, 8); + } else if (otype == 4 && olen > 9 && found == 0) { + trim_copy(text, textlen, (const char *)m + p + 9, olen - 9); + found = 1; + } + p += olen; + } + return found != 0; +} diff --git a/src/mdb.h b/src/mdb.h new file mode 100644 index 0000000..58ba606 --- /dev/null +++ b/src/mdb.h @@ -0,0 +1,37 @@ +/** @file mdb.h + * @brief Pull a job name and a line of text out of a message data block. + * + * An MDB (IEAVM105) is a chain of objects, each two bytes of length + * followed by two of type. The general object (type 1) names the job + * that wrote the message at +48; a text object (type 4) holds its line at + * +9. The walk is here rather than in src/ztop.c because this is the one + * place ztop parses bytes that came from another address space, and it is + * the part worth a test that runs anywhere. + */ +#ifndef ZTOP_MDB_H +#define ZTOP_MDB_H + +#include <stddef.h> + +/** Largest text line kept: a console line is at most 132 columns. */ +enum { MDB_TEXT_MAX = 200 }; + +/** + * @brief Extract the job name and the first text object of an MDB copy. + * + * @param mdb the block as zmcs() copied it out of the console's space + * @param len how many bytes of it are valid + * @param job out: job name, blank padded or empty; never left unterminated + * @param joblen size of @p job + * @param text out: the line, trimmed; empty when there is none + * @param textlen size of @p text + * @return true when a text object was found + * + * Every length read is checked against @p len before it is used: the + * block was written by someone else, so a length in it is input, not a + * fact. + */ +bool mdb_line(const char *mdb, int len, char *job, size_t joblen, char *text, + size_t textlen); + +#endif /* ZTOP_MDB_H */ @@ -60,7 +60,7 @@ static unsigned int nib(uint32_t v, unsigned int shift) { return (v >> shift) & 0xFu; } -bool zipl_decode(uint32_t date, uint32_t hundredths, struct zipl *out) { +bool zipl_decode(uint32_t date, uint32_t hundredths, zipl *out) { static const unsigned int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, }; @@ -96,7 +96,7 @@ bool zipl_decode(uint32_t date, uint32_t hundredths, struct zipl *out) { return true; } -bool zsys_read(struct zsys *z) { +bool zsys_read(zsys *z) { #ifdef __MVS__ uintptr_t cvt, asvt; uint32_t maxu, entry; @@ -112,7 +112,7 @@ bool zsys_read(struct zsys *z) { maxu = rd32(asvt + 0x204); z->slots = maxu > 0x10000 ? 0x10000 : maxu; for (i = 0; i < z->slots && z->njobs < ZJOB_MAX; i++) { - struct zjob *j = &z->job[z->njobs]; + zjob *j = &z->job[z->njobs]; uint32_t namep; entry = rd32(asvt + 0x210 + (uintptr_t)i * 4); @@ -140,7 +140,7 @@ bool zsys_read(struct zsys *z) { #endif } -bool zpage_read(struct zpage *p) { +bool zpage_read(zpage *p) { #ifdef __MVS__ uintptr_t cvt = rd32(0x10); uintptr_t asmvt; @@ -11,43 +11,42 @@ #ifndef ZTOP_SYS_H #define ZTOP_SYS_H -#include <stdbool.h> #include <stdint.h> -#define ZJOB_MAX 512 +enum { ZJOB_MAX = 512 }; /** @brief One address space. */ -struct zjob { +typedef struct zjob { unsigned int asid; uint32_t ascb; char name[9]; -}; +} zjob; /** @brief The IPL date and time, decoded from the SMCA. */ -struct zipl { +typedef struct zipl { unsigned int year; unsigned int month; unsigned int day; /**< day of month */ unsigned int hour; unsigned int minute; unsigned int second; -}; +} zipl; /** @brief What the system view shows. */ -struct zsys { +typedef struct zsys { char sysname[9]; char host[65]; char os[64]; char machine[32]; unsigned int slots; /**< ASVT entries, used or free */ int njobs; - struct zjob job[ZJOB_MAX]; + zjob job[ZJOB_MAX]; bool ipl_ok; - struct zipl ipl; + zipl ipl; uint64_t real_k; /**< real storage online at IPL, in K */ uint32_t cvt; uint32_t smca; -}; +} zsys; /** * @brief Decode SMCAIDTE (0CYYDDDF, packed) and SMCAITME (hundredths). @@ -56,17 +55,17 @@ struct zsys { * @param out Where to write. * @return false when the fields do not describe a real date and time. */ -bool zipl_decode(uint32_t date, uint32_t hundredths, struct zipl *out); +bool zipl_decode(uint32_t date, uint32_t hundredths, zipl *out); /** * @brief Fill @p z from the CVT, the SMCA and the ASVT. * @param z Where to write. * @return false when this is not z/OS, with @c z untouched. */ -bool zsys_read(struct zsys *z); +bool zsys_read(zsys *z); /** @brief Auxiliary storage state, from the ASMVT. */ -struct zpage { +typedef struct zpage { bool common_pds; /**< a common page data set exists */ bool common_full; bool plpa_pds; @@ -77,13 +76,13 @@ struct zpage { bool warm_start; unsigned int vio_ci; /**< control intervals in the VIO journal */ uint32_t asmvt; -}; +} zpage; /** * @brief Fill @p p from the ASMVT. * @param p Where to write. * @return false when this is not z/OS or there is no ASMVT. */ -bool zpage_read(struct zpage *p); +bool zpage_read(zpage *p); #endif diff --git a/src/uss.c b/src/uss.c new file mode 100644 index 0000000..2311c60 --- /dev/null +++ b/src/uss.c @@ -0,0 +1,172 @@ +/** @file uss.c + * @brief z/OS UNIX readers: mounted file systems and processes. + * + * w_getmntent() fills one contiguous header+entries buffer; statvfs() is + * then asked for the size and the read-only flag, so no mount-mode bit has + * to be decoded. w_getpsent() is a walk: token 0 is the first process and + * each call returns the token of the next one. + */ +#ifdef __MVS__ +#define _OPEN_SYS 1 /* sys/mntent.h, sys/ps.h */ +#define _OPEN_SYS_EXT 1 /* __errno2() */ +#endif + +#include "uss.h" + +#ifdef __MVS__ +#include <errno.h> +#include <sys/mntent.h> +#include <sys/ps.h> +#include <sys/statvfs.h> +#include <time.h> /* CLOCKS_PER_SEC */ +#endif + +#include <stdio.h> +#include <string.h> + +#ifdef __MVS__ +/** Copy at most @p n bytes of a name into @p dst, trimmed of blanks. */ +static void name_copy(char *dst, size_t len, const char *src, size_t n) { + size_t i = 0; + + while (i < n && src[i] != '\0' && src[i] != ' ') + i++; + if (i >= len) + i = len - 1; + memcpy(dst, src, i); + dst[i] = '\0'; +} + +/** One w_getmntent() buffer: header, then the entries the service fills. */ +static struct { + MNTE3H h; + W_MNTENT3 e[USS_MAX_FS]; +} mnt; + +bool uss_fs_read(uss_fs_list *out) { + int n, i, fit; + + memset(out, 0, sizeof *out); + memset(&mnt, 0, sizeof mnt); + memcpy(mnt.h.mnt3H_cbid, MNTE3H_ID, 4); + mnt.h.mnt3H_cblen = (int)sizeof mnt; + mnt.h.mnt3H_bodylen = (int)sizeof(W_MNTENT3); + + n = w_getmntent((char *)&mnt, (int)sizeof mnt); + if (n < 0) { + (void)snprintf(out->err, sizeof out->err, "w_getmntent: errno %d", + errno); + return false; + } + /* The header length is the buffer size on the way in and what the + * service wrote on the way out; either can be smaller than the + * entries it reports, and a short one must not become a negative + * row count. */ + fit = (int)((mnt.h.mnt3H_cblen - (int)sizeof(MNTE3H)) / + (int)sizeof(W_MNTENT3)); + if (n == 0) /* some levels report the count only here */ + n = fit; + if (fit < n) + n = fit; + if (n < 0) + n = 0; + out->truncated = n > USS_MAX_FS; + out->nfs = n > USS_MAX_FS ? USS_MAX_FS : n; + + for (i = 0; i < out->nfs; i++) { + const W_MNTENT3 *e = &mnt.e[i]; + uss_fs *f = &out->fs[i]; + struct statvfs v; + + name_copy(f->fsname, sizeof f->fsname, e->mnt3_fsname, + sizeof f->fsname); + name_copy(f->type, sizeof f->type, e->mnt3_fstname, sizeof f->type); + name_copy(f->mount, sizeof f->mount - 1, e->mnt3_mountpoint, + sizeof e->mnt3_mountpoint); + f->pct = -1; + if (statvfs(e->mnt3_mountpoint, &v) != 0) { + f->eno = errno; + continue; + } + f->ok = true; + f->rdonly = (v.f_flag & ST_RDONLY) != 0; + { + unsigned long unit = v.f_frsize ? v.f_frsize : v.f_bsize; + unsigned long long total = (unsigned long long)v.f_blocks * unit; + unsigned long long avail = (unsigned long long)v.f_bavail * unit; + + f->kb_total = total / 1024; + f->kb_free = avail / 1024; + f->pct = total ? (int)((total - avail) * 100 / total) : -1; + } + } + return true; +} + +bool uss_ps_read(uss_ps_list *out) { + int token = 0; + + memset(out, 0, sizeof *out); + for (;;) { + W_PSPROC ps; + char tty[32], path[128], cmd[128]; + int next; + + if (out->nproc >= USS_MAX_PS) { + out->truncated = true; + break; + } + memset(&ps, 0, sizeof ps); + memset(tty, 0, sizeof tty); + memset(path, 0, sizeof path); + memset(cmd, 0, sizeof cmd); + ps.ps_conttylen = (int)sizeof tty; + ps.ps_conttyptr = tty; + ps.ps_pathlen = (int)sizeof path; + ps.ps_pathptr = path; + ps.ps_cmdlen = (int)sizeof cmd; + ps.ps_cmdptr = cmd; + + next = w_getpsent(token, &ps, sizeof ps); + if (next == 0) /* no more processes this caller may see */ + break; + if (next < 0) { + (void)snprintf(out->err, sizeof out->err, "w_getpsent: errno %d", + errno); + return out->nproc > 0; + } + token = next; + + { + uss_proc *p = &out->proc[out->nproc]; + + p->pid = (int)ps.ps_pid; + p->ppid = (int)ps.ps_ppid; + p->uid = (int)ps.ps_euid; /* effective UID */ + p->state = ps.ps_state; + p->st = (char)(ps.ps_state & PS_STATECHAR); + p->swapped = (ps.ps_state & PS_SWAP) != 0; + p->cpu_ms = (unsigned long)((ps.ps_usertime + ps.ps_systime) * + 1000 / CLOCKS_PER_SEC); + name_copy(p->tty, sizeof p->tty, tty, sizeof tty); + name_copy(p->cmd, sizeof p->cmd, cmd, sizeof cmd); + } + out->nproc++; + } + return true; +} +#else /* !__MVS__ */ +bool uss_fs_read(uss_fs_list *out) { + memset(out, 0, sizeof *out); + (void)snprintf(out->err, sizeof out->err, + "mounted file systems are only available on z/OS"); + return false; +} + +bool uss_ps_read(uss_ps_list *out) { + memset(out, 0, sizeof *out); + (void)snprintf(out->err, sizeof out->err, + "process data is only available on z/OS"); + return false; +} +#endif /* __MVS__ */ diff --git a/src/uss.h b/src/uss.h new file mode 100644 index 0000000..00245f7 --- /dev/null +++ b/src/uss.h @@ -0,0 +1,61 @@ +/** @file uss.h + * @brief z/OS UNIX readers: processes and mounted file systems. + * + * Two fixed-capacity tables filled in place. Neither reader allocates, and + * on a host that is not z/OS both report the same message the CVT readers + * print, so the portable build still links and the view stays honest. + */ +#ifndef ZTOP_USS_H +#define ZTOP_USS_H + +#include <stdint.h> + +enum { + USS_MAX_FS = 64, /**< mounted file systems kept */ + USS_MAX_PS = 128, /**< processes kept */ +}; + +/** @brief One mounted file system, from w_getmntent() and statvfs(). */ +typedef struct uss_fs { + char fsname[45]; /**< aggregate data set name */ + char mount[128]; /**< mount point, trimmed */ + char type[9]; /**< file system type name */ + bool rdonly; /**< statvfs says ST_RDONLY */ + bool ok; /**< statvfs succeeded */ + uint64_t kb_total; + uint64_t kb_free; + int pct; /**< 0..100, or -1 when the size is unknown */ + int eno; /**< errno from a failed statvfs() */ +} uss_fs; + +typedef struct uss_fs_list { + uss_fs fs[USS_MAX_FS]; + int nfs; + bool truncated; + char err[160]; +} uss_fs_list; + +/** @brief One process, from w_getpsent(). */ +typedef struct uss_proc { + int pid; + int ppid; + int uid; + unsigned state; /**< raw ps_state: letter in low bits, flags above */ + char st; /**< the state letter, ps_state & PS_STATECHAR */ + bool swapped; + unsigned long cpu_ms; /**< user + system time in milliseconds */ + char tty[16]; + char cmd[64]; +} uss_proc; + +typedef struct uss_ps_list { + uss_proc proc[USS_MAX_PS]; + int nproc; + bool truncated; + char err[160]; +} uss_ps_list; + +bool uss_fs_read(uss_fs_list *out); +bool uss_ps_read(uss_ps_list *out); + +#endif @@ -8,19 +8,19 @@ #include <stdio.h> #include <string.h> -struct rd { +typedef struct rd { const unsigned char *p; unsigned len; unsigned off; bool bad; -}; +} rd; static uint32_t be32(const unsigned char *p) { return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | (uint32_t)p[3]; } -static uint32_t rd32(struct rd *r) { +static uint32_t rd32(rd *r) { if (r->bad || r->off > r->len || r->len - r->off < 4) { r->bad = true; return 0; @@ -30,7 +30,7 @@ static uint32_t rd32(struct rd *r) { return v; } -static unsigned rd8(struct rd *r) { +static unsigned rd8(rd *r) { if (r->bad || r->off >= r->len) { r->bad = true; return 0; @@ -38,7 +38,7 @@ static unsigned rd8(struct rd *r) { return r->p[r->off++]; } -static bool fail(struct vsm_info *v, const char *what, unsigned off) { +static bool fail(vsm_info *v, const char *what, unsigned off) { (void)snprintf(v->err, sizeof v->err, "%s at offset %u", what, off); return false; } @@ -46,13 +46,13 @@ static bool fail(struct vsm_info *v, const char *what, unsigned off) { /* Every block needs at least its 8-byte address/length pair. A count larger * than the remaining bytes can never be honest, and it would make us loop * for a long time on a corrupt or truncated data area. */ -static bool count_ok(const struct rd *r, uint32_t n) { +static bool count_ok(const rd *r, uint32_t n) { if (r->off > r->len) return false; return n <= (r->len - r->off) / 8; } -void vsm_reset(struct vsm_info *v, unsigned area, unsigned space) { +void vsm_reset(vsm_info *v, unsigned area, unsigned space) { memset(v, 0, sizeof *v); v->area = area; v->space = space; @@ -70,7 +70,7 @@ bool vsm_work_data(const unsigned char *work, unsigned wlen, dlen = be32(work + 8); if (dlen == 0) { - *data = NULL; + *data = nullptr; *len = 0; return true; } @@ -82,16 +82,15 @@ bool vsm_work_data(const unsigned char *work, unsigned wlen, return true; } -static struct vsm_subpool *sub_find(struct vsm_info *v, unsigned id, - uint32_t tcb) { +static vsm_subpool *sub_find(vsm_info *v, unsigned id, uint32_t tcb) { for (int i = 0; i < v->nsub; i++) if (v->sub[i].id == id && v->sub[i].tcb == tcb) return &v->sub[i]; if (v->nsub == VSM_MAX_SUB) { v->truncated = true; - return NULL; + return nullptr; } - struct vsm_subpool *s = &v->sub[v->nsub++]; + vsm_subpool *s = &v->sub[v->nsub++]; memset(s, 0, sizeof *s); s->id = id; s->tcb = tcb; @@ -101,8 +100,7 @@ static struct vsm_subpool *sub_find(struct vsm_info *v, unsigned id, /* One allocated block, plus its free descriptors when SPACE=FREE. * Collected into s when there is room, and into the area totals always. */ -static bool parse_block(struct rd *r, struct vsm_info *v, - struct vsm_subpool *s) { +static bool parse_block(rd *r, vsm_info *v, vsm_subpool *s) { unsigned off = r->off; uint32_t addr = rd32(r); uint32_t blen = rd32(r); @@ -136,6 +134,11 @@ static bool parse_block(struct rd *r, struct vsm_info *v, v->foot += blen; v->free += bfree; v->used += used; + /* A block is counted on the side of the line its first byte is on. */ + if (addr < VSM_LINE_16M) + v->below += used; + else + v->above += used; if (blen > v->largest) v->largest = blen; if (addr < v->lo) @@ -143,11 +146,15 @@ static bool parse_block(struct rd *r, struct vsm_info *v, if ((uint64_t)addr + blen > v->hi) v->hi = (uint32_t)((uint64_t)addr + blen); - if (s != NULL) { + if (s != nullptr) { s->blocks++; s->foot += blen; s->free += bfree; s->used += used; + if (addr < VSM_LINE_16M) + s->below += used; + else + s->above += used; if (blen > s->largest) s->largest = blen; if (addr < s->lo) @@ -158,7 +165,7 @@ static bool parse_block(struct rd *r, struct vsm_info *v, return true; } -static bool parse_alloc_section(struct rd *r, struct vsm_info *v) { +static bool parse_alloc_section(rd *r, vsm_info *v) { uint32_t nsub = rd32(r); if (r->bad) return fail(v, "subpool count past end of data", r->off - 4); @@ -192,8 +199,8 @@ static bool parse_alloc_section(struct rd *r, struct vsm_info *v) { return fail(v, "allocated-block count larger than the data area", r->off - 4); - struct vsm_subpool *s = sub_find(v, id, v->area == VSM_PVT ? tcb : 0); - if (s != NULL) { + vsm_subpool *s = sub_find(v, id, v->area == VSM_PVT ? tcb : 0); + if (s != nullptr) { s->key = flags & 0x0Fu; s->flags = flags; } @@ -204,9 +211,8 @@ static bool parse_alloc_section(struct rd *r, struct vsm_info *v) { return true; } -bool vsm_parse_alloc(struct vsm_info *v, const unsigned char *data, - unsigned len) { - struct rd r = {data, len, 0, false}; +bool vsm_parse_alloc(vsm_info *v, const unsigned char *data, unsigned len) { + rd r = {data, len, 0, false}; if (v->area == VSM_PVT) { uint32_t n = rd32(&r); @@ -229,9 +235,8 @@ bool vsm_parse_alloc(struct vsm_info *v, const unsigned char *data, return true; } -bool vsm_parse_unalloc(struct vsm_info *v, const unsigned char *data, - unsigned len) { - struct rd r = {data, len, 0, false}; +bool vsm_parse_unalloc(vsm_info *v, const unsigned char *data, unsigned len) { + rd r = {data, len, 0, false}; uint32_t nreg = rd32(&r); if (r.bad) @@ -10,7 +10,6 @@ #ifndef ZTOP_VSM_H #define ZTOP_VSM_H -#include <stdbool.h> #include <stdint.h> /** @brief Area codes, the same numbers the assembler stub takes. */ @@ -20,42 +19,49 @@ enum vsm_area { VSM_CSA = 1, VSM_SQA = 2, VSM_LSQA = 3, VSM_PVT = 4 }; enum vsm_space { VSM_FREE = 1, VSM_UNALLOC = 2 }; /** @brief Rows kept per area; PVT can hold several TCBs of subpools. */ -#define VSM_MAX_SUB 512 +enum { VSM_MAX_SUB = 512 }; /** @brief Region descriptors kept for display (CSA/ECSA/RUCSA, PVT regions). */ -#define VSM_MAX_REGION 16 +enum { VSM_MAX_REGION = 16 }; + +/** @brief The 16-megabyte line: below it are 24-bit addresses. */ +enum { VSM_LINE_16M = 0x01000000 }; /** @brief One subpool: allocated blocks, the free space in them, the real use. */ -struct vsm_subpool { +typedef struct vsm_subpool { unsigned id; /**< translated subpool number */ unsigned key; /**< storage key from the descriptor's flag byte */ unsigned flags; /**< the descriptor's flag byte */ uint32_t tcb; /**< owning TCB for PVT, zero for common areas */ uint64_t used; /**< block bytes minus free space inside the blocks */ + uint64_t below; /**< used bytes in blocks starting below the 16M line */ + uint64_t above; /**< used bytes in blocks starting at or above 16M */ uint64_t free; /**< free space inside allocated blocks */ uint64_t foot; /**< footprint: sum of allocated block lengths */ uint64_t largest; /**< largest allocated block */ uint32_t lo, hi; /**< address range of the allocated blocks */ unsigned blocks; /**< number of allocated blocks */ -}; +} vsm_subpool; /** @brief A region descriptor (an unallocated range, or a PVT region). */ -struct vsm_region { +typedef struct vsm_region { uint32_t addr; uint32_t len; -}; +} vsm_region; /** @brief Everything one area reports, accumulated over continuation calls. */ -struct vsm_info { +typedef struct vsm_info { unsigned area; /**< enum vsm_area */ unsigned space; /**< enum vsm_space of the last parse */ unsigned tcbs; /**< PVT: TCBs listed */ int nsub; - struct vsm_subpool sub[VSM_MAX_SUB]; + vsm_subpool sub[VSM_MAX_SUB]; unsigned blocks; /**< allocated blocks in the area */ uint64_t used; + uint64_t below; /**< used bytes in blocks starting below the 16M line */ + uint64_t above; /**< used bytes in blocks starting at or above 16M */ uint64_t free; uint64_t foot; uint64_t largest; @@ -65,15 +71,15 @@ struct vsm_info { uint64_t unalloc; /**< unallocated bytes (CSA/PVT only) */ uint64_t largest_unalloc; int nregion; - struct vsm_region region[VSM_MAX_REGION]; + vsm_region region[VSM_MAX_REGION]; bool truncated; /**< more subpools than VSM_MAX_SUB; totals still right */ bool unalloc_valid; /**< the UNALLOC request succeeded */ char err[160]; /**< why the last parse or service call failed */ -}; +} vsm_info; /** @brief Zero @p v and set the request it describes. */ -void vsm_reset(struct vsm_info *v, unsigned area, unsigned space); +void vsm_reset(vsm_info *v, unsigned area, unsigned space); /** * @brief Check the work-area header and locate the data area. @@ -95,11 +101,9 @@ bool vsm_work_data(const unsigned char *work, unsigned wlen, * @return false on a stream that does not match the documented layout, with * v::err set and the offset in it. */ -bool vsm_parse_alloc(struct vsm_info *v, const unsigned char *data, - unsigned len); +bool vsm_parse_alloc(vsm_info *v, const unsigned char *data, unsigned len); /** @brief Parse an unallocated-storage data area (CSA or PVT only). */ -bool vsm_parse_unalloc(struct vsm_info *v, const unsigned char *data, - unsigned len); +bool vsm_parse_unalloc(vsm_info *v, const unsigned char *data, unsigned len); #endif diff --git a/src/zmcs.c b/src/zmcs.c new file mode 100644 index 0000000..3473724 --- /dev/null +++ b/src/zmcs.c @@ -0,0 +1,22 @@ +/** + * @file zmcs.c + * @brief The portable half of the console reader: there is none. + * + * The console lives in src/zmcs.s, which is built only on z/OS. The + * tests and the portable build still link the symbol, so this file + * answers "no" instead of letting the link fail. It is deliberately + * the only thing here: a fake console would let a change look tested. + */ +#include "zmcs.h" + +#ifndef __MVS__ + +int zmcs(zmcs_req *req) { + req->rc = 8; + req->rsn = 0; + if (req->len != nullptr) + *req->len = 0; + return 0; +} + +#endif /* !__MVS__ */ diff --git a/src/zmcs.h b/src/zmcs.h new file mode 100644 index 0000000..4ad870c --- /dev/null +++ b/src/zmcs.h @@ -0,0 +1,73 @@ +/** + * @file zmcs.h + * @brief Extended MCS console: the one service that needs supervisor + * state. + * + * MCSOPER and MCSOPMSG both require supervisor state (SA22-7614-.., + * "MCSOPER - Environment": minimum authorization supervisor state and + * any PSW key), and MCSOPMSG also wants access-register mode, so the + * calls live in src/zmcs.s: it switches to + * supervisor state, calls the macro, switches back, and hands the + * caller a copy of the message data block (MDB, IEAVM105, 160 bytes + * plus text). Nothing else in ztop needs a privileged instruction. + * + * The alternative, an EMCS console opened through an authorized + * caller, is what this is: the program must be marked APF + * (`extattr +ap ./ztop`, Makefile target `auth`) and the user needs + * READ on MVS.MCSOPER.consname. + * + * The stub builds the MCSOPER/MODESET control blocks in its own CSECT, so + * it is a 31-bit module of its own: an assembler CSECT linked into an + * XPLINK program object is read-only, and the macros store into those + * blocks (measured on CBT, 2026-11-27). See the header of src/zmcs.s. + * + * The offsets in zmcs_req are the stub's interface and are checked in + * src/ztop.c. Keep the two in step. + */ +#ifndef ZTOP_ZMCS_H +#define ZTOP_ZMCS_H + +#include <stdint.h> + +/** Operations for zmcs_req.op. */ +enum zmcs_op { + ZMCS_ACTIVATE = 0, /**< open the console, fills consid */ + ZMCS_GETMSG = 1, /**< one message into buf, length in len */ + ZMCS_DEACTIVATE = 2, /**< close the console */ +}; + +/** + * @brief The request block the stub reads and writes. + * + * All pointers are 31-bit: the program is built -m32, so every object + * it owns is reachable from an AMODE 31 assembler routine. + */ +typedef struct zmcs_req { + int op; /**< 0: one of enum zmcs_op */ + char *name; /**< 4: 8 byte console name, blank padded */ + unsigned *consid; /**< 8: console id, out */ + char *buf; /**< 12: MDB copy destination */ + int *len; /**< 16: in capacity, out copied */ + int rc; /**< 20: service return code */ + int rsn; /**< 24: service reason code */ + unsigned char ecb[4]; /**< 28: MSGECB, posted on a message */ + unsigned alet; /**< 32: MCSCSAA: ALET, from the service */ + unsigned long long save[18]; /**< 40: the stub's save area */ + void *csa; /**< 184: MCSCSA: status area, from it */ +} zmcs_req; + +/** Largest MDB we copy out of the console's data space. */ +enum { ZMCS_MDB_MAX = 4096 }; + +#ifdef __MVS__ +/* HLASM upcases the ESD name of the CSECT, so the assembler side is + * exported as ZMCS whatever case the source is written in, and the C + * side has to ask for that name. The stub is not XPLINK, so the call + * uses the OS linkage convention, exactly like src/zvslist.S. */ +int zmcs(zmcs_req *req) __asm__("ZMCS"); +#pragma linkage(zmcs, OS) +#else +int zmcs(zmcs_req *req); +#endif + +#endif /* ZTOP_ZMCS_H */ diff --git a/src/zmcs.s b/src/zmcs.s new file mode 100644 index 0000000..c239c5f --- /dev/null +++ b/src/zmcs.s @@ -0,0 +1,185 @@ +* +* zmcs -- the extended MCS console. MCSOPER and MCSOPMSG both need +* supervisor state (SA22-7614 "Environment": supervisor state, any PSW +* key) and MCSOPMSG also wants access-register mode. +* +* The caller fills a zmcs_req (src/zmcs.h) and calls zmcs() with it. +* The offsets below are that block, -m32, asserted by the C side. +* +* Module rule: MODESET and MCSOPER build their control blocks here and +* store into them at run time, so this has to be a 31-bit module of its +* own, reached through a thunk. An assembler CSECT linked into an +* XPLINK program object is read-only and that store is a 0C4. +* +zmcs CSECT +zmcs AMODE 31 +zmcs RMODE 24 +* +* Request block offsets (src/zmcs.h). +ZR_OP EQU 0 int op +ZR_NAME EQU 4 char *name +ZR_CONS EQU 8 unsigned *consid, out +ZR_BUF EQU 12 char *buf +ZR_LEN EQU 16 int *len, in capacity, out count +ZR_RC EQU 20 int rc +ZR_RSN EQU 24 int rsn +ZR_ECB EQU 28 unsigned char ecb[4], MSGECB +ZR_ALET EQU 32 unsigned alet, out, MCSCSAA +ZR_SAVE EQU 40 save area, 18 fullwords +ZR_CSA EQU 184 void *csa, out, MCSCSA +* +* Operations (enum zmcs_op). +ZOP_ACT EQU 0 +ZOP_GET EQU 1 +ZOP_DEA EQU 2 +* + STM R14,R12,12(R13) + BALR R12,R0 + USING *,R12 + LAE R12,R0(R0,R12) AR12 = 0: our own storage + L R2,0(R1) R1 -> list, word 0 = the request + LTR R2,R2 + BZ BADREQ + LAE R11,R0(R0,R2) AR11 = 0 too + LA R10,ZR_SAVE(,R11) save area, inside the request + ST R13,4(,R10) the caller's, for a dump + ST R10,8(,R13) + LR R13,R10 + L R3,ZR_OP(,R11) + CHI R3,ZOP_ACT + BE DOACT + CHI R3,ZOP_GET + BE DOGET + CHI R3,ZOP_DEA + BE DODEACT + LA R3,4 unknown request + B BACK +* +* ACTIVATE, in primary space mode: MCSOPER does not need AR mode. +* MCSCSA and MCSCSAA are output areas: the service writes the address +* of the console status area and its ALET there. QLIMIT bounds the +* queue, so a console left open cannot grow until storage runs out. +* +DOACT DS 0H + MODESET MODE=SUP + LTR R15,R15 + BNZ NORIGHTS + L R5,ZR_NAME(,R11) NAME and TERMNAME + L R6,ZR_CONS(,R11) CONSID, out + L R4,ZR_CSA(,R11) MCSCSA, out + L R3,ZR_ALET(,R11) MCSCSAA, out + LHI R9,ZMCS_QLIMIT + MCSOPER REQUEST=ACTIVATE, X + NAME=(R5), X + TERMNAME=(R5), X + MSGDLVRY=FIFO, X + MSGECB=ZR_ECB(,R11), X + MCSCSA=(R4), X + MCSCSAA=(R3), X + CONSID=(R6), X + QLIMIT=(R9), X + RTNCODE=ZR_RC(,R11), X + RSNCODE=ZR_RSN(,R11) + MODESET MODE=PROB + B BACKRC +* +* GETMSG. The service wants AR mode and hands back the MDB address in +* R1 with its ALET in AR1. The MDB lies in the console's data space, +* so it is copied out here, still in AR mode. RC 8 is "nothing +* queued", the steady state. +* +DOGET DS 0H + SAC 0 primary mode for MODESET + MODESET MODE=SUP + LTR R15,R15 + BNZ NORIGHTS + SAC 512 AR mode for MCSOPMSG + SYSSTATE ASCENV=AR + L R6,ZR_CONS(,R11) + MCSOPMSG REQUEST=GETMSG, X + CONSID=(R6), X + RTNCODE=ZR_RC(,R11), X + RSNCODE=ZR_RSN(,R11) + CHI R15,0 + BE DOCOPY + CHI R15,ZMCS_RC_QUEUED + BNE GETEND +DOCOPY DS 0H + LAE R8,0(R0,R1) R8 = MDB, AR8 = the MDB's ALET + LH R4,0(,R8) MDBLEN, the first halfword + L R5,ZR_LEN(,R11) capacity the caller offered + CR R4,R5 + BNH COPYOK + LR R4,R5 clip +COPYOK DS 0H + ST R4,ZR_LEN(,R11) length copied + L R3,ZR_BUF(,R11) + LAE R3,0(R0,R3) AR3 = 0: our own space + LTR R4,R4 + BZ GETEND +CLOOP DS 0H + MVC 0(1,R3),0(R8) + LA R3,1(,R3) + LA R8,1(,R8) + BCT R4,CLOOP +GETEND DS 0H + SAC 0 primary mode for MODESET + SYSSTATE ASCENV=P + MODESET MODE=PROB + B BACKRC +* +* DEACTIVATE: hand the console id back. +* +DODEACT DS 0H + MODESET MODE=SUP + LTR R15,R15 + BNZ NORIGHTS + L R6,ZR_CONS(,R11) + MCSOPER REQUEST=DEACTIVATE, X + CONSID=(R6), X + RTNCODE=ZR_RC(,R11), X + RSNCODE=ZR_RSN(,R11) + MODESET MODE=PROB + B BACKRC +* +* Returns. NORIGHTS is the MODESET refusal: an unauthorized program +* never gets here, the restricted SVC ends the task with S047. +* +NORIGHTS DS 0H + ST R15,ZR_RSN(,R11) the MODESET reason + LA R3,12 + ST R3,ZR_RC(,R11) + B BACK +BADREQ DS 0H + LA R3,4 + B BACK +BACKRC DS 0H + L R3,ZR_RC(,R11) +BACK DS 0H + LR R2,R11 + L R13,4(,R13) + LM R14,R12,12(R13) + BR R14 +* +* Constants. +ZMCS_QLIMIT EQU 2000 messages the console may queue +ZMCS_RC_QUEUED EQU 4 message returned, queueing suspended +* +* Registers, by name, so a number is never read as a register. +R0 EQU 0 +R1 EQU 1 +R2 EQU 2 +R3 EQU 3 +R4 EQU 4 +R5 EQU 5 +R6 EQU 6 +R7 EQU 7 +R8 EQU 8 +R9 EQU 9 +R10 EQU 10 +R11 EQU 11 +R12 EQU 12 +R13 EQU 13 +R14 EQU 14 +R15 EQU 15 + END @@ -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,29 @@ #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"); +static_assert(sizeof(zmcs_req) == 188, "zmcs_req: runs into the next object"); +#endif #if defined(__MVS__) /* HLASM upcases the ESD name of the CSECT, so the assembler side is @@ -32,7 +50,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 +58,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 +97,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 +116,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 +152,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 +161,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 +183,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 +198,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 +227,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 +268,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 +279,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 +288,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 +297,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 +325,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 +380,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 +522,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 +552,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 +570,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 +673,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 +690,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 +711,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 +747,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 +842,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 +960,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 +986,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 +1090,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 +1130,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 +1161,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 +1625,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 +1744,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 +1809,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 +1823,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 +1877,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 +1901,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 +1940,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 +1965,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 +1981,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 +1999,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 +2011,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 +2026,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 +2039,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 +2096,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; } |
