-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile_to_callgrind.py
More file actions
196 lines (144 loc) · 6.48 KB
/
profile_to_callgrind.py
File metadata and controls
196 lines (144 loc) · 6.48 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# -*- coding: utf-8 -*-
# Copyright (c) 2010--2012 Peter Dinges <pdinges@acm.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Docstrings will be empty if optimization (-OO command line flag) is enabled.
# However, the description has to be available regardless of this. Thus put
# it in an extra variable.
__description = \
"""
Convert execution profile dumps generated with the cProfile module to a file
in the Callgrind format (typically named 'callgrind.out...').
The Callgrind format is widely supported in profiling tools. For example,
the excellent profile inspection tool KCacheGrind supports Callgrind files.
This script accepts several profile dumps as input and allows some aggregation
and grouping of the data.
"""
__doc__ = __description
from contextlib import closing
import sys
def main(arguments):
options, arguments = parse_arguments( arguments )
output = get_output( arguments[0], options )
callgraph = get_callgraph( arguments )
#- Output -----------------------------------------------------------------
try:
with closing( output ):
callgrind_profile = CallgrindProfile( callgraph )
callgrind_profile.dump( output )
sys.exit(0)
except IOError as error:
message = "ERROR: Could not store the output.\nReason: {0}"
print( message.format( error ), file=sys.stderr )
sys.exit(1)
from functools import reduce
class CallgrindProfile:
"""
A callgrind profile generated from a callgraph.CallGraph.
See "Callgrind Format Specification" in the Valgrind documentation at
http://www.valgrind.org/docs/manual/cl-format.html (Jan. 26, 2010)
Inspired by the 'lsprofcalltree.py' script by David Allouche et al.
"""
def __init__(self, callgraph):
self.__callgraph = callgraph
def dump(self, file):
print( "events: Ticks", file=file )
self._print_summary( file )
for function in self.__callgraph:
self._print_function( function, file )
def _print_summary(self, file):
total_time = int( self.__callgraph.total_time() * 1000 )
print( "summary: {0:d}".format( total_time ), file=file )
def _print_function(self, function, file):
print( "fi={0:s}".format( function.filename() ), file=file )
print( "fn={0:s}".format( self._absolute_name( function ) ), file=file )
inline_time = int( function.inline_time() * 1000 )
cost_data = ( function.line_number(), inline_time )
print( "{0:d} {1:d}".format( *cost_data ), file=file)
for call in function.outgoing_calls():
self._print_call( call, file )
def _print_call(self, call, file):
callee = call.callee()
print( "cfn={0:s}".format( self._absolute_name( callee ) ), file=file )
print( "cfi={0:s}".format( callee.filename() ), file=file )
calls_data = ( call.total_callcount(), callee.line_number() )
print( "calls={0:d} {1:d}".format( *calls_data ), file=file )
cumulative_time = int( call.cumulative_time() * 1000 )
cost_data = ( call.caller().line_number(), cumulative_time )
print( "{0:d} {1:d}".format( *cost_data ), file=file )
@staticmethod
def _absolute_name(function):
if function.namespace():
return "{0:s}::{1:s}".format( function.namespace(), function.name() )
else:
return function.name()
import optparse
def parse_arguments( arguments ):
usage_string = "%prog <list_of_profile_file_paths>"
parser = optparse.OptionParser(
usage=usage_string,
description=__description.strip()
)
parser.add_option( "-o",
"--output-name",
dest="output_name",
default=None,
metavar="FILE",
help="Write output to FILE instead of "
"callgrind.out.FIRST_INPUT_FILE Use '-' to have the"
"output written to the terminal (stdout)."
)
parser.add_option( "-w",
"--overwrite",
dest="overwrite",
action="store_true",
default=False,
help="Overwrite the output file if it already exists."
)
options, arguments = parser.parse_args( arguments )
if len( arguments ) < 1:
parser.print_usage()
sys.exit(2)
return options, arguments
import os.path
import sys
def get_output( first_profile_name, options ):
if options.output_name == "-":
return sys.stdout
elif not options.output_name:
base_name = os.path.splitext( os.path.basename( first_profile_name ) )[0]
file_name = "callgrind.out.{0}".format( base_name )
directory = os.path.dirname( first_profile_name )
options.output_name = os.path.join( directory, file_name)
if not options.overwrite and os.path.exists( options.output_name ):
message = "ERROR: Output file '{0}' already exists. Aborting."
print( message.format( options.output_name ), file=sys.stderr )
sys.exit(1)
return open( options.output_name, "wt" )
import callgraph
import pstats
import sys
def get_callgraph( profile_names ):
callgraph_ = callgraph.CallGraph()
for profile_name in profile_names:
try:
stats = pstats.Stats( profile_name )
callgraph_.add( stats )
except IOError as error:
message = "ERROR: Could not open profile file.\nReason: {0}"
print( message.format( error), file=sys.stderr )
sys.exit(1)
return callgraph_
if __name__ == '__main__':
main( sys.argv[ 1: ] )