About nixCraft

Topics

Linux Setting processor affinity for a certain task or process

Posted by Vivek Gite [Last updated: April 12, 2008]

When you are using SMP (Symmetric MultiProcessing) you might want to override the kernel's process scheduling and bind a certain process to a specific CPU(s).

But what is CPU affinity?

CPU affinity is nothing but a scheduler property that "bonds" a process to a given set of CPUs on the SMP system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. Note that the Linux scheduler also supports natural CPU affinity:

The scheduler attempts to keep processes on the same CPU as long as practical for performance reasons. Therefore, forcing a specific CPU affinity is useful only in certain applications. For example, application such as Oracle (ERP apps) use # of cpus per instance licensed. You can bound Oracle to specific CPU to avoid license problem. This is a really useful on large server having 4 or 8 CPUS

Setting processor affinity for a certain task or process using taskset command

taskset is used to set or retrieve the CPU affinity of a running process given its PID or to launch a new COMMAND with a given CPU affinity. However taskset is not installed by default. You need to install schedutils (Linux scheduler utilities) package.

Install schedutils

Debian Linux:
# apt-get install schedutils
Red Hat Enterprise Linux:
# up2date schedutils
OR
# rpm -ivh schedutils*
Under latest version of Debian / Ubuntu Linux taskset is installed by default using util-linux package.

The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU. For example:

To set the processor affinity of process 13545 to processor #0 (1st processor) type following command:
# taskset 0x00000001 -p 13545
If you find a bitmask hard to use, then you can specify a numerical list of processors instead of a bitmask using -c flag:
# taskset -c 1 -p 13545
# taskset -c 3,4 -p 13545

Where,

See also:

UPDATED for accuracy.

E-mail this to a Friend    Printable Version

You may also be interested in other helpful articles:

Discussion on This Article:

  1. triton Says:

    How can I check if it is is using only 1 cpu?

  2. nixcraft Says:

    Use any one of the following commands:
    less /proc/interruptsor
    cd /sys/devices/system/cpu
    ls
    ormpstat

    See url

  3. Ricardo Reis Says:

    it should be

    taskset -c -p 1 13545

    options firts, arguments later. I know, it seems stupid but it is what it is. It only worked like this on my machines (debian)

  4. Erik Bussink Says:

    Hiya, thanks for this very interesting subject. I was looking for something like this, to be able to schedule my VMware Workstation 6.0 to run on my HP xw9400 Workstation Dual-Core Opteron (3&4) under RHEL5 (x86-64).

  5. Ramanath Says:

    Suppose I give this command

    root@dialer243:~#taskset -c -p 1 13545

    how do I check that the command worked or not!! I mean the cpu affinity was set or not ?

  6. Sutanto Kurniawan Says:

    Ramanath:
    You can use the ‘top’ command.

    After loading ‘top’ press 1 (one) to switch to SMP view, which will show each CPU usage.

    Try assigning CPU intensive program to one of your CPU, and see (in ‘top’) whether the affinity is set or not.

    In my system, the program taskset definitely do it’s job. If I don’t use the taskset, the CPU that work on my program is vary from 1 to 4 (I have quad core), when I assign the program to one specific CPU, then only that CPU that has high usage.

  7. Boca Says:

    A little odd, but installing schedutils on ubuntu gutsy would remove essential components which is not recommended. However, taskset is available as part of default install.

  8. mike503 Says:

    Yeah, taskset is from util-linux. The syntax is now taskset -cp $CPU $PID

    Took me a few minutes to figure that one out :) It wants to remove util-linux for schedutils and that has valuable utilities in it. It has taskset anyway. This article might be good to update…

    Ubuntu 7.10 gutsy x86_64.

  9. ellis bright Says:

    this is great - i have a java wrapper on a engine written in C++ that crashes whenever the engine is run on more than 1 processor. since all the newer processors are multi-core, i have been running the OS in uniprocessor mode and basically wasting the other cores!
    ~Ellis http://www.lightspoke.com

  10. InQuest Says:

    Does the taskset do its job on already running processes. I tried but it didn’t work. First I ran 2 identical processes without taskset. They were homogeneously running on all 8 cpus I had. Now, when I tried assigning 2 cpus to 1 and 6 to another, it didn’t work.
    I killed the process where I assigned 6 cpus. Still the other was using all 8 cpus.

  11. franchisu Says:

    hi guys,
    I tried using taskset and schedtool interchangeably, and I noticed that when I’m running virtualbox, which is a cpu intensive process, then I change proc affinity which doesn’t seem to have that big an effect in ‘top’. I change from one proc to another and the output of ‘top’ seems unchanged, while the process is already running. However, if I begin a new cpu intensive process and assign it an affinity before running it, then I can see changes in the output of ‘top’. any clues to this? perhaps my already running virtualbox is too ‘attached’ to its original proc that the linux kernel decides to override/not pay attention to my schedtool/taskset commands?

  12. anuu Says:

    anu@el[~]$ taskset -c -p 1 11574
    pid 11574’s current affinity list: 0,1
    pid 11574’s new affinity list: 1

    it works to me
    debian etch

    great tool to look what proceses are running is
    htop

  13. franchisu Says:

    Actually, it evidently works especially when I set a process’ affinity before running the process. I noticed that while running a cpu or memory intensive task such as Virtual box or VMware, alternating/changing processor affinity doesn’t seem as evident as my previous example. Anyone noticed this? Perhaps it has something to do with the Linux kernel overriding the manual affinity changing since the kernel knows how to better handle processes and it probably knows that there will be performance hits when moving processes such as VMware/Virtualbox from one proc to another (e.g. cache hit rate etc) ?

  14. Angelina Says:

    Hi,

    How to use task set before the process started?

    Example: when I start oracle db, I want to use only 2 CPU2 for oracle DB processes. In this point I do not know what is PID.

    Oracle says, if you use 4 CPUs at ANY point of time, you should pay oracle license for 4 CPUs.

    Please help me … I do not want to setup kernel maxcpus parameter because I will loose CPUs …I want to use them for some other processes.

    Regards,
    Angelina

  15. vivek Says:

    Angelina,

    Try following for 2 cpu
    taskset -c 0,1 /etc/init.d/oracle-xe start
    OR
    su - oracle
    taskset -c 0,1 lsnrctl start
    taskset -c 0,1 dbstart

    OR
    taskset -c 0,1 sqlplus '/ as sysdba'
    And at SQL prompt type:
    startup

    HTH

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tags: , , , , , , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Powered by Open source software.