glances_processlist.py 27.5 KB
Newer Older
A
Alessio Sergi 已提交
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
A
Alessio Sergi 已提交
4
#
5
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
A
Alessio Sergi 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# 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
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

A
PEP 257  
Alessio Sergi 已提交
20 21
"""Process list plugin."""

22
import operator
23
import os
A
Alessio Sergi 已提交
24 25
from datetime import timedelta

26
from glances.compat import iteritems
27
from glances.globals import LINUX, WINDOWS
28 29
from glances.logger import logger
from glances.processes import glances_processes
30
from glances.plugins.glances_core import Plugin as CorePlugin
A
Alessio Sergi 已提交
31
from glances.plugins.glances_plugin import GlancesPlugin
A
Alessio Sergi 已提交
32

D
desbma 已提交
33

34 35 36 37 38 39 40 41 42 43 44
def convert_timedelta(delta):
    """Convert timedelta to human-readable time."""
    days, total_seconds = delta.days, delta.seconds
    hours = days * 24 + total_seconds // 3600
    minutes = (total_seconds % 3600) // 60
    seconds = str(total_seconds % 60).zfill(2)
    microseconds = str(delta.microseconds)[:2].zfill(2)

    return hours, minutes, seconds, microseconds


45 46 47 48 49 50 51
def split_cmdline(cmdline):
    """Return path, cmd and arguments for a process cmdline."""
    path, cmd = os.path.split(cmdline[0])
    arguments = ' '.join(cmdline[1:]).replace('\n', ' ')
    # XXX: workaround for psutil issue #742
    if LINUX and any(x in cmdline[0] for x in ('chrome', 'chromium')):
        try:
52
            exe, arguments = cmdline[0].split(' ', 1)
53 54 55 56 57 58 59
            path, cmd = os.path.split(exe)
        except ValueError:
            arguments = None

    return path, cmd, arguments


A
Alessio Sergi 已提交
60
class Plugin(GlancesPlugin):
A
PEP 257  
Alessio Sergi 已提交
61 62

    """Glances' processes plugin.
A
Alessio Sergi 已提交
63 64 65 66

    stats is a list
    """

