Low Priority Tasks Preempting High Priority
Hey @alfredchen, since some of the refactoring that you've done, I've been getting issues with low priority tasks preempting high. This is most evident when playing a game and a background backup initiates and causes stuttering on audio (at least -11 on pipewire), and game frame rates to drop below half.
It's not clear to me the best way to demonstrate, so I wrote a python script that tests the results of setting nice levels on processes running parallel. To my surprise, only tasks of nice -15 or below (to -20) appear to run first. Afterwards, it appears to be some sort of free for all where everything is running concurrently with nice tasks finishing before not nice.
Here's the code:
[REDACTED, please use latest code from new project: https://github.com/damentz/nice-bench]
And here's an example result on a 6 core i7-9850H with SMT turned off (to reduce total core count well below number of running tasks).
$ sudo python3 test-nice.py
Process 52855 running with nice level: -20
Process 52856 running with nice level: -19
Process 52857 running with nice level: -18
Process 52858 running with nice level: -17
Process 52859 running with nice level: -16
Process 52860 running with nice level: -15
Process 52861 running with nice level: -10
Process 52862 running with nice level: -5
Process 52863 running with nice level: 0
Process 52864 running with nice level: 5
Process 52865 running with nice level: 10
Process 52867 running with nice level: 16
Process 52866 running with nice level: 15
Process 52868 running with nice level: 17
Process 52869 running with nice level: 18
Process 52870 running with nice level: 19
Task completed with nice level -18 in 85.9176 seconds
Task completed with nice level -19 in 86.2456 seconds
Task completed with nice level -16 in 86.7041 seconds
Task completed with nice level -20 in 86.7869 seconds
Task completed with nice level -17 in 86.9846 seconds
Task completed with nice level -15 in 136.1135 seconds
Task completed with nice level -5 in 166.8674 seconds
Task completed with nice level 17 in 172.7494 seconds
Task completed with nice level -10 in 173.2906 seconds
Task completed with nice level 15 in 173.3222 seconds
Task completed with nice level 10 in 182.0230 seconds
Task completed with nice level 5 in 219.7501 seconds
Task completed with nice level 0 in 239.2033 seconds
Task completed with nice level 19 in 244.8352 seconds
Task completed with nice level 16 in 250.8314 seconds
Task completed with nice level 18 in 250.8146 seconds
All tasks completed.
I'm not entirely sure what to make of this. Nice of -16
and less all run in parallel amongst the 6 cores, but after they complete, we're seeing a nice of 17
beat a -10
!
Looking at top output, it all looks fine with CPU usage roughly ordered by nice level up until everything -16 and below finish. Then it looks like this with negative nice scattered with positive nice tasks as if priority doesn't matter:
Tasks: 435 total, 13 running, 418 sleeping, 0 stopped, 4 zombie
%Cpu(s): 66.4 us, 0.2 sy, 33.3 ni, 0.0 id, 0.0 wa, 0.2 hi, 0.0 si, 0.0 st
MiB Mem : 47527.1 total, 28445.3 free, 13362.5 used, 9536.1 buff/cache
MiB Swap: 9009.2 total, 9003.2 free, 6.0 used. 34164.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
53449 root 13 -16 18344 8948 2912 R 99.1 0.0 0:52.77 python3
53450 root 13 -15 18344 8948 2912 R 99.1 0.0 0:52.74 python3
53453 root 22 0 18344 8996 2960 R 93.5 0.0 0:15.30 python3
53455 root 27 10 18344 8996 2960 R 85.0 0.0 0:12.14 python3
53451 root 17 -10 18344 8996 2960 R 73.8 0.0 0:43.82 python3
53454 root 23 5 18344 8996 2960 R 71.0 0.0 0:12.55 python3
53452 root 17 -5 18344 8996 2960 R 24.3 0.0 0:16.75 python3
53460 root 29 19 18344 9000 2960 R 12.1 0.0 0:04.11 python3
53456 root 23 15 18344 8996 2960 R 8.4 0.0 0:04.15 python3
53457 root 29 16 18344 8996 2960 R 8.4 0.0 0:03.90 python3
53458 root 27 17 18344 8996 2960 R 8.4 0.0 0:03.89 python3
35113 steven 0 0 2747252 277164 110872 S 4.7 0.6 0:37.41 Isolated Web Co
53459 root 25 18 18344 8996 2960 R 4.7 0.0 0:03.43 python3
You can try the code by dropping it to a file and executing sudo <file name>
. By default it goes for 16 processes with a spread of nice levels. Highly recommend artifically reducing core numbers to test if your system goes beyond 8.
And disclaimer, I did use ChatGPT to help write the python code, hopefully it doesn't have obvious errors in invoking sub processes that are the actual reason for the issue.