Test Failed
Push — develop ( fdd819...a14634 )
by Nicolas
02:40 queued 17s
created

glances.plugins.processlist.PluginModel.__init__()   C

Complexity

Conditions 9

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 25
nop 3
dl 0
loc 43
rs 6.6666
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# SPDX-FileCopyrightText: 2023 Nicolas Hennion <[email protected]>
6
#
7
# SPDX-License-Identifier: LGPL-3.0-only
8
#
9
10
"""Process list plugin."""
11
12
import os
13
import copy
14
15
from glances.logger import logger
16
from glances.globals import WINDOWS, key_exist_value_not_none_not_v, replace_special_chars
17
from glances.processes import glances_processes, sort_stats
18
from glances.outputs.glances_unicode import unicode_message
19
from glances.plugins.core import PluginModel as CorePluginModel
20
from glances.plugins.plugin.model import GlancesPluginModel
21
22
# Fields description
23
# description: human readable description
24
# short_name: shortname to use un UI
25
# unit: unit type
26
# rate: if True then compute and add *_gauge and *_rate_per_is fields
27
# min_symbol: Auto unit should be used if value > than 1 'X' (K, M, G)...
28
fields_description = {
29
    'pid': {
30
        'description': 'Process identifier (ID)',
31
        'unit': 'number',
32
    },
33
    'name': {
34
        'description': 'Process name',
35
        'unit': 'string',
36
    },
37
    'cmdline': {
38
        'description': 'Command line with arguments',
39
        'unit': 'list',
40
    },
41
    'username': {
42
        'description': 'Process owner',
43
        'unit': 'string',
44
    },
45
    'num_threads': {
46
        'description': 'Number of threads',
47
        'unit': 'number',
48
    },
49
    'cpu_percent': {
50
        'description': 'Process CPU consumption',
51
        'unit': 'percent',
52
    },
53
    'memory_percent': {
54
        'description': 'Process memory consumption',
55
        'unit': 'percent',
56
    },
57
    'memory_info': {
58
        'description': 'Process memory information (dict with rss, vms, shared, text, lib, data, dirty keys)',
59
        'unit': 'byte',
60
    },
61
    'status': {
62
        'description': 'Process status',
63
        'unit': 'string',
64
    },
65
    'nice': {
66
        'description': 'Process nice value',
67
        'unit': 'number',
68
    },
69
    'cpu_times': {
70
        'description': 'Process CPU times (dict with user, system, iowait keys)',
71
        'unit': 'second',
72
    },
73
    'gids': {
74
        'description': 'Process group IDs (dict with real, effective, saved keys)',
75
        'unit': 'number',
76
    },
77
    'io_counters': {
78
        'description': 'Process IO counters (list with read_count, write_count, read_bytes, write_bytes, io_tag keys)',
79
        'unit': 'byte',
80
    },
81
}
82
83
84
def seconds_to_hms(input_seconds):
85
    """Convert seconds to human-readable time."""
86
    minutes, seconds = divmod(input_seconds, 60)
87
    hours, minutes = divmod(minutes, 60)
88
89
    hours = int(hours)
90
    minutes = int(minutes)
91
    seconds = str(int(seconds)).zfill(2)
92
93
    return hours, minutes, seconds
94
95
96
def split_cmdline(bare_process_name, cmdline):
97
    """Return path, cmd and arguments for a process cmdline based on bare_process_name.
98
99
    If first argument of cmdline starts with the bare_process_name then
100
    cmdline will just be considered cmd and path will be empty (see https://github.com/nicolargo/glances/issues/1795)
101
102
    :param bare_process_name: Name of the process from psutil
103
    :param cmdline: cmdline from psutil
104
    :return: Tuple with three strings, which are path, cmd and arguments of the process
105
    """
106
    if cmdline[0].startswith(bare_process_name):
107
        path, cmd = "", cmdline[0]
108
    else:
109
        path, cmd = os.path.split(cmdline[0])
110
    arguments = ' '.join(cmdline[1:])
111
    return path, cmd, arguments
112
113
114
class PluginModel(GlancesPluginModel):
115
    """Glances' processes plugin.
116
117
    stats is a list
118
    """
119
120
    # Define the header layout of the processes list columns
121
    layout_header = {
122
        'cpu': '{:<6} ',
123
        'mem': '{:<5} ',
124
        'virt': '{:<5} ',
125
        'res': '{:<5} ',
126
        'pid': '{:>{width}} ',
127
        'user': '{:<10} ',
128
        'time': '{:>8} ',
129
        'thread': '{:<3} ',
130
        'nice': '{:>3} ',
131
        'status': '{:>1} ',
132
        'ior': '{:>4} ',
133
        'iow': '{:<4} ',
134
        'command': '{} {}',
135
    }
136
137
    # Define the stat layout of the processes list columns
