~zinigor/cardano-node/trunk

« back to all changes in this revision

Viewing changes to scripts/benchmarking/lib.sh

  • Committer: Igor Zinovyev
  • Date: 2021-08-13 19:12:27 UTC
  • Revision ID: zinigor@gmail.com-20210813191227-stlnsj3mc5ypwn0c
Tags: upstream-1.27.0
ImportĀ upstreamĀ versionĀ 1.27.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env bash
 
2
# shellcheck disable=SC1090,SC2034,SC2154,SC2039,SC1007,SC2207,SC2145,SC2155,SC2206
 
3
## Don't import this file directly.
 
4
 
 
5
## TODO:  debug the spectacular failure this causes..
 
6
# set -e
 
7
 
 
8
. "${__COMMON_SRCROOT}/scripts/benchmarking/lib-nix.sh"
 
9
 
 
10
##
 
11
## This depends on the setup done by scripts/common.sh
 
12
##
 
13
 
 
14
oprint() {
 
15
        echo "--( $*" >&2
 
16
}
 
17
export -f oprint
 
18
oprint_top() {
 
19
        ## This only prints if ran from the top-level shell process.
 
20
        if test -z "${lib_recursing}"; then oprint "$@"; fi
 
21
}
 
22
export -f oprint_top
 
23
 
 
24
vprint() {
 
25
        if test -n "${verbose}${debug}"; then echo "-- $*" >&2; fi
 
26
}
 
27
export -f vprint
 
28
vprint_top() {
 
29
        ## This only prints if either in debug mode,
 
30
        ## or ran from the top-level shell process.
 
31
        if test -z "${lib_recursing}" -o -n "${debug}"; then vprint "$@"; fi
 
32
}
 
33
export -f vprint_top
 
34
 
 
35
dprint() {
 
36
        if test -n "${debug}"; then echo "-- $*" >&2; fi
 
37
}
 
38
export -f dprint
 
39
 
 
40
fprint() {
 
41
        echo "-- FATAL:  $*" >&2
 
42
}
 
43
export -f fprint
 
44
 
 
45
prebuild() {
 
46
        local pkg="$1" exe="${2:-${pkg}}"
 
47
        vprint "prebuilding the \"$pkg:$exe\" executable in \"${mode}\" mode.."
 
48
        run --no-stats "$pkg" "$exe" --help >/dev/null || true
 
49
}
 
50
export -f prebuild
 
51
 
 
52
run() {
 
53
        if test -n "${verbose}"
 
54
        then run_verbose "$@"
 
55
        else run_quiet   "$@"
 
56
        fi
 
57
}
 
58
export -f run
 
59
 
 
60
run_verbose() {
 
61
        actually_run "$@"
 
62
}
 
63
export -f run_verbose
 
64
 
 
65
run_quiet()
 
66
{
 
67
        ## This nightmare below (minus the last line) does one simple thing:
 
68
        ##   ..it ensures the --build-extra argument has the mode-specific argument
 
69
        ##   that muffles output from the mode-appropriate build tool.
 
70
        local bld_extra=
 
71
        while test -n "$1"
 
72
        do case "$1" in
 
73
           --build-extra )    bld_extra=$2; shift;;
 
74
           * ) break;; esac; shift; done
 
75
        case ${mode} in
 
76
                nix )               bld_extra="--no-build-output --quiet ${bld_extra}";;
 
77
                cabal )             bld_extra="-v0 ${bld_extra}";;
 
78
                stack | stack-nix ) bld_extra="--silent ${bld_extra}";; esac;
 
79
 
 
80
        actually_run --build-extra "${bld_extra}" "$@"
 
81
}
 
82
export -f run_quiet
 
83
 
 
84
actually_run()
 
85
{
 
86
        local ARGS=("$@")
 
87
        dprint "actually_run:  ${ARGS[@]@Q}"
 
88
        local bld_extra=
 
89
        local profile= profile_suffix= user_suffix= profile_prefix= profile_file=
 
90
        local profile_root='./profile'
 
91
        local profmode=
 
92
        local rtsopts=
 
93
        local stats=t
 
94
        local tag=
 
95
        while test -n "$1"
 
96
        do # ARGS=("$@"); dprint "handling: ${ARGS[@]@Q}"
 
97
           case "$1" in
 
98
           --build-extra )       bld_extra="$2"; shift;;
 
99
           --profile )           profile=$2; shift;;
 
100
           --profile-suffix )    user_suffix=.$2; shift;;
 
101
           --no-stats )          stats=;;
 
102
           --tag ) case $2 in
 
103
                           git-head | HEAD ) tag="$(git symbolic-ref HEAD | sed 's,.*/,,g')";;
 
104
                           * )               tag=$2;; esac; shift;;
 
105
           * ) break;; esac; shift; done
 
106
        dprint "actually_run binary extra args:  $*"
 
107
 
 
108
        rtsopts="+RTS";
 
109
        profile_prefix="${profile_root}/$(generate_mnemonic "${tag}")${user_suffix}"
 
