Skip to content

Commit c5a5839

Browse files
ahmedkrmnAMarkovic
authored andcommitted
scripts/performance: Add topN_perf.py script
Syntax: topN_perf.py [-h] [-n] <number of displayed top functions> -- \ <qemu executable> [<qemu executable options>] \ <target executable> [<target execurable options>] [-h] - Print the script arguments help message. [-n] - Specify the number of top functions to print. - If this flag is not specified, the tool defaults to 25. Example of usage: topN_perf.py -n 20 -- qemu-arm coulomb_double-arm Example Output: No. Percentage Name Invoked by ---- ---------- ------------------------- ------------------------- 1 16.25% float64_mul qemu-x86_64 2 12.01% float64_sub qemu-x86_64 3 11.99% float64_add qemu-x86_64 4 5.69% helper_mulsd qemu-x86_64 5 4.68% helper_addsd qemu-x86_64 6 4.43% helper_lookup_tb_ptr qemu-x86_64 7 4.28% helper_subsd qemu-x86_64 8 2.71% f64_compare qemu-x86_64 9 2.71% helper_ucomisd qemu-x86_64 10 1.04% helper_pand_xmm qemu-x86_64 11 0.71% float64_div qemu-x86_64 12 0.63% helper_pxor_xmm qemu-x86_64 13 0.50% 0x00007f7b7004ef95 [JIT] tid 491 14 0.50% 0x00007f7b70044e83 [JIT] tid 491 15 0.36% helper_por_xmm qemu-x86_64 16 0.32% helper_cc_compute_all qemu-x86_64 17 0.30% 0x00007f7b700433f0 [JIT] tid 491 18 0.30% float64_compare_quiet qemu-x86_64 19 0.27% soft_f64_addsub qemu-x86_64 20 0.26% round_to_int qemu-x86_64 Signed-off-by: Ahmed Karaman <[email protected]> Signed-off-by: Aleksandar Markovic <[email protected]> Reviewed-by: Aleksandar Markovic <[email protected]> Message-Id: <[email protected]>
1 parent 2c5b1a7 commit c5a5839

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

