| Ryan Prichard | 41f1970 | 2019-12-23 13:21:42 -0800 | [diff] [blame] | 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 |  | 
|  | 29 | top=$(cd $(dirname $0) && pwd) | 
|  | 30 |  | 
|  | 31 | usage() { | 
|  | 32 | cat <<EOF | 
|  | 33 | Usage: $0 [options] | 
|  | 34 |  | 
|  | 35 | Toolchain: | 
|  | 36 | --cc CC           C compiler driver to compiler the benchmark with | 
|  | 37 |  | 
|  | 38 | Run 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 |  | 
|  | 43 | Timing options: | 
|  | 44 | --multitime N     Use multitime to run the benchmark N iterations | 
|  | 45 | --time CMD        Use the given time command | 
|  | 46 |  | 
|  | 47 | Misc: | 
|  | 48 | --keep-tmp-dir    Don't delete the temporary directory on exit | 
|  | 49 |  | 
|  | 50 | Examples: | 
|  | 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" | 
|  | 53 | EOF | 
|  | 54 | exit 0 | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | cc= | 
|  | 58 | cpu_mask=0 | 
|  | 59 | linker= | 
|  | 60 | use_adb=0 | 
|  | 61 | time_cmd= | 
|  | 62 | keep_tmp_dir=0 | 
|  | 63 |  | 
|  | 64 | need_arg() { | 
|  | 65 | if [ $2 -eq 0 ]; then | 
|  | 66 | echo "error: $1 needs an argument" | 
|  | 67 | exit 1 | 
|  | 68 | fi | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | while [ $# -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 | 
|  | 88 | done | 
|  | 89 |  | 
|  | 90 | # If no --cc is specified, use gcc because that's likely available. | 
|  | 91 | if [ "$cc" == "" ]; then | 
|  | 92 | cc='gcc -ldl -pthread' | 
|  | 93 | fi | 
|  | 94 |  | 
|  | 95 | case "$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 | ;; | 
|  | 107 | esac | 
|  | 108 |  | 
|  | 109 | # Generate the JSON file as a side effect of regenerating the benchmark. | 
|  | 110 | json_file=$top/tmp/libandroid_servers_arm64.json | 
|  | 111 | if [ ! -e $json_file ]; then | 
|  | 112 | $top/gen_bench.sh --keep-tmp-dir --skip-gen-bench | 
|  | 113 | fi | 
|  | 114 |  | 
|  | 115 | tmp_dir=$(mktemp -d) | 
|  | 116 | work_dir=$tmp_dir/linker-reloc-bench | 
|  | 117 |  | 
|  | 118 | python3 -B $top/regen/gen_bench.py --ninja --cc "$cc" $json_file $work_dir | 
|  | 119 |  | 
|  | 120 | env_setup=() | 
|  | 121 | if [ $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 ) | 
|  | 127 | else | 
|  | 128 | target_dir=$work_dir | 
|  | 129 | env_setup+=( env LD_LIBRARY_PATH=$target_dir ) | 
|  | 130 | fi | 
|  | 131 |  | 
|  | 132 | main_cmd=() | 
|  | 133 | if [ "$linker" != "" ]; then | 
|  | 134 | main_cmd+=( $linker ) | 
|  | 135 | fi | 
|  | 136 | main_cmd+=( $target_dir/linker_reloc_bench_main ) | 
|  | 137 |  | 
|  | 138 | # Run the program once to warm the cache. | 
|  | 139 | echo "+ ${env_setup[@]}" "${main_cmd[@]}" | 
|  | 140 | "${env_setup[@]}" "${main_cmd[@]}" | 
|  | 141 |  | 
|  | 142 | # Run the benchmark proper. | 
|  | 143 | run_cmd=() | 
|  | 144 | if [ $cpu_mask -ne 0 ]; then | 
|  | 145 | run_cmd+=( taskset $cpu_mask ) | 
|  | 146 | fi | 
|  | 147 | run_cmd+=( $time_cmd ) | 
|  | 148 |  | 
|  | 149 | echo "+ ${env_setup[@]}" "${run_cmd[@]}" "${main_cmd[@]}" | 
|  | 150 | "${env_setup[@]}" "${run_cmd[@]}" "${main_cmd[@]}" | 
|  | 151 |  | 
|  | 152 | if [ $keep_tmp_dir -eq 0 ]; then | 
|  | 153 | rm -fr $tmp_dir | 
|  | 154 | fi |