138
    layout_stat = {
139
        'cpu': '{:<6.1f}',
140
        'cpu_no_digit': '{:<6.0f}',
141
        'mem': '{:<5.1f} ',
142
        'virt': '{:<5} ',
143
        'res': '{:<5} ',
144
        'pid': '{:>{width}} ',
145
        'user': '{:<10} ',
146
        'time': '{:>8} ',
147
        'thread': '{:<3} ',
148
        'nice': '{:>3} ',
149
        'status': '{:>1} ',
150
        'ior': '{:>4} ',
151
        'iow': '{:<4} ',
152
        'command': '{}',
153
        'name': '[{}]',
154
    }
155
156
    def __init__(self, args=None, config=None):
157
        """Init the plugin."""
158
        super(PluginModel, self).__init__(args=args, config=config,
159
                                          fields_description=fields_description,
160
                                          stats_init_value=[])
161
162
        # We want to display the stat in the curse interface
163
        self.display_curse = True
164
165
        # Trying to display proc time
166
        self.tag_proc_time = True
167
168
        # Call CorePluginModel to get the core number (needed when not in IRIX mode / Solaris mode)
169
        try:
170
            self.nb_log_core = CorePluginModel(args=self.args).update()["log"]
171
        except Exception:
172
            self.nb_log_core = 0
173
174
        # Get the max values (dict)
175
        self.max_values = copy.deepcopy(glances_processes.max_values())
176
177
        # Get the maximum PID number
178
        # Use to optimize space (see https://github.com/nicolargo/glances/issues/959)
179
        self.pid_max = glances_processes.pid_max
180
181
        # Set the default sort key if it is defined in the configuration file
182
        if config is not None and 'processlist' in config.as_dict():
183
            if 'sort_key' in config.as_dict()['processlist']:
184
                logger.debug(
185
                    'Configuration overwrites processes sort key by {}'.format(
186
                        config.as_dict()['processlist']['sort_key']
187
                    )
188
                )
189
                glances_processes.set_sort_key(config.as_dict()['processlist']['sort_key'], False)
190
            if 'export' in config.as_dict()['processlist']:
191
                glances_processes.export_process_filter = config.as_dict()['processlist']['export']
192
                if args.export:
193
                    logger.info("Export process filter is set to: {}".format(
194
                        config.as_dict()['processlist']['export']))
195
196
        # The default sort key could also be overwrite by command line (see #1903)
197
        if args and args.sort_processes_key is not None:
198
            glances_processes.set_sort_key(args.sort_processes_key, False)
199
200
        # Note: 'glances_processes' is already init in the processes.py script
201
202
    def get_key(self):
203
        """Return the key of the list."""
204
        return 'pid'
205
206
    def update(self):
207
        """Update processes stats using the input method."""
208
        # Update the stats
209
        if self.input_method == 'local':
210
            stats = self.update_local()
211
        else:
212
            stats = self.get_init_value()
213
214
        # Get the max values (dict)
215
        # Use Deep copy to avoid change between update and display
216
        self.max_values = copy.deepcopy(glances_processes.max_values())
217
218
        # Update the stats
219
        self.stats = stats
220
221
        return self.stats
222
223
    def update_local(self):
224
        # Update stats using the standard system lib
225
        # Note: Update is done in the processcount plugin
226
        # Just return the result
227
        if self.args.programs:
228
            stats = glances_processes.get_list(as_programs=True)
229
        else:
230
            stats = glances_processes.get_list()
231
232
        return stats
233
234
    def get_export(self):
235
        """Return the processes list to export.
236
        Not all the processeses are exported.
237
        Only the one defined in the Glances configuration file (see #794 for details).
238
        """
239
        return glances_processes.get_export()
240
241
    def get_nice_alert(self, value):
242
        """Return the alert relative to the Nice configuration list"""
243
        value = str(value)
244
        try:
245
            if value in self.get_limit('nice_critical'):
246
                return 'CRITICAL'
247
        except KeyError:
248
            pass
249
        try:
250
            if value in self.get_limit('nice_warning'):
251
                return 'WARNING'
252
        except KeyError:
253
            pass
254
        try:
255
            if value in self.get_limit('nice_careful'):
256
                return 'CAREFUL'
257
        except KeyError:
258
            pass
259
        return 'DEFAULT'
260
261
    def _get_process_curses_cpu(self, p, selected, args):
262
        """Return process CPU curses"""
263
        if key_exist_value_not_none_not_v('cpu_percent', p, ''):
264
            cpu_layout = self.layout_stat['cpu'] if p['cpu_percent'] < 100 else self.layout_stat['cpu_no_digit']
265
            if args.disable_irix and self.nb_log_core != 0:
266
                msg = cpu_layout.format(p['cpu_percent'] / float(self.nb_log_core))
267
            else:
268
                msg = cpu_layout.format(p['cpu_percent'])
