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 | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 38 | _ProcessMetric('autoserv', |
| 39 | test_func=_is_parent_autoserv), |
| 40 | _ProcessMetric('sysmon', |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame^] | 41 | test_func=partial(_is_python_module, |
| 42 | 'chromite.scripts.sysmon')), |
| 43 | _ProcessMetric('job_aborter', |
| 44 | test_func=partial(_is_python_module, |
| 45 | 'lucifer.cmd.job_aborter')), |
| 46 | _ProcessMetric('job_reporter', |
| 47 | test_func=partial(_is_python_module, |
| 48 | 'lucifer.cmd.job_reporter')), |
| 49 | _ProcessMetric('lucifer_run_job', |
| 50 | test_func=_is_lucifer_run_job), |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 51 | _ProcessMetric('apache', |
| 52 | test_func=_is_apache), |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 53 | ] |
| 54 | self._other_metric = _ProcessMetric('other') |
| 55 | |
| 56 | def collect(self): |
| 57 | for proc in psutil.process_iter(): |
| 58 | self._collect_proc(proc) |
| 59 | self._flush() |
| 60 | |
| 61 | def _collect_proc(self, proc): |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 62 | for metric in self._metrics: |
Allen Li | f8397a8 | 2017-07-13 13:19:44 -0700 | [diff] [blame] | 63 | if metric.add(proc): |
| 64 | break |
| 65 | else: |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 66 | self._other_metric.add(proc) |
| 67 | |
| 68 | def _flush(self): |
| 69 | for metric in self._metrics: |
| 70 | metric.flush() |
| 71 | self._other_metric.flush() |
| 72 | |
| 73 | |
| 74 | class _ProcessMetric(object): |
| 75 | """Class for gathering process metrics.""" |
| 76 | |
| 77 | def __init__(self, process_name, test_func=lambda proc: True): |
| 78 | """Initialize instance. |
| 79 | |
| 80 | process_name is used to identify the metric stream. |
| 81 | |
| 82 | test_func is a function called |
| 83 | for each process. If it returns True, the process is counted. The |
| 84 | default test is to count every process. |
| 85 | """ |
| 86 | self._fields = { |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 87 | 'process_name': process_name, |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 88 | } |
| 89 | self._test_func = test_func |
| 90 | self._count = 0 |
| 91 | self._cpu_percent = 0 |
| 92 | |
| 93 | def add(self, proc): |
| 94 | """Do metric collection for the given process. |
| 95 | |
| 96 | Returns True if the process was collected. |
| 97 | """ |
| 98 | if not self._test_func(proc): |
| 99 | return False |
| 100 | self._count += 1 |
| 101 | self._cpu_percent += proc.cpu_percent() |
| 102 | return True |
| 103 | |
| 104 | def flush(self): |
| 105 | """Finish collection and send metrics.""" |
| 106 | _count_metric.set(self._count, fields=self._fields) |
| 107 | self._count = 0 |
Aviv Keshet | 0b634e9 | 2017-07-14 14:29:48 -0700 | [diff] [blame] | 108 | _cpu_percent_metric.set(int(round(self._cpu_percent)), fields=self._fields) |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 109 | self._cpu_percent = 0 |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 110 | |
| 111 | |
| 112 | def _is_parent_autoserv(proc): |
| 113 | """Return whether proc is a parent (not forked) autoserv process.""" |
| 114 | return _is_autoserv(proc) and not _is_autoserv(proc.parent()) |
| 115 | |
| 116 | |
| 117 | def _is_autoserv(proc): |
| 118 | """Return whether proc is an autoserv process.""" |
| 119 | # This relies on the autoserv script being run directly. The script should |
| 120 | # be named autoserv exactly and start with a shebang that is /usr/bin/python, |
| 121 | # NOT /bin/env |
| 122 | return proc.name() == 'autoserv' |
| 123 | |
| 124 | |
| 125 | def _is_apache(proc): |
| 126 | """Return whether a proc is an apache2 process.""" |
| 127 | return proc.name() == 'apache2' |
| 128 | |
| 129 | |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame^] | 130 | def _is_python_module(module, proc): |
| 131 | """Return whether proc is a process running a Python module.""" |
Aviv Keshet | 70a91c5 | 2017-07-17 16:09:09 -0700 | [diff] [blame] | 132 | cmdline = proc.cmdline() |
| 133 | return (cmdline and |
| 134 | cmdline[0].endswith('python') and |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame^] | 135 | cmdline[1:3] == ['-m', module]) |
| 136 | |
| 137 | |
| 138 | def _is_lucifer_run_job(proc): |
| 139 | """Return whether proc is a lucifer_run_job process.""" |
| 140 | return proc.name() == 'lucifer_run_job' |