Change Priority
The Snakeoil tab contains several obscure configuration options, one of them would be the Change Priority. This control is used to redefine how CPU(s) juggle running processes, an example of how to set this up is shown here:
This blog post will quickly run through how you can use this to tweak your Snakeoil machine.
Advertising
In theory you can just leave this setting blank. The defaults are fine most of the time, tweaking this merely sets a “minimum guarantee” on how the processes are executed. This can be a good thing (especially when you need to extract everything you could from a low speed Atom CPU). An example where you need to use this option is when you hear a occasional drop out in audio. Drop outs in audio happens because your CPU is caught up doing other things (e.g. performing a media scan), and did not have enough tim to put out enough data onto the audio buffer. Tweaking the priorities can prevent that from happening.
We will not be going into the inner details of operating system concepts, but just note the following:
- Snakeoil is a Real Time Operating System (RTOS). The process scheduler works in RT because the kernel is fully preemptive.
- Snakeoil Uses Deadline scheduler for I/O scheduling
If you want to know more about these concepts, Wikipedia provided some excellent background resources to explain Process Scheduling and I/O Scheduling. The RT kernel group also has a FAQ. Suffice to say Snakeoil is designed to run with minimal overheads and is always ready to execute a process if needed.
This tweak can be a double edged sword, any mis-configuration in the Priority settings may well be detrimental to the sound quality. i.e. if you set too many processes to RT, the CPU may get too busy again and miss the time critical step of filling up the audio buffer.
The idea of tweaking Priority is to find a right balance on how the processes are queued up and executed.
So how do we set priorities? First, we need to find out the processes that are running on your mahcine. To find this list, go to the Home Tab, and click the Top button. You will see a screen that looks like this:
top - 09:59:14 up 1:23, 0 users, load average: 0.09, 0.07, 0.06 Tasks: 99 total, 1 running, 98 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.2 us, 1.1 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem: 8297312 total, 190416 used, 8106896 free, 13112 buffers KiB Swap: 0 total, 0 used, 0 free. 136184 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 78 root 20 0 0 0 0 S 6.3 0.0 4:44.60 irq/17-xhci 1 root 20 0 4184 2232 1448 S 0.0 0.0 0:05.91 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root -2 0 0 0 0 S 0.0 0.0 0:00.33 ksoftirqd/0 4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:+ 6 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/u8+ 7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 posixcputm+ 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kclksetdel+
The list will be about 50 process long, but really you’d only need to concentrate on the first 10 or so. Two columns would be of interest to you, they are:
- 3rd and 4th column - PR and NI, short for priority and nice. The default priority level is usually 20, with a nice level of 0. We will only look at PR most of the time as NI is usually zero. Any process with a PR of rt means it is running in “real time". Snakeoil is a RTOS, so when you see rt you are assured the process is running at the highest possible priority possible (subject to your computer hardware’s limitations)
- 8th column - S, short for State. Usually it will either be in R (Running) or S (Suspended). If you see R, it means it’s a good thing because it means the CPU is actually executing the instructions. S is the more common state where it tells you the CPU is ready to execute, but is waiting on something else (usually I/O related). With computer audio players, most processes will end up in the S state, and is perfectly normal.
- 12th column - COMMAND. This is the command line used for the process, i.e. the name of the process. This is the field that’s required when configuring the Change Priority options in the Snakeoil tab.
Back to the Top output, you’d need to look at the first 10 processes for a span of about 60 seconds. Take note of the commands that pop up (check the PR and S columns as well). The usual processes to look out for will be anything USB (for your USB audio device), Ethernet (if you stream music from a NAS) and music player (mpd, squeezelite, etc).
These processes are just some example typical of the way I run my Snakeoil (Squeezelite only with music streamed from a NAS). Your list of active processes is likely to be different if you are playing music directly from your local SSD. Regardless of setup, all you need to do is to identify a short list of processes that dominate the 10 top list, and tweak them. Any process with a priority of rt can be ignored.
You are ready to start tweaking the priority once you have identified which processes to target. In our example, let’s say we have identified 3 process worth tweaking, they are:
- USB: under the command of irq/17-xhci
- Ethernet: under the command of irq/10-prp2
- Squeezelite: under the command of squeezelite
Click the Add Process button 3 times to add 3 entries. Each entry consist of the following fields:
- Process name: This is the command name of the process, e.g. irq/17 (now that / is a special character and you’d need to escape it with a \, so for irq/17 you enter it as irq\/17)
- Prio: This is the new priority level for the process. It’s a number from 1-99 (Higher number = higher priority)
- Remove: Click this button to remove the entry
Enter a unique part of process name (command column) in the process name field. i.e. don’t use text like irq because there are multiple IRQ processes running. Be more specific, like irq/17. The second field defines the priority level for the process (a number from 1 to 99, with 99 being realtime). Here is an example:Squeezelite and xhci are running in real time, while Ethernet is not, but still at a relatively high numer. Click the Save Changes button to apply the settings (these setting will remain active after a reboot).
Experiment by modifying the numbers and see if it changes the sound quality in any meaningful way. Remember this rule - a higher number isn’t always better, the idea is to strike a good scheulding policy, not run everything as needed, when needed.
That’s not all.
Modern software are multi-threaded. What you did above is merely setting the main process to realtime, in some instances that does practically nothing, case in point is the squeezelite software player. While the main process is running in real time, any child process (threads) may not be.
Not that there’s anything wrong with this, as the main process still runs in rt, but what’s more important, is to specify the output thread to be real time as well. You can do so with the special options in the software player, like so:The magic is in the -p argument.
-p <priority> Set real time priority of output thread (1-99)
What that option does is to set the output thread to real time priority1. The memory will be locked too which means it will not be swapped out (reducing latency to minimum).
Footnotes:
1. For Squeezelite -p option to work, you’d need firmware 0.1.4-prelease-20160515 or later. Check the forums for the latest version.