269
            alert = self.get_alert(
270
                p['cpu_percent'],
271
                highlight_zero=False,
272
                is_max=(p['cpu_percent'] == self.max_values['cpu_percent']),
273
                header="cpu",
274
            )
275
            ret = self.curse_add_line(msg, alert)
276
        else:
277
            msg = self.layout_header['cpu'].format('?')
278
            ret = self.curse_add_line(msg)
279
        return ret
280
281
    def _get_process_curses_mem(self, p, selected, args):
282
        """Return process MEM curses"""
283
        if key_exist_value_not_none_not_v('memory_percent', p, ''):
284
            msg = self.layout_stat['mem'].format(p['memory_percent'])
285
            alert = self.get_alert(
286
                p['memory_percent'],
287
                highlight_zero=False,
288
                is_max=(p['memory_percent'] == self.max_values['memory_percent']),
289
                header="mem",
290
            )
291
            ret = self.curse_add_line(msg, alert)
292
        else:
293
            msg = self.layout_header['mem'].format('?')
294
            ret = self.curse_add_line(msg)
295
        return ret
296
297
    def _get_process_curses_vms(self, p, selected, args):
298
        """Return process VMS curses"""
299
        if key_exist_value_not_none_not_v('memory_info', p, '', 1) and 'vms' in p['memory_info']:
300
            msg = self.layout_stat['virt'].format(self.auto_unit(p['memory_info']['vms'], low_precision=False))
301
            ret = self.curse_add_line(msg, optional=True)
302
        else:
303
            msg = self.layout_header['virt'].format('?')
304
            ret = self.curse_add_line(msg)
305
        return ret
306
307
    def _get_process_curses_rss(self, p, selected, args):
308
        """Return process RSS curses"""
309
        if key_exist_value_not_none_not_v('memory_info', p, '', 0) and 'rss' in p['memory_info']:
310
            msg = self.layout_stat['res'].format(self.auto_unit(p['memory_info']['rss'], low_precision=False))
311
            ret = self.curse_add_line(msg, optional=True)
312
        else:
313
            msg = self.layout_header['res'].format('?')
314
            ret = self.curse_add_line(msg)
315
        return ret
316
317
    def _get_process_curses_username(self, p, selected, args):
318
        """Return process username curses"""
319
        if 'username' in p:
320
            # docker internal users are displayed as ints only, therefore str()
321
            # Correct issue #886 on Windows OS
322
            msg = self.layout_stat['user'].format(str(p['username'])[:9])
323
            ret = self.curse_add_line(msg)
324
        else:
325
            msg = self.layout_header['user'].format('?')
326
            ret = self.curse_add_line(msg)
327
        return ret
328
329
    def _get_process_curses_time(self, p, selected, args):
330
        """Return process time curses"""
331
        try:
332
            # Sum user and system time
333
            user_system_time = p['cpu_times']['user'] + p['cpu_times']['system']
334
        except (OverflowError, TypeError):
335
            # Catch OverflowError on some Amazon EC2 server
336
            # See https://github.com/nicolargo/glances/issues/87
337
            # Also catch TypeError on macOS
338
            # See: https://github.com/nicolargo/glances/issues/622
339
            # logger.debug("Cannot get TIME+ ({})".format(e))
340
            msg = self.layout_header['time'].format('?')
341
            ret = self.curse_add_line(msg, optional=True)
342
        else:
343
            hours, minutes, seconds = seconds_to_hms(user_system_time)
344
            if hours > 99:
345
                msg = '{:<7}h'.format(hours)
346
            elif 0 < hours < 100:
347
                msg = '{}h{}:{}'.format(hours, minutes, seconds)
348
            else:
349
                msg = '{}:{}'.format(minutes, seconds)
350
            msg = self.layout_stat['time'].format(msg)
351
            if hours > 0:
352
                ret = self.curse_add_line(msg, decoration='CPU_TIME', optional=True)
353
            else:
354
                ret = self.curse_add_line(msg, optional=True)
355
        return ret
356
357
    def _get_process_curses_thread(self, p, selected, args):
358
        """Return process thread curses"""
359
        if 'num_threads' in p:
360
            num_threads = p['num_threads']
361
            if num_threads is None:
362
                num_threads = '?'
363
            msg = self.layout_stat['thread'].format(num_threads)
364
            ret = self.curse_add_line(msg)
365
        else:
366
            msg = self.layout_header['thread'].format('?')
367
            ret = self.curse_add_line(msg)
368
        return ret
369
370
    def _get_process_curses_nice(self, p, selected, args):
371
        """Return process nice curses"""
372
        if 'nice' in p:
373
            nice = p['nice']
374
            if nice is None:
375
                nice = '?'
376
            msg = self.layout_stat['nice'].format(nice)
