How to set CPU affinity of the sandboxed renderer process

753 views
Skip to first unread message

Yuhao Zhu

unread,
Mar 15, 2014, 12:34:41 AM3/15/14
to chromi...@chromium.org
I have the following code in the sandboxed renderer running on a dual core system:

#define CPU_SETSIZE 1024
#define __NCPUBITS  (8 * sizeof (unsigned long))
typedef struct
{
   unsigned long __bits[CPU_SETSIZE / __NCPUBITS];
} cpu_set_t;

#define CPU_SET(cpu, cpusetp) ((cpusetp)->__bits[(cpu)/__NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))
#define CPU_ZERO(cpusetp) memset((cpusetp), 0, sizeof(cpu_set_t))

int err;
pid = getpid();
cpu_set_t new_mask;
CPU_ZERO(&new_mask);
CPU_SET(1, &new_mask);
int syscallres = syscall(__NR_sched_setaffinity, pid, sizeof(new_mask), &new_mask);
if (syscallres)
{
    err = errno;
    fprintf(stdout, "set affinity error %d\n", err);
}
else  fprintf(stdout, "set affinity for %d to %ul\n", pid, new_mask.__bits[0]);

My intention was to run the renderer process only on core 1(i.e., the second core). And that's why I did CPU_SET(1, &new_mask); I'm was wondering if this is the right way to set the mask?

This program returned set affinity correctly. However, seems like the renderer was still running on both cores. Any suggestions?

Primiano Tucci

unread,
Mar 17, 2014, 4:34:02 AM3/17/14
to Yuhao Zhu, Chromium-dev
Just a couple of notes:
- The way you set the cpu_set_t mask looks correct.
- Any reason why you redefine / copy-paste the CPU_ macros and don't just include sched.h?
- Ditto for syscall(_NR_SCHED...) Just use sched_setaffinity. (Note the full path to the header you need it is: ./third_party/android_tools/ndk/platforms/android-14/arch-arm/usr/include/sched.h, which should be already in your include path)
- sched_setaffinity is still per-thread, not per process. The full story is a bit longer... these days you should use pthread_setaffinity_np if you use the pthread interface (who doesn't?), but at a first glance that is not available in the ndk (nor I could find any trace in the Android AOSP bionic libc). sched_setaffinity should do its job, but remember that you have to call it before spawning any other thread (if you want the other threads to inherit the cpu mask), or you have to repeat the call on any thread previously spawned to achieve the same. For the renderer process, a good place should be very early in content/renderer/renderer_main.cc.
- You can doublecheck that your call had effect by looking at the output of adb shell cat /proc/PID/status | grep Cpus_allowed
- If you see the renderer running on other processors and you are 100% sure that the syscall did succeed, probably you just called it too late and other threads are still unbounded.

Cheers,
Primiano



--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

Reply all
Reply to author
Forward
0 new messages