Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 1 | # Copyright 2016 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 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 5 | """System metrics.""" |
| 6 | |
Allen Li | 13bdf0c | 2017-03-02 15:18:16 -0800 | [diff] [blame] | 7 | from __future__ import absolute_import |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 8 | |
| 9 | import errno |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 10 | import logging |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 11 | import os |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 12 | import time |
| 13 | |
Mike Frysinger | cb56b64 | 2019-08-25 15:33:08 -0400 | [diff] [blame] | 14 | import psutil # pylint: disable=import-error |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 15 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 16 | from chromite.lib import metrics |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 17 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 18 | |
Allen Li | 79317bb | 2016-12-16 18:25:07 -0800 | [diff] [blame] | 19 | logger = logging.getLogger(__name__) |
| 20 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 21 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 22 | _cpu_count_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 23 | "dev/cpu/count", description="Number of CPU cores." |
| 24 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 25 | _cpu_time_metric = metrics.FloatMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 26 | "dev/cpu/time", |
| 27 | description="percentage of time spent by the CPU " "in different states.", |
| 28 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 29 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 30 | _disk_free_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 31 | "dev/disk/free", description="Available bytes on disk partition." |
| 32 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 33 | _disk_total_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 34 | "dev/disk/total", description="Total bytes on disk partition." |
| 35 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 36 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 37 | _inodes_free_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 38 | "dev/inodes/free", |
| 39 | description="Number of available inodes on " "disk partition (unix only).", |
| 40 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 41 | _inodes_total_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 42 | "dev/inodes/total", |
| 43 | description="Number of possible inodes on " "disk partition (unix only)", |
| 44 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 45 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 46 | _mem_free_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 47 | "dev/mem/free", |
| 48 | description="Amount of memory available to a " |
| 49 | "process (in Bytes). Buffers are considered " |
| 50 | "free memory.", |
| 51 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 52 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 53 | _mem_total_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 54 | "dev/mem/total", description="Total physical memory in Bytes." |
| 55 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 56 | |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 57 | _BOOT_TIME = psutil.boot_time() |
Allen Li | c987fc9 | 2017-03-02 14:54:51 -0800 | [diff] [blame] | 58 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 59 | _disk_read_metric = metrics.CounterMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 60 | "dev/disk/read", |
| 61 | start_time=_BOOT_TIME, |
| 62 | description="Number of Bytes read on disk.", |
| 63 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 64 | _disk_write_metric = metrics.CounterMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 65 | "dev/disk/write", |
| 66 | start_time=_BOOT_TIME, |
| 67 | description="Number of Bytes written on disk.", |
| 68 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 69 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 70 | _uptime_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 71 | "dev/uptime", description="Machine uptime, in seconds." |
| 72 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 73 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 74 | _load_average_metric = metrics.FloatMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 75 | "dev/proc/load_average", |
| 76 | description="Number of processes currently " "in the system run queue.", |
| 77 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 78 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 79 | # ts_mon pipeline uses backend clocks when assigning timestamps to metric |
| 80 | # points. By comparing point timestamp to the point value (i.e. time by |
| 81 | # machine's local clock), we can potentially detect some anomalies (clock |
| 82 | # drift, unusually high metrics pipeline delay, completely wrong clocks, etc). |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 83 | # |
| 84 | # It is important to gather this metric right before the flush. |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 85 | _unix_time_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 86 | "dev/unix_time", |
| 87 | description="Number of milliseconds since epoch" |
| 88 | " based on local machine clock.", |
| 89 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 90 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 91 | _os_name_metric = metrics.StringMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 92 | "proc/os/name", description="OS name on the machine" |
| 93 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 94 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 95 | _os_version_metric = metrics.StringMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 96 | "proc/os/version", description="OS version on the machine" |
| 97 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 98 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 99 | _os_arch_metric = metrics.StringMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 100 | "proc/os/arch", description="OS architecture on this machine" |
| 101 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 102 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 103 | _python_arch_metric = metrics.StringMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 104 | "proc/python/arch", |
| 105 | description="python userland " "architecture on this machine", |
| 106 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 107 | |
| 108 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 109 | def collect_uptime(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 110 | _uptime_metric.set(int(time.time() - _BOOT_TIME)) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 111 | |
| 112 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 113 | def collect_cpu_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 114 | _cpu_count_metric.set(psutil.cpu_count()) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 115 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 116 | times = psutil.cpu_times_percent() |
| 117 | for mode in ("user", "system", "idle"): |
| 118 | _cpu_time_metric.set(getattr(times, mode), {"mode": mode}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 119 | |
| 120 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 121 | def collect_disk_info(mountpoints=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 122 | if mountpoints is None: |
| 123 | mountpoints = [disk.mountpoint for disk in psutil.disk_partitions()] |
| 124 | for mountpoint in mountpoints: |
| 125 | _collect_disk_info_single(mountpoint) |
| 126 | _collect_fs_inode_info(mountpoint) |
| 127 | _collect_disk_io_info() |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 128 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 129 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 130 | def _collect_disk_info_single(mountpoint): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 131 | fields = {"path": mountpoint} |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 133 | try: |
| 134 | usage = psutil.disk_usage(mountpoint) |
| 135 | except OSError as ex: |
| 136 | if ex.errno == errno.ENOENT: |
| 137 | # This happens on Windows when querying a removable drive that |
| 138 | # doesn't have any media inserted right now. |
| 139 | pass |
| 140 | else: |
| 141 | raise |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 142 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 143 | _disk_free_metric.set(usage.free, fields=fields) |
| 144 | _disk_total_metric.set(usage.total, fields=fields) |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 146 | # inode counts are only available on Unix. |
| 147 | if os.name == "posix": |
| 148 | _collect_fs_inode_info(mountpoint) |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 149 | |
| 150 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 151 | def _collect_fs_inode_info(mountpoint): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 152 | fields = {"path": mountpoint} |
| 153 | stats = os.statvfs(mountpoint) |
| 154 | _inodes_free_metric.set(stats.f_favail, fields=fields) |
| 155 | _inodes_total_metric.set(stats.f_files, fields=fields) |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 156 | |
| 157 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 158 | def _collect_disk_io_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 159 | try: |
| 160 | # pylint: disable=dict-items-not-iterating |
| 161 | disk_counters = psutil.disk_io_counters(perdisk=True).items() |
| 162 | except RuntimeError as ex: |
| 163 | if "couldn't find any physical disk" in str(ex): |
| 164 | # Disk performance counters aren't enabled on Windows. |
| 165 | pass |
| 166 | else: |
| 167 | raise |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 168 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 169 | for disk, counters in disk_counters: |
| 170 | fields = {"disk": disk} |
| 171 | _disk_read_metric.set(counters.read_bytes, fields=fields) |
| 172 | _disk_write_metric.set(counters.write_bytes, fields=fields) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 173 | |
| 174 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 175 | def collect_mem_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 176 | # We don't report mem.used because (due to virtual memory) it is not |
| 177 | # useful. |
| 178 | mem = psutil.virtual_memory() |
| 179 | _mem_free_metric.set(mem.available) |
| 180 | _mem_total_metric.set(mem.total) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 181 | |
| 182 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 183 | def collect_load_avg(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 184 | try: |
| 185 | avg1, avg5, avg15 = os.getloadavg() |
| 186 | except OSError: |
| 187 | pass |
| 188 | else: |
| 189 | _load_average_metric.set(avg1, fields={"minutes": 1}) |
| 190 | _load_average_metric.set(avg5, fields={"minutes": 5}) |
| 191 | _load_average_metric.set(avg15, fields={"minutes": 15}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 192 | |
| 193 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 194 | def collect_unix_time(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 195 | _unix_time_metric.set(int(time.time() * 1000)) |