I have quad core CPU with hyper threading enabled. So I have 8 logical cores. I want to limit my application to use only 4 cores and I want this 4 cores to be different physical cores. Which taskset options (core numbers) should I use? :

  • taskset -c 0,1,2,3 command or
  • taskset -c 0,2,4,6 command

Thank you.


Solution 1:

You can see which cores are on which processors by taking at look at /proc/cpuinfo. For example, you will see on a machine with a dual core cpu:

processor   : 0
(snip)
core id     : 0
cpu cores   : 2

for the first core on the processor and:

processor   : 1
(snip)
core id : 1
cpu cores   : 2

on the second.

So, generally speaking, I think you would want:

taskset -c 0,1,2,3

or:

taskset -c 4,5,6,7

Solution 2:

You may want to take a look at CPU Set (cset) to create a CPU shield group for your application. This way you can ensure that real cores are being used for the important work. I typically disable hyperthreading (on Nehalem systems) for my realtime and low-latency applications.

Here's a tutorial for creating groups of CPUs for specific applications/processes using cset, which is a little more organized than taskset. https://rt.wiki.kernel.org/index.php/Cpuset_management_utility/tutorial

As far as determining the core count and location assignments, take a look at: http://dag.wieers.com/blog/is-hyper-threading-enabled-on-a-linux-system

A quick run of the utility linked on a quad-core Nehalem with hyperthreading enabled shows:

[root@XXX ~/hwloc-1.0.3]# ./utils/lstopo 
Machine (7980MB) + Socket #0 + L3 #0 (8192KB)
  L2 #0 (256KB) + L1 #0 (32KB) + Core #0
    PU #0 (phys=0)
    PU #1 (phys=4)
  L2 #1 (256KB) + L1 #1 (32KB) + Core #1
    PU #2 (phys=1)
    PU #3 (phys=5)
  L2 #2 (256KB) + L1 #2 (32KB) + Core #2
    PU #4 (phys=2)
    PU #5 (phys=6)
  L2 #3 (256KB) + L1 #3 (32KB) + Core #3
    PU #6 (phys=3)
    PU #7 (phys=7)

Solution 3:

You can use like:

taskset -c -p 0-3 pid