GlancesProcesses.set_extended_stats()   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 46
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nop 2
dl 0
loc 46
rs 8.9093
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
        return [p.info for p in processlist if p.is_running()]
482
483
    def get_sorted_attrs(self):
484
        defaults = ['cpu_percent', 'cpu_times', 'memory_percent', 'name', 'status', 'num_threads']
485
        optional = ['io_counters'] if not self.disable_io_counters else []
486
487
        return defaults + optional
488
489
    def get_displayed_attr(self):
490
        defaults = ['memory_info', 'nice', 'pid']
491
        optional = ['gids'] if not self.disable_gids else []
492
493
        return defaults + optional
494
495
    def get_cached_attrs(self):
496
        return ['cmdline', 'username']
497
498
    def maybe_add_cached_attrs(self, sorted_attrs, cached_attrs):
499
        # Some stats are not sort key
500
        # An optimisation can be done be only grabbed displayed_attr
501
        # for displayed processes (but only in standalone mode...)
502
        sorted_attrs.extend(self.get_displayed_attr())
503
        # Some stats are cached (not necessary to be refreshed every time)
504
        if self.cache_timer.finished():
505
            sorted_attrs += cached_attrs
506
            self.cache_timer.set(self.cache_timeout)
507
            self.cache_timer.reset()
508
            is_cached = False
509
        else:
510
            is_cached = True
511
512
        return is_cached, sorted_attrs
513
514
    def get_pid_time_and_status(self, time_since_update, proc):
515
        # PID is the key
516
        proc['key'] = 'pid'
517
518
        # Time since last update (for disk_io rate computation)
519
        proc['time_since_update'] = time_since_update
520
521
        # Process status (only keep the first char)
522
        proc['status'] = str(proc.get('status', '?'))[:1].upper()
523
524
        return proc
525
526
    def get_io_counters(self, proc):
527
        # procstat['io_counters'] is a list:
528
        # [read_bytes, write_bytes, read_bytes_old, write_bytes_old, io_tag]
529
        # If io_tag = 0 > Access denied or first time (display "?")
530
        # If io_tag = 1 > No access denied (display the IO rate)
531
        if 'io_counters' in proc and proc['io_counters'] is not None:
532
            io_new = [proc['io_counters'][2], proc['io_counters'][3]]
533
            # For IO rate computation
534
            # Append saved IO r/w bytes
535
            try:
536
                proc['io_counters'] = io_new + self.io_old[proc['pid']]
537
                io_tag = 1
538
            except KeyError:
539
                proc['io_counters'] = io_new + [0, 0]
540
                io_tag = 0
541
            # then save the IO r/w bytes
542
            self.io_old[proc['pid']] = io_new
543
        else:
544
            proc['io_counters'] = [0, 0] + [0, 0]
545
            io_tag = 0
546
        # Append the IO tag (for display)
547
        proc['io_counters'] += [io_tag]
548
549
        return proc
550
551
    def maybe_add_cached_stats(self, is_cached, cached_attrs, proc):
552
        if is_cached:
553
            # Grab cached values (in case of a new incoming process)
554
            if proc['pid'] not in self.processlist_cache:
555
                try:
556
                    self.processlist_cache[proc['pid']] = psutil.Process(pid=proc['pid']).as_dict(
557
                        attrs=cached_attrs, ad_value=None
558
                    )
559
                except psutil.NoSuchProcess:
560
                    pass
561
            # Add cached value to current stat
562
            try:
563
                proc.update(self.processlist_cache[proc['pid']])
564
            except KeyError:
565
                pass
566
        else:
567
            # Save values to cache
568
            try:
569
                self.processlist_cache[proc['pid']] = {cached: proc[cached] for cached in cached_attrs}
570
            except KeyError:
571
                pass
572
573
        return proc
574
575
    def update(self):
576
        """Update the processes stats."""
577
        # Init new processes stats
578
        processlist = []
579
580
        # Do not process if disable tag is set
581
        if self.disable_tag:
582
            return processlist
583
584
        # Time since last update (for disk_io rate computation)
585
        time_since_update = getTimeSinceLastUpdate('process_disk')
586
587
        # Grab standard stats
588
        #####################
589
        sorted_attrs = self.get_sorted_attrs()
590
591
        # The following attributes are cached and only retrieve every self.cache_timeout seconds
592
        # Warning: 'name' can not be cached because it is used for filtering
593
        cached_attrs = self.get_cached_attrs()
594
595
        is_cached, sorted_attrs = self.maybe_add_cached_attrs(sorted_attrs, cached_attrs)
