José Fonseca | 299a1b3 | 2012-01-26 20:32:59 +0000 | [diff] [blame] | 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 | |
José Fonseca | 447576d | 2012-01-27 14:27:13 +0000 | [diff] [blame] | 36 | import optparse |
José Fonseca | 299a1b3 | 2012-01-26 20:32:59 +0000 | [diff] [blame] | 37 | import cPickle as pickle |
| 38 | import sys |
| 39 | import time |
| 40 | |
| 41 | |
José Fonseca | baee579 | 2012-03-15 00:09:25 +0000 | [diff] [blame] | 42 | class Call: |
| 43 | |
| 44 | def __init__(self, callTuple): |
| 45 | self.no, self.functionName, self.args, self.ret = callTuple |
José Fonseca | bf34127 | 2012-03-16 15:40:31 +0000 | [diff] [blame^] | 46 | self._hash = None |
José Fonseca | baee579 | 2012-03-15 00:09:25 +0000 | [diff] [blame] | 47 | |
| 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é Fonseca | bf34127 | 2012-03-16 15:40:31 +0000 | [diff] [blame^] | 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)) |
| 69 | return self._hash |
José Fonseca | baee579 | 2012-03-15 00:09:25 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | class 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 | |
| 97 | class 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é Fonseca | 299a1b3 | 2012-01-26 20:32:59 +0000 | [diff] [blame] | 111 | def main(): |
José Fonseca | 447576d | 2012-01-27 14:27:13 +0000 | [diff] [blame] | 112 | 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é Fonseca | c6977a7 | 2012-01-27 14:28:06 +0000 | [diff] [blame] | 124 | # 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é Fonseca | 299a1b3 | 2012-01-26 20:32:59 +0000 | [diff] [blame] | 133 | startTime = time.time() |
José Fonseca | baee579 | 2012-03-15 00:09:25 +0000 | [diff] [blame] | 134 | parser = Counter(sys.stdin, options.quiet) |
| 135 | parser.parse() |
José Fonseca | 299a1b3 | 2012-01-26 20:32:59 +0000 | [diff] [blame] | 136 | stopTime = time.time() |
| 137 | duration = stopTime - startTime |
José Fonseca | baee579 | 2012-03-15 00:09:25 +0000 | [diff] [blame] | 138 | sys.stderr.write('%u calls, %.03f secs, %u calls/sec\n' % (parser.calls, duration, parser.calls/duration)) |
José Fonseca | 299a1b3 | 2012-01-26 20:32:59 +0000 | [diff] [blame] | 139 | |
| 140 | |
| 141 | if __name__ == '__main__': |
| 142 | main() |