| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Hooks that apply globally to all scripts that import or use Telemetry.""" | 5 """Hooks that apply globally to all scripts that import or use Telemetry.""" |
| 6 | 6 |
| 7 import atexit |
| 7 import os | 8 import os |
| 8 import signal | 9 import signal |
| 9 import sys | 10 import sys |
| 10 | 11 |
| 12 from telemetry.core import platform |
| 11 from telemetry.core import util | 13 from telemetry.core import util |
| 12 from telemetry.util import exception_formatter | 14 from telemetry.util import exception_formatter |
| 13 | 15 |
| 14 | 16 |
| 15 def InstallHooks(): | 17 def InstallHooks(): |
| 16 RemoveAllStalePycFiles(util.GetTelemetryDir()) | 18 RemoveAllStalePycFiles(util.GetTelemetryDir()) |
| 17 RemoveAllStalePycFiles(util.GetBaseDir()) | 19 RemoveAllStalePycFiles(util.GetBaseDir()) |
| 18 InstallUnhandledExceptionFormatter() | 20 InstallUnhandledExceptionFormatter() |
| 19 InstallStackDumpOnSigusr1() | 21 InstallStackDumpOnSigusr1() |
| 20 InstallTerminationHook() | 22 InstallTerminationHook() |
| 23 InstallAtExitHook() |
| 21 | 24 |
| 22 | 25 |
| 23 def RemoveAllStalePycFiles(base_dir): | 26 def RemoveAllStalePycFiles(base_dir): |
| 24 """Scan directories for old .pyc files without a .py file and delete them.""" | 27 """Scan directories for old .pyc files without a .py file and delete them.""" |
| 25 for dirname, _, filenames in os.walk(base_dir): | 28 for dirname, _, filenames in os.walk(base_dir): |
| 26 if '.svn' in dirname or '.git' in dirname: | 29 if '.svn' in dirname or '.git' in dirname: |
| 27 continue | 30 continue |
| 28 for filename in filenames: | 31 for filename in filenames: |
| 29 root, ext = os.path.splitext(filename) | 32 root, ext = os.path.splitext(filename) |
| 30 if ext != '.pyc': | 33 if ext != '.pyc': |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 signal.signal(signal.SIGUSR1, PrintDiagnostics) | 67 signal.signal(signal.SIGUSR1, PrintDiagnostics) |
| 65 | 68 |
| 66 | 69 |
| 67 def InstallTerminationHook(): | 70 def InstallTerminationHook(): |
| 68 """Catch SIGTERM, print a stack trace, and exit.""" | 71 """Catch SIGTERM, print a stack trace, and exit.""" |
| 69 def PrintStackAndExit(sig, stack_frame): | 72 def PrintStackAndExit(sig, stack_frame): |
| 70 exception_string = 'Received signal %s, exiting' % sig | 73 exception_string = 'Received signal %s, exiting' % sig |
| 71 exception_formatter.PrintFormattedFrame(stack_frame, exception_string) | 74 exception_formatter.PrintFormattedFrame(stack_frame, exception_string) |
| 72 sys.exit(-1) | 75 sys.exit(-1) |
| 73 signal.signal(signal.SIGTERM, PrintStackAndExit) | 76 signal.signal(signal.SIGTERM, PrintStackAndExit) |
| 77 |
| 78 |
| 79 def InstallAtExitHook(): |
| 80 """Ensure all subprocesses are killed. |
| 81 |
| 82 Subprocesses should never outlive Telemetry. If they do, they can cause |
| 83 hangs on the builbots. |
| 84 """ |
| 85 # TODO(tonyg): Find a way to do something similar on Windows. |
| 86 if platform.GetHostPlatform().GetOSName() == 'win': |
| 87 return |
| 88 |
| 89 # Create new process group and become its leader. |
| 90 os.setpgrp() |
| 91 |
| 92 # At exit, terminate everything in the group. |
| 93 def KillGroup(): |
| 94 # Ignore the TERM we're about to get. |
| 95 signal.signal(signal.SIGTERM, signal.SIG_IGN) |
| 96 os.killpg(0, signal.SIGTERM) |
| 97 atexit.register(KillGroup) |
| OLD | NEW |