mirror of https://github.com/nicolargo/glances
Correct unitary tests issues. Add some PEP8 tips.
parent
bdd4735d00
commit
ec27ef53d4
|
@ -2,7 +2,7 @@
|
||||||
#
|
#
|
||||||
# This file is part of Glances.
|
# This file is part of Glances.
|
||||||
#
|
#
|
||||||
# Copyright (C) 2015 Kirby Banman <kirby.banman@gmail.com>
|
# Copyright (C) 2016 Kirby Banman <kirby.banman@gmail.com>
|
||||||
#
|
#
|
||||||
# Glances is free software; you can redistribute it and/or modify
|
# Glances is free software; you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Lesser General Public License as published by
|
# it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
@ -17,20 +17,23 @@
|
||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
"""NVIDIA plugin."""
|
"""GPU plugin (limited to NVIDIA chipsets)"""
|
||||||
|
|
||||||
from glances.logger import logger
|
from glances.logger import logger
|
||||||
from glances.plugins.glances_plugin import GlancesPlugin
|
from glances.plugins.glances_plugin import GlancesPlugin
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from pynvml import *
|
import pynvml
|
||||||
except ImportError:
|
except ImportError:
|
||||||
logger.info("Could not import pynvml. NVIDIA stats will not be collected.")
|
logger.debug("Could not import pynvml. NVIDIA stats will not be collected.")
|
||||||
|
gpu_nvidia_tag = False
|
||||||
|
else:
|
||||||
|
gpu_nvidia_tag = True
|
||||||
|
|
||||||
|
|
||||||
class Plugin(GlancesPlugin):
|
class Plugin(GlancesPlugin):
|
||||||
|
|
||||||
"""Glances NVIDIA plugin.
|
"""Glances GPU plugin (limited to NVIDIA chipsets).
|
||||||
|
|
||||||
stats is a list of dictionaries with one entry per GPU
|
stats is a list of dictionaries with one entry per GPU
|
||||||
"""
|
"""
|
||||||
|
@ -39,38 +42,42 @@ class Plugin(GlancesPlugin):
|
||||||
"""Init the plugin"""
|
"""Init the plugin"""
|
||||||
super(Plugin, self).__init__(args=args)
|
super(Plugin, self).__init__(args=args)
|
||||||
|
|
||||||
try:
|
# Init the NVidia API
|
||||||
nvmlInit()
|
self.init_nvidia()
|
||||||
self.nvml_ready = True
|
|
||||||
self.device_handles = self.get_device_handles()
|
|
||||||
self.devices_ready
|
|
||||||
except Exception:
|
|
||||||
logger.info("pynvml could not be initialized.")
|
|
||||||
self.nvml_ready = False
|
|
||||||
|
|
||||||
|
# We want to display the stat in the curse interface
|
||||||
|
# !!! TODO: Not implemented yeat
|
||||||
self.display_curse = False
|
self.display_curse = False
|
||||||
|
|
||||||
|
# Init the stats
|
||||||
self.reset()
|
self.reset()
|
||||||
|
|
||||||
if self.input_method == 'local':
|
|
||||||
# Update stats
|
|
||||||
self.stats = self.get_stats()
|
|
||||||
elif self.input_method == 'snmp':
|
|
||||||
# Update stats using SNMP
|
|
||||||
# Not avalaible
|
|
||||||
pass
|
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
"""Reset/init the stats."""
|
"""Reset/init the stats."""
|
||||||
self.stats = []
|
self.stats = []
|
||||||
|
|
||||||
|
def init_nvidia(self):
|
||||||
|
"""Init the NVIDIA API"""
|
||||||
|
if not gpu_nvidia_tag:
|
||||||
|
self.nvml_ready = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
pynvml.nvmlInit()
|
||||||
|
self.device_handles = self.get_device_handles()
|
||||||
|
self.nvml_ready = True
|
||||||
|
except Exception:
|
||||||
|
logger.debug("pynvml could not be initialized.")
|
||||||
|
self.nvml_ready = False
|
||||||
|
|
||||||
|
return self.nvml_ready
|
||||||
|
|
||||||
@GlancesPlugin._log_result_decorator
|
@GlancesPlugin._log_result_decorator
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the GPU stats"""
|
"""Update the GPU stats"""
|
||||||
|
|
||||||
self.reset()
|
self.reset()
|
||||||
|
|
||||||
if not self.devices_ready:
|
if not self.nvml_ready:
|
||||||
return self.stats
|
return self.stats
|
||||||
|
|
||||||
if self.input_method == 'local':
|
if self.input_method == 'local':
|
||||||
|
@ -92,9 +99,10 @@ class Plugin(GlancesPlugin):
|
||||||
"""
|
"""
|
||||||
Returns a list of NVML device handles, one per device. Can throw NVMLError.
|
Returns a list of NVML device handles, one per device. Can throw NVMLError.
|
||||||
"""
|
"""
|
||||||
return [nvmlDeviceGetHandleByIndex(i) for i in range(0, nvmlDeviceGetCount())]
|
return [pynvml.nvmlDeviceGetHandleByIndex(i) for i in range(0, pynvml.nvmlDeviceGetCount())]
|
||||||
|
|
||||||
def get_stats(self):
|
def get_stats(self):
|
||||||
|
"""Get GPU stats"""
|
||||||
stats = []
|
stats = []
|
||||||
for index, device_handle in enumerate(self.device_handles):
|
for index, device_handle in enumerate(self.device_handles):
|
||||||
device_stats = {}
|
device_stats = {}
|
||||||
|
@ -108,31 +116,37 @@ class Plugin(GlancesPlugin):
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
def get_device_name(self, device_handle):
|
def get_device_name(self, device_handle):
|
||||||
|
"""Get GPU device name"""
|
||||||
try:
|
try:
|
||||||
return nvmlDeviceGetName(device_handle)
|
return pynvml.nvmlDeviceGetName(device_handle)
|
||||||
except NVMlError:
|
except pynvml.NVMlError:
|
||||||
return "NVIDIA GPU"
|
return "NVIDIA GPU"
|
||||||
|
|
||||||
def get_memory_percent(self, device_handle):
|
def get_memory_percent(self, device_handle):
|
||||||
|
"""Get GPU device memory consumption in percent"""
|
||||||
try:
|
try:
|
||||||
return nvmlDeviceGetUtilizationRates(device_handle).memory
|
return pynvml.nvmlDeviceGetUtilizationRates(device_handle).memory
|
||||||
except NVMLError:
|
except pynvml.NVMLError:
|
||||||
try:
|
try:
|
||||||
memory_info = nvmlDeviceGetMemoryInfo(device_handle)
|
memory_info = pynvml.nvmlDeviceGetMemoryInfo(device_handle)
|
||||||
return memory_info.used * 100 / memory_info.total
|
return memory_info.used * 100 / memory_info.total
|
||||||
except NVMLError:
|
except pynvml.NVMLError:
|
||||||
return -1
|
return None
|
||||||
|
|
||||||
def get_processor_percent(self, device_handle):
|
def get_processor_percent(self, device_handle):
|
||||||
|
"""Get GPU device CPU consumption in percent"""
|
||||||
try:
|
try:
|
||||||
return nvmlDeviceGetUtilizationRates(device_handle).gpu
|
return pynvml.nvmlDeviceGetUtilizationRates(device_handle).gpu
|
||||||
except NVMLError:
|
except pynvml.NVMLError:
|
||||||
return -1
|
return None
|
||||||
|
|
||||||
def exit(self):
|
def exit(self):
|
||||||
super(NvidiaPlugin, self).exit(args=args)
|
"""Overwrite the exit method to close the GPU API"""
|
||||||
if self.nvml_ready:
|
if self.nvml_ready:
|
||||||
try:
|
try:
|
||||||
nvmlShutdown()
|
pynvml.nvmlShutdown()
|
||||||
except Exception:
|
except Exception as e:
|
||||||
logger.warn("pynvml failed to shut down correctly.")
|
logger.debug("pynvml failed to shutdown correctly ({})".format(e))
|
||||||
|
|
||||||
|
# Call the father exit method
|
||||||
|
super(Plugin, self).exit()
|
||||||
|
|
Loading…
Reference in New Issue