67
    def __init__(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
68
        """Init the plugin."""
A
Alessio Sergi 已提交
69
        super(Plugin, self).__init__(args=args)
A
Alessio Sergi 已提交
70 71 72 73

        # We want to display the stat in the curse interface
        self.display_curse = True

74 75 76
        # Trying to display proc time
        self.tag_proc_time = True

77 78 79 80 81 82
        # Call CorePlugin to get the core number (needed when not in IRIX mode / Solaris mode)
        try:
            self.nb_log_core = CorePlugin(args=self.args).update()["log"]
        except Exception:
            self.nb_log_core = 0

83
        # Note: 'glances_processes' is already init in the processes.py script
84

85
    def get_key(self):
A
PEP 257  
Alessio Sergi 已提交
86
        """Return the key of the list."""
87 88
        return 'pid'

89
    def reset(self):
A
PEP 257  
Alessio Sergi 已提交
90
        """Reset/init the stats."""
91
        self.stats = []
92 93

    def update(self):
A
PEP 257  
Alessio Sergi 已提交
94
        """Update processes stats using the input method."""
95 96
        # Reset stats
        self.reset()
97

98
        if self.input_method == 'local':
99 100 101
            # Update stats using the standard system lib
            # Note: Update is done in the processcount plugin
            # Just return the processes list
102
            if glances_processes.is_tree_enabled():
D
desbma 已提交
103 104 105
                self.stats = glances_processes.gettree()
            else:
                self.stats = glances_processes.getlist()
106
        elif self.input_method == 'snmp':
N
Nicolargo 已提交
107
            # No SNMP grab for processes
108
            pass
109

110
        return self.stats
111

112
    def get_process_tree_curses_data(self, node, args, first_level=True, max_node_count=None):
A
PEP 257  
Alessio Sergi 已提交
113
        """Get curses data to display for a process tree."""
D
desbma 已提交
114
        ret = []
115
        node_count = 0
N
nicolargo 已提交
116
        if not node.is_root and ((max_node_count is None) or (max_node_count > 0)):
D
desbma 已提交
117
            node_data = self.get_process_curses_data(node.stats, False, args)
118
            node_count += 1
D
desbma 已提交
119
            ret.extend(node_data)
A
Alessio Sergi 已提交
120
        for child in node.iter_children():
121
            # stop if we have enough nodes to display
N
nicolargo 已提交
122
            if max_node_count is not None and node_count >= max_node_count:
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                break

            if max_node_count is None:
                children_max_node_count = None
            else:
                children_max_node_count = max_node_count - node_count
            child_data = self.get_process_tree_curses_data(child,
                                                           args,
                                                           first_level=node.is_root,
                                                           max_node_count=children_max_node_count)
            if max_node_count is None:
                node_count += len(child)
            else:
                node_count += min(children_max_node_count, len(child))

D
desbma 已提交
138
            if not node.is_root:
D
desbma 已提交
139
                child_data = self.add_tree_decoration(child_data, child is node.children[-1], first_level)
D
desbma 已提交
140 141 142
            ret.extend(child_data)
        return ret

D
desbma 已提交
143
    def add_tree_decoration(self, child_data, is_last_child, first_level):
A
PEP 257  
Alessio Sergi 已提交
144
        """Add tree curses decoration and indentation to a subtree."""
D
desbma 已提交
145 146 147
        # find process command indices in messages
        pos = []
        for i, m in enumerate(child_data):
D
desbma 已提交
148 149 150
            if m.get("_tree_decoration", False):
                del m["_tree_decoration"]
                pos.append(i)
D
desbma 已提交
151

D
desbma 已提交
152 153 154 155 156 157 158
        # add new curses items for tree decoration
        new_child_data = []
        new_pos = []
        for i, m in enumerate(child_data):
            if i in pos:
                new_pos.append(len(new_child_data))
                new_child_data.append(self.curse_add_line(""))
D
desbma 已提交
159
                new_child_data[-1]["_tree_decoration"] = True
D
desbma 已提交
160 161 162 163
            new_child_data.append(m)
        child_data = new_child_data
        pos = new_pos

D
desbma 已提交
164 165 166 167 168
        # draw node prefix
        if is_last_child:
            prefix = "└─"
        else:
            prefix = "├─"
D
desbma 已提交
169
        child_data[pos[0]]["msg"] = prefix
D
desbma 已提交
170 171 172

        # add indentation
        for i in pos:
D
desbma 已提交
173
            spacing = 2
D
desbma 已提交
174
            if first_level:
D
desbma 已提交
175 176 177 178 179
                spacing = 1
            elif is_last_child and (i is not pos[0]):
                # compensate indentation for missing '│' char
                spacing = 3
            child_data[i]["msg"] = "%s%s" % (" " * spacing, child_data[i]["msg"])
D
desbma 已提交
180 181 182 183 184 185 186 187

        if not is_last_child:
            # add '│' tree decoration
            for i in pos[1:]:
                old_str = child_data[i]["msg"]
                if first_level:
                    child_data[i]["msg"] = " │" + old_str[2:]
                else:
D
desbma 已提交
188
                    child_data[i]["msg"] = old_str[:2] + "│" + old_str[3:]
D
desbma 已提交
189 190
        return child_data

D
desbma 已提交
191
    def get_process_curses_data(self, p, first, args):
A
PEP 257  
Alessio Sergi 已提交
192
        """Get curses data to display for a process."""
193
        ret = [self.curse_new_line()]
D
desbma 已提交
194 195
        # CPU
        if 'cpu_percent' in p and p['cpu_percent'] is not None and p['cpu_percent'] != '':
196 197 198 199
            if args.disable_irix and self.nb_log_core != 0:
                msg = '{0:>6.1f}'.format(p['cpu_percent'] / float(self.nb_log_core))
            else:
                msg = '{0:>6.1f}'.format(p['cpu_percent'])
D
desbma 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
            ret.append(self.curse_add_line(msg,
                                           self.get_alert(p['cpu_percent'], header="cpu")))
        else:
            msg = '{0:>6}'.format('?')
            ret.append(self.curse_add_line(msg))
        # MEM
        if 'memory_percent' in p and p['memory_percent'] is not None and p['memory_percent'] != '':
            msg = '{0:>6.1f}'.format(p['memory_percent'])
            ret.append(self.curse_add_line(msg,
                                           self.get_alert(p['memory_percent'], header="mem")))
        else:
            msg = '{0:>6}'.format('?')
            ret.append(self.curse_add_line(msg))
        # VMS/RSS
        if 'memory_info' in p and p['memory_info'] is not None and p['memory_info'] != '':
            # VMS
            msg = '{0:>6}'.format(self.auto_unit(p['memory_info'][1], low_precision=False))
            ret.append(self.curse_add_line(msg, optional=True))
            # RSS
            msg = '{0:>6}'.format(self.auto_unit(p['memory_info'][0], low_precision=False))
            ret.append(self.curse_add_line(msg, optional=True))
        else:
            msg = '{0:>6}'.format('?')
            ret.append(self.curse_add_line(msg))
            ret.append(self.curse_add_line(msg))
        # PID
        msg = '{0:>6}'.format(p['pid'])
        ret.append(self.curse_add_line(msg))
        # USER
        if 'username' in p:
            # docker internal users are displayed as ints only, therefore str()
            msg = ' {0:9}'.format(str(p['username'])[:9])
            ret.append(self.curse_add_line(msg))
        else:
            msg = ' {0:9}'.format('?')
            ret.append(self.curse_add_line(msg))
        # NICE
        if 'nice' in p:
            nice = p['nice']
            if nice is None:
                nice = '?'
            msg = '{0:>5}'.format(nice)
A
Alessio Sergi 已提交
242 243
            if isinstance(nice, int) and ((WINDOWS and nice != 32) or
                                          (not WINDOWS and nice != 0)):
D
desbma 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
                ret.append(self.curse_add_line(msg, decoration='NICE'))
            else:
                ret.append(self.curse_add_line(msg))
        else:
            msg = '{0:>5}'.format('?')
            ret.append(self.curse_add_line(msg))
        # STATUS
        if 'status' in p:
            status = p['status']
            msg = '{0:>2}'.format(status)
            if status == 'R':
                ret.append(self.curse_add_line(msg, decoration='STATUS'))
            else:
                ret.append(self.curse_add_line(msg))
        else:
            msg = '{0:>2}'.format('?')
            ret.append(self.curse_add_line(msg))
        # TIME+
        if self.tag_proc_time:
            try:
264
                delta = timedelta(seconds=sum(p['cpu_times']))
265 266
            except (OverflowError, TypeError) as e:
                # Catch OverflowError on some Amazon EC2 server
D
desbma 已提交
267
                # See https://github.com/nicolargo/glances/issues/87
268 269
                # Also catch TypeError on Mac OS X
                # See: https://github.com/nicolargo/glances/issues/622
270
                logger.debug("Cannot get TIME+ ({0})".format(e))
D
desbma 已提交
271 272
                self.tag_proc_time = False
            else:
273 274
                hours, minutes, seconds, microseconds = convert_timedelta(delta)
                if hours:
275 276
                    msg = '{0:>4}h'.format(hours)
                    ret.append(self.curse_add_line(msg, decoration='CPU_TIME', optional=True))
277
                    msg = '{0}:{1}'.format(str(minutes).zfill(2), seconds)
278
                else:
279
                    msg = '{0:>4}:{1}.{2}'.format(minutes, seconds, microseconds)
D
desbma 已提交
280
        else:
281
            msg = '{0:>10}'.format('?')
D
desbma 已提交
282 283 284 285
        ret.append(self.curse_add_line(msg, optional=True))
        # IO read/write
        if 'io_counters' in p:
            # IO read
286
            io_rs = int((p['io_counters'][0] - p['io_counters'][2]) / p['time_since_update'])
D
desbma 已提交
287 288 289
            if io_rs == 0:
                msg = '{0:>6}'.format("0")
            else:
290
                msg = '{0:>6}'.format(self.auto_unit(io_rs, low_precision=True))
D
desbma 已提交
291 292
            ret.append(self.curse_add_line(msg, optional=True, additional=True))
            # IO write
293
            io_ws = int((p['io_counters'][1] - p['io_counters'][3]) / p['time_since_update'])
D
desbma 已提交
294 295 296
            if io_ws == 0:
                msg = '{0:>6}'.format("0")
            else:
297
                msg = '{0:>6}'.format(self.auto_unit(io_ws, low_precision=True))
D
desbma 已提交
298 299 300 301 302 303 304
            ret.append(self.curse_add_line(msg, optional=True, additional=True))
        else:
            msg = '{0:>6}'.format("?")
            ret.append(self.curse_add_line(msg, optional=True, additional=True))
            ret.append(self.curse_add_line(msg, optional=True, additional=True))

        # Command line
305 306 307
        # If no command line for the process is available, fallback to
        # the bare process name instead
        cmdline = p['cmdline']
308
        try:
309 310
            # XXX: remove `cmdline != ['']` when we'll drop support for psutil<4.0.0
            if cmdline and cmdline != ['']:
311
                path, cmd, arguments = split_cmdline(cmdline)
312
                if os.path.isdir(path) and not args.process_short_name:
313
                    msg = ' {0}'.format(path) + os.sep
D
desbma 已提交
314
                    ret.append(self.curse_add_line(msg, splittable=True))
D
desbma 已提交
315 316 317
                    if glances_processes.is_tree_enabled():
                        # mark position to add tree decoration
                        ret[-1]["_tree_decoration"] = True
318
                    ret.append(self.curse_add_line(cmd, decoration='PROCESS', splittable=True))
D
desbma 已提交
319
                else:
320
                    msg = ' {0}'.format(cmd)
321
                    ret.append(self.curse_add_line(msg, decoration='PROCESS', splittable=True))
D
desbma 已提交
322 323 324
                    if glances_processes.is_tree_enabled():
                        # mark position to add tree decoration
                        ret[-1]["_tree_decoration"] = True
325 326 327
                if arguments:
                    msg = ' {0}'.format(arguments)
                    ret.append(self.curse_add_line(msg, splittable=True))
328 329
            else:
                msg = ' {0}'.format(p['name'])
330
                ret.append(self.curse_add_line(msg, splittable=True))
331
        except UnicodeEncodeError:
332
            ret.append(self.curse_add_line('', splittable=True))
D
desbma 已提交
333 334 335 336 337 338 339 340 341 342

        # Add extended stats but only for the top processes
        # !!! CPU consumption ???
        # TODO: extended stats into the web interface
        if first and 'extended_stats' in p:
            # Left padding
            xpad = ' ' * 13
            # First line is CPU affinity
            if 'cpu_affinity' in p and p['cpu_affinity'] is not None:
                ret.append(self.curse_new_line())
A
Alessio Sergi 已提交
343
                msg = xpad + 'CPU affinity: ' + str(len(p['cpu_affinity'])) + ' cores'
D
desbma 已提交
344 345
                ret.append(self.curse_add_line(msg, splittable=True))
            # Second line is memory info
346
            if 'memory_info' in p and p['memory_info'] is not None:
D
desbma 已提交
347
                ret.append(self.curse_new_line())
A
Alessio Sergi 已提交
348
                msg = xpad + 'Memory info: '
349
                for k, v in iteritems(p['memory_info']._asdict()):
D
desbma 已提交
350 351 352 353
                    # Ignore rss and vms (already displayed)
                    if k not in ['rss', 'vms'] and v is not None:
                        msg += k + ' ' + self.auto_unit(v, low_precision=False) + ' '
                if 'memory_swap' in p and p['memory_swap'] is not None:
A
Alessio Sergi 已提交
354
                    msg += 'swap ' + self.auto_unit(p['memory_swap'], low_precision=False)
D
desbma 已提交
355 356 357 358
                ret.append(self.curse_add_line(msg, splittable=True))
            # Third line is for open files/network sessions
            msg = ''
            if 'num_threads' in p and p['num_threads'] is not None:
A
Alessio Sergi 已提交
359
                msg += 'threads ' + str(p['num_threads']) + ' '
D
desbma 已提交
360
            if 'num_fds' in p and p['num_fds'] is not None:
A
Alessio Sergi 已提交
361
                msg += 'files ' + str(p['num_fds']) + ' '
D
desbma 已提交
362
            if 'num_handles' in p and p['num_handles'] is not None:
A
Alessio Sergi 已提交
363
                msg += 'handles ' + str(p['num_handles']) + ' '
D
desbma 已提交
364
            if 'tcp' in p and p['tcp'] is not None:
A
Alessio Sergi 已提交
365
                msg += 'TCP ' + str(p['tcp']) + ' '
D
desbma 已提交
366
            if 'udp' in p and p['udp'] is not None:
A
Alessio Sergi 已提交
367
                msg += 'UDP ' + str(p['udp']) + ' '
D
desbma 已提交
368 369
            if msg != '':
                ret.append(self.curse_new_line())
A
Alessio Sergi 已提交
370
                msg = xpad + 'Open: ' + msg
D
desbma 已提交
371 372 373 374
                ret.append(self.curse_add_line(msg, splittable=True))
            # Fouth line is IO nice level (only Linux and Windows OS)
            if 'ionice' in p and p['ionice'] is not None:
                ret.append(self.curse_new_line())
A
Alessio Sergi 已提交
375 376
                msg = xpad + 'IO nice: '
                k = 'Class is '
D
desbma 已提交
377 378 379
                v = p['ionice'].ioclass
                # Linux: The scheduling class. 0 for none, 1 for real time, 2 for best-effort, 3 for idle.
                # Windows: On Windows only ioclass is used and it can be set to 2 (normal), 1 (low) or 0 (very low).
A
Alessio Sergi 已提交
380
                if WINDOWS:
D
desbma 已提交
381 382 383 384 385
                    if v == 0:
                        msg += k + 'Very Low'
                    elif v == 1:
                        msg += k + 'Low'
                    elif v == 2:
A
Alessio Sergi 已提交
386
                        msg += 'No specific I/O priority'
D
desbma 已提交
387 388 389 390
                    else:
                        msg += k + str(v)
                else:
                    if v == 0:
A
Alessio Sergi 已提交
391
                        msg += 'No specific I/O priority'
D
desbma 已提交
392 393 394 395 396 397 398 399 400 401 402
                    elif v == 1:
                        msg += k + 'Real Time'
                    elif v == 2:
                        msg += k + 'Best Effort'
                    elif v == 3:
                        msg += k + 'IDLE'
                    else:
                        msg += k + str(v)
                #  value is a number which goes from 0 to 7.
                # The higher the value, the lower the I/O priority of the process.
                if hasattr(p['ionice'], 'value') and p['ionice'].value != 0:
A
Alessio Sergi 已提交
403
                    msg += ' (value %s/7)' % str(p['ionice'].value)
D
desbma 已提交
404 405 406 407
                ret.append(self.curse_add_line(msg, splittable=True))

        return ret

A
Alessio Sergi 已提交
408
    def msg_curse(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
409
        """Return the dict to display in the curse interface."""
A
Alessio Sergi 已提交
410 411 412
        # Init the return message
        ret = []

413
        # Only process if stats exist and display plugin enable...
D
desbma 已提交
414
        if not self.stats or args.disable_process:
415 416
            return ret

A
Alessio Sergi 已提交
417
        # Compute the sort key
418
        process_sort_key = glances_processes.sort_key
A
Alessio Sergi 已提交
419 420

        # Header
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
        self.__msg_curse_header(ret, process_sort_key, args)

        # Process list
        if glances_processes.is_tree_enabled():
            ret.extend(self.get_process_tree_curses_data(
                self.sort_stats(process_sort_key), args, first_level=True,
                max_node_count=glances_processes.max_processes))
        else:
            # Loop over processes (sorted by the sort key previously compute)
            first = True
            for p in self.sort_stats(process_sort_key):
                ret.extend(self.get_process_curses_data(p, first, args))
                # End of extended stats
                first = False
            if glances_processes.process_filter is not None:
436 437 438
                if args.reset_minmax_tag:
                    args.reset_minmax_tag = not args.reset_minmax_tag
                    self.__mmm_reset()
439
                self.__msg_curse_sum(ret, args=args)
N
nicolargo 已提交
440 441
                self.__msg_curse_sum(ret, mmm='min', args=args)
                self.__msg_curse_sum(ret, mmm='max', args=args)
442 443 444 445 446 447 448 449 450 451

        # Return the message with decoration
        return ret

    def __msg_curse_header(self, ret, process_sort_key, args=None):
        """
        Build the header and add it to the ret dict
        """
        sort_style = 'SORT'

452 453 454 455 456 457
        if args.disable_irix and 0 < self.nb_log_core < 10:
            msg = '{0:>6}'.format('CPU%/' + str(self.nb_log_core))
        elif args.disable_irix and self.nb_log_core != 0:
            msg = '{0:>6}'.format('CPU%/C')
        else:
            msg = '{0:>6}'.format('CPU%')
A
Alessio Sergi 已提交
458
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'cpu_percent' else 'DEFAULT'))
A
Alessio Sergi 已提交
459
        msg = '{0:>6}'.format('MEM%')
