Thread affinity in Android

2,462 views
Skip to first unread message

Alejandro Pérez

unread,
Mar 1, 2014, 11:52:07 AM3/1/14
to andro...@googlegroups.com

I have strange problems with thread affinity. I have created a program in C:

#define _GNU_SOURCE
#include<stdio.h>
#include <sys/syscall.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include <time.h>
#include <errno.h>
#define handle_error_en(en, msg) \
               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)


#define NANOS 1000000000LL
#define SIZE 1000

void* mesaureTime(void *cpu)
{
    unsigned long i = 0;
    int s;
    cpu_set_t cpuset;
    struct timespec start, end;
    long elapsed;
    pthread_t id = pthread_self();
    CPU_ZERO(&cpuset);
    CPU_SET(*(int *) cpu, &cpuset); 
    s = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset); 
    if (s != 0)
        handle_error_en(s, "pthread_setaffinity_np");

    if(pthread_equal(id,tid[0]))
        printf("Realizando test...\n");    

    while(i<SIZE){
    clock_gettime(CLOCK_MONOTONIC, &start);
    // Do some calculation.  
    factorial(150000);      
    clock_gettime(CLOCK_MONOTONIC, &end);
    arrayTimes[i] = elapsed;
    elapsed = end.tv_nsec - start.tv_nsec + (end.tv_sec - start.tv_sec)*NANOS;      
    i++;    
    }   
    printf("Finished\n");
    return 0;
}


int factorial(int a){ 

    if (a==1){
            return 1;               
    }else{
        a=a*factorial(a-1);
    }
    return a;
}

int main(int argc, char *argv[])
{
    int i = 0;
    int err, result;    
    int *cpu_pointer;
    int cpu = atoi(argv[1]);
    cpu_pointer = &cpu;

    err = pthread_create(&tid[i], NULL, mesaureTime, (void *) cpu_pointer);

    if (err != 0)
        printf("can't create thread :[%s]", strerror(err));
    else
       printf("Hilo de test creado satisfactoriamente\n"); 
    pthread_join(tid[0], NULL);
    printf("\n Finalizado el test\n");
    return 0;
}

This code works well in a Dual Core Intel CPU with Ubuntu, but when I have compiled it with arm-linux-gnueabi-gcc and I have executed in my Android devices (Nexus 4, Nexus 5 and S4), the program can't assign the thread in CPU 2, CPU 3 or CPU 4, it has only worked in CPU 1. The pthread_setaffinity_np function always returns an error (invalid argument) with CPU 2, 3 or 4.

I have read some questions here Is it possible to set affinity with sched_setaffinity in Android? and Android set thread affinity. I have tried it but I have obtain the same result.

Glenn Kasten

unread,
Mar 2, 2014, 10:16:03 PM3/2/14
to andro...@googlegroups.com
I have heard a rumor that affinity does not work well on hot-swap devices
where cores are turned on/off dynamically, as the given core might not
be known to the kernel any more.  But I was unable to find a reference for this.
If that rumor is true you
could try spinning up a bunch of CPU-bound threads for a while first,
to let the governor activate all cores, and then try the affinity set.
Message has been deleted

Shivnandan Kumar

unread,
Jan 24, 2017, 10:24:42 AM1/24/17
to android-ndk
You need to make sure that core is online when you try to set affinity for that core. So, better make sure that all core are online before trying this .You can make cpu core online by using sysfs entry provided you have root permission.
Reply all
Reply to author
Forward
0 new messages