Skip to content

Commit 912df26

Browse files
committed
Compare issue with Process.cpu_times() #1339
1 parent f220f22 commit 912df26

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Bugs corrected:
1616
* GPU plugin not exported to influxdb #1333
1717
* Crash after running fine for several hours #1335
1818
* Timezone listed doesn’t match system timezone, outputs wrong time #1337
19+
* Compare issue with Process.cpu_times() #1339
1920

2021
Others:
2122

glances/processes.py

+45-15
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,40 @@ def weighted(value):
375375
return -float('inf') if value is None else value
376376

377377

378+
def _sort_io_counters(process,
379+
sortedby='io_counters',
380+
sortedby_secondary='memory_percent'):
381+
"""Specific case for io_counters
382+
Sum of io_r + io_w"""
383+
return process[sortedby][0] - process[sortedby][2] + process[sortedby][1] - process[sortedby][3]
384+
385+
386+
def _sort_cpu_times(process,
387+
sortedby='cpu_times',
388+
sortedby_secondary='memory_percent'):
389+
""" Specific case for cpu_times
390+
Patch for "Sorting by process time works not as expected #1321"
391+
By default PsUtil only takes user time into account
392+
see (https://github.com/giampaolo/psutil/issues/1339)
393+
The following implementation takes user and system time into account"""
394+
return process[sortedby][0] + process[sortedby][1]
395+
396+
397+
def _sort_lambda(sortedby='cpu_percent',
398+
sortedby_secondary='memory_percent'):
399+
"""Return a sort lambda function for the sortedbykey"""
400+
ret = None
401+
if sortedby == 'io_counters':
402+
ret = _sort_io_counters
403+
elif sortedby == 'cpu_times':
404+
ret = _sort_cpu_times
405+
return ret
406+
407+
378408
def sort_stats(stats,
379409
sortedby='cpu_percent',
380410
sortedby_secondary='memory_percent',
381-
reverse=True,):
411+
reverse=True):
382412
"""Return the stats (dict) sorted by (sortedby).
383413
384414
Reverse the sort if reverse is True.
@@ -387,28 +417,28 @@ def sort_stats(stats,
387417
# No need to sort...
388418
return stats
389419

390-
if sortedby == 'io_counters':
391-
# Specific case for io_counters
392-
# Sum of io_r + io_w
420+
# Check if a specific sort should be done
421+
sort_lambda = _sort_lambda(sortedby=sortedby,
422+
sortedby_secondary=sortedby_secondary)
423+
424+
if sort_lambda is not None:
425+
# Specific sort
393426
try:
394-
# Sort process by IO rate (sum IO read + IO write)
395-
stats.sort(key=lambda process: process[sortedby][0] -
396-
process[sortedby][2] + process[sortedby][1] -
397-
process[sortedby][3],
398-
reverse=reverse)
427+
stats.sort(key=sort_lambda, reverse=reverse)
399428
except Exception:
400-
stats.sort(key=lambda x: (weighted(x['cpu_percent']),
401-
weighted(x[sortedby_secondary])),
429+
# If an error is detected, fallback to cpu_percent
430+
stats.sort(key=lambda process: (weighted(process['cpu_percent']),
431+
weighted(process[sortedby_secondary])),
402432
reverse=reverse)
403433
else:
404-
# Others sorts
434+
# Standard sort
405435
try:
406-
stats.sort(key=lambda x: (weighted(x[sortedby]),
407-
weighted(x[sortedby_secondary])),
436+
stats.sort(key=lambda process: (weighted(process[sortedby]),
437+
weighted(process[sortedby_secondary])),
408438
reverse=reverse)
409439
except (KeyError, TypeError):
410440
# Fallback to name
411-
stats.sort(key=lambda x: x['name'] if x['name'] is not None else '~',
441+
stats.sort(key=lambda process: process['name'] if process['name'] is not None else '~',
412442
reverse=False)
413443

414444
return stats

0 commit comments

Comments
 (0)