A
Alessio Sergi 已提交
460
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'memory_percent' else 'DEFAULT'))
A
Alessio Sergi 已提交
461
        msg = '{0:>6}'.format('VIRT')
A
Alessio Sergi 已提交
462
        ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
463
        msg = '{0:>6}'.format('RES')
A
Alessio Sergi 已提交
464
        ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
465
        msg = '{0:>6}'.format('PID')
466
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
467
        msg = ' {0:10}'.format('USER')
A
Alessio Sergi 已提交
468
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'username' else 'DEFAULT'))
A
Alessio Sergi 已提交
469
        msg = '{0:>4}'.format('NI')
470
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
471
        msg = '{0:>2}'.format('S')
472
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
473
        msg = '{0:>10}'.format('TIME+')
474
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'cpu_times' else 'DEFAULT', optional=True))
475
        msg = '{0:>6}'.format('R/s')
476
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'io_counters' else 'DEFAULT', optional=True, additional=True))
477
        msg = '{0:>6}'.format('W/s')
478
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'io_counters' else 'DEFAULT', optional=True, additional=True))
A
Alessio Sergi 已提交
479
        msg = ' {0:8}'.format('Command')
480
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'name' else 'DEFAULT'))
A
Alessio Sergi 已提交
481

N
nicolargo 已提交
482
    def __msg_curse_sum(self, ret, sep_char='_', mmm=None, args=None):
