blob: fd7989b8ff3be7cab3b711fc2f9c48212c9be05b [file] [log] [blame]
José Fonseca299a1b32012-01-26 20:32:59 +00001#!/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
29Run as:
30
31 apitrace pickle foo.trace | python unpickle.py
32
33'''
34
35
José Fonseca447576d2012-01-27 14:27:13 +000036import optparse
José Fonseca299a1b32012-01-26 20:32:59 +000037import cPickle as pickle
38import sys
39import time
40
41
José Fonsecabaee5792012-03-15 00:09:25 +000042class Call:
43
44 def __init__(self, callTuple):
45 self.no, self.functionName, self.args, self.ret = callTuple
46
47 def __str__(self):
48 s = self.functionName
49 if self.no is not None:
50 s = str(self.no) + ' ' + s
51 s += '(' + ', '.join(map(repr, self.args)) + ')'
52 if self.ret is not None:
53 s += ' = '
54 s += repr(self.ret)
55 return s
56
57 def __eq__(self, other):
58 return \
59 self.functionName == other.functionName and \
60 self.args == other.args and \
61 self.ret == other.ret
62
63 def __hash__(self):
64 # XXX: hack due to unhashable types
65 #return hash(self.functionName) ^ hash(tuple(self.args)) ^ hash(self.ret)
66 return hash(self.functionName) ^ hash(repr(self.args)) ^ hash(repr(self.ret))
67
68
69
70class Unpickler:
71
72 callFactory = Call
73
74 def __init__(self, stream):
75 self.stream = stream
76
77 def parse(self):
78 while self.parseCall():
79 pass
80
81 def parseCall(self):
82 try:
83 callTuple = pickle.load(self.stream)
84 except EOFError:
85 return False
86 else:
87 call = self.callFactory(callTuple)
88 self.handleCall(call)
89 return True
90
91 def handleCall(self, call):
92 pass
93
94
95class Counter(Unpickler):
96
97 def __init__(self, stream, quiet):
98 Unpickler.__init__(self, stream)
99 self.quiet = quiet
100 self.calls = 0
101
102 def handleCall(self, call):
103 if not self.quiet:
104 sys.stdout.write(str(call))
105 sys.stdout.write('\n')
106 self.calls += 1
107
108
José Fonseca299a1b32012-01-26 20:32:59 +0000109def main():
José Fonseca447576d2012-01-27 14:27:13 +0000110 optparser = optparse.OptionParser(
111 usage="\n\tapitrace pickle trace. %prog [options]")
112 optparser.add_option(
113 '--quiet',
114 action="store_true", dest="quiet", default=False,
115 help="don't dump calls to stdout")
116
117 (options, args) = optparser.parse_args(sys.argv[1:])
118
119 if args:
120 optparser.error('unexpected arguments')
121
José Fonsecac6977a72012-01-27 14:28:06 +0000122 # Change stdin to binary mode
123 try:
124 import msvcrt
125 except ImportError:
126 pass
127 else:
128 import os
129 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
130
José Fonseca299a1b32012-01-26 20:32:59 +0000131 startTime = time.time()
José Fonsecabaee5792012-03-15 00:09:25 +0000132 parser = Counter(sys.stdin, options.quiet)
133 parser.parse()
José Fonseca299a1b32012-01-26 20:32:59 +0000134 stopTime = time.time()
135 duration = stopTime - startTime
José Fonsecabaee5792012-03-15 00:09:25 +0000136 sys.stderr.write('%u calls, %.03f secs, %u calls/sec\n' % (parser.calls, duration, parser.calls/duration))
José Fonseca299a1b32012-01-26 20:32:59 +0000137
138
139if __name__ == '__main__':
140 main()