From 0fa75e813ecf0f5f3dd01f89aa76d0e25ab4fcd4 Mon Sep 17 00:00:00 2001 From: kosaki Date: Tue, 21 Oct 2014 18:46:33 +0000 Subject: [PATCH] * ext/etc/etc.c (etc_nprocessors_affin): optimize memory usege a bit. Typical rubyist never use 8k cpus machine. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48080 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ ext/etc/etc.c | 39 +++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index a24010b039..da148ec1a1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Oct 22 03:33:58 2014 KOSAKI Motohiro + + * ext/etc/etc.c (etc_nprocessors_affin): optimize memory usege a + bit. Typical rubyist never use 8k cpus machine. + Wed Oct 22 00:01:09 2014 Yuki Yugui Sonoda * configure.in (XCFLAGS): Add include path for NaCl libraries. diff --git a/ext/etc/etc.c b/ext/etc/etc.c index a37d77cef6..58c00b0161 100644 --- a/ext/etc/etc.c +++ b/ext/etc/etc.c @@ -916,7 +916,7 @@ etc_nprocessors_affin(void) cpu_set_t *cpuset; size_t size; int ret; - int ncpus; + int n; /* * XXX: @@ -934,23 +934,30 @@ etc_nprocessors_affin(void) * So, we use hardcode number for workaround. Current linux kernel * (Linux 3.17) support 8192 cpus at maximum. Then 16384 is enough large. */ - ncpus = 16384; + for (n=64; n < 16384; n *= 2) { + size = CPU_ALLOC_SIZE(n); + if (size >= 1024) { + cpuset = xcalloc(1, size); + if (!cpuset) + return -1; + } else { + cpuset = alloca(size); + CPU_ZERO_S(size, cpuset); + } - cpuset = CPU_ALLOC(ncpus); - if (!cpuset) { - return -1; + ret = sched_getaffinity(0, size, cpuset); + if (ret == 0) { + /* On success, count number of cpus. */ + ret = CPU_COUNT_S(size, cpuset); + } + + if (size >= 1024) { + xfree(cpuset); + } + if (ret > 0) { + return ret; + } } - size = CPU_ALLOC_SIZE(ncpus); - CPU_ZERO_S(size, cpuset); - - ret = sched_getaffinity(0, size, cpuset); - if (ret==-1) { - goto free; - } - - ret = CPU_COUNT_S(size, cpuset); - free: - CPU_FREE(cpuset); return ret; }