-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetrics.py
More file actions
190 lines (150 loc) · 5.33 KB
/
metrics.py
File metadata and controls
190 lines (150 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
CPU/GPU Metrics
===============
Get information relating to the usage of the CPU and GPU (where applicable)
"""
import contextlib
import logging
import psutil
import os
import typing
from .pynvml import (
nvmlDeviceGetComputeRunningProcesses,
nvmlDeviceGetCount,
nvmlDeviceGetGraphicsRunningProcesses,
nvmlDeviceGetHandleByIndex,
nvmlDeviceGetMemoryInfo,
nvmlDeviceGetUtilizationRates,
nvmlInit,
nvmlShutdown,
)
RESOURCES_METRIC_PREFIX: str = "resources"
logger = logging.getLogger(__name__)
def get_process_memory(processes: list[psutil.Process]) -> int:
"""Get the resident set size.
Parameters
----------
processes: list[psutil.Process]
processes to monitor
Returns
-------
int
total process memory
"""
rss: int = 0
for process in processes:
with contextlib.suppress(Exception):
rss += process.memory_info().rss / 1024 / 1024
return rss
def get_process_cpu(
processes: list[psutil.Process], interval: float | None = None
) -> float:
"""Get the CPU usage
If first time being called, use a small interval to collect initial CPU metrics.
Parameters
----------
processes: list[psutil.Process]
list of processes to track for CPU usage.
interval: float, optional
interval to measure across, default is None, use previous measure time difference.
Returns
-------
float
CPU percentage usage
"""
cpu_percent: int = 0
for process in processes:
with contextlib.suppress(Exception):
cpu_percent += process.cpu_percent(interval=interval)
return cpu_percent
def is_gpu_used(handle, processes: list[psutil.Process]) -> bool:
"""Check if the GPU is being used by the list of processes.
Parameters
----------
handle: Unknown
connector to GPU API
processes: list[psutil.Process]
list of processes to monitor
Returns
-------
bool
if GPU is being used
"""
pids = [process.pid for process in processes]
gpu_pids = [process.pid for process in nvmlDeviceGetComputeRunningProcesses(handle)]
gpu_pids.extend(
process.pid for process in nvmlDeviceGetGraphicsRunningProcesses(handle)
)
return len(list(set(gpu_pids) & set(pids))) > 0
def get_gpu_metrics(processes: list[psutil.Process]) -> list[tuple[float, float]]:
"""Get GPU metrics.
Parameters
----------
processes: list[psutil.Process]
list of processes to monitor
Returns
-------
list[tuple[float, float]]
For each GPU identified:
- gpu_percent
- gpu_memory
"""
gpu_metrics: list[tuple[float, float]] = []
with contextlib.suppress(Exception):
nvmlInit()
device_count = nvmlDeviceGetCount()
for i in range(device_count):
handle = nvmlDeviceGetHandleByIndex(i)
if is_gpu_used(handle, processes):
utilisation_percent = nvmlDeviceGetUtilizationRates(handle).gpu
memory = nvmlDeviceGetMemoryInfo(handle)
memory_percent = 100 * memory.free / memory.total
gpu_metrics.append((utilisation_percent, memory_percent))
nvmlShutdown()
return gpu_metrics
class SystemResourceMeasurement:
"""Class for taking and storing a system resources measurement."""
def __init__(
self,
processes: list[psutil.Process],
interval: float | None,
) -> None:
"""Perform a measurement of system resource consumption.
Parameters
----------
processes: list[psutil.Process]
processes to measure across.
interval: float | None
interval to measure, if None previous measure time used for interval.
"""
self.cpu_percent: float | None = get_process_cpu(processes, interval=interval)
self.cpu_memory: float | None = get_process_memory(processes)
self.gpus: list[dict[str, float]] = get_gpu_metrics(processes)
def to_dict(self) -> dict[str, float]:
"""Create metrics dictionary for sending to a Simvue server."""
_metrics: dict[str, float] = {
f"{RESOURCES_METRIC_PREFIX}/cpu.usage.percentage": self.cpu_percent,
f"{RESOURCES_METRIC_PREFIX}/cpu.usage.memory": self.cpu_memory,
f"{RESOURCES_METRIC_PREFIX}/memory.virtual.available.percentage": self.memory_available_percent,
f"{RESOURCES_METRIC_PREFIX}/disk.available.percentage": self.disk_available_percent,
}
for i, gpu in enumerate(self.gpus or []):
_metrics[f"{RESOURCES_METRIC_PREFIX}/gpu.utilisation.percent.{i}"] = gpu[
"utilisation"
]
_metrics[f"{RESOURCES_METRIC_PREFIX}/gpu.utilisation.memory.{i}"] = gpu[
"memory"
]
return _metrics
@property
def gpu_percent(self) -> float:
return sum(m[0] for m in self.gpus or []) / (len(self.gpus or []) or 1)
@property
def gpu_memory(self) -> float:
return sum(m[1] for m in self.gpus or []) / (len(self.gpus or []) or 1)
@property
def memory_available_percent(self) -> float:
return 100 - typing.cast("float", psutil.virtual_memory().percent)
@property
def disk_available_percent(self) -> float:
return 100 - psutil.disk_usage(os.getcwd()).percent