diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-24 11:52:12 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-24 11:52:12 -0600 |
| commit | 2b257338481007b88331ae445b26ff99bdbf8977 (patch) | |
| tree | d5878c980bbeeffd02f17faf556854e599bd4251 | |
| parent | d60ce596e6f4ecd62ada6be0916caea22c75e0a8 (diff) | |
| download | zash-2b257338481007b88331ae445b26ff99bdbf8977.tar.gz | |
docs: humanize the markdown docsdocs/startup-probe
Plain sentences where the prose had decorative bold lead-ins, em dashes
and one-line closers. Claims, numbers and commands are unchanged, except
two corrections that the startup probe settled: README said the remaining
z/OS gap was the price of the address mode, and DESIGN's open question on
`a | b &` said the gain depended on the address mode. Both are the 64-bit
C run-time, not the address mode.
| -rw-r--r-- | DESIGN.md | 201 | ||||
| -rw-r--r-- | README.md | 50 | ||||
| -rw-r--r-- | bench/README.md | 139 | ||||
| -rw-r--r-- | notes/investigation.md | 44 |
4 files changed, 219 insertions, 215 deletions
@@ -55,10 +55,10 @@ creation and character encoding. ## Language level -- **C17**, compiled with `-std=c17` and no compiler extensions beyond a +- C17, compiled with `-std=c17` and no compiler extensions beyond a few attributes that are wrapped in macros. C23 was tried first and dropped: it cannot be relied on for the z/OS toolchain. -- **Clang on Linux, IBM Open XL C/C++ (Clang-based) on z/OS.** The tree +- Clang on Linux, IBM Open XL C/C++ (Clang-based) on z/OS. The tree also builds cleanly with GCC (`-std=c17 -pedantic -Wall -Wextra`), which is checked from time to time to keep the code honest. The legacy z/OS XL C/C++ (`xlc`) is not a target. @@ -162,24 +162,23 @@ modes, several of these assumptions turn out to need qualifying: | `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: +Two independent costs, and only one of them is about copying. -- **A fixed penalty for Language Environment at 64 bits**: about 4 ms - per fork and 28 ms per program start, whatever the program does or - holds. The address mode itself is free — an assembler program with no - C run-time starts in 0.8 ms at 31 bits or 64 — so the cost is LE - building the 64-bit enclave, 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 +- A fixed penalty for Language Environment at 64 bits: about 4 ms per + fork and 28 ms per program start, whatever the program does or holds. + The address mode itself is free (an assembler program with no C + run-time starts in 0.8 ms at 31 bits and at 64), so the cost is LE + building the 64-bit enclave. 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 64-bit C +Two consequences. The rule above still holds, since `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. The three rows zash used to lose on z/OS +(`( : ) &`, `( cd / ) &`, `( cd / )`) are lost to the 64-bit C run-time, not to the plan: they would be won without changing the plan at all. @@ -231,10 +230,10 @@ zash avoids it for *output-only* builtins (`echo`, `printf`, `pwd`, `test`, `true`, `false`, `type`): builtins whose only effect is writing to stdout/stderr. Two things make that safe: -1. **Builtins write through `out1`/`out2`, never to fd 1 directly.** The +1. Builtins write through `out1`/`out2`, never to fd 1 directly. The output object is pointed at a pipe, or at a memory buffer, for the duration of the builtin. The real fd table is untouched. -2. **Expansion side effects are undone.** Expanding the builtin's +2. Expansion side effects are undone. Expanding the builtin's arguments can assign variables (`${x=1}`, `$((i++))`). The expansion runs inside a *capture scope* that records every variable change and reverts it afterwards, so the visible behaviour matches a subshell. @@ -300,31 +299,31 @@ zash builds in ASCII mode (`-fzos-le-char-mode=ascii`, the zopen convention) or in EBCDIC (IBM-1047), chosen at compile time. The source never assumes either: -- **No in-band marker bytes.** A classic ash encodes quoting inside the +- No in-band marker bytes. A classic ash encodes quoting inside the word as control bytes in the 0x81+ range. In IBM-1047 those are ordinary letters (`0x81` is `a`). zash keeps words as a typed list of parts (literal, parameter, command substitution, arithmetic), each flagged quoted or not. There are no reserved byte values. -- **No arithmetic on letters.** `a`..`z` are not contiguous in EBCDIC. +- No arithmetic on letters. `a`..`z` are not contiguous in EBCDIC. Character tests go through `<ctype.h>`. Bracket ranges such as `[a-z]` are matched by alphabet position when both ends are letters of the same case (or both digits), and by code point otherwise. -- **No tables sorted by hand.** Lookup tables are hashed, not binary +- No tables sorted by hand. Lookup tables are hashed, not binary searched in source order, because collation differs. -- **Character literals only.** Never a numeric code for a character. +- Character literals only. Never a numeric code for a character. ## Memory -- **`scratch`**: a bump arena used as a stack. A command takes a mark, +- `scratch`: a bump arena used as a stack. A command takes a mark, expands its words into the arena, runs, and releases to the mark. No per-string `free()`. -- **Parse arenas**: each top-level command is parsed into a +- Parse arenas: each top-level command is parsed into a reference-counted arena. Normally it is reset and reused for the next command. If the command defines a function, the function takes a reference and the read loop moves on to a fresh arena. Function bodies are therefore never copied, and are freed when the function is redefined or unset and no call is in progress. -- **Heap**: long-lived things only: variables, the command table, traps. +- Heap: long-lived things only: variables, the command table, traps. Each arena keeps one released chunk for reuse, so a loop body crossing a chunk boundary does not `malloc()` and `free()` on every iteration. @@ -369,12 +368,12 @@ character sets. Redirections are resolved (files opened, here-documents materialised) by one routine and then applied in one of two ways: -- **Planned**, for external commands. An `fdplan` is an array saying which +- Planned, for external commands. An `fdplan` is an array saying which parent descriptor each child descriptor 0..9 should be. Redirections edit the array. Nothing is `dup2()`ed in the shell. The plan is handed to `spawn()` as its `fd_map` on z/OS and translated to `posix_spawn` file actions elsewhere. -- **Real**, for builtins, functions and compound commands. The affected +- Real, for builtins, functions and compound commands. The affected descriptors are saved above 10, replaced, and restored afterwards. Here-documents up to `PIPE_BUF` are written into a pipe by the shell; @@ -390,7 +389,7 @@ collecting pointers. The array is cached and rebuilt only when an exported variable changes. Variables inherited from the environment are referenced in place, not copied. -**Scopes** are a single mechanism with three uses: `local` variables in +Scopes are a single mechanism with three uses: `local` variables in functions, temporary assignments (`VAR=x cmd` for builtins and functions), and the capture scope described under [Process creation](#running-builtins-without-a-subshell). A scope is a @@ -460,18 +459,18 @@ them can be tested (see [Checking z/OS code off z/OS](#checking-zos-code-off-zos Code outside `sys_*.c` tests `SYS_MVS`, a compile-time 0 or 1, so the Linux build compiles the z/OS-only branches away entirely. -It **compiles and runs on z/OS**. `make zos-ascii` and `make zos-ebcdic` +It compiles and runs on z/OS. `make zos-ascii` and `make zos-ebcdic` build it with ibm-clang for both character sets (ASCII is the default -`./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. 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: +`./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. 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 autoconversion off, `SETCVTON` with `pccsid` 0 after `spawn()`, an @@ -503,27 +502,27 @@ adds what it needs to work beside EBCDIC programs. Decisions and reasons: -- **Hooks, not lookups.** `_TAG_REDIR_*` are watched by variable hooks +- Hooks, not lookups. `_TAG_REDIR_*` are watched by variable hooks (as `PATH` and `IFS` are), so a redirection tests a cached byte. A prefix on an external command (`_TAG_REDIR_OUT=TXT cmd >f`) never becomes a shell variable. `run_external()` therefore scans the command's own assignments, and only when there are any. -- **Pipeline stages.** A prefix on a stage tags that stage's pipe end: +- Pipeline stages. A prefix on a stage tags that stage's pipe end: `_TAG_REDIR_OUT=TXT a | b` tags `a`'s output, as documented for `/bin/sh`. -- **The tag follows the writer.** A deferred tag takes the CCSID of the +- The tag follows the writer. A deferred tag takes the CCSID of the first program that writes, so an EBCDIC tool behind `>` in an ASCII shell still produces a correctly tagged IBM-1047 file. -- **Pipes in ASCII builds are IBM-1047.** If they were tagged 819 (as +- Pipes in ASCII builds are IBM-1047. If they were tagged 819 (as zoslib does) an EBCDIC reader would see ASCII. Tagged 1047, ASCII writers are converted on write and ASCII readers on read. Between two ASCII programs that costs two conversions per byte. `set +o pipetag` avoids it when no EBCDIC program is involved. -- **Sniffing.** Single-byte CCSIDs only, so file offsets are unchanged +- Sniffing. Single-byte CCSIDs only, so file offsets are unchanged and the `lseek()` that keeps a shared stdin correct still works. Standard input is never converted: its conversion state is shared with the commands the shell runs. -- **Recommendation.** For a `/bin/sh` replacement, build EBCDIC: 1047 +- Recommendation. For a `/bin/sh` replacement, build EBCDIC: 1047 scripts need no conversion at all. Build ASCII for zopen environments. Cost: one `fstat()`/`fcntl()` per redirection, here-document or script, @@ -570,16 +569,16 @@ 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 takes three addresses in a +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. +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. -Two details are load-bearing, and the second is not obvious: +Two details matter, and the second is not obvious: - The function type is declared with `#pragma linkage(iggcsi_fn, OS_UPSTACK)`, **not** plain `OS`. Under `-m32` (XPLINK) `OS` means @@ -638,8 +637,8 @@ 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 +PDS directories are read with `fopen("//'DSN'", "rb,recfm=u,blksize=256")` +and decoded block by block. DD names come from walking the TIOT (PSATOLD, TCBTIO, entries from +24). `mvsfmt.c`: portable C over byte buffers, every length checked before use, big-endian fields read a byte at a time. They run (and are fuzzed) @@ -661,7 +660,7 @@ 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. +TIOT walk. Neither is reached by a test on the machine. ### Per-command z/OS options @@ -696,7 +695,7 @@ possible is therefore made to run, or at least compile, on Linux: - `make mvs-asan`: the whole suite under ASan/UBSan with the MVS branches on over the Linux platform layer: tagging hooks, `_TAG_REDIR_*` prefixes, `ZOS_*` variables. -- **Stand-in catalog.** With `ZASH_TEST_CATALOG` naming a file, +- Stand-in catalog. With `ZASH_TEST_CATALOG` naming a file, `sys_posix.c` builds real IGGCSI00 work areas (EBCDIC, small, so the resume protocol runs), PDS directory blocks (members split across blocks, varying user data) and TIOT entries, and the completion code @@ -718,18 +717,18 @@ will not always emit. Policy: -1. **Measure first**, on z/OS, with a real workload. +1. Measure first, on z/OS, with a real workload. 2. Rewrite only a leaf routine with a narrow, documented interface. -3. **Keep the C version.** It is the reference implementation, it is +3. Keep the C version. It is the reference implementation, it is what Linux builds use, and the tests run against both so the two cannot drift. 4. The choice is made at build time. The default build is pure C. -**None so far.** The catalog used to need `zos_csi.s`, an AMODE switch +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: +[MVS names](#mvs-names-completion-and-the-catalog)). The policy above +stands for any future candidate. ## Testing @@ -791,39 +790,39 @@ Rules: ## Deliberate differences from other shells -- **The last stage of a pipeline runs in the current shell** when it is +- The last stage of a pipeline runs in the current shell when it is not an external command, as in ksh and the z/OS `/bin/sh` (unlike dash and bash). `cmd | while read x; do ...; done` therefore needs no `fork()`, and variables set in the loop survive it. - Consequently `cmd | exit 3` exits the shell, and `cmd | cd dir` changes its directory. -- **Under job control every stage is a process.** In an interactive +- Under job control every stage is a process. In an interactive shell with `set -m` (the default there) the last stage is not run in the shell, because it could not be stopped with the rest of the job. `cmd | read x` therefore does not set `x` at the prompt. bash makes the same choice. -- **`$((` is always arithmetic** (as in dash). -- **`((` at the start of a command is arithmetic only if it can be.** +- `$((` is always arithmetic (as in dash). +- `((` at the start of a command is arithmetic only if it can be. `((a) | b)` and `((echo x))` remain nested subshells, as POSIX has them. See `parse_arith_command()` for the rule, and write `( (` to be certain of a subshell. -- **`trap` in a pipeline or `$( )` prints the traps of the shell that - started it**, until that subshell sets a trap of its own. -- **`function` is a reserved word**, so a command cannot be named +- `trap` in a pipeline or `$( )` prints the traps of the shell that + started it, until that subshell sets a trap of its own. +- `function` is a reserved word, so a command cannot be named `function`. -- **Arithmetic** has `++`, `--`, the comma operator and every `op=`. -- **A script on standard input shares it with its commands.** zash never +- Arithmetic has `++`, `--`, the comma operator and every `op=`. +- A script on standard input shares it with its commands. zash never reads ahead of the command it is running, so `printf 'read v\ndata\necho $v\n' | zash` prints `data` (dash does not). -- **Exit statuses follow the ash family**: 2 for a failed redirection, +- Exit statuses follow the ash family: 2 for a failed redirection, 127 from `type` and `command -v` for an unknown name. -- **An expansion error in a command the shell prepares for a child** +- An expansion error in a command the shell prepares for a child (a pipeline stage, `&`, `$( )`) is reported on the shell's stderr, not on the stderr that command would have had. -- **`local`** is provided (as in FreeBSD sh and dash) although POSIX does +- `local` is provided (as in FreeBSD sh and dash) although POSIX does not define it. -- **`set -o pipefail`** is provided (POSIX.1-2024). +- `set -o pipefail` is provided (POSIX.1-2024). - The extensions listed under [Extensions](#extensions). ## Extensions @@ -839,17 +838,17 @@ here to keep those scripts running. An extension is accepted only if it costs a script that does not use it nothing measurable. Concretely: -- **Parse-time or lookup-time only.** `function f { }` and `$'...'` are +- Parse-time or lookup-time only. `function f { }` and `$'...'` are gone after parsing. New builtins are rows in the builtin table. -- **Computed on read.** `RANDOM` and `SECONDS` are `V_DYNAMIC`: an unused +- Computed on read. `RANDOM` and `SECONDS` are `V_DYNAMIC`: an unused one costs nothing. -- **Inside a branch that is already taken.** The `ERR` trap is tested +- Inside a branch that is already taken. The `ERR` trap is tested only after a command has failed, where `errexit` was already tested. Integer and dynamic variables share the one flag test that read-only variables already needed in `var_set_n()`. -- **Guarded by one test.** Alias lookup is skipped while no alias is +- Guarded by one test. Alias lookup is skipped while no alias is defined, and happens while parsing, so a loop body pays once. -- **Measured.** Instruction counts (`valgrind --tool=callgrind`) of the +- Measured. Instruction counts (`valgrind --tool=callgrind`) of the benchmarks before and after the whole extension and interactive work differ by about 0.1% (loop, strings, functions, read loop). One real regression was found this way and fixed: more builtins meant longer @@ -882,14 +881,14 @@ nothing measurable. Concretely: ### Deferred or declined -- **Indexed arrays.** Variables hold one string, and arrays touch `var.c` +- Indexed arrays. Variables hold one string, and arrays touch `var.c` and `expand.c` on the hot path. Only if real scripts need them, and measured first. -- **Extended globbing (`@(a|b)`, `*(x)`),** co-processes (`|&`, +- Extended globbing (`@(a|b)`, `*(x)`), co-processes (`|&`, `print -p`), `typeset -u/-l/-L/-R`, `select`. -- **ksh93 and bash extras:** associative arrays, `${v/pat/repl}`, +- ksh93 and bash extras: associative arrays, `${v/pat/repl}`, `${v:off:len}`, floating point, process substitution. -- **`MAIL` checking, `bind`,** and FreeBSD's `-p` privileged mode. +- `MAIL` checking, `bind`, and FreeBSD's `-p` privileged mode. ## The interactive layer @@ -897,12 +896,12 @@ Nothing here is reached by a script. The single hook on the shared path is one test in `input_fill()`: "is this input a terminal in an interactive shell?" -- **Prompts** (`interact.c`). PS1 and PS2 get FreeBSD's backslash +- Prompts (`interact.c`). PS1 and PS2 get FreeBSD's backslash escapes (`\h \H \u \w \W \$`) and then parameter, command and arithmetic expansion. The prompt is expanded from inside the lexer, so the parser's state is saved around it, and an error in the expansion falls back to the raw text instead of failing at every prompt. -- **The built-in editor** (`edit_builtin.c`) needs only a terminal that +- The built-in editor (`edit_builtin.c`) needs only a terminal that understands four ANSI sequences. There is no terminfo or curses dependency. The line is drawn on one row that scrolls sideways, and is redrawn in a single `write()` per key. Emacs mode has the usual motion, @@ -910,17 +909,17 @@ interactive shell?" has insert and command modes with counts, the common motions, `d c y` with motions, `x X r ~ D C S p P u`, history with `k j G / ? n N`. UTF-8 is handled as far as single-width characters. -- **libedit** (`edit_libedit.c`) implements the same two functions: +- libedit (`edit_libedit.c`) implements the same two functions: `make EDIT=libedit LDLIBS=-ledit`. The shell's own history list stays authoritative and is copied into libedit before each line. -- **Completion** (`complete.c`) covers variables after `$`, aliases, +- Completion (`complete.c`) covers variables after `$`, aliases, functions, builtins and `PATH` programs in command position, and files elsewhere, with `/` after a directory. Completed text is backslash-escaped unless the word is inside quotes. -- **History** (`history.c`): whole commands (a PS2 line is appended to +- History (`history.c`): whole commands (a PS2 line is appended to its entry), `HISTSIZE`, `HISTFILE` (mode 0600), and `fc -l`, `fc -s`, `fc -e`. -- **Character sets.** Key codes and ESC come from `charset.h`. The +- Character sets. Key codes and ESC come from `charset.h`. The EBCDIC values are IBM-1047 and have not been tried on a z/OS terminal. ## Job control @@ -936,7 +935,7 @@ terminal that is the default (`jobs.c`). `job_begin()` and `job_wait_fg()` or `job_end_bg()`. While a job is being launched, `fork_shell()` and `spawn_command()` ask `jobs.c` for the group to join. -- **Spawn and the terminal.** A forked child puts itself in the group +- Spawn and the terminal. A forked child puts itself in the group and takes the terminal before it runs anything. A spawned child cannot run code, so the platform layer does it: `POSIX_SPAWN_SETPGROUP` plus, on glibc 2.35 and later, the `tcsetpgrp` file action. On z/OS it is @@ -953,26 +952,26 @@ terminal that is the default (`jobs.c`). ## Roadmap -1. **Scripting core** (done on Linux). -2. **Extensions, job control and the interactive layer** (done on Linux, +1. Scripting core (done on Linux). +2. Extensions, job control and the interactive layer (done on Linux, tested there under ASan/UBSan, and the interactive tests need a pseudo-terminal). -3. **First z/OS build** (done): compiles with Open XL in both character +3. First z/OS build (done): compiles with Open XL in both character modes, the suite passes under the LE heap checker, and the benchmarks - are faster than every shell there but ksh88, and zash matches it on - external commands (see the tables in README). Still open: the + beat every shell there except ksh88, which zash matches on external + commands (see the tables in README). Still open: the remaining `VERIFY` notes in `sys_zos.c` and the benchmarks under `_BPX_SHAREAS=` other than the default. -4. **`[[ ]]`** (done), then whatever real z/OS scripts turn out to need. -5. **z/OS features**: file tagging and auto-conversion, data set +4. `[[ ]]` (done), then whatever real z/OS scripts turn out to need. +5. z/OS features: file tagging and auto-conversion, data set completion, `time`, job names, message IDs and `ZOS_*` (written, 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. +6. Tuning: profile on z/OS, and use HLASM where it is justified. First finding, and the largest: Language Environment's 64-bit start-up costs about 4 ms per `fork()` and 28 ms per program start. - The address mode is not the cause — a bare assembler program starts - in 0.8 ms at 64 bits — and the cost cannot be tuned away through LE + The address mode is not the cause: a bare assembler program starts + in 0.8 ms at 64 bits. The cost cannot be tuned away through LE options. See [the cost model](#the-cost-model). ## Open questions @@ -988,8 +987,8 @@ terminal that is the default (`jobs.c`). unless the user fills it. - `a | b &` and `{ a; } | b` still fork where a spawn-only plan exists. 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. + 0.10 ms (see [the cost model](#the-cost-model)), so the gain comes + from dropping the 64-bit C run-time, not from 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 @@ -34,40 +34,40 @@ CPU time is billed, and every `fork()` copies an address space. zash is for shops that care about that: -- **Low-MSU shops and anyone watching script CPU time.** The shell does its +- Low-MSU shops and anyone watching script CPU time. The shell does its work in-process wherever the language allows. When a process is needed it uses `spawn()`, so `_BPX_SHAREAS` can keep the child in the same address space. -- **Shops that want a modern `/bin/sh`, not a different language.** Scripts +- Shops that want a modern `/bin/sh`, not a different language. Scripts written for zash are POSIX `sh` scripts. They run under the existing `/bin/sh`, dash, FreeBSD sh and bash. -- **People who type at it.** File and command completion, history and line +- People who type at it. File and command completion, history and line editing are in scope, provided they cost a script nothing. -Think "the dash of z/OS", but more usable interactively than dash. +It aims to be the dash of z/OS, but more usable interactively. ## Goals -1. **A drop-in replacement for the z/OS `/bin/sh`** for POSIX scripts, +1. A drop-in replacement for the z/OS `/bin/sh` for POSIX scripts, including the z/OS behaviours scripts rely on (`_BPX_SHAREAS`, `_BPX_SPAWN_SCRIPT`, `STEPLIB`, file tagging). -2. **Script performance at least equal to `/bin/sh`**, measured on z/OS, +2. Script performance at least equal to `/bin/sh`, measured on z/OS, by using `spawn()` instead of `fork()` and by not creating a process at all for builtins, even in pipelines and command substitutions. -3. **Interactive comfort that is free for scripts**: job control, tab +3. Interactive comfort that is free for scripts: job control, tab completion for files and commands, history, emacs- and vi-style editing. The keys are bash's, and `bind '^[[H'=beginning-of-line` moves one in the BSD shells' notation. A built-in editor is the default and libedit is a compile-time alternative. Neither is initialised by a non-interactive shell. -4. **The Korn shell features z/OS scripts actually use**: `print`, +4. The Korn shell features z/OS scripts actually use: `print`, `whence`, `typeset`, `function name { }`, `(( ))`, `let`, aliases. They are accepted only where they cost other scripts nothing. -5. **ASCII or EBCDIC, chosen at compile time.** The source makes no +5. ASCII or EBCDIC, chosen at compile time. The source makes no assumption about the character set. -6. **Small and readable.** One directory, a plain POSIX Makefile, every +6. Small and readable. One directory, a plain POSIX Makefile, every function documented. -7. **Heavily tested.** See [Testing](#testing). +7. Heavily tested. See [Testing](#testing). ## Non-goals @@ -147,23 +147,23 @@ Chrome/Chromium to turn the two Markdown documents into HTML and PDF, and The project is tested at several levels: -- **Behavioural tests** for every feature, as shell scripts with expected +- Behavioural tests for every feature, as shell scripts with expected output. Portable tests are also run under dash and the results compared, so a disagreement with an existing ash-family shell is always visible. -- **AddressSanitizer and UndefinedBehaviorSanitizer** builds run the whole +- AddressSanitizer and UndefinedBehaviorSanitizer builds run the whole suite on Linux. -- **Valgrind** runs the whole suite on Linux, including leak checking. -- **Process accounting tests** assert the exact process count of a +- Valgrind runs the whole suite on Linux, including leak checking. +- Process accounting tests assert the exact process count of a construct (for example, `$(echo x)` creates none), so a performance regression fails the suite. -- **Security tests** (`tests/zos/security.sh`) check what the shell must +- Security tests (`tests/zos/security.sh`) check what the shell must never do: create a file more permissive than the umask allows, look for a command in the current directory, or run what an expansion or a file name produced. -- **Interactive tests** drive the shell through a pseudo-terminal: editor +- Interactive tests drive the shell through a pseudo-terminal: editor keys in both modes, completion, history, prompts and job control. They need python3 and are skipped without it. -- **On z/OS** `make zos-check` runs the same behavioural and +- On z/OS `make zos-check` runs the same behavioural and process-accounting tests, plus a z/OS-only group (`tests/zos/`), in either character mode. There is no ASan, UBSan or Valgrind there, but Language Environment can put a check zone after every heap element and @@ -175,7 +175,7 @@ The project is tested at several levels: character modes pass `zos-check`; under the heap checker two signal tests fail, as they do not in `zos-check` (see [DESIGN.md](DESIGN.md#testing)). -- **A debugger** is `lldb` on Linux (`make debug`, `make attach`) and +- A debugger is `lldb` on Linux (`make debug`, `make attach`) and `dbx` on z/OS (`make zos-debug`, `make zos-dbx`). `make zos-amblist` maps the CSECTs of the program object when a module-level question comes up. Break by function (`st in main`) or by address. On a build @@ -300,7 +300,7 @@ like a pipeline stage already did, so the same 3000 cost 0.00 s here against the 0.79 of `sh`. What is left of the gap in the `background jobs` row above is the price -of the address mode, not of the design: measured on this machine a +of the 64-bit C run-time, not of the design: measured on this machine a 64-bit `fork()` costs 4.2 ms and a 31-bit one 0.10 ms, and the same program starts in 29.5 ms at 64-bit against 1.1 ms at 31-bit. The z/OS make targets therefore build `-m32`, and the table above is the last @@ -321,16 +321,16 @@ word instead of eight. Linux is feature-complete for the first release (see [Status](#status)). On z/OS, in order: -1. **Manual pages**: `zash(1)` and a page per builtin group, installed by +1. Manual pages: `zash(1)` and a page per builtin group, installed by `make install`, kept beside the code they document. -2. **The editor on a real terminal**: interactive tests on a 3270-style +2. The editor on a real terminal: interactive tests on a 3270-style terminal, in both character modes. -3. **`_BPX_SHAREAS`**: the benchmark rows above re-run under +3. `_BPX_SHAREAS`: the benchmark rows above re-run under `_BPX_SHAREAS=YES`, `NO` and `REUSE`. -4. **The remaining `VERIFY` notes**: `sys_zos.c`, `charset.h`, the EBCDIC +4. The remaining `VERIFY` notes: `sys_zos.c`, `charset.h`, the EBCDIC `$'\cX'` codes and the data set paths, each checked on hardware and the note removed. -5. **z/OS conveniences**: `tag`, `alloc`/`free` and `tso` builtins, MVS +5. z/OS conveniences: `tag`, `alloc`/`free` and `tso` builtins, MVS data set redirection, job names, message IDs. [DESIGN.md](DESIGN.md#roadmap) has the longer version, including why each diff --git a/bench/README.md b/bench/README.md index 0ff147f..8bbb8fe 100644 --- a/bench/README.md +++ b/bench/README.md @@ -19,21 +19,21 @@ sh bench/run.sh "$(pwd)/zash" /bin/sh bash zsh oksh 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, +- 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. +- `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 </dev/null &`); otherwise an interrupted `ssh` session takes the run with it. -The times are **CPU seconds, user + sys, children included**, so they do +The times are CPU seconds, user + sys, children included, so they do not include wall-clock start-up. See [Start-up is not in these numbers](#start-up-is-not-in-these-numbers). @@ -41,8 +41,8 @@ numbers](#start-up-is-not-in-these-numbers). Measured on the CBT z/OS system with IBM Open XL C/C++ 2.1 (clang 18.1.8), the same compiler and the same source in both modes, so the -only variable is the width the C run-time is built at — not the address -mode itself; [an assembler program with no C run-time](#is-it-the-address-mode-or-the-c-runtime) +only variable is the width the C run-time is built at. The address mode +is not a variable: [an assembler program with no C run-time](#is-it-the-address-mode-or-the-c-runtime) starts in 0.8 ms at either width. | | `-m64` | `-m32` | ratio | @@ -52,33 +52,34 @@ starts in 0.8 ms at either width. | the same, 64 MB touched | 28.2 ms | 27.7 ms | 1.0× | | start a trivial program | 29.5 ms | 1.1 ms | 27× | -Two separate costs, and they behave differently: +Two costs, and they behave differently: -- **A fixed penalty for being 64-bit.** About 4 ms per `fork()` and - about 28 ms per program start, paid whatever the program does and - whatever storage it holds. A ten-line program that returns 0 pays it. -- **A per-byte copy cost that both modes pay.** Roughly 0.4 ms per MB of +- A fixed penalty for being 64-bit. About 4 ms per `fork()` and about + 28 ms per program start, paid whatever the program does and whatever + storage it holds. A ten-line program that returns 0 pays it. +- A per-byte copy cost that both modes pay. Roughly 0.4 ms per MB of touched private storage, identical at `-m64` and `-m32`. At 64 MB it dominates and the address mode stops mattering. -The 28 ms is not program load: a 28 KB program and a 900 KB program cost -the same, and the sticky bit changes nothing (29.4 ms either way). It is -not I/O either — the process uses under 1 ms of CPU in that time and -simply waits. It is not XPLINK: Open XL spells the three modes `-m64` -(64-bit XPLINK), `-m32` (32-bit XPLINK) and `-m32n` (32-bit non-XPLINK), -and the two 32-bit ones both start in about 1 ms. It is not the 64-bit -address space either: an assembler program with no C run-time starts in -0.8 ms at 31 bits *and* at 64 bits (see [Is it the address mode or the -C runtime?](#is-it-the-address-mode-or-the-c-runtime)). Nor is it zash's -own storage — LE reports 50 KB of user heap, 94 KB of library heap and -1 MB of stack for `zash -c :`, so a fork has almost nothing to copy. +Program load is ruled out: a 28 KB program and a 900 KB program cost the +same, and the sticky bit changes nothing (29.4 ms either way). I/O is +ruled out too: the process uses under 1 ms of CPU in that time and +simply waits. XPLINK is ruled out by the same programs built at the +other two modes Open XL spells, `-m32` (32-bit XPLINK) and `-m32n` +(32-bit non-XPLINK); both start in about 1 ms. The address space is +ruled out by the assembler probe: no C run-time, and it starts in 0.8 ms +at 31 bits *and* at 64 bits (see [Is it the address mode or the +C runtime?](#is-it-the-address-mode-or-the-c-runtime)). zash's own +storage is too small to matter: LE reports 50 KB of user heap, 94 KB of +library heap and 1 MB of stack for `zash -c :`, so a fork has almost +nothing to copy. What is slow is Language Environment starting in 64-bit mode. The [LE Programming Guide for 64-bit](https://www.ibm.com/docs/en/zos) puts -it plainly — "the creation and termination of that HLL environment -multiple times is needlessly inefficient" — and offers PreInit -(`CELQPIPI`) to create it once and reuse it, which is no help to a shell -that starts, runs and exits. On this machine that creation costs 28 ms +it plainly: "the creation and termination of that HLL environment +multiple times is needlessly inefficient". It offers PreInit +(`CELQPIPI`) to create that environment once and reuse it, which is no +help to a shell that starts, runs and exits. On this machine that creation costs 28 ms of wall time at 64 bits (about 1 ms at 31), and no option moves it. For scale, the same measurement on `/bin/sh` (z/OS sh, AMODE 31) is 2.1 ms @@ -145,21 +146,20 @@ time sh -c 'i=0; while [ $i -lt 100 ]; do ./t64; i=$((i+1)); done' time sh -c 'i=0; while [ $i -lt 100 ]; do ./t32; i=$((i+1)); done' ``` -Two traps in getting this right, both of which produced wrong answers -here first: +Two traps, both of which produced wrong answers here first: -- **Check the build, not the flag.** `-m31` is rejected; `-m32` is the +- Check the build, not the flag. `-m31` is rejected; `-m32` is the spelling that works, and `file` prints `amode=31` for the result. `ibm-clang -print-targets` lists `systemz32` as a registered target. -- **Make sure the probe measures what you think.** `return p ? 0 : 0;` - lets the compiler delete the allocation, and the "8 MB" row then reads - the same as "0 MB". Touch the memory after the loop. +- Make sure the probe measures what you think. `return p ? 0 : 0;` lets + the compiler delete the allocation, and the "8 MB" row then reads the + same as "0 MB". Touch the memory after the loop. ## Is it the address mode or the C runtime? It is the C runtime, and the address mode is free. The probe is the -same empty program written in HLASM — no LE, no XPLINK, just a return -code and `SVC 3` (EXIT) — at each width, against the C version: +same empty program written in HLASM, with no LE and no XPLINK, just a +return code and `SVC 3` (EXIT), at each width, against the C version: 100 starts each, three rounds, the same numbers every round: @@ -174,22 +174,23 @@ code and `SVC 3` (EXIT) — at each width, against the C version: A 64-bit address space costs nothing: `tiny64` loads above the bar and starts as fast as `tiny31`. The 28 ms arrives with LE at 64 bits and only with it, and the 31-bit LE start is about 1 ms. (The 32-bit C rows -read 1.0–1.1 ms in an earlier session; the absolute figures drift, the +read 1.0 to 1.1 ms in an earlier session; the absolute figures drift, the comparison holds.) -The `fork()` penalty was not split the same way — an assembler `fork()` -probe has not been written — but it is the cheapest explanation of a -fixed 4 ms that appears only in 64-bit children: the child re-creates -the enclave the parent had. +The `fork()` penalty was not split the same way, since no assembler +`fork()` probe exists, but it is the cheapest explanation of a fixed +4 ms that appears only in 64-bit children: the child re-creates the +enclave the parent had. -The probe in full — HLASM source, build lines, the loop — and the -follow-ups it leaves open are in -[notes/investigation.md](../notes/investigation.md). +The probe in full, with its HLASM source, build lines and loop, is in +[notes/investigation.md](../notes/investigation.md); that file also +lists what it leaves open. ## LE run-time options: no measurable effect The obvious suspect for a 4 ms `fork()` and a 28 ms start is LE's own -sizing — `HEAP64`, `HEAPPOOLS64`, `STACK64`. It is not. +sizing: `HEAP64`, `HEAPPOOLS64`, `STACK64`. None of them moves the +number. | `_CEE_RUNOPTS` | fork | start | | --- | --- | --- | @@ -212,7 +213,7 @@ storage actually used, in case a future LE release behaves differently: _CEE_RUNOPTS='RPTOPTS(ON),RPTSTG(ON)' ./zash -c : 2>rpt.txt ``` -The report lists each option, its value, and where it was set — +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 :` @@ -233,8 +234,9 @@ interactive session. Compare it with `time`, not with `make bench`. ## Address mode and the 31-bit build zash is a 31-bit (AMODE 31) XPLINK program, which is what the z/OS -Makefile targets build. It used to be 64-bit; the costs above are what -that choice cost, and they were the whole of the remaining z/OS gap. +Makefile targets build. It used to be built 64-bit; the costs above are +what that choice cost, and they accounted for the whole of the remaining +z/OS gap. The same sources build at either width with the same compiler: @@ -263,9 +265,9 @@ Nothing else moves: `report` 0.18 → 0.16, `deploy` 0.15 → 0.14, `spawn` 1.17 → 1.15. Every row is the same or slightly better, because a smaller address space costs nothing. -Two things had to be settled before 31-bit could ship, and both are: +Two things had to be settled before 31-bit could ship: -1. **The catalog search.** It used to need `zos_csi.s`, an `AMODE 64` +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 calls IGGCSI00 directly, with `fetch()` and `#pragma linkage(..., OS_UPSTACK)`, and @@ -274,9 +276,9 @@ Two things had to be settled before 31-bit could ship, and both are: 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. -2. **Address space.** A 31-bit shell is limited to storage below 2 GB. - Ample for a shell, not for `x=$(<hugefile)`, which now fails with - "out of memory" rather than growing past it. +2. Address space. A 31-bit shell is limited to storage below 2 GB, which + is plenty for a shell but not for `x=$(<hugefile)`: that now fails + with "out of memory" rather than growing past it. 32-bit pointers also shrink the nodes, arenas and hash tables, which is why no row got worse. @@ -285,7 +287,7 @@ why no row got worse. What to run when a number here is surprising. -**Which address mode is this program, and does every CSECT agree?** +### Which address mode is this program, and does every CSECT agree? ```sh file ./zash # amode=31 or amode=64 @@ -297,9 +299,10 @@ grep -nE 'AMODE|RMODE|ENTRY POINT' zash.amblist 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 would fork more slowly than a -small one: +### 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 @@ -310,15 +313,17 @@ 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`. Note that 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. +### 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? -**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. +`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 @@ -328,5 +333,5 @@ 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–0.02 difference as noise +the fork-heavy ones move more, so treat a 0.01 to 0.02 difference as noise and re-run before believing it. diff --git a/notes/investigation.md b/notes/investigation.md index b12d5ea..06e47bd 100644 --- a/notes/investigation.md +++ b/notes/investigation.md @@ -1,8 +1,7 @@ # Investigations -Questions worth more than a benchmark row, and the probes that answered -them. `bench/README.md` holds the cost model and the benchmark numbers; -this file holds the experiments behind them and the ones still open. +Probes behind the numbers in `bench/README.md`, and what they left +open. Machine for everything here: the CBT z/OS system, z/OS 29.00, IBM Open XL C/C++ 2.1 (clang 18.1.8), reached over SSH. Probes live in @@ -10,11 +9,11 @@ XL C/C++ 2.1 (clang 18.1.8), reached over SSH. Probes live in ## 1. Is the 64-bit start-up cost the address mode or the C runtime? -**Answer: the C runtime. A 64-bit address space costs nothing.** +The C runtime. A 64-bit address space costs nothing. A `-m64` program that does nothing pays about 28 ms per start; the same source at `-m32` pays about 1 ms. The open question was which part of -"64-bit" carries that cost — the address space, the loader, XPLINK, or +"64-bit" carries that cost: the address space, the loader, XPLINK, or Language Environment. ### Method: take the C runtime out @@ -74,31 +73,32 @@ Three rounds, same numbers to 0.01 s each time. ### What it shows -- `tiny64` is RMODE 64, loads above the bar and starts in 0.8 ms — the - 64-bit address space and the loader that maps it are free. +- `tiny64` is RMODE 64, loads above the bar and starts in 0.8 ms, so + the 64-bit address space and the loader that maps it are free. - The 28 ms arrives with LE at 64 bits and only with it; at 31 bits LE costs about 1 ms over bare metal. -- XPLINK is not it either: `-m32` and `-m32n` differ by nothing. +- XPLINK does not enter into it: `-m32` and `-m32n` start at the same + speed. - So the fix for zash was to stop using the 64-bit C run-time, not to stop using 64-bit addressing. -This corrected two claims made earlier from the 32-bit rows alone — -that the cost was "not C code", and that "only the 64-bit address space -is slow". Both are wrong; the address space is free and the C run-time -is the whole cost. +Earlier notes drew two conclusions from the 32-bit rows alone: that the +cost was "not C code", and that "only the 64-bit address space is +slow". Neither holds. The address space is free and the C run-time is +the whole cost. ### Not settled -- **The 4 ms `fork()`.** No assembler `fork()` probe has been written, - so the fork penalty is attributed to LE by inference (a fixed cost - that appears only in 64-bit children), not by measurement. `BPX1FRK` - is callable from HLASM at either AMODE; that is the next probe. -- **Where inside LE the 28 ms goes.** `RPTOPTS`/`RPTSTG` show nothing - tunable and `HEAP64`/`STACK64` move nothing, but splitting DLL - load from enclave creation has not been tried. -- **Why 31-bit LE costs 1 ms.** Assumed to be the same enclave work at +- The 4 ms `fork()`. No assembler `fork()` probe has been written, so + the fork penalty is attributed to LE by inference (a fixed cost that + appears only in 64-bit children), not by measurement. `BPX1FRK` is + callable from HLASM at either AMODE; that is the next probe. +- Where inside LE the 28 ms goes. `RPTOPTS`/`RPTSTG` show nothing + tunable and `HEAP64`/`STACK64` move nothing, but DLL load has not been + separated from enclave creation. +- Why 31-bit LE costs 1 ms. Assumed to be the same enclave work at smaller sizes; not measured separately. Absolute figures drift between sessions and machines (the 32-bit C rows -read 1.0–1.1 ms in an earlier run, 2.0 ms here); only comparisons made -in one run are worth quoting. +read 1.0 to 1.1 ms in an earlier run, 2.0 ms here); only comparisons +made in one run are worth quoting. |