483 484
        """
        Build the sum message (only when filter is on) and add it to the ret dict
N
nicolargo 已提交
485 486 487 488
        * ret: list of string where the message is added
        * sep_char: define the line separation char
        * mmm: display min, max, mean or current (if mmm=None)
        * args: Glances args
489 490
        """
        ret.append(self.curse_new_line())
N
nicolargo 已提交
491 492 493
        if mmm is None:
            ret.append(self.curse_add_line(sep_char * 69))
            ret.append(self.curse_new_line())
494
        # CPU percent sum
N
nicolargo 已提交
495 496 497
        msg = '{0:>6.1f}'.format(self.__sum_stats('cpu_percent', mmm=mmm))
        ret.append(self.curse_add_line(msg,
                                       decoration=self.__mmm_deco(mmm)))
498
        # MEM percent sum
N
nicolargo 已提交
499 500 501
        msg = '{0:>6.1f}'.format(self.__sum_stats('memory_percent', mmm=mmm))
        ret.append(self.curse_add_line(msg,
                                       decoration=self.__mmm_deco(mmm)))
502 503 504
        # VIRT and RES memory sum
        if 'memory_info' in self.stats[0] and self.stats[0]['memory_info'] is not None and self.stats[0]['memory_info'] != '':
            # VMS
