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.
11MAXIMUM_SAMPLE_RATE=10000000
12SAMPLE_EVERY_N_CYCLES=10000
13SAMPLE_RATE_CONFIG_FILE="/proc/sys/kernel/perf_event_max_sample_rate"
14KERNEL_MAP_CONFIG_FILE="/proc/sys/kernel/kptr_restrict"
15CALL_GRAPH_METHOD="fp"  # dwarf does not play nice with JITted objects.
16EVENT_TYPE=${EVENT_TYPE:=cycles:u}
17
18########## Usage
19
20usage() {
21cat << EOF
22usage: $0 <benchmark_command>
23
24Executes <benchmark_command> under observation by Linux perf.
25Sampling event is cycles in user space, call graphs are recorded.
26EOF
27}
28
29if [ $# -eq 0 ] || [ "$1" = "-h" ]  || [ "$1" = "--help" ] ; then
30  usage
31  exit 1
32fi
33
34########## Actual script execution
35
36ACTUAL_SAMPLE_RATE=$(cat $SAMPLE_RATE_CONFIG_FILE)
37if [ "$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
40fi
41
42ACTUAL_KERNEL_MAP_RESTRICTION=$(cat $KERNEL_MAP_CONFIG_FILE)
43if [ "$ACTUAL_KERNEL_MAP_RESTRICTION" -ne "0" ] ; then
44  echo "Disabling kernel address map restriction..."
45  echo 0 | sudo tee $KERNEL_MAP_CONFIG_FILE
46fi
47
48# Extract the command being perfed, so that we can prepend arguments to the
49# arguments that the user supplied.
50COMMAND=$1
51shift 1
52
53echo "Running..."
54perf 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