377
            ret = self.curse_add_line(msg, decoration=self.get_nice_alert(nice))
378
        else:
379
            msg = self.layout_header['nice'].format('?')
380
            ret = self.curse_add_line(msg)
381
        return ret
382
383
    def _get_process_curses_status(self, p, selected, args):
384
        """Return process status curses"""
385
        if 'status' in p:
386
            status = p['status']
387
            msg = self.layout_stat['status'].format(status)
388
            if status == 'R':
389
                ret = self.curse_add_line(msg, decoration='STATUS')
390
            else:
391
                ret = self.curse_add_line(msg)
392
        else:
393
            msg = self.layout_header['status'].format('?')
394
            ret = self.curse_add_line(msg)
395
        return ret
396
397
    def _get_process_curses_io(self, p, selected, args, rorw='ior'):
398
        """Return process IO Read or Write curses"""
399
        if 'io_counters' in p and p['io_counters'][4] == 1 and p['time_since_update'] != 0:
400
            # Display rate if stats is available and io_tag ([4]) == 1
401
            # IO
402
            io = int(
403
                (p['io_counters'][0 if rorw == 'ior' else 1] - p['io_counters'][2 if rorw == 'ior' else 3])
404
                / p['time_since_update']
405
            )
406
            if io == 0:
407
                msg = self.layout_stat[rorw].format("0")
408
            else:
409
                msg = self.layout_stat[rorw].format(self.auto_unit(io, low_precision=True))
410
            ret = self.curse_add_line(msg, optional=True, additional=True)
411
        else:
412
            msg = self.layout_header[rorw].format("?")
413
            ret = self.curse_add_line(msg, optional=True, additional=True)
414
        return ret
415
416
    def _get_process_curses_io_read(self, p, selected, args):
417
        """Return process IO Read curses"""
418
        return self._get_process_curses_io(p, selected, args, rorw='ior')
419
420
    def _get_process_curses_io_write(self, p, selected, args):
421
        """Return process IO Write curses"""
422
        return self._get_process_curses_io(p, selected, args, rorw='iow')
423
424
    def get_process_curses_data(self, p, selected, args):
425
        """Get curses data to display for a process.
426
427
        - p is the process to display
428
        - selected is a tag=True if p is the selected process
429
        """
430
        ret = [self.curse_new_line()]
431
432
        # When a process is selected:
433
        # * display a special character at the beginning of the line
434
        # * underline the command name
435
        ret.append(
436
            self.curse_add_line(
437
                unicode_message('PROCESS_SELECTOR') if (selected and not args.disable_cursor) else ' ', 'SELECTED'
438
            )
439
        )
440
441
        # CPU
442
        ret.append(self._get_process_curses_cpu(p, selected, args))
443
444
        # MEM
445
        ret.append(self._get_process_curses_mem(p, selected, args))
446
        ret.append(self._get_process_curses_vms(p, selected, args))
447
        ret.append(self._get_process_curses_rss(p, selected, args))
448
449
        # PID
450
        if not self.args.programs:
451
            # Display processes, so the PID should be displayed
452
            msg = self.layout_stat['pid'].format(p['pid'], width=self.__max_pid_size())
453
        else:
454
            # Display programs, so the PID should not be displayed
455
            # Instead displays the number of children
456
            msg = self.layout_stat['pid'].format(
457
                len(p['childrens']) if 'childrens' in p else '_', width=self.__max_pid_size()
458
            )
459
        ret.append(self.curse_add_line(msg))
460
461
        # USER
462
        ret.append(self._get_process_curses_username(p, selected, args))
463
464
        # TIME+
465
        ret.append(self._get_process_curses_time(p, selected, args))
466
467
        # THREAD
468
        ret.append(self._get_process_curses_thread(p, selected, args))
469
470
        # NICE
471
        ret.append(self._get_process_curses_nice(p, selected, args))
472
473
        # STATUS
474
        ret.append(self._get_process_curses_status(p, selected, args))
475
476
        # IO read/write
477
        ret.append(self._get_process_curses_io_read(p, selected, args))
478
        ret.append(self._get_process_curses_io_write(p, selected, args))
479
480
        # Command line
481
        # If no command line for the process is available, fallback to the bare process name instead
482
        bare_process_name = p['name']
483
        cmdline = p.get('cmdline', '?')
484
485
        try:
486
            process_decoration = 'PROCESS_SELECTED' if (selected and not args.disable_cursor) else 'PROCESS'
487
            if cmdline:
488
                path, cmd, arguments = split_cmdline(bare_process_name, cmdline)
489
                # Manage end of line in arguments (see #1692)
490
                arguments = replace_special_chars(arguments)
491
                if os.path.isdir(path) and not args.process_short_name:
492
                    msg = self.layout_stat['command'].format(path) + os.sep
493
                    ret.append(self.curse_add_line(msg, splittable=True))