596
597
        # Remove attributes set by the user in the config file (see #1524)
598
        sorted_attrs = [i for i in sorted_attrs if i not in self.disable_stats]
599
600
        # Buid and sort the process list
601
        processlist = self.build_process_list(sorted_attrs)
602
603
        # Update the processcount
604
        self.update_processcount(processlist)
605
606
        # Loop over processes and :
607
        # - add extended stats for selected process
608
        # - add metadata
609
        for position, proc in enumerate(processlist):
610
            # Extended stats
611
            ################
612
613
            # Get the selected process when the 'e' key is pressed
614
            if self.is_selected_extended_process(position):
615
                self.extended_process = proc
616
617
            # Grab extended stats only for the selected process (see issue #2225)
618
            if self.extended_process is not None and proc['pid'] == self.extended_process['pid']:
619
                proc.update(self.set_extended_stats(self.extended_process))
620
                self.extended_process = namedtuple_to_dict(proc)
621
622
            # Meta data
623
            ###########
624
            proc = self.get_pid_time_and_status(time_since_update, proc)
625
626
            # Process IO
627
            proc = self.get_io_counters(proc)
628
629
            # Manage cached information
630
            proc = self.maybe_add_cached_stats(is_cached, cached_attrs, proc)
631
632
        # Remove non running process from the cache (avoid issue #2976)
633
        self.remove_non_running_procs(processlist)
634
635
        # Filter and transform process export list
636
        self.processlist_export = self.update_export_list(processlist)
637
638
        # Filter and transform process list
639
        processlist = self.update_list(processlist)
640
641
        # Compute the maximum value for keys in self._max_values_list: CPU, MEM
642
        # Useful to highlight the processes with maximum values
643
        self.compute_max_value(processlist)
644
645
        # Update the stats
646
        self.processlist = processlist
647
648
        return self.processlist
649
650
    def compute_max_value(self, processlist):
651
        for k in [i for i in self._max_values_list if i not in self.disable_stats]:
652
            values_list = [i[k] for i in processlist if i[k] is not None]
653
            if values_list:
654
                self.set_max_values(k, max(values_list))
655
656
    def remove_non_running_procs(self, processlist):
657
        pids_running = [p['pid'] for p in processlist]
658
        pids_cached = list(self.processlist_cache.keys()).copy()
659
        for pid in pids_cached:
660
            if pid not in pids_running:
661
                self.processlist_cache.pop(pid, None)
662
663
    def update_list(self, processlist):
664
        """Return the process list after filtering and transformation (namedtuple to dict)."""
665
        if self._filter_focus.filter is not None and self._filter_focus.filter != []:
666
            ret = list(filter(lambda p: self._filter_focus.is_filtered(p), processlist))
667
            return list_of_namedtuple_to_list_of_dict(ret)
668
        if self._filter.filter is None:
669
            return list_of_namedtuple_to_list_of_dict(processlist)
670
        ret = list(filter(lambda p: self._filter.is_filtered(p), processlist))
671
        return list_of_namedtuple_to_list_of_dict(ret)
672
673
    def update_export_list(self, processlist):
674
        """Return the process export list after filtering and transformation (namedtuple to dict)."""
675
        if self._filter_export.filter == []:
676
            return []
677
        ret = list(filter(lambda p: self._filter_export.is_filtered(p), processlist))
678
        return list_of_namedtuple_to_list_of_dict(ret)
679
680
    def get_count(self):
681
        """Get the number of processes."""
682
        return self.processcount
683
684
    def get_list(self, sorted=False, as_programs=False):
685
        """Get the processlist (sorted or not).
686
        By default, return the list of threads.
687
        If as_programs is True, return the list of programs."""
688
        if sorted:
689
            self.processlist = sort_stats(self.processlist, sorted_by=self.sort_key, reverse=self.sort_reverse)
690
        if as_programs:
691
            return processes_to_programs(self.processlist)
692
        return self.processlist
693
694
    def get_export(self):
695
        """Return the processlist for export."""
696
        return self.processlist_export
697
698
    def get_stats(self, pid):
699
        """Get stats for the given pid."""
700
        return dictlist_first_key_value(self.processlist, 'pid', pid)
701
702
    @property
703
    def sort_key(self):
704
        """Get the current sort key."""
705
        return self._sort_key
706
707
    def set_sort_key(self, key, auto=True):
708
        """Set the current sort key."""
709
        if key == 'auto':
710
            self.auto_sort = True
711
            self._sort_key = 'cpu_percent'
712
        else:
713
            self.auto_sort = auto