N
nicolargo 已提交
505 506 507 508
            msg = '{0:>6}'.format(self.auto_unit(self.__sum_stats('memory_info', indice=1, mmm=mmm), low_precision=False))
            ret.append(self.curse_add_line(msg,
                                           decoration=self.__mmm_deco(mmm),
                                           optional=True))
509
            # RSS
N
nicolargo 已提交
510 511 512 513
            msg = '{0:>6}'.format(self.auto_unit(self.__sum_stats('memory_info', indice=0, mmm=mmm), low_precision=False))
            ret.append(self.curse_add_line(msg,
                                           decoration=self.__mmm_deco(mmm),
                                           optional=True))
D
desbma 已提交
514
        else:
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
            msg = '{0:>6}'.format('')
            ret.append(self.curse_add_line(msg))
            ret.append(self.curse_add_line(msg))
        # PID
        msg = '{0:>6}'.format('')
        ret.append(self.curse_add_line(msg))
        # USER
        msg = ' {0:9}'.format('')
        ret.append(self.curse_add_line(msg))
        # NICE
        msg = '{0:>5}'.format('')
        ret.append(self.curse_add_line(msg))
        # STATUS
        msg = '{0:>2}'.format('')
        ret.append(self.curse_add_line(msg))
        # TIME+
        msg = '{0:>10}'.format('')
        ret.append(self.curse_add_line(msg, optional=True))
        # IO read/write
