diff options
Diffstat (limited to 'DESIGN.md')
| -rw-r--r-- | DESIGN.md | 201 |
1 files changed, 146 insertions, 55 deletions
@@ -1,9 +1,11 @@ <p align="center"> - <img src="packaging/zash-logo.svg" alt="zash" width="128"> + <img src="packaging/zash-logo.svg" alt="zash" width="220"> </p> # zash design +<p align="center">Sean Kurtz</p> + This is the reference for how zash is built and why. It records decisions, not a wish list. Where something is planned but not built, or written but not yet verified on z/OS, it says so. @@ -88,7 +90,13 @@ creation and character encoding. (`/** @brief ... */`) at its definition. Struct members and enum constants are documented with `/**< ... */`. Prototypes in headers are not documented a second time. Longer explanations use Markdown. - `make doc` runs Doxygen if it is installed. + `make doc` runs Doxygen if it is installed, and `make docs` renders this + file and README.md to HTML and PDF in `docs/`, which is generated and + not committed. The PDF comes from the HTML through a headless browser, + so no LaTeX installation is needed. The colours are the ones + `formal/zash-spec.typ` uses, and the README's logo is the colour one + there; the monochrome pair exists for the browsers' dark mode on the + repository page. - `.clang-tidy` and `.clangd` are provided, and `make tidy` and `make compdb` drive them. @@ -145,6 +153,41 @@ A forked child can never share the address space. So the rule is: > process is needed, use `spawn()`. Use `fork()` only when the language > requires a copy of the shell itself.** +Measured on z/OS, with the same compiler and source in both address +modes, several of these assumptions turn out to need qualifying: + +| | `-m64` | `-m32` | +| --- | --- | --- | +| `fork()` + `wait()`, empty parent | 4.2 ms | 0.10 ms | +| `fork()` + `wait()`, 64 MB touched | 28.2 ms | 27.7 ms | +| start a trivial program | 29.5 ms | 1.1 ms | + +So there are two independent costs, and only one of them is about +copying: + +- **A fixed penalty for being 64-bit**: about 4 ms per fork and 28 ms per + program start, whatever the program does or holds. This is what makes + `fork()` expensive here, and it is not storage-related, not program + load, and not the sticky bit. +- **A copy cost both modes pay**, about 0.4 ms per MB of touched private + storage. At 64 MB it swamps the address-mode difference and the two + modes converge. + +Two consequences. The rule above still holds — `spawn()` costs no more +than `fork()` and avoids the copy — but the reason usually given for it, +that z/OS `fork()` is inherently expensive, is true only of the copy, +and only above a few MB. And the three rows that zash used to lose on +z/OS (`( : ) &`, `( cd / ) &`, `( cd / )`) are lost to the address mode, +not to the plan: they would be won without changing the plan at all. + +The start-up figure does not appear in `make bench`, which measures CPU +and not wall time, but it is what a `#!` script or a `zash -c` pays. +LE's `HEAP64`/`HEAPPOOLS64`/`STACK64` settings make no measurable +difference to either cost, so there is nothing to tune there. + +[bench/README.md](bench/README.md) has the numbers, the probe programs +and the method. + ### What each construct costs | Construct | Mechanism | @@ -372,6 +415,13 @@ default dispositions for anything the shell catches (automatic on exec) and for anything the shell ignores for its own purposes (passed to `spawn()` explicitly). +On z/OS the shell sets `_EDC_SIG_DFLT=1` in its own environment at +start-up (unless the user set it). Language Environment otherwise prints +`CEE5208S The signal SIGxxx was received.` on stderr whenever a process +dies of a signal with the default action, which is noise in a script's +output that `/bin/sh` does not produce; the runtime reads the variable +when the signal arrives, so the children the shell forks inherit it too. + Job control (process groups, `fg`, `bg`, `^Z`) is part of the interactive milestone. @@ -390,8 +440,8 @@ and backslash escapes are always interpreted. ## The z/OS layer -`sys_zos.c` is the only C file that knows about z/OS. `zos_csi.s` is its -one piece of assembler. It provides: +`sys_zos.c` is the only C file that knows about z/OS, and the whole +z/OS layer is C: there is no assembler left in the tree. It provides: - `sys_spawn()`: `spawn()` with `fd_map` taken from the fd plan and a `struct inheritance` for signal defaults and process group. @@ -411,7 +461,11 @@ build it with ibm-clang for both character sets (ASCII is `./zash`, the build zopen uses, and EBCDIC is `./zash-ebcdic`), `tests/zos/*` drive spawn, pipes, the environment handed to `spawn()`, tagging, TSO and the MVS decoders there, and `make zos-heapcheck` runs the whole suite under LE -`HEAPCHK` and `HEAPZONES`, the nearest thing z/OS has to ASan. What no +`HEAPCHK` and `HEAPZONES`, the nearest thing z/OS has to ASan. In that +mode two signal tests (`posix/traps`, `scripts/signals`) fail, because a +self-`kill` no longer ends the shell the way it does under `zos-check`; +they pass in `zos-check` and pass individually under the same run +options, and they have not been rewritten to allow for the mode. What no test reaches is still marked `VERIFY` in the source: - the tagging system calls: a deferred tag applied by a writer with @@ -511,36 +565,30 @@ validated before the catalog is asked (`csi_filter_ok()`): 1-44 characters, qualifiers of 1-8, legal characters, and a first qualifier that is not all wildcard. -**The Catalog Search Interface.** IGGCSI00 is AMODE 31 and takes three -addresses: reason area, selection criteria, work area. zash is AMODE 64, so -`zos_csi.s` (ZASHCSI) switches mode around a branch to the module. It -loads the module with LOAD on the first call, keeps the entry point in the -block, and branches to it from then on: a Tab already asks the catalog -as much as a screen can show, and it should not also ask the system to -search its directories each time. +**The Catalog Search Interface.** IGGCSI00 takes three addresses in a +standard OS parameter list: reason area, selection criteria, work area, +with the VL bit on the last. zash is 31-bit XPLINK and IGGCSI00 is AMODE +31, so the two agree and the call is a plain call through a function +pointer: no mode switch and no assembler. `fetch()` loads the module on +the first search, and it stays loaded for the life of the shell -- a Tab +already asks the catalog as much as a screen can show, and it should not +also ask the system to search its directories every time. -Everything the glue touches is one `csi_block` from `__malloc31()`, taken -on the first Tab and held. It has to stay below 2 GB, because IGGCSI00 is -AMODE 31 and all three of its addresses have to fit in 31 bits: +Two details are load-bearing, and the second is not obvious: -| Offset | Field | | -| --- | --- | --- | -| +0 | parameter list | A(reason), A(criteria), A(work) with the VL bit | -| +12 | 72-byte save area | for IGGCSI00 | -| +84 | reason area | | -| +88 | 16 doublewords | the caller's R14-R13 | -| +216 | entry point | EPA LOAD returned, 0 until the first call | -| +220 | return code | | -| +224 | module name | CL8`IGGCSI00`, IBM-1047 | -| +232 | selection criteria | 162 bytes | -| +394 | work area | 64,000 bytes, as IBM recommends | - -The block is declared `packed`, so the compiler cannot add padding the -assembler's DSECT does not have, and `aligned(8)`, so the save area starts -on a doubleword and the `STMG` to it cannot fault. The glue keeps the -caller's registers in the block rather than in a save area reached through -R13, because under XPLINK-64 OS linkage the caller's R13 is not an OS save -area. The offsets are checked by `_Static_assert`, alignment included. +- The function type is declared with `#pragma linkage(iggcsi_fn, + OS_UPSTACK)`, **not** plain `OS`. Under `-m32` (XPLINK) `OS` means + `OS_NOSTACK`, which does not give the callee a standard 72-byte save + area on R13. IGGCSI00 is a traditional upward-stack OS routine, so it + takes a program check on the first call. Measured on z/OS: `OS` abends, + `OS_UPSTACK` returns rc=0 with 15,250 bytes of entries for `SYS1.**`. +- The name handed to `fetch()` is an ordinary `"IGGCSI00"` literal, and + **not** the eight IBM-1047 bytes the assembler used to pass to LOAD. + Under `-fzos-le-char-mode=ascii`, `fetch` is remapped to `__fetch_a`, + which converts the name itself, so handing it EBCDIC bytes + double-converts them and LOAD returns nothing. `sys_dynalloc()` fetches + `"BPXWDY2 "` the same way, and at 31-bit that fetch really does + succeed, which is why tests that used to skip now run. The searches are narrow on purpose: @@ -550,10 +598,10 @@ The searches are narrow on purpose: - The entry types are non-VSAM, generation data group and data set, cluster, alternate index and VSAM path (`ABCGHR`). Blank would add user catalog connectors and aliases, and those are not data set names. -- The work area is 64,000 bytes, the size IBM recommends. The search - stops after two calls: one call returns about 1100 names at 58 bytes - an entry, so two cover two screens of an 80-column terminal. That is - more than the editor displays. +- The work area is 32 KB (32768 bytes); IBM recommends 64,000. One + call returns about 560 names at roughly 58 bytes an entry, and the + search stops after `CSI_MAX_CALLS` = 3 of them: more names than a + completion screen can show. - Before a resume, the required length (`CSIREQLN`) is compared with the work area. Managing Catalogs asks for that check, and an entry that no longer fits ends the search instead of being asked for again. @@ -564,10 +612,26 @@ data) rather than depending on how zero fields are returned. Halfword lengths. Catalog entries (type `0`) and entries in error (flag X'40') are skipped. -One block, one search at a time: a nested search is refused rather than -given a second block, because the block holds the resume name and the -return code of the search that is running. The loaded module is never -released; MVS drops it when the shell ends. +One search at a time: a nested search is refused rather than given a +second block, because the work area carries the resume name and the +return code of the search that is running. The block is taken per search +and freed before the search returns, so a forked child carries no idle +32 KB. The loaded module stays loaded unless an abend retires it; MVS +drops it at task end. + +**Failure is final.** A search that fails returns false and is not +retried: `csi_dead` makes every later search return at once without +calling IGGCSI00. The one failure that is new at 31-bit is a program +check or abend inside the module, which arrives as a signal; +`csi_call()` catches `SIGABND`, `SIGSEGV`, `SIGILL`, `SIGFPE` and +`SIGBUS` for the length of that call only and longjmps back into itself, +so no frame of `csi_search()` or `csi_parse()` is skipped and a +completion list is never abandoned half-built. `sigsetjmp(..., 1)` saves +the mask, so the jump unblocks the signal it caught. Nothing switches +addressing mode and no register is kept across the call, so the C +run-time needs no repair afterwards; the shell's own handlers only +record the signal (see [Signals and traps](#signals-and-traps)), so one arriving during the +call cannot disturb it. **PDS directories** are read with `fopen("//'DSN'", "rb,recfm=u,blksize=256")` and decoded block by block. **DD names** come from walking the TIOT @@ -577,15 +641,19 @@ use, big-endian fields read a byte at a time. They run (and are fuzzed) on Linux. Checked on z/OS rather than left for later: `SYS1.*` returns 135 names -and `SYS1.MA*` returns SYS1.MACLIB, both through the assembled glue, -and Tab in an interactive shell completes `//'SYS1.MA` to `//'SYS1.MACLIB'` -and lists that PDS's members. `make zos-amblist` maps the load module, so -the OS-linkage parameter list, the AMODE and RMODE 31 placement can be -read off the listing, and `make zos-heapcheck` runs the suite with the -catalog work under LE heap checking. A probe on the machine confirmed -the block is shared, not doubled: a second search straight after the -first succeeds, a search started from inside a completion callback is -refused, and both hold under `HEAPCHK(ON)` and `HEAPZONES`. +and `SYS1.MA*` returns SYS1.MACLIB, and Tab in an interactive shell +completes `//'SYS1.MA` to `//'SYS1.MACLIB'` and lists that PDS's members. +Those checks were made at AMODE 64 through the assembler glue; re-running +them against the 31-bit build (which calls IGGCSI00 from C) is the +remaining item, together with the abend path: a probe pointed at +protected storage has to leave the shell alive, fail that one search, and +see the next Tab return at once without a call. `make zos-amblist` still +maps the load module, which is how the AMODE and the absence of any +assembler CSECT are confirmed, and `make zos-heapcheck` runs the suite +with the catalog work under LE heap checking. At 31-bit `malloc()` is +below 2 GB, so the work area needs no `__malloc31()`, and one block per +search means a second search straight after the first succeeds without +the first's resume name leaking into it. Still VERIFY: `fopen()` delivering PDSE directories in PDS format, and the TIOT walk -- neither is reached by a test on the machine. @@ -652,9 +720,11 @@ Policy: cannot drift. 4. The choice is made at build time. The default build is pure C. -The one piece of HLASM so far is not a speed-up but a necessity: -`zos_csi.s`, the AMODE switch for calling IGGCSI00 (see -[MVS names](#mvs-names-completion-and-the-catalog)). +**None so far.** The catalog used to need `zos_csi.s`, an AMODE switch +to reach the AMODE 31 IGGCSI00 from a 64-bit program. At 31-bit the C +code calls it directly and the assembler is gone (see +[MVS names](#mvs-names-completion-and-the-catalog)). The policy below +is the standing one for any future candidate: ## Testing @@ -691,11 +761,21 @@ Rules: - The suite must pass under ASan/UBSan and under Valgrind before a milestone is called done. - Tests use only POSIX utilities, so that they run unchanged on z/OS. + Where the z/OS utility disagrees with the one the expectations were + made with, the test normalises it rather than skipping: `wc` pads its + count (a `| tr -d ' '` after it), EBCDIC `tr` ranges are not contiguous + and `tr -c` with a class does not strip punctuation (splitting is done + with `sed`), and `od` can print upper-case hex. `make zos-check` also + puts a bundled `sleep` (`tests/tools/zos-sleep.c`) first in `PATH`, + because `/bin/sleep` rejects a fractional interval and four tests of + job ordering need one. - A test that depends on ASCII ordering or code points is named `*.ascii.sh` and is skipped for an EBCDIC build. Anywhere else such a dependency is a bug in the test. - The `tests/zos/` expectations were written from documentation and have - not been run. The first z/OS build must review them, not trust them. + since been run on the machine: `make zos-check` passes in both + character modes, and what the suite still misses is marked `VERIFY` in + the source. - The interactive tests are the one exception to "POSIX utilities only": they need a pseudo-terminal, and `tests/tools/ptydrive.py` provides one. They run on Linux. On z/OS the interactive layer has to be tried by @@ -884,6 +964,10 @@ terminal that is the default (`jobs.c`). VERIFY on z/OS). `tag` and the `alloc`/`free` builtins, MVS data set redirection and `tso` are to do. 6. **Tuning**: profile on z/OS, and use HLASM where it is justified. + First finding, and the largest: the 64-bit address mode itself costs + about 4 ms per `fork()` and 28 ms per program start, which is the + whole of the remaining z/OS gap and cannot be tuned away through LE + options. See [the cost model](#the-cost-model). ## Open questions @@ -897,4 +981,11 @@ terminal that is the default (`jobs.c`). `r`)? At present nothing is predefined, so the alias table stays empty unless the user fills it. - `a | b &` and `{ a; } | b` still fork where a spawn-only plan exists. - Worth doing once z/OS measurements say how much a fork costs. + The measurement says a 64-bit fork costs 4.2 ms and a 31-bit one + 0.10 ms (see [the cost model](#the-cost-model)), so the gain depends + on the address mode as much as on the plan. +- Should the z/OS build stay 31-bit? It is now the default: it removes + the 4 ms fork and the 28 ms start-up, and the catalog is reached from + C without the assembler that used to make the difference. What it + costs is the 2 GB address space, which no measurement here has come + close to needing. |
