diff options
author | nicolargo <nicolas@nicolargo.com> | 2023-05-20 17:31:56 +0200 |
---|---|---|
committer | nicolargo <nicolas@nicolargo.com> | 2023-05-20 17:31:56 +0200 |
commit | db12498833cf7a42c61edd1f934eaa760e663962 (patch) | |
tree | 4fd7955eb7e7f0f0ffde1f63c5a07fcb30f49e5d | |
parent | 8f5e6d9c12c72d3b1d5ded0c929f946ed4a663cd (diff) |
Per CPU refactorized
-rw-r--r-- | glances/cpu_percent.py | 2 | ||||
-rw-r--r-- | glances/plugins/percpu/model.py | 78 | ||||
-rw-r--r-- | glances/plugins/plugin/model.py | 17 |
3 files changed, 87 insertions, 10 deletions
diff --git a/glances/cpu_percent.py b/glances/cpu_percent.py index 6789a9c9..b6fc2438 100644 --- a/glances/cpu_percent.py +++ b/glances/cpu_percent.py @@ -19,7 +19,7 @@ class CpuPercent(object): """Get and store the CPU percent.""" - def __init__(self, cached_timer_cpu=3): + def __init__(self, cached_timer_cpu=2): self.cpu_info = {'cpu_name': None, 'cpu_hz_current': None, 'cpu_hz': None} self.cpu_percent = 0 self.percpu_percent = [] diff --git a/glances/plugins/percpu/model.py b/glances/plugins/percpu/model.py index 5e3e6492..14b2b7ab 100644 --- a/glances/plugins/percpu/model.py +++ b/glances/plugins/percpu/model.py @@ -12,6 +12,75 @@ from glances.cpu_percent import cpu_percent from glances.plugins.plugin.model import GlancesPluginModel +# Fields description +# description: human readable description +# short_name: shortname to use un UI +# unit: unit type +# rate: is it a rate ? If yes, // by time_since_update when displayed, +# min_symbol: Auto unit should be used if value > than 1 'X' (K, M, G)... +# +# Example: +# [{'key': 'cpu_number', 'cpu_number': 0, 'total': 24.4, 'user': 20.3, 'system': 3.9, +# 'idle': 75.6, 'nice': 0.0, 'iowait': 0.3, 'irq': 0.0, 'softirq': 0.0, 'steal': 0.0, +# 'guest': 0.0, 'guest_nice': 0.0} +fields_description = { + 'cpu_number': { + 'getter': {'fct': 'cpu_percent.get', 'arg': {'percpu': True}}, + 'description': 'CPU core identifier.', + 'unit': 'number' + }, + 'total': { + 'getter': {'fct': 'cpu_percent.get', 'arg': {'percpu': True}}, + 'description': 'Sum of all CPU percentages for the current Core (except idle).', + 'unit': 'percent' + }, + 'system': { + 'getter': {'fct': 'cpu_percent.get', 'arg': {'percpu': True}}, + 'description': 'percent time spent in kernel space. System CPU time is the \ +time spent running code in the Operating System kernel.', + 'unit': 'percent', + }, + 'user': { + 'getter': {'fct': 'cpu_percent.get', 'arg': {'percpu': True}}, + 'description': 'CPU percent time spent in user space. \ +User CPU time is the time spent on the processor running your program\'s code (or code in libraries).', + 'unit': 'percent', + }, + 'iowait': { + 'getter': {'fct': 'cpu_percent.get', 'arg': {'percpu': True}}, + 'description': '*(Linux)*: percent time spent by the CPU waiting for I/O \ +operations to complete.', + 'unit': 'percent', + }, + 'idle': { + 'getter': {'fct': 'cpu_percent.get', 'arg': {'percpu': True}}, + 'description': 'percent of CPU used by any program. Every program or task \ +that runs on a computer system occupies a certain amount of processing \ +time on the CPU. If the CPU has completed all tasks it is idle.', + 'unit': 'percent', + }, + 'irq': { + 'getter': {'fct': 'cpu_percent.get', 'arg': {'percpu': True}}, + 'description': '*(Linux and BSD)*: percent time spent servicing/handling \ +hardware/software interrupts. Time servicing interrupts (hardware + \ +software).', + 'unit': 'percent', + }, + 'nice': { + 'getter': {'fct': 'cpu_percent.get', 'arg': {'percpu': True}}, + 'description': '*(Unix)*: percent time occupied by user level processes with \ +a positive nice value. The time the CPU has spent running users\' \ +processes that have been *niced*.', + 'unit': 'percent', + }, + 'steal': { + 'getter': {'fct': 'cpu_percent.get', 'arg': {'percpu': True}}, + 'description': '*(Linux)*: percentage of time a virtual CPU waits for a real \ +CPU while the hypervisor is servicing another virtual processor.', + 'unit': 'percent', + }, +} + # Define the history items list items_history_list = [ {'name': 'user', 'description': 'User CPU usage', 'y_unit': '%'}, @@ -29,7 +98,11 @@ class PluginModel(GlancesPluginModel): def __init__(self, args=None, config=None): """Init the plugin.""" super(PluginModel, self).__init__( - args=args, config=config, items_history_list=items_history_list, stats_init_value=[] + args=args, + config=config, + items_history_list=items_history_list, + stats_init_value=[], + fields_description=fields_description ) # We want to display the stat in the curse interface @@ -49,7 +122,8 @@ class PluginModel(GlancesPluginModel): # Grab per-CPU stats using psutil's cpu_percent(percpu=True) and # cpu_times_percent(percpu=True) methods if self.input_method == 'local': - stats = cpu_percent.get(percpu=True) + # stats = cpu_percent.get(percpu=True) + stats = self.update_local(stats) else: # Update stats using SNMP pass diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py index a3c58757..c9a4bd60 100644 --- a/glances/plugins/plugin/model.py +++ b/glances/plugins/plugin/model.py @@ -13,12 +13,16 @@ I am your father... ...of all Glances model plugins. """ +# System import import re import copy -# PsUtil is call "dynamically", so the import should not be removed +# The following lib are imported "dynamically", so the import should not be removed +# You should ignire the warning message from your IDE (imported but not unused) import psutil +from glances.cpu_percent import cpu_percent +# Others imports from glances.globals import iterkeys, itervalues, listkeys, mean, nativestr, json_dumps, json_dumps_dictlist from glances.actions import GlancesActions from glances.history import GlancesHistory @@ -206,20 +210,19 @@ class GlancesPluginModel(object): g_lib = self else: g_lib = globals()[g_lib_name] + # Call the "getter" if g_arg: g_call = getattr(g_lib, g_func)(**g_arg) else: g_call = getattr(g_lib, g_func)() - # The result is stored in the stats dict - # (only if the field is in the self.fields_description) + # Transform the result (because we need serializable data) if hasattr(g_call, '_fields'): - # It's a psutil object + # It's (probably) a psutil object for name in [f for f in g_call._fields if f in self.fields_description]: stats[name] = getattr(g_call, name) else: - # It's a Dict object - for name in [f for f in g_call if f in self.fields_description]: - stats[name] = g_call.get(name, None) + # It's already a Python type + stats = g_call return stats def update_computes(self, stats): |