2 ##########################################################################
4 # Copyright 2012 Jose Fonseca
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:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
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
25 ##########################################################################/
27 '''Sample program for apitrace pickle command.
31 apitrace pickle foo.trace | python unpickle.py
37 import cPickle as pickle
44 def __init__(self, callTuple):
45 self.no, self.functionName, self.args, self.ret = callTuple
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:
58 def __eq__(self, other):
60 self.functionName == other.functionName and \
61 self.args == other.args and \
65 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))
76 def __init__(self, stream):
80 while self.parseCall():
85 callTuple = pickle.load(self.stream)
89 call = self.callFactory(callTuple)
93 def handleCall(self, call):
97 class Counter(Unpickler):
99 def __init__(self, stream, quiet):
100 Unpickler.__init__(self, stream)
104 def handleCall(self, call):
106 sys.stdout.write(str(call))
107 sys.stdout.write('\n')
112 optparser = optparse.OptionParser(
113 usage="\n\tapitrace pickle trace. %prog [options]")
114 optparser.add_option(
116 action="store_true", dest="quiet", default=False,
117 help="don't dump calls to stdout")
119 (options, args) = optparser.parse_args(sys.argv[1:])
122 optparser.error('unexpected arguments')
124 # Change stdin to binary mode
131 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
133 startTime = time.time()
134 parser = Counter(sys.stdin, options.quiet)
136 stopTime = time.time()
137 duration = stopTime - startTime
138 sys.stderr.write('%u calls, %.03f secs, %u calls/sec\n' % (parser.calls, duration, parser.calls/duration))
141 if __name__ == '__main__':