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