N
nicolargo 已提交
534
        if 'io_counters' in self.stats[0] and mmm is None:
535
            # IO read
N
nicolargo 已提交
536
            io_rs = int((self.__sum_stats('io_counters', 0) - self.__sum_stats('io_counters', indice=2, mmm=mmm)) / self.stats[0]['time_since_update'])
537 538 539 540
            if io_rs == 0:
                msg = '{0:>6}'.format('0')
            else:
                msg = '{0:>6}'.format(self.auto_unit(io_rs, low_precision=True))
N
nicolargo 已提交
541 542 543
            ret.append(self.curse_add_line(msg,
                                           decoration=self.__mmm_deco(mmm),
                                           optional=True, additional=True))
544
            # IO write
N
nicolargo 已提交
545
            io_ws = int((self.__sum_stats('io_counters', 1) - self.__sum_stats('io_counters', indice=3, mmm=mmm)) / self.stats[0]['time_since_update'])
546 547 548 549
            if io_ws == 0:
                msg = '{0:>6}'.format('0')
            else:
                msg = '{0:>6}'.format(self.auto_unit(io_ws, low_precision=True))
N
nicolargo 已提交
550 551 552
            ret.append(self.curse_add_line(msg,
                                           decoration=self.__mmm_deco(mmm),
                                           optional=True, additional=True))
