aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/scripts/flamegraph.sh
blob: 3a6c9e21c3409e5dfd4e704f8a08600c9cd2d2e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env bash

# Copyright (c) 2023 Egor Tensin <egor@tensin.name>
# This file is part of the "cimple" project.
# For details, see https://github.com/egor-tensin/cimple.
# Distributed under the MIT License.

# This script attaches to a set of processes and makes a flame graph using
# flamegraph.pl.

set -o errexit -o nounset -o pipefail
shopt -s inherit_errexit lastpipe

script_name="$( basename -- "${BASH_SOURCE[0]}" )"
readonly script_name

script_usage() {
	local msg
	for msg; do
		echo "$script_name: $msg"
	done

	echo "usage: $script_name OUTPUT_PATH PID [PID...]"
}

join_pids() {
	local result=''
	while [ "$#" -gt 0 ]; do
		if [ -n "$result" ]; then
			result="$result,"
		fi
		result="$result$1"
		shift
	done
	echo "$result"
}

output_dir=''

cleanup() {
	if [ -n "$output_dir" ]; then
		echo "Removing temporary directory: $output_dir"
		rm -rf -- "$output_dir"
	fi
}

make_graph() {
	wait "$record_pid" || true
	perf script -i "$output_dir/perf.data" > "$output_dir/perf.out"
	stackcollapse-perf.pl "$output_dir/perf.out" > "$output_dir/perf.folded"
	flamegraph.pl --width 1400 --color mem "$output_dir/perf.folded" > "$output_path"
}

record_pid=''

stop_record() {
	echo "Stopping 'perf record' process $record_pid"
	kill -SIGINT "$record_pid"
	make_graph
}

check_tools() {
	local tool
	for tool in perf stackcollapse-perf.pl flamegraph.pl; do
		if ! command -v "$tool" &> /dev/null; then
			echo "$script_name: $tool is missing" >&2
			exit 1
		fi
	done
}

main() {
	trap cleanup EXIT
	check_tools

	if [ "$#" -lt 1 ]; then
		script_usage "output path is required" >&2
		exit 1
	fi
	local output_path="$1"
	output_path="$( realpath -- "$output_path" )"
	shift

	if [ "$#" -lt 1 ]; then
		script_usage "at least one process ID is required" >&2
		exit 1
	fi
	local pids
	pids="$( join_pids "$@" )"
	shift

	echo "Output path: $output_path"
	echo "PIDs: $pids"

	output_dir="$( dirname -- "$output_path" )"
	output_dir="$( mktemp -d --tmpdir="$output_dir" )"
	readonly output_dir

	perf record \
		-o "$output_dir/perf.data" \
		--freq=99 \
		--call-graph dwarf,65528 \
		--pid="$pids" \
		--no-inherit &

	record_pid="$!"
	echo "Started 'perf record' process $record_pid"
	trap stop_record SIGINT SIGTERM

	make_graph
}

main "$@"