blob: 98e2c30b4505d27d6b779200d38382a74004addf [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
José Fonsecabf341272012-03-16 15:40:31 +000046 self._hash = None
José Fonsecabaee5792012-03-15 00:09:25 +000047
48 def __str__(self):
49 s = self.functionName
50 if self.no is not None:
51 s = str(self.no) + ' ' + s
52 s += '(' + ', '.join(map(repr, self.args)) + ')'
53 if self.ret is not None:
54 s += ' = '
55 s += repr(self.ret)
56 return s
57
58 def __eq__(self, other):
59 return \
60 self.functionName == other.functionName and \
61 self.args == other.args and \
62 self.ret == other.ret
63
64 def __hash__(self):
José Fonsecabf341272012-03-16 15:40:31 +000065 if self._hash is None:
66 # XXX: hack due to unhashable types
67 #self._hash = hash(self.functionName) ^ hash(tuple(self.args)) ^ hash(self.ret)
68 self._hash = hash(self.functionName) ^ hash(repr(self.args)) ^ hash(repr(self.ret))
69 return self._hash
José Fonsecabaee5792012-03-15 00:09:25 +000070
71
72class Unpickler:
73
74 callFactory = Call
75
76 def __init__(self, stream):
77 self.stream = stream
78
79 def parse(self):
80 while self.parseCall():
81 pass
82
83 def parseCall(self):
84 try:
85 callTuple = pickle.load(self.stream)
86 except EOFError:
87 return False
88 else:
89 call = self.callFactory(callTuple)
90 self.handleCall(call)
91 return True
92
93 def handleCall(self, call):
94 pass
95
96
97class Counter(Unpickler):
98
99 def __init__(self, stream, quiet):
100 Unpickler.__init__(self, stream)
101 self.quiet = quiet
102 self.calls = 0
103
104 def handleCall(self, call):
105 if not self.quiet:
106 sys.stdout.write(str(call))
107 sys.stdout.write('\n')
108 self.calls += 1
109
110
José Fonseca299a1b32012-01-26 20:32:59 +0000111def main():
José Fonseca447576d2012-01-27 14:27:13 +0000112 optparser = optparse.OptionParser(
113 usage="\n\tapitrace pickle trace. %prog [options]")
114 optparser.add_option(
115 '--quiet',
116 action="store_true", dest="quiet", default=False,
117 help="don't dump calls to stdout")
118
119 (options, args) = optparser.parse_args(sys.argv[1:])
120
121 if args:
122 optparser.error('unexpected arguments')
123
José Fonsecac6977a72012-01-27 14:28:06 +0000124 # Change stdin to binary mode
125 try:
126 import msvcrt
127 except ImportError:
128 pass
129 else:
130 import os
131 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
132
José Fonseca299a1b32012-01-26 20:32:59 +0000133 startTime = time.time()
José Fonsecabaee5792012-03-15 00:09:25 +0000134 parser = Counter(sys.stdin, options.quiet)
135 parser.parse()
José Fonseca299a1b32012-01-26 20:32:59 +0000136 stopTime = time.time()
137 duration = stopTime - startTime
José Fonsecabaee5792012-03-15 00:09:25 +0000138 sys.stderr.write('%u calls, %.03f secs, %u calls/sec\n' % (parser.calls, duration, parser.calls/duration))
José Fonseca299a1b32012-01-26 20:32:59 +0000139
140
141if __name__ == '__main__':
142 main()