110
        case "${profile}" in
 
111
           time )            vprint "profiling:  time"
 
112
                             profmode='-P';  profile_suffix="prof";;
 
113
           space )           vprint "profiling:  space, by default cost centre"
 
114
                             profmode='-h';  profile_suffix="hp";;
 
115
           space-module )    vprint "profiling:  space, by module"
 
116
                             profmode='-hm'; profile_suffix="hp";;
 
117
           space-closure )   vprint "profiling:  space, by closure"
 
118
                             profmode='-hd'; profile_suffix="hp";;
 
119
           space-type )      vprint "profiling:  space, by type"
 
120
                             profmode='-hy'; profile_suffix="hp";;
 
121
           space-retainer )  vprint "profiling:  space, by retainer"
 
122
                             profmode='-hr'; profile_suffix="hp";;
 
123
           space-bio )       vprint "profiling:  space, biographic"
 
124
                             profmode='-hb'; profile_suffix="hp";
 
125
                             rtsopts+=' -N1';;
 
126
           '' )              true;;
 
127
           * ) fprint "--profile requires a mode argument:  time space space-module space-closure space-type space-retainer space-bio"; exit 1;; esac
 
128
 
 
129
        if test -n "${stats}${profile}"
 
130
        then mkdir -p "${profile_root}"
 
131
        fi
 
132
        if test -n "${stats}"
 
133
        then rtsopts+=" --machine-readable -t${profile_prefix}.stats"
 
134
        fi
 
135
        if test -n "${profile}"
 
136
        then profile_file="${profile_prefix}${profile_suffix}"
 
137
             # rtsopts="+RTS --machine-readable -l -t${profile_prefix}.stats -po${profile_prefix} -ol ${profile_prefix}.eventlog ";
 
138
             # Sadly GHC 8.6 doesn't support -ol
 
139
             rtsopts+=" -l -po${profile_prefix} ";
 
140
        fi
 
141
        rtsopts+="${profmode} -RTS";
 
142
        if test "${rtsopts}" = "+RTS -RTS"
 
143
        then rtsopts=; fi
 
144
        vprint "RTS options:    ${rtsopts}"
 
145
        if test -n "${rtsopts}"
 
146
        then vprint "result prefix:  ${profile_prefix}"; fi
 
147
 
 
148
        local pkg="$1"; shift
 
149
        local exe="$1"; shift
 
150
 
 
151
        case ${mode} in
 
152
        nix )       X=(run_nix_executable "$pkg" "$exe" "${bld_extra}" ${rtsopts} "$@");;
 
153
        cabal )     X=(cabal v2-run ${bld_extra} exe:$exe --    ${rtsopts} "$@");;
 
154
        stack )     X=(stack ${bld_extra} run $exe        --    ${rtsopts} "$@");;
 
155
        stack-nix ) X=(stack ${bld_extra} run --nix $exe  --    ${rtsopts} "$@");;
 
156
        * ) echo "INTERNAL ERROR: unknown mode:  $mode" >&2; return 1;;
 
157
        esac
 
158
 
 
159
        vprint "${X[@]}"
 
160
        "${X[@]}"
 
161
        local status=$?
 
162
 
 
163
        if test  -f "${exe}.eventlog"
 
164
        then mv "${exe}.eventlog" "${profile_prefix}.eventlog"
 
165
        fi
 
166
        if test -f "${profile_file}" -a -x "$(command -v profiteur 2>/dev/null)"
 
167
        then profiteur "${profile_file}"
 
168
        fi
 
169
        if test -f "${profile_file}" -a -x "$(command -v profiterole 2>/dev/null)"
 
170
        then profiterole "${profile_file}"
 
171
        fi
 
172
        if test -f "${profile_file}" -a -x "$(command -v eventlog2html 2>/dev/null)"
 
173
        then eventlog2html "${profile_prefix}.eventlog"
 
174
        fi
 
175
 
 
176
        return ${status}
 
177
}
 
178
export -f actually_run
 
179
 
 
180
##
 
181
## Misc stuff, too few to be worth splitting out
 
182
##
 
183
generate_wordpair() {
 
184
        case ${mode} in
 
185
                nix ) nix-shell -p diceware --run 'diceware --no-caps --num 2 --wordlist en_eff -d-' 2>/dev/null || true;;
 
186
                * ) true;; esac
 
187
}
 
188
 
 
189
generate_mnemonic()
 
190
{
 
191
        local mnemonic="${1:-$(generate_wordpair)}"
 
192
        local timestamp="$(date +%s)"
 
193
        local commit="$(git rev-parse HEAD | cut -c-8)"
 
194
        local status=''
 
195
 
 
196
        if git diff --quiet --exit-code
 
197
        then status=pristine
 
198
        else status=modified
 
199
        fi
 
200
 
 
201
        echo "${timestamp}.${commit}.${status}${mnemonic:+.${mnemonic}}"
 
202
}
 
203
export -f generate_mnemonic