summaryrefslogtreecommitdiffstats
path: root/glances
diff options
context:
space:
mode:
authorNicolas Hennion <nicolas.hennion@thalesaleniaspace.com>2021-05-24 18:34:48 +0200
committerNicolas Hennion <nicolas.hennion@thalesaleniaspace.com>2021-05-24 18:34:48 +0200
commit04c61576ad1731fd1705e6447c7bd62135279ea4 (patch)
tree52c6818b4425033f57a99580694f2d1f0ec88d4f /glances
parent783e6e988b3cabdbcb3ea2dd7e353d1170500ea5 (diff)
Replace py-cpuinfo by PsUtil and home made methods
Diffstat (limited to 'glances')
-rw-r--r--glances/cpu_percent.py21
-rw-r--r--glances/plugins/glances_quicklook.py41
2 files changed, 32 insertions, 30 deletions
diff --git a/glances/cpu_percent.py b/glances/cpu_percent.py
index 335fc373..5df7b83d 100644
--- a/glances/cpu_percent.py
+++ b/glances/cpu_percent.py
@@ -2,7 +2,7 @@
#
# This file is part of Glances.
#
-# Copyright (C) 2019 Nicolargo <nicolas@nicolargo.com>
+# Copyright (C) 2021 Nicolargo <nicolas@nicolargo.com>
#
# 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
@@ -29,12 +29,14 @@ class CpuPercent(object):
"""Get and store the CPU percent."""
def __init__(self, cached_time=1):
+ self.cpu_info = {}
self.cpu_percent = 0
self.percpu_percent = []
# cached_time is the minimum time interval between stats updates
# since last update is passed (will retrieve old cached info instead)
self.cached_time = 0
+ self.timer_cpu_info = Timer(0)
self.timer_cpu = Timer(0)
self.timer_percpu = Timer(0)
@@ -50,6 +52,23 @@ class CpuPercent(object):
else:
return self.__get_cpu()
+ def get_info(self):
+ """Get additional informations about the CPU"""
+ # Never update more than 1 time per cached_time
+ if self.timer_cpu_info.finished():
+ # Get the CPU name from the /proc/cpuinfo file
+ # @TODO: Multisystem...
+ try:
+ self.cpu_info['cpu_name'] = open('/proc/cpuinfo', 'r').readlines()[4].split(':')[1][1:-2]
+ except:
+ self.cpu_info['cpu_name'] = 'CPU'
+ # Get the CPU freq current/max
+ self.cpu_info['cpu_hz_current'] = psutil.cpu_freq().current
+ self.cpu_info['cpu_hz'] = psutil.cpu_freq().max
+ # Reset timer for cache
+ self.timer_cpu_info = Timer(self.cached_time)
+ return self.cpu_info
+
def __get_cpu(self):
"""Update and/or return the CPU using the psutil library."""
# Never update more than 1 time per cached_time
diff --git a/glances/plugins/glances_quicklook.py b/glances/plugins/glances_quicklook.py
index babca182..1a99b8f7 100644
--- a/glances/plugins/glances_quicklook.py
+++ b/glances/plugins/glances_quicklook.py
@@ -27,15 +27,6 @@ from glances.plugins.glances_plugin import GlancesPlugin
import psutil
-# Import plugin specific dependency
-try:
- from cpuinfo import cpuinfo
-except ImportError as e:
- cpuinfo_tag = False
- logger.warning("Missing Python Lib ({}), Quicklook plugin will not display CPU info".format(e))
-else:
- cpuinfo_tag = True
-
# Define the history items list
# All items in this list will be historised if the --enable-history tag is set
@@ -79,6 +70,7 @@ class Plugin(GlancesPlugin):
# Get the latest CPU percent value
stats['cpu'] = cpu_percent.get()
stats['percpu'] = cpu_percent.get(percpu=True)
+
# Use the psutil lib for the memory (virtual and swap)
stats['mem'] = psutil.virtual_memory().percent
try:
@@ -86,30 +78,17 @@ class Plugin(GlancesPlugin):
except RuntimeError:
# Correct issue in Illumos OS (see #1767)
stats['swap'] = None
+
+ # Get additional information
+ logger.info(cpu_percent.get_info())
+ stats['cpu_name'] = cpu_percent.get_info()['cpu_name']
+ stats['cpu_hz_current'] = self._mhz_to_hz(cpu_percent.get_info()['cpu_hz_current'])
+ stats['cpu_hz'] = self._mhz_to_hz(cpu_percent.get_info()['cpu_hz'])
+
elif self.input_method == 'snmp':
# Not available
pass
- # Optionnaly, get the CPU name/frequency
- # thanks to the cpuinfo lib: https://github.com/workhorsy/py-cpuinfo
- if cpuinfo_tag:
- cpu_info = cpuinfo.get_cpu_info()
- # Check cpu_info (issue #881)
- if cpu_info is not None:
- # Use brand_raw if the key exist (issue #1685)
- if cpu_info.get('brand_raw') is not None:
- stats['cpu_name'] = cpu_info.get('brand_raw', 'CPU')
- else:
- stats['cpu_name'] = cpu_info.get('brand', 'CPU')
- if 'hz_actual_raw' in cpu_info:
- stats['cpu_hz_current'] = cpu_info['hz_actual_raw'][0]
- elif 'hz_actual' in cpu_info:
- stats['cpu_hz_current'] = cpu_info['hz_actual'][0]
- if 'hz_advertised_raw' in cpu_info:
- stats['cpu_hz'] = cpu_info['hz_advertised_raw'][0]
- elif 'hz_advertised' in cpu_info:
- stats['cpu_hz'] = cpu_info['hz_advertised'][0]
-
# Update the stats
self.stats = stats
@@ -208,3 +187,7 @@ class Plugin(GlancesPlugin):
def _hz_to_ghz(self, hz):
"""Convert Hz to Ghz."""
return hz / 1000000000.0
+
+ def _mhz_to_hz(self, hz):
+ """Convert Mhz to Hz."""
+ return hz * 1000000.0 \ No newline at end of file