# bench The benchmark scripts, and what the numbers cost on z/OS. Each script is also a test: `run.sh` compares every shell's output with the reference shell's, so a wrong answer fails instead of merely being timed. `startup.sh` is the exception: it times one process start, and there is no output to check. ## Running On Linux, `make bench`: the body benchmarks, then the start-up table. On z/OS (ASCII build), from the source directory: ```sh export PATH=/bin:/usr/bin TMPDIR=$HOME/tmp _BPXK_AUTOCVT=ON sh bench/run.sh "$(pwd)/zash" /bin/sh bash zsh oksh sh bench/startup.sh "$(pwd)/zash" /bin/sh bash zsh ``` A file that `git` has just written is tagged UTF-8 on z/OS, and IBM's sh reads that as garbage (`FSUM7332 syntax error`): a benchmark script added or re-checked-out needs its tag first, or run it with zash. ```sh chtag -tc ISO8859-1 bench/startup.sh ``` Three things about that line are not optional: - Every shell is an absolute path. `run.sh` changes to a work directory before running anything, so a relative `./zash` prints `n/a` in every row. - `_BPXK_AUTOCVT=ON`. The scripts are tagged ISO8859-1. Without it, /bin/sh and the other shells cannot read them and fail with `FSUM7332 syntax error: got ), expecting Newline`. zash is not affected: it converts by tag itself. - `TMPDIR` on a writable filesystem. That is where the work directory goes. A full run takes about a minute and is best run detached (`nohup ... > bench.txt 2>&1 rpt.txt ``` The report lists each option, its value, and where it was set: installation default, `CELQUOPT`, or the `_CEE_RUNOPTS` override. Both reports go to the message file, stderr by default. `_CEE_RUNOPTS` is inherited, so any LE child of the process prints its own report; `-c :` keeps that to one. ### None of them does: the cost is a wait No option moves it. Not one, including POSIX: | `_CEE_RUNOPTS` | start | | --- | --- | | defaults | 30.5 ms | | `POSIX(OFF)` | 30.5-31.0 ms | | `TRAP(OFF)` | 29.3 ms | | `RPTOPTS(OFF)` | 30.8 ms | What moves it is the machine. The same binary read 30.5, then 21.5, then 5.8 ms in one session while `tiny64` (1.39), `c32` (1.76) and a CPU calibration loop stayed put: the 64-bit path is waiting for something shared, and its queue is long when the LPAR is busy. The child is charged 1.0-1.25 ms of CPU either way, against 5.8 to 30.5 ms of wall, so the rest is not spent on a processor; and while it is at 30 ms two concurrent 64-bit starts do not overlap at all (54 ms each), where at 5.8 ms they do. Measure it with `bench/le-init.sh` -- z/OS only, since it builds the 31-bit, 64-bit and HLASM programs it times: ```sh make bench # builds build/zos-tools/spawn sh bench/le-init.sh ``` What is still open, and the measurement that had to be thrown away to get this far, is in [notes/investigation.md](../notes/investigation.md). ### It was the LPA The wait is the load. The 64-bit runtime is not LPA-resident by default, so every address space loads and relocates the DLL for itself; put it in the dynamic LPA from the linklist library it already lives in and the cost goes away: ```text SETPROG LPA,ADD,MODNAME=CELQLIB,DSNAME=CEE.SCEERUN2 # + CELQDCPP, CELQDEST, # CELQDMNP, CELQDSNF, # CELQIOTD, CELQMCHK, # CELQV004, IBMHV011 ``` Measured either side of that, 40 starts, milliseconds per start: | | before | after | | --- | --- | --- | | `c32` (`-m32`) | 1.61 | 1.61 | | `c64` (`-m64`) | 5.83 | 1.63 | | `pr64` (`-m64`, `printf`) | 6.06 | 1.79 | | `fork()` + `wait()`, empty parent, `-m64` | 4.20 | 0.10 | | `/bin/zsh`, the 64-bit shell | 31.42 | 2.83 | | two 64-bit starts at once | 2 × 28 ms | 2 × 1.6 ms | So the 27× ratio of this section was a system not running the advice IBM gives in its own 64-bit programming guide, and the address mode keeps its clean record. `zsh` confirms it independently: a 64-bit program nobody here wrote, 31.4 ms to 2.8 ms. The change is gone at the next IPL, which rebuilds the dynamic LPA, so a system that wants this needs the `LPA ADD` in a PROGxx member or those commands in `COMMNDxx`. `README.md` has the permanent form and `notes/investigation.md` the full table. ## Start-up `run.sh` takes CPU time, so the milliseconds a process spends before it runs anything are invisible to every row: `subshell` and `zz-fgsub` can read 0.00 for zash while `zash -c :` costs wall time. Starting a process is mostly waiting for the runtime to come up, which costs almost no CPU. `startup.sh` measures it, by the wall clock, one start per `shell -c :` and one per `shell script`. A cell is the fastest of `BENCH_R` rounds (default 5) of `BENCH_N` starts (default 100), and the two columns of a row are measured one after the other inside each round, so they see the same load. A minimum is the right summary because contention only ever adds time; the comment at the top of the script says why `awk` is not used. On a shared z/OS machine the absolute numbers still move with whatever else the LPAR is running: read a run's rows against each other, not against a table from a different day. It matters for anything that starts zash often: a script with a `#!` line, `zash -c`, a spawned subshell. It does not matter for a long interactive session. Measured with 100 starts each: `-c :` is a bare start, `script` is the kernel starting the shell on a one-line file, which is what a `#!` line does. ## The host's sh `compat.sh` runs every `tests/posix` script under zash and under the shell this host calls `sh`, each in its own directory, and lists the ones whose output or status differ: ```sh sh bench/compat.sh ./zash [/bin/sh] ``` The `.exp` files are a reference shell's output, so this is not a test. It measures how close a script that names `#!/bin/sh` gets to the meaning that name has here, which is the compatibility zash aims for. On z/OS today, 10 of the 38 scripts agree and 28 differ: most differences are wording that POSIX leaves to the shell (an IBM sh says `FSUM7351 not found` where zash says `not found`), and the rest are behaviour worth reading one at a time. Linux numbers are lower-information, because `/bin/sh` there is usually bash while the `.exp` files come from dash. Every one of the 28 has been read, and none of them is a zash bug: zash agrees with dash on all 38 (that is what the suite says), and the falling-out is the IBM shell's. The groups: | Scripts | What IBM sh does | POSIX / other shells | | --- | --- | --- | | charset, collate.ascii, test-ops | `[ a \< b ]` fails with `FSUM6807 expression syntax error` | required by POSIX since 1992 | | functions, func-edge | arithmetic that spans a newline is `FSUM7425 unknown operator` | whitespace, as in every other shell | | readcmd | no `fc`, so `$(fc ...)` is `FSUM7351 not found` | `fc` is a POSIX special builtin | | invoke | a script read from standard input loses its backquotes | — | | builtins, control | `echo -n` prints `-n` as text | `-n` is unspecified but universal | | heredoc | an unterminated here-document at end of file is an error | dash ends it silently, bash warns | | arith, arith-edge | `9223372036854775807 + 1` is a fatal error | overflow is undefined; zash wraps | | edge, expand-edge | quotes inside `${y:-"q r"}` stay literal; unquoted `$@` keeps empty fields | the dash meaning is the usual one | | split | a trailing `IFS` delimiter makes an empty last field | both families drop it | | cmdsub | `: $(exit 7)` is 7 | `:` succeeds, so 0 | | errexit, errexit-edge | `set -e` reaches into `&&` lists and subshells | it does not | | traps | a subshell survives `kill -INT $$`; the script runs on with status 0 instead of dying at 130 | zash and dash die, like the shell itself would | | misc | `echo ~` with `HOME` unset stays `~` | unspecified; ash prints nothing | | misc-builtins | `type nosuch` is status 1 and says `not a shell builtin` | a status over 0; zash says 127, like bash | | params, read-edge, redir, redir-edge, vars | every failure is status 1 | the ash family uses 2 for redirection and assignment errors ([Deliberate differences](../DESIGN.md#deliberate-differences-from-other-shells)) | | printf-ext | bytes 193-194 where zash prints 65-66 | the IBM sh is EBCDIC on that path | The `params, read-edge, redir, redir-edge, vars` row is the only one where the choice is ours rather than IBM's: zash follows the ash family, as `DESIGN.md` says it would, so a script written for ksh on z/OS that tests `$?` against 1 will see 2 for a failed redirection. z/OS, ASCII build, `-m64`, 200 starts: | shell | `-c :` | `script` | | --- | --- | --- | | zash | 1.34 ms | 1.37 ms | | /bin/sh (IBM's) | 2.21 ms | 2.24 ms | | zsh | 2.83 ms | 2.83 ms | zsh is shown both ways on purpose. It used to start in 31.4 ms here, which is the 64-bit start-up cost described above and nothing to do with zsh; with the run-time in the LPA it starts in 2.8, and zash's 1.3 is zash being small. A `#!` line costs about the same as `-c :`: reading one line of script is not what the time goes on. Linux, same framework, 200 starts: | shell | `-c :` | `script` | | --- | --- | --- | | zash | 1.08 ms | 1.15 ms | | oksh | 1.10 ms | 0.92 ms | | dash | 1.21 ms | 1.13 ms | | ksh | 1.50 ms | 2.02 ms | | zsh | 1.58 ms | 1.82 ms | | bash | 16.88 ms | 18.67 ms | Two runs an hour apart differ by as much as 50% on the same binary (an earlier pair read zash 0.53 and 1.08 for `-c :`), so these numbers are for comparing rows within one run, not across machines or days. The clock is `tests/tools/now.c`, ten lines, because `/bin/date` on z/OS has no sub-second format. `BENCH_N` sets the number of starts, and `BENCH_NOW` points at a different clock. ## The line editor The editor is built in, and `editor.py` is what made that decision easy. It measures the two costs the off-the-shelf editor changed, both in the interactive path that `run.sh` and `startup.sh` do not touch, on a pseudo-terminal: `BENCH_N` starts, each timed from just before the fork to the first byte of the prompt, then `BENCH_KEYS` keys typed one at a time, each timed from the write to its echo. ```sh python3 bench/editor.py ./zash dash bash # any shells, it types at each ``` Linux, `-O3`, 20 and then 50 starts, keys medians of 200: | editor | start | one key | | --- | --- | --- | | built-in | 0.94-1.02 ms | 7.2-8.5 us | | libedit | 1.20-1.27 ms | 7.2-7.4 us | libedit, which zash could be built against (`make EDIT=libedit`) until it was dropped, paid about a third of a millisecond more to get to the prompt: three more shared libraries to load (libedit, ncurses, and the terminfo entry it reads), `el_init()`, an `~/.editrc` attempt, and the copy of the shell's history into libedit. Every key costs the same either way; the round trip is the terminal driver and the scheduler, and the editor's own work on a key is too small to see through that. So the built-in editor wins the start that never edits, which is the case `make check` and a `#!` line are; for a session spent typing, the two are the same. Neither forks or execs per key, which is the cost that would have mattered. DESIGN.md records the rest of the reason: FreeBSD and NetBSD `sh` use libedit because it is in their base system, and z/OS has no such thing. ## Address mode: 64-bit now zash is built 64-bit (AMODE 64) XPLINK, which is what the z/OS Makefile targets do. It was 31-bit for a while, because at that time the 64-bit start and `fork()` costs above were paid by every process; they were the start-up load of the 64-bit runtime, and [the LPA change](#is-it-the-address-mode-or-the-c-runtime) removed them. With both widths equal, the default is the width IBM builds for. The same sources build at either width with the same compiler: ```sh ibm-clang ... -O2 -m64 -fzos-le-char-mode=ascii -o zash $(COMMON) src/sys_zos.c ``` (`-m32` is 32-bit XPLINK, `-m32n` is 32-bit non-XPLINK, `-m64` is 64-bit XPLINK. `-m31` does not exist and is rejected.) Measured against `/bin/sh`, CPU seconds, ASCII build, same box: | benchmark | zash `-m64` | zash `-m32` | z/OS sh | | --- | --- | --- | --- | | `jobs` (200 `( : ) &`) | 0.08 | 0.07 | 0.12 | | `zz-bgsub` (200 `( : ) &`) | 0.06 | 0.08 | 0.12 | | `zz-bgcd` (200 `( cd / ) &`) | 0.07 | 0.08 | 0.12 | | `zz-fgcd` (200 `( cd / )` fg) | 0.08 | 0.08 | 0.05 | | `zz-fgsub` (200 `( : )` fg) | 0.00 | 0.00 | 0.05 | | `zz-bgtrue` (200 `/bin/true &`) | 0.07 | 0.05 | 0.11 | | `zz-bgsh` (200 `/bin/sh -c : &`) | 0.07 | 0.05 | 0.13 | The rows that fork improve by a factor of ten and stop being losses, except `zz-fgcd`, which is 0.08 against `sh` 0.05 and was 0.84 before. Nothing else moves: `report` 0.18 → 0.17, `deploy` 0.15 → 0.16, `spawn` 0.47 → 0.49, `cmdsub` 0.04 → 0.04, `strings` 0.28 → 0.27, `printf` 1.17 → 1.19. The `-m32` column is the earlier run, taken before the last speed-ups; the `-m64` column and the `sh` column are from one later run on the same box. The two widths are within noise of each other on every row, which is what [It was the LPA](#it-was-the-lpa) predicts. Two things had to be settled for the 31-bit build, and the first one is still the open question at 64-bit: 1. The catalog search. It used to need `zos_csi.s`, an `AMODE 64` routine that switched modes with `SAM31`/`SAM64` around IGGCSI00 and passed a `__malloc31` block. At 31-bit the C code called IGGCSI00 directly, with `fetch()` and `#pragma linkage(..., OS_UPSTACK)`, and the assembler was gone. Two traps cost a day between them: under `-m32` plain `OS` means `OS_NOSTACK` and the call takes a program check (use `OS_UPSTACK`), and the name given to `fetch()` must be an ordinary literal, because `__fetch_a` converts it and EBCDIC bytes get converted twice. `OS_UPSTACK` does not exist at `-m64`, so the 64-bit build declares the module with plain `OS` linkage, which is the mode-switching glue the compiler reference describes for a 64-bit XPLINK call to an AMODE 31 routine. Neither IGGCSI00 nor BPXWDY2 loads on the machine these numbers come from, so on this system that path is untested; if it abends, `csi_call()` catches the program check and completion falls back to file names. 2. Address space. A 31-bit shell was limited to storage below 2 GB, which is plenty for a shell but not for `x=$( zash.amblist # note the leading blank grep -nE 'AMODE|RMODE|ENTRY POINT' zash.amblist ``` `make zos-amblist` does this for `./zash`. At 31-bit there is no assembler CSECT left to check, so the listing should show one member and `AMODE 31` throughout. ### What storage does the program actually use? This is the input to fork cost, and the reason a big shell forks more slowly than a small one: ```sh _CEE_RUNOPTS='RPTSTG(ON)' ./zash -c : 2>rpt.txt _CEE_RUNOPTS='RPTOPTS(ON)' ./zash -c : 2>opts.txt ``` RPTSTG reports heap and stack use per enclave; RPTOPTS reports every run-time option, its value, and where it was set. Both go to the message file, stderr by default. ### Where is the shell spending CPU? `dbx` works on z/OS and is the target of `make zos-debug` and `make zos-dbx`. The costs above are not CPU: nearly all of the 28 ms is a wait, so a profile taken inside zash will not show them. That is why the probes here are separate programs rather than shell built-ins. ### What did the compiler emit? `ibm-clang -S` gives assembly, and `-fsave-optimization-record` (or `-Rpass=`) gives clang's remarks about what was and was not optimised. ## Reading the two tables `n/a` means the shell lacks the feature the benchmark uses. `differs` means it computed something else, and the run says so. `port bug` marks the third-party oksh port's own breakage (`echo hi | cat` prints garbage there), not anything zash did. Rows that create processes move by a few hundredths between runs, and the fork-heavy ones move more, so treat a 0.01 to 0.02 difference as noise and re-run before believing it.