scripts/performance/topN_perf.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/usr/bin/env python3
2+
3+
# Print the top N most executed functions in QEMU using perf.
4+
# Syntax:
5+
# topN_perf.py [-h] [-n] <number of displayed top functions> -- \
6+
# <qemu executable> [<qemu executable options>] \
7+
# <target executable> [<target execurable options>]
8+
#
9+
# [-h] - Print the script arguments help message.
10+
# [-n] - Specify the number of top functions to print.
11+
# - If this flag is not specified, the tool defaults to 25.
12+
#
13+
# Example of usage:
14+
# topN_perf.py -n 20 -- qemu-arm coulomb_double-arm
15+
#
16+
# This file is a part of the project "TCG Continuous Benchmarking".
17+
#
18+
# Copyright (C) 2020 Ahmed Karaman <[email protected]>
19+
# Copyright (C) 2020 Aleksandar Markovic <[email protected]>
20+
#
21+
# This program is free software: you can redistribute it and/or modify
22+
# it under the terms of the GNU General Public License as published by
23+
# the Free Software Foundation, either version 2 of the License, or
24+
# (at your option) any later version.
25+
#
26+
# This program is distributed in the hope that it will be useful,
27+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
28+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29+
# GNU General Public License for more details.
30+
#
31+
# You should have received a copy of the GNU General Public License
32+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
33+
34+
import argparse
35+
import os
36+
import subprocess
37+
import sys
38+
39+
40+
# Parse the command line arguments
41+
parser = argparse.ArgumentParser(
42+
usage='topN_perf.py [-h] [-n] <number of displayed top functions > -- '
43+
'<qemu executable> [<qemu executable options>] '
44+
'<target executable> [<target executable options>]')
45+
46+
parser.add_argument('-n', dest='top', type=int, default=25,
47+
help='Specify the number of top functions to print.')
48+
49+
parser.add_argument('command', type=str, nargs='+', help=argparse.SUPPRESS)
50+
51+
args = parser.parse_args()
52+
53+
# Extract the needed variables from the args
54+
command = args.command
55+
top = args.top
56+
57+
# Insure that perf is installed
58+
check_perf_presence = subprocess.run(["which", "perf"],
59+
stdout=subprocess.DEVNULL)
60+
if check_perf_presence.returncode:
61+
sys.exit("Please install perf before running the script!")
62+
63+
# Insure user has previllage to run perf
64+
check_perf_executability = subprocess.run(["perf", "stat", "ls", "/"],
65+
stdout=subprocess.DEVNULL,
66+
stderr=subprocess.DEVNULL)
67+
if check_perf_executability.returncode:
68+
sys.exit(
69+
"""
70+
Error:
71+
You may not have permission to collect stats.
72+
73+
Consider tweaking /proc/sys/kernel/perf_event_paranoid,
74+
which controls use of the performance events system by
75+
unprivileged users (without CAP_SYS_ADMIN).
76+
77+
-1: Allow use of (almost) all events by all users
78+
Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
79+
0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN
80+
Disallow raw tracepoint access by users without CAP_SYS_ADMIN
81+
1: Disallow CPU event access by users without CAP_SYS_ADMIN
82+
2: Disallow kernel profiling by users without CAP_SYS_ADMIN
83+
84+
To make this setting permanent, edit /etc/sysctl.conf too, e.g.:
85+
kernel.perf_event_paranoid = -1
86+
87+
* Alternatively, you can run this script under sudo privileges.
88+
"""
89+
)
90+
91+
# Run perf record
92+
perf_record = subprocess.run((["perf", "record", "--output=/tmp/perf.data"] +
93+
command),
94+
stdout=subprocess.DEVNULL,
95+
stderr=subprocess.PIPE)
96+
if perf_record.returncode:
97+
os.unlink('/tmp/perf.data')
98+
sys.exit(perf_record.stderr.decode("utf-8"))
99+
100+
# Save perf report output to /tmp/perf_report.out
101+
with open("/tmp/perf_report.out", "w") as output:
102+
perf_report = subprocess.run(
103+
["perf", "report", "--input=/tmp/perf.data", "--stdio"],
104+
stdout=output,
105+
stderr=subprocess.PIPE)
106+
if perf_report.returncode:
107+
os.unlink('/tmp/perf.data')
108+
output.close()
109+
os.unlink('/tmp/perf_report.out')
110+
sys.exit(perf_report.stderr.decode("utf-8"))
111+
112+
# Read the reported data to functions[]
113+
functions = []
114+
with open("/tmp/perf_report.out", "r") as data:
115+
# Only read lines that are not comments (comments start with #)
116+
# Only read lines that are not empty
117+
functions = [line for line in data.readlines() if line and line[0]
118+
!= '#' and line[0] != "\n"]
119+
120+
# Limit the number of top functions to "top"
121+
number_of_top_functions = top if len(functions) > top else len(functions)
122+
123+
# Store the data of the top functions in top_functions[]
124+
top_functions = functions[:number_of_top_functions]
125+
126+
# Print table header
127+
print('{:>4} {:>10} {:<30} {}\n{} {} {} {}'.format('No.',
128+
'Percentage',
129+
'Name',
130+
'Invoked by',
131+
'-' * 4,
132+
'-' * 10,
133+
'-' * 30,
134+
'-' * 25))
135+
136+
# Print top N functions
137+
for (index, function) in enumerate(top_functions, start=1):
138+
function_data = function.split()
139+
function_percentage = function_data[0]
140+
function_name = function_data[-1]
141+
function_invoker = ' '.join(function_data[2:-2])
142+
print('{:>4} {:>10} {:<30} {}'.format(index,
143+
function_percentage,
144+
function_name,
145+
function_invoker))
146+
147+
# Remove intermediate files
148+
os.unlink('/tmp/perf.data')
149+
os.unlink('/tmp/perf_report.out')

0 commit comments

Comments
 (0)