Test Failed
Push — master ( 183265...afa1da )
by Nicolas
03:15 queued 16s
created

PluginModel._get_process_curses_time()   A

Complexity

Conditions 5

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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