Merge branch 'issue3387' into develop

This commit is contained in:
nicolargo 2025-12-26 18:22:31 +01:00
commit f72b3e226e
2 changed files with 13 additions and 7 deletions

View File

@ -966,3 +966,5 @@ class ProcesslistPlugin(GlancesPluginModel):
# By default return 5 (corresponding to 99999 PID number) # By default return 5 (corresponding to 99999 PID number)
return 5 return 5
# End of file

View File

@ -478,10 +478,7 @@ class GlancesProcesses:
# Only get the info key # Only get the info key
# PsUtil 6+ no longer check PID reused #2755 so use is_running in the loop # PsUtil 6+ no longer check PID reused #2755 so use is_running in the loop
# Note: not sure it is realy needed but CPU consumption look the same with or without it # Note: not sure it is realy needed but CPU consumption look the same with or without it
processlist = [p.info for p in processlist if p.is_running()] return [p.info for p in processlist if p.is_running()]
# Sort the processes list by the current sort_key
return sort_stats(processlist, sorted_by=self.sort_key, reverse=True)
def get_sorted_attrs(self): def get_sorted_attrs(self):
defaults = ['cpu_percent', 'cpu_times', 'memory_percent', 'name', 'status', 'num_threads'] defaults = ['cpu_percent', 'cpu_times', 'memory_percent', 'name', 'status', 'num_threads']
@ -599,6 +596,8 @@ class GlancesProcesses:
# Remove attributes set by the user in the config file (see #1524) # Remove attributes set by the user in the config file (see #1524)
sorted_attrs = [i for i in sorted_attrs if i not in self.disable_stats] sorted_attrs = [i for i in sorted_attrs if i not in self.disable_stats]
# Buid and sort the process list
processlist = self.build_process_list(sorted_attrs) processlist = self.build_process_list(sorted_attrs)
# Update the processcount # Update the processcount
@ -757,6 +756,7 @@ def _sort_io_counters(process, sorted_by='io_counters', sorted_by_secondary='mem
:return: Sum of io_r + io_w :return: Sum of io_r + io_w
""" """
logger.info(f'*** Sort by cpu_times called {type(process[sorted_by])} {process[sorted_by]}')
return process[sorted_by][0] - process[sorted_by][2] + process[sorted_by][1] - process[sorted_by][3] return process[sorted_by][0] - process[sorted_by][2] + process[sorted_by][1] - process[sorted_by][3]
@ -768,7 +768,7 @@ def _sort_cpu_times(process, sorted_by='cpu_times', sorted_by_secondary='memory_
see (https://github.com/giampaolo/psutil/issues/1339) see (https://github.com/giampaolo/psutil/issues/1339)
The following implementation takes user and system time into account The following implementation takes user and system time into account
""" """
return process[sorted_by][0] + process[sorted_by][1] return process[sorted_by]['user'] + process[sorted_by]['system']
def _sort_lambda(sorted_by='cpu_percent', sorted_by_secondary='memory_percent'): def _sort_lambda(sorted_by='cpu_percent', sorted_by_secondary='memory_percent'):
@ -793,18 +793,22 @@ def sort_stats(stats, sorted_by='cpu_percent', sorted_by_secondary='memory_perce
# Specific sort # Specific sort
try: try:
stats = sorted(stats, key=sort_lambda, reverse=reverse) stats = sorted(stats, key=sort_lambda, reverse=reverse)
except Exception: except Exception as e:
# If an error is detected, fallback to cpu_percent # If an error is detected, fallback to cpu_percent
logger.debug(f'Error while sorting by {sorted_by}, fallback to cpu_percent ({e})')
stats = sorted(stats, key=sort_by_these_keys('cpu_percent', sorted_by_secondary), reverse=reverse) stats = sorted(stats, key=sort_by_these_keys('cpu_percent', sorted_by_secondary), reverse=reverse)
else: else:
# Standard sort # Standard sort
try: try:
stats = sorted(stats, key=sort_by_these_keys(sorted_by, sorted_by_secondary), reverse=reverse) stats = sorted(stats, key=sort_by_these_keys(sorted_by, sorted_by_secondary), reverse=reverse)
except (KeyError, TypeError): except (KeyError, TypeError) as e:
# Fallback to name # Fallback to name
logger.debug(f'Error while sorting by {sorted_by}, fallback to name ({e})')
stats.sort(key=lambda process: process['name'] if process['name'] is not None else '~', reverse=False) stats.sort(key=lambda process: process['name'] if process['name'] is not None else '~', reverse=False)
return stats return stats
glances_processes = GlancesProcesses() glances_processes = GlancesProcesses()
# End of file processes.py