PID column too small if kernel.pid_max is > 99999 #959

This commit is contained in:
nicolargo 2016-11-10 09:47:39 +01:00
parent 408208b210
commit 25cca79218
3 changed files with 46 additions and 4 deletions

1
NEWS
View File

@ -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:

View File

@ -2,7 +2,7 @@
#
# This file is part of Glances.
#
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
# Copyright (C) 2016 Nicolargo <nicolas@nicolargo.com>
#
# 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

View File

@ -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()