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 |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import psutil |
| 11 | |
| 12 | from chromite.lib import cros_logging as logging |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 13 | from chromite.lib import metrics |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 14 | |
| 15 | logger = logging.getLogger(__name__) |
| 16 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 17 | _count_metric = metrics.GaugeMetric( |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 18 | 'proc/count', |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 19 | description='Number of processes currently running.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 20 | _cpu_percent_metric = metrics.GaugeMetric( |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 21 | 'proc/cpu_percent', |
| 22 | description='CPU usage percent of processes.') |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 23 | |
| 24 | |
| 25 | def collect_proc_info(): |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 26 | collector = _ProcessMetricsCollector() |
| 27 | collector.collect() |
| 28 | |
| 29 | |
| 30 | class _ProcessMetricsCollector(object): |
| 31 | """Class for collecting process metrics.""" |
| 32 | |
| 33 | def __init__(self): |
| 34 | self._metrics = [ |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 35 | _ProcessMetric('autoserv', |
| 36 | test_func=_is_parent_autoserv), |
| 37 | _ProcessMetric('sysmon', |
| 38 | test_func=_is_sysmon), |
| 39 | _ProcessMetric('apache', |
| 40 | test_func=_is_apache), |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 41 | ] |
| 42 | self._other_metric = _ProcessMetric('other') |
| 43 | |
| 44 | def collect(self): |
| 45 | for proc in psutil.process_iter(): |
| 46 | self._collect_proc(proc) |
| 47 | self._flush() |
| 48 | |
| 49 | def _collect_proc(self, proc): |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 50 | for metric in self._metrics: |
Allen Li | f8397a8 | 2017-07-13 13:19:44 -0700 | [diff] [blame^] | 51 | if metric.add(proc): |
| 52 | break |
| 53 | else: |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 54 | self._other_metric.add(proc) |
| 55 | |
| 56 | def _flush(self): |
| 57 | for metric in self._metrics: |
| 58 | metric.flush() |
| 59 | self._other_metric.flush() |
| 60 | |
| 61 | |
| 62 | class _ProcessMetric(object): |
| 63 | """Class for gathering process metrics.""" |
| 64 | |
| 65 | def __init__(self, process_name, test_func=lambda proc: True): |
| 66 | """Initialize instance. |
| 67 | |
| 68 | process_name is used to identify the metric stream. |
| 69 | |
| 70 | test_func is a function called |
| 71 | for each process. If it returns True, the process is counted. The |
| 72 | default test is to count every process. |
| 73 | """ |
| 74 | self._fields = { |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 75 | 'process_name': process_name, |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 76 | } |
| 77 | self._test_func = test_func |
| 78 | self._count = 0 |
| 79 | self._cpu_percent = 0 |
| 80 | |
| 81 | def add(self, proc): |
| 82 | """Do metric collection for the given process. |
| 83 | |
| 84 | Returns True if the process was collected. |
| 85 | """ |
| 86 | if not self._test_func(proc): |
| 87 | return False |
| 88 | self._count += 1 |
| 89 | self._cpu_percent += proc.cpu_percent() |
| 90 | return True |
| 91 | |
| 92 | def flush(self): |
| 93 | """Finish collection and send metrics.""" |
| 94 | _count_metric.set(self._count, fields=self._fields) |
| 95 | self._count = 0 |
Aviv Keshet | 0b634e9 | 2017-07-14 14:29:48 -0700 | [diff] [blame] | 96 | _cpu_percent_metric.set(int(round(self._cpu_percent)), fields=self._fields) |
Allen Li | 6bb74d5 | 2017-06-22 14:44:53 -0700 | [diff] [blame] | 97 | self._cpu_percent = 0 |
Allen Li | 51bb612 | 2017-06-21 12:04:13 -0700 | [diff] [blame] | 98 | |
| 99 | |
| 100 | def _is_parent_autoserv(proc): |
| 101 | """Return whether proc is a parent (not forked) autoserv process.""" |
| 102 | return _is_autoserv(proc) and not _is_autoserv(proc.parent()) |
| 103 | |
| 104 | |
| 105 | def _is_autoserv(proc): |
| 106 | """Return whether proc is an autoserv process.""" |
| 107 | # This relies on the autoserv script being run directly. The script should |
| 108 | # be named autoserv exactly and start with a shebang that is /usr/bin/python, |
| 109 | # NOT /bin/env |
| 110 | return proc.name() == 'autoserv' |
| 111 | |
| 112 | |
| 113 | def _is_apache(proc): |
| 114 | """Return whether a proc is an apache2 process.""" |
| 115 | return proc.name() == 'apache2' |
| 116 | |
| 117 | |
| 118 | def _is_sysmon(proc): |
| 119 | """Return whether proc is a sysmon process.""" |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 120 | return (proc.cmdline()[0].endswith('python') |
| 121 | and proc.cmdline()[1:3] == ['-m', 'chromite.scripts.sysmon']) |