553 554 555 556
        else:
            msg = '{0:>6}'.format('')
            ret.append(self.curse_add_line(msg, optional=True, additional=True))
            ret.append(self.curse_add_line(msg, optional=True, additional=True))
N
nicolargo 已提交
557
        if mmm is None:
558
            msg = ' < {0}'.format('current')
N
nicolargo 已提交
559 560
            ret.append(self.curse_add_line(msg, optional=True))
        else:
561 562 563
            msg = ' < {0}'.format(mmm)
            ret.append(self.curse_add_line(msg, optional=True))
            msg = ' (\'M\' to reset)'
N
nicolargo 已提交
564 565 566 567 568 569 570 571 572 573
            ret.append(self.curse_add_line(msg, optional=True))

    def __mmm_deco(self, mmm):
        """
        Return the decoration string for the current mmm status
        """
        if mmm is not None:
            return 'DEFAULT'
        else:
            return 'FILTER'
N
Nicolargo 已提交
574

575 576 577 578 579 580 581
    def __mmm_reset(self):
        """
        Reset the MMM stats
        """
        self.mmm_min = {}
        self.mmm_max = {}

N
nicolargo 已提交
582
    def __sum_stats(self, key, indice=None, mmm=None):
583 584
        """
        Return the sum of the stats value for the given key
N
nicolargo 已提交
585 586
        * indice: If indice is set, get the p[key][indice]
        * mmm: display min, max, mean or current (if mmm=None)
587
        """
