Commit 8b4ae459 authored by Leszek Swirski's avatar Leszek Swirski Committed by Commit Bot

[base] Check CPU affinity when counting CPUs

On Linux, use sched_getaffinity to count usable CPUs, so that runners of
Chromium can restrict cores with e.g. taskset.

This is observable locally by running

    taskset ./chrome

recording a trace with chrome://tracing, and checking the number of
foreground worker pool threads created.

Change-Id: I0402c1323a6e26a8fae6ce845e89a7c69d0acc25
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2270183
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Auto-Submit: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#785807}
parent 29751815
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "base/system/sys_info.h" #include "base/system/sys_info.h"
#include <errno.h> #include <errno.h>
#include <sched.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
...@@ -56,7 +57,21 @@ int NumberOfProcessors() { ...@@ -56,7 +57,21 @@ int NumberOfProcessors() {
return 1; return 1;
} }
return static_cast<int>(res); int num_cpus = static_cast<int>(res);
#ifdef OS_LINUX
// Restrict the CPU count based on the process's CPU affinity mask, if
// available.
cpu_set_t* cpu_set = CPU_ALLOC(num_cpus);
size_t cpu_set_size = CPU_ALLOC_SIZE(num_cpus);
int ret = sched_getaffinity(0, cpu_set_size, cpu_set);
if (ret == 0) {
num_cpus = CPU_COUNT_S(cpu_set_size, cpu_set);
}
CPU_FREE(cpu_set);
#endif // OS_LINUX
return num_cpus;
} }
base::LazyInstance<base::internal::LazySysInfoValue<int, NumberOfProcessors>>:: base::LazyInstance<base::internal::LazySysInfoValue<int, NumberOfProcessors>>::
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment