CPU Affinity setting not sticking

Tim asked:

I’m attempting to set the affinity of mysql (latest Percona Server) on a 4vCpu VPS virtualized with OpenVZ and running CentOS 6.x.

The command I’m running is taskset -pc 0-1 1026. I’ve also tried 0,1 instead. The output is:

pid 1026's current affinity list: 0-3
pid 1026's new affinity list: 0-3

So, it does not seem to be ‘sticking’. It can be confirmed by running taskset again to find the cpu’s available to the process. Is this an issue with my virtualization type, perhaps?

I’m doing this because every now and then I need mysql to run quite hard, and going over 2.0-4.0 for more than 2 hours gets me a restart with my current provider.

Is there a clear reason why this isn’t working, or is more troubleshooting required?

My answer:

CPU affinity masks are ignored inside OpenVZ containers. You will not be able to get this to “work”.

In RHEL5-based kernels we had the notion of virtual cpus: tasks were
scheduled on vcpus while the vcpus were somehow distributed among
physical cpus. The sched_setaffinity syscall could be used to bind
tasks to vcpus then.

This concept was cumbersome and often sub-optimal so in RHEL6 we
decided to drop it: currently setting nr_cpus limit for a container
is actually equivalent to setting cpulimit. The decision is justified
by the fact that the latest Linux scheduler is smart enough to gather
actively interacting tasks together so that there is no need to limit
parallelism artificially.

As a result, cpu affinity support was dropped. Tasks can still use the
sched_setaffinity syscall, but it will be ignored.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.