Assigning Processes to CPU Cores

I have access to a CentOS 6 web server (a PHP/MySQL forum) running Apache 2.2.15 and MySQL 5.1.52. This box has dual oct-core Xeons.

What I find looking over the CPU graphs is that the first two cores (0 and 1) hit 90% usage whilst the other fourteen are only reaching 30-40% usage. Core 8, which I believe is the first core of the second processor, see's high usage (up to 90%) but then as mentioned, all others are low.

Apache fires up about 20 processes and I suspect in its default state, it can't understand, and make use of all the cores naturally. This should be the kernels job I think (can someone confirm?). However, with those cores maxing out, there is a noticable performance drop despite spare RAM and disk I/O.

Is there something I can tweak in Apache to "make it aware" of all the other cores, or perhaps a kernel peramater to assign certain process to certain cores? Can I exclude cores 0 and 1 for example, from being used for Apache and MySQL?


Solution 1:

Tunning process affinity with taskset

e.g assign current process to cpu-core 0,1,3

$taskset -cp 0,1,3 $$

pid 21698's current affinity list: 0-7

pid 21698's new affinity list: 0,1,3

Isolate CPUs to run particular process only. Kernel parameter “isolcpus” can isolate particular CPUs from doing other tasks. Together with taskset, you can have particular CPUs to run designated tasks only. E.g put “isolcpus=2,3 “ in grub.conf will isolate CPU 2 and 3.

http://honglus.blogspot.com/2011/03/tune-interrupt-and-process-cpu-affinity.html

Solution 2:

Not all application traffic is highly paralellizeable. We have a web-application internally that is resolutely single-threaded, even when running on a 24-core monster. In our case, the web-server will happily use as many cores as we throw at it for static serving; but when bound to a specific app-server it stays with one.

If we look at per-core usage when that single-threaded process is running each core will be running at 100*(1/n) percentage. The kernel is moving the load across multiple cores as it should, but only 100% of one processor's capacity is ever used.

In our case, we are running a LAMx stack. Where (x) is undefined in this answer. As it turns out, what (x) represents is rather important. So yes, ability to use lots of cores is dependent upon all steps in the stack, not just the LAM parts.

Solution 3:

It is, indeed, kernel's job to schedule tasks. In a multi core environment, at times (but not always), it happens that kernel assigns job to all the cores but on one or two cores all the heavy duty processes like apache gets assigned.
Ideally your kernel should already be aware that there are multiple cores in your machine. Still run the following to confirm as superuser:

dmidecode|grep Core

On my dual core machine it says:

Version: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz
Core Count: 2
Core Enabled: 2