Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2017 The ChromiumOS Authors |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Process metrics.""" |
| 6 | |
| 7 | from __future__ import absolute_import |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 8 | |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 9 | from functools import partial |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 10 | import logging |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 11 | |
Mike Frysinger | cb56b64 | 2019-08-25 15:33:08 -0400 | [diff] [blame] | 12 | import psutil # pylint: disable=import-error |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 13 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 14 | from chromite.lib import metrics |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 15 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 16 | |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 17 | logger = logging.getLogger(__name__) |
| 18 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 19 | _count_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | "proc/count", description="Number of processes currently running." |
| 21 | ) |
Congbin Guo | 16b64d5 | 2023-02-10 17:50:30 -0800 | [diff] [blame] | 22 | _thread_count_metric = metrics.GaugeMetric( |
| 23 | "proc/thread_count", description="Number of threads currently running." |
| 24 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 25 | _cpu_percent_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 26 | "proc/cpu_percent", description="CPU usage percent of processes." |
| 27 | ) |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame] | 28 | _cpu_times_metric = metrics.CumulativeMetric( |
| 29 | "proc/cpu_times", |
| 30 | description="Accumulated CPU time in each specific mode of processes.", |
| 31 | ) |
Congbin Guo | cf2750c | 2023-02-11 21:25:16 -0800 | [diff] [blame] | 32 | _read_count_metric = metrics.CounterMetric( |
| 33 | "proc/read/count", |
| 34 | description="Accumulated read operation count of processes.", |
| 35 | ) |
| 36 | _read_bytes_metric = metrics.CounterMetric( |
| 37 | "proc/read/bytes", description="Accumulated read bytes of processes." |
| 38 | ) |
| 39 | _read_chars_metric = metrics.CounterMetric( |
| 40 | "proc/read/chars", |
| 41 | description="Accumulated buffered read bytes of processes.", |
| 42 | ) |
| 43 | _write_count_metric = metrics.CounterMetric( |
| 44 | "proc/write/count", |
| 45 | description="Accumulated write operation count of processes.", |
| 46 | ) |
| 47 | _write_bytes_metric = metrics.CounterMetric( |
| 48 | "proc/write/bytes", description="Accumulated write bytes of processes." |
| 49 | ) |
| 50 | _write_chars_metric = metrics.CounterMetric( |
| 51 | "proc/write/chars", |
| 52 | description="Accumulated buffered write bytes of processes.", |
| 53 | ) |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def collect_proc_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | collector = _ProcessMetricsCollector() |
| 58 | collector.collect() |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | class _ProcessMetricsCollector(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | """Class for collecting process metrics.""" |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 63 | |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame] | 64 | # We need to store some per process metrics of last run in order to |
| 65 | # calculate the detla and aggregate them. |
| 66 | old_cpu_times = {} |
Congbin Guo | cf2750c | 2023-02-11 21:25:16 -0800 | [diff] [blame] | 67 | old_io_counters = {} |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame] | 68 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | def __init__(self): |
| 70 | self._metrics = [ |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 71 | _ProcessMetric("adb", test_func=partial(_is_process_name, "adb")), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | _ProcessMetric("autoserv", test_func=_is_parent_autoserv), |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 73 | _ProcessMetric( |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 74 | "bbagent", test_func=partial(_is_process_name, "bbagent") |
| 75 | ), |
| 76 | _ProcessMetric( |
Congbin Guo | 3cdc11e | 2022-10-11 16:02:32 -0700 | [diff] [blame] | 77 | "cache-downloader", |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 78 | test_func=partial(_is_process_name, "downloader"), |
Congbin Guo | 3cdc11e | 2022-10-11 16:02:32 -0700 | [diff] [blame] | 79 | ), |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 80 | _ProcessMetric("cipd", test_func=partial(_is_process_name, "cipd")), |
Congbin Guo | 3cdc11e | 2022-10-11 16:02:32 -0700 | [diff] [blame] | 81 | _ProcessMetric( |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 82 | "cloudtail", test_func=partial(_is_process_name, "cloudtail") |
| 83 | ), |
| 84 | _ProcessMetric( |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 85 | "common-tls", test_func=partial(_is_process_name, "common-tls") |
| 86 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | _ProcessMetric("curl", test_func=partial(_is_process_name, "curl")), |
| 88 | _ProcessMetric( |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 89 | "dnsmasq", test_func=partial(_is_process_name, "dnsmasq") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | ), |
| 91 | _ProcessMetric( |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 92 | "drone-agent", |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 93 | test_func=partial(_is_process_name, "drone-agent"), |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 94 | ), |
| 95 | _ProcessMetric( |
| 96 | "fleet-tlw", test_func=partial(_is_process_name, "fleet-tlw") |
| 97 | ), |
| 98 | _ProcessMetric( |
| 99 | "getty", test_func=partial(_is_process_name, "getty") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | ), |
| 101 | _ProcessMetric( |
| 102 | "gs_offloader", |
| 103 | test_func=partial(_is_process_name, "gs_offloader.py"), |
| 104 | ), |
| 105 | _ProcessMetric("gsutil", test_func=_is_gsutil), |
| 106 | _ProcessMetric("java", test_func=partial(_is_process_name, "java")), |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 107 | _ProcessMetric("k8s_system", test_func=_is_k8s_system), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | _ProcessMetric( |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 109 | "labservice", test_func=partial(_is_process_name, "labservice") |
| 110 | ), |
| 111 | _ProcessMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 112 | "lxc-attach", test_func=partial(_is_process_name, "lxc-attach") |
| 113 | ), |
| 114 | _ProcessMetric( |
| 115 | "lxc-start", test_func=partial(_is_process_name, "lxc-start") |
| 116 | ), |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 117 | _ProcessMetric( |
Congbin Guo | 91355e1 | 2023-02-14 13:51:20 -0800 | [diff] [blame] | 118 | "podman-pull", |
| 119 | test_func=partial(_is_cmd_with_subcmd, "podman", "pull"), |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 120 | ), |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 121 | _ProcessMetric( |
Congbin Guo | 91355e1 | 2023-02-14 13:51:20 -0800 | [diff] [blame] | 122 | "podman-run", |
| 123 | test_func=partial(_is_cmd_with_subcmd, "podman", "run"), |
| 124 | ), |
| 125 | _ProcessMetric( |
| 126 | "phosphorus-fetch-crashes", |
| 127 | test_func=partial( |
| 128 | _is_cmd_with_subcmd, "phosphorus", "fetch-crashes" |
| 129 | ), |
| 130 | ), |
| 131 | _ProcessMetric( |
| 132 | "phosphorus-prejob", |
| 133 | test_func=partial(_is_cmd_with_subcmd, "phosphorus", "prejob"), |
| 134 | ), |
| 135 | _ProcessMetric( |
| 136 | "phosphorus-run-test", |
| 137 | test_func=partial( |
| 138 | _is_cmd_with_subcmd, "phosphorus", "run-test" |
| 139 | ), |
| 140 | ), |
| 141 | _ProcessMetric( |
| 142 | "phosphorus-upload-to-gs", |
| 143 | test_func=partial( |
| 144 | _is_cmd_with_subcmd, "phosphorus", "upload-to-gs" |
| 145 | ), |
| 146 | ), |
| 147 | _ProcessMetric( |
| 148 | "phosphorus-upload-to-tko", |
| 149 | test_func=partial( |
| 150 | _is_cmd_with_subcmd, "phosphorus", "upload-to-tko" |
| 151 | ), |
| 152 | ), |
| 153 | # Catch all phosphorus subcommands we missed. |
| 154 | _ProcessMetric( |
| 155 | "phosphorus-other", |
| 156 | test_func=partial(_is_process_name, "phosphorus"), |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 157 | ), |
| 158 | _ProcessMetric("recipe", test_func=_is_recipe), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 159 | _ProcessMetric("sshd", test_func=partial(_is_process_name, "sshd")), |
| 160 | _ProcessMetric("swarming_bot", test_func=_is_swarming_bot), |
| 161 | _ProcessMetric( |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 162 | "swarming_sub_task", test_func=_is_swarming_sub_task |
| 163 | ), |
| 164 | _ProcessMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | "sysmon", |
| 166 | test_func=partial(_is_python_module, "chromite.scripts.sysmon"), |
| 167 | ), |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 168 | _ProcessMetric("tko_proxy", test_func=_is_tko_proxy), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 169 | ] |
| 170 | self._other_metric = _ProcessMetric("other") |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 171 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | def collect(self): |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame] | 173 | new_cpu_times = {} |
Congbin Guo | cf2750c | 2023-02-11 21:25:16 -0800 | [diff] [blame] | 174 | new_io_counters = {} |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 175 | for proc in psutil.process_iter(): |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame] | 176 | new_cpu_times[proc.pid] = proc.cpu_times() |
Congbin Guo | cf2750c | 2023-02-11 21:25:16 -0800 | [diff] [blame] | 177 | new_io_counters[proc.pid] = proc.io_counters() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | self._collect_proc(proc) |
| 179 | self._flush() |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame] | 180 | _ProcessMetricsCollector.old_cpu_times = new_cpu_times |
Congbin Guo | cf2750c | 2023-02-11 21:25:16 -0800 | [diff] [blame] | 181 | _ProcessMetricsCollector.old_io_counters = new_io_counters |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 182 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 183 | def _collect_proc(self, proc): |
| 184 | for metric in self._metrics: |
| 185 | if metric.add(proc): |
| 186 | break |
| 187 | else: |
| 188 | self._other_metric.add(proc) |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 189 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 190 | def _flush(self): |
| 191 | for metric in self._metrics: |
| 192 | metric.flush() |
| 193 | self._other_metric.flush() |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 194 | |
| 195 | |
| 196 | class _ProcessMetric(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 197 | """Class for gathering process metrics.""" |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 198 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 199 | def __init__(self, process_name, test_func=lambda proc: True): |
| 200 | """Initialize instance. |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 201 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 202 | process_name is used to identify the metric stream. |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 203 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | test_func is a function called |
| 205 | for each process. If it returns True, the process is counted. The |
| 206 | default test is to count every process. |
| 207 | """ |
| 208 | self._fields = { |
| 209 | "process_name": process_name, |
| 210 | } |
| 211 | self._test_func = test_func |
| 212 | self._count = 0 |
Congbin Guo | 16b64d5 | 2023-02-10 17:50:30 -0800 | [diff] [blame] | 213 | self._thread_count = 0 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 214 | self._cpu_percent = 0 |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame] | 215 | self._cpu_times = _CPUTimes() |
Congbin Guo | cf2750c | 2023-02-11 21:25:16 -0800 | [diff] [blame] | 216 | self._io_counters = _IOCounters() |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 217 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 218 | def add(self, proc): |
| 219 | """Do metric collection for the given process. |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 220 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 221 | Returns True if the process was collected. |
| 222 | """ |
| 223 | if not self._test_func(proc): |
| 224 | return False |
| 225 | self._count += 1 |
Congbin Guo | 16b64d5 | 2023-02-10 17:50:30 -0800 | [diff] [blame] | 226 | self._thread_count += proc.num_threads() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 227 | self._cpu_percent += proc.cpu_percent() |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame] | 228 | |
| 229 | self._cpu_times += _CPUTimes( |
| 230 | proc.cpu_times() |
| 231 | ) - _ProcessMetricsCollector.old_cpu_times.get(proc.pid) |
| 232 | |
Congbin Guo | cf2750c | 2023-02-11 21:25:16 -0800 | [diff] [blame] | 233 | self._io_counters += _IOCounters( |
| 234 | proc.io_counters() |
| 235 | ) - _ProcessMetricsCollector.old_io_counters.get(proc.pid) |
| 236 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 237 | return True |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 238 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 239 | def flush(self): |
| 240 | """Finish collection and send metrics.""" |
| 241 | _count_metric.set(self._count, fields=self._fields) |
| 242 | self._count = 0 |
Congbin Guo | 16b64d5 | 2023-02-10 17:50:30 -0800 | [diff] [blame] | 243 | |
| 244 | _thread_count_metric.set(self._thread_count, fields=self._fields) |
| 245 | self._thread_count = 0 |
| 246 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 247 | _cpu_percent_metric.set( |
| 248 | int(round(self._cpu_percent)), fields=self._fields |
| 249 | ) |
| 250 | self._cpu_percent = 0 |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 251 | |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame] | 252 | for mode, t in self._cpu_times.asdict().items(): |
| 253 | _cpu_times_metric.increment_by( |
| 254 | t, fields={**self._fields, "mode": mode} |
| 255 | ) |
| 256 | self._cpu_times = _CPUTimes() |
| 257 | |
Congbin Guo | cf2750c | 2023-02-11 21:25:16 -0800 | [diff] [blame] | 258 | _read_count_metric.increment_by( |
| 259 | self._io_counters.read_count, fields=self._fields |
| 260 | ) |
| 261 | _read_bytes_metric.increment_by( |
| 262 | self._io_counters.read_bytes, fields=self._fields |
| 263 | ) |
| 264 | _read_chars_metric.increment_by( |
| 265 | self._io_counters.read_chars, fields=self._fields |
| 266 | ) |
| 267 | _write_count_metric.increment_by( |
| 268 | self._io_counters.write_count, fields=self._fields |
| 269 | ) |
| 270 | _write_bytes_metric.increment_by( |
| 271 | self._io_counters.write_bytes, fields=self._fields |
| 272 | ) |
| 273 | _write_chars_metric.increment_by( |
| 274 | self._io_counters.write_chars, fields=self._fields |
| 275 | ) |
| 276 | self._io_counters = _IOCounters() |
| 277 | |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame] | 278 | |
| 279 | class _CPUTimes(object): |
| 280 | """A container for CPU times metrics.""" |
| 281 | |
| 282 | def __init__(self, v=None): |
| 283 | self.system = v.system if v else 0 |
| 284 | self.user = v.user if v else 0 |
| 285 | self.iowait = v.iowait if v else 0 |
| 286 | self.children_system = v.children_system if v else 0 |
| 287 | self.children_user = v.children_user if v else 0 |
| 288 | |
| 289 | def __sub__(self, rhs): |
| 290 | if not rhs: |
| 291 | return self |
| 292 | |
| 293 | r = _CPUTimes() |
| 294 | r.system = self.system - rhs.system |
| 295 | r.user = self.user - rhs.user |
| 296 | r.iowait = self.iowait - rhs.iowait |
| 297 | r.children_system = self.children_system - rhs.children_system |
| 298 | r.children_user = self.children_user - rhs.children_user |
| 299 | return r |
| 300 | |
| 301 | def __iadd__(self, rhs): |
| 302 | if not rhs: |
| 303 | return self |
| 304 | |
| 305 | self.system += rhs.system |
| 306 | self.user += rhs.user |
| 307 | self.iowait += rhs.iowait |
| 308 | self.children_system += rhs.children_system |
| 309 | self.children_user += rhs.children_user |
| 310 | return self |
| 311 | |
| 312 | def asdict(self): |
| 313 | return { |
| 314 | "system": self.system, |
| 315 | "user": self.user, |
| 316 | "iowait": self.iowait, |
| 317 | "children_system": self.children_system, |
| 318 | "children_user": self.children_user, |
| 319 | } |
| 320 | |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 321 | |
| 322 | def _is_parent_autoserv(proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 323 | """Return whether proc is a parent (not forked) autoserv process.""" |
| 324 | return _is_autoserv(proc) and not _is_autoserv(proc.parent()) |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 325 | |
| 326 | |
| 327 | def _is_autoserv(proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | """Return whether proc is an autoserv process.""" |
| 329 | # This relies on the autoserv script being run directly. The script should |
| 330 | # be named autoserv exactly and start with a shebang that is /usr/bin/python, |
| 331 | # NOT /bin/env |
| 332 | return _is_process_name("autoserv", proc) |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 333 | |
| 334 | |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 335 | def _is_python_module(module, proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 336 | """Return whether proc is a process running a Python module.""" |
| 337 | cmdline = proc.cmdline() |
| 338 | return ( |
| 339 | cmdline |
| 340 | and cmdline[0].endswith("python") |
| 341 | and cmdline[1:3] == ["-m", module] |
| 342 | ) |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 343 | |
| 344 | |
Prathmesh Prabhu | 0b795f0 | 2018-05-07 13:12:37 -0700 | [diff] [blame] | 345 | def _is_process_name(name, proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 346 | """Return whether process proc is named name.""" |
| 347 | return proc.name() == name |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 348 | |
| 349 | |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 350 | def _is_recipe(proc): |
| 351 | """Return whether proc is a recipe process. |
| 352 | |
| 353 | An example proc is like |
| 354 | '/home/.../bin/python -u -s |
| 355 | /home/.../kitchen-checkout/recipe_engine/recipe_engine/main.py ...'. |
| 356 | """ |
| 357 | cmdline = proc.cmdline() |
| 358 | return ( |
| 359 | len(cmdline) >= 4 |
| 360 | and cmdline[0].endswith("/python") |
| 361 | and cmdline[3].endswith("/recipe_engine/main.py") |
| 362 | ) |
| 363 | |
| 364 | |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 365 | def _is_swarming_bot(proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 366 | """Return whether proc is a Swarming bot. |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 367 | |
Trent Apted | c20bb6d | 2023-05-10 15:00:03 +1000 | [diff] [blame^] | 368 | A swarming bot process is like |
| 369 | '/usr/bin/python3.8 <bot-zip-path> start_bot'. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 370 | """ |
| 371 | cmdline = proc.cmdline() |
| 372 | return ( |
| 373 | len(cmdline) == 3 |
| 374 | and cmdline[0].split("/")[-1].startswith("python") |
| 375 | and cmdline[2] == "start_bot" |
| 376 | ) |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 377 | |
| 378 | |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 379 | def _is_swarming_sub_task(proc): |
| 380 | """Return whether proc is a Swarming bot sub task. |
| 381 | |
| 382 | An example Swarming sub task: |
| 383 | /usr/bin/python3.8 -u /.../swarming_bot.2.zip run_isolated ... |
| 384 | """ |
| 385 | cmdline = proc.cmdline() |
| 386 | return ( |
| 387 | len(cmdline) >= 4 |
| 388 | and cmdline[0].split("/")[-1].startswith("python") |
| 389 | and cmdline[2].split("/")[-1].startswith("swarming_bot.") |
| 390 | ) |
| 391 | |
| 392 | |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 393 | def _is_gsutil(proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 394 | """Return whether proc is gsutil.""" |
| 395 | cmdline = proc.cmdline() |
| 396 | return ( |
| 397 | len(cmdline) >= 2 |
| 398 | and cmdline[0] == "python" |
| 399 | and cmdline[1].endswith("gsutil") |
| 400 | ) |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 401 | |
| 402 | |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 403 | def _is_k8s_system(proc): |
| 404 | """Return whether proc is a k8s system process.""" |
| 405 | return proc.name() in ("kubelet", "kube-proxy") |
| 406 | |
| 407 | |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 408 | def _is_tko_proxy(proc): |
| 409 | """Return whether proc is a tko proxy. |
| 410 | |
| 411 | A tk proxy process is like |
| 412 | '/opt/cloud_sql_proxy -dir=<...> |
| 413 | -instances=google.com:chromeos-lab:us-central1:tko |
| 414 | -credential_file=<...>'. |
| 415 | """ |
| 416 | cmdline = proc.cmdline() |
| 417 | return ( |
| 418 | len(cmdline) == 4 |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 419 | and cmdline[0].split("/")[-1] == "cloud_sql_proxy" |
| 420 | and cmdline[2] == "-instances=google.com:chromeos-lab:us-central1:tko" |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 421 | ) |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 422 | |
| 423 | |
Congbin Guo | 91355e1 | 2023-02-14 13:51:20 -0800 | [diff] [blame] | 424 | def _is_cmd_with_subcmd(cmd, subcmd, proc): |
| 425 | """Return whiter proc is a subcommand of a command process. |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 426 | |
Congbin Guo | 91355e1 | 2023-02-14 13:51:20 -0800 | [diff] [blame] | 427 | For example: 'podman pull image:tag' or `phosphorus run-test ...`. |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 428 | """ |
| 429 | cmdline = proc.cmdline() |
Congbin Guo | 91355e1 | 2023-02-14 13:51:20 -0800 | [diff] [blame] | 430 | return proc.name() == cmd and len(cmdline) > 1 and cmdline[1] == subcmd |
Congbin Guo | cf2750c | 2023-02-11 21:25:16 -0800 | [diff] [blame] | 431 | |
| 432 | |
| 433 | class _CPUTimes(object): |
| 434 | """A container for CPU times metrics.""" |
| 435 | |
| 436 | def __init__(self, v=None): |
| 437 | self.system = v.system if v else 0 |
| 438 | self.user = v.user if v else 0 |
| 439 | self.iowait = v.iowait if v else 0 |
| 440 | self.children_system = v.children_system if v else 0 |
| 441 | self.children_user = v.children_user if v else 0 |
| 442 | |
| 443 | def __sub__(self, rhs): |
| 444 | if not rhs: |
| 445 | return self |
| 446 | |
| 447 | r = _CPUTimes() |
| 448 | r.system = self.system - rhs.system |
| 449 | r.user = self.user - rhs.user |
| 450 | r.iowait = self.iowait - rhs.iowait |
| 451 | r.children_system = self.children_system - rhs.children_system |
| 452 | r.children_user = self.children_user - rhs.children_user |
| 453 | return r |
| 454 | |
| 455 | def __iadd__(self, rhs): |
| 456 | if not rhs: |
| 457 | return self |
| 458 | |
| 459 | self.system += rhs.system |
| 460 | self.user += rhs.user |
| 461 | self.iowait += rhs.iowait |
| 462 | self.children_system += rhs.children_system |
| 463 | self.children_user += rhs.children_user |
| 464 | return self |
| 465 | |
| 466 | def asdict(self): |
| 467 | return { |
| 468 | "system": self.system, |
| 469 | "user": self.user, |
| 470 | "iowait": self.iowait, |
| 471 | "children_system": self.children_system, |
| 472 | "children_user": self.children_user, |
| 473 | } |
| 474 | |
| 475 | |
| 476 | class _IOCounters(object): |
| 477 | """A container for I/O counter metrics.""" |
| 478 | |
| 479 | def __init__(self, v=None): |
| 480 | self.read_count = v.read_count if v else 0 |
| 481 | self.read_bytes = v.read_bytes if v else 0 |
| 482 | self.read_chars = v.read_chars if v else 0 |
| 483 | self.write_count = v.write_count if v else 0 |
| 484 | self.write_bytes = v.write_bytes if v else 0 |
| 485 | self.write_chars = v.write_chars if v else 0 |
| 486 | |
| 487 | def __sub__(self, rhs): |
| 488 | if not rhs: |
| 489 | return self |
| 490 | |
| 491 | r = _IOCounters() |
| 492 | r.read_count = self.read_count - rhs.read_count |
| 493 | r.read_bytes = self.read_bytes - rhs.read_bytes |
| 494 | r.read_chars = self.read_chars - rhs.read_chars |
| 495 | r.write_count = self.write_count - rhs.write_count |
| 496 | r.write_bytes = self.write_bytes - rhs.write_bytes |
| 497 | r.write_chars = self.write_chars - rhs.write_chars |
| 498 | return r |
| 499 | |
| 500 | def __iadd__(self, rhs): |
| 501 | if not rhs: |
| 502 | return self |
| 503 | |
| 504 | self.read_count += rhs.read_count |
| 505 | self.read_bytes += rhs.read_bytes |
| 506 | self.write_count += rhs.write_count |
| 507 | self.write_bytes += rhs.write_bytes |
| 508 | return self |