N
nicolargo 已提交
588
        # Compute stats summary
589 590 591 592 593 594
        ret = 0
        for p in self.stats:
            if indice is None:
                ret += p[key]
            else:
                ret += p[key][indice]
N
nicolargo 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

        # Manage Min/Max/Mean
        mmm_key = self.__mmm_key(key, indice)
        if mmm == 'min':
            try:
                if self.mmm_min[mmm_key] > ret:
                    self.mmm_min[mmm_key] = ret
            except AttributeError:
                self.mmm_min = {}
                return 0
            except KeyError:
                self.mmm_min[mmm_key] = ret
            ret = self.mmm_min[mmm_key]
        elif mmm == 'max':
            try:
                if self.mmm_max[mmm_key] < ret:
                    self.mmm_max[mmm_key] = ret
            except AttributeError:
                self.mmm_max = {}
                return 0
            except KeyError:
                self.mmm_max[mmm_key] = ret
            ret = self.mmm_max[mmm_key]

        return ret

    def __mmm_key(self, key, indice):
        ret = key
        if indice is not None:
            ret += str(indice)
A
Alessio Sergi 已提交
625
        return ret
626

A
Alessio Sergi 已提交
627
    def sort_stats(self, sortedby=None):
A
PEP 257  
Alessio Sergi 已提交
628
        """Return the stats sorted by sortedby variable."""
629
        if sortedby is None:
630 631 632
            # No need to sort...
            return self.stats

633 634 635
        tree = glances_processes.is_tree_enabled()

        if sortedby == 'io_counters' and not tree:
636 637 638 639
            # Specific case for io_counters
            # Sum of io_r + io_w
            try:
                # Sort process by IO rate (sum IO read + IO write)
D
desbma 已提交
640 641 642
                self.stats.sort(key=lambda process: process[sortedby][0] -
                                process[sortedby][2] + process[sortedby][1] -
                                process[sortedby][3],
A
Alessio Sergi 已提交
643
                                reverse=glances_processes.sort_reverse)
644
            except Exception:
645
                self.stats.sort(key=operator.itemgetter('cpu_percent'),
A
Alessio Sergi 已提交
646
                                reverse=glances_processes.sort_reverse)
647 648
        else:
            # Others sorts
649
            if tree:
A
Alessio Sergi 已提交
650
                self.stats.set_sorting(sortedby, glances_processes.sort_reverse)
651 652
            else:
                try:
653
                    self.stats.sort(key=operator.itemgetter(sortedby),
A
Alessio Sergi 已提交
654
                                    reverse=glances_processes.sort_reverse)
655
                except (KeyError, TypeError):
656
                    self.stats.sort(key=operator.itemgetter('name'),
D
desbma 已提交
657
                                    reverse=False)
658

A
Alessio Sergi 已提交
659
        return self.stats