| 1 | from __future__ import print_function
 | 
| 2 | """
 | 
| 3 | pytrace.py
 | 
| 4 | """
 | 
| 5 | 
 | 
| 6 | import cStringIO
 | 
| 7 | import os
 | 
| 8 | #import struct
 | 
| 9 | import sys
 | 
| 10 | 
 | 
| 11 | # TODO: Two kinds of tracing?
 | 
| 12 | # - FullTracer -> Chrome trace?
 | 
| 13 | # - ReservoirSamplingTracer() -- flame graph that is deterministic?
 | 
| 14 | 
 | 
| 15 | # TODO: Check this in but just go ahead and fix wild.sh instead.
 | 
| 16 | 
 | 
| 17 | 
 | 
| 18 | class Tracer(object):
 | 
| 19 |     # Limit to 10M events by default.
 | 
| 20 |     def __init__(self, max_events=10e6):
 | 
| 21 |         self.pid = os.getpid()
 | 
| 22 |         # append
 | 
| 23 |         self.event_strs = cStringIO.StringIO()
 | 
| 24 | 
 | 
| 25 |         # After max_events we stop recording
 | 
| 26 |         self.max_events = max_events
 | 
| 27 |         self.num_events = 0
 | 
| 28 |         self.depth = 0
 | 
| 29 | 
 | 
| 30 |     # Python VM callback
 | 
| 31 |     def OnEvent(self, frame, event_type, arg):
 | 
| 32 |         # Test overhead
 | 
| 33 |         # 7.5 seconds.  Geez.  That's crazy high.
 | 
| 34 |         # The baseline is 2.7 seconds, and _lsprof takes 3.8 seconds.
 | 
| 35 | 
 | 
| 36 |         # I guess that's why pytracing is a decorator and only works on one part of
 | 
| 37 |         # the program.
 | 
| 38 |         # pytracing isn't usable with large programs.  It can't run abuild -h.
 | 
| 39 | 
 | 
| 40 |         # What I really want is the nicer visualization.  I don't want the silly
 | 
| 41 |         # cProfile output.
 | 
| 42 | 
 | 
| 43 |         self.num_events += 1
 | 
| 44 |         name = frame.f_code.co_name
 | 
| 45 |         filename = frame.f_code.co_filename
 | 
| 46 |         if event_type in ('call', 'c_call'):
 | 
| 47 |             self.depth += 1
 | 
| 48 | 
 | 
| 49 |         record = '%s%s\t%s\t%s\t%s\t%s\n' % (
 | 
| 50 |             '  ' * self.depth, event_type, filename, frame.f_lineno, name, arg)
 | 
| 51 |         self.event_strs.write(record)
 | 
| 52 | 
 | 
| 53 |         if event_type in ('return', 'c_return'):
 | 
| 54 |             self.depth -= 1
 | 
| 55 | 
 | 
| 56 |         return
 | 
| 57 | 
 | 
| 58 |         # NOTE:  Do we want a struct.pack version eventually?
 | 
| 59 |         #self.event_strs.write('')
 | 
| 60 | 
 | 
| 61 |     def Start(self):
 | 
| 62 |         sys.setprofile(self.OnEvent)
 | 
| 63 | 
 | 
| 64 |     def Stop(self, path):
 | 
| 65 |         sys.setprofile(None)
 | 
| 66 |         # Only one process should write out the file!
 | 
| 67 |         if os.getpid() != self.pid:
 | 
| 68 |             return
 | 
| 69 | 
 | 
| 70 |         # TODO:
 | 
| 71 |         # - report number of events?
 | 
| 72 |         # - report number of bytes?
 | 
| 73 |         print('num_events: %d' % self.num_events, file=sys.stderr)
 | 
| 74 |         print('Writing to %r' % path, file=sys.stderr)
 | 
| 75 |         with open(path, 'w') as f:
 | 
| 76 |             f.write(self.event_strs.getvalue())
 | 
| 77 | 
 | 
| 78 | 
 | 
| 79 | def main(argv):
 | 
| 80 |     t = Tracer()
 | 
| 81 |     import urlparse
 | 
| 82 |     t.Start()
 | 
| 83 |     print(urlparse.urlparse('http://example.com/foo'))
 | 
| 84 |     t.Stop('demo.pytrace')
 | 
| 85 | 
 | 
| 86 | 
 | 
| 87 | if __name__ == '__main__':
 | 
| 88 |     try:
 | 
| 89 |         main(sys.argv)
 | 
| 90 |     except RuntimeError as e:
 | 
| 91 |         print >> sys.stderr, 'FATAL: %s' % e
 | 
| 92 |         sys.exit(1)
 |