3 # Print the percentage of instructions spent in each phase of QEMU
7 # dissect.py [-h] -- <qemu executable> [<qemu executable options>] \
8 # <target executable> [<target executable options>]
10 # [-h] - Print the script arguments help message.
13 # dissect.py -- qemu-arm coulomb_double-arm
15 # This file is a part of the project "TCG Continuous Benchmarking".
17 # Copyright (C) 2020 Ahmed Karaman <ahmedkhaledkaraman@gmail.com>
18 # Copyright (C) 2020 Aleksandar Markovic <aleksandar.qemu.devel@gmail.com>
20 # This program is free software: you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation, either version 2 of the License, or
23 # (at your option) any later version.
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
30 # You should have received a copy of the GNU General Public License
31 # along with this program. If not, see <https://www.gnu.org/licenses/>.
40 def get_JIT_line(callgrind_data
):
42 Search for the first instance of the JIT call in
43 the callgrind_annotate output when ran using --tree=caller
44 This is equivalent to the self number of instructions of JIT.
47 callgrind_data (list): callgrind_annotate output
53 for i
in range(len(callgrind_data
)):
54 if callgrind_data
[i
].strip('\n') and \
55 callgrind_data
[i
].split()[-1] == "[???]":
59 sys
.exit("Couldn't locate the JIT call ... Exiting.")
64 # Parse the command line arguments
65 parser
= argparse
.ArgumentParser(
66 usage
='dissect.py [-h] -- '
67 '<qemu executable> [<qemu executable options>] '
68 '<target executable> [<target executable options>]')
70 parser
.add_argument('command', type=str, nargs
='+', help=argparse
.SUPPRESS
)
72 args
= parser
.parse_args()
74 # Extract the needed variables from the args
75 command
= args
.command
77 # Insure that valgrind is installed
78 check_valgrind
= subprocess
.run(
79 ["which", "valgrind"], stdout
=subprocess
.DEVNULL
)
80 if check_valgrind
.returncode
:
81 sys
.exit("Please install valgrind before running the script.")
83 # Save all intermediate files in a temporary directory
84 with tempfile
.TemporaryDirectory() as tmpdirname
:
85 # callgrind output file path
86 data_path
= os
.path
.join(tmpdirname
, "callgrind.data")
87 # callgrind_annotate output file path
88 annotate_out_path
= os
.path
.join(tmpdirname
, "callgrind_annotate.out")
91 callgrind
= subprocess
.run((["valgrind",
93 "--callgrind-out-file=" + data_path
]
95 stdout
=subprocess
.DEVNULL
,
96 stderr
=subprocess
.PIPE
)
97 if callgrind
.returncode
:
98 sys
.exit(callgrind
.stderr
.decode("utf-8"))
100 # Save callgrind_annotate output
101 with
open(annotate_out_path
, "w") as output
:
102 callgrind_annotate
= subprocess
.run(
103 ["callgrind_annotate", data_path
, "--tree=caller"],
105 stderr
=subprocess
.PIPE
)
106 if callgrind_annotate
.returncode
:
107 sys
.exit(callgrind_annotate
.stderr
.decode("utf-8"))
109 # Read the callgrind_annotate output to callgrind_data[]
111 with
open(annotate_out_path
, 'r') as data
:
112 callgrind_data
= data
.readlines()
114 # Line number with the total number of instructions
115 total_instructions_line_number
= 20
116 # Get the total number of instructions
117 total_instructions_line_data
= \
118 callgrind_data
[total_instructions_line_number
]
119 total_instructions
= total_instructions_line_data
.split()[0]
120 total_instructions
= int(total_instructions
.replace(',', ''))
122 # Line number with the JIT self number of instructions
123 JIT_self_instructions_line_number
= get_JIT_line(callgrind_data
)
124 # Get the JIT self number of instructions
125 JIT_self_instructions_line_data
= \
126 callgrind_data
[JIT_self_instructions_line_number
]
127 JIT_self_instructions
= JIT_self_instructions_line_data
.split()[0]
128 JIT_self_instructions
= int(JIT_self_instructions
.replace(',', ''))
130 # Line number with the JIT self + inclusive number of instructions
131 # It's the line above the first JIT call when running with --tree=caller
132 JIT_total_instructions_line_number
= JIT_self_instructions_line_number
-1
133 # Get the JIT self + inclusive number of instructions
134 JIT_total_instructions_line_data
= \
135 callgrind_data
[JIT_total_instructions_line_number
]
136 JIT_total_instructions
= JIT_total_instructions_line_data
.split()[0]
137 JIT_total_instructions
= int(JIT_total_instructions
.replace(',', ''))
139 # Calculate number of instructions in helpers and code generation
140 helpers_instructions
= JIT_total_instructions
-JIT_self_instructions
141 code_generation_instructions
= total_instructions
-JIT_total_instructions
143 # Print results (Insert commas in large numbers)
144 # Print total number of instructions
145 print('{:<20}{:>20}\n'.
146 format("Total Instructions:",
147 format(total_instructions
, ',')))
148 # Print code generation instructions and percentage
149 print('{:<20}{:>20}\t{:>6.3f}%'.
150 format("Code Generation:",
151 format(code_generation_instructions
, ","),
152 (code_generation_instructions
/ total_instructions
) * 100))
153 # Print JIT instructions and percentage
154 print('{:<20}{:>20}\t{:>6.3f}%'.
155 format("JIT Execution:",
156 format(JIT_self_instructions
, ","),
157 (JIT_self_instructions
/ total_instructions
) * 100))
158 # Print helpers instructions and percentage
159 print('{:<20}{:>20}\t{:>6.3f}%'.
161 format(helpers_instructions
, ","),
162 (helpers_instructions
/total_instructions
)*100))
165 if __name__
== "__main__":