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 | |
| 42 | def main(): |
José Fonseca | 447576d | 2012-01-27 14:27:13 +0000 | [diff] [blame] | 43 | optparser = optparse.OptionParser( |
| 44 | usage="\n\tapitrace pickle trace. %prog [options]") |
| 45 | optparser.add_option( |
| 46 | '--quiet', |
| 47 | action="store_true", dest="quiet", default=False, |
| 48 | help="don't dump calls to stdout") |
| 49 | |
| 50 | (options, args) = optparser.parse_args(sys.argv[1:]) |
| 51 | |
| 52 | if args: |
| 53 | optparser.error('unexpected arguments') |
| 54 | |
José Fonseca | c6977a7 | 2012-01-27 14:28:06 +0000 | [diff] [blame] | 55 | # Change stdin to binary mode |
| 56 | try: |
| 57 | import msvcrt |
| 58 | except ImportError: |
| 59 | pass |
| 60 | else: |
| 61 | import os |
| 62 | msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) |
| 63 | |
José Fonseca | 299a1b3 | 2012-01-26 20:32:59 +0000 | [diff] [blame] | 64 | calls = 0 |
| 65 | startTime = time.time() |
| 66 | while True: |
| 67 | try: |
| 68 | call = pickle.load(sys.stdin) |
| 69 | except EOFError: |
| 70 | break |
| 71 | else: |
| 72 | callNo, functionName, args, ret = call |
José Fonseca | 447576d | 2012-01-27 14:27:13 +0000 | [diff] [blame] | 73 | if not options.quiet: |
| 74 | sys.stdout.write('%u ' % callNo) |
| 75 | sys.stdout.write(functionName) |
| 76 | sys.stdout.write('(' + ', '.join(map(repr, args)) + ')') |
| 77 | if ret is not None: |
| 78 | sys.stdout.write(' = ') |
| 79 | sys.stdout.write(repr(ret)) |
| 80 | sys.stdout.write('\n') |
José Fonseca | 299a1b3 | 2012-01-26 20:32:59 +0000 | [diff] [blame] | 81 | calls += 1 |
| 82 | stopTime = time.time() |
| 83 | duration = stopTime - startTime |
José Fonseca | 447576d | 2012-01-27 14:27:13 +0000 | [diff] [blame] | 84 | sys.stderr.write('%u calls, %.03f secs, %u calls/sec\n' % (calls, duration, calls/duration)) |
José Fonseca | 299a1b3 | 2012-01-26 20:32:59 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | if __name__ == '__main__': |
| 88 | main() |