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 | ) |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def collect_proc_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 31 | collector = _ProcessMetricsCollector() |
| 32 | collector.collect() |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 33 | |
| 34 | |
| 35 | class _ProcessMetricsCollector(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | """Class for collecting process metrics.""" |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 37 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | def __init__(self): |
| 39 | self._metrics = [ |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame^] | 40 | _ProcessMetric("adb", test_func=partial(_is_process_name, "adb")), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | _ProcessMetric("autoserv", test_func=_is_parent_autoserv), |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 42 | _ProcessMetric( |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame^] | 43 | "bbagent", test_func=partial(_is_process_name, "bbagent") |
| 44 | ), |
| 45 | _ProcessMetric( |
Congbin Guo | 3cdc11e | 2022-10-11 16:02:32 -0700 | [diff] [blame] | 46 | "cache-downloader", |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 47 | test_func=partial(_is_process_name, "downloader"), |
Congbin Guo | 3cdc11e | 2022-10-11 16:02:32 -0700 | [diff] [blame] | 48 | ), |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 49 | _ProcessMetric("cipd", test_func=partial(_is_process_name, "cipd")), |
Congbin Guo | 3cdc11e | 2022-10-11 16:02:32 -0700 | [diff] [blame] | 50 | _ProcessMetric( |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame^] | 51 | "cloudtail", test_func=partial(_is_process_name, "cloudtail") |
| 52 | ), |
| 53 | _ProcessMetric( |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 54 | "common-tls", test_func=partial(_is_process_name, "common-tls") |
| 55 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | _ProcessMetric("curl", test_func=partial(_is_process_name, "curl")), |
| 57 | _ProcessMetric( |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 58 | "dnsmasq", test_func=partial(_is_process_name, "dnsmasq") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | ), |
| 60 | _ProcessMetric( |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 61 | "drone-agent", |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 62 | test_func=partial(_is_process_name, "drone-agent"), |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 63 | ), |
| 64 | _ProcessMetric( |
| 65 | "fleet-tlw", test_func=partial(_is_process_name, "fleet-tlw") |
| 66 | ), |
| 67 | _ProcessMetric( |
| 68 | "getty", test_func=partial(_is_process_name, "getty") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | ), |
| 70 | _ProcessMetric( |
| 71 | "gs_offloader", |
| 72 | test_func=partial(_is_process_name, "gs_offloader.py"), |
| 73 | ), |
| 74 | _ProcessMetric("gsutil", test_func=_is_gsutil), |
| 75 | _ProcessMetric("java", test_func=partial(_is_process_name, "java")), |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame^] | 76 | _ProcessMetric("k8s_system", test_func=_is_k8s_system), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | _ProcessMetric( |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 78 | "labservice", test_func=partial(_is_process_name, "labservice") |
| 79 | ), |
| 80 | _ProcessMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | "lxc-attach", test_func=partial(_is_process_name, "lxc-attach") |
| 82 | ), |
| 83 | _ProcessMetric( |
| 84 | "lxc-start", test_func=partial(_is_process_name, "lxc-start") |
| 85 | ), |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 86 | _ProcessMetric( |
| 87 | "podman-pull", test_func=partial(_is_podman, "pull") |
| 88 | ), |
| 89 | _ProcessMetric("podman-run", test_func=partial(_is_podman, "run")), |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame^] | 90 | _ProcessMetric( |
| 91 | "phosphorus", test_func=partial(_is_process_name, "phosphorus") |
| 92 | ), |
| 93 | _ProcessMetric("recipe", test_func=_is_recipe), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 94 | _ProcessMetric("sshd", test_func=partial(_is_process_name, "sshd")), |
| 95 | _ProcessMetric("swarming_bot", test_func=_is_swarming_bot), |
| 96 | _ProcessMetric( |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame^] | 97 | "swarming_sub_task", test_func=_is_swarming_sub_task |
| 98 | ), |
| 99 | _ProcessMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | "sysmon", |
| 101 | test_func=partial(_is_python_module, "chromite.scripts.sysmon"), |
| 102 | ), |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 103 | _ProcessMetric("tko_proxy", test_func=_is_tko_proxy), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 104 | ] |
| 105 | self._other_metric = _ProcessMetric("other") |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 106 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | def collect(self): |
| 108 | for proc in psutil.process_iter(): |
| 109 | self._collect_proc(proc) |
| 110 | self._flush() |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 111 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 112 | def _collect_proc(self, proc): |
| 113 | for metric in self._metrics: |
| 114 | if metric.add(proc): |
| 115 | break |
| 116 | else: |
| 117 | self._other_metric.add(proc) |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 118 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 119 | def _flush(self): |
| 120 | for metric in self._metrics: |
| 121 | metric.flush() |
| 122 | self._other_metric.flush() |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 123 | |
| 124 | |
| 125 | class _ProcessMetric(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | """Class for gathering process metrics.""" |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 127 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 128 | def __init__(self, process_name, test_func=lambda proc: True): |
| 129 | """Initialize instance. |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | process_name is used to identify the metric stream. |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | test_func is a function called |
| 134 | for each process. If it returns True, the process is counted. The |
| 135 | default test is to count every process. |
| 136 | """ |
| 137 | self._fields = { |
| 138 | "process_name": process_name, |
| 139 | } |
| 140 | self._test_func = test_func |
| 141 | self._count = 0 |
Congbin Guo | 16b64d5 | 2023-02-10 17:50:30 -0800 | [diff] [blame] | 142 | self._thread_count = 0 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 143 | self._cpu_percent = 0 |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | def add(self, proc): |
| 146 | """Do metric collection for the given process. |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 147 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 148 | Returns True if the process was collected. |
| 149 | """ |
| 150 | if not self._test_func(proc): |
| 151 | return False |
| 152 | self._count += 1 |
Congbin Guo | 16b64d5 | 2023-02-10 17:50:30 -0800 | [diff] [blame] | 153 | self._thread_count += proc.num_threads() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | self._cpu_percent += proc.cpu_percent() |
| 155 | return True |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 156 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | def flush(self): |
| 158 | """Finish collection and send metrics.""" |
| 159 | _count_metric.set(self._count, fields=self._fields) |
| 160 | self._count = 0 |
Congbin Guo | 16b64d5 | 2023-02-10 17:50:30 -0800 | [diff] [blame] | 161 | |
| 162 | _thread_count_metric.set(self._thread_count, fields=self._fields) |
| 163 | self._thread_count = 0 |
| 164 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | _cpu_percent_metric.set( |
| 166 | int(round(self._cpu_percent)), fields=self._fields |
| 167 | ) |
| 168 | self._cpu_percent = 0 |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 169 | |
| 170 | |
| 171 | def _is_parent_autoserv(proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | """Return whether proc is a parent (not forked) autoserv process.""" |
| 173 | return _is_autoserv(proc) and not _is_autoserv(proc.parent()) |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 174 | |
| 175 | |
| 176 | def _is_autoserv(proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 177 | """Return whether proc is an autoserv process.""" |
| 178 | # This relies on the autoserv script being run directly. The script should |
| 179 | # be named autoserv exactly and start with a shebang that is /usr/bin/python, |
| 180 | # NOT /bin/env |
| 181 | return _is_process_name("autoserv", proc) |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 182 | |
| 183 | |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 184 | def _is_python_module(module, proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 185 | """Return whether proc is a process running a Python module.""" |
| 186 | cmdline = proc.cmdline() |
| 187 | return ( |
| 188 | cmdline |
| 189 | and cmdline[0].endswith("python") |
| 190 | and cmdline[1:3] == ["-m", module] |
| 191 | ) |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 192 | |
| 193 | |
Prathmesh Prabhu | 0b795f0 | 2018-05-07 13:12:37 -0700 | [diff] [blame] | 194 | def _is_process_name(name, proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | """Return whether process proc is named name.""" |
| 196 | return proc.name() == name |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 197 | |
| 198 | |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame^] | 199 | def _is_recipe(proc): |
| 200 | """Return whether proc is a recipe process. |
| 201 | |
| 202 | An example proc is like |
| 203 | '/home/.../bin/python -u -s |
| 204 | /home/.../kitchen-checkout/recipe_engine/recipe_engine/main.py ...'. |
| 205 | """ |
| 206 | cmdline = proc.cmdline() |
| 207 | return ( |
| 208 | len(cmdline) >= 4 |
| 209 | and cmdline[0].endswith("/python") |
| 210 | and cmdline[3].endswith("/recipe_engine/main.py") |
| 211 | ) |
| 212 | |
| 213 | |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 214 | def _is_swarming_bot(proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 215 | """Return whether proc is a Swarming bot. |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 216 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 217 | A swarming bot process is like '/usr/bin/python3.8 <bot-zip-path> start_bot'. |
| 218 | """ |
| 219 | cmdline = proc.cmdline() |
| 220 | return ( |
| 221 | len(cmdline) == 3 |
| 222 | and cmdline[0].split("/")[-1].startswith("python") |
| 223 | and cmdline[2] == "start_bot" |
| 224 | ) |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 225 | |
| 226 | |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame^] | 227 | def _is_swarming_sub_task(proc): |
| 228 | """Return whether proc is a Swarming bot sub task. |
| 229 | |
| 230 | An example Swarming sub task: |
| 231 | /usr/bin/python3.8 -u /.../swarming_bot.2.zip run_isolated ... |
| 232 | """ |
| 233 | cmdline = proc.cmdline() |
| 234 | return ( |
| 235 | len(cmdline) >= 4 |
| 236 | and cmdline[0].split("/")[-1].startswith("python") |
| 237 | and cmdline[2].split("/")[-1].startswith("swarming_bot.") |
| 238 | ) |
| 239 | |
| 240 | |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 241 | def _is_gsutil(proc): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | """Return whether proc is gsutil.""" |
| 243 | cmdline = proc.cmdline() |
| 244 | return ( |
| 245 | len(cmdline) >= 2 |
| 246 | and cmdline[0] == "python" |
| 247 | and cmdline[1].endswith("gsutil") |
| 248 | ) |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 249 | |
| 250 | |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame^] | 251 | def _is_k8s_system(proc): |
| 252 | """Return whether proc is a k8s system process.""" |
| 253 | return proc.name() in ("kubelet", "kube-proxy") |
| 254 | |
| 255 | |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 256 | def _is_tko_proxy(proc): |
| 257 | """Return whether proc is a tko proxy. |
| 258 | |
| 259 | A tk proxy process is like |
| 260 | '/opt/cloud_sql_proxy -dir=<...> |
| 261 | -instances=google.com:chromeos-lab:us-central1:tko |
| 262 | -credential_file=<...>'. |
| 263 | """ |
| 264 | cmdline = proc.cmdline() |
| 265 | return ( |
| 266 | len(cmdline) == 4 |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 267 | and cmdline[0].split("/")[-1] == "cloud_sql_proxy" |
| 268 | and cmdline[2] == "-instances=google.com:chromeos-lab:us-central1:tko" |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 269 | ) |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 270 | |
| 271 | |
| 272 | def _is_podman(subcmd, proc): |
| 273 | """Return whiter proc is a podman process. |
| 274 | |
| 275 | A podman pull process is like |
| 276 | 'podman pull image:tag' |
| 277 | A podman run process is like |
| 278 | 'podman run --option ... image:tag' |
| 279 | """ |
| 280 | cmdline = proc.cmdline() |
Congbin Guo | ba85d0b | 2023-01-27 18:32:21 -0800 | [diff] [blame] | 281 | return proc.name() == "podman" and len(cmdline) > 1 and cmdline[1] == subcmd |