summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-25 22:19:20 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-25 22:19:20 -0600
commit202bd66a5125e8159efbe1a4fd34aa229640a4ff (patch)
tree344d1d0bc0408b3fe7116135fcb25e38fc7d2334
parent480f189d76575a917797fbb293af98bf50f65974 (diff)
downloadztop-202bd66a5125e8159efbe1a4fd34aa229640a4ff.tar.gz
leakcheck: a shell check of the LE report, with a selftest
The old z/OS awk rejects comments and trips over the program file, so the report is summed with sed and shell arithmetic instead. --selftest feeds it one clean and one leaking report so the checker itself is tested.
-rw-r--r--Makefile21
-rw-r--r--tests/leak.awk23
-rw-r--r--tests/leakcheck.sh75
3 files changed, 78 insertions, 41 deletions
diff --git a/Makefile b/Makefile
index 675aea4..6d450bc 100644
--- a/Makefile
+++ b/Makefile
@@ -53,11 +53,9 @@ TESTFLAGS = -std=c17 -g -O1 -Wall -Wextra -Wshadow -Wpedantic \
# 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 = -f tests/leak.awk
all:
- @(uname -s | grep OS/390 > /dev/null) || \
+ @uname -s | grep OS/390 > /dev/null || \
{ echo "ztop builds only on z/OS; this tree is a backup copy"; exit 1; }
$(MAKE) ztop
@@ -80,26 +78,13 @@ test: tests/test_vsm.c tests/test_sys.c src/vsm.c src/sys.c tui/test_tui.c tui/t
./tests/test_sys
$(TESTCC) $(TESTFLAGS) -Itui -o tui/test_tui tui/test_tui.c tui/tui.c
./tui/test_tui
+ @sh tests/leakcheck.sh --selftest
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
+ @sh tests/leakcheck.sh ./ztop
clean:
rm -f ztop $(OBJ) tests/test_vsm tests/test_sys tui/test_tui tui/demo
diff --git a/tests/leak.awk b/tests/leak.awk
deleted file mode 100644
index 7e630e1..0000000
--- a/tests/leak.awk
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- if ($0 == "HEAP statistics:" || $0 == "HEAP24 statistics:" ||
- $0 == "ANYHEAP statistics:" || $0 == "BELOWHEAP statistics:" ||
- $0 == "THREADHEAP statistics:" || $0 == "Additional Heap statistics:")
- inheap = 1
- if ($0 == "End of Storage Report")
- inheap = 0
- if (inheap && index($0, "Successful Get Heap requests:"))
- get += $NF
- if (inheap && index($0, "Successful Free Heap requests:"))
- fre += $NF
- if (inheap && index($0, "Successful Create Heap requests:"))
- cre += $NF
- if (inheap && index($0, "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
-}
diff --git a/tests/leakcheck.sh b/tests/leakcheck.sh
new file mode 100644
index 0000000..6192772
--- /dev/null
+++ b/tests/leakcheck.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+# Language Environment sees every heap request the program makes, so its
+# storage report is a leak test: run each one-shot mode under RPTSTG(ON),
+# RPTOPTS(ON) and HEAPCHK(ON), then read the report back. A get without
+# its free, or a created heap without its discard, is a leak.
+#
+# tests/leakcheck.sh ./ztop run the modes and check each report
+# tests/leakcheck.sh --report FILE check one saved report
+# tests/leakcheck.sh --selftest check the checker
+set -eu
+
+count() {
+ sed -n "s/^$1: *//p" "$2" | {
+ n=0
+ while read -r v; do
+ n=$((n + v))
+ done
+ echo "$n"
+ }
+}
+
+report() {
+ log=$1
+ if ! grep -q 'Storage Report' "$log"; then
+ echo "no storage report in $log"
+ return 1
+ fi
+ g=$(count "Successful Get Heap requests" "$log")
+ f=$(count "Successful Free Heap requests" "$log")
+ c=$(count "Successful Create Heap requests" "$log")
+ d=$(count "Successful Discard Heap requests" "$log")
+ if [ "$g" -ne "$f" ] || [ "$c" -ne "$d" ]; then
+ echo "LEAK: gets $g frees $f, create $c discard $d"
+ return 1
+ fi
+ echo "heap gets $g frees $f, create $c discard $d"
+}
+
+if [ "${1:-}" = "--report" ]; then
+ report "$2"
+ exit
+fi
+
+dir=${HOME}/tmp/ztop/leak
+
+if [ "${1:-}" = "--selftest" ]; then
+ mkdir -p "$dir"
+ printf 'STORAGE Report\nHEAP statistics:\nSuccessful Get Heap requests: 3\nSuccessful Free Heap requests: 3\nSuccessful Create Heap requests: 1\nSuccessful Discard Heap requests: 1\nEnd of Storage Report\n' > "$dir/clean.log"
+ printf 'STORAGE Report\nHEAP statistics:\nSuccessful Get Heap requests: 5\nSuccessful Free Heap requests: 4\nSuccessful Create Heap requests: 0\nSuccessful Discard Heap requests: 0\nEnd of Storage Report\n' > "$dir/leak.log"
+ report "$dir/clean.log"
+ if report "$dir/leak.log"; then
+ echo "selftest: a leak was not reported"
+ exit 1
+ fi
+ echo "leakcheck: ok"
+ exit
+fi
+
+bin=${1:-./ztop}
+mkdir -p "$dir"
+opts='RPTSTG(ON),RPTOPTS(ON),HEAPCHK(ON)'
+run() {
+ name=$1
+ shift
+ _CEE_RUNOPTS=$opts "$bin" "$@" > "$dir/$name.out" 2> "$dir/$name.log"
+ printf '%-5s ' "$name"
+ report "$dir/$name.log"
+}
+
+for a in csa sqa lsqa pvt; do
+ run "$a" -a "$a"
+done
+run -s -s
+run -j -j
+run -p -p