summaryrefslogtreecommitdiffstats
path: root/glances/plugins/percpu/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'glances/plugins/percpu/__init__.py')
-rw-r--r--glances/plugins/percpu/__init__.py62
1 files changed, 50 insertions, 12 deletions
diff --git a/glances/plugins/percpu/__init__.py b/glances/plugins/percpu/__init__.py
index 87e292b8..66e2c438 100644
--- a/glances/plugins/percpu/__init__.py
+++ b/glances/plugins/percpu/__init__.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
@@ -96,16 +95,23 @@ class PluginModel(GlancesPluginModel):
def __init__(self, args=None, config=None):
"""Init the plugin."""
- super(PluginModel, self).__init__(
- args=args, config=config,
+ super().__init__(
+ args=args,
+ config=config,
items_history_list=items_history_list,
stats_init_value=[],
- fields_description=fields_description
+ fields_description=fields_description,
)
# We want to display the stat in the curse interface
self.display_curse = True
+ # Manage the maximum number of CPU to display (related to enhancement request #2734)
+ if config:
+ self.max_cpu_display = config.get_int_value('percpu', 'max_cpu_display', 4)
+ else:
+ self.max_cpu_display = 4
+
def get_key(self):
"""Return the key of the list."""
return 'cpu_number'
@@ -139,35 +145,67 @@ class PluginModel(GlancesPluginModel):
if not self.stats or not self.args.percpu or self.is_disabled():
return ret
+ # Define the default header
+ header = ['user', 'system', 'idle', 'iowait', 'steal']
+
# Build the string message
if self.is_disabled('quicklook'):
- msg = '{:7}'.format('PER CPU')
+ msg = '{:5}'.format('CPU')
ret.append(self.curse_add_line(msg, "TITLE"))
+ header.insert(0, 'total')
# Per CPU stats displayed per line
- for stat in ['user', 'system', 'idle', 'iowait', 'steal']:
+ for stat in header:
if stat not in self.stats[0]:
continue
- msg = '{:>7}'.format(stat)
+ msg = f'{stat:>7}'
ret.append(self.curse_add_line(msg))
+ # Manage the maximum number of CPU to display (related to enhancement request #2734)
+ if len(self.stats) > self.max_cpu_display:
+ # If the number of CPU is > max_cpu_display then sort and display top 'n'
+ percpu_list = sorted(self.stats, key=lambda x: x['total'], reverse=True)
+ else:
+ percpu_list = self.stats
+
# Per CPU stats displayed per column
- for cpu in self.stats:
+ for cpu in percpu_list[0 : self.max_cpu_display]:
ret.append(self.curse_new_line())
if self.is_disabled('quicklook'):
try:
- msg = '{:6.1f}%'.format(cpu['total'])
+ cpu_id = cpu[cpu['key']]
+ if cpu_id < 10:
+ msg = f'CPU{cpu_id:1} '
+ else:
+ msg = f'{cpu_id:4} '
except TypeError:
# TypeError: string indices must be integers (issue #1027)
- msg = '{:>6}%'.format('?')
+ msg = '{:4} '.format('?')
ret.append(self.curse_add_line(msg))
- for stat in ['user', 'system', 'idle', 'iowait', 'steal']:
+ for stat in header:
if stat not in self.stats[0]:
continue
try:
- msg = '{:6.1f}%'.format(cpu[stat])
+ msg = f'{cpu[stat]:6.1f}%'
except TypeError:
msg = '{:>6}%'.format('?')
ret.append(self.curse_add_line(msg, self.get_alert(cpu[stat], header=stat)))
+ # Add a new line with sum of all others CPU
+ if len(self.stats) > self.max_cpu_display:
+ ret.append(self.curse_new_line())
+ if self.is_disabled('quicklook'):
+ ret.append(self.curse_add_line('CPU* '))
+ for stat in header:
+ if stat not in self.stats[0]:
+ continue
+ cpu_stat = sum([i[stat] for i in percpu_list[0 : self.max_cpu_display]]) / len(
+ [i[stat] for i in percpu_list[0 : self.max_cpu_display]]
+ )
+ try:
+ msg = f'{cpu_stat:6.1f}%'
+ except TypeError:
+ msg = '{:>6}%'.format('?')
+ ret.append(self.curse_add_line(msg, self.get_alert(cpu_stat, header=stat)))
+
return ret