@@ -375,10 +375,40 @@ def weighted(value):
375
375
return - float ('inf' ) if value is None else value
376
376
377
377
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
+
378
408
def sort_stats (stats ,
379
409
sortedby = 'cpu_percent' ,
380
410
sortedby_secondary = 'memory_percent' ,
381
- reverse = True , ):
411
+ reverse = True ):
382
412
"""Return the stats (dict) sorted by (sortedby).
383
413
384
414
Reverse the sort if reverse is True.
@@ -387,28 +417,28 @@ def sort_stats(stats,
387
417
# No need to sort...
388
418
return stats
389
419
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
393
426
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 )
399
428
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 ])),
402
432
reverse = reverse )
403
433
else :
404
- # Others sorts
434
+ # Standard sort
405
435
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 ])),
408
438
reverse = reverse )
409
439
except (KeyError , TypeError ):
410
440
# 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 '~' ,
412
442
reverse = False )
413
443
414
444
return stats
0 commit comments