494
                    ret.append(self.curse_add_line(cmd, decoration=process_decoration, splittable=True))
495
                else:
496
                    msg = self.layout_stat['command'].format(cmd)
497
                    ret.append(self.curse_add_line(msg, decoration=process_decoration, splittable=True))
498
                if arguments:
499
                    msg = ' ' + self.layout_stat['command'].format(arguments)
500
                    ret.append(self.curse_add_line(msg, splittable=True))
501
            else:
502
                msg = self.layout_stat['name'].format(bare_process_name)
503
                ret.append(self.curse_add_line(msg, decoration=process_decoration, splittable=True))
504
        except (TypeError, UnicodeEncodeError) as e:
505
            # Avoid crash after running fine for several hours #1335
506
            logger.debug("Can not decode command line '{}' ({})".format(cmdline, e))
507
            ret.append(self.curse_add_line('', splittable=True))
508
509
        return ret
510
511
    def is_selected_process(self, args):
512
        return (
513
            args.is_standalone
514
            and self.args.enable_process_extended
515
            and args.cursor_position is not None
516
            and glances_processes.extended_process is not None
517
        )
518
519
    def msg_curse(self, args=None, max_width=None):
520
        """Return the dict to display in the curse interface."""
521
        # Init the return message
522
        ret = []
523
524
        # Only process if stats exist and display plugin enable...
525
        if not self.stats or args.disable_process:
526
            return ret
527
528
        # Compute the sort key
529
        process_sort_key = glances_processes.sort_key
530
        processes_list_sorted = self.__sort_stats(process_sort_key)
531
532
        # Display extended stats for selected process
533
        #############################################
534
535
        if self.is_selected_process(args):
536
            self.__msg_curse_extended_process(ret, glances_processes.extended_process)
537
538
        # Display others processes list
539
        ###############################
540
541
        # Header
542
        self.__msg_curse_header(ret, process_sort_key, args)
543
544
        # Process list
545
        # Loop over processes (sorted by the sort key previously compute)
546
        # This is a Glances bottleneck (see flame graph),
547
        # TODO: get_process_curses_data should be optimzed
548
        for position, process in enumerate(processes_list_sorted):
549
            ret.extend(self.get_process_curses_data(process, position == args.cursor_position, args))
550
551
        # A filter is set Display the stats summaries
552
        if glances_processes.process_filter is not None:
553
            if args.reset_minmax_tag:
554
                args.reset_minmax_tag = not args.reset_minmax_tag
555
                self.__mmm_reset()
556
            self.__msg_curse_sum(ret, args=args)
557
            self.__msg_curse_sum(ret, mmm='min', args=args)
558
            self.__msg_curse_sum(ret, mmm='max', args=args)
559
560
        # Return the message with decoration
561
        return ret
562
563
    def __msg_curse_extended_process(self, ret, p):
564
        """Get extended curses data for the selected process (see issue #2225)
565
566
        The result depends of the process type (process or thread).
567
568
        Input p is a dict with the following keys:
569
        {'status': 'S',
570
         'memory_info': {'rss': 466890752, 'vms': 3365347328, 'shared': 68153344,
571
                         'text': 659456, 'lib': 0, 'data': 774647808, 'dirty': 0],
572
         'pid': 4980,
573
         'io_counters': [165385216, 0, 165385216, 0, 1],
574
         'num_threads': 20,
575
         'nice': 0,
576
         'memory_percent': 5.958135664449709,
577
         'cpu_percent': 0.0,
578
         'gids': {'real': 1000, 'effective': 1000, 'saved': 1000},
579
         'cpu_times': {'user': 696.38, 'system': 119.98, 'children_user': 0.0,
580
                       'children_system': 0.0, 'iowait': 0.0),
581
         'name': 'WebExtensions',
582
         'key': 'pid',
583
         'time_since_update': 2.1997854709625244,
584
         'cmdline': ['/snap/firefox/2154/usr/lib/firefox/firefox', '-contentproc', '-childID', '...'],
585
         'username': 'nicolargo',
586
         'cpu_min': 0.0,
587
         'cpu_max': 7.0,
588
         'cpu_mean': 3.2}
589
        """
590
        if self.args.programs:
591
            self.__msg_curse_extended_process_program(ret, p)
592
        else:
593
            self.__msg_curse_extended_process_thread(ret, p)
594
595
    def __msg_curse_extended_process_program(self, ret, p):
596
        # Title
597
        msg = "Pinned program {} ('e' to unpin)".format(p['name'])
598
        ret.append(self.curse_add_line(msg, "TITLE"))
599
600
        ret.append(self.curse_new_line())
601
        ret.append(self.curse_new_line())
602
603
    def __msg_curse_extended_process_thread(self, ret, p):
604
        # Title
605
        ret.append(self.curse_add_line("Pinned thread ", "TITLE"))
