Home | History | Annotate | Download | only in tools
      1 #! /bin/sh
      2 #
      3 # Copyright 2016 the V8 project authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 #
      7 
      8 ########## Global variable definitions
      9 
     10 # Ensure that <your CPU clock> / $SAMPLE_EVERY_N_CYCLES < $MAXIMUM_SAMPLE_RATE.
     11 MAXIMUM_SAMPLE_RATE=10000000
     12 SAMPLE_EVERY_N_CYCLES=10000
     13 SAMPLE_RATE_CONFIG_FILE="/proc/sys/kernel/perf_event_max_sample_rate"
     14 KERNEL_MAP_CONFIG_FILE="/proc/sys/kernel/kptr_restrict"
     15 CALL_GRAPH_METHOD="fp"  # dwarf does not play nice with JITted objects.
     16 EVENT_TYPE=${EVENT_TYPE:=cycles:u}
     17 
     18 ########## Usage
     19 
     20 usage() {
     21 cat << EOF
     22 usage: $0 <benchmark_command>
     23 
     24 Executes <benchmark_command> under observation by Linux perf.
     25 Sampling event is cycles in user space, call graphs are recorded.
     26 EOF
     27 }
     28 
     29 if [ $# -eq 0 ] || [ "$1" = "-h" ]  || [ "$1" = "--help" ] ; then
     30   usage
     31   exit 1
     32 fi
     33 
     34 ########## Actual script execution
     35 
     36 ACTUAL_SAMPLE_RATE=$(cat $SAMPLE_RATE_CONFIG_FILE)
     37 if [ "$ACTUAL_SAMPLE_RATE" -lt "$MAXIMUM_SAMPLE_RATE" ] ; then
     38   echo "Setting appropriate maximum sample rate..."
     39   echo $MAXIMUM_SAMPLE_RATE | sudo tee $SAMPLE_RATE_CONFIG_FILE
     40 fi
     41 
     42 ACTUAL_KERNEL_MAP_RESTRICTION=$(cat $KERNEL_MAP_CONFIG_FILE)
     43 if [ "$ACTUAL_KERNEL_MAP_RESTRICTION" -ne "0" ] ; then
     44   echo "Disabling kernel address map restriction..."
     45   echo 0 | sudo tee $KERNEL_MAP_CONFIG_FILE
     46 fi
     47 
     48 # Extract the command being perfed, so that we can prepend arguments to the
     49 # arguments that the user supplied.
     50 COMMAND=$1
     51 shift 1
     52 
     53 echo "Running..."
     54 perf record -R \
     55   -e $EVENT_TYPE \
     56   -c $SAMPLE_EVERY_N_CYCLES \
     57   --call-graph $CALL_GRAPH_METHOD \
     58   -i "$COMMAND" --perf_basic_prof "$@"
     59