From 25cca79218e24eff2178968b683abd2d8c70e12c Mon Sep 17 00:00:00 2001 From: nicolargo Date: Thu, 10 Nov 2016 09:47:39 +0100 Subject: [PATCH] PID column too small if kernel.pid_max is > 99999 #959 --- NEWS | 1 + glances/plugins/glances_processlist.py | 16 +++++++++++-- glances/processes.py | 33 ++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 6f3abb1d..dfcefc2e 100644 --- a/NEWS +++ b/NEWS @@ -22,6 +22,7 @@ Enhancements and news features: * Make the log logger configurable (issue #900) * System uptime in export (issue #890) * Refactor the --disable-* options (issue #948) + * PID column too small if kernel.pid_max is > 99999 (issue #959) Bugs corrected: diff --git a/glances/plugins/glances_processlist.py b/glances/plugins/glances_processlist.py index 07b0755c..87e0a190 100644 --- a/glances/plugins/glances_processlist.py +++ b/glances/plugins/glances_processlist.py @@ -2,7 +2,7 @@ # # This file is part of Glances. # -# Copyright (C) 2015 Nicolargo +# Copyright (C) 2016 Nicolargo # # 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 @@ -82,6 +82,10 @@ class Plugin(GlancesPlugin): # Get the max values (dict) self.max_values = glances_processes.max_values() + # Get the maximum PID number + # Use to optimize space (see https://github.com/nicolargo/glances/issues/959) + self.pid_max = glances_processes.pid_max + # Note: 'glances_processes' is already init in the processes.py script def get_key(self): @@ -240,7 +244,7 @@ class Plugin(GlancesPlugin): ret.append(self.curse_add_line(msg)) ret.append(self.curse_add_line(msg)) # PID - msg = '{:>6}'.format(p['pid']) + msg = '{:>{width}}'.format(p['pid'], width=self.__max_pid_size() + 1) ret.append(self.curse_add_line(msg)) # USER if 'username' in p: @@ -647,3 +651,11 @@ class Plugin(GlancesPlugin): return sort_stats(self.stats, sortedby, tree=glances_processes.is_tree_enabled(), reverse=glances_processes.sort_reverse) + + def __max_pid_size(self): + """Return the maximum PID size in number of char""" + if self.pid_max is not None: + return len(str(self.pid_max)) + else: + # By default return 5 (corresponding to 99999 PID number) + return 5 diff --git a/glances/processes.py b/glances/processes.py index 838e4491..7dd3f172 100644 --- a/glances/processes.py +++ b/glances/processes.py @@ -69,7 +69,7 @@ class GlancesProcesses(object): self._sort_key = 'cpu_percent' self.allprocesslist = [] self.processlist = [] - self.processcount = {'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0} + self.reset_processcount() # Tag to enable/disable the processes stats (to reduce the Glances CPU consumption) # Default is to enable the processes stats @@ -94,6 +94,13 @@ class GlancesProcesses(object): self._max_values = {} self.reset_max_values() + def reset_processcount(self): + self.processcount = {'total': 0, + 'running': 0, + 'sleeping': 0, + 'thread': 0, + 'pid_max': None} + def enable(self): """Enable process stats.""" self.disable_tag = False @@ -112,6 +119,25 @@ class GlancesProcesses(object): """Disable extended process stats.""" self.disable_extended_tag = True + @property + def pid_max(self): + """Get the maximum number of PID + On a Linux operating system, the value is read from + the /proc/sys/kernel/pid_max file. + + If the file is unreadable or not available (on others OS), + return None. + + :returns: int or None + """ + if LINUX: + # For the moment, only available on LINUX + # Waiting from https://github.com/giampaolo/psutil/issues/720 + try: + return int(open('/proc/sys/kernel/pid_max').readline().rstrip()) + except IOError: + return None + @property def max_processes(self): """Get the maximum number of processes showed in the UI.""" @@ -406,7 +432,7 @@ class GlancesProcesses(object): """Update the processes stats.""" # Reset the stats self.processlist = [] - self.processcount = {'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0} + self.reset_processcount() # Do not process if disable tag is set if self.disable_tag: @@ -418,6 +444,9 @@ class GlancesProcesses(object): # Reset the max dict self.reset_max_values() + # Update the maximum process ID (pid) number + self.processcount['pid_max'] = self.pid_max + # Build an internal dict with only mandatories stats (sort keys) processdict = {} excluded_processes = set()