OILS / benchmarks / gc.sh View on Github | oilshell.org

716 lines, 403 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# benchmarks/gc.sh <function name>
5
6set -o nounset
7set -o pipefail
8set -o errexit
9
10REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
11
12source benchmarks/common.sh # benchmark-html-head
13source benchmarks/cachegrind.sh # with-cachegrind
14source build/dev-shell.sh # R_LIBS_USER
15source test/tsv-lib.sh
16
17readonly BASE_DIR=_tmp/gc
18
19# duplicated in benchmarks/gc-cachegrind.sh
20readonly BASE_DIR_CACHEGRIND=_tmp/gc-cachegrind
21
22# See benchmarks/gperftools.sh. I think the Ubuntu package is very old
23
24download-tcmalloc() {
25 # TODO: move this to ../oil_DEPS ?
26 wget --directory _deps \
27 https://github.com/gperftools/gperftools/releases/download/gperftools-2.10/gperftools-2.10.tar.gz
28
29 # Then ./configure; make; sudo make install
30 # installs in /usr/local/lib
31
32 # Note: there's a warning about libunwind -- maybe install that first. Does
33 # it only apply to CPU profiles?
34}
35
36debug-tcmalloc() {
37 touch mycpp/marksweep_heap.cc
38
39 # No evidence of difference
40 for bin in _bin/cxx-{opt,opt+tcmalloc}/osh; do
41 echo $bin
42 ninja $bin
43
44 ldd $bin
45 echo
46
47 ls -l $bin
48 echo
49
50 # Check what we're linking against
51 nm $bin | egrep -i 'malloc|calloc'
52 #wc -l
53 echo
54 done
55}
56
57install-m32() {
58 # needed to compile with -m32
59 sudo apt-get install gcc-multilib g++-multilib
60}
61
62max-rss() {
63 # %e is real time
64 /usr/bin/time --format '%e %M' -- "$@"
65}
66
67compare-m32() {
68 for bin in _bin/cxx-opt{,32}/osh; do
69 echo $bin
70 ninja $bin
71
72 ldd $bin
73 echo
74
75 file $bin
76 echo
77
78 ls -l $bin
79 echo
80
81 # 141136 KiB vs. 110924 KiB. Significant savings, but it's slower.
82 max-rss $bin --ast-format none -n benchmarks/testdata/configure-coreutils
83
84 done
85}
86
87banner() {
88 echo -----
89 echo "$@"
90}
91
92print-tasks() {
93 local -a workloads=(
94 parse.configure-coreutils
95 parse.configure-cpython
96 parse.abuild
97 ex.bashcomp-parse-help # only runs with bash
98 ex.abuild-print-help # bash / dash / zsh
99 ex.compute-fib # bash / dash / zsh
100 )
101
102 local -a shells=(
103 "bash$TAB-"
104 "dash$TAB-"
105 "zsh$TAB-"
106
107 "_bin/cxx-opt+bumpleak/osh${TAB}mut"
108 "_bin/cxx-opt+bumproot/osh${TAB}mut"
109
110 "_bin/cxx-opt+bumpsmall/osh${TAB}mut+alloc"
111 "_bin/cxx-opt+nopool/osh${TAB}mut+alloc"
112 "_bin/cxx-opt+nopool/osh${TAB}mut+alloc+free+gc"
113
114 # these have trivial GC stats
115 "_bin/cxx-opt/osh${TAB}mut+alloc"
116 "_bin/cxx-opt/osh${TAB}mut+alloc+free"
117 # good GC stats
118 "_bin/cxx-opt/osh${TAB}mut+alloc+free+gc"
119 "_bin/cxx-opt/osh${TAB}mut+alloc+free+gc+exit"
120 )
121
122 if test -n "${TCMALLOC:-}"; then
123 shells+=(
124 "_bin/cxx-opt+tcmalloc/osh${TAB}mut+alloc"
125 "_bin/cxx-opt+tcmalloc/osh${TAB}mut+alloc+free"
126 "_bin/cxx-opt+tcmalloc/osh${TAB}mut+alloc+free+gc"
127 )
128 fi
129
130 local id=0
131
132 for workload in "${workloads[@]}"; do
133 for shell in "${shells[@]}"; do
134 local row_part="$workload${TAB}$shell"
135
136 # Skip these rows
137 case $row_part in
138 "ex.bashcomp-parse-help${TAB}dash"*)
139 continue
140 ;;
141 "ex.bashcomp-parse-help${TAB}zsh"*)
142 continue
143 ;;
144 esac
145
146 local join_id="gc-$id"
147 local row="$join_id${TAB}$row_part"
148 echo "$row"
149
150 id=$((id + 1))
151
152 done
153
154 # Run a quick 10 tasks
155 if test -n "${QUICKLY:-}" && test $id -gt 10; then
156 break
157 fi
158 done
159}
160
161print-cachegrind-tasks() {
162 local -a workloads=(
163 # coreutils is on osh-parser
164 #parse.configure-coreutils
165
166 #parse.configure-cpython
167
168 # Faster tasks, like benchmarks/uftrace, which is instrumented
169 parse.abuild
170 ex.compute-fib
171 )
172
173 local -a shells=(
174 "bash${TAB}-"
175 "_bin/cxx-opt+bumpleak/osh${TAB}mut"
176 "_bin/cxx-opt+bumproot/osh${TAB}mut"
177
178 "_bin/cxx-opt+bumpsmall/osh${TAB}mut+alloc"
179 "_bin/cxx-opt+nopool/osh${TAB}mut+alloc"
180 "_bin/cxx-opt+nopool/osh${TAB}mut+alloc+free+gc"
181
182 "_bin/cxx-opt/osh${TAB}mut+alloc"
183 "_bin/cxx-opt/osh${TAB}mut+alloc+free"
184 "_bin/cxx-opt/osh${TAB}mut+alloc+free+gc"
185 "_bin/cxx-opt/osh${TAB}mut+alloc+free+gc+exit"
186 )
187
188 local id=0
189 for workload in "${workloads[@]}"; do
190 for shell in "${shells[@]}"; do
191 local row_part="$workload${TAB}$shell"
192
193 local join_id="cachegrind-$id"
194 local row="$join_id${TAB}$row_part"
195 echo "$row"
196
197 id=$((id + 1))
198 done
199 done
200 #print-tasks | egrep 'configure-coreutils' | egrep osh
201}
202
203
204readonly BIG_THRESHOLD=$(( 1 * 1000 * 1000 * 1000 )) # 1 B
205
206run-tasks() {
207 local tsv_out=$1
208 local mode=${2:-time}
209
210 while read -r join_id task sh_path shell_runtime_opts; do
211
212 # Parse different files
213 case $task in
214 parse.configure-coreutils)
215 data_file='benchmarks/testdata/configure-coreutils'
216 ;;
217 parse.configure-cpython)
218 data_file='Python-2.7.13/configure'
219 ;;
220 parse.abuild)
221 data_file='benchmarks/testdata/abuild'
222 ;;
223 esac
224
225 # Construct argv for each task
226 local -a argv
227 case $task in
228 parse.*)
229 argv=( -n $data_file )
230
231 case $sh_path in
232 _bin/*/osh)
233 argv=( --ast-format none "${argv[@]}" )
234 ;;
235 esac
236 ;;
237
238 ex.bashcomp-parse-help)
239 argv=( benchmarks/parse-help/pure-excerpt.sh parse_help_file
240 benchmarks/parse-help/clang.txt )
241 ;;
242
243 ex.abuild-print-help)
244 argv=( testdata/osh-runtime/abuild -h )
245 ;;
246
247 ex.compute-fib)
248 # fewer iterations when instrumented
249 local iters
250 if test $mode = time; then
251 iters=100
252 else
253 iters=10
254 fi
255
256 argv=( benchmarks/compute/fib.sh $iters 44 )
257 ;;
258
259 *)
260 die "Invalid task $task"
261 ;;
262 esac
263
264 echo $join_id $task $sh_path $shell_runtime_opts
265
266 argv=( $sh_path "${argv[@]}" )
267 #echo + "${argv[@]}"
268 #set -x
269
270 if test $mode = cachegrind; then
271 # Add prefix
272 argv=( $0 with-cachegrind $BASE_DIR_CACHEGRIND/raw/$join_id.txt "${argv[@]}" )
273 fi
274
275 # Wrap in a command that writes one row of a TSV
276 # Note: for cachegrind, we need the join ID, but the --rusage is meaningless
277 local -a instrumented=(
278 time-tsv -o $tsv_out --append
279 --rusage
280 --field "$join_id" --field "$task" --field "$sh_path"
281 --field "$shell_runtime_opts"
282 -- "${argv[@]}"
283 )
284
285 # Run with the right environment variables
286
287 case $shell_runtime_opts in
288 -)
289 "${instrumented[@]}" > /dev/null
290 ;;
291 mut)
292 OILS_GC_STATS=1 \
293 "${instrumented[@]}" > /dev/null
294 ;;
295 mut+alloc)
296 # disable GC with big threshold
297 OILS_GC_STATS=1 OILS_GC_THRESHOLD=$BIG_THRESHOLD \
298 "${instrumented[@]}" > /dev/null
299 ;;
300 mut+alloc+free)
301 # do a single GC on exit
302 OILS_GC_STATS=1 OILS_GC_THRESHOLD=$BIG_THRESHOLD OILS_GC_ON_EXIT=1 \
303 "${instrumented[@]}" > /dev/null
304 ;;
305 mut+alloc+free+gc)
306 # Default configuration
307 #
308 # Save the GC stats here. None of the other runtime options are that
309 # interesting.
310
311 if test $mode = 'time' && test $sh_path != _bin/cxx-opt+nopool/osh; then
312 OILS_GC_STATS_FD=99 \
313 "${instrumented[@]}" > /dev/null 99>$BASE_DIR/raw/$join_id.txt
314 else
315 "${instrumented[@]}" > /dev/null
316 fi
317 ;;
318 mut+alloc+free+gc+exit)
319 # also GC on exit
320 OILS_GC_STATS=1 OILS_GC_ON_EXIT=1 \
321 "${instrumented[@]}" > /dev/null
322 ;;
323
324 *)
325 die "Invalid shell runtime opts $shell_runtime_opts"
326 ;;
327 esac
328
329 done
330
331 # TODO: OILS_GC_STATS_FD and tsv_column_from_files.py
332}
333
334fd-demo() {
335 local out=_tmp/gc/demo.txt
336
337 local bin=_bin/cxx-dbg/oils-for-unix
338 ninja $bin
339
340 # Hm you can't do $fd>out.txt, but that's OK
341 local fd=99
342
343 OILS_GC_STATS_FD=$fd 99>$out \
344 $bin --ast-format none -n benchmarks/testdata/configure
345
346 ls -l $out
347 cat $out
348}
349
350more-variants() {
351 # TODO: could revive this
352
353 case $compare_more in
354 (*m32*)
355 # Surprisingly, -m32 is SLOWER, even though it allocates less.
356 # My guess is because less work is going into maintaining this code path in
357 # GCC.
358
359 # 223 ms
360 # 61.9 MB bytes allocated
361 local bin=_bin/cxx-opt32/oils-for-unix
362 OILS_GC_THRESHOLD=$big_threshold \
363 run-osh $tsv_out $bin 'm32 mutator+malloc' $file
364
365 # 280 ms
366 OILS_GC_STATS=1 \
367 run-osh $tsv_out $bin 'm32 mutator+malloc+free+gc' $file
368 ;;
369 esac
370
371 # Show log of GC
372 case $compare_more in
373 (*gcverbose*)
374 local bin=_bin/cxx-gcverbose/oils-for-unix
375 # 280 ms
376 OILS_GC_STATS=1 OILS_GC_ON_EXIT=1 \
377 run-osh $tsv_out $bin 'gcverbose mutator+malloc+free+gc' $file
378 ;;
379 esac
380
381 if command -v pretty-tsv; then
382 pretty-tsv $tsv_out
383 fi
384}
385
386build-binaries() {
387 local -a bin=( _bin/cxx-opt{,+bumpleak,+bumproot,+bumpsmall,+nopool}/osh )
388
389 if test -n "${TCMALLOC:-}"; then
390 bin+=( _bin/cxx-opt+tcmalloc/osh )
391 fi
392 ninja "${bin[@]}"
393}
394
395measure-all() {
396 build-binaries
397
398 local tsv_out=${1:-$BASE_DIR/raw/times.tsv}
399 mkdir -p $(dirname $tsv_out)
400
401 # Make the header
402 time-tsv -o $tsv_out --print-header \
403 --rusage --field join_id --field task --field sh_path --field shell_runtime_opts
404
405 time print-tasks | run-tasks $tsv_out
406
407 if command -v pretty-tsv; then
408 pretty-tsv $tsv_out
409 fi
410}
411
412measure-cachegrind() {
413 build-binaries
414
415 local tsv_out=${1:-$BASE_DIR_CACHEGRIND/raw/times.tsv}
416
417 mkdir -p $(dirname $tsv_out)
418
419 # Make the header
420 time-tsv -o $tsv_out --print-header \
421 --rusage --field join_id --field task --field sh_path --field shell_runtime_opts
422
423 print-cachegrind-tasks | run-tasks $tsv_out cachegrind
424
425 # TODO: join cachegrind columns
426
427 if command -v pretty-tsv; then
428 pretty-tsv $tsv_out
429 fi
430}
431
432print-report() {
433 local in_dir=$1
434
435 benchmark-html-head 'Memory Management Overhead'
436
437 cat <<EOF
438 <body class="width60">
439 <p id="home-link">
440 <a href="/">oilshell.org</a>
441 </p>
442EOF
443
444 cmark << 'EOF'
445## Memory Management Overhead
446
447Source code: [oil/benchmarks/gc.sh](https://github.com/oilshell/oil/tree/master/benchmarks/gc.sh)
448EOF
449
450 cmark << 'EOF'
451### GC Stats
452
453EOF
454
455 tsv2html $in_dir/gc_stats.tsv
456
457 cmark << 'EOF'
458
459- Underlying data: [stage2/gc_stats.tsv](stage2/gc_stats.tsv)
460- More columns: [stage1/gc_stats.tsv](stage1/gc_stats.tsv)
461
462### Resource Usage
463
464#### parse.configure-cpython
465
466EOF
467
468 tsv2html $in_dir/parse.configure-cpython.tsv
469
470 cmark << 'EOF'
471#### parse.configure-coreutils
472
473Parsing the autoconf-generated `configure` script from GNU coreutils.
474
475Note that unlike other shells, `osh -n` retains all nodes on purpose. (See the
476[parser benchmark](../osh-parser/index.html)).
477
478EOF
479
480 tsv2html $in_dir/parse.configure-coreutils.tsv
481
482 cmark <<'EOF'
483#### parse.abuild
484
485Parsing `abuild` from Alpine Linux.
486EOF
487
488 tsv2html $in_dir/parse.abuild.tsv
489
490 cmark <<'EOF'
491#### ex.compute-fib
492
493A synthetic benchmark for POSIX shell arithmetic.
494EOF
495
496 tsv2html $in_dir/ex.compute-fib.tsv
497
498 cmark <<'EOF'
499#### ex.bashcomp-parse-help
500
501A realistic `bash-completion` workload.
502EOF
503
504 tsv2html $in_dir/ex.bashcomp-parse-help.tsv
505
506 cmark <<'EOF'
507#### ex.abuild-print-help
508
509Running `abuild -h` from Alpine Linux.
510
511EOF
512
513 tsv2html $in_dir/ex.abuild-print-help.tsv
514
515 cmark << 'EOF'
516- Underlying data: [stage2/times.tsv](stage2/times.tsv)
517EOF
518
519 cat <<EOF
520
521 </body>
522</html>
523EOF
524}
525
526make-report() {
527 mkdir -p $BASE_DIR/{stage1,stage2}
528
529 # Concatenate tiny files
530 benchmarks/gc_stats_to_tsv.py $BASE_DIR/raw/gc-*.txt \
531 > $BASE_DIR/stage1/gc_stats.tsv
532
533 # Make TSV files
534 benchmarks/report.R gc $BASE_DIR $BASE_DIR/stage2
535
536 # Make HTML
537 benchmarks/report.sh stage3 $BASE_DIR
538}
539
540soil-run() {
541 ### Run in soil/benchmarks
542
543 measure-all
544
545 make-report
546}
547
548#
549# Misc Tests
550#
551
552gc-parse-smoke() {
553 local variant=${1:-opt}
554 local file=${2:-configure}
555
556 local bin=_bin/cxx-$variant/osh
557 ninja $bin
558
559 # OILS_GC_THRESHOLD=1000 OILS_GC_ON_EXIT=1 \
560 time _OILS_GC_VERBOSE=1 OILS_GC_STATS=1 \
561 $bin --ast-format none -n $file
562
563 # No leaks
564 # OILS_GC_STATS=1 OILS_GC_THRESHOLD=1000 OILS_GC_ON_EXIT=1 $bin -n -c '('
565}
566
567gc-parse-big() {
568 local variant=${1:-opt}
569
570 gc-parse-smoke $variant benchmarks/testdata/configure-coreutils
571}
572
573gc-run-smoke() {
574 local variant=${1:-opt}
575
576 local bin=_bin/cxx-$variant/oils-for-unix
577 ninja $bin
578
579 # expose a bug with printf
580 _OILS_GC_VERBOSE=1 OILS_GC_STATS=1 OILS_GC_THRESHOLD=500 OILS_GC_ON_EXIT=1 \
581 $bin -c 'for i in $(seq 100); do printf "%s\\n" "-- $i"; done'
582}
583
584gc-run-oil() {
585 ### Run some scripts from the repo
586
587 local variant=${1:-opt}
588
589 local bin=_bin/cxx-$variant/oils-for-unix
590 ninja $bin
591
592 local i=0
593 for script in */*.sh; do
594 case $script in
595 (build/clean.sh|build/common.sh|build/dev.sh)
596 # Top level does something!
597 echo "=== SKIP $script"
598 continue
599 ;;
600 esac
601
602 echo
603 echo "=== ($i) $script"
604
605 # Just run the top level, which (hopefully) does nothing
606 _OILS_GC_VERBOSE=1 OILS_GC_STATS=1 OILS_GC_THRESHOLD=1000 OILS_GC_ON_EXIT=1 \
607 $bin $script
608
609 i=$((i + 1))
610 if test $i -gt 60; then
611 break
612 fi
613 done
614}
615
616gc-run-big() {
617 local variant=${1:-opt}
618
619 local target=_bin/cxx-$variant/oils-for-unix
620 ninja $target
621
622 local osh=$REPO_ROOT/$target
623
624 local dir=_tmp/gc-run-big
625 rm -r -f -v $dir
626 mkdir -v -p $dir
627
628 pushd $dir
629 time _OILS_GC_VERBOSE=1 OILS_GC_STATS=1 OILS_GC_THRESHOLD=100000 OILS_GC_ON_EXIT=1 \
630 $osh ../../Python-2.7.13/configure
631 popd
632}
633
634run-verbose() {
635 _OILS_GC_VERBOSE=1 OILS_GC_STATS=1 \
636 /usr/bin/time --format '*** MAX RSS KiB = %M' -- \
637 "$@"
638}
639
640# This hit the 24-bit object ID limitation in 2.5 seconds
641# Should be able to run indefinitely.
642run-for-a-long-time() {
643 local bin=_bin/cxx-opt/osh
644 ninja $bin
645 run-verbose $bin benchmarks/compute/fib.sh 10000
646
647 # time _OILS_GC_VERBOSE=1 OILS_GC_STATS=1 _bin/cxx-opt/osh benchmarks/compute/fib.sh 10000
648}
649
650while-loop() {
651 local i=0
652 while test $i -lt 10000; do
653 if ((i % 1000 == 0)) ; then
654 echo $i
655 fi
656 i=$((i + 1))
657 continue # BUG: skipped GC point
658 done
659}
660
661for-loop() {
662 for i in $(seq 10000); do
663 if ((i % 1000 == 0)) ; then
664 echo $i
665 fi
666 continue
667 done
668}
669
670recurse() {
671 local n=${1:-3000}
672
673 if ((n % 100 == 0)) ; then
674 echo $n
675 fi
676
677 if test $n = 0; then
678 return
679 fi
680
681 recurse $((n - 1))
682}
683
684test-loops() {
685 ### Regression for leak
686
687 local bin=_bin/cxx-opt/osh
688 ninja $bin
689
690 run-verbose $bin $0 recurse
691 echo
692
693 run-verbose $bin $0 while-loop
694 echo
695
696 run-verbose $bin $0 for-loop
697}
698
699expand-loop() {
700 local n=$1
701
702 local bin=_bin/cxx-opt/osh
703 ninja $bin
704
705 set -x
706 time _OILS_GC_VERBOSE=1 OILS_GC_STATS=1 \
707 $bin -c "for i in {1..$n}; do echo \$i; done > /dev/null"
708 set +x
709}
710
711test-brace-exp() {
712 expand-loop 330000
713 expand-loop 340000
714}
715
716"$@"