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

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