提交 6916f7c4 编写于 作者: N nicolargo

Improve code quality

上级 67485845
......@@ -76,18 +76,19 @@ class Export(GlancesExport):
stat_name = '{}.{}'.format(name, columns[i])
stat_value = points[i]
try:
self.client.gauge(self._normalize(stat_name),
self.client.gauge(normalize(stat_name),
stat_value)
except Exception as e:
logger.error("Can not export stats to Statsd (%s)" % e)
logger.debug("Export {} stats to Statsd".format(name))
def _normalize(self, name):
"""Normalize name for the Statsd convention"""
# Name should not contain some specials chars (issue #1068)
ret = name.replace(':', '')
ret = ret.replace('%', '')
ret = ret.replace(' ', '_')
def normalize(name):
"""Normalize name for the Statsd convention"""
return ret
# Name should not contain some specials chars (issue #1068)
ret = name.replace(':', '')
ret = ret.replace('%', '')
ret = ret.replace(' ', '_')
return ret
......@@ -62,7 +62,7 @@ class Plugin(GlancesPlugin):
try:
pynvml.nvmlInit()
self.device_handles = self.get_device_handles()
self.device_handles = get_device_handles()
self.nvml_ready = True
except Exception:
logger.debug("pynvml could not be initialized.")
......@@ -124,7 +124,7 @@ class Plugin(GlancesPlugin):
return True
def msg_curse(self, args=None):
def msg_curse(self, args=None, max_width=None):
"""Return the dict to display in the curse interface."""
# Init the return message
ret = []
......@@ -209,12 +209,6 @@ class Plugin(GlancesPlugin):
return ret
def get_device_handles(self):
"""
Returns a list of NVML device handles, one per device. Can throw NVMLError.
"""
return [pynvml.nvmlDeviceGetHandleByIndex(i) for i in range(pynvml.nvmlDeviceGetCount())]
def get_device_stats(self):
"""Get GPU stats"""
stats = []
......@@ -226,37 +220,15 @@ class Plugin(GlancesPlugin):
# GPU id (for multiple GPU, start at 0)
device_stats['gpu_id'] = index
# GPU name
device_stats['name'] = self.get_device_name(device_handle)
device_stats['name'] = get_device_name(device_handle)
# Memory consumption in % (not available on all GPU)
device_stats['mem'] = self.get_mem(device_handle)
device_stats['mem'] = get_mem(device_handle)
# Processor consumption in %
device_stats['proc'] = self.get_proc(device_handle)
device_stats['proc'] = get_proc(device_handle)
stats.append(device_stats)
return stats
def get_device_name(self, device_handle):
"""Get GPU device name"""
try:
return pynvml.nvmlDeviceGetName(device_handle)
except pynvml.NVMlError:
return "NVIDIA"
def get_mem(self, device_handle):
"""Get GPU device memory consumption in percent"""
try:
memory_info = pynvml.nvmlDeviceGetMemoryInfo(device_handle)
return memory_info.used * 100.0 / memory_info.total
except pynvml.NVMLError:
return None
def get_proc(self, device_handle):
"""Get GPU device CPU consumption in percent"""
try:
return pynvml.nvmlDeviceGetUtilizationRates(device_handle).gpu
except pynvml.NVMLError:
return None
def exit(self):
"""Overwrite the exit method to close the GPU API"""
if self.nvml_ready:
......@@ -267,3 +239,35 @@ class Plugin(GlancesPlugin):
# Call the father exit method
super(Plugin, self).exit()
def get_device_handles():
"""
Returns a list of NVML device handles, one per device. Can throw NVMLError.
"""
return [pynvml.nvmlDeviceGetHandleByIndex(i) for i in range(pynvml.nvmlDeviceGetCount())]
def get_device_name(device_handle):
"""Get GPU device name"""
try:
return pynvml.nvmlDeviceGetName(device_handle)
except pynvml.NVMlError:
return "NVIDIA"
def get_mem(device_handle):
"""Get GPU device memory consumption in percent"""
try:
memory_info = pynvml.nvmlDeviceGetMemoryInfo(device_handle)
return memory_info.used * 100.0 / memory_info.total
except pynvml.NVMLError:
return None
def get_proc(device_handle):
"""Get GPU device CPU consumption in percent"""
try:
return pynvml.nvmlDeviceGetUtilizationRates(device_handle).gpu
except pynvml.NVMLError:
return None
......@@ -107,7 +107,7 @@ class Plugin(GlancesPlugin):
return 'OK'
def msg_curse(self, args=None):
def msg_curse(self, args=None, max_width=None):
"""Return the dict to display in the curse interface."""
# Init the return message
# Only process if stats exist and display plugin enable...
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册