Linux changing affinity of a process


I’m working out how to use taskset to set CPU affinity in Linux (Kubuntu) so I can have Handbrake and OBS and/or a game running simultaneously, usually giving them each their own 8-core CCD to play with and I’m not entirely sure I’m doing it right. For the record I’m using a 16 core/32 Thread R9 3950X.

So I have handbrake running a transcode now, and I used both top and System Monitor to get its PID (both agreed with 37144, which is a good sign lol)

So I did

$ taskset -pc 0-15 37144
pid 37144's current affinity list: 0-31
pid 37144's new affinity list: 0-15

Which seems like it should have been successful. Except CPU usage hasn’t gone down. All CPU cores are still maxed out. I ran top again and 37144 (handbrake) is still using 2521% of my CPU which is around 80% of each core.

I double checked taskset

taskset -cp 37144
pid 37144's current affinity list: 0-15

According to taskset handbrake should be relegated to the first 16 virtual cores, but it evidently isn’t. Is there something I’m missing about how this command works?

Did you try the man page yet?

I’ve never installed, let alone used taskset, but I can imagine you need to set the rules ahead of the task as well as when a task has started, the kernel will first finish whatever tasks are running on the cores not allocated before restricting work to the cores that are allocated to said tasks.

I think it’s possible to write a script, that hooks into the application you want to restrict task-assignment, that runs taskset between the start of the task and the actual beginning of the task on the cores. Tricky though.

The issue is that with the size of these files, these transcodes can last days. The one I was doing when I wrote this finished at about 10 am this morning, but I started it on Sunday. I’d quite like to be able to use my PC in the meantime by restricting affinity ad hoc and then resetting it back to all cores when I’m not using my PC. I can’t really predict this before hand, and I wouldn’t want to just always have it running on fewer cores because then it would take all week to finish.

On Windows this was trivial to achieve in task manager using set affinity (and lbr it was necessary. Windows was much worse at allocating resources itself and needed a lot more manual intervention than Linux has required so far), and it seems like it should be trivial in Linux too but it seems there is something fundamental I’m just not getting.

Tbh my current understanding comes from reading the man page. The only thing I can really think of is this part:

-p , –pid

operate on an existing PID and not launch a new task

It could be launching a new instance of Handbrake with the desired affinity instead of applying that to the current task. I thought I was already using the -p option though.

For lols I did actually have a go playing Assassin’s Creed Odyseey while the transcode was running. It was better than I expected, it still ran better than the PS4 version xD Which is still to say that it wasn’t great, but it wasn’t as bad as I expected.

1 Like

I’ve asked this in a couple of different places and haven’t really had a response that really solved the issue, which kind of surprised me. Is this really such a niche thing to want to do? Does anyone know of another way of changing process affinity on the fly, if not taskset? Ngl this is the first time I’ve really considered moving back to Windows, because essentially being unable to use my PC for another 58 and a half hours is kind of big dealbreaker. That’s the current ETA on handbrake.