summaryrefslogtreecommitdiff
path: root/llvm/bench.sh
blob: 00edc88edd5d036da08fb3f4b37462d0a222d8ac (plain) (blame)
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
#!/bin/sh
# What compiler instrumentation costs: zig build bench-llvm -Doptimize=ReleaseFast.
# Arguments: ztrace, the bench tool (src/instr/bench.zig), the plugin, the
# runtime, the targets directory.
set -eu
ztrace=$1 bench=$2 plugin=$3 rt=$4 targets=$5
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

# target : argument when traced : argument when not : how many times more
# work the second is. With no tracer a target runs too briefly to time at
# the first.
workloads="fib:24:35:199.006 sort:2000:400000:200 list:5000:1000000:200 bfs:3000:600000:200"
selections="lines stores loads calls vars lines,calls,vars lines,calls,vars,stores edges cmps cyg"

# The best of three runs: milliseconds, then whatever else the tool printed.
best() {
    for _ in 1 2 3; do "$bench" "$@"; done | sort -n | head -1
}

cat <<'EOF2'
Per selection of `ztrace cc --instr`:
  events   emitted at the traced workload
  idle     time against the uninstrumented program, with no tracer
  ring     the same into a ring drained and thrown away
  ns       nanoseconds an event, into that ring
  B/event  bytes of recording an event (`ztrace run`, a moment a line)
  B/line   bytes of recording a source line executed
  rec/s    events a second into the recording
EOF2
for opt in -O0 -O2; do
    for w in $workloads; do
        IFS=: read -r name arg long scale <<EOF2
$w
EOF2
        clang $opt "$targets/instr_$name.c" -o "$tmp/plain"
        start=$(best --untraced "$tmp/plain" 0 | cut -d' ' -f1)
        alone=$(best --untraced "$tmp/plain" "$long" | awk -v start="$start" '{ print $1 - start }')
        printf '\n%s %s: uninstrumented, %.1f ms at %s; traced at %s\n' "$name" "$opt" "$alone" "$long" "$arg"
        printf '  %-24s %9s %7s %9s %6s %8s %7s %9s\n' selection events idle ring ns B/event B/line rec/s
        for s in $selections; do
            "$ztrace" cc --instr "$s" --plugin "$plugin" --rt "$rt" -- clang $opt "$targets/instr_$name.c" -o "$tmp/instr"
            idle=$(best --untraced "$tmp/instr" "$long" | cut -d' ' -f1)
            traced=$(best "$tmp/instr" "$arg")
            # The lines the workload executes, whatever is selected: counted once.
            [ "$s" = lines ] && lines=$(echo "$traced" | sed 's/.* line=\([0-9]*\).*/\1/')
            recorded=$("$ztrace" run -o "$tmp/r.ztrace" -- "$tmp/instr" "$arg" 2>&1 | sed 's/.* \([0-9]*\) bytes.* \([0-9]*\) events\/s.*/\1 \2/')
            echo "$traced $recorded" | awk -v s="$s" -v start="$start" -v alone="$alone" -v scale="$scale" -v idle="$idle" -v lines="$lines" \
                '{ printf "  %-24s %9d %6.1fx %8.1fx %6.1f %8.1f %7.1f %9d\n", s, $2, (idle - start) / alone, ($1 - start) * scale / alone, ($1 - start - alone / scale) * 1e6 / $2, $(NF - 1) / $2, $(NF - 1) / lines, $NF }'
        done
    done
done

"$ztrace" cc --plugin "$plugin" --rt "$rt" -- clang -O0 "$targets/instr_sort.c" -o "$tmp/sort"
echo
echo "sort 2000 -O0, held at each line by the stop mask and resumed at once:"
best --step "$tmp/sort" 2000 | awk '{ ms = $1; sub(/.* line=/, ""); printf "  %d stops in %.0f ms: %.0f a second\n", $1, ms, $1 / ms * 1000 }'