# 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 | 3.60 | | two 64-bit starts at once | 2 × 28 ms | 1.6 ms each | 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 3.6 ms. The change is gone at the next IPL — the dynamic LPA is rebuilt — so a system that wants this needs an `IEALPAxx` list or those commands in `COMMNDxx`. See [notes/investigation.md](../notes/investigation.md) for 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`. 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. z/OS, ASCII build, `-m32`: | shell | `-c :` | `script` | | --- | --- | --- | | zash | 1.33 ms | 1.34 ms | | /bin/sh (IBM's) | 2.20 ms | 2.23 ms | | zsh | 31.42 ms | 31.62 ms | zash starts a little faster than the sh it replaces, and both are an order of magnitude faster than zsh, which pays the 64-bit start-up cost described above. A `#!` line costs about the same as `-c :`: reading one line of script is not what the time goes on. Linux, same framework: | shell | `-c :` | `script` | | --- | --- | --- | | zash | 0.51 ms | 0.49 ms | | dash | 0.54 ms | 0.57 ms | | oksh | 0.75 ms | 0.75 ms | | ksh | 1.00 ms | 1.03 ms | | bash | 8.45 ms | 8.74 ms | 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. ## 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.90 | 0.07 | 0.12 | | `zz-bgsub` (200 `( : ) &`) | 0.86 | 0.08 | 0.12 | | `zz-bgcd` (200 `( cd / ) &`) | 0.85 | 0.08 | 0.12 | | `zz-fgcd` (200 `( cd / )` fg) | 0.84 | 0.08 | 0.05 | | `zz-fgsub` (200 `( : )` fg) | 0.00 | 0.00 | 0.04 | | `zz-bgtrue` (200 `/bin/true &`) | 0.06 | 0.05 | 0.10 | | `zz-bgsh` (200 `/bin/sh -c : &`) | 0.06 | 0.05 | 0.14 | The rows that fork improve by a factor of ten and stop being losses. Nothing else moves: `report` 0.18 → 0.16, `deploy` 0.15 → 0.14, `spawn` 0.47 → 0.45, `cmdsub` 0.04 → 0.04, `strings` 0.28 → 0.25, `printf` 1.17 → 1.15. (These rows are the 31-bit run. Re-measured 64-bit with the run-time in the LPA, the same rows are 0.25, 0.07, 0.04, 0.28, 0.48 and 1.17; nothing regressed. `zz-bgcd` 0.06 against `sh` 0.12, `zz-bgsub` 0.08 against 0.12.) 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.