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