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
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:
57 def __eq__(self, other):
59 self.functionName == other.functionName and \
60 self.args == other.args and \
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))
74 def __init__(self, stream):
78 while self.parseCall():
83 callTuple = pickle.load(self.stream)
87 call = self.callFactory(callTuple)
91 def handleCall(self, call):
95 class Counter(Unpickler):
97 def __init__(self, stream, quiet):
98 Unpickler.__init__(self, stream)
102 def handleCall(self, call):
104 sys.stdout.write(str(call))
105 sys.stdout.write('\n')
110 optparser = optparse.OptionParser(
111 usage="\n\tapitrace pickle trace. %prog [options]")
112 optparser.add_option(
114 action="store_true", dest="quiet", default=False,
115 help="don't dump calls to stdout")
117 (options, args) = optparser.parse_args(sys.argv[1:])
120 optparser.error('unexpected arguments')
122 # Change stdin to binary mode
129 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
131 startTime = time.time()
132 parser = Counter(sys.stdin, options.quiet)
134 stopTime = time.time()
135 duration = stopTime - startTime
136 sys.stderr.write('%u calls, %.03f secs, %u calls/sec\n' % (parser.calls, duration, parser.calls/duration))
139 if __name__ == '__main__':