Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 2 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Process metrics.""" |
| 7 | |
| 8 | from __future__ import absolute_import |
| 9 | from __future__ import print_function |
| 10 | |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 11 | from functools import partial |
| 12 | |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 13 | import psutil |
| 14 | |
| 15 | from chromite.lib import cros_logging as logging |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 16 | from chromite.lib import metrics |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 17 | |
| 18 | logger = logging.getLogger(__name__) |
| 19 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 20 | _count_metric = metrics.GaugeMetric( |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 21 | 'proc/count', |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 22 | description='Number of processes currently running.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 23 | _cpu_percent_metric = metrics.GaugeMetric( |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 24 | 'proc/cpu_percent', |
| 25 | description='CPU usage percent of processes.') |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def collect_proc_info(): |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 29 | collector = _ProcessMetricsCollector() |
| 30 | collector.collect() |
| 31 | |
| 32 | |
| 33 | class _ProcessMetricsCollector(object): |
| 34 | """Class for collecting process metrics.""" |
| 35 | |
| 36 | def __init__(self): |
| 37 | self._metrics = [ |
Allen Li | 3511a83 | 2018-06-27 14:41:01 -0700 | [diff] [blame] | 38 | _ProcessMetric('apache', |
| 39 | test_func=partial(_is_process_name, 'apache2')), |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 40 | _ProcessMetric('autoserv', |
| 41 | test_func=_is_parent_autoserv), |
Allen Li | 3511a83 | 2018-06-27 14:41:01 -0700 | [diff] [blame] | 42 | _ProcessMetric('gs_offloader', |
| 43 | test_func=_is_gs_offloader), |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 44 | _ProcessMetric('job_aborter', |
| 45 | test_func=partial(_is_python_module, |
| 46 | 'lucifer.cmd.job_aborter')), |
| 47 | _ProcessMetric('job_reporter', |
| 48 | test_func=partial(_is_python_module, |
| 49 | 'lucifer.cmd.job_reporter')), |
Allen Li | ee3f5c4 | 2018-08-27 18:07:06 -0700 | [diff] [blame^] | 50 | _ProcessMetric('lucifer', |
| 51 | test_func=partial(_is_process_name, 'lucifer')), |
Prathmesh Prabhu | 5ed6f90 | 2018-05-07 14:13:02 -0700 | [diff] [blame] | 52 | _ProcessMetric('lxc-start', |
| 53 | test_func=partial(_is_process_name, 'lxc-start')), |
| 54 | _ProcessMetric('lxc-attach', |
| 55 | test_func=partial(_is_process_name, 'lxc-attach')), |
Allen Li | 3511a83 | 2018-06-27 14:41:01 -0700 | [diff] [blame] | 56 | _ProcessMetric('sysmon', |
| 57 | test_func=partial(_is_python_module, |
| 58 | 'chromite.scripts.sysmon')), |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 59 | ] |
| 60 | self._other_metric = _ProcessMetric('other') |
| 61 | |
| 62 | def collect(self): |
| 63 | for proc in psutil.process_iter(): |
| 64 | self._collect_proc(proc) |
| 65 | self._flush() |
| 66 | |
| 67 | def _collect_proc(self, proc): |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 68 | for metric in self._metrics: |
Allen Li | f8397a8 | 2017-07-13 13:19:44 -0700 | [diff] [blame] | 69 | if metric.add(proc): |
| 70 | break |
| 71 | else: |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 72 | self._other_metric.add(proc) |
| 73 | |
| 74 | def _flush(self): |
| 75 | for metric in self._metrics: |
| 76 | metric.flush() |
| 77 | self._other_metric.flush() |
| 78 | |
| 79 | |
| 80 | class _ProcessMetric(object): |
| 81 | """Class for gathering process metrics.""" |
| 82 | |
| 83 | def __init__(self, process_name, test_func=lambda proc: True): |
| 84 | """Initialize instance. |
| 85 | |
| 86 | process_name is used to identify the metric stream. |
| 87 | |
| 88 | test_func is a function called |
| 89 | for each process. If it returns True, the process is counted. The |
| 90 | default test is to count every process. |
| 91 | """ |
| 92 | self._fields = { |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 93 | 'process_name': process_name, |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 94 | } |
| 95 | self._test_func = test_func |
| 96 | self._count = 0 |
| 97 | self._cpu_percent = 0 |
| 98 | |
| 99 | def add(self, proc): |
| 100 | """Do metric collection for the given process. |
| 101 | |
| 102 | Returns True if the process was collected. |
| 103 | """ |
| 104 | if not self._test_func(proc): |
| 105 | return False |
| 106 | self._count += 1 |
| 107 | self._cpu_percent += proc.cpu_percent() |
| 108 | return True |
| 109 | |
| 110 | def flush(self): |
| 111 | """Finish collection and send metrics.""" |
| 112 | _count_metric.set(self._count, fields=self._fields) |
| 113 | self._count = 0 |
Aviv Keshet | 0b634e9 | 2017-07-14 14:29:48 -0700 | [diff] [blame] | 114 | _cpu_percent_metric.set(int(round(self._cpu_percent)), fields=self._fields) |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 115 | self._cpu_percent = 0 |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 116 | |
| 117 | |
| 118 | def _is_parent_autoserv(proc): |
| 119 | """Return whether proc is a parent (not forked) autoserv process.""" |
| 120 | return _is_autoserv(proc) and not _is_autoserv(proc.parent()) |
| 121 | |
| 122 | |
| 123 | def _is_autoserv(proc): |
| 124 | """Return whether proc is an autoserv process.""" |
| 125 | # This relies on the autoserv script being run directly. The script should |
| 126 | # be named autoserv exactly and start with a shebang that is /usr/bin/python, |
| 127 | # NOT /bin/env |
Prathmesh Prabhu | 0b795f0 | 2018-05-07 13:12:37 -0700 | [diff] [blame] | 128 | return _is_process_name('autoserv', proc) |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 129 | |
| 130 | |
Allen Li | 3511a83 | 2018-06-27 14:41:01 -0700 | [diff] [blame] | 131 | def _is_gs_offloader(proc): |
| 132 | """Return whether proc is a gs_offloader process.""" |
| 133 | cmdline = proc.cmdline() |
| 134 | return (len(cmdline) >= 2 |
| 135 | and cmdline[0].endswith('python') |
| 136 | and cmdline[1].endswith('gs_offloader.py')) |
| 137 | |
| 138 | |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 139 | def _is_python_module(module, proc): |
| 140 | """Return whether proc is a process running a Python module.""" |
Aviv Keshet | 70a91c5 | 2017-07-17 16:09:09 -0700 | [diff] [blame] | 141 | cmdline = proc.cmdline() |
| 142 | return (cmdline and |
| 143 | cmdline[0].endswith('python') and |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 144 | cmdline[1:3] == ['-m', module]) |
| 145 | |
| 146 | |
Prathmesh Prabhu | 0b795f0 | 2018-05-07 13:12:37 -0700 | [diff] [blame] | 147 | def _is_process_name(name, proc): |
| 148 | """Return whether process proc is named name.""" |
| 149 | return proc.name() == name |