diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 22:16:54 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 22:16:54 -0600 |
| commit | a1cf90eb067effe8a7c85f5d2024a0c3d22817e1 (patch) | |
| tree | 329d80de9538bec914092e55833a2fc7d7009bb9 | |
| parent | cef8eb44791f24b3d50006690960dce84779cfea (diff) | |
| download | ztop-a1cf90eb067effe8a7c85f5d2024a0c3d22817e1.tar.gz | |
leakcheck: let Language Environment look for a leak
RPTSTG, RPTOPTS and HEAPCHK will not find what the program never asks
for, but they do see every get and every free. Every one-shot mode now
runs under them and the storage report is checked for a heap request
that was never returned.
| -rw-r--r-- | Makefile | 27 | ||||
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | src/ztop.c | 156 | ||||
| -rw-r--r-- | tests/leak.awk | 39 | ||||
| -rw-r--r-- | tui/tui.c | 66 | ||||
| -rw-r--r-- | tui/tui.h | 17 |
6 files changed, 242 insertions, 64 deletions
@@ -48,6 +48,14 @@ TESTCC = cc TESTFLAGS = -std=c17 -g -O1 -Wall -Wextra -Wshadow -Wpedantic \ -D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200112L +# Language Environment sees every heap request the program makes. Its +# storage report is therefore the leak test a z/OS user can run: if the +# gets and the frees differ by the end of the enclave, something was +# allocated and never freed. HEAPCHK(ON) additionally walks the heap at +# every call, so a double free or an overlay abends instead of hiding. +LEAKDIR = $(HOME)/tmp/ztop/leak +LEAKAWK = tests/leak.awk + all: @(uname -s | grep OS/390 > /dev/null) || \ { echo "ztop builds only on z/OS; this tree is a backup copy"; exit 1; } @@ -76,5 +84,22 @@ test: tests/test_vsm.c tests/test_sys.c src/vsm.c src/sys.c tui/test_tui.c tui/t demo: tui/demo.c tui/tui.c $(TESTCC) $(TESTFLAGS) -Itui -o tui/demo tui/demo.c tui/tui.c +leakcheck: ztop + @mkdir -p $(LEAKDIR) + @set -e; opts='RPTSTG(ON),RPTOPTS(ON),HEAPCHK(ON)'; \ + for a in csa sqa lsqa pvt; do \ + _CEE_RUNOPTS=$$opts ./ztop -a $$a \ + > $(LEAKDIR)/$$a.out 2> $(LEAKDIR)/$$a.log; \ + printf '%-5s ' $$a; \ + awk $(LEAKAWK) $(LEAKDIR)/$$a.log; \ + done + @set -e; opts='RPTSTG(ON),RPTOPTS(ON),HEAPCHK(ON)'; \ + for m in -s -j -p; do \ + _CEE_RUNOPTS=$$opts ./ztop $$m \ + > $(LEAKDIR)/$$m.out 2> $(LEAKDIR)/$$m.log; \ + printf '%-5s ' $$m; \ + awk $(LEAKAWK) $(LEAKDIR)/$$m.log; \ + done + clean: - rm -f ztop $(OBJ) tests/test_vsm tui/test_tui tui/demo + rm -f ztop $(OBJ) tests/test_vsm tests/test_sys tui/test_tui tui/demo @@ -41,6 +41,7 @@ z/OS for the real binary, with IBM Open XL in the path: ```sh make # ibm-clang -std=c23 -m32; the .S is assembled by HLASM make check # storage, system and jobs +make leakcheck # every mode under the LE storage and heap checks ``` The parser, the library and `sys.c`'s structure have no z/OS dependency, @@ -498,16 +498,23 @@ static void collect(struct app *a) { static void draw_header(struct tui *t, const struct app *a) { char now[16]; + char right[64]; time_t clock_now = time(NULL); struct tm tm; (void)localtime_r(&clock_now, &tm); (void)strftime(now, sizeof now, "%H:%M:%S", &tm); - tui_fill(t, 0, 0, t->w, 1, ' ', TUI_TITLE); - tui_printf(t, 1, 0, t->w - 30, TUI_TITLE, " ztop %s %s %s collect %dms ", - ZTOP_VERSION, a->host, now, a->collect_ms); + 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); + (void)snprintf(right, sizeof right, "collect %dms %s", a->collect_ms, + now); + tui_printf(t, t->w - 1 - (int)strlen(right), 0, (int)strlen(right), + TUI_PLAIN, "%s", right); if (a->paused) - tui_put(t, t->w - 9, 0, " PAUSED ", 8, TUI_WARN); + tui_printf(t, t->w - 9 - (int)strlen(right), 0, 8, TUI_WARN, + " PAUSED "); } static void draw_tabs(struct tui *t, const struct app *a) { @@ -515,7 +522,7 @@ static void draw_tabs(struct tui *t, const struct app *a) { "4 PAGING"}; int x = tui_tabs_draw(t, 0, 1, labels, 4, a->tab); - tui_printf(t, x + 1, 1, t->w - x - 2, TUI_LABEL, "refresh %ds +/-", + tui_printf(t, x + 1, 1, t->w - x - 2, TUI_META, "refresh %ds +/-", a->interval); } @@ -545,10 +552,10 @@ 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_TITLE : TUI_LABEL, " CSA "); - tui_printf(t, 7, 3, 6, a->sub == 1 ? TUI_TITLE : TUI_LABEL, " SQA "); - tui_printf(t, 13, 3, 6, a->sub == 2 ? TUI_TITLE : TUI_LABEL, " LSQA "); - tui_printf(t, 20, 3, 6, a->sub == 3 ? TUI_TITLE : TUI_LABEL, " PVT "); + 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 "); if (!a->have[a->sub]) { tui_printf(t, 1, 6, t->w - 2, TUI_BAD, "%s: %s", area_upper[a->sub], @@ -565,9 +572,15 @@ static void draw_storage(struct tui *t, struct app *a) { (void)snprintf(total, sizeof total, "-"); (void)snprintf(pcbuf, sizeof pcbuf, "-"); } - tui_printf(t, 1, 4, t->w - 2, TUI_PLAIN, - "used %s free %s mapped %s total %s use %s blocks %u", used, - freed, mapped, total, pcbuf, v->blocks); + tui_printf(t, 1, 4, t->w - 16, TUI_PLAIN, + "used %s free %s mapped %s total %s use ", used, freed, + mapped, total); + tui_printf(t, t->w - 5 - 12, 4, 4, + 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); @@ -581,58 +594,105 @@ static void draw_storage(struct tui *t, struct app *a) { (unsigned long long)v->largest_unalloc); } +/** @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); +} + +/** @brief An accent heading with a dim rule filling the row. */ +static void section(struct 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); +} + static void draw_system(struct tui *t, const struct app *a) { const struct zsys *z = &a->sys; + char b1[32], b2[32]; + int half = t->w / 2; if (!a->have_sys) { tui_printf(t, 1, 4, t->w - 2, TUI_BAD, "%s", a->sys_err); return; } - tui_printf(t, 1, 4, t->w - 2, TUI_LABEL, "system %s", z->sysname); - tui_printf(t, 1, 5, t->w - 2, TUI_PLAIN, "host %s", z->host); - tui_printf(t, 1, 6, t->w - 2, TUI_PLAIN, "os %s", z->os); - tui_printf(t, 1, 7, t->w - 2, TUI_PLAIN, "machine %s", z->machine); - tui_printf(t, 1, 8, t->w - 2, TUI_PLAIN, "spaces %d used, %u slots", - z->njobs, z->slots); - if (z->ipl_ok) - tui_printf(t, 1, 9, t->w - 2, TUI_PLAIN, - "ipl %04u-%02u-%02u %02u:%02u:%02u", z->ipl.year, - z->ipl.month, z->ipl.day, z->ipl.hour, z->ipl.minute, - z->ipl.second); - tui_printf(t, 1, 10, t->w - 2, TUI_PLAIN, "real %llu K online at IPL", - (unsigned long long)z->real_k); - tui_printf( - t, 1, 12, t->w - 2, TUI_LABEL, - "storage CSA %llu unallocated, PVT %llu", - (unsigned long long)(a->info[0].unalloc_valid ? a->info[0].unalloc : 0), - (unsigned long long)(a->info[3].unalloc_valid ? a->info[3].unalloc - : 0)); + human(z->real_k * 1024, b1, sizeof b1); + section(t, 4, "identity"); + kv(t, 1, 5, half - 1, "system", z->sysname, TUI_ACCENT); + kv(t, half, 5, t->w - half - 1, "host", z->host, TUI_PLAIN); + kv(t, 1, 6, half - 1, "release", z->os, TUI_PLAIN); + kv(t, half, 6, t->w - half - 1, "machine", z->machine, TUI_PLAIN); + + section(t, 8, "ipl"); + if (z->ipl_ok) { + (void)snprintf(b1, sizeof b1, "%04u-%02u-%02u", z->ipl.year, + z->ipl.month, z->ipl.day); + (void)snprintf(b2, sizeof b2, "%02u:%02u:%02u", z->ipl.hour, + z->ipl.minute, z->ipl.second); + kv(t, 1, 9, half - 1, "date", b1, TUI_PLAIN); + kv(t, half, 9, t->w - half - 1, "time", b2, TUI_PLAIN); + } + + section(t, 11, "storage"); + human(z->real_k * 1024, b1, sizeof b1); + kv(t, 1, 12, half - 1, "real", b1, TUI_PLAIN); + (void)snprintf(b2, sizeof b2, "%d used / %u slots", z->njobs, z->slots); + kv(t, half, 12, t->w - half - 1, "address spaces", b2, TUI_PLAIN); + if (a->info[0].unalloc_valid) { + human(a->info[0].unalloc, b1, sizeof b1); + kv(t, 1, 13, half - 1, "csa unallocated", b1, TUI_PLAIN); + } + if (a->info[3].unalloc_valid) { + human(a->info[3].unalloc, b1, sizeof b1); + kv(t, half, 13, t->w - half - 1, "pvt unallocated", b1, TUI_PLAIN); + } } static void draw_paging(struct tui *t, const struct app *a) { const struct zpage *p = &a->page; + char b[48]; + int half = t->w / 2; if (!a->have_page) { tui_printf(t, 1, 4, t->w - 2, TUI_BAD, "%s", a->page_err); return; } - tui_printf(t, 1, 4, t->w - 2, TUI_LABEL, "aux storage ASMVT %08X", - (unsigned int)p->asmvt); - tui_printf(t, 1, 6, t->w - 2, p->common_pds ? TUI_GOOD : TUI_WARN, - "common data set %-3s %s", p->common_pds ? "yes" : "no", - p->common_full ? "full" : ""); - tui_printf(t, 1, 7, t->w - 2, p->plpa_pds ? TUI_GOOD : TUI_WARN, - "plpa data set %-3s %s", p->plpa_pds ? "yes" : "no", - p->plpa_full ? "full" : ""); - tui_printf(t, 1, 8, t->w - 2, TUI_PLAIN, "local paging %s", - p->local_paging ? "on" : "off"); - tui_printf(t, 1, 9, t->w - 2, - p->quick_start || p->warm_start ? TUI_WARN : TUI_PLAIN, - "start %s%s", p->quick_start ? "quick" : "cold", - p->warm_start ? ", warm VIO" : ""); - tui_printf(t, 1, 10, t->w - 2, TUI_PLAIN, - "vio journal %s, %u control intervals", - p->vio_journaling ? "active" : "off", p->vio_ci); + section(t, 4, "auxiliary storage"); + (void)snprintf(b, sizeof b, "%08X", (unsigned int)p->asmvt); + kv(t, 1, 5, half - 1, "asmvt", b, TUI_META); + + section(t, 7, "page data sets"); + (void)snprintf(b, sizeof b, "%s%s", p->common_pds ? "yes" : "no", + p->common_full ? ", full" : ""); + kv(t, 1, 8, half - 1, "common", b, + p->common_full ? TUI_BAD + : p->common_pds ? TUI_GOOD + : TUI_WARN); + (void)snprintf(b, sizeof b, "%s%s", p->plpa_pds ? "yes" : "no", + p->plpa_full ? ", full" : ""); + kv(t, half, 8, t->w - half - 1, "plpa", b, + p->plpa_full ? TUI_BAD + : p->plpa_pds ? TUI_GOOD + : TUI_WARN); + kv(t, 1, 9, half - 1, "local paging", p->local_paging ? "on" : "off", + p->local_paging ? TUI_GOOD : TUI_WARN); + (void)snprintf(b, sizeof b, "%s%s", p->quick_start ? "quick" : "cold", + p->warm_start ? ", warm VIO" : ""); + kv(t, half, 9, t->w - half - 1, "start", b, + p->quick_start || p->warm_start ? TUI_WARN : TUI_PLAIN); + + section(t, 11, "vio journal"); + kv(t, 1, 12, half - 1, "state", p->vio_journaling ? "active" : "off", + p->vio_journaling ? TUI_GOOD : TUI_META); + (void)snprintf(b, sizeof b, "%u", p->vio_ci); + kv(t, half, 12, t->w - half - 1, "control intervals", b, TUI_PLAIN); } static void draw_jobs(struct tui *t, struct app *a) { diff --git a/tests/leak.awk b/tests/leak.awk new file mode 100644 index 0000000..f68c05e --- /dev/null +++ b/tests/leak.awk @@ -0,0 +1,39 @@ +# Read the storage report Language Environment writes at termination +# (RPTSTG(ON)) and fail when the heap was not given back: a get without +# its free, or a created heap without its discard, is a leak. +# +# awk -f tests/leak.awk < storage-report + +/^(HEAP|HEAP24|ANYHEAP|BELOWHEAP|THREADHEAP|Additional Heap) statistics:/ { + inheap = 1 +} + +/^End of Storage Report/ { + inheap = 0 +} + +inheap && /^Successful Get Heap requests:/ { + get += $NF +} + +inheap && /^Successful Free Heap requests:/ { + fre += $NF +} + +inheap && /^Successful Create Heap requests:/ { + cre += $NF +} + +inheap && /^Successful Discard Heap requests:/ { + dis += $NF +} + +END { + if (get != fre || cre != dis) { + printf "LEAK: gets %d frees %d, create %d discard %d\n", get, fre, + cre, dis + exit 1 + } + printf "heap gets %d frees %d, create %d discard %d\n", get, fre, cre, + dis +} @@ -371,6 +371,11 @@ void tui_printf(struct tui *t, int x, int y, int maxw, tui_style st, } void tui_box(struct tui *t, int x, int y, int w, int h, tui_style st) { + tui_box_frame(t, x, y, w, h, st, NULL, TUI_PLAIN); +} + +void tui_box_frame(struct tui *t, int x, int y, int w, int h, tui_style st, + const char *title, tui_style title_st) { if (w < 2 || h < 2) return; for (int i = 1; i < w - 1; i++) { @@ -385,6 +390,16 @@ void tui_box(struct tui *t, int x, int y, int w, int h, tui_style 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); + if (title != NULL && title[0] != '\0' && w > 6) { + char chip[64]; + int n = (int)strlen(title); + int room = w - 6; + + if (n > room) + n = room; + (void)snprintf(chip, sizeof chip, " %.*s ", n, title); + tui_printf(t, x + 2, y, (int)strlen(chip), title_st, "%s", chip); + } } void tui_flush(struct tui *t) { @@ -453,7 +468,7 @@ int tui_tabs_draw(struct tui *t, int x, int y, const char *const *labels, int n, labels[i]); x += len; if (i + 1 < n) { - tui_put(t, x, y, " ", 1, TUI_PLAIN); + tui_put(t, x, y, "|", 1, TUI_META); x += 1; } } @@ -907,29 +922,50 @@ bool tui_input_key(struct tui_input *in, int key) { /* ---- progress and status -------------------------------------------- */ void tui_progress(struct tui *t, int x, int y, int width, int pct) { - if (width < 1) + tui_style st; + int fill, room, i; + + if (width < 4) return; - int fill = pct < 0 ? 0 : pct * width / 100; + room = width - 2; + fill = pct < 0 ? 0 : pct * room / 100; + if (fill > room) + fill = room; + st = pct < 0 ? TUI_META + : pct < 70 ? TUI_GOOD + : pct < 85 ? TUI_WARN + : TUI_BAD; + tui_put(t, x, y, "[", 1, TUI_META); + for (i = 0; i < room; i++) + tui_put(t, x + 1 + i, y, i < fill ? "=" : " ", 1, + i < fill ? st : TUI_META); + tui_put(t, x + width - 1, y, "]", 1, TUI_META); +} + +void tui_hints(struct tui *t, int x, int y, int maxw, + const struct tui_hint *hints, int n) { + for (int i = 0; i < n; i++) { + int kl = (int)strlen(hints[i].key); + int ll = (int)strlen(hints[i].label); - if (fill > width) - fill = width; - tui_style st = pct < 0 ? TUI_STYLE(7, 0, TUI_DIM) - : pct < 70 ? TUI_GOOD - : pct < 85 ? TUI_WARN - : TUI_BAD; - for (int i = 0; i < width; i++) - tui_put(t, x + i, y, i < fill ? "#" : "-", 1, - i < fill ? st : TUI_STYLE(7, 0, TUI_DIM)); + if (x + kl + ll + 4 > maxw) + break; + tui_put(t, x, y, " ", 1, TUI_META); + tui_printf(t, x + 1, y, kl, TUI_KEYCAP, "%s", hints[i].key); + tui_put(t, x + 1 + kl, y, " ", 1, TUI_META); + tui_printf(t, x + 2 + kl, y, ll, TUI_META, "%s", hints[i].label); + x += kl + ll + 3; + } } void tui_statusbar(struct tui *t, const char *left, const char *right) { int y = t->h - 1; - tui_fill(t, 0, y, t->w, 1, ' ', TUI_TITLE); + tui_fill(t, 0, y, t->w, 1, ' ', TUI_BAR); if (left != NULL) - tui_printf(t, 1, y, t->w - 2, TUI_TITLE, "%s", left); + tui_printf(t, 1, y, t->w - 2, TUI_PLAIN, "%s", left); if (right != NULL) { int n = (int)strlen(right); - tui_printf(t, t->w - 1 - n, y, n, TUI_TITLE, "%s", right); + tui_printf(t, t->w - 1 - n, y, n, TUI_PLAIN, "%s", right); } } @@ -50,6 +50,10 @@ typedef struct { #define TUI_BAD TUI_STYLE(1, 0, TUI_BOLD) #define TUI_TAB_ON TUI_STYLE(0, 7, TUI_BOLD) #define TUI_TAB_OFF TUI_STYLE(7, 0, TUI_DIM) +#define TUI_ACCENT TUI_STYLE(6, 0, TUI_BOLD) +#define TUI_META TUI_STYLE(7, 0, TUI_DIM) +#define TUI_KEYCAP TUI_STYLE(0, 6, 0) +#define TUI_BAR TUI_STYLE(7, 4, 0) enum { TUI_KEY_NONE = -1, @@ -110,6 +114,9 @@ void tui_put(struct tui *t, int x, int y, const char *s, int maxw, 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); +/** @brief A box with a title set into its top border. */ +void tui_box_frame(struct tui *t, int x, int y, int w, int h, tui_style st, + const char *title, tui_style title_st); void tui_rule(struct tui *t, int y, char ch, tui_style st); void tui_flush(struct tui *t); @@ -189,6 +196,16 @@ int tui_tabs_hit(int x, const char *const *labels, int n); /** Cycle tabs with left/right/tab/backtab; true when it was consumed. */ bool tui_tabs_key(int key, int *active, int n); +/** @brief A key cap and its label, for the status bar. */ +struct tui_hint { + const char *key; + const char *label; +}; + +/** @brief Draw key caps and their labels along one row. */ +void tui_hints(struct tui *t, int x, int y, int maxw, + const struct tui_hint *hints, int n); + /** A single-line text input. */ struct tui_input { char buf[256]; |