714
            self._sort_key = key
715
716
    def nice_decrease(self, pid):
717
        """Decrease nice level
718
        On UNIX this is a number which usually goes from -20 to 20.
719
        The higher the nice value, the lower the priority of the process."""
720
        p = psutil.Process(pid)
721
        try:
722
            p.nice(p.nice() - 1)
723
            logger.info(f'Set nice level of process {pid} to {p.nice()} (higher the priority)')
724
        except psutil.AccessDenied:
725
            logger.warning(f'Can not decrease (higher the priority) the nice level of process {pid} (access denied)')
726
727
    def nice_increase(self, pid):
728
        """Increase nice level
729
        On UNIX this is a number which usually goes from -20 to 20.
730
        The higher the nice value, the lower the priority of the process."""
731
        p = psutil.Process(pid)
732
        try:
733
            p.nice(p.nice() + 1)
734
            logger.info(f'Set nice level of process {pid} to {p.nice()} (lower the priority)')
735
        except psutil.AccessDenied:
736
            logger.warning(f'Can not increase (lower the priority) the nice level of process {pid} (access denied)')
737
738
    def kill(self, pid, timeout=3):
739
        """Kill process with pid"""
740
        assert pid != os.getpid(), "Glances can kill itself..."
741
        p = psutil.Process(pid)
742
        logger.debug(f'Send kill signal to process: {p}')
743
        p.kill()
744
        return p.wait(timeout)
745
746
747
def weighted(value):
748
    """Manage None value in dict value."""
749
    return -float('inf') if value is None else value
750
751
752
def sort_by_these_keys(first, second):
753
    return lambda process: (weighted(process.get(first)), weighted(process.get(second)))
754
755
756
def _sort_io_counters(process, sorted_by='io_counters', sorted_by_secondary='memory_percent'):
757
    """Specific case for io_counters
758
759
    :return: Sum of io_r + io_w
760
    """
761
    logger.info(f'*** Sort by cpu_times called {type(process[sorted_by])} {process[sorted_by]}')
762
    return process[sorted_by][0] - process[sorted_by][2] + process[sorted_by][1] - process[sorted_by][3]
763
764
765
def _sort_cpu_times(process, sorted_by='cpu_times', sorted_by_secondary='memory_percent'):
766
    """Specific case for cpu_times
767
768
    Patch for "Sorting by process time works not as expected #1321"
769
    By default PsUtil only takes user time into account
770
    see (https://github.com/giampaolo/psutil/issues/1339)
771
    The following implementation takes user and system time into account
772
    """
773
    return process[sorted_by]['user'] + process[sorted_by]['system']
774
775
776
def _sort_lambda(sorted_by='cpu_percent', sorted_by_secondary='memory_percent'):
777
    """Return a sort lambda function for the sorted_by key"""
778
    return {'io_counters': _sort_io_counters, 'cpu_times': _sort_cpu_times}.get(sorted_by, None)
779
780
781
def sort_stats(stats, sorted_by='cpu_percent', sorted_by_secondary='memory_percent', reverse=True):
782
    """Return the stats (dict) sorted by (sorted_by).
783
    A secondary sort key should be specified.
784
785
    Reverse the sort if reverse is True.
786
    """
787
    if sorted_by is None and sorted_by_secondary is None:
788
        # No need to sort...
789
        return stats
790
791
    # Check if a specific sort should be done
792
    sort_lambda = _sort_lambda(sorted_by=sorted_by, sorted_by_secondary=sorted_by_secondary)
793
794
    if sort_lambda is not None:
795
        # Specific sort
796
        try:
797
            stats = sorted(stats, key=sort_lambda, reverse=reverse)
798
        except Exception as e:
799
            # If an error is detected, fallback to cpu_percent
800
            logger.debug(f'Error while sorting by {sorted_by}, fallback to cpu_percent ({e})')
801
            stats = sorted(stats, key=sort_by_these_keys('cpu_percent', sorted_by_secondary), reverse=reverse)
802
    else:
803
        # Standard sort
804
        try:
805
            stats = sorted(stats, key=sort_by_these_keys(sorted_by, sorted_by_secondary), reverse=reverse)
806
        except (KeyError, TypeError) as e:
807
            # Fallback to name
808
            logger.debug(f'Error while sorting by {sorted_by}, fallback to name ({e})')
809
            stats.sort(key=lambda process: process['name'] if process['name'] is not None else '~', reverse=False)
810
811
    return stats
812
813
814
glances_processes = GlancesProcesses()
815
816
# End of file processes.py
817