1#!/bin/bash -e
2#
3# Copyright (C) 2019 The Android Open Source Project
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#  * Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11#  * Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in
13#    the documentation and/or other materials provided with the
14#    distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28
29top=$(cd $(dirname $0) && pwd)
30
31usage() {
32cat <<EOF
33Usage: $0 [options]
34
35Toolchain:
36  --cc CC           C compiler driver to compiler the benchmark with
37
38Run config:
39  --cpu-mask MASK   MASK is a hex mask of CPU affinity passed to taskset
40  --linker LINKER   Run the benchmark using a specific dynamic linker
41  --adb             Run the test using adb
42
43Timing options:
44  --multitime N     Use multitime to run the benchmark N iterations
45  --time CMD        Use the given time command
46
47Misc:
48  --keep-tmp-dir    Don't delete the temporary directory on exit
49
50Examples:
51  --cc "\$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android29-clang
52        -fuse-ld=lld -Wl,--pack-dyn-relocs=android+relr -Wl,--use-android-relr-tags"
53EOF
54  exit 0
55}
56
57cc=
58cpu_mask=0
59linker=
60use_adb=0
61time_cmd=
62keep_tmp_dir=0
63
64need_arg() {
65  if [ $2 -eq 0 ]; then
66    echo "error: $1 needs an argument"
67    exit 1
68  fi
69}
70
71while [ $# -gt 0 ]; do
72  arg=$1
73  shift
74  case "$arg" in
75    --cc)           need_arg "$arg" "$#"; cc=$1; shift;;
76    --cpu-mask)     need_arg "$arg" "$#"; cpu_mask=$1; shift;;
77    --linker)       need_arg "#arg" "$#"; linker=$1; shift;;
78    --adb)          use_adb=1;;
79    --multitime)    need_arg "$arg" "$#"; time_cmd=--multitime=$1; shift;;
80    --time)         need_arg "$arg" "$#"; time_cmd=--time=$1; shift;;
81    --keep-tmp-dir) keep_tmp_dir=1;;
82    --help)         usage;;
83    *)
84      echo "error: unrecognized option: $arg"
85      exit 1
86      ;;
87  esac
88done
89
90# If no --cc is specified, use gcc because that's likely available.
91if [ "$cc" == "" ]; then
92  cc='gcc -ldl -pthread'
93fi
94
95case "$time_cmd" in
96  "") time_cmd=time;;
97  --time=*) time_cmd="${time_cmd#--time=}";;
98  --multitime=*)
99    iter="${time_cmd#--multitime=}"
100    if [ $use_adb -eq 1 ]; then
101      time_cmd="/data/local/tmp/multitime"
102    else
103      time_cmd="multitime"
104    fi
105    time_cmd="$time_cmd -n$iter -s0"
106    ;;
107esac
108
109# Generate the JSON file as a side effect of regenerating the benchmark.
110json_file=$top/tmp/libandroid_servers_arm64.json
111if [ ! -e $json_file ]; then
112  $top/gen_bench.sh --keep-tmp-dir --skip-gen-bench
113fi
114
115tmp_dir=$(mktemp -d)
116work_dir=$tmp_dir/linker-reloc-bench
117
118python3 -B $top/regen/gen_bench.py --ninja --cc "$cc" $json_file $work_dir
119
120env_setup=()
121if [ $use_adb -eq 1 ]; then
122  target_dir=/data/local/tmp/linker-reloc-bench
123  adb shell rm -rf $target_dir
124  (cd $tmp_dir; tar -c linker-reloc-bench/linker_reloc_bench_main linker-reloc-bench/*.so \
125    | gzip -1 -c | adb shell 'cd /data/local/tmp && tar xz')
126  env_setup+=( adb shell LD_LIBRARY_PATH=$target_dir )
127else
128  target_dir=$work_dir
129  env_setup+=( env LD_LIBRARY_PATH=$target_dir )
130fi
131
132main_cmd=()
133if [ "$linker" != "" ]; then
134  main_cmd+=( $linker )
135fi
136main_cmd+=( $target_dir/linker_reloc_bench_main )
137
138# Run the program once to warm the cache.
139echo "+ ${env_setup[@]}" "${main_cmd[@]}"
140       "${env_setup[@]}" "${main_cmd[@]}"
141
142# Run the benchmark proper.
143run_cmd=()
144if [ $cpu_mask -ne 0 ]; then
145  run_cmd+=( taskset $cpu_mask )
146fi
147run_cmd+=( $time_cmd )
148
149echo "+ ${env_setup[@]}" "${run_cmd[@]}" "${main_cmd[@]}"
150       "${env_setup[@]}" "${run_cmd[@]}" "${main_cmd[@]}"
151
152if [ $keep_tmp_dir -eq 0 ]; then
153  rm -fr $tmp_dir
154fi
155