]> git.cworth.org Git - apitrace/blob - scripts/unpickle.py
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / scripts / unpickle.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2012 Jose Fonseca
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25 ##########################################################################/
26
27 '''Sample program for apitrace pickle command.
28
29 Run as:
30
31    apitrace pickle foo.trace | python unpickle.py
32
33 '''
34
35
36 import itertools
37 import optparse
38 import sys
39 import time
40 import re
41 import cPickle as pickle
42
43
44 class Visitor:
45
46     def __init__(self):
47         self.dispatch = {}
48         self.dispatch[type(None)] = self.visitNone
49         self.dispatch[bool] = self.visitBool
50         self.dispatch[int] = self.visitInt
51         self.dispatch[long] = self.visitInt
52         self.dispatch[float] = self.visitFloat
53         self.dispatch[str] = self.visitStr
54         self.dispatch[tuple] = self.visitTuple
55         self.dispatch[list] = self.visitList
56         self.dispatch[dict] = self.visitDict
57         self.dispatch[bytearray] = self.visitByteArray
58
59     def visit(self, obj):
60         method = self.dispatch.get(type(obj), self.visitObj)
61         return method(obj)
62
63     def visitObj(self, obj):
64         raise NotImplementedError
65
66     def visitAtom(self, obj):
67         return self.visitObj(obj)
68
69     def visitNone(self, obj):
70         return self.visitAtom(obj)
71
72     def visitBool(self, obj):
73         return self.visitAtom(obj)
74
75     def visitInt(self, obj):
76         return self.visitAtom(obj)
77
78     def visitFloat(self, obj):
79         return self.visitAtom(obj)
80
81     def visitStr(self, obj):
82         return self.visitAtom(obj)
83
84     def visitIterable(self, obj):
85         return self.visitObj(obj)
86
87     def visitTuple(self, obj):
88         return self.visitIterable(obj)
89
90     def visitList(self, obj):
91         return self.visitIterable(obj)
92
93     def visitDict(self, obj):
94         raise NotImplementedError
95
96     def visitByteArray(self, obj):
97         raise NotImplementedError
98
99
100 class Dumper(Visitor):
101
102     id_re = re.compile('^[_A-Za-z][_A-Za-z0-9]*$')
103
104     def visitObj(self, obj):
105         return repr(obj)
106
107     def visitStr(self, obj):
108         if self.id_re.match(obj):
109             return obj
110         else:
111             return repr(obj)
112
113     def visitTuple(self, obj):
114         return '[' + ', '.join(itertools.imap(self.visit, obj)) + ']'
115
116     def visitList(self, obj):
117         return '(' + ', '.join(itertools.imap(self.visit, obj)) + ')'
118
119     def visitByteArray(self, obj):
120         return 'blob(%u)' % len(obj)
121
122
123 class Hasher(Visitor):
124     '''Returns a hashable version of the objtree.'''
125
126     def visitObj(self, obj):
127         return obj
128
129     def visitAtom(self, obj):
130         return obj
131
132     def visitIterable(self, obj):
133         return tuple(itertools.imap(self.visit, obj))
134
135     def visitByteArray(self, obj):
136         return str(obj)
137
138
139 class Rebuilder(Visitor):
140     '''Returns a hashable version of the objtree.'''
141
142     def visitAtom(self, obj):
143         return obj
144
145     def visitIterable(self, obj):
146         changed = False
147         newItems = []
148         for oldItem in obj:
149             newItem = self.visit(oldItem)
150             if newItem is not oldItem:
151                 changed = True
152             newItems.append(newItem)
153         if changed:
154             klass = type(obj)
155             return klass(newItems)
156         else:
157             return obj
158
159     def visitByteArray(self, obj):
160         return obj
161
162
163 class Call:
164
165     def __init__(self, callTuple):
166         self.no, self.functionName, self.args, self.ret = callTuple
167         self._hash = None
168
169     def __str__(self):
170         s = self.functionName
171         if self.no is not None:
172             s = str(self.no) + ' ' + s
173         dumper = Dumper()
174         s += '(' + ', '.join(itertools.imap(dumper.visit, self.args)) + ')'
175         if self.ret is not None:
176             s += ' = '
177             s += dumper.visit(self.ret)
178         return s
179
180     def __eq__(self, other):
181         return \
182             self.functionName == other.functionName and \
183             self.args == other.args and \
184             self.ret == other.ret
185
186     def __hash__(self):
187         if self._hash is None:
188             hasher = Hasher()
189             hashable = hasher.visit(self.functionName), hasher.visit(self.args), hasher.visit(self.ret)
190             self._hash = hash(hashable)
191         return self._hash
192
193
194 class Unpickler:
195
196     callFactory = Call
197
198     def __init__(self, stream):
199         self.stream = stream
200
201     def parse(self):
202         while self.parseCall():
203             pass
204
205     def parseCall(self):
206         try:
207             callTuple = pickle.load(self.stream)
208         except EOFError:
209             return False
210         else:
211             call = self.callFactory(callTuple)
212             self.handleCall(call)
213             return True
214
215     def handleCall(self, call):
216         pass
217
218
219 class Counter(Unpickler):
220
221     def __init__(self, stream, verbose = False):
222         Unpickler.__init__(self, stream)
223         self.verbose = verbose
224         self.numCalls = 0
225         self.functionFrequencies = {}
226
227     def parse(self):
228         Unpickler.parse(self)
229
230         functionFrequencies = self.functionFrequencies.items()
231         functionFrequencies.sort(lambda (name1, freq1), (name2, freq2): cmp(freq1, freq2))
232         for name, frequency in functionFrequencies:
233             sys.stdout.write('%8u %s\n' % (frequency, name))
234
235     def handleCall(self, call):
236         if self.verbose:
237             sys.stdout.write(str(call))
238             sys.stdout.write('\n')
239         self.numCalls += 1
240         try:
241             self.functionFrequencies[call.functionName] += 1
242         except KeyError:
243             self.functionFrequencies[call.functionName] = 1
244
245
246 def main():
247     optparser = optparse.OptionParser(
248         usage="\n\tapitrace pickle <trace> | %prog [options]")
249     optparser.add_option(
250         '-p', '--profile',
251         action="store_true", dest="profile", default=False,
252         help="profile call parsing")
253     optparser.add_option(
254         '-v', '--verbose',
255         action="store_true", dest="verbose", default=False,
256         help="dump calls to stdout")
257
258     (options, args) = optparser.parse_args(sys.argv[1:])
259
260     if args:
261         optparser.error('unexpected arguments')
262
263     # Change stdin to binary mode
264     try:
265         import msvcrt
266     except ImportError:
267         pass
268     else:
269         import os
270         msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
271
272     startTime = time.time()
273     parser = Counter(sys.stdin, options.verbose)
274     parser.parse()
275     stopTime = time.time()
276     duration = stopTime - startTime
277
278     if options.profile:
279         sys.stderr.write('Processed %u calls in %.03f secs, at %u calls/sec\n' % (parser.numCalls, duration, parser.numCalls/duration))
280
281
282 if __name__ == '__main__':
283     main()