summaryrefslogtreecommitdiffhomepage
path: root/bench/README.md
blob: 2249e3ca389cec3a63bf50a6103b810bca25e853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# 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 </dev/null &`); otherwise an interrupted
`ssh` session takes the run with it.

The times in `run.sh`'s table are CPU seconds, user + sys, children
included, so they do not include wall-clock start-up. See [Start-up](#start-up),
which is measured separately and by the clock, not by CPU.

## What a process costs on z/OS

Measured on z/OS 29.00 with IBM Open XL C/C++, the same compiler and the
same source in both modes, so the 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.

Before the run-time was LPA-resident (2.1 era):

| | `-m64` | `-m32` | ratio |
| --- | --- | --- | --- |
| `fork()` + `wait()`, empty parent | 4.20 ms | 0.10 ms | 42× |
| the same, 8 MB touched | 7.20 ms | 3.30 ms | 2× |
| the same, 64 MB touched | 28.2 ms | 27.7 ms | 1.0× |
| start a trivial program | 29.5 ms | 1.1 ms | 27× |

Those two fixed costs are gone. With the 64-bit run-time in the LPA
(2.2, `-std=c23`), the same probes read 1.5-1.6 ms per start at either
width and 0.10 ms per `fork()`; the per-byte copy cost stays. [It was
the LPA](#it-was-the-lpa) has the either-side table.

The mechanism those old numbers exposed is worth keeping in mind, since
any z/OS system without the LPA change still has it:

- 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.

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 was slow, at 64 bits only, was the C run-time being loaded out of
the linklist in every new address space. Two passages in IBM's own
books point at it: the [LE Programming Guide for 64-bit](https://www.ibm.com/docs/en/zos)
warns that "the creation and termination of that HLL environment
multiple times is needlessly inefficient", and the initialization
manual's answer to that is the dynamic LPA. On a system with the
run-time in the LPA that cost is 1.3 ms of wall time per start at
either width, and no LE option moves it.

For scale, `/bin/sh` (z/OS sh, AMODE 31) starts in 2.2 ms. In the 2.1 era
that compared against 6.01 s for 200 zash starts at `-m64`. With the
run-time in the LPA, zash starts in 1.34 ms: the shell is now the faster
of the two.

### Probing it yourself

The probes are in `bench/probes/`: `fk3.c` measures `fork()` against the
storage the parent holds, `tiny.c` is the program that does nothing, and
`tiny31.s`/`tiny64.s` are the same start in HLASM, with no C runtime at
all. `run.sh` builds and times all of them at both widths:

```sh
sh bench/probes/run.sh                        # N=..., TUNE='-mtune=native'
```

Two traps, both of which produced wrong answers here first:

- 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.

## 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, 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:

| program | AMODE | run-time | per start |
| --- | --- | --- | --- |
| `tiny31` | 31 | none | 0.8 ms |
| `tiny64` | 64 | none | 0.8 ms |
| `c32` | 31 | LE, XPLINK | 2.0 ms |
| `c32n` | 31 | LE, non-XPLINK | 2.0 ms |
| `c64` | 64 | LE, XPLINK | 29.4 ms |

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 to 1.1 ms in an earlier session; the absolute figures drift, the
comparison holds.)

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 LPA result below rules that explanation out:
the 4 ms was the child loading the runtime, and with it loaded once the
64-bit `fork()` is 0.10 ms, the 31-bit number.

The probes are kept in `bench/probes/`; [notes/investigation.md](../notes/investigation.md)
has the reasoning behind them and lists what they leave 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`. None of them moves the
number.

| `_CEE_RUNOPTS` | fork | start |
| --- | --- | --- |
| defaults | 4.4 ms | 29.4 ms |
| `HEAPPOOLS64(OFF)` | 4.2 ms | 29.4 ms |
| `HEAP64(256M,256M,KEEP,16M,16M,KEEP,4M,4M,FREE),HEAPPOOLS64(OFF)` | 4.4 ms | — |
| `STACK64(1M,1M,4M)` | — | 29.3 ms |
| `STACK64(1M,1M,64M)` | 4.2 ms | 29.7 ms |
| all of the small ones together | — | 29.4 ms |

Everything lands inside the run-to-run spread. So there is nothing to
tune and nothing worth building into the program with a `CELQUOPT`
module: the cost is not in the sizes LE is asked for.

`HEAP64` numbers are in megabytes and must carry an `M`
(`HEAP64(256K,...)` is rejected). To see the effective options and the
storage actually used, in case a future LE release behaves differently:

```sh
_CEE_RUNOPTS='RPTOPTS(ON),RPTSTG(ON)' ./zash -c : 2>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`.

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, `-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.

## 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=$(<hugefile)`; that limit is gone
   with the 64-bit build.

## Diagnostics

What to run when a number here is surprising.

### Which address mode is this program, and does every CSECT agree?

```sh
file ./zash                                  # amode=31 or amode=64
 echo ' LISTLOAD OUTPUT=BOTH' | amblist ./zash > 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.