18 from typing
import Tuple
24 from py3nvml
import py3nvml
29 def getCpuUsage() -> float:
33 float -> CPU usage as percentage since last call of this function
36 return psutil.cpu_percent()
39 def getRamUsage() -> float:
43 float -> Used RAM memory as percentage
46 return psutil.virtual_memory().percent
49 def getAvailableRam() -> int:
53 int -> total RAM memory in GB as int
56 memory = psutil.virtual_memory()
57 return int(memory.total / (1024 ** 3))
60 def getAvailableCpuCount() -> int:
61 availableCPUs = os.cpu_count()
62 return availableCPUs
if availableCPUs
is not None else 1
65 def getGpuUsage() -> float:
67 py3nvml init must be called before calling this function
68 otherwise it will raise an exception
72 float -> GPU usage as percentage
75 handle = py3nvml.nvmlDeviceGetHandleByIndex(0)
76 utilization = py3nvml.nvmlDeviceGetUtilizationRates(handle)
78 if isinstance(utilization, py3nvml.c_nvmlUtilization_t):
79 return float(utilization.gpu)
81 logging.getLogger(
"coretexpylib").debug(
">> [Coretex] Failed to extract gpu usage metric")
85 def getGpuMemoryUsage() -> float:
87 py3nvml init must be called before calling this function
88 otherwise it will raise an exception
92 float -> GPU memory usage as percentage
95 handle = py3nvml.nvmlDeviceGetHandleByIndex(0)
96 memory = py3nvml.nvmlDeviceGetMemoryInfo(handle)
98 if not isinstance(memory, py3nvml.c_nvmlMemory_t):
99 logging.getLogger(
"coretexpylib").debug(
">> [Coretex] Failed to extract gpu memory usage metric")
102 return float(memory.used / memory.total * 100)
105 def getGpuTemperature() -> float:
107 py3nvml init must be called before calling this function
108 otherwise it will raise an exception
112 float -> GPU temperature
115 handle = py3nvml.nvmlDeviceGetHandleByIndex(0)
116 temperature = py3nvml.nvmlDeviceGetTemperature(handle, py3nvml.NVML_TEMPERATURE_GPU)
118 return float(temperature)
121 def getSwapUsage() -> float:
125 float -> Used swap memory as percentage
128 return psutil.swap_memory().percent
131 def getTotalSwapMemory() -> int:
135 int -> total swap memory in GB
138 return int(psutil.swap_memory().total / (1024 ** 3))
141 def getDiskRead() -> float:
145 float -> total amount of bytes read from disk
148 counters = psutil.disk_io_counters()
151 logging.getLogger(
"coretexpylib").debug(
">> [Coretex] Failed to extract disk read metric")
154 return float(counters.read_bytes)
157 def getDiskWrite() -> float:
161 float -> total amount of bytes wrote to disk
164 counters = psutil.disk_io_counters()
167 logging.getLogger(
"coretexpylib").debug(
">> [Coretex] Failed to extract disk write metric")
170 return float(counters.write_bytes)
173 def getStorageUsage() -> float:
177 float -> Used storage space as percentage
180 info = shutil.disk_usage(
"/")
181 return info.used / info.total * 100
184 def _getNetworkUsage() -> Tuple[float, float]:
185 counters = psutil.net_io_counters(pernic =
True)
190 for counter
in counters.values():
191 bytesRecv = counter.bytes_recv
192 bytesSent = counter.bytes_sent
194 totalBytesRecv += bytesRecv
195 totalBytesSent += bytesSent
197 return float(totalBytesRecv * 8), float(totalBytesSent * 8)
200 def getDownloadSpeed() -> float:
204 float -> total amount of bytes downloaded
207 return _getNetworkUsage()[0]
210 def getUploadSpeed() -> float:
214 float -> total amount of bytes uploaded
217 return _getNetworkUsage()[1]