Browse Source

Allow use of signals for starting/stopping of profiling

Sam Meder 3 years ago
parent
commit
619a70fe1b
3 changed files with 23 additions and 3 deletions
  1. 3
    0
      .gitignore
  2. 20
    3
      GreenletProfiler.py
  3. 0
    0
      _vendorized_yappi/__init__.py

+ 3
- 0
.gitignore View File

@@ -35,3 +35,6 @@ nosetests.xml
35 35
 .project
36 36
 .pydevproject
37 37
 doc/_build
38
+
39
+.idea
40
+*.iml

+ 20
- 3
GreenletProfiler.py View File

@@ -1,5 +1,6 @@
1
-vimport os
1
+import os
2 2
 import sys
3
+import signal
3 4
 
4 5
 import greenlet
5 6
 
@@ -86,6 +87,12 @@ def main():
86 87
         default=None,
87 88
         help="Log file name")
88 89
 
90
+    parser.add_option(
91
+        "-u", "--signals",
92
+        action="store_true", dest="use_signals",
93
+        default=False,
94
+        help="Uses the USR1 (start) and USR2 (stop) signals to control when profiling happens. [default: False]")
95
+
89 96
     clock_types = ['wall', 'cpu']
90 97
     parser.add_option(
91 98
         "-c", "--clock_type",
@@ -100,8 +107,17 @@ def main():
100 107
     if len(args) > 0:
101 108
         sys.path.insert(0, os.path.dirname(args[0]))
102 109
         set_clock_type(options.clock_type)
103
-        start(options.profile_builtins, not options.profile_single_thread)
104
-        atexit.register(cleanup, options)
110
+        if options.use_signals:
111
+            def signal_start(signal_number, frame):
112
+                start(options.profile_builtins, not options.profile_single_thread)
113
+            signal.signal(signal.SIGUSR1, signal_start)
114
+            
115
+            def signal_stop(signal_number, frame):
116
+                cleanup(options)
117
+            signal.signal(signal.SIGUSR2, signal_stop)
118
+        else:
119
+            start(options.profile_builtins, not options.profile_single_thread)
120
+            atexit.register(cleanup, options)
105 121
         if sys.version_info >= (3, 0):
106 122
             exec (compile(open(args[0]).read(), args[0], 'exec'),
107 123
                   sys._getframe(1).f_globals, sys._getframe(1).f_locals)
@@ -123,6 +139,7 @@ def cleanup(options):
123 139
     else:
124 140
         stats.print_all()
125 141
         get_thread_stats().print_all()
142
+    clear_stats()
126 143
 
127 144
 if __name__ == "__main__":
128 145
     main()

+ 0
- 0
_vendorized_yappi/__init__.py View File