606
        ret.append(self.curse_add_line(p['name'], "UNDERLINE"))
607
        ret.append(self.curse_add_line(" ('e' to unpin)"))
608
609
        # First line is CPU affinity
610
        ret.append(self.curse_new_line())
611
        ret.append(self.curse_add_line(' CPU Min/Max/Mean: '))
612
        msg = '{: >7.1f}{: >7.1f}{: >7.1f}%'.format(p['cpu_min'], p['cpu_max'], p['cpu_mean'])
613
        ret.append(self.curse_add_line(msg, decoration='INFO'))
614
        if 'cpu_affinity' in p and p['cpu_affinity'] is not None:
615
            ret.append(self.curse_add_line(' Affinity: '))
616
            ret.append(self.curse_add_line(str(len(p['cpu_affinity'])), decoration='INFO'))
617
            ret.append(self.curse_add_line(' cores', decoration='INFO'))
618
        if 'ionice' in p and p['ionice'] is not None and hasattr(p['ionice'], 'ioclass'):
619
            msg = ' IO nice: '
620
            k = 'Class is '
621
            v = p['ionice'].ioclass
622
            # Linux: The scheduling class. 0 for none, 1 for real time, 2 for best-effort, 3 for idle.
623
            # Windows: On Windows only ioclass is used and it can be set to 2 (normal), 1 (low) or 0 (very low).
624
            if WINDOWS:
625
                if v == 0:
626
                    msg += k + 'Very Low'
627
                elif v == 1:
628
                    msg += k + 'Low'
629
                elif v == 2:
630
                    msg += 'No specific I/O priority'
631
                else:
632
                    msg += k + str(v)
633
            else:
634
                if v == 0:
635
                    msg += 'No specific I/O priority'
636
                elif v == 1:
637
                    msg += k + 'Real Time'
638
                elif v == 2:
639
                    msg += k + 'Best Effort'
640
                elif v == 3:
641
                    msg += k + 'IDLE'
642
                else:
643
                    msg += k + str(v)
644
            #  value is a number which goes from 0 to 7.
645
            # The higher the value, the lower the I/O priority of the process.
646
            if hasattr(p['ionice'], 'value') and p['ionice'].value != 0:
647
                msg += ' (value %s/7)' % str(p['ionice'].value)
648
            ret.append(self.curse_add_line(msg, splittable=True))
649
650
        # Second line is memory info
651
        ret.append(self.curse_new_line())
652
        ret.append(self.curse_add_line(' MEM Min/Max/Mean: '))
653
        msg = '{: >7.1f}{: >7.1f}{: >7.1f}%'.format(p['memory_min'], p['memory_max'], p['memory_mean'])
654
        ret.append(self.curse_add_line(msg, decoration='INFO'))
655
        if 'memory_info' in p and p['memory_info'] is not None:
656
            ret.append(self.curse_add_line(' Memory info: '))
657
            for k, v in p['memory_info'].items():
658
                ret.append(
659
                    self.curse_add_line(
660
                        self.auto_unit(v, low_precision=False),
661
                        decoration='INFO',
662
                        splittable=True,
663
                    )
664
                )
665
                ret.append(self.curse_add_line(' ' + k + ' ', splittable=True))
666
            if 'memory_swap' in p and p['memory_swap'] is not None:
667
                ret.append(
668
                    self.curse_add_line(
669
                        self.auto_unit(p['memory_swap'], low_precision=False), decoration='INFO', splittable=True
670
                    )
671
                )
672
                ret.append(self.curse_add_line(' swap ', splittable=True))
673
674
        # Third line is for open files/network sessions
675
        ret.append(self.curse_new_line())
676
        ret.append(self.curse_add_line(' Open: '))
677
        for stat_prefix in ['num_threads', 'num_fds', 'num_handles', 'tcp', 'udp']:
678
            if stat_prefix in p and p[stat_prefix] is not None:
679
                ret.append(self.curse_add_line(str(p[stat_prefix]), decoration='INFO'))
680
                ret.append(self.curse_add_line(' {} '.format(stat_prefix.replace('num_', ''))))
681
682
        ret.append(self.curse_new_line())
683
        ret.append(self.curse_new_line())
684
685
    def __msg_curse_header(self, ret, process_sort_key, args=None):
686
        """Build the header and add it to the ret dict."""
687
        sort_style = 'SORT'
688
689
        if args.disable_irix and 0 < self.nb_log_core < 10:
690
            msg = self.layout_header['cpu'].format('CPU%/' + str(self.nb_log_core))
691
        elif args.disable_irix and self.nb_log_core != 0:
692
            msg = self.layout_header['cpu'].format('CPU%/C')
693
        else:
694
            msg = self.layout_header['cpu'].format('CPU%')
695
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'cpu_percent' else 'DEFAULT'))
696
        msg = self.layout_header['mem'].format('MEM%')
