Manolete
Manolete

Reputation: 3517

Find out the defualt pthread affinity policy followed by a Linux Kernel 2.6.32

I am trying to find out what is the default affinity policy followed by a Scientific Linux Kernel 2.6.32 when creating pthreads in a HT system. Is there a way of knowing it? In the pthreads creation there are no calls to affinity, so I guess the affinity is left to the OS.

Upvotes: 0

Views: 155

Answers (1)

Andres Gonzalez
Andres Gonzalez

Reputation: 2239

Not sure if this is helpful but here is some code that will get and set the thread affinity using the pthread API:

// nail down this thread so it will only run on a specific CPU
cpu_set_t tCPUSet;
CPU_ZERO(&tCPUSet);
CPU_SET(ENCODE_LEFT_THREAD_CPU, &tCPUSet);
nReturnCode = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &tCPUSet);
if (nReturnCode == -1)  {
    pLogFile->Write(LOG_ERR, "ERROR: encodeLeftThread(): pthread_setaffinity() failed, %s, exiting thread... ", strerror(errno));
    nThreadReturnValue = THREAD_CPU_AFFINITY_ERRORS;
    pthread_exit((void *)&nThreadReturnValue);
}

CPU_ZERO(&tCPUSet);
nReturnCode = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &tCPUSet);
if (nReturnCode == -1)  {
    pLogFile->Write(LOG_ERR, "ERROR: encodeLeftThread(): 2nd pthread_getaffinity() failed, %s, exiting thread... ", strerror(errno));
    nThreadReturnValue = THREAD_CPU_AFFINITY_ERRORS;
    pthread_exit((void *)&nThreadReturnValue);
}

sCPUs.clear();
sCPUs.append("    new left thread CPU affinity: ");
if (bInitLog)  {
    for (int i = 0; i < CPU_SETSIZE; i++)  {
        if (CPU_ISSET(i, &tCPUSet))  {
            sCPUs.append(" ");
            char c = '0' + i;
            sCPUs = sCPUs + c;
        }
    }
}
if (bInitLog) pLogFile->Write(LOG_DEBUG, "%s ", sCPUs.c_str());

Upvotes: 1

Related Questions