CPU isolation with cgroups

Optimize your system for ultimate performance.

Moderators: MattKingUSA, khz

Post Reply
robin
Established Member
Posts: 40
Joined: Wed Nov 04, 2020 10:56 am
Been thanked: 3 times

CPU isolation with cgroups

Post by robin »

Hi out there,
I was experimenting with CPU isolation and it seems I get a way more stable experience (no xruns) under load when I keep all my "non-audio"-stuff on a seperate CPU. Since the "isolcpus" kernel parameter approach is problematic for this because the scheduler doesnt load balance the isolated cores, I get good results using croups (v2). Heres my script to do it on my 4 core:

Code: Select all

#!/bin/bash
systemctl set-property --runtime system.slice AllowedCPUs=0
systemctl set-property --runtime user.slice AllowedCPUs=0

mkdir /sys/fs/cgroup/shield
echo 1-3 > /sys/fs/cgroup/shield/cpuset.cpus
echo $1 > /sys/fs/cgroup/shield/cgroup.procs
Adjust to your core count, check and adjust the cgroups mount point (used path is for Arch, don't know where the others mount it...)
Run as root, give it a user shell PID as argument (you can find it using "echo $$") and all processes launched from this shell will operate on the reserved CPUs, uninterrupted by any system- or user-daemons :D
Let me know what you think,
I will put it on github once I make it a little nicer, just a proof of concept so far
User avatar
LAM
Established Member
Posts: 992
Joined: Thu Oct 08, 2020 3:16 pm
Has thanked: 140 times
Been thanked: 348 times

Re: CPU isolation with cgroups

Post by LAM »

Interesting, I should try.

I had very good results with isolcpus and launching Qjackctl with taskset (that execute RaySession, that executes all the audio apps... did the trick).

in mix, nobody can hear your screen

robin
Established Member
Posts: 40
Joined: Wed Nov 04, 2020 10:56 am
Been thanked: 3 times

Re: CPU isolation with cgroups

Post by robin »

I also tried isolcpus and taskset which kinda worked for sinlge processes like amp sims and synths, but went south quickly under load when mixing. While researching I found this post that explained why that happens, and showed a solution. Sadly cset doesnt work on cgroups v2 any more, so I tried the "native way" ;)
User avatar
LAM
Established Member
Posts: 992
Joined: Thu Oct 08, 2020 3:16 pm
Has thanked: 140 times
Been thanked: 348 times

Re: CPU isolation with cgroups

Post by LAM »

robin wrote: Thu May 19, 2022 8:46 pm I also tried isolcpus and taskset which kinda worked for sinlge processes like amp sims and synths, but went south quickly under load when mixing. While researching I found this post that explained why that happens, and showed a solution. Sadly cset doesnt work on cgroups v2 any more, so I tried the "native way" ;)
The article may be right, but is not what I'm seeing, the processes are spread across the cpus.
I checked both with the ps command and htop.

Code: Select all

ps -e -o pid,psr,comm | grep 'Bespoke\|qjackctl\|carla\|raysession'
   3539   0 qjackctl
   3614   1 raysession
   3654   1 carla-rack
   3657   0 carla-rack
   3734   0 carla-bridge-na
   3736   0 carla-bridge-na
   3751   1 carla-bridge-na
   3755   0 carla-bridge-na
   3821   1 BespokeSynth
  16077   0 carla-rack
Anyway, I will try your suggestion and see if I get some benefits. :)

in mix, nobody can hear your screen

User avatar
scott.thomason
Established Member
Posts: 120
Joined: Fri Apr 01, 2022 2:54 am
Has thanked: 16 times
Been thanked: 27 times
Contact:

Re: CPU isolation with cgroups

Post by scott.thomason »

My approach has been to throw money at the problem. I bought a fast 8-core processor yielding 16 CPU threads, and I just let the kernel figure it out.

---scott
---scott

LinkedIn
j_e_f_f_g
Established Member
Posts: 2032
Joined: Fri Aug 10, 2012 10:48 pm
Been thanked: 357 times

Re: CPU isolation with cgroups

Post by j_e_f_f_g »

scott.thomason wrote: My approach has been to throw money at the problem.
In that case, I should point out that, according to Merlyn, I'm the biggest problem with this forum.

Nothing smaller than 20 dollar bills please.

Author of BackupBand at https://sourceforge.net/projects/backupband/files/
My fans show their support by mentioning my name in their signature.

robin
Established Member
Posts: 40
Joined: Wed Nov 04, 2020 10:56 am
Been thanked: 3 times

Re: CPU isolation with cgroups

Post by robin »

scott.thomason wrote: Thu May 19, 2022 10:43 pm My approach has been to throw money at the problem.
you can always do both ;)
j_e_f_f_g
Established Member
Posts: 2032
Joined: Fri Aug 10, 2012 10:48 pm
Been thanked: 357 times

Re: CPU isolation with cgroups

Post by j_e_f_f_g »

robin wrote: you can always do both
Back off, robin. Get your own sugar daddy.

Author of BackupBand at https://sourceforge.net/projects/backupband/files/
My fans show their support by mentioning my name in their signature.

User avatar
scott.thomason
Established Member
Posts: 120
Joined: Fri Apr 01, 2022 2:54 am
Has thanked: 16 times
Been thanked: 27 times
Contact:

Re: CPU isolation with cgroups

Post by scott.thomason »

j_e_f_f_g wrote: Fri May 20, 2022 5:13 pm Get your own sugar daddy.
LOL, I'm no sugar daddy, I just never encountered the problem until I read about it here. Normally I'd be all for the "invest time in learning, you cheapskate" mode.
---scott

LinkedIn
SimonHF
Posts: 1
Joined: Fri Feb 24, 2023 1:21 am
Has thanked: 1 time

Re: CPU isolation with cgroups

Post by SimonHF »

robin wrote: Thu May 19, 2022 4:22 pm

Hi out there,
I was experimenting with CPU isolation and it seems I get a way more stable experience (no xruns) under load when I keep all my "non-audio"-stuff on a seperate CPU. Since the "isolcpus" kernel parameter approach is problematic for this because the scheduler doesnt load balance the isolated cores, I get good results using croups (v2). Heres my script to do it on my 4 core:

Code: Select all

#!/bin/bash
systemctl set-property --runtime system.slice AllowedCPUs=0
systemctl set-property --runtime user.slice AllowedCPUs=0

mkdir /sys/fs/cgroup/shield
echo 1-3 > /sys/fs/cgroup/shield/cpuset.cpus
echo $1 > /sys/fs/cgroup/shield/cgroup.procs

Adjust to your core count, check and adjust the cgroups mount point (used path is for Arch, don't know where the others mount it...)
Run as root, give it a user shell PID as argument (you can find it using "echo $$") and all processes launched from this shell will operate on the reserved CPUs, uninterrupted by any system- or user-daemons :D
Let me know what you think,
I will put it on github once I make it a little nicer, just a proof of concept so far

Hi Robin, I tried out your script and it worked great! Thanks for posting! However, another question: Once the script is run and the process is running on the 'shield' vCPUs, how to reverse and get the process running on the user.slice again? Naively I thought I could just echo the PID to /sys/fs/cgroup/user.slice/cgroup.procs ... but that always gives an error. How to reverse the operation, or is it a one way trip for the process? :-) Thanks in advance!

Post Reply