blob: a19a6109785977bb4b2487f3fad0fee628a52d5f [file] [log] [blame]
Allen Li51bb6122017-06-21 12:04:13 -07001# 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
7from __future__ import absolute_import
Allen Li51bb6122017-06-21 12:04:13 -07008
Allen Li3992c662018-01-05 15:26:36 -08009from functools import partial
Chris McDonald59650c32021-07-20 15:29:28 -060010import logging
Allen Li3992c662018-01-05 15:26:36 -080011
Mike Frysingercb56b642019-08-25 15:33:08 -040012import psutil # pylint: disable=import-error
Allen Li51bb6122017-06-21 12:04:13 -070013
Allen Lia9c6e802017-07-11 15:42:47 -070014from chromite.lib import metrics
Allen Li51bb6122017-06-21 12:04:13 -070015
Chris McDonald59650c32021-07-20 15:29:28 -060016
Allen Li51bb6122017-06-21 12:04:13 -070017logger = logging.getLogger(__name__)
18
Allen Lia9c6e802017-07-11 15:42:47 -070019_count_metric = metrics.GaugeMetric(
Allen Li6bb74d52017-06-22 14:44:53 -070020 'proc/count',
Allen Li51bb6122017-06-21 12:04:13 -070021 description='Number of processes currently running.')
Allen Lia9c6e802017-07-11 15:42:47 -070022_cpu_percent_metric = metrics.GaugeMetric(
Allen Li6bb74d52017-06-22 14:44:53 -070023 'proc/cpu_percent',
24 description='CPU usage percent of processes.')
Allen Li51bb6122017-06-21 12:04:13 -070025
26
27def collect_proc_info():
Allen Li6bb74d52017-06-22 14:44:53 -070028 collector = _ProcessMetricsCollector()
29 collector.collect()
30
31
32class _ProcessMetricsCollector(object):
33 """Class for collecting process metrics."""
34
35 def __init__(self):
36 self._metrics = [
Allen Li3511a832018-06-27 14:41:01 -070037 _ProcessMetric('apache',
38 test_func=partial(_is_process_name, 'apache2')),
Allen Li22989bd2017-07-12 10:34:37 -070039 _ProcessMetric('autoserv',
40 test_func=_is_parent_autoserv),
Aviv Keshet98f33792019-10-29 11:05:53 -070041 _ProcessMetric('getty',
42 test_func=partial(_is_process_name, 'getty')),
Allen Li3511a832018-06-27 14:41:01 -070043 _ProcessMetric('gs_offloader',
44 test_func=_is_gs_offloader),
Allen Li3992c662018-01-05 15:26:36 -080045 _ProcessMetric('job_aborter',
46 test_func=partial(_is_python_module,
47 'lucifer.cmd.job_aborter')),
48 _ProcessMetric('job_reporter',
49 test_func=partial(_is_python_module,
50 'lucifer.cmd.job_reporter')),
Allen Liee3f5c42018-08-27 18:07:06 -070051 _ProcessMetric('lucifer',
52 test_func=partial(_is_process_name, 'lucifer')),
Prathmesh Prabhu5ed6f902018-05-07 14:13:02 -070053 _ProcessMetric('lxc-start',
54 test_func=partial(_is_process_name, 'lxc-start')),
55 _ProcessMetric('lxc-attach',
56 test_func=partial(_is_process_name, 'lxc-attach')),
Allen Li3511a832018-06-27 14:41:01 -070057 _ProcessMetric('sysmon',
58 test_func=partial(_is_python_module,
59 'chromite.scripts.sysmon')),
Allen Li6bb74d52017-06-22 14:44:53 -070060 ]
61 self._other_metric = _ProcessMetric('other')
62
63 def collect(self):
64 for proc in psutil.process_iter():
65 self._collect_proc(proc)
66 self._flush()
67
68 def _collect_proc(self, proc):
Allen Li6bb74d52017-06-22 14:44:53 -070069 for metric in self._metrics:
Allen Lif8397a82017-07-13 13:19:44 -070070 if metric.add(proc):
71 break
72 else:
Allen Li6bb74d52017-06-22 14:44:53 -070073 self._other_metric.add(proc)
74
75 def _flush(self):
76 for metric in self._metrics:
77 metric.flush()
78 self._other_metric.flush()
79
80
81class _ProcessMetric(object):
82 """Class for gathering process metrics."""
83
84 def __init__(self, process_name, test_func=lambda proc: True):
85 """Initialize instance.
86
87 process_name is used to identify the metric stream.
88
89 test_func is a function called
90 for each process. If it returns True, the process is counted. The
91 default test is to count every process.
92 """
93 self._fields = {
Allen Li22989bd2017-07-12 10:34:37 -070094 'process_name': process_name,
Allen Li6bb74d52017-06-22 14:44:53 -070095 }
96 self._test_func = test_func
97 self._count = 0
98 self._cpu_percent = 0
99
100 def add(self, proc):
101 """Do metric collection for the given process.
102
103 Returns True if the process was collected.
104 """
105 if not self._test_func(proc):
106 return False
107 self._count += 1
108 self._cpu_percent += proc.cpu_percent()
109 return True
110
111 def flush(self):
112 """Finish collection and send metrics."""
113 _count_metric.set(self._count, fields=self._fields)
114 self._count = 0
Aviv Keshet0b634e92017-07-14 14:29:48 -0700115 _cpu_percent_metric.set(int(round(self._cpu_percent)), fields=self._fields)
Allen Li6bb74d52017-06-22 14:44:53 -0700116 self._cpu_percent = 0
Allen Li51bb6122017-06-21 12:04:13 -0700117
118
119def _is_parent_autoserv(proc):
120 """Return whether proc is a parent (not forked) autoserv process."""
121 return _is_autoserv(proc) and not _is_autoserv(proc.parent())
122
123
124def _is_autoserv(proc):
125 """Return whether proc is an autoserv process."""
126 # This relies on the autoserv script being run directly. The script should
127 # be named autoserv exactly and start with a shebang that is /usr/bin/python,
128 # NOT /bin/env
Prathmesh Prabhu0b795f02018-05-07 13:12:37 -0700129 return _is_process_name('autoserv', proc)
Allen Li51bb6122017-06-21 12:04:13 -0700130
131
Allen Li3511a832018-06-27 14:41:01 -0700132def _is_gs_offloader(proc):
133 """Return whether proc is a gs_offloader process."""
134 cmdline = proc.cmdline()
135 return (len(cmdline) >= 2
136 and cmdline[0].endswith('python')
137 and cmdline[1].endswith('gs_offloader.py'))
138
139
Allen Li3992c662018-01-05 15:26:36 -0800140def _is_python_module(module, proc):
141 """Return whether proc is a process running a Python module."""
Aviv Keshet70a91c52017-07-17 16:09:09 -0700142 cmdline = proc.cmdline()
143 return (cmdline and
144 cmdline[0].endswith('python') and
Allen Li3992c662018-01-05 15:26:36 -0800145 cmdline[1:3] == ['-m', module])
146
147
Prathmesh Prabhu0b795f02018-05-07 13:12:37 -0700148def _is_process_name(name, proc):
149 """Return whether process proc is named name."""
150 return proc.name() == name