|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import time |
| 4 | + |
| 5 | +from lib.colorer import color_log |
| 6 | +from lib.colorer import qa_notice |
| 7 | +from lib.utils import format_process |
| 8 | + |
| 9 | + |
| 10 | +if sys.version_info[0] == 2: |
| 11 | + ProcessLookupError = OSError |
| 12 | + |
| 13 | + |
| 14 | +# Don't inherit BaseWorkerMessage to bypass cyclic import. |
| 15 | +class RegisterProcessMessage(object): |
| 16 | + """Ask the sampler in the main test-run process to register |
| 17 | + given process. |
| 18 | + """ |
| 19 | + def __init__(self, worker_id, worker_name, pid, task_id, server_name): |
| 20 | + self.worker_id = worker_id |
| 21 | + self.worker_name = worker_name |
| 22 | + self.pid = pid |
| 23 | + self.task_id = task_id |
| 24 | + self.server_name = server_name |
| 25 | + |
| 26 | + |
| 27 | +# Don't inherit BaseWatcher to bypass cyclic import. |
| 28 | +class SamplerWatcher(object): |
| 29 | + def __init__(self, sampler): |
| 30 | + self._sampler = sampler |
| 31 | + self._last_sample = 0 |
| 32 | + self._sample_interval = 0.1 # seconds |
| 33 | + self._warn_interval = self._sample_interval * 4 |
| 34 | + |
| 35 | + def process_result(self, obj): |
| 36 | + if isinstance(obj, RegisterProcessMessage): |
| 37 | + self._sampler.register_process( |
| 38 | + obj.pid, obj.task_id, obj.server_name, obj.worker_id, |
| 39 | + obj.worker_name) |
| 40 | + self._wakeup() |
| 41 | + |
| 42 | + def process_timeout(self, delta_seconds): |
| 43 | + self._wakeup() |
| 44 | + |
| 45 | + def _wakeup(self): |
| 46 | + """Invoke Sampler.sample() if enough time elapsed since |
| 47 | + the previous call. |
| 48 | + """ |
| 49 | + now = time.time() |
| 50 | + delta = now - self._last_sample |
| 51 | + if self._last_sample > 0 and delta > self._warn_interval: |
| 52 | + template = 'Low sampling resolution. The expected interval\n' + \ |
| 53 | + 'is {:.2f} seconds ({:.2f} seconds without warnings),\n' + \ |
| 54 | + 'but the last sample was collected {:.2f} seconds ago.' |
| 55 | + qa_notice(template.format(self._sample_interval, self._warn_interval, |
| 56 | + delta)) |
| 57 | + if delta > self._sample_interval: |
| 58 | + self._sampler._sample() |
| 59 | + self._last_sample = now |
| 60 | + |
| 61 | + |
| 62 | +class Sampler: |
| 63 | + def __init__(self): |
| 64 | + # The instance is created in the test-run main process. |
| 65 | + |
| 66 | + # Field for an instance in a worker. |
| 67 | + self._worker_id = None |
| 68 | + self._worker_name = None |
| 69 | + self._queue = None |
| 70 | + |
| 71 | + # Field for an instance in the main process. |
| 72 | + self._watcher = SamplerWatcher(self) |
| 73 | + |
| 74 | + self.processes = dict() |
| 75 | + |
| 76 | + def set_queue(self, queue, worker_id, worker_name): |
| 77 | + # Called from a worker process (_run_worker()). |
| 78 | + self._worker_id = worker_id |
| 79 | + self._worker_name = worker_name |
| 80 | + self._queue = queue |
| 81 | + self._watcher = None |
| 82 | + |
| 83 | + @property |
| 84 | + def watcher(self): |
| 85 | + if not self._watcher: |
| 86 | + raise RuntimeError('sampler: watcher is available only in the ' + |
| 87 | + 'main test-run process') |
| 88 | + return self._watcher |
| 89 | + |
| 90 | + def register_process(self, pid, task_id, server_name, worker_id=None, |
| 91 | + worker_name=None): |
| 92 | + """Register a process to sampling. |
| 93 | +
|
| 94 | + Call it without worker_* arguments from a worker |
| 95 | + process. |
| 96 | + """ |
| 97 | + if not self._queue: |
| 98 | + # In main test-run process. |
| 99 | + self.processes[pid] = { |
| 100 | + 'task_id': task_id, |
| 101 | + 'server_name': server_name, |
| 102 | + 'worker_id': worker_id, |
| 103 | + 'worker_name': worker_name, |
| 104 | + } |
| 105 | + self._log('register', pid) |
| 106 | + return |
| 107 | + |
| 108 | + # Pass to the main test-run process. |
| 109 | + self._queue.put(RegisterProcessMessage( |
| 110 | + self._worker_id, self._worker_name, pid, task_id, server_name)) |
| 111 | + |
| 112 | + def unregister_process(self, pid): |
| 113 | + if self._queue: |
| 114 | + raise NotImplementedError('sampler: a process unregistration ' + |
| 115 | + 'from a test-run worker is not ' + |
| 116 | + 'implemented yet') |
| 117 | + if pid not in self.processes: |
| 118 | + return |
| 119 | + |
| 120 | + self._log('unregister', pid) |
| 121 | + del self.processes[pid] |
| 122 | + |
| 123 | + def _log(self, event, pid): |
| 124 | + # Those logs are not written due to gh-247. |
| 125 | + process_def = self.processes[pid] |
| 126 | + task_id = process_def['task_id'] |
| 127 | + test_name = task_id[0] + ((':' + task_id[1]) if task_id[1] else '') |
| 128 | + worker_name = process_def['worker_name'] |
| 129 | + server_name = process_def['server_name'] |
| 130 | + color_log('DEBUG: sampler: {} {}\n'.format( |
| 131 | + event, format_process(pid)), schema='info') |
| 132 | + color_log(' | worker: {}\n'.format(worker_name)) |
| 133 | + color_log(' | test: {}\n'.format(test_name)) |
| 134 | + color_log(' | server: {}\n'.format(str(server_name))) |
| 135 | + |
| 136 | + def _sample(self): |
| 137 | + for pid in list(self.processes.keys()): |
| 138 | + # Unregister processes that're gone. |
| 139 | + # Assume that PIDs are rarely reused. |
| 140 | + try: |
| 141 | + os.kill(pid, 0) |
| 142 | + except ProcessLookupError: |
| 143 | + self.unregister_process(pid) |
| 144 | + else: |
| 145 | + self._sample_process(pid) |
| 146 | + |
| 147 | + def _sample_process(self, pid): |
| 148 | + # Your sampling code here. |
| 149 | + pass |
| 150 | + |
| 151 | + |
| 152 | +# The 'singleton' sampler instance: created in the main test-run |
| 153 | +# process, but then work differently in the main process and |
| 154 | +# workers. |
| 155 | +sampler = Sampler() |
0 commit comments