Add caching for processing username and cmdline

This commit is contained in:
Nicolas Hennion 2021-05-26 11:17:58 +02:00
parent 4665727e72
commit 87336db3fd
2 changed files with 33 additions and 6 deletions

View File

@ -80,7 +80,6 @@ class Plugin(GlancesPlugin):
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'])

View File

@ -40,7 +40,8 @@ class GlancesProcesses(object):
# The internals caches will be cleaned each 'cache_timeout' seconds
self.cache_timeout = cache_timeout
self.cache_timer = Timer(self.cache_timeout)
# First iteration, no cache
self.cache_timer = Timer(0)
# Init the io dict
# key = pid
@ -56,6 +57,9 @@ class GlancesProcesses(object):
self.processlist = []
self.reset_processcount()
# Cache is a dict with key=pid and value = dict of cached value
self.processlist_cache = {}
# Tag to enable/disable the processes stats (to reduce the Glances CPU consumption)
# Default is to enable the processes stats
self.disable_tag = False
@ -260,17 +264,28 @@ class GlancesProcesses(object):
# Grab standard stats
#####################
standard_attrs = ['cmdline', 'cpu_percent', 'cpu_times', 'memory_info',
cached_attrs = ['cmdline', 'username']
standard_attrs = ['cpu_percent', 'cpu_times', 'memory_info',
'memory_percent', 'name', 'nice', 'pid', 'ppid',
'status', 'username', 'status', 'num_threads']
'status', 'status', 'num_threads']
if self.cache_timer.finished():
standard_attrs += cached_attrs
self.cache_timer.set(self.cache_timeout)
self.cache_timer.reset()
is_cached = False
else:
is_cached = True
if not self.disable_io_counters:
standard_attrs += ['io_counters']
if not self.disable_gids:
standard_attrs += ['gids']
# and build the processes stats list (psutil>=5.3.0)
self.processlist = [p.info for p in psutil.process_iter(attrs=standard_attrs,
ad_value=None)
self.processlist = [p.info
for p in psutil.process_iter(attrs=standard_attrs,
ad_value=None)
# OS-related processes filter
if not (BSD and p.info['name'] == 'idle') and
not (WINDOWS and p.info['name'] == 'System Idle Process') and
@ -374,6 +389,19 @@ class GlancesProcesses(object):
# Append the IO tag (for display)
proc['io_counters'] += [io_tag]
# Manage cached information
if is_cached:
# Add cached values
if proc['pid'] not in self.processlist_cache:
self.processlist_cache[proc['pid']]= psutil.Process(pid=proc['pid']).as_dict(attrs=cached_attrs,
ad_value=None)
for cached in cached_attrs:
proc[cached] = self.processlist_cache[proc['pid']][cached]
else:
# Save values to cache
self.processlist_cache[proc['pid']] = { cached: proc[cached] for cached in cached_attrs }
# Compute the maximum value for keys in self._max_values_list: CPU, MEM
# Usefull to highlight the processes with maximum values
for k in self._max_values_list: