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

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