697
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'memory_percent' else 'DEFAULT'))
698
        msg = self.layout_header['virt'].format('VIRT')
699
        ret.append(self.curse_add_line(msg, optional=True))
700
        msg = self.layout_header['res'].format('RES')
701
        ret.append(self.curse_add_line(msg, optional=True))
702
        if not self.args.programs:
703
            msg = self.layout_header['pid'].format('PID', width=self.__max_pid_size())
704
        else:
705
            msg = self.layout_header['pid'].format('NPROCS', width=self.__max_pid_size())
706
        ret.append(self.curse_add_line(msg))
707
        msg = self.layout_header['user'].format('USER')
708
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'username' else 'DEFAULT'))
709
        msg = self.layout_header['time'].format('TIME+')
710
        ret.append(
711
            self.curse_add_line(msg, sort_style if process_sort_key == 'cpu_times' else 'DEFAULT', optional=True)
712
        )
713
        msg = self.layout_header['thread'].format('THR')
714
        ret.append(self.curse_add_line(msg))
715
        msg = self.layout_header['nice'].format('NI')
716
        ret.append(self.curse_add_line(msg))
717
        msg = self.layout_header['status'].format('S')
718
        ret.append(self.curse_add_line(msg))
719
        msg = self.layout_header['ior'].format('R/s')
720
        ret.append(
721
            self.curse_add_line(
722
                msg, sort_style if process_sort_key == 'io_counters' else 'DEFAULT', optional=True, additional=True
723
            )
724
        )
725
        msg = self.layout_header['iow'].format('W/s')
726
        ret.append(
727
            self.curse_add_line(
728
                msg, sort_style if process_sort_key == 'io_counters' else 'DEFAULT', optional=True, additional=True
729
            )
730
        )
731
        if args.is_standalone and not args.disable_cursor:
732
            if self.args.programs:
733
                shortkey = "('k' to kill)"
734
            else:
735
                shortkey = "('e' to pin | 'k' to kill)"
736
        else:
737
            shortkey = ""
738
        msg = self.layout_header['command'].format("Programs" if self.args.programs else "Command", shortkey)
739
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'name' else 'DEFAULT'))
740
741
    def __msg_curse_sum(self, ret, sep_char='_', mmm=None, args=None):
742
        """
743
        Build the sum message (only when filter is on) and add it to the ret dict.
744
745
        :param ret: list of string where the message is added
746
        :param sep_char: define the line separation char
747
        :param mmm: display min, max, mean or current (if mmm=None)
748
        :param args: Glances args
749
        """
750
        ret.append(self.curse_new_line())
751
        if mmm is None:
752
            ret.append(self.curse_add_line(sep_char * 69))
753
            ret.append(self.curse_new_line())
754
        # CPU percent sum
755
        msg = self.layout_stat['cpu'].format(self.__sum_stats('cpu_percent', mmm=mmm))
756
        ret.append(self.curse_add_line(msg, decoration=self.__mmm_deco(mmm)))
757
        # MEM percent sum
758
        msg = self.layout_stat['mem'].format(self.__sum_stats('memory_percent', mmm=mmm))
759
        ret.append(self.curse_add_line(msg, decoration=self.__mmm_deco(mmm)))
760
        # VIRT and RES memory sum
761
        if (
762
            'memory_info' in self.stats[0]
763
            and self.stats[0]['memory_info'] is not None
764
            and self.stats[0]['memory_info'] != ''
765
        ):
766
            # VMS
767
            msg = self.layout_stat['virt'].format(
768
                self.auto_unit(self.__sum_stats('memory_info', sub_key='vms', mmm=mmm), low_precision=False)
769
            )
770
            ret.append(self.curse_add_line(msg, decoration=self.__mmm_deco(mmm), optional=True))
771
            # RSS
772
            msg = self.layout_stat['res'].format(
773
                self.auto_unit(self.__sum_stats('memory_info', sub_key='rss', mmm=mmm), low_precision=False)
774
            )
775
            ret.append(self.curse_add_line(msg, decoration=self.__mmm_deco(mmm), optional=True))
776
        else:
777
            msg = self.layout_header['virt'].format('')
778
            ret.append(self.curse_add_line(msg))
779
            msg = self.layout_header['res'].format('')
780
            ret.append(self.curse_add_line(msg))
781
        # PID
782
        msg = self.layout_header['pid'].format('', width=self.__max_pid_size())
783
        ret.append(self.curse_add_line(msg))
784
        # USER
785
        msg = self.layout_header['user'].format('')
786
        ret.append(self.curse_add_line(msg))
787
        # TIME+
788
        msg = self.layout_header['time'].format('')
789
        ret.append(self.curse_add_line(msg, optional=True))
790
        # THREAD
791
        msg = self.layout_header['thread'].format('')
792
        ret.append(self.curse_add_line(msg))
793
        # NICE
794
        msg = self.layout_header['nice'].format('')
795
        ret.append(self.curse_add_line(msg))
796
        # STATUS
797
        msg = self.layout_header['status'].format('')
798
        ret.append(self.curse_add_line(msg))
799
        # IO read/write
800
        if 'io_counters' in self.stats[0] and mmm is None:
801
            # IO read
802
            io_rs = int(
803
                (self.__sum_stats('io_counters', 0) - self.__sum_stats('io_counters', sub_key=2, mmm=mmm))
804
                / self.stats[0]['time_since_update']
805
            )
806
            if io_rs == 0:
807
                msg = self.layout_stat['ior'].format('0')
808
            else:
809
                msg = self.layout_stat['ior'].format(self.auto_unit(io_rs, low_precision=True))
810
            ret.append(self.curse_add_line(msg, decoration=self.__mmm_deco(mmm), optional=True, additional=True))
811
            # IO write
812
            io_ws = int(
813
                (self.__sum_stats('io_counters', 1) - self.__sum_stats('io_counters', sub_key=3, mmm=mmm))
814
                / self.stats[0]['time_since_update']
815
            )
816
            if io_ws == 0:
817
                msg = self.layout_stat['iow'].format('0')
818
            else:
819
                msg = self.layout_stat['iow'].format(self.auto_unit(io_ws, low_precision=True))
820
            ret.append(self.curse_add_line(msg, decoration=self.__mmm_deco(mmm), optional=True, additional=True))
821
        else:
822
            msg = self.layout_header['ior'].format('')
823
            ret.append(self.curse_add_line(msg, optional=True, additional=True))
824
            msg = self.layout_header['iow'].format('')
825
            ret.append(self.curse_add_line(msg, optional=True, additional=True))
826
        if mmm is None:
827
            msg = ' < {}'.format('current')
828
            ret.append(self.curse_add_line(msg, optional=True))
829
        else:
830
            msg = ' < {}'.format(mmm)
831
            ret.append(self.curse_add_line(msg, optional=True))
832
            msg = ' (\'M\' to reset)'
833
            ret.append(self.curse_add_line(msg, optional=True))
834
835
    def __mmm_deco(self, mmm):
836
        """Return the decoration string for the current mmm status."""
837
        if mmm is not None:
838
            return 'DEFAULT'
839
        else:
840
            return 'FILTER'
841
842
    def __mmm_reset(self):
843
        """Reset the MMM stats."""
844
        self.mmm_min = {}
845
        self.mmm_max = {}
846
847
    def __sum_stats(self, key, sub_key=None, mmm=None):
848
        """Return the sum of the stats value for the given key.
849
850
        :param sub_key: If sub_key is set, get the p[key][sub_key]
851
        :param mmm: display min, max, mean or current (if mmm=None)
852
        """
853
        # Compute stats summary
854
        ret = 0
855
        for p in self.stats:
856
            if key not in p:
857
                # Correct issue #1188
858
                continue
859
            if p[key] is None:
860
                # Correct https://github.com/nicolargo/glances/issues/1105#issuecomment-363553788
861
                continue
862
            if sub_key is None:
863
                ret += p[key]
864
            else:
865
                ret += p[key][sub_key]
866
867
        # Manage Min/Max/Mean
868
        mmm_key = self.__mmm_key(key, sub_key)
869
        if mmm == 'min':
870
            try:
871
                if self.mmm_min[mmm_key] > ret:
872
                    self.mmm_min[mmm_key] = ret
873
            except AttributeError:
874
                self.mmm_min = {}
875
                return 0
876
            except KeyError:
877
                self.mmm_min[mmm_key] = ret
878
            ret = self.mmm_min[mmm_key]
879
        elif mmm == 'max':
880
            try:
881
                if self.mmm_max[mmm_key] < ret:
882
                    self.mmm_max[mmm_key] = ret
883
            except AttributeError:
884
                self.mmm_max = {}
885
                return 0
886
            except KeyError:
887
                self.mmm_max[mmm_key] = ret
888
            ret = self.mmm_max[mmm_key]
889
890
        return ret
891
892
    def __mmm_key(self, key, sub_key):
893
        ret = key
894
        if sub_key is not None:
895
            ret += str(sub_key)
896
        return ret
897
898
    def __sort_stats(self, sorted_by=None):
899
        """Return the stats (dict) sorted by (sorted_by)."""
900
        return sort_stats(self.stats, sorted_by, reverse=glances_processes.sort_reverse)
901
902
    def __max_pid_size(self):
903
        """Return the maximum PID size in number of char."""
904
        if self.pid_max is not None:
905
            return len(str(self.pid_max))
906
        else:
907
            # By default return 5 (corresponding to 99999 PID number)
908
            return 5
909