Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 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( |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 20 | 'proc/count', |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 21 | description='Number of processes currently running.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 22 | _cpu_percent_metric = metrics.GaugeMetric( |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 23 | 'proc/cpu_percent', |
| 24 | description='CPU usage percent of processes.') |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | def collect_proc_info(): |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 28 | collector = _ProcessMetricsCollector() |
| 29 | collector.collect() |
| 30 | |
| 31 | |
| 32 | class _ProcessMetricsCollector(object): |
| 33 | """Class for collecting process metrics.""" |
| 34 | |
| 35 | def __init__(self): |
| 36 | self._metrics = [ |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 37 | _ProcessMetric('autoserv', |
| 38 | test_func=_is_parent_autoserv), |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 39 | _ProcessMetric('curl', |
| 40 | test_func=partial(_is_process_name, 'curl')), |
Aviv Keshet | 98f3379 | 2019-10-29 11:05:53 -0700 | [diff] [blame] | 41 | _ProcessMetric('getty', |
| 42 | test_func=partial(_is_process_name, 'getty')), |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 43 | _ProcessMetric('gs_archive_server', |
| 44 | test_func=partial(_is_python_module, |
| 45 | 'gs_archive_server')), |
Allen Li | 3511a83 | 2018-06-27 14:41:01 -0700 | [diff] [blame] | 46 | _ProcessMetric('gs_offloader', |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 47 | test_func=partial(_is_process_name, 'gs_offloader.py')), |
| 48 | _ProcessMetric('gsutil', |
| 49 | test_func=_is_gsutil), |
| 50 | _ProcessMetric('java', |
| 51 | test_func=partial(_is_process_name, 'java')), |
Prathmesh Prabhu | 5ed6f90 | 2018-05-07 14:13:02 -0700 | [diff] [blame] | 52 | _ProcessMetric('lxc-attach', |
| 53 | test_func=partial(_is_process_name, 'lxc-attach')), |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 54 | _ProcessMetric('lxc-start', |
| 55 | test_func=partial(_is_process_name, 'lxc-start')), |
| 56 | _ProcessMetric('sshd', |
| 57 | test_func=partial(_is_process_name, 'sshd')), |
| 58 | _ProcessMetric('swarming_bot', |
| 59 | test_func=_is_swarming_bot), |
Allen Li | 3511a83 | 2018-06-27 14:41:01 -0700 | [diff] [blame] | 60 | _ProcessMetric('sysmon', |
| 61 | test_func=partial(_is_python_module, |
| 62 | 'chromite.scripts.sysmon')), |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 63 | ] |
| 64 | self._other_metric = _ProcessMetric('other') |
| 65 | |
| 66 | def collect(self): |
| 67 | for proc in psutil.process_iter(): |
| 68 | self._collect_proc(proc) |
| 69 | self._flush() |
| 70 | |
| 71 | def _collect_proc(self, proc): |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 72 | for metric in self._metrics: |
Allen Li | f8397a8 | 2017-07-13 13:19:44 -0700 | [diff] [blame] | 73 | if metric.add(proc): |
| 74 | break |
| 75 | else: |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 76 | self._other_metric.add(proc) |
| 77 | |
| 78 | def _flush(self): |
| 79 | for metric in self._metrics: |
| 80 | metric.flush() |
| 81 | self._other_metric.flush() |
| 82 | |
| 83 | |
| 84 | class _ProcessMetric(object): |
| 85 | """Class for gathering process metrics.""" |
| 86 | |
| 87 | def __init__(self, process_name, test_func=lambda proc: True): |
| 88 | """Initialize instance. |
| 89 | |
| 90 | process_name is used to identify the metric stream. |
| 91 | |
| 92 | test_func is a function called |
| 93 | for each process. If it returns True, the process is counted. The |
| 94 | default test is to count every process. |
| 95 | """ |
| 96 | self._fields = { |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 97 | 'process_name': process_name, |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 98 | } |
| 99 | self._test_func = test_func |
| 100 | self._count = 0 |
| 101 | self._cpu_percent = 0 |
| 102 | |
| 103 | def add(self, proc): |
| 104 | """Do metric collection for the given process. |
| 105 | |
| 106 | Returns True if the process was collected. |
| 107 | """ |
| 108 | if not self._test_func(proc): |
| 109 | return False |
| 110 | self._count += 1 |
| 111 | self._cpu_percent += proc.cpu_percent() |
| 112 | return True |
| 113 | |
| 114 | def flush(self): |
| 115 | """Finish collection and send metrics.""" |
| 116 | _count_metric.set(self._count, fields=self._fields) |
| 117 | self._count = 0 |
Aviv Keshet | 0b634e9 | 2017-07-14 14:29:48 -0700 | [diff] [blame] | 118 | _cpu_percent_metric.set(int(round(self._cpu_percent)), fields=self._fields) |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 119 | self._cpu_percent = 0 |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 120 | |
| 121 | |
| 122 | def _is_parent_autoserv(proc): |
| 123 | """Return whether proc is a parent (not forked) autoserv process.""" |
| 124 | return _is_autoserv(proc) and not _is_autoserv(proc.parent()) |
| 125 | |
| 126 | |
| 127 | def _is_autoserv(proc): |
| 128 | """Return whether proc is an autoserv process.""" |
| 129 | # This relies on the autoserv script being run directly. The script should |
| 130 | # be named autoserv exactly and start with a shebang that is /usr/bin/python, |
| 131 | # NOT /bin/env |
Prathmesh Prabhu | 0b795f0 | 2018-05-07 13:12:37 -0700 | [diff] [blame] | 132 | return _is_process_name('autoserv', proc) |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 133 | |
| 134 | |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 135 | def _is_python_module(module, proc): |
| 136 | """Return whether proc is a process running a Python module.""" |
Aviv Keshet | 70a91c5 | 2017-07-17 16:09:09 -0700 | [diff] [blame] | 137 | cmdline = proc.cmdline() |
| 138 | return (cmdline and |
| 139 | cmdline[0].endswith('python') and |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 140 | cmdline[1:3] == ['-m', module]) |
| 141 | |
| 142 | |
Prathmesh Prabhu | 0b795f0 | 2018-05-07 13:12:37 -0700 | [diff] [blame] | 143 | def _is_process_name(name, proc): |
| 144 | """Return whether process proc is named name.""" |
| 145 | return proc.name() == name |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 146 | |
| 147 | |
| 148 | def _is_swarming_bot(proc): |
| 149 | """Return whether proc is a Swarming bot. |
| 150 | |
| 151 | A swarming bot process is like '/usr/bin/python3.8 <bot-zip-path> start_bot'. |
| 152 | """ |
| 153 | cmdline = proc.cmdline() |
| 154 | return (len(cmdline) == 3 and |
| 155 | cmdline[0].split('/')[-1].startswith('python') and |
| 156 | cmdline[2] == 'start_bot') |
| 157 | |
| 158 | |
| 159 | def _is_gsutil(proc): |
| 160 | """Return whether proc is gsutil.""" |
| 161 | cmdline = proc.cmdline() |
| 162 | return (len(cmdline) >= 2 and |
| 163 | cmdline[0] == 'python' and |
| 164 | cmdline[1].endswith('gsutil')) |