summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-25 20:36:47 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-25 20:36:47 -0600
commit8f2578664a1420759f2a0e29e27d128a7d3e2567 (patch)
tree8ae3e40922dd2921b6a82d441d2345a8a7c35522
parentddeb7f2effb7f7a3ff3b183a741121e21e409f45 (diff)
downloadztop-8f2578664a1420759f2a0e29e27d128a7d3e2567.tar.gz
ztop: parse the VSMLIST answer, add a full-screen view
The old parser scanned from offset 4, which is the data-area pointer, not the data. Read the documented framing instead: the work area's first word is the service return code, then the address and length of the data area. Continuation (rc=4) reuses the same work area and must not clear that word, so the request build now happens once and each piece is merged. Parsing follows the reference: subpool descriptors, allocated blocks with their free descriptors (used = block length - free), unallocated regions and holes for CSA and PVT. CSA gets a total and a percentage; the other areas do not have that request. The display is a small cell-buffer TUI: diffed output, ASCII box drawing, termios raw input, CPR sizing because TIOCGWINSZ reports zero on z/OS, and EBCDIC ESC on __MVS__. One-shot output stays exact bytes for grep. tests/test_vsm.c builds synthetic data areas for the parser and runs on any host; tests/demo_tui.c exercises the terminal layer.
-rw-r--r--.gitignore8
-rw-r--r--DESIGN.md77
-rw-r--r--Makefile24
-rw-r--r--README.md72
-rw-r--r--src/tui.c552
-rw-r--r--src/tui.h93
-rw-r--r--src/vsm.c278
-rw-r--r--src/vsm.h102
-rw-r--r--src/ztop.c816
-rw-r--r--src/zvslist.s34
-rw-r--r--tests/demo_tui.c49
-rw-r--r--tests/test_vsm.c229
12 files changed, 2165 insertions, 169 deletions
diff --git a/.gitignore b/.gitignore
index 2262c85..a91913a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
-/ztop
-*.o
-*.lst
-CL*.txt
+ztop
+src/*.o
+tests/test_vsm
+tests/demo_tui
diff --git a/DESIGN.md b/DESIGN.md
index 590926f..5bbac35 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -86,18 +86,69 @@ C23 for every project from here on: `-std=c23`, `constexpr`, `bool`,
`nullptr`, `__attribute__((aligned))` where it matters. IBM Open XL 2.2
(clang 21) accepts `-std=c23`.
-## Open
+## The shape of the answer
+
+`VSMLIST` fills a caller-supplied work area. The first 32 bytes are a
+control block: return code at 0, then the address (4) and length (8) of a
+data area placed after it. The data area is in MVS byte order, so
+`src/vsm.c` reads it big-endian and never casts.
+
+The stream is a count followed by fixed 8-byte address/length pairs, at
+every level: number of subpools, then per subpool an 8-byte descriptor
+(format, descriptor length, subpool id, flags, owning TCB in the private
+area), then a block count and the blocks. With `SPACE=FREE` each allocated
+block is followed by a free-descriptor count and the free extents inside
+it; allocated bytes are the block length minus those free extents. With
+`SPACE=UNALLOC` the stream is regions, and inside each region the holes.
+
+The call is a continuation: return code 4 means "did not fit, issue the
+same call with the same work area again", and each call reports only its
+own piece. The return code at work+0 must be zero before the first call
+and left untouched between the continuation calls; clearing it each time
+restarts the request and never finishes. `src/vsm.c` therefore merges the
+pieces until the service reports zero, and `ztop -x` dumps the header and
+the first bytes of each piece.
+
+What `VSMLIST` does not return is the area size. CSA can still be shown as
+a percentage because the unallocated request returns the holes, so
+footprint plus holes is the mapped extent; SQA, LSQA and the private area
+have no such request and show used, free and mapped without a total.
-- The work-area layout. The reference describes the descriptor stream in
- figures whose byte tables do not survive the documentation conversion, so
- `src/ztop.c` walks it with a plausibility check and `-x` dumps the raw
- bytes. Next: confirm the framing against a dump on a live system, then
- replace the guess with the real rule.
+## The terminal
+
+A cell buffer and a diff flush, not curses. ncurses brings its own
+terminfo database, which is another thing to install and keep in step on
+every z/OS system, and its output is a stream of absolute cursor motion
+anyway. `src/tui.c` keeps one screen of cells and one of what is already
+on the glass, and writes only the runs that differ, with one style change
+per run. No Unicode: the box and the bar are `+`, `-`, `|`, `#`, because
+the terminal path is an EBCDIC-to-ASCII conversion that is not a charset
+translation.
+
+Three z/OS details are load-bearing:
+
+- The escape character in the source has to be EBCDIC X'27'. The tty
+ converts EBCDIC to ASCII on output, so a C `"\x1b"` arrives as X'8F';
+ X'27' arrives as the ESC the terminal expects. `src/tui.c` picks the
+ byte with `#ifdef __MVS__`; input is not converted, so keys arrive as
+ EBCDIC and are mapped there too.
+- `TIOCGWINSZ` succeeds with zero columns and rows. The size comes from a
+ cursor-position report (`ESC[6n`, answer `ESC[row;colR`) with a timeout,
+ then `COLUMNS`/`LINES`, then 80x24. `SIGWINCH` is handled where the
+ system sends it.
+- `-D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200112L` is what exposes termios,
+ `select` and `clock_gettime` in the z/OS headers; `nanosleep` and `usleep`
+ are not declared, so the refresh wait is `select`.
+
+## Open
- shortcut: the stream is scanned for a X'00' format byte, a sane length and
- a subpool id; a mis-parse shows up as an impossible subpool or a wild byte
- count, and `-x` is the way to see it.
-- A refresh loop (`-d`, `-n`) and a `%`-of-area column need the area sizes,
- which VSMLIST does not return (it reports allocation, not the limit).
-- `SP=PVT` output is grouped by owning TCB, which needs a different
- walk from the per-area one.
+- Live confirmation. The parser follows the documented layout and is
+ covered by synthetic data areas in `tests/test_vsm.c`, but no real
+ `VSMLIST` answer has been parsed yet: the executable is APF-marked and
+ still abends SC78 R=90 because no HFS directory is APF-listed. The
+ first run on an authorized system is the acceptance test for the framing.
+- Area sizes for SQA, LSQA and the private area. `VSMLIST` reports
+ allocation and free space, not the limit; a percentage there needs a
+ different source, and none is planned.
+- `SP=PVT` output is already grouped by owning TCB, but the view still
+ prints one list; per-TCB folding is a display change, not a data change.
diff --git a/Makefile b/Makefile
index 4009fda..c1490a8 100644
--- a/Makefile
+++ b/Makefile
@@ -21,21 +21,29 @@
CC = ibm-clang
AS = as
-CFLAGS = -std=c23 -O2 -m32 -Wall -Wextra -Wshadow -Wpedantic
+CFLAGS = -std=c23 -O2 -m32 -Wall -Wextra -Wshadow -Wpedantic \
+ -D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200112L
+# The parser and the TUI framework have no z/OS dependency; the test and
+# demo targets build them on the host so a Linux tree can check them.
+TESTCC = cc
+TESTFLAGS = -std=c17 -g -O1 -Wall -Wextra -Wshadow -Wpedantic \
+ -D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200112L
# AC(1) is half of what makes an executable APF-authorized, the other
# half being the APF bit on the file (make apf). Quoted: the z/OS shell
# reads `(` in an unquoted word as a syntax error.
LDFLAGS = -Wl,'-bAC(1)'
OBJ = src/zvslist.o
+SRC = src/ztop.c src/vsm.c src/tui.c
+HDR = src/vsm.h src/tui.h
all:
@(uname -s | grep OS/390 > /dev/null) || \
{ echo "ztop builds only on z/OS; this tree is a backup copy"; exit 1; }
$(MAKE) ztop
-ztop: src/ztop.c $(OBJ)
- $(CC) $(CFLAGS) $(LDFLAGS) -o $@ src/ztop.c $(OBJ)
+ztop: $(SRC) $(HDR) $(OBJ)
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(SRC) $(OBJ)
# The assembler reads HFS source through the file's coded character set
# tag; a git checkout arrives tagged UTF-8 and HLASM then reads the file
@@ -59,5 +67,13 @@ check: ztop
./ztop -a sqa
./ztop -a pvt
+# Portable checks: no VSMLIST, no terminal.
+test: tests/test_vsm.c src/vsm.c src/vsm.h
+ $(TESTCC) $(TESTFLAGS) -o tests/test_vsm tests/test_vsm.c src/vsm.c
+ ./tests/test_vsm
+
+demo: tests/demo_tui.c src/tui.c src/tui.h
+ $(TESTCC) $(TESTFLAGS) -o tests/demo_tui tests/demo_tui.c src/tui.c
+
clean:
- rm -f ztop $(OBJ)
+ rm -f ztop $(OBJ) tests/test_vsm tests/demo_tui
diff --git a/README.md b/README.md
index f7dae65..d90cccc 100644
--- a/README.md
+++ b/README.md
@@ -1,35 +1,67 @@
# ztop
-How much storage is in each area of a z/OS system, from a z/OS UNIX shell.
+How much storage is in each area of a z/OS system -- CSA, SQA, LSQA, and
+the private area -- from a z/OS UNIX shell over SSH.
-```
-subpool bytes blocks
-227 1048576 12
-231 16777216 41
-total 17825792
+```text
+subpool key blocks used free mapped largest address
+227 8 12 1048576 196608 1245184 1048576 00A00000-00B30000
+231 8 41 16777216 2097152 18874368 4194304 00C00000-02C00000
+CSF total: 17825792 bytes, 17121280 mapped (96.2% of mapped+unallocated)
```
-The numbers come from `VSMLIST`, the interface z/OS offers an ordinary
-user task (problem state, key 8, `LINKAGE=SYSTEM`) for exactly this. It
-is an assembler macro with no C equivalent, so `src/zvslist.s` issues it
-and `src/ztop.c` parses and formats the answer.
+The numbers come from `VSMLIST`, the interface z/OS offers an authorized
+program for exactly this. It is an assembler macro with no C equivalent, so
+`src/zvslist.s` issues it and `src/vsm.c` parses the answer; `src/tui.c` is
+the terminal layer and `src/ztop.c` is the command.
## Build
-z/OS only, with IBM Open XL and HLASM in the path:
+z/OS only for the real binary, with IBM Open XL and HLASM in the path:
```sh
make # ibm-clang -std=c23 -m32, plus `as` for src/zvslist.s
-./ztop # CSA
-./ztop -a sqa # SQA, -a lsqa, -a pvt, -x dumps the raw work area
+make apf # then give the executable the APF bit
+```
+
+The parser and the TUI framework have no z/OS dependency, so on any host
+with a C compiler and a terminal:
+
+```sh
+make test # synthetic VSMLIST data areas, no z/OS needed
+make demo # a small exercise of the TUI framework
```
-On Linux `make` stops with a message: this tree is kept there as a backup
-copy, not to build. Keep the two in step with `git` -- one repository,
-cloned in `~/src/ztop` on both machines.
+## Use
+
+```text
+ztop one-shot CSA
+ztop -a sqa SQA, -a lsqa, -a pvt
+ztop -a csa,pvt several areas
+ztop -x dump the raw work area of the first call
+ztop -d [seconds] interactive, 2 s refresh (1..60)
+```
+
+Without `-d` the exact bytes of the answer go to standard output, suitable
+for `grep` and for a shell prompt. With `-d` there is a full-screen view:
+
+| key | |
+| --- | --- |
+| `1`..`4` | CSA, SQA, LSQA, private |
+| `a` | all four areas |
+| `p` | pause/resume refresh |
+| `r` | collect now |
+| `+`/`-` | refresh 1..60 s |
+| `j`/`k`, `PgUp`/`PgDn`, `g`/`G` | scroll a subpool list |
+| `?` | help |
+| `q` | quit |
+
+The design, including why the data source is what it is and what is not
+verified on z/OS, is in [DESIGN.md](DESIGN.md).
-## What it is not yet
+## What it is not
-A snapshot, not a monitor: no refresh loop, no `%`-of-limit column, no
-per-TCB private breakdown. [DESIGN.md](DESIGN.md) says what comes next and
-why the data source is what it is.
+A snapshot, not an accounting tool: no history, no per-job attribution, no
+SMF. CSA has a total because `VSMLIST` returns the unallocated holes as
+well; SQA, LSQA and the private area do not, so those views show
+used/free/mapped and no percentage.
diff --git a/src/tui.c b/src/tui.c
new file mode 100644
index 0000000..25ac085
--- /dev/null
+++ b/src/tui.c
@@ -0,0 +1,552 @@
+/**
+ * @file tui.c
+ * @brief See tui.h. One output buffer per frame, one diff against the last.
+ */
+#include "tui.h"
+
+#include <errno.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/select.h>
+#include <unistd.h>
+
+#ifdef __MVS__
+#define ESCS "\x27" /* EBCDIC ESC; the tty converts it to 0x1b */
+#define ESC_CHAR 0x27u
+#else
+#define ESCS "\x1b"
+#define ESC_CHAR 0x1bu
+#endif
+
+#define ALT_ON ESCS "[?1049h"
+#define ALT_OFF ESCS "[?1049l"
+#define CUR_OFF ESCS "[?25l"
+#define CUR_ON ESCS "[?25h"
+#define CLEAR ESCS "[H" ESCS "[2J"
+#define RESET ESCS "[0m"
+
+static struct tui *g_tui;
+static volatile sig_atomic_t g_winch;
+
+static void wr(int fd, const void *buf, size_t n)
+{
+ const char *p = buf;
+
+ while (n > 0) {
+ ssize_t k = write(fd, p, n);
+ if (k < 0 && errno == EINTR)
+ continue;
+ if (k <= 0)
+ return;
+ p += (size_t)k;
+ n -= (size_t)k;
+ }
+}
+
+static void restore(struct tui *t)
+{
+ if (!t->open)
+ return;
+ wr(t->fd_out, RESET ALT_OFF, strlen(RESET ALT_OFF));
+ (void)tcsetattr(t->fd_in, TCSANOW, &t->orig);
+}
+
+static void on_winch(int sig)
+{
+ (void)sig;
+ g_winch = 1;
+}
+
+static void on_term(int sig)
+{
+ if (g_tui != NULL)
+ restore(g_tui);
+ _exit(128 + sig);
+}
+
+static void out_flush(struct tui *t)
+{
+ if (t->olen > 0) {
+ wr(t->fd_out, t->out, (size_t)t->olen);
+ t->olen = 0;
+ }
+}
+
+static void out(struct tui *t, const char *s, size_t n)
+{
+ if (t->olen + (int)n > (int)sizeof t->out)
+ out_flush(t);
+ if (n > sizeof t->out) {
+ wr(t->fd_out, s, n);
+ return;
+ }
+ memcpy(t->out + t->olen, s, n);
+ t->olen += (int)n;
+}
+
+static void outs(struct tui *t, const char *s)
+{
+ out(t, s, strlen(s));
+}
+
+static void outf(struct tui *t, const char *fmt, ...)
+{
+ char b[64];
+ va_list ap;
+ int n;
+
+ va_start(ap, fmt);
+ n = vsnprintf(b, sizeof b, fmt, ap);
+ va_end(ap);
+ if (n > 0)
+ out(t, b, (size_t)n < sizeof b ? (size_t)n : sizeof b - 1);
+}
+
+static void sgr(struct tui *t, int fg, int bg, int attr)
+{
+ char b[40];
+ int n = snprintf(b, sizeof b, "%s[0", ESCS);
+
+ if (attr & TUI_BOLD)
+ n += snprintf(b + n, sizeof b - (size_t)n, ";1");
+ if (attr & TUI_DIM)
+ n += snprintf(b + n, sizeof b - (size_t)n, ";2");
+ if (attr & TUI_UNDERLINE)
+ n += snprintf(b + n, sizeof b - (size_t)n, ";4");
+ if (attr & TUI_REVERSE)
+ n += snprintf(b + n, sizeof b - (size_t)n, ";7");
+ n += snprintf(b + n, sizeof b - (size_t)n, ";3%d;4%dm", fg, bg);
+ out(t, b, (size_t)n);
+}
+
+/* Ask the terminal where the cursor is; its answer is the only size z/OS
+ * gives us. Returns false if the terminal does not answer. */
+static bool cpr_size(int in, int outfd, int *w, int *h)
+{
+ unsigned char b[64];
+ int n = 0;
+ bool got = false, esc = false, csi = false;
+ int vals[2] = { 0, 0 }, nv = 0, v = 0;
+
+ wr(outfd, ESCS "[6n", sizeof ESCS "[6n" - 1);
+ for (int rounds = 0; rounds < 30 && !got; rounds++) {
+ fd_set f;
+ struct timeval tv = { 0, 10000 };
+
+ FD_ZERO(&f);
+ FD_SET(in, &f);
+ int r = select(in + 1, &f, NULL, NULL, &tv);
+ if (r < 0 && errno == EINTR)
+ continue;
+ if (r <= 0)
+ continue;
+ ssize_t k = read(in, b + n, sizeof b - (size_t)n);
+ if (k <= 0)
+ continue;
+ for (ssize_t i = 0; i < k; i++) {
+ unsigned char c = b[n + (size_t)i];
+ if (!esc) {
+ if (c == ESC_CHAR)
+ esc = true;
+ continue;
+ }
+ if (!csi) {
+ if (c == '[')
+ csi = true;
+ else if (c != ESC_CHAR)
+ esc = false;
+ continue;
+ }
+ if (c >= '0' && c <= '9') {
+ v = v * 10 + (c - '0');
+ } else if (c == ';' && nv == 0) {
+ vals[nv++] = v;
+ v = 0;
+ } else if (c == 'R') {
+ vals[nv++] = v;
+ got = true;
+ break;
+ } else {
+ esc = csi = false;
+ nv = v = 0;
+ }
+ }
+ n = 0;
+ }
+ if (!got || nv < 2 || vals[0] <= 0 || vals[1] <= 0)
+ return false;
+ *h = vals[0];
+ *w = vals[1];
+ return true;
+}
+
+static void probe_size(struct tui *t, int *w, int *h)
+{
+ struct winsize ws;
+
+ *w = 80;
+ *h = 24;
+ if (ioctl(t->fd_out, TIOCGWINSZ, &ws) == 0 &&
+ ws.ws_col > 0 && ws.ws_row > 0) {
+ *w = ws.ws_col;
+ *h = ws.ws_row;
+ } else if (!cpr_size(t->fd_in, t->fd_out, w, h)) {
+ const char *c = getenv("COLUMNS");
+ const char *l = getenv("LINES");
+ if (c != NULL && atoi(c) > 0)
+ *w = atoi(c);
+ if (l != NULL && atoi(l) > 0)
+ *h = atoi(l);
+ }
+ if (*w > TUI_MAX_W)
+ *w = TUI_MAX_W;
+ if (*h > TUI_MAX_H)
+ *h = TUI_MAX_H;
+ if (*w < 20)
+ *w = 80;
+ if (*h < 5)
+ *h = 24;
+}
+
+bool tui_resize(struct tui *t)
+{
+ int w, h;
+
+ probe_size(t, &w, &h);
+ if (w == t->w && h == t->h)
+ return false;
+
+ struct tui_cell *cur = calloc((size_t)w * (size_t)h, sizeof *cur);
+ struct tui_cell *old = calloc((size_t)w * (size_t)h, sizeof *old);
+ if (cur == NULL || old == NULL) {
+ free(cur);
+ free(old);
+ return false;
+ }
+ free(t->cur);
+ free(t->old);
+ t->cur = cur;
+ t->old = old;
+ t->w = w;
+ t->h = h;
+ t->full = true;
+ tui_clear(t);
+ return true;
+}
+
+bool tui_open(struct tui *t)
+{
+ struct termios r;
+
+ memset(t, 0, sizeof *t);
+ t->fd_in = 0;
+ t->fd_out = 1;
+ if (!isatty(t->fd_in) || !isatty(t->fd_out))
+ return false;
+ if (tcgetattr(t->fd_in, &t->orig) != 0)
+ return false;
+ r = t->orig;
+ r.c_lflag &= (tcflag_t)~(ICANON | ECHO | ISIG | IEXTEN);
+ r.c_iflag &= (tcflag_t)~(IXON | ICRNL | BRKINT | INPCK | ISTRIP);
+ r.c_oflag &= (tcflag_t)~OPOST;
+ r.c_cc[VMIN] = 0;
+ r.c_cc[VTIME] = 0;
+ if (tcsetattr(t->fd_in, TCSANOW, &r) != 0)
+ return false;
+
+ t->open = true;
+ g_tui = t;
+ g_winch = 0;
+ (void)signal(SIGTERM, on_term);
+ (void)signal(SIGINT, on_term);
+ (void)signal(SIGHUP, on_term);
+ (void)signal(SIGQUIT, on_term);
+#ifdef SIGWINCH
+ (void)signal(SIGWINCH, on_winch);
+#endif
+ outs(t, ALT_ON CUR_OFF CLEAR);
+ out_flush(t);
+ if (!tui_resize(t)) {
+ restore(t);
+ t->open = false;
+ g_tui = NULL;
+ return false;
+ }
+ return true;
+}
+
+void tui_close(struct tui *t)
+{
+ if (!t->open)
+ return;
+ restore(t);
+ t->open = false;
+ g_tui = NULL;
+ free(t->cur);
+ free(t->old);
+ t->cur = NULL;
+ t->old = NULL;
+}
+
+void tui_size(struct tui *t, int *w, int *h)
+{
+ *w = t->w;
+ *h = t->h;
+}
+
+void tui_clear(struct tui *t)
+{
+ int n = t->w * t->h;
+
+ for (int i = 0; i < n; i++) {
+ t->cur[i].ch = ' ';
+ t->cur[i].fg = 7;
+ t->cur[i].bg = 0;
+ t->cur[i].attr = 0;
+ }
+}
+
+void tui_fill(struct tui *t, int x, int y, int w, int h, char ch, tui_style st)
+{
+ if (x < 0) { w += x; x = 0; }
+ if (y < 0) { h += y; y = 0; }
+ if (x + w > t->w) w = t->w - x;
+ if (y + h > t->h) h = t->h - y;
+ if (w <= 0 || h <= 0)
+ return;
+ for (int j = y; j < y + h; j++) {
+ struct tui_cell *row = t->cur + (size_t)j * (size_t)t->w;
+ for (int i = x; i < x + w; i++) {
+ row[i].ch = (unsigned char)ch;
+ row[i].fg = st.fg;
+ row[i].bg = st.bg;
+ row[i].attr = st.attr;
+ }
+ }
+}
+
+void tui_put(struct tui *t, int x, int y, const char *s, int maxw, tui_style st)
+{
+ if (y < 0 || y >= t->h)
+ return;
+ struct tui_cell *row = t->cur + (size_t)y * (size_t)t->w;
+ for (int i = 0; s[i] != '\0' && x < t->w; i++, x++) {
+ unsigned char c = (unsigned char)s[i];
+ if (maxw > 0 && i >= maxw)
+ break;
+ if (x < 0)
+ continue;
+ if (c < 0x20)
+ c = '?';
+ row[x].ch = c;
+ row[x].fg = st.fg;
+ row[x].bg = st.bg;
+ row[x].attr = st.attr;
+ }
+}
+
+void tui_printf(struct tui *t, int x, int y, int maxw, tui_style st,
+ const char *fmt, ...)
+{
+ char b[512];
+ va_list ap;
+
+ va_start(ap, fmt);
+ (void)vsnprintf(b, sizeof b, fmt, ap);
+ va_end(ap);
+ tui_put(t, x, y, b, maxw, st);
+}
+
+void tui_box(struct tui *t, int x, int y, int w, int h, tui_style st)
+{
+ if (w < 2 || h < 2)
+ return;
+ for (int i = 1; i < w - 1; i++) {
+ tui_put(t, x + i, y, "-", 1, st);
+ tui_put(t, x + i, y + h - 1, "-", 1, st);
+ }
+ for (int j = 1; j < h - 1; j++) {
+ tui_put(t, x, y + j, "|", 1, st);
+ tui_put(t, x + w - 1, y + j, "|", 1, st);
+ }
+ tui_put(t, x, y, "+", 1, st);
+ tui_put(t, x + w - 1, y, "+", 1, st);
+ tui_put(t, x, y + h - 1, "+", 1, st);
+ tui_put(t, x + w - 1, y + h - 1, "+", 1, st);
+}
+
+void tui_flush(struct tui *t)
+{
+ int lfg = -1, lbg = -1, la = -1;
+
+ if (!t->open)
+ return;
+ if (t->full) {
+ outs(t, CLEAR);
+ memset(t->old, 0, (size_t)t->w * (size_t)t->h * sizeof *t->old);
+ t->full = false;
+ }
+ for (int y = 0; y < t->h; y++) {
+ struct tui_cell *row = t->cur + (size_t)y * (size_t)t->w;
+ struct tui_cell *orow = t->old + (size_t)y * (size_t)t->w;
+ int x = 0;
+
+ while (x < t->w) {
+ while (x < t->w && row[x].ch == orow[x].ch &&
+ row[x].fg == orow[x].fg && row[x].bg == orow[x].bg &&
+ row[x].attr == orow[x].attr)
+ x++;
+ if (x >= t->w)
+ break;
+ int start = x;
+ while (x < t->w && !(row[x].ch == orow[x].ch &&
+ row[x].fg == orow[x].fg &&
+ row[x].bg == orow[x].bg &&
+ row[x].attr == orow[x].attr))
+ x++;
+ outf(t, "%s[%d;%dH", ESCS, y + 1, start + 1);
+ for (int i = start; i < x; i++) {
+ if (row[i].fg != lfg || row[i].bg != lbg || row[i].attr != la) {
+ sgr(t, row[i].fg, row[i].bg, row[i].attr);
+ lfg = row[i].fg;
+ lbg = row[i].bg;
+ la = row[i].attr;
+ }
+ char c = (char)(row[i].ch != 0 ? row[i].ch : ' ');
+ out(t, &c, 1);
+ }
+ }
+ }
+ memcpy(t->old, t->cur, (size_t)t->w * (size_t)t->h * sizeof *t->cur);
+ out_flush(t);
+}
+
+static void shift_in(struct tui *t, int n)
+{
+ if (n >= t->inlen) {
+ t->inlen = 0;
+ return;
+ }
+ memmove(t->in, t->in + n, (size_t)(t->inlen - n));
+ t->inlen -= n;
+}
+
+static int decode(struct tui *t)
+{
+ unsigned char *b = t->in;
+
+ if (t->inlen == 0)
+ return TUI_KEY_NONE;
+ if (b[0] != ESC_CHAR) {
+ int k = b[0];
+ shift_in(t, 1);
+ return k;
+ }
+ if (t->inlen < 2)
+ return TUI_KEY_NONE;
+ if (b[1] != '[') {
+ shift_in(t, 1);
+ return TUI_KEY_ESC;
+ }
+ if (t->inlen < 3)
+ return TUI_KEY_NONE;
+
+ int k = TUI_KEY_NONE;
+ switch (b[2]) {
+ case 'A': k = TUI_KEY_UP; break;
+ case 'B': k = TUI_KEY_DOWN; break;
+ case 'C': k = TUI_KEY_RIGHT; break;
+ case 'D': k = TUI_KEY_LEFT; break;
+ case 'H': k = TUI_KEY_HOME; break;
+ case 'F': k = TUI_KEY_END; break;
+ default: break;
+ }
+ if (k != TUI_KEY_NONE) {
+ shift_in(t, 3);
+ return k;
+ }
+ if (b[2] >= '1' && b[2] <= '9') {
+ int v = 0, i = 2;
+ while (i < t->inlen && b[i] >= '0' && b[i] <= '9') {
+ v = v * 10 + (b[i] - '0');
+ i++;
+ }
+ if (i >= t->inlen)
+ return TUI_KEY_NONE; /* wait for the final byte */
+ if (i < t->inlen && b[i] == '~') {
+ switch (v) {
+ case 1: case 7: k = TUI_KEY_HOME; break;
+ case 2: k = TUI_KEY_NONE; break;
+ case 3: k = TUI_KEY_DEL; break;
+ case 4: case 8: k = TUI_KEY_END; break;
+ case 5: k = TUI_KEY_PGUP; break;
+ case 6: k = TUI_KEY_PGDN; break;
+ default: break;
+ }
+ shift_in(t, i + 1);
+ return k;
+ }
+ }
+ /* Unknown CSI: drop the introducer and try again, so a paste of
+ * unsupported sequences does not turn into random commands. */
+ shift_in(t, 3);
+ return TUI_KEY_NONE;
+}
+
+int tui_key(struct tui *t, int timeout_ms)
+{
+ if (g_winch) {
+ g_winch = 0;
+ return TUI_KEY_RESIZE;
+ }
+ for (;;) {
+ if (t->inlen > 0) {
+ int k = decode(t);
+ if (k != TUI_KEY_NONE)
+ return k;
+ }
+ if (g_winch) {
+ g_winch = 0;
+ return TUI_KEY_RESIZE;
+ }
+ int wait = (t->inlen > 0) ? 30 : timeout_ms;
+ fd_set f;
+ struct timeval tv, *ptv = NULL;
+
+ FD_ZERO(&f);
+ FD_SET(t->fd_in, &f);
+ if (wait >= 0) {
+ tv.tv_sec = wait / 1000;
+ tv.tv_usec = (wait % 1000) * 1000;
+ ptv = &tv;
+ }
+ int r = select(t->fd_in + 1, &f, NULL, NULL, ptv);
+ if (r < 0) {
+ if (errno == EINTR)
+ continue;
+ return TUI_KEY_NONE;
+ }
+ if (r == 0) {
+ if (t->inlen > 0) {
+ if (t->inlen == 1 && t->in[0] == ESC_CHAR) {
+ t->inlen = 0;
+ return TUI_KEY_ESC;
+ }
+ t->inlen = 0;
+ }
+ return TUI_KEY_NONE;
+ }
+ ssize_t n = read(t->fd_in, t->in + t->inlen,
+ sizeof t->in - (size_t)t->inlen);
+ if (n <= 0) {
+ if (n < 0 && errno == EINTR)
+ continue;
+ return TUI_KEY_ESC;
+ }
+ t->inlen += (int)n;
+ }
+}
diff --git a/src/tui.h b/src/tui.h
new file mode 100644
index 0000000..b68f09e
--- /dev/null
+++ b/src/tui.h
@@ -0,0 +1,93 @@
+/**
+ * @file tui.h
+ * @brief A small cell-based terminal UI: double buffer, diff flush, raw keys.
+ *
+ * Deliberately no ncurses and no terminfo. Output is plain ANSI (xterm and
+ * anything that copies it), input is decoded by hand. Only bytes are stored,
+ * so a cell is one character wide: ASCII art, not UTF-8 box drawing. On z/OS
+ * the C compiler's EBCDIC literals are converted by the tty, which is why
+ * TUI_ESC below is 0x27 there and 0x1b everywhere else.
+ */
+#ifndef ZTOP_TUI_H
+#define ZTOP_TUI_H
+
+#include <stdbool.h>
+#include <termios.h>
+
+struct tui_cell {
+ unsigned char ch;
+ unsigned char fg;
+ unsigned char bg;
+ unsigned char attr;
+};
+
+typedef struct {
+ unsigned char fg; /**< 0-7, 7 is white */
+ unsigned char bg; /**< 0-7, 0 is black */
+ unsigned char attr; /**< TUI_BOLD / TUI_REVERSE / TUI_DIM / TUI_UNDERLINE */
+} tui_style;
+
+#define TUI_BOLD 1u
+#define TUI_REVERSE 2u
+#define TUI_DIM 4u
+#define TUI_UNDERLINE 8u
+
+#define TUI_STYLE(f, b, a) ((tui_style){ (unsigned char)(f), (unsigned char)(b), (unsigned char)(a) })
+#define TUI_PLAIN TUI_STYLE(7, 0, 0)
+#define TUI_TITLE TUI_STYLE(7, 4, TUI_BOLD)
+#define TUI_LABEL TUI_STYLE(6, 0, 0)
+#define TUI_GOOD TUI_STYLE(2, 0, 0)
+#define TUI_WARN TUI_STYLE(3, 0, 0)
+#define TUI_BAD TUI_STYLE(1, 0, TUI_BOLD)
+
+enum {
+ TUI_KEY_NONE = -1,
+ TUI_KEY_ESC = 256,
+ TUI_KEY_UP,
+ TUI_KEY_DOWN,
+ TUI_KEY_LEFT,
+ TUI_KEY_RIGHT,
+ TUI_KEY_HOME,
+ TUI_KEY_END,
+ TUI_KEY_PGUP,
+ TUI_KEY_PGDN,
+ TUI_KEY_DEL,
+ TUI_KEY_RESIZE
+};
+
+#define TUI_MAX_W 500
+#define TUI_MAX_H 200
+
+struct tui {
+ int w, h;
+ int fd_in, fd_out;
+ struct tui_cell *cur;
+ struct tui_cell *old;
+ bool full;
+ bool open;
+ struct termios orig;
+ unsigned char in[64];
+ int inlen;
+ char out[32768];
+ int olen;
+};
+
+bool tui_open(struct tui *t);
+void tui_close(struct tui *t);
+
+/** @brief Re-query the size; returns true when it changed and the buffers moved. */
+bool tui_resize(struct tui *t);
+void tui_size(struct tui *t, int *w, int *h);
+
+void tui_clear(struct tui *t);
+void tui_fill(struct tui *t, int x, int y, int w, int h, char ch, tui_style st);
+void tui_put(struct tui *t, int x, int y, const char *s, int maxw, tui_style st);
+void tui_printf(struct tui *t, int x, int y, int maxw, tui_style st,
+ const char *fmt, ...);
+void tui_box(struct tui *t, int x, int y, int w, int h, tui_style st);
+void tui_flush(struct tui *t);
+
+/** @brief Wait up to @p timeout_ms (-1 = forever) for a key. */
+int tui_key(struct tui *t, int timeout_ms);
+
+#endif
diff --git a/src/vsm.c b/src/vsm.c
new file mode 100644
index 0000000..66e0186
--- /dev/null
+++ b/src/vsm.c
@@ -0,0 +1,278 @@
+/**
+ * @file vsm.c
+ * @brief Parse the VSMLIST data area. No z/OS dependencies: the parser is
+ * exercised on Linux against synthetic streams in tests/test_vsm.c.
+ */
+#include "vsm.h"
+
+#include <stdio.h>
+#include <string.h>
+
+struct rd {
+ const unsigned char *p;
+ unsigned len;
+ unsigned off;
+ bool bad;
+};
+
+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)
+{
+ if (r->bad || r->off > r->len || r->len - r->off < 4) {
+ r->bad = true;
+ return 0;
+ }
+ uint32_t v = be32(r->p + r->off);
+ r->off += 4;
+ return v;
+}
+
+static unsigned rd8(struct rd *r)
+{
+ if (r->bad || r->off >= r->len) {
+ r->bad = true;
+ return 0;
+ }
+ return r->p[r->off++];
+}
+
+static bool fail(struct vsm_info *v, const char *what, unsigned off)
+{
+ (void)snprintf(v->err, sizeof v->err, "%s at offset %u", what, off);
+ return false;
+}
+
+/* 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)
+{
+ 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)
+{
+ memset(v, 0, sizeof *v);
+ v->area = area;
+ v->space = space;
+ v->lo = 0xFFFFFFFFu;
+}
+
+bool vsm_work_data(const unsigned char *work, unsigned wlen,
+ const unsigned char **data, unsigned *len)
+{
+ uint32_t addr, dlen;
+ uintptr_t base = (uintptr_t)work;
+
+ if (wlen < 12)
+ return false;
+ addr = be32(work + 4);
+ dlen = be32(work + 8);
+
+ if (dlen == 0) {
+ *data = NULL;
+ *len = 0;
+ return true;
+ }
+ if ((uint64_t)addr < (uint64_t)base ||
+ (uint64_t)addr + dlen > (uint64_t)base + wlen)
+ return false;
+ *data = (const unsigned char *)(uintptr_t)addr;
+ *len = dlen;
+ return true;
+}
+
+static struct vsm_subpool *sub_find(struct 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;
+ }
+ struct vsm_subpool *s = &v->sub[v->nsub++];
+ memset(s, 0, sizeof *s);
+ s->id = id;
+ s->tcb = tcb;
+ s->lo = 0xFFFFFFFFu;
+ return s;
+}
+
+/* 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)
+{
+ unsigned off = r->off;
+ uint32_t addr = rd32(r);
+ uint32_t blen = rd32(r);
+ uint64_t bfree = 0;
+
+ if (r->bad)
+ return fail(v, "allocated block past end of data", off);
+
+ if (v->space == VSM_FREE) {
+ uint32_t nf = rd32(r);
+ if (r->bad)
+ return fail(v, "free-descriptor count past end of data", r->off - 4);
+ if (!count_ok(r, nf))
+ return fail(v, "free-descriptor count larger than the data area", r->off - 4);
+ for (uint32_t i = 0; i < nf; i++) {
+ unsigned foff = r->off;
+ (void)rd32(r); /* free block address */
+ uint32_t flen = rd32(r);
+ if (r->bad)
+ return fail(v, "free descriptor past end of data", foff);
+ bfree += flen;
+ }
+ }
+ if (bfree > blen)
+ bfree = blen; /* never report negative use */
+
+ uint64_t used = (uint64_t)blen - bfree;
+ v->blocks++;
+ v->foot += blen;
+ v->free += bfree;
+ v->used += used;
+ if (blen > v->largest)
+ v->largest = blen;
+ if (addr < v->lo)
+ v->lo = addr;
+ if ((uint64_t)addr + blen > v->hi)
+ v->hi = (uint32_t)((uint64_t)addr + blen);
+
+ if (s != NULL) {
+ s->blocks++;
+ s->foot += blen;
+ s->free += bfree;
+ s->used += used;
+ if (blen > s->largest)
+ s->largest = blen;
+ if (addr < s->lo)
+ s->lo = addr;
+ if ((uint64_t)addr + blen > s->hi)
+ s->hi = (uint32_t)((uint64_t)addr + blen);
+ }
+ return true;
+}
+
+static bool parse_alloc_section(struct rd *r, struct vsm_info *v)
+{
+ uint32_t nsub = rd32(r);
+ if (r->bad)
+ return fail(v, "subpool count past end of data", r->off - 4);
+ /* A descriptor is at least 8 bytes, so this bound is safe. */
+ if (nsub > (r->len - r->off) / 8 + 1)
+ return fail(v, "subpool count larger than the data area", r->off - 4);
+
+ for (uint32_t i = 0; i < nsub; i++) {
+ unsigned off = r->off;
+ unsigned fmt = rd8(r);
+ unsigned dlen = rd8(r);
+ unsigned id = rd8(r);
+ unsigned flags = rd8(r);
+ uint32_t tcb = rd32(r);
+
+ if (r->bad)
+ return fail(v, "subpool descriptor past end of data", off);
+ if (fmt != 0)
+ return fail(v, "subpool descriptor format byte is not zero", off);
+ if (dlen < 8 || dlen > 64)
+ return fail(v, "subpool descriptor length is out of range", off);
+ if ((uint64_t)off + dlen > r->len)
+ return fail(v, "subpool descriptor length past end of data", off);
+ r->off = off + dlen; /* skip trailing reserved bytes */
+
+ uint32_t nblocks = rd32(r);
+ if (r->bad)
+ return fail(v, "allocated-block count past end of data", r->off - 4);
+ if (!count_ok(r, nblocks))
+ 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) {
+ s->key = flags & 0x0Fu;
+ s->flags = flags;
+ }
+ for (uint32_t b = 0; b < nblocks; b++)
+ if (!parse_block(r, v, s))
+ return false;
+ }
+ return true;
+}
+
+bool vsm_parse_alloc(struct vsm_info *v, const unsigned char *data, unsigned len)
+{
+ struct rd r = { data, len, 0, false };
+
+ if (v->area == VSM_PVT) {
+ uint32_t n = rd32(&r);
+ if (r.bad)
+ return fail(v, "TCB count past end of data", r.off - 4);
+ if (n > (r.len - r.off) / 4 + 1)
+ return fail(v, "TCB count larger than the data area", r.off - 4);
+ v->tcbs += n;
+ for (uint32_t t = 0; t < n; t++) {
+ unsigned off = r.off;
+ (void)rd32(&r); /* TCB address, for progress only */
+ if (r.bad)
+ return fail(v, "TCB address past end of data", off);
+ if (!parse_alloc_section(&r, v))
+ return false;
+ }
+ } else if (!parse_alloc_section(&r, v)) {
+ return false;
+ }
+ return true;
+}
+
+bool vsm_parse_unalloc(struct vsm_info *v, const unsigned char *data, unsigned len)
+{
+ struct rd r = { data, len, 0, false };
+ uint32_t nreg = rd32(&r);
+
+ if (r.bad)
+ return fail(v, "region count past end of data", r.off - 4);
+ if (nreg > (r.len - r.off) / 12 + 1)
+ return fail(v, "region count larger than the data area", r.off - 4);
+
+ for (uint32_t i = 0; i < nreg; i++) {
+ unsigned off = r.off;
+ uint32_t addr = rd32(&r);
+ uint32_t rlen = rd32(&r);
+
+ if (r.bad)
+ return fail(v, "region descriptor past end of data", off);
+ if (v->nregion < VSM_MAX_REGION) {
+ v->region[v->nregion].addr = addr;
+ v->region[v->nregion].len = rlen;
+ }
+ v->nregion++;
+
+ uint32_t nblocks = rd32(&r);
+ if (r.bad)
+ return fail(v, "unallocated-block count past end of data", r.off - 4);
+ if (!count_ok(&r, nblocks))
+ return fail(v, "unallocated-block count larger than the data area", r.off - 4);
+ for (uint32_t b = 0; b < nblocks; b++) {
+ unsigned boff = r.off;
+ (void)rd32(&r); /* unallocated block address */
+ uint32_t blen = rd32(&r);
+ if (r.bad)
+ return fail(v, "unallocated block past end of data", boff);
+ v->unalloc += blen;
+ v->unalloc_blocks++;
+ if (blen > v->largest_unalloc)
+ v->largest_unalloc = blen;
+ }
+ }
+ return true;
+}
diff --git a/src/vsm.h b/src/vsm.h
new file mode 100644
index 0000000..e245a3c
--- /dev/null
+++ b/src/vsm.h
@@ -0,0 +1,102 @@
+/**
+ * @file vsm.h
+ * @brief The VSMLIST data area, as z/OS documents it.
+ *
+ * The layout comes from z/OS MVS Programming: Authorized Assembler Services
+ * Guide, "Virtual Storage Management", figures 37-46. The work-area header
+ * (return code, data-area address, data-area length) is not part of this
+ * file: src/ztop.c owns the header and hands the data area here.
+ */
+#ifndef ZTOP_VSM_H
+#define ZTOP_VSM_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+/** @brief Area codes, the same numbers the assembler stub takes. */
+enum vsm_area { VSM_CSA = 1, VSM_SQA = 2, VSM_LSQA = 3, VSM_PVT = 4 };
+
+/** @brief SPACE= requests. VSM_FREE includes the allocated information. */
+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
+
+/** @brief Region descriptors kept for display (CSA/ECSA/RUCSA, PVT regions). */
+#define VSM_MAX_REGION 16
+
+/** @brief One subpool: allocated blocks, the free space in them, the real use. */
+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 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 */
+};
+
+/** @brief A region descriptor (an unallocated range, or a PVT region). */
+struct vsm_region {
+ uint32_t addr;
+ uint32_t len;
+};
+
+/** @brief Everything one area reports, accumulated over continuation calls. */
+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];
+
+ unsigned blocks; /**< allocated blocks in the area */
+ uint64_t used;
+ uint64_t free;
+ uint64_t foot;
+ uint64_t largest;
+ uint32_t lo, hi;
+
+ unsigned unalloc_blocks;
+ uint64_t unalloc; /**< unallocated bytes (CSA/PVT only) */
+ uint64_t largest_unalloc;
+ int nregion;
+ struct 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 */
+};
+
+/** @brief Zero @p v and set the request it describes. */
+void vsm_reset(struct vsm_info *v, unsigned area, unsigned space);
+
+/**
+ * @brief Check the work-area header and locate the data area.
+ *
+ * Bytes 0-3 are the service return code, 4-7 the address of the data area,
+ * 8-11 its length. The data area must lie inside @p work.
+ *
+ * @return false when the header cannot be believed.
+ */
+bool vsm_work_data(const unsigned char *work, unsigned wlen,
+ const unsigned char **data, unsigned *len);
+
+/**
+ * @brief Parse an allocated-storage data area (SPACE=ALLOC or FREE).
+ *
+ * Merges into @p v, so a caller walking a return code 4 continuation simply
+ * calls this again for each piece. For PVT the block is the per-TCB form.
+ *
+ * @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);
+
+/** @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);
+
+#endif
diff --git a/src/ztop.c b/src/ztop.c
index 7284a42..8aa84e4 100644
--- a/src/ztop.c
+++ b/src/ztop.c
@@ -1,160 +1,742 @@
/**
* @file ztop.c
- * @brief Show how much storage is allocated in each subpool of a common area.
- *
- * The numbers come from VSMLIST, the one interface z/OS offers an ordinary
- * user task for this (problem state, key 8, LINKAGE=SYSTEM). It is a
- * macro, so the call lives in src/zvslist.s and this file only parses what
- * comes back.
- *
- * ztop [-a csa|sqa|lsqa|pvt] [-x]
- *
- * Only z/OS builds: VSMLIST does not exist anywhere else.
+ * @brief Storage overview for z/OS: one-shot reports and a full-screen view.
*/
#include <stdbool.h>
+#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
-#include <stddef.h>
#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "tui.h"
+#include "vsm.h"
-/** @brief One request to the assembler stub; layout is shared with zvslist.s. */
+#define ZTOP_VERSION "0.2"
+#define WORK_SIZE (1024u * 1024u)
+
+#if defined(__MVS__)
+#pragma linkage(zvslist, OS)
+extern int zvslist(void *);
+
+/* The 31-bit request block. R13 is saved by the stub, so the save area has
+ * to live in storage the C compiler gives us, not in the read-only CSECT. */
struct zreq {
- int area; /**< 1 CSA, 2 SQA, 3 LSQA, 4 private */
- unsigned len; /**< bytes in wk */
- void *wk; /**< work area, VSMLIST writes into it */
- int rc; /**< the service's return code */
- void *self; /**< scratch: the stub keeps the block here */
- unsigned long long save[18]; /**< the stub's save area: it has no own */
- int auth; /**< TESTAUTH FCTN=1: 0 means authorized */
+ int area;
+ unsigned len;
+ void *wk;
+ int rc;
+ void *self;
+ unsigned long long save[18];
+ int space;
};
+_Static_assert(offsetof(struct zreq, self) == 16, "zreq.self");
+_Static_assert(offsetof(struct zreq, save) == 24, "zreq.save");
+_Static_assert(offsetof(struct zreq, space) == 168, "zreq.space");
+
+static unsigned char work[WORK_SIZE] __attribute__((aligned(8)));
+#endif
+
+static const char *const area_name[] = { "csa", "sqa", "lsqa", "pvt" };
+static const char *const area_upper[] = { "CSA", "SQA", "LSQA", "PVT" };
+
+static void human(uint64_t n, char *buf, size_t len)
+{
+ if (n >= (1ULL << 30))
+ (void)snprintf(buf, len, "%.1fG", (double)n / (double)(1ULL << 30));
+ else if (n >= (1ULL << 20))
+ (void)snprintf(buf, len, "%.1fM", (double)n / (double)(1ULL << 20));
+ else if (n >= (1ULL << 10))
+ (void)snprintf(buf, len, "%.1fK", (double)n / (double)(1ULL << 10));
+ else
+ (void)snprintf(buf, len, "%llu", (unsigned long long)n);
+}
-/* The stub addresses these by offset, because it has no base register:
- * an assembler CSECT is read-only in an XPLINK load module, so the save
- * area cannot live there. */
#if defined(__MVS__)
-/* 31-bit z/OS layout is the one the stub addresses; a 64-bit host
- * lays the pointers out differently and these offsets are meaningless. */
-_Static_assert(offsetof(struct zreq, self) == 16, "zvslist.s stores self at 16");
-_Static_assert(offsetof(struct zreq, save) == 24, "zvslist.s uses 24 for save");
-_Static_assert(offsetof(struct zreq, auth) == 168, "zvslist.s stores at 168");
+static void hexdump(const char *tag, const unsigned char *p, unsigned len,
+ unsigned max)
+{
+ if (len > max)
+ len = max;
+ for (unsigned i = 0; i < len; i += 16) {
+ (void)fprintf(stderr, "%s %04X:", tag, i);
+ for (unsigned j = 0; j < 16 && i + j < len; j++)
+ (void)fprintf(stderr, " %02X", p[i + j]);
+ (void)fputc('\n', stderr);
+ }
+}
+
+static void dump_work(const struct vsm_info *v, const unsigned char *workp,
+ const unsigned char *data, unsigned 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)(uintptr_t)data, len);
+ hexdump("work", workp, 32, 32);
+ if (data != NULL && len > 0)
+ hexdump("data", data, len, 256);
+}
#endif
-/* HLASM upcases the ESD name of the CSECT, so the assembler side is
- * exported as ZVSLIST whatever case the source is written in. */
-extern int zvslist(struct zreq *req) __asm__("ZVSLIST");
-#pragma linkage(zvslist, OS)
+static int pct(const struct vsm_info *v)
+{
+ uint64_t total;
-/** @brief Work area size; IBM's minimum is 4K, a busy CSA needs more. */
-static constexpr unsigned WORK = 65536;
+ if (!v->unalloc_valid || v->area != VSM_CSA)
+ return -1;
+ total = v->foot + v->unalloc;
+ if (total == 0)
+ return 0;
+ return (int)((v->used * 100u + total / 2) / total);
+}
-/** @brief A subpool descriptor (VSM data area, `VSMD`). */
-static constexpr unsigned SUBPOOL_DESC = 8;
+#if defined(__MVS__)
+static bool fetch_one(struct vsm_info *v, int area, int space, bool dump)
+{
+ int calls = 0;
-static unsigned be32(const unsigned char *p) {
- return ((unsigned)p[0] << 24) | ((unsigned)p[1] << 16) | ((unsigned)p[2] << 8) | p[3];
+ vsm_reset(v, (unsigned)area, (unsigned)space);
+ memset(work, 0, 4); /* return code must start at 0 */
+ for (;;) {
+ if (++calls > 64) {
+ (void)snprintf(v->err, sizeof v->err,
+ "VSMLIST did not finish after 64 calls");
+ return false;
+ }
+ struct zreq req = { 0 };
+ req.area = area;
+ req.space = space;
+ req.len = WORK_SIZE;
+ req.wk = work;
+ req.rc = -1;
+ (void)zvslist(&req);
+ if (req.rc != 0 && req.rc != 4) {
+ (void)snprintf(v->err, sizeof v->err,
+ "VSMLIST returned %d", req.rc);
+ return false;
+ }
+ const unsigned char *data;
+ unsigned len;
+ if (!vsm_work_data(work, WORK_SIZE, &data, &len)) {
+ (void)snprintf(v->err, sizeof v->err,
+ "VSMLIST work area has an unusable data pointer");
+ return false;
+ }
+ if (dump)
+ dump_work(v, work, data, len, req.rc);
+ bool ok = space == VSM_UNALLOC
+ ? vsm_parse_unalloc(v, data, len)
+ : vsm_parse_alloc(v, data, len);
+ if (!ok)
+ return false;
+ if (req.rc == 0)
+ return true;
+ /* rc 4: the service wants the same request and work area again. */
+ }
+}
+#else
+static bool fetch_one(struct vsm_info *v, int area, int space, bool dump)
+{
+ (void)area;
+ (void)space;
+ (void)dump;
+ (void)snprintf(v->err, sizeof v->err, "VSMLIST is only available on z/OS");
+ return false;
}
+#endif
+
+static bool collect_area(struct vsm_info *v, int area, bool dump)
+{
+ if (!fetch_one(v, area, VSM_FREE, dump))
+ return false;
+ if (area == VSM_CSA || area == VSM_PVT) {
+ struct vsm_info u;
+ if (fetch_one(&u, area, VSM_UNALLOC, dump)) {
+ v->unalloc = u.unalloc;
+ v->unalloc_blocks = u.unalloc_blocks;
+ v->largest_unalloc = u.largest_unalloc;
+ v->nregion = u.nregion;
+ memcpy(v->region, u.region, sizeof v->region);
+ v->unalloc_valid = true;
+ } else {
+ (void)snprintf(v->err, sizeof v->err, "%s", u.err);
+ }
+ }
+ return true;
+}
+
+static void print_area(const struct vsm_info *v)
+{
+ const char *name = area_name[v->area - 1];
+ bool pvt = v->area == VSM_PVT;
+ int p = pct(v);
+
+ (void)printf("area %s: used %llu free %llu mapped %llu blocks %u",
+ name, (unsigned long long)v->used,
+ (unsigned long long)v->free,
+ (unsigned long long)v->foot, v->blocks);
+ if (v->area == VSM_CSA && v->unalloc_valid)
+ (void)printf(" unallocated %llu total %llu use %d%%",
+ (unsigned long long)v->unalloc,
+ (unsigned long long)(v->foot + v->unalloc), p);
+ if (pvt)
+ (void)printf(" tcbs %u", v->tcbs);
+ (void)printf("\n");
-/** @brief Storage behind one subpool descriptor. */
-struct subpool {
- unsigned id;
- unsigned long long bytes;
- unsigned blocks;
+ if (pvt)
+ (void)printf("%3s %3s %8s %5s %10s %10s %10s %10s %8s\n",
+ "sp", "key", "tcb", "blks", "used", "free", "mapped",
+ "largest", "addr");
+ else
+ (void)printf("%3s %3s %6s %10s %10s %10s %10s %17s\n",
+ "sp", "key", "blks", "used", "free", "mapped",
+ "largest", "address");
+
+ for (int i = 0; i < v->nsub; i++) {
+ const struct vsm_subpool *s = &v->sub[i];
+ char addr[32];
+
+ if (s->blocks == 0)
+ (void)snprintf(addr, sizeof addr, "-");
+ else if (pvt)
+ (void)snprintf(addr, sizeof addr, "%08X", (unsigned)s->lo);
+ else
+ (void)snprintf(addr, sizeof addr, "%08X-%08X",
+ (unsigned)s->lo, (unsigned)s->hi);
+ if (pvt)
+ (void)printf("%3u %3u %08X %5u %10llu %10llu %10llu %10llu %s\n",
+ s->id, s->key, (unsigned)s->tcb, s->blocks,
+ (unsigned long long)s->used,
+ (unsigned long long)s->free,
+ (unsigned long long)s->foot,
+ (unsigned long long)s->largest, addr);
+ else
+ (void)printf("%3u %3u %6u %10llu %10llu %10llu %10llu %s\n",
+ s->id, s->key, s->blocks,
+ (unsigned long long)s->used,
+ (unsigned long long)s->free,
+ (unsigned long long)s->foot,
+ (unsigned long long)s->largest, addr);
+ }
+ if (v->truncated)
+ (void)printf("(subpool list truncated at %d rows)\n", VSM_MAX_SUB);
+
+ if (v->unalloc_valid) {
+ (void)printf("unallocated %llu bytes in %u blocks, largest %llu",
+ (unsigned long long)v->unalloc, v->unalloc_blocks,
+ (unsigned long long)v->largest_unalloc);
+ for (int i = 0; i < v->nregion && i < VSM_MAX_REGION; i++)
+ (void)printf("%s%08X+%08X", i == 0 ? " regions" : " ",
+ (unsigned)v->region[i].addr,
+ (unsigned)v->region[i].len);
+ (void)printf("\n");
+ }
+ if (v->err[0] != '\0')
+ (void)printf("warning: %s\n", v->err);
+}
+
+struct app {
+ struct vsm_info info[4];
+ bool have[4];
+ char err[4][160];
+ int sel;
+ bool all;
+ bool paused;
+ bool help;
+ int interval;
+ int collect_ms;
+ int scroll;
+ char host[80];
};
-static void dump(const unsigned char *p, unsigned len) {
- for (unsigned i = 0; i < len; i += 16) {
- printf("%04X ", i);
- for (unsigned j = 0; j < 16; j++)
- printf(i + j < len ? " %02X" : " ", p[i + j]);
- printf(" ");
- for (unsigned j = 0; j < 16 && i + j < len; j++)
- printf("%c", p[i + j] >= 0x20 && p[i + j] < 0x7F ? p[i + j] : '.');
- printf("\n");
+static int visible_rows(const struct tui *t)
+{
+ int n = t->h - 8;
+ return n < 1 ? 1 : n;
+}
+
+static void collect(struct app *a)
+{
+ struct timespec t0, t1;
+ int first = a->all ? 0 : a->sel;
+ int last = a->all ? 3 : a->sel;
+
+ (void)clock_gettime(CLOCK_MONOTONIC, &t0);
+ for (int i = first; i <= last; i++) {
+ struct vsm_info v;
+ if (collect_area(&v, i + 1, false)) {
+ a->info[i] = v;
+ a->have[i] = true;
+ a->err[i][0] = '\0';
+ } else {
+ (void)snprintf(a->err[i], sizeof a->err[i], "%s", v.err);
+ if (!a->have[i])
+ a->info[i].area = (unsigned)(i + 1);
+ }
}
+ (void)clock_gettime(CLOCK_MONOTONIC, &t1);
+ a->collect_ms = (int)((t1.tv_sec - t0.tv_sec) * 1000 +
+ (t1.tv_nsec - t0.tv_nsec) / 1000000);
}
-/**
- * @brief Walk the VSMLIST data area and total the allocated bytes per subpool.
- *
- * A subpool descriptor is X'00', then its length, the subpool id, key and
- * flags, and the owning TCB; allocated block descriptors follow it as
- * address/size pairs. A new subpool descriptor ends the previous run of
- * blocks.
- *
- * shortcut: the descriptor stream has no explicit end marker here, so it
- * is walked until the bytes stop being plausible; a wrong guess shows up as
- * an implausible subpool id rather than as silent corruption, and -x dumps
- * the raw work area to check against.
- */
-static int parse(const unsigned char *p, unsigned len, struct subpool *out, int max) {
- int n = 0;
- unsigned off = 4; /* bytes 0-3 are the return code */
+static void draw_tabs(struct tui *t, const struct app *a)
+{
+ static const char *const tabs[4] = { "1 CSA", "2 SQA", "3 LSQA", "4 PVT" };
- while (off + SUBPOOL_DESC <= len && n < max) {
- if (p[off] != 0x00 || p[off + 1] < SUBPOOL_DESC || p[off + 1] > 64) {
- off++;
+ tui_fill(t, 0, 1, t->w, 1, ' ', TUI_PLAIN);
+ for (int i = 0; i < 4; i++) {
+ tui_style st = (!a->all && a->sel == i) ? TUI_TITLE : TUI_LABEL;
+ tui_printf(t, 1 + i * 9, 1, 8, st, " %s ", tabs[i]);
+ }
+ tui_printf(t, 38, 1, 7, a->all ? TUI_TITLE : TUI_LABEL, " a ALL ");
+ if (a->paused)
+ tui_printf(t, t->w - 8, 1, 7, TUI_WARN, " PAUSED");
+}
+
+static void draw_bar(struct tui *t, int x, int y, int p)
+{
+ char b[13];
+ int fill = p < 0 ? 0 : p / 10;
+
+ if (fill > 10)
+ fill = 10;
+ b[0] = '[';
+ for (int i = 0; i < 10; i++)
+ b[1 + i] = i < fill ? '#' : '-';
+ b[11] = ']';
+ b[12] = '\0';
+ tui_put(t, x, y, b, 12,
+ p < 0 ? TUI_STYLE(7, 0, TUI_DIM)
+ : p < 70 ? TUI_GOOD : p < 85 ? TUI_WARN : TUI_BAD);
+}
+
+static void draw_all(struct tui *t, const struct app *a)
+{
+ static const int xnum = 8, xused = 15, xfree = 26, xmap = 37;
+ static const int xtot = 48, xpct = 59, xbar = 66;
+
+ tui_printf(t, 1, 3, 6, TUI_LABEL, "AREA");
+ tui_printf(t, xnum, 3, 6, TUI_LABEL, "BLOCKS");
+ tui_printf(t, xused, 3, 9, TUI_LABEL, "USED");
+ tui_printf(t, xfree, 3, 9, TUI_LABEL, "FREE");
+ tui_printf(t, xmap, 3, 9, TUI_LABEL, "MAPPED");
+ tui_printf(t, xtot, 3, 9, TUI_LABEL, "TOTAL");
+ tui_printf(t, xpct, 3, 5, TUI_LABEL, "USE");
+
+ for (int i = 0; i < 4; i++) {
+ int y = 4 + i;
+ const struct vsm_info *v = &a->info[i];
+ char used[16], freed[16], mapped[16], total[16], pc[8];
+ int p;
+
+ if (!a->have[i]) {
+ tui_printf(t, 1, y, 0, TUI_BAD, "%-4s %s", area_upper[i],
+ a->err[i][0] ? a->err[i] : "collecting...");
continue;
}
- unsigned len_desc = p[off + 1];
- struct subpool *s = &out[n++];
- s->id = p[off + 2];
- s->bytes = 0;
- s->blocks = 0;
- off += len_desc;
+ p = pct(v);
+ human(v->used, used, sizeof used);
+ human(v->free, freed, sizeof freed);
+ human(v->foot, mapped, sizeof mapped);
+ if (v->area == VSM_CSA && v->unalloc_valid) {
+ human(v->foot + v->unalloc, total, sizeof total);
+ (void)snprintf(pc, sizeof pc, "%d%%", p);
+ } else {
+ (void)snprintf(total, sizeof total, "-");
+ (void)snprintf(pc, sizeof pc, "-");
+ }
+ tui_printf(t, 1, y, 4, TUI_PLAIN, "%s", area_upper[i]);
+ tui_printf(t, xnum, y, 6, TUI_PLAIN, "%6u", v->blocks);
+ tui_printf(t, xused, y, 9, TUI_PLAIN, "%9s", used);
+ tui_printf(t, xfree, y, 9, TUI_PLAIN, "%9s", freed);
+ tui_printf(t, xmap, y, 9, TUI_PLAIN, "%9s", mapped);
+ tui_printf(t, xtot, y, 9, TUI_PLAIN, "%9s", total);
+ tui_printf(t, xpct, y, 5,
+ p < 0 ? TUI_PLAIN : p < 70 ? TUI_GOOD :
+ p < 85 ? TUI_WARN : TUI_BAD, "%5s", pc);
+ draw_bar(t, xbar, y, p);
+ }
+}
+
+static void draw_detail(struct tui *t, struct app *a)
+{
+ const struct vsm_info *v = &a->info[a->sel];
+ bool pvt = a->sel == 3;
+ int rows = visible_rows(t);
+ int p = pct(v);
+
+ if (!a->have[a->sel]) {
+ tui_printf(t, 1, 3, 0, TUI_BAD, "%s: %s", area_upper[a->sel],
+ a->err[a->sel][0] ? a->err[a->sel] : "no data");
+ return;
+ }
+
+ char used[16], freed[16], mapped[16], total[16];
+ human(v->used, used, sizeof used);
+ human(v->free, freed, sizeof freed);
+ human(v->foot, mapped, sizeof mapped);
+ if (v->area == VSM_CSA && v->unalloc_valid)
+ human(v->foot + v->unalloc, total, sizeof total);
+ else
+ (void)snprintf(total, sizeof total, "-");
- while (off + 8 <= len && p[off] != 0x00) {
- unsigned long long size = be32(p + off + 4) & 0x7FFFFFF8u;
- if (size == 0 || size > (4ull << 30))
+ tui_printf(t, 1, 3, 0, TUI_PLAIN,
+ "%s used %s free %s mapped %s total %s %d%%",
+ area_upper[a->sel], used, freed, mapped, total, p);
+
+ if (pvt) {
+ tui_printf(t, 1, 4, 3, TUI_LABEL, "SP");
+ tui_printf(t, 5, 4, 3, TUI_LABEL, "KY");
+ tui_printf(t, 9, 4, 8, TUI_LABEL, "TCB");
+ tui_printf(t, 18, 4, 5, TUI_LABEL, "BLKS");
+ tui_printf(t, 25, 4, 9, TUI_LABEL, "USED");
+ tui_printf(t, 35, 4, 9, TUI_LABEL, "FREE");
+ tui_printf(t, 45, 4, 9, TUI_LABEL, "MAPPED");
+ tui_printf(t, 55, 4, 9, TUI_LABEL, "LARGEST");
+ tui_printf(t, 65, 4, 9, TUI_LABEL, "ADDR");
+ } else {
+ tui_printf(t, 1, 4, 3, TUI_LABEL, "SP");
+ tui_printf(t, 5, 4, 3, TUI_LABEL, "KY");
+ tui_printf(t, 9, 4, 6, TUI_LABEL, "BLKS");
+ tui_printf(t, 16, 4, 9, TUI_LABEL, "USED");
+ tui_printf(t, 26, 4, 9, TUI_LABEL, "FREE");
+ tui_printf(t, 36, 4, 9, TUI_LABEL, "MAPPED");
+ tui_printf(t, 46, 4, 9, TUI_LABEL, "LARGEST");
+ tui_printf(t, 56, 4, 17, TUI_LABEL, "ADDRESS");
+ }
+
+ if (a->scroll > v->nsub - rows)
+ a->scroll = v->nsub - rows;
+ if (a->scroll < 0)
+ a->scroll = 0;
+
+ for (int r = 0; r < rows && a->scroll + r < v->nsub; r++) {
+ const struct vsm_subpool *s = &v->sub[a->scroll + r];
+ int y = 5 + r;
+ char u[16], f[16], m[16], l[16], addr[32];
+
+ human(s->used, u, sizeof u);
+ human(s->free, f, sizeof f);
+ human(s->foot, m, sizeof m);
+ human(s->largest, l, sizeof l);
+ if (s->blocks == 0)
+ (void)snprintf(addr, sizeof addr, "-");
+ else if (pvt)
+ (void)snprintf(addr, sizeof addr, "%08X", (unsigned)s->lo);
+ else
+ (void)snprintf(addr, sizeof addr, "%08X-%08X",
+ (unsigned)s->lo, (unsigned)s->hi);
+
+ tui_printf(t, 1, y, 3, TUI_PLAIN, "%3u", s->id);
+ tui_printf(t, 5, y, 3, TUI_PLAIN, "%3u", s->key);
+ if (pvt) {
+ tui_printf(t, 9, y, 8, TUI_LABEL, "%08X", (unsigned)s->tcb);
+ tui_printf(t, 18, y, 5, TUI_PLAIN, "%5u", s->blocks);
+ tui_printf(t, 25, y, 9, TUI_PLAIN, "%9s", u);
+ tui_printf(t, 35, y, 9, TUI_PLAIN, "%9s", f);
+ tui_printf(t, 45, y, 9, TUI_PLAIN, "%9s", m);
+ tui_printf(t, 55, y, 9, TUI_PLAIN, "%9s", l);
+ tui_printf(t, 65, y, 8, TUI_LABEL, "%s", addr);
+ } else {
+ tui_printf(t, 9, y, 6, TUI_PLAIN, "%6u", s->blocks);
+ tui_printf(t, 16, y, 9, TUI_PLAIN, "%9s", u);
+ tui_printf(t, 26, y, 9, TUI_PLAIN, "%9s", f);
+ tui_printf(t, 36, y, 9, TUI_PLAIN, "%9s", m);
+ tui_printf(t, 46, y, 9, TUI_PLAIN, "%9s", l);
+ tui_printf(t, 56, y, 17, TUI_LABEL, "%s", addr);
+ }
+ }
+
+ /* The line above the keys: first an error, then regions. */
+ int y = t->h - 3;
+ if (a->err[a->sel][0] != '\0')
+ tui_printf(t, 1, y, t->w - 2, TUI_BAD, "! %s", a->err[a->sel]);
+ else if (v->unalloc_valid) {
+ if (v->nregion > 0) {
+ tui_printf(t, 1, y, t->w - 2, TUI_LABEL,
+ "regions: %08X+%08X%s", (unsigned)v->region[0].addr,
+ (unsigned)v->region[0].len,
+ v->nregion > 1 ? " ..." : "");
+ } else {
+ tui_printf(t, 1, y, t->w - 2, TUI_PLAIN,
+ "unallocated %llu bytes, %u blocks, largest %llu",
+ (unsigned long long)v->unalloc, v->unalloc_blocks,
+ (unsigned long long)v->largest_unalloc);
+ }
+ }
+}
+
+static void draw_help(struct tui *t)
+{
+ int w = t->w < 56 ? t->w - 2 : 54;
+ int h = 15;
+ int x = (t->w - w) / 2;
+ int y = (t->h - h) / 2;
+
+ tui_fill(t, x, y, w, h, ' ', TUI_PLAIN);
+ tui_box(t, x, y, w, h, TUI_LABEL);
+ tui_printf(t, x + 2, y + 1, w - 4, TUI_TITLE, "ztop keys");
+ tui_printf(t, x + 2, y + 3, w - 4, TUI_PLAIN, "q quit");
+ tui_printf(t, x + 2, y + 4, w - 4, TUI_PLAIN, "1-4 csa, sqa, lsqa, pvt");
+ tui_printf(t, x + 2, y + 5, w - 4, TUI_PLAIN, "a all areas");
+ tui_printf(t, x + 2, y + 6, w - 4, TUI_PLAIN, "p pause refresh");
+ tui_printf(t, x + 2, y + 7, w - 4, TUI_PLAIN, "r refresh now");
+ tui_printf(t, x + 2, y + 8, w - 4, TUI_PLAIN, "+ - interval up / down");
+ tui_printf(t, x + 2, y + 9, w - 4, TUI_PLAIN, "j k scroll subpools");
+ tui_printf(t, x + 2, y + 10, w - 4, TUI_PLAIN, "up dn same");
+ tui_printf(t, x + 2, y + 11, w - 4, TUI_PLAIN, "g G first / last");
+ tui_printf(t, x + 2, y + 12, w - 4, TUI_PLAIN, "? close help");
+}
+
+static void draw(struct tui *t, struct app *a)
+{
+ char now[16];
+ time_t clock_now = time(NULL);
+ struct tm tm;
+
+ tui_clear(t);
+ (void)localtime_r(&clock_now, &tm);
+ (void)strftime(now, sizeof now, "%H:%M:%S", &tm);
+
+ tui_fill(t, 0, 0, t->w, 1, ' ', TUI_TITLE);
+ tui_printf(t, 1, 0, t->w - 12, TUI_TITLE,
+ " ztop %s %s %s refresh %ds collect %dms ",
+ ZTOP_VERSION, a->host, now, a->interval, a->collect_ms);
+ draw_tabs(t, a);
+ tui_fill(t, 0, 2, t->w, 1, '-', TUI_STYLE(7, 0, TUI_DIM));
+
+ if (t->w < 70 || t->h < 14) {
+ tui_printf(t, 1, 3, 0, TUI_BAD, "terminal too small: need 70x14");
+ } else if (a->all) {
+ draw_all(t, a);
+ tui_printf(t, 1, 9, 0, TUI_PLAIN,
+ "unallocated: CSA %llu PVT %llu",
+ (unsigned long long)(a->info[0].unalloc_valid
+ ? a->info[0].unalloc : 0),
+ (unsigned long long)(a->info[3].unalloc_valid
+ ? a->info[3].unalloc : 0));
+ } else {
+ draw_detail(t, a);
+ }
+
+ if (a->help)
+ draw_help(t);
+
+ tui_fill(t, 0, t->h - 1, t->w, 1, ' ', TUI_TITLE);
+ tui_printf(t, 1, t->h - 1, t->w - 2, TUI_TITLE,
+ "q quit 1-4 area a all p pause r refresh +/- %ds ? help",
+ a->interval);
+}
+
+static bool parse_areas(const char *s, unsigned *mask)
+{
+ unsigned m = 0;
+
+ while (*s != '\0') {
+ size_t n = strcspn(s, ",");
+ if (n == 3 && strncmp(s, "csa", 3) == 0)
+ m |= 1u << 0;
+ else if (n == 3 && strncmp(s, "sqa", 3) == 0)
+ m |= 1u << 1;
+ else if (n == 4 && strncmp(s, "lsqa", 4) == 0)
+ m |= 1u << 2;
+ else if (n == 3 && strncmp(s, "pvt", 3) == 0)
+ m |= 1u << 3;
+ else if (n == 3 && strncmp(s, "all", 3) == 0)
+ m |= 0xFu;
+ else
+ return false;
+ s += n;
+ if (*s == ',')
+ s++;
+ }
+ *mask = m;
+ return m != 0;
+}
+
+static void usage(FILE *f)
+{
+ (void)fprintf(f,
+ "usage: ztop [-a csa|sqa|lsqa|pvt|all] [-x] [-d [seconds]] [-h]\n"
+ " -a area to report (default csa); comma-separate areas or use all\n"
+ " -x dump the VSMLIST work and data areas (one-shot only)\n"
+ " -d interactive full-screen view, refreshed every 2 seconds\n"
+ " -h this text\n");
+}
+
+static void run_oneshot(unsigned mask, bool dump)
+{
+ int rc = 0;
+
+ for (int i = 0; i < 4; i++) {
+ if ((mask & (1u << i)) == 0)
+ continue;
+ struct vsm_info v;
+ if (!collect_area(&v, i + 1, dump)) {
+ (void)fprintf(stderr, "ztop: %s: %s\n", area_name[i], v.err);
+ rc = 1;
+ continue;
+ }
+ if (i != 0)
+ (void)printf("\n");
+ print_area(&v);
+ }
+ exit(rc);
+}
+
+static void run_tui(unsigned mask)
+{
+ static struct app a;
+ struct tui t;
+
+ memset(&a, 0, sizeof a);
+ a.interval = 2;
+ a.all = (mask & 0xFu) == 0xFu;
+ for (int i = 0; i < 4; i++) {
+ a.info[i].area = (unsigned)(i + 1);
+ if (!a.all && (mask & (1u << i)) != 0 && a.sel == 0)
+ a.sel = i;
+ }
+ if (gethostname(a.host, sizeof a.host - 1) != 0)
+ (void)snprintf(a.host, sizeof a.host, "?");
+ a.host[sizeof a.host - 1] = '\0';
+
+ if (!tui_open(&t)) {
+ (void)fprintf(stderr, "ztop: -d needs a terminal\n");
+ exit(1);
+ }
+ collect(&a);
+ for (;;) {
+ draw(&t, &a);
+ tui_flush(&t);
+ int k = tui_key(&t, a.paused ? -1 : a.interval * 1000);
+
+ if (k == TUI_KEY_NONE) {
+ collect(&a);
+ continue;
+ }
+ if (k == TUI_KEY_RESIZE) {
+ (void)tui_resize(&t);
+ continue;
+ }
+ if (a.help) {
+ if (k == '?' || k == TUI_KEY_ESC)
+ a.help = false;
+ else if (k == 'q')
break;
- s->bytes += size;
- s->blocks++;
- off += 8;
+ continue;
+ }
+ if (k == 'q' || k == 0x03 || k == TUI_KEY_ESC)
+ break;
+ if (k >= '1' && k <= '4') {
+ a.sel = k - '1';
+ a.all = false;
+ a.scroll = 0;
+ collect(&a);
+ continue;
+ }
+ if (k == 'a') {
+ a.all = !a.all;
+ collect(&a);
+ continue;
+ }
+ if (k == 'p') {
+ a.paused = !a.paused;
+ continue;
+ }
+ if (k == 'r') {
+ collect(&a);
+ continue;
+ }
+ if (k == '+' || k == '=') {
+ if (a.interval < 60)
+ a.interval++;
+ continue;
+ }
+ if (k == '-') {
+ if (a.interval > 1)
+ a.interval--;
+ continue;
+ }
+ if (k == '?') {
+ a.help = true;
+ continue;
+ }
+ int rows = visible_rows(&t);
+ int max = a.info[a.sel].nsub - rows;
+ if (max < 0)
+ max = 0;
+ if (k == 'j' || k == TUI_KEY_DOWN) {
+ if (a.scroll < max)
+ a.scroll++;
+ } else if (k == 'k' || k == TUI_KEY_UP) {
+ if (a.scroll > 0)
+ a.scroll--;
+ } else if (k == TUI_KEY_PGDN) {
+ a.scroll += rows;
+ if (a.scroll > max)
+ a.scroll = max;
+ } else if (k == TUI_KEY_PGUP) {
+ a.scroll -= rows;
+ if (a.scroll < 0)
+ a.scroll = 0;
+ } else if (k == 'g' || k == TUI_KEY_HOME) {
+ a.scroll = 0;
+ } else if (k == 'G' || k == TUI_KEY_END) {
+ a.scroll = max;
}
}
- return n;
+ tui_close(&t);
}
-int main(int argc, char **argv) {
- static unsigned char work[WORK] __attribute__((aligned(8)));
- static struct subpool subs[256];
- int area = 1;
- bool hex = false;
+int main(int argc, char **argv)
+{
+ unsigned mask = 0;
+ bool dump = false;
+ bool interactive = false;
+ int interval = 0;
for (int i = 1; i < argc; i++) {
- if (strcmp(argv[i], "-x") == 0) {
- hex = true;
- } else if (strcmp(argv[i], "-a") == 0 && i + 1 < argc) {
- const char *a = argv[++i];
- if (strcmp(a, "csa") == 0) area = 1;
- else if (strcmp(a, "sqa") == 0) area = 2;
- else if (strcmp(a, "lsqa") == 0) area = 3;
- else if (strcmp(a, "pvt") == 0) area = 4;
- else { fprintf(stderr, "ztop: no such area '%s'\n", a); return 2; }
+ if (strcmp(argv[i], "-h") == 0) {
+ usage(stdout);
+ return 0;
+ } else if (strcmp(argv[i], "-x") == 0) {
+ dump = true;
+ } else if (strcmp(argv[i], "-a") == 0) {
+ if (i + 1 >= argc || !parse_areas(argv[++i], &mask)) {
+ usage(stderr);
+ return 2;
+ }
+ } else if (strncmp(argv[i], "-a", 2) == 0 && argv[i][2] != '\0') {
+ if (!parse_areas(argv[i] + 2, &mask)) {
+ usage(stderr);
+ return 2;
+ }
+ } else if (strcmp(argv[i], "-d") == 0) {
+ interactive = true;
+ if (i + 1 < argc && argv[i + 1][0] >= '0' && argv[i + 1][0] <= '9')
+ interval = atoi(argv[++i]);
} else {
- fprintf(stderr, "usage: ztop [-a csa|sqa|lsqa|pvt] [-x]\n");
+ usage(stderr);
return 2;
}
}
+ if (mask == 0)
+ mask = 1u;
- struct zreq req = {.area = area, .len = WORK, .wk = work, .rc = -1};
- /* The stub's return value is not usable through OS linkage; req.rc
- * is what tells whether the service ran. */
- (void)zvslist(&req);
- if (req.rc != 0) {
- fprintf(stderr, "ztop: VSMLIST returned %d\n", req.rc);
- return 1;
- }
- if (hex)
- dump(work, 512);
-
- int n = parse(work, WORK, subs, 256);
- printf("%-8s %10s %8s\n", "subpool", "bytes", "blocks");
- unsigned long long total = 0;
- for (int i = 0; i < n; i++) {
- printf("%-8u %10llu %8u\n", subs[i].id, subs[i].bytes, subs[i].blocks);
- total += subs[i].bytes;
+ if (interactive) {
+ if (dump)
+ (void)fprintf(stderr, "ztop: -x is ignored with -d\n");
+ if (interval < 1)
+ interval = 2;
+ if (interval > 60)
+ interval = 60;
+ run_tui(mask);
+ } else {
+ run_oneshot(mask, dump);
}
- printf("%-8s %10llu\n", "total", total);
- return n > 0 ? 0 : 1;
+ return 0;
}
diff --git a/src/zvslist.s b/src/zvslist.s
index a48ad32..b773b23 100644
--- a/src/zvslist.s
+++ b/src/zvslist.s
@@ -6,11 +6,12 @@
* holding the address of the request block.
*
* struct zreq { int area; unsigned len; void *wk; int rc;
-* void *self; unsigned long long save[18]; int auth; }
+* void *self; unsigned long long save[18]; int space; }
*
-* area 1 CSA, 2 SQA, 3 LSQA, 4 private; len is the work area length;
-* rc receives the service's return code (0 complete, 4 "did not fit,
-* ask again", anything else is an error).
+* area 1 CSA, 2 SQA, 3 LSQA, 4 private; space 1 FREE (allocated storage
+* and the free space inside it), 2 UNALLOC (holes in CSA or the private
+* area); len is the work area length; rc receives the service's own
+* return code (0 complete, 4 "did not fit, ask again", else an error).
*
* Returns 0 if the service was called, -1 for an unknown area.
*
@@ -19,15 +20,18 @@
* and the first store into it takes a 0C4. Only the base register and
* the literal pool stay here, and both are read-only.
*
-* SPACE=ALLOC and LOC=31 are the macro defaults. LINKAGE=SVC is
-* used rather than the default SYSTEM: the PC-number path through
-* CVT+772/+88 branches off to a bad address here, the branch entry
-* does not.
+* LINKAGE=SVC is used rather than the default SYSTEM: the PC-number
+* path through CVT+772/+88 branches off to a bad address here, the
+* branch entry does not.
*
* VSMLIST writes its control block into the first 32 bytes of the work
* area and needs the register operand in doubled parentheses: with
* WKAREA=(4,5) the macro generates "LA 1,4" and stores at address 12.
*
+* The return code at work+0 is the service's own state: it must be zero
+* before the first call, and left alone for the continuation calls.
+* The C side clears it once, before the loop; the stub must not.
+*
* The offsets below are shared with src/ztop.c and checked there.
SYSSTATE AMODE64=NO,ARCHLVL=2,OSREL=ZOSV1R6
zvslist CSECT
@@ -47,7 +51,7 @@ zvslist RMODE 24
L 3,0(2) area
L 5,4(2) work area length
L 4,8(2) work area address
- XC 0(4,4),0(4) return code zero before the call
+ L 6,168(2) space: 1 FREE, 2 UNALLOC
LHI 15,-1
ST 15,12(2) unknown area until proven
CHI 3,1 the area codes are small
@@ -59,13 +63,21 @@ zvslist RMODE 24
CHI 3,4
BE PVT
B OUT
-CSA VSMLIST SP=CSA,WKAREA=((4),5)
+CSA CHI 6,2
+ BE CSAU
+ VSMLIST SP=CSA,WKAREA=((4),5)
+ B DONE
+CSAU VSMLIST SP=CSA,SPACE=UNALLOC,WKAREA=((4),5)
B DONE
SQA VSMLIST SP=SQA,WKAREA=((4),5)
B DONE
LSQA VSMLIST SP=LSQA,WKAREA=((4),5)
B DONE
-PVT VSMLIST SP=PVT,WKAREA=((4),5)
+PVT CHI 6,2
+ BE PVTU
+ VSMLIST SP=PVT,WKAREA=((4),5)
+ B DONE
+PVTU VSMLIST SP=PVT,SPACE=UNALLOC,WKAREA=((4),5)
DONE LR 2,11 the struct, kept in R11
L 4,8(2) work area
L 15,0(4) service return code
diff --git a/tests/demo_tui.c b/tests/demo_tui.c
new file mode 100644
index 0000000..1e9ad1f
--- /dev/null
+++ b/tests/demo_tui.c
@@ -0,0 +1,49 @@
+/* A tiny exercise of src/tui.c: boxes, colors, keys, resize. Not installed;
+ * run it on any terminal with `make demo && ./tests/demo_tui`. */
+#include <stdio.h>
+#include <string.h>
+
+#include "../src/tui.h"
+
+int main(void)
+{
+ struct tui t;
+ int x = 4;
+
+ if (!tui_open(&t)) {
+ (void)fprintf(stderr, "demo_tui: needs a terminal\n");
+ return 77;
+ }
+ for (;;) {
+ int w, h;
+
+ tui_size(&t, &w, &h);
+ tui_clear(&t);
+ tui_fill(&t, 0, 0, w, 1, ' ', TUI_TITLE);
+ tui_printf(&t, 1, 0, w - 2, TUI_TITLE, " tui demo %dx%d", w, h);
+ tui_box(&t, 1, 2, w - 2, h - 4, TUI_LABEL);
+ if (w > 50 && h > 12) {
+ tui_printf(&t, 3, 3, w - 6, TUI_PLAIN,
+ "arrows move the bar, r resizes, q quits");
+ tui_printf(&t, 3, 5, w - 6, TUI_GOOD, "position: %d", x);
+ tui_fill(&t, 3, 7, (w - 8) * x / 20, 1, '#', TUI_GOOD);
+ tui_box(&t, w / 2 - 10, h / 2 - 3, 22, 7, TUI_WARN);
+ tui_printf(&t, w / 2 - 8, h / 2 - 1, 18, TUI_WARN, "a box");
+ } else {
+ tui_printf(&t, 1, 3, w - 2, TUI_BAD, "too small");
+ }
+ tui_flush(&t);
+
+ int k = tui_key(&t, 200);
+ if (k == 'q' || k == 0x03)
+ break;
+ if (k == TUI_KEY_RESIZE || k == 'r')
+ (void)tui_resize(&t);
+ else if (k == TUI_KEY_RIGHT && x < 20)
+ x++;
+ else if (k == TUI_KEY_LEFT && x > 0)
+ x--;
+ }
+ tui_close(&t);
+ return 0;
+}
diff --git a/tests/test_vsm.c b/tests/test_vsm.c
new file mode 100644
index 0000000..0d8d358
--- /dev/null
+++ b/tests/test_vsm.c
@@ -0,0 +1,229 @@
+/* Synthetic VSMLIST data areas, built from the layouts in the z/OS
+ * Authorized Assembler Services Guide. These tests do not need z/OS. */
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "../src/vsm.h"
+
+static unsigned char buf[8192];
+static unsigned len;
+
+static void put8(unsigned v)
+{
+ if (len < sizeof buf)
+ buf[len++] = (unsigned char)v;
+}
+
+static void put32(uint32_t v)
+{
+ put8(v >> 24);
+ put8(v >> 16);
+ put8(v >> 8);
+ put8(v);
+}
+
+static void desc(unsigned id, unsigned flags, uint32_t tcb, unsigned dlen)
+{
+ put8(0);
+ put8(dlen);
+ put8(id);
+ put8(flags);
+ put32(tcb);
+ for (unsigned i = 8; i < dlen; i++)
+ put8(0);
+}
+
+static void block(uint32_t addr, uint32_t blen)
+{
+ put32(addr);
+ put32(blen);
+}
+
+static int fail_line(int line, const char *what)
+{
+ (void)fprintf(stderr, "test_vsm.c:%d: %s\n", line, what);
+ return 1;
+}
+
+#define CHECK(c) do { if (!(c)) return fail_line(__LINE__, #c); } while (0)
+
+static int test_csa(void)
+{
+ struct vsm_info v;
+
+ len = 0;
+ put32(2);
+ desc(227, 0x08, 0, 8);
+ put32(2);
+ block(0x1000, 0x1000);
+ put32(2);
+ block(0x1000, 0x100);
+ block(0x1200, 0x200);
+ block(0x3000, 0x800);
+ put32(0);
+ desc(228, 0x09, 0, 8);
+ put32(1);
+ block(0x4000, 0x400);
+ put32(1);
+ block(0x4000, 0x400);
+
+ vsm_reset(&v, VSM_CSA, VSM_FREE);
+ CHECK(vsm_parse_alloc(&v, buf, len));
+ CHECK(v.nsub == 2);
+ CHECK(v.blocks == 3);
+ CHECK(v.foot == 0x1C00);
+ CHECK(v.free == 0x700);
+ CHECK(v.used == 0x1500);
+ CHECK(v.largest == 0x1000);
+ CHECK(v.sub[0].id == 227);
+ CHECK(v.sub[0].key == 8);
+ CHECK(v.sub[0].used == 0x1500);
+ CHECK(v.sub[0].foot == 0x1800);
+ CHECK(v.sub[0].blocks == 2);
+ CHECK(v.sub[0].lo == 0x1000);
+ CHECK(v.sub[0].hi == 0x3800);
+ CHECK(v.sub[1].id == 228);
+ CHECK(v.sub[1].key == 9);
+ CHECK(v.sub[1].used == 0);
+ CHECK(v.sub[1].free == 0x400);
+ return 0;
+}
+
+static int test_merge(void)
+{
+ struct vsm_info v;
+
+ len = 0;
+ put32(1);
+ desc(227, 0x08, 0, 8);
+ put32(1);
+ block(0x1000, 0x1000);
+ put32(0);
+
+ vsm_reset(&v, VSM_CSA, VSM_FREE);
+ CHECK(vsm_parse_alloc(&v, buf, len));
+ CHECK(vsm_parse_alloc(&v, buf, len));
+ CHECK(v.nsub == 1);
+ CHECK(v.blocks == 2);
+ CHECK(v.used == 0x2000);
+ CHECK(v.sub[0].blocks == 2);
+ return 0;
+}
+
+static int test_pvt(void)
+{
+ struct vsm_info v;
+
+ len = 0;
+ put32(1);
+ put32(0x00F00000);
+ put32(1);
+ desc(0, 0x08, 0x00F00000, 8);
+ put32(1);
+ block(0x100000, 0x2000);
+ put32(0);
+
+ vsm_reset(&v, VSM_PVT, VSM_FREE);
+ CHECK(vsm_parse_alloc(&v, buf, len));
+ CHECK(v.tcbs == 1);
+ CHECK(v.nsub == 1);
+ CHECK(v.sub[0].id == 0);
+ CHECK(v.sub[0].tcb == 0x00F00000);
+ CHECK(v.used == 0x2000);
+ return 0;
+}
+
+static int test_unalloc(void)
+{
+ struct vsm_info v;
+
+ len = 0;
+ put32(2);
+ put32(0x00A00000);
+ put32(0x00100000);
+ put32(2);
+ block(0x00A00100, 0x100);
+ block(0x00A80000, 0x40000);
+ put32(0x00B00000);
+ put32(0x00200000);
+ put32(1);
+ block(0x00B10000, 0x8000);
+
+ vsm_reset(&v, VSM_CSA, VSM_UNALLOC);
+ CHECK(vsm_parse_unalloc(&v, buf, len));
+ CHECK(v.unalloc_blocks == 3);
+ CHECK(v.unalloc == 0x48100);
+ CHECK(v.largest_unalloc == 0x40000);
+ CHECK(v.nregion == 2);
+ CHECK(v.region[0].addr == 0x00A00000);
+ CHECK(v.region[1].len == 0x00200000);
+ return 0;
+}
+
+static int test_bad(void)
+{
+ struct vsm_info v;
+
+ /* descriptor length below the documented minimum */
+ len = 0;
+ put32(1);
+ desc(227, 0, 0, 7);
+ vsm_reset(&v, VSM_CSA, VSM_FREE);
+ CHECK(!vsm_parse_alloc(&v, buf, len));
+
+ /* format byte is not zero */
+ len = 0;
+ put32(1);
+ put8(1);
+ put8(8);
+ put8(227);
+ put8(0);
+ put32(0);
+ put32(0);
+ vsm_reset(&v, VSM_CSA, VSM_FREE);
+ CHECK(!vsm_parse_alloc(&v, buf, len));
+
+ /* an impossible block count must not spin */
+ len = 0;
+ put32(1);
+ desc(227, 0, 0, 8);
+ put32(0x10000000u);
+ vsm_reset(&v, VSM_CSA, VSM_FREE);
+ CHECK(!vsm_parse_alloc(&v, buf, len));
+
+ /* truncated block */
+ len = 0;
+ put32(1);
+ desc(227, 0, 0, 8);
+ put32(1);
+ put8(0);
+ vsm_reset(&v, VSM_CSA, VSM_FREE);
+ CHECK(!vsm_parse_alloc(&v, buf, len));
+
+ /* truncated unallocated block */
+ len = 0;
+ put32(1);
+ put32(0x1000);
+ put32(0x1000);
+ put32(1);
+ put8(0);
+ vsm_reset(&v, VSM_CSA, VSM_UNALLOC);
+ CHECK(!vsm_parse_unalloc(&v, buf, len));
+
+ return 0;
+}
+
+int main(void)
+{
+ int rc = 0;
+
+ rc |= test_csa();
+ rc |= test_merge();
+ rc |= test_pvt();
+ rc |= test_unalloc();
+ rc |= test_bad();
+ if (rc == 0)
+ (void)printf("test_vsm: ok\n");
+ return rc;
+}