提交 25cca792 编写于 作者: N nicolargo

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

上级 408208b2
...@@ -22,6 +22,7 @@ Enhancements and news features: ...@@ -22,6 +22,7 @@ Enhancements and news features:
* Make the log logger configurable (issue #900) * Make the log logger configurable (issue #900)
* System uptime in export (issue #890) * System uptime in export (issue #890)
* Refactor the --disable-* options (issue #948) * Refactor the --disable-* options (issue #948)
* PID column too small if kernel.pid_max is > 99999 (issue #959)
Bugs corrected: Bugs corrected:
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# #
# This file is part of Glances. # 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 # 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 # it under the terms of the GNU Lesser General Public License as published by
...@@ -82,6 +82,10 @@ class Plugin(GlancesPlugin): ...@@ -82,6 +82,10 @@ class Plugin(GlancesPlugin):
# Get the max values (dict) # Get the max values (dict)
self.max_values = glances_processes.max_values() 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 # Note: 'glances_processes' is already init in the processes.py script
def get_key(self): def get_key(self):
...@@ -240,7 +244,7 @@ class Plugin(GlancesPlugin): ...@@ -240,7 +244,7 @@ class Plugin(GlancesPlugin):
ret.append(self.curse_add_line(msg)) ret.append(self.curse_add_line(msg))
ret.append(self.curse_add_line(msg)) ret.append(self.curse_add_line(msg))
# PID # PID
msg = '{:>6}'.format(p['pid']) msg = '{:>{width}}'.format(p['pid'], width=self.__max_pid_size() + 1)
ret.append(self.curse_add_line(msg)) ret.append(self.curse_add_line(msg))
# USER # USER
if 'username' in p: if 'username' in p:
...@@ -647,3 +651,11 @@ class Plugin(GlancesPlugin): ...@@ -647,3 +651,11 @@ class Plugin(GlancesPlugin):
return sort_stats(self.stats, sortedby, return sort_stats(self.stats, sortedby,
tree=glances_processes.is_tree_enabled(), tree=glances_processes.is_tree_enabled(),
reverse=glances_processes.sort_reverse) 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
...@@ -69,7 +69,7 @@ class GlancesProcesses(object): ...@@ -69,7 +69,7 @@ class GlancesProcesses(object):
self._sort_key = 'cpu_percent' self._sort_key = 'cpu_percent'
self.allprocesslist = [] self.allprocesslist = []
self.processlist = [] 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) # Tag to enable/disable the processes stats (to reduce the Glances CPU consumption)
# Default is to enable the processes stats # Default is to enable the processes stats
...@@ -94,6 +94,13 @@ class GlancesProcesses(object): ...@@ -94,6 +94,13 @@ class GlancesProcesses(object):
self._max_values = {} self._max_values = {}
self.reset_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): def enable(self):
"""Enable process stats.""" """Enable process stats."""
self.disable_tag = False self.disable_tag = False
...@@ -112,6 +119,25 @@ class GlancesProcesses(object): ...@@ -112,6 +119,25 @@ class GlancesProcesses(object):
"""Disable extended process stats.""" """Disable extended process stats."""
self.disable_extended_tag = True 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 @property
def max_processes(self): def max_processes(self):
"""Get the maximum number of processes showed in the UI.""" """Get the maximum number of processes showed in the UI."""
...@@ -406,7 +432,7 @@ class GlancesProcesses(object): ...@@ -406,7 +432,7 @@ class GlancesProcesses(object):
"""Update the processes stats.""" """Update the processes stats."""
# Reset the stats # Reset the stats
self.processlist = [] self.processlist = []
self.processcount = {'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0} self.reset_processcount()
# Do not process if disable tag is set # Do not process if disable tag is set
if self.disable_tag: if self.disable_tag:
...@@ -418,6 +444,9 @@ class GlancesProcesses(object): ...@@ -418,6 +444,9 @@ class GlancesProcesses(object):
# Reset the max dict # Reset the max dict
self.reset_max_values() 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) # Build an internal dict with only mandatories stats (sort keys)
processdict = {} processdict = {}
excluded_processes = set() excluded_processes = set()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册