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
|
# 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.
## Running
On Linux, `make bench`.
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
```
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,
ksh88 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 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).
## What a process costs on z/OS
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 address mode.
| | `-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× |
Two separate 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
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, and not C code: 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 1.0 ms. Only the
64-bit address space is slow. 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.
What is left is Language Environment building the enclave. 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
of wall time, in both modes, and no option moves it.
For scale, the same measurement on `/bin/sh` (ksh88, AMODE 31) is 2.1 ms
per start and 0.42 s for 200 of them, against 6.01 s for 200 starts of
zash at `-m64`.
### Probing it yourself
Two small programs, no shell involved. This one measures `fork()`:
```c
/* fk3.c: fork() cost against the storage the parent holds. p[0] is read
after the loop so the allocation cannot be optimised away. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/times.h>
int main(int argc, char **argv) {
long mb = argc > 1 ? atol(argv[1]) : 0;
int n = argc > 2 ? atoi(argv[2]) : 100;
char *p = NULL;
if (mb > 0) {
p = malloc((size_t)mb * 1024 * 1024);
if (!p) { printf("malloc failed\n"); return 1; }
memset(p, 'x', (size_t)mb * 1024 * 1024);
}
struct tms a, b;
times(&a);
for (int i = 0; i < n; i++) {
pid_t c = fork();
if (c == 0) _exit(0);
int st;
waitpid(c, &st, 0);
}
times(&b);
clock_t t = (b.tms_utime - a.tms_utime) + (b.tms_stime - a.tms_stime) +
(b.tms_cutime - a.tms_cutime) + (b.tms_cstime - a.tms_cstime);
printf(" %4ld MB: %6.2f s (%5.2f ms per fork)", mb, (double)t / 100.0,
(double)t / 100.0 * 1000.0 / n);
if (p) printf(" [p[0]=%c]", p[0]);
printf("\n");
return 0;
}
```
```sh
ibm-clang -O2 -m64 -o fk_64 fk3.c && ibm-clang -O2 -m32 -o fk_32 fk3.c
for m in 0 8 64; do ./fk_64 $m 100; ./fk_32 $m 100; done
```
Start-up cost is measured by starting a program that does nothing, so
that nothing but the run-time matters:
```sh
echo 'int main(void) { return 0; }' > tiny.c
ibm-clang -O2 -m64 -o t64 tiny.c && ibm-clang -O2 -m32 -o t32 tiny.c
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:
- **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.
## 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.
| `_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.
## Start-up is not in these numbers
`run.sh` takes CPU time, so the ~28 ms of wall time a 64-bit program
spends starting is invisible to every row. That is why `subshell` and
`zz-fgsub` can read 0.00 for zash while `zash -c :` takes 30 ms of wall
time. Both statements are true, and the CPU table is the wrong place to
look for start-up cost.
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. 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.
The same sources build at either width with the same compiler:
```sh
ibm-clang ... -O2 -m32 -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 ksh88, CPU seconds, ASCII build, same box:
| benchmark | zash `-m64` | zash `-m32` | ksh88 |
| --- | --- | --- | --- |
| `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. 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:
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
the assembler is 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.
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.
32-bit pointers also shrink the nodes, arenas and hash tables, which is
why no row got worse.
## 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 would fork 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`. 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.
**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–0.02 difference as noise
and re-run before believing it.
|