0

Possible Duplicate:
CPU Affinity

On an x86 machine running Linux (fc12), can I make a process to run on a particular core of a multi processor system? I know there is a function sched_setaffinity which can help to choose the processor, but I want to execute the process on a particular core of a processor.

Community
  • 1
  • 1
ankur
  • 74
  • 4
  • Curious, what is the use case for this? – taz Aug 30 '12 at 12:52
  • @taz:I want to use al the cores for better performance – ankur Aug 30 '12 at 13:03
  • 1
    @anio I think sched_setaffinity is used to set affinity for a processor, i want to set the affinity for cores – ankur Aug 30 '12 at 13:05
  • 1
    "I want to use al the cores for better performance" -- you will very likely get the opposite. Do not tamper with processor affinities unless you know exactly what you're doing. It is almost always better not to touch them (the only allowable exception is running several heavyweight processes on a NUMA system). The OS will by itself make use of several cores, and not having a hard restriction on which cores may be used gives overall a better resource utilization (there are mechanisms to prevent threads from bouncing between CPUs senselessly, if this is what you are afraid of). – Damon Aug 30 '12 at 13:09
  • 1
    @ankur A core is considered a processor. – anio Aug 30 '12 at 13:09
  • @anio I have 2 processors each one having 2 cores – ankur Aug 30 '12 at 13:15
  • "not having a hard restriction on which cores may be used gives overall a better resource utilization" This is true only if an underlying scheduling mechanism in use, such as by the processor, is more efficient than the scheme the user wants to implement. (Probably often a good assumption) – taz Aug 30 '12 at 13:18
  • @ankur Physically we distinguish a processor and a core, yes. But as far as scheduling tasks there is no distinction because a thread can run on ANY core. The core is the lowest level of hardware that a thread can be mapped to. You cannot have a thread running on multiple cores at the same time. – anio Aug 30 '12 at 13:23
  • @taz: Given the choice of leaving a processor core idle and not executing code (solely because of affinity mask) and running application code on that core, which is the better one? Sure, there is this thing about hot caches, but that's what the mechanism behind `sched_migration_cost` takes care of. Also note that your code may run on broken-by-design architectures where all your assumptions fail. The AMD Bulldozer architecture is an example of this. If you don't know _exactly_ what you do, your code will run ~20% slower. The OS (being "native" on the machine) hopefully knows. – Damon Aug 30 '12 at 13:54
  • @Damon I am not familiar with details of this subject. Are you saying that given the choice, it's better to run the instructions? And after looking it up, `sched_migration_cost` seems like exactly the kind of more efficient underlying mechanism I referred to. I am not advocating DIY for core scheduling. However, your point about architectures like Bulldozer remains valid. My understanding is that some platforms natively run well on Bulldozer and some are subject to inefficiencies due to the "broken-by-design" architecture (again, I don't know the details behind this). – taz Aug 30 '12 at 14:20
  • 1
    @taz: I'm saying that executing code when a CPU is idle as opposed to not executing it is faster, and better (pretty obvious). The only issue being that caches won't be hot. However, this is a non-issue if it doesn't happen a billion times per second. Linux for example, by default, never migrates a thread any sooner than after 0.5 microseconds, which is roughly a million or so instructions. This makes the impact of cache effects neglegible, but at the same times allows a thread to run in a timely manner, when otherwise it would maybe have to wait for several milliseconds. – Damon Aug 30 '12 at 17:15

2 Answers2

1

Not sure if this is what you need, I used this code to make a thread run on a specific core. Compile and link with -pthread.

#include "pthread.h"
#include "sched.h"

int affinity = 3; //core id
pthread_t mythread;
mythread = pthread_self();
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(affinity, &cpuset); //lets kernel know only core affinity may run this thread
if (pthread_setaffinity_np(mythread, sizeof(cpu_set_t), &cpuset) <0){
    perror("sched_set_affinity");
}
Lefteris E
  • 2,806
  • 1
  • 24
  • 23
  • pthread_setaffinity_np sets the cpu(and not core) affinity for the thread.A cpu may have multiple cores – ankur Aug 30 '12 at 13:23
0

AFAIK you can not distinguish CPU core from CPU. As far as Linux is concerned, core is full blown CPU. For example if you have 2 CPUs with 2 cores each, Linux will think that you have 4 CPUs.

Marko Kevac
  • 2,902
  • 30
  • 47