Test Failed
Push — develop ( df4b31...c89eff )
by Nicolas
03:00
created

GlancesProcesses.enable_extended()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 4
rs 10
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
import os
10
11
import psutil
12
13
from glances.filter import GlancesFilter, GlancesFilterList
14
from glances.globals import (
15
    BSD,
16
    LINUX,
17
    MACOS,
18
    WINDOWS,
19
    dictlist_first_key_value,
20
    list_of_namedtuple_to_list_of_dict,
21
    namedtuple_to_dict,
22
)
23
from glances.logger import logger
24
from glances.programs import processes_to_programs
25
from glances.timer import Timer, getTimeSinceLastUpdate
26
27
psutil_version_info = tuple([int(num) for num in psutil.__version__.split('.')])
28
29
# This constant defines the list of mandatory processes stats. Thoses stats can not be disabled by the user
30
mandatory_processes_stats_list = ['pid', 'name']
31
32
# This constant defines the list of available processes sort key
33
sort_processes_stats_list = ['cpu_percent', 'memory_percent', 'username', 'cpu_times', 'io_counters', 'name']
34
35
# Sort dictionary for human
36
sort_for_human = {
37
    'io_counters': 'disk IO',
38
    'cpu_percent': 'CPU consumption',
39
    'memory_percent': 'memory consumption',
40
    'cpu_times': 'process time',
41
    'username': 'user name',
42
    'name': 'processs name',
43
    None: 'None',
44
}
45
46
47
class GlancesProcesses:
48
    """Get processed stats using the psutil library."""
49
50
    def __init__(self, cache_timeout=60):
51
        """Init the class to collect stats about processes."""
52
        # Init the args, coming from the classes derived from GlancesMode
53
        # Should be set by the set_args method
54
        self.args = None
55
56
        # The internals caches will be cleaned each 'cache_timeout' seconds
57
        self.cache_timeout = cache_timeout
58
        # First iteration, no cache
59
        self.cache_timer = Timer(0)
60
61
        # Init the io_old dict used to compute the IO bitrate
62
        # key = pid
63
        # value = [ read_bytes_old, write_bytes_old ]
64
        self.io_old = {}
65
66
        # Init stats
67
        self.auto_sort = None
68
        self._sort_key = None
69
        # Default processes sort key is 'auto'
70
        # Can be overwrite from the configuration file (issue#1536) => See glances_processlist.py init
71
        self.set_sort_key('auto', auto=True)
72
        self.processlist = []
73
        self.reset_processcount()
74
75
        # Cache is a dict with key=pid and value = dict of cached value
76
        self.processlist_cache = {}
77
78
        # List of processes to focus on
79
        self._filter_focus = GlancesFilterList()
80
81
        # List of processes stats to export
82
        # Only process matching one of the filter will be exported
83
        self._filter_export = GlancesFilterList()
84
        self.processlist_export = []
85
86
        # Tag to enable/disable the processes stats (to reduce the Glances CPU consumption)
87
        # Default is to enable the processes stats
88
        self.disable_tag = False
89
90
        # Extended stats for top process is enable by default
91
        self.disable_extended_tag = False
92
        self.extended_process = None
93
94
        # Tests (and disable if not available) optionals features
95
        self._test_grab()
96
97
        # Maximum number of processes showed in the UI (None if no limit)
98
        self._max_processes = None
99
100
        # Process filter
101
        self._filter = GlancesFilter()
102
103
        # Whether or not to hide kernel threads
104
        self.no_kernel_threads = False
105
106
        # Store maximums values in a dict
107
        # Used in the UI to highlight the maximum value
108
        self._max_values_list = ('cpu_percent', 'memory_percent')
109
        # { 'cpu_percent': 0.0, 'memory_percent': 0.0 }
110
        self._max_values = {}
111
        self.reset_max_values()
112
113
        # Set the key's list be disabled in order to only display specific attribute in the process list
114
        self.disable_stats = []
115
116
    def _test_grab(self):
117
        """Test somes optionals features"""
118
        # Test if the system can grab io_counters
119
        try:
120
            p = psutil.Process()
121
            p.io_counters()
122
        except Exception as e:
123
            logger.warning(f'PsUtil can not grab processes io_counters ({e})')
124
            self.disable_io_counters = True
125
        else:
126
            logger.debug('PsUtil can grab processes io_counters')
127
            self.disable_io_counters = False
128
129
        # Test if the system can grab gids
130
        try:
131
            p = psutil.Process()
132
            p.gids()
133
        except Exception as e:
134
            logger.warning(f'PsUtil can not grab processes gids ({e})')
135
            self.disable_gids = True
136
        else:
137
            logger.debug('PsUtil can grab processes gids')
138
            self.disable_gids = False
139
140
    def set_args(self, args):
141
        """Set args."""
142
        self.args = args
143
144
        if self.args.process_focus is not None:
145
            logger.info(f"Focus process filter (--process-focus option) is set to: {self.args.process_focus}")
146
            self.process_focus = self.args.process_focus
147
148
    def reset_internal_cache(self):
149
        """Reset the internal cache."""
150
        self.cache_timer = Timer(0)
151
        self.processlist_cache = {}
152
        if hasattr(psutil.process_iter, 'cache_clear'):
153
            # Cache clear only available in PsUtil 6 or higher
154
            psutil.process_iter.cache_clear()
155
156
    def reset_processcount(self):
157
        """Reset the global process count"""
158
        self.processcount = {'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0, 'pid_max': None}
159
160
    def update_processcount(self, plist):
161
        """Update the global process count from the current processes list"""
162
        # Update the maximum process ID (pid) number
163
        self.processcount['pid_max'] = self.pid_max
164
        # For each key in the processcount dict
165
        # count the number of processes with the same status
166
        for k in list(self.processcount.keys()):
167
            self.processcount[k] = len(list(filter(lambda v: v.get('status', '?') is k, plist)))
0 ignored issues
show
introduced by
The variable k does not seem to be defined in case the for loop on line 166 is not entered. Are you sure this can never be the case?
Loading history...
168
        # Compute thread
169
        try:
170
            self.processcount['thread'] = sum(i['num_threads'] for i in plist if i['num_threads'] is not None)
171
        except KeyError:
172
            self.processcount['thread'] = None
173
        # Compute total
174
        self.processcount['total'] = len(plist)
175
176
    def enable(self):
177
        """Enable process stats."""
178
        self.disable_tag = False
179
        self.update()
180
181
    def disable(self):
182
        """Disable process stats."""
183
        self.disable_tag = True
184
185
    def enable_extended(self):
186
        """Enable extended process stats."""
187
        self.disable_extended_tag = False
188
        self.update()
189
190
    def disable_extended(self):
191
        """Disable extended process stats."""
192
        self.disable_extended_tag = True
193
194
    @property
195
    def pid_max(self):
196
        """
197
        Get the maximum PID value.
198
199
        On Linux, the value is read from the `/proc/sys/kernel/pid_max` file.
200
201
        From `man 5 proc`:
202
        The default value for this file, 32768, results in the same range of
203
        PIDs as on earlier kernels. On 32-bit platforms, 32768 is the maximum
204
        value for pid_max. On 64-bit systems, pid_max can be set to any value
205
        up to 2^22 (PID_MAX_LIMIT, approximately 4 million).
206
207
        If the file is unreadable or not available for whatever reason,
208
        returns None.
209
210
        Some other OSes:
211
        - On FreeBSD and macOS the maximum is 99999.
212
        - On OpenBSD >= 6.0 the maximum is 99999 (was 32766).
213
        - On NetBSD the maximum is 30000.
214
215
        :returns: int or None
216
        """
217
        if LINUX:
218
            # XXX: waiting for https://github.com/giampaolo/psutil/issues/720
219
            try:
220
                with open('/proc/sys/kernel/pid_max', 'rb') as f:
221
                    return int(f.read())
222
            except OSError:
223
                return None
224
        else:
225
            return None
226
227
    @property
228
    def processes_count(self):
229
        """Get the current number of processes showed in the UI."""
230
        return min(self._max_processes - 2, glances_processes.processcount['total'] - 1)
231
232
    @property
233
    def max_processes(self):
234
        """Get the maximum number of processes showed in the UI."""
235
        return self._max_processes
236
237
    @max_processes.setter
238
    def max_processes(self, value):
239
        """Set the maximum number of processes showed in the UI."""
240
        self._max_processes = value
241
242
    @property
243
    def disable_stats(self):
244
        """Set disable_stats list"""
245
        return self._disable_stats
246
247
    @disable_stats.setter
248
    def disable_stats(self, stats_list):
249
        """Set disable_stats list"""
250
        self._disable_stats = [i for i in stats_list if i not in mandatory_processes_stats_list]
251
252
    @property
253
    def process_filter_input(self):
254
        """Get the process filter (given by the user)."""
255
        return self._filter.filter_input
256
257
    @property
258
    def process_filter(self):
259
        """Get the process filter (current apply filter)."""
260
        return self._filter.filter
261
262
    @process_filter.setter
263
    def process_filter(self, value):
264
        """Set the process filter."""
265
        self._filter.filter = value
266
267
    @property
268
    def process_filter_key(self):
269
        """Get the process filter key."""
270
        return self._filter.filter_key
271
272
    @property
273
    def process_filter_re(self):
274
        """Get the process regular expression compiled."""
275
        return self._filter.filter_re
276
277
    # Process focus filter
278
    # List of Glances filter
279
280
    @property
281
    def process_focus(self):
282
        """Get the focus process filter."""
283
        return self._filter_focus.filter
284
285
    @process_focus.setter
286
    def process_focus(self, value):
287
        """Set the focus process filter list."""
288
        self._filter_focus.filter = value
289
290
    # Export filter
291
    # List of Glances filter
292
293
    @property
294
    def export_process_filter(self):
295
        """Get the export process filter (current export process filter list)."""
296
        return self._filter_export.filter
297
298
    @export_process_filter.setter
299
    def export_process_filter(self, value):
300
        """Set the export process filter list."""
301
        self._filter_export.filter = value
302
303
    # Kernel threads
304
305
    def disable_kernel_threads(self):
306
        """Ignore kernel threads in process list."""
307
        self.no_kernel_threads = True
308
309
    @property
310
    def sort_reverse(self):
311
        """Return True to sort processes in reverse 'key' order, False instead."""
312
        if self.sort_key == 'name' or self.sort_key == 'username':
313
            return False
314
315
        return True
316
317
    def max_values(self):
318
        """Return the max values dict."""
319
        return self._max_values
320
321
    def get_max_values(self, key):
322
        """Get the maximum values of the given stat (key)."""
323
        return self._max_values[key]
324
325
    def set_max_values(self, key, value):
326
        """Set the maximum value for a specific stat (key)."""
327
        self._max_values[key] = value
328
329
    def reset_max_values(self):
330
        """Reset the maximum values dict."""
331
        self._max_values = {}
332
        for k in self._max_values_list:
333
            self._max_values[k] = 0.0
334
335
    def set_extended_stats(self, proc):
336
        """Set the extended stats for the given PID."""
337
        # - cpu_affinity (Linux, Windows, FreeBSD)
338
        # - ionice (Linux and Windows > Vista)
339
        # - num_ctx_switches (not available on Illumos/Solaris)
340
        # - num_fds (Unix-like)
341
        # - num_handles (Windows)
342
        # - memory_maps (only swap, Linux)
343
        #   https://www.cyberciti.biz/faq/linux-which-process-is-using-swap/
344
        # - connections (TCP and UDP)
345
        # - CPU min/max/mean
346
347
        # Set the extended stats list (OS dependent)
348
        extended_stats = ['cpu_affinity', 'ionice', 'num_ctx_switches']
349
        if LINUX:
350
            # num_fds only available on Unix system (see issue #1351)
351
            extended_stats += ['num_fds']
352
        if WINDOWS:
353
            extended_stats += ['num_handles']
354
355
        ret = {}
356
        try:
357
            logger.debug('Grab extended stats for process {}'.format(proc['pid']))
358
359
            # Get PID of the selected process
360
            selected_process = psutil.Process(proc['pid'])
361
362
            # Get the extended stats for the selected process
363
            ret = selected_process.as_dict(attrs=extended_stats, ad_value=None)
364
365
            # Get memory swap for the selected process (Linux Only)
366
            ret['memory_swap'] = self.__get_extended_memory_swap(selected_process)
367
368
            # Get number of TCP and UDP network connections for the selected process
369
            ret['tcp'], ret['udp'] = self.__get_extended_connections(selected_process)
370
        except (psutil.NoSuchProcess, ValueError, AttributeError) as e:
371
            logger.error(f'Can not grab extended stats ({e})')
372
            self.extended_process = None
373
            ret['extended_stats'] = False
374
        else:
375
            # Compute CPU and MEM min/max/mean
376
            # Merge the returned dict with the current on
377
            ret.update(self.__get_min_max_mean(proc))
378
            self.extended_process = ret
379
            ret['extended_stats'] = True
380
        return namedtuple_to_dict(ret)
381
382
    def get_extended_stats(self):
383
        """Return the extended stats.
384
385
        Return the process stat when extended_stats = True
386
        """
387
        for p in self.processlist:
388
            if p.get('extended_stats'):
389
                return p
390
        return None
391
392
    def __get_min_max_mean(self, proc, prefix=['cpu', 'memory']):
393
        """Return the min/max/mean for the given process"""
394
        ret = {}
395
        for stat_prefix in prefix:
396
            min_key = stat_prefix + '_min'
397
            max_key = stat_prefix + '_max'
398
            mean_sum_key = stat_prefix + '_mean_sum'
399
            mean_counter_key = stat_prefix + '_mean_counter'
400
            if min_key not in self.extended_process:
401
                ret[min_key] = proc[stat_prefix + '_percent']
402
            else:
403
                ret[min_key] = min(proc[stat_prefix + '_percent'], self.extended_process[min_key])
404
            if max_key not in self.extended_process:
405
                ret[max_key] = proc[stat_prefix + '_percent']
406
            else:
407
                ret[max_key] = max(proc[stat_prefix + '_percent'], self.extended_process[max_key])
408
            if mean_sum_key not in self.extended_process:
409
                ret[mean_sum_key] = proc[stat_prefix + '_percent']
410
            else:
411
                ret[mean_sum_key] = self.extended_process[mean_sum_key] + proc[stat_prefix + '_percent']
412
            if mean_counter_key not in self.extended_process:
413
                ret[mean_counter_key] = 1
414
            else:
415
                ret[mean_counter_key] = self.extended_process[mean_counter_key] + 1
416
            ret[stat_prefix + '_mean'] = ret[mean_sum_key] / ret[mean_counter_key]
417
        return ret
418
419
    def __get_extended_memory_swap(self, process):
420
        """Return the memory swap for the given process"""
421
        if not LINUX:
422
            return None
423
        try:
424
            memory_swap = sum([v.swap for v in process.memory_maps()])
425
        except (psutil.NoSuchProcess, KeyError):
426
            # (KeyError catch for issue #1551)
427
            pass
428
        except (psutil.AccessDenied, NotImplementedError):
429
            # NotImplementedError: /proc/${PID}/smaps file doesn't exist
430
            # on kernel < 2.6.14 or CONFIG_MMU kernel configuration option
431
            # is not enabled (see psutil #533/glances #413).
432
            memory_swap = None
433
        return memory_swap
434
435
    def __get_extended_connections(self, process):
436
        """Return a tuple with (tcp, udp) connections count
437
        The code is compliant with both PsUtil<6 and Psutil>=6
438
        """
439
        try:
440
            # Hack for issue #2754 (PsUtil 6+)
441
            if psutil_version_info[0] >= 6:
442
                tcp = len(process.net_connections(kind="tcp"))
443
                udp = len(process.net_connections(kind="udp"))
444
            else:
445
                tcp = len(process.connections(kind="tcp"))
446
                udp = len(process.connections(kind="udp"))
447
        except (psutil.AccessDenied, psutil.NoSuchProcess):
448
            # Manage issue1283 (psutil.AccessDenied)
449
            tcp = None
450
            udp = None
451
        return tcp, udp
452
453
    def is_selected_extended_process(self, position):
454
        """Return True if the process is the selected one for extended stats."""
455
        return (
456
            hasattr(self.args, 'programs')
457
            and not self.args.programs
458
            and hasattr(self.args, 'enable_process_extended')
459
            and self.args.enable_process_extended
460
            and not self.disable_extended_tag
461
            and hasattr(self.args, 'cursor_position')
462
            and position == self.args.cursor_position
463
            and not self.args.disable_cursor
464
        )
465
466
    def build_process_list(self, sorted_attrs):
467
        # Build the processes stats list (it is why we need psutil>=5.3.0) (see issue #2755)
468
        processlist = list(
469
            filter(
470
                lambda p: not (BSD and p.info['name'] == 'idle')
471
                and not (WINDOWS and p.info['name'] == 'System Idle Process')
472
                and not (MACOS and p.info['name'] == 'kernel_task')
473
                and not (self.no_kernel_threads and LINUX and p.info['gids'].real == 0),
474
                psutil.process_iter(attrs=sorted_attrs, ad_value=None),
475
            )
476
        )
477
478
        # Only get the info key
479
        # PsUtil 6+ no longer check PID reused #2755 so use is_running in the loop
480
        # Note: not sure it is realy needed but CPU consumption look the same with or without it
481
        processlist = [p.info for p in processlist if p.is_running()]
482
483
        # Sort the processes list by the current sort_key
484
        return sort_stats(processlist, sorted_by=self.sort_key, reverse=True)
485
486
    def get_sorted_attrs(self):
487
        defaults = ['cpu_percent', 'cpu_times', 'memory_percent', 'name', 'status', 'num_threads']
488
        optional = ['io_counters'] if not self.disable_io_counters else []
489
490
        return defaults + optional
491
492
    def get_displayed_attr(self):
493
        defaults = ['memory_info', 'nice', 'pid']
494
        optional = ['gids'] if not self.disable_gids else []
495
496
        return defaults + optional
497
498
    def get_cached_attrs(self):
499
        return ['cmdline', 'username']
500
501
    def maybe_add_cached_attrs(self, sorted_attrs, cached_attrs):
502
        # Some stats are not sort key
503
        # An optimisation can be done be only grabbed displayed_attr
504
        # for displayed processes (but only in standalone mode...)
505
        sorted_attrs.extend(self.get_displayed_attr())
506
        # Some stats are cached (not necessary to be refreshed every time)
507
        if self.cache_timer.finished():
508
            sorted_attrs += cached_attrs
509
            self.cache_timer.set(self.cache_timeout)
510
            self.cache_timer.reset()
511
            is_cached = False
512
        else:
513
            is_cached = True
514
515
        return is_cached, sorted_attrs
516
517
    def get_pid_time_and_status(self, time_since_update, proc):
518
        # PID is the key
519
        proc['key'] = 'pid'
520
521
        # Time since last update (for disk_io rate computation)
522
        proc['time_since_update'] = time_since_update
523
524
        # Process status (only keep the first char)
525
        proc['status'] = str(proc.get('status', '?'))[:1].upper()
526
527
        return proc
528
529
    def get_io_counters(self, proc):
530
        # procstat['io_counters'] is a list:
531
        # [read_bytes, write_bytes, read_bytes_old, write_bytes_old, io_tag]
532
        # If io_tag = 0 > Access denied or first time (display "?")
533
        # If io_tag = 1 > No access denied (display the IO rate)
534
        if 'io_counters' in proc and proc['io_counters'] is not None:
535
            io_new = [proc['io_counters'][2], proc['io_counters'][3]]
536
            # For IO rate computation
537
            # Append saved IO r/w bytes
538
            try:
539
                proc['io_counters'] = io_new + self.io_old[proc['pid']]
540
                io_tag = 1
541
            except KeyError:
542
                proc['io_counters'] = io_new + [0, 0]
543
                io_tag = 0
544
            # then save the IO r/w bytes
545
            self.io_old[proc['pid']] = io_new
546
        else:
547
            proc['io_counters'] = [0, 0] + [0, 0]
548
            io_tag = 0
549
        # Append the IO tag (for display)
550
        proc['io_counters'] += [io_tag]
551
552
        return proc
553
554
    def maybe_add_cached_stats(self, is_cached, cached_attrs, proc):
555
        if is_cached:
556
            # Grab cached values (in case of a new incoming process)
557
            if proc['pid'] not in self.processlist_cache:
558
                try:
559
                    self.processlist_cache[proc['pid']] = psutil.Process(pid=proc['pid']).as_dict(
560
                        attrs=cached_attrs, ad_value=None
561
                    )
562
                except psutil.NoSuchProcess:
563
                    pass
564
            # Add cached value to current stat
565
            try:
566
                proc.update(self.processlist_cache[proc['pid']])
567
            except KeyError:
568
                pass
569
        else:
570
            # Save values to cache
571
            try:
572
                self.processlist_cache[proc['pid']] = {cached: proc[cached] for cached in cached_attrs}
573
            except KeyError:
574
                pass
575
576
        return proc
577
578
    def update(self):
579
        """Update the processes stats."""
580
        # Init new processes stats
581
        processlist = []
582
583
        # Do not process if disable tag is set
584
        if self.disable_tag:
585
            return processlist
586
587
        # Time since last update (for disk_io rate computation)
588
        time_since_update = getTimeSinceLastUpdate('process_disk')
589
590
        # Grab standard stats
591
        #####################
592
        sorted_attrs = self.get_sorted_attrs()
593
594
        # The following attributes are cached and only retrieve every self.cache_timeout seconds
595
        # Warning: 'name' can not be cached because it is used for filtering
596
        cached_attrs = self.get_cached_attrs()
597
598
        is_cached, sorted_attrs = self.maybe_add_cached_attrs(sorted_attrs, cached_attrs)
599
600
        # Remove attributes set by the user in the config file (see #1524)
601
        sorted_attrs = [i for i in sorted_attrs if i not in self.disable_stats]
602
        processlist = self.build_process_list(sorted_attrs)
603
604
        # Update the processcount
605
        self.update_processcount(processlist)
606
607
        # Loop over processes and :
608
        # - add extended stats for selected process
609
        # - add metadata
610
        for position, proc in enumerate(processlist):
611
            # Extended stats
612
            ################
613
614
            # Get the selected process when the 'e' key is pressed
615
            if self.is_selected_extended_process(position):
616
                self.extended_process = proc
617
618
            # Grab extended stats only for the selected process (see issue #2225)
619
            if self.extended_process is not None and proc['pid'] == self.extended_process['pid']:
620
                proc.update(self.set_extended_stats(self.extended_process))
621
                self.extended_process = namedtuple_to_dict(proc)
622
623
            # Meta data
624
            ###########
625
            proc = self.get_pid_time_and_status(time_since_update, proc)
626
627
            # Process IO
628
            proc = self.get_io_counters(proc)
629
630
            # Manage cached information
631
            proc = self.maybe_add_cached_stats(is_cached, cached_attrs, proc)
632
633
        # Remove non running process from the cache (avoid issue #2976)
634
        self.remove_non_running_procs(processlist)
635
636
        # Filter and transform process export list
637
        self.processlist_export = self.update_export_list(processlist)
638
639
        # Filter and transform process list
640
        processlist = self.update_list(processlist)
641
642
        # Compute the maximum value for keys in self._max_values_list: CPU, MEM
643
        # Useful to highlight the processes with maximum values
644
        self.compute_max_value(processlist)
645
646
        # Update the stats
647
        self.processlist = processlist
648
649
        return self.processlist
650
651
    def compute_max_value(self, processlist):
652
        for k in [i for i in self._max_values_list if i not in self.disable_stats]:
653
            values_list = [i[k] for i in processlist if i[k] is not None]
654
            if values_list:
655
                self.set_max_values(k, max(values_list))
656
657
    def remove_non_running_procs(self, processlist):
658
        pids_running = [p['pid'] for p in processlist]
659
        pids_cached = list(self.processlist_cache.keys()).copy()
660
        for pid in pids_cached:
661
            if pid not in pids_running:
662
                self.processlist_cache.pop(pid, None)
663
664
    def update_list(self, processlist):
665
        """Return the process list after filtering and transformation (namedtuple to dict)."""
666
        if self._filter_focus.filter is not None and self._filter_focus.filter != []:
667
            ret = list(filter(lambda p: self._filter_focus.is_filtered(p), processlist))
668
            return list_of_namedtuple_to_list_of_dict(ret)
669
        if self._filter.filter is None:
670
            return list_of_namedtuple_to_list_of_dict(processlist)
671
        ret = list(filter(lambda p: self._filter.is_filtered(p), processlist))
672
        return list_of_namedtuple_to_list_of_dict(ret)
673
674
    def update_export_list(self, processlist):
675
        """Return the process export list after filtering and transformation (namedtuple to dict)."""
676
        if self._filter_export.filter == []:
677
            return []
678
        ret = list(filter(lambda p: self._filter_export.is_filtered(p), processlist))
679
        return list_of_namedtuple_to_list_of_dict(ret)
680
681
    def get_count(self):
682
        """Get the number of processes."""
683
        return self.processcount
684
685
    def get_list(self, sorted_by=None, as_programs=False):
686
        """Get the processlist.
687
        By default, return the list of threads.
688
        If as_programs is True, return the list of programs."""
689
        if as_programs:
690
            return processes_to_programs(self.processlist)
691
        return self.processlist
692
693
    def get_export(self):
694
        """Return the processlist for export."""
695
        return self.processlist_export
696
697
    def get_stats(self, pid):
698
        """Get stats for the given pid."""
699
        return dictlist_first_key_value(self.processlist, 'pid', pid)
700
701
    @property
702
    def sort_key(self):
703
        """Get the current sort key."""
704
        return self._sort_key
705
706
    def set_sort_key(self, key, auto=True):
707
        """Set the current sort key."""
708
        if key == 'auto':
709
            self.auto_sort = True
710
            self._sort_key = 'cpu_percent'
711
        else:
712
            self.auto_sort = auto
713
            self._sort_key = key
714
715
    def nice_decrease(self, pid):
716
        """Decrease nice level
717
        On UNIX this is a number which usually goes from -20 to 20.
718
        The higher the nice value, the lower the priority of the process."""
719
        p = psutil.Process(pid)
720
        try:
721
            p.nice(p.nice() - 1)
722
            logger.info(f'Set nice level of process {pid} to {p.nice()} (higher the priority)')
723
        except psutil.AccessDenied:
724
            logger.warning(f'Can not decrease (higher the priority) the nice level of process {pid} (access denied)')
725
726
    def nice_increase(self, pid):
727
        """Increase nice level
728
        On UNIX this is a number which usually goes from -20 to 20.
729
        The higher the nice value, the lower the priority of the process."""
730
        p = psutil.Process(pid)
731
        try:
732
            p.nice(p.nice() + 1)
733
            logger.info(f'Set nice level of process {pid} to {p.nice()} (lower the priority)')
734
        except psutil.AccessDenied:
735
            logger.warning(f'Can not increase (lower the priority) the nice level of process {pid} (access denied)')
736
737
    def kill(self, pid, timeout=3):
738
        """Kill process with pid"""
739
        assert pid != os.getpid(), "Glances can kill itself..."
740
        p = psutil.Process(pid)
741
        logger.debug(f'Send kill signal to process: {p}')
742
        p.kill()
743
        return p.wait(timeout)
744
745
746
def weighted(value):
747
    """Manage None value in dict value."""
748
    return -float('inf') if value is None else value
749
750
751
def sort_by_these_keys(first, second):
752
    return lambda process: (weighted(process.get(first)), weighted(process.get(second)))
753
754
755
def _sort_io_counters(process, sorted_by='io_counters', sorted_by_secondary='memory_percent'):
756
    """Specific case for io_counters
757
758
    :return: Sum of io_r + io_w
759
    """
760
    return process[sorted_by][0] - process[sorted_by][2] + process[sorted_by][1] - process[sorted_by][3]
761
762
763
def _sort_cpu_times(process, sorted_by='cpu_times', sorted_by_secondary='memory_percent'):
764
    """Specific case for cpu_times
765
766
    Patch for "Sorting by process time works not as expected #1321"
767
    By default PsUtil only takes user time into account
768
    see (https://github.com/giampaolo/psutil/issues/1339)
769
    The following implementation takes user and system time into account
770
    """
771
    return process[sorted_by][0] + process[sorted_by][1]
772
773
774
def _sort_lambda(sorted_by='cpu_percent', sorted_by_secondary='memory_percent'):
775
    """Return a sort lambda function for the sorted_by key"""
776
    return {'io_counters': _sort_io_counters, 'cpu_times': _sort_cpu_times}.get(sorted_by, None)
777
778
779
def sort_stats(stats, sorted_by='cpu_percent', sorted_by_secondary='memory_percent', reverse=True):
780
    """Return the stats (dict) sorted by (sorted_by).
781
    A secondary sort key should be specified.
782
783
    Reverse the sort if reverse is True.
784
    """
785
    if sorted_by is None and sorted_by_secondary is None:
786
        # No need to sort...
787
        return stats
788
789
    # Check if a specific sort should be done
790
    sort_lambda = _sort_lambda(sorted_by=sorted_by, sorted_by_secondary=sorted_by_secondary)
791
792
    if sort_lambda is not None:
793
        # Specific sort
794
        try:
795
            stats = sorted(stats, key=sort_lambda, reverse=reverse)
796
        except Exception:
797
            # If an error is detected, fallback to cpu_percent
798
            stats = sorted(stats, key=sort_by_these_keys('cpu_percent', sorted_by_secondary), reverse=reverse)
799
    else:
800
        # Standard sort
801
        try:
802
            stats = sorted(stats, key=sort_by_these_keys(sorted_by, sorted_by_secondary), reverse=reverse)
803
        except (KeyError, TypeError):
804
            # Fallback to name
805
            stats.sort(key=lambda process: process['name'] if process['name'] is not None else '~', reverse=False)
806
807
    return stats
808
809
810
glances_processes = GlancesProcesses()
811