ruby-changes:35999
From: kosaki <ko1@a...>
Date: Wed, 22 Oct 2014 10:22:17 +0900 (JST)
Subject: [ruby-changes:35999] kosaki:r48080 (trunk): * ext/etc/etc.c (etc_nprocessors_affin): optimize memory usege a
kosaki 2014-10-22 03:46:33 +0900 (Wed, 22 Oct 2014) New Revision: 48080 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48080 Log: * ext/etc/etc.c (etc_nprocessors_affin): optimize memory usege a bit. Typical rubyist never use 8k cpus machine. Modified files: trunk/ChangeLog trunk/ext/etc/etc.c Index: ChangeLog =================================================================== --- ChangeLog (revision 48079) +++ ChangeLog (revision 48080) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Oct 22 03:33:58 2014 KOSAKI Motohiro <kosaki.motohiro@g...> + + * 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 <yugui@y...> * configure.in (XCFLAGS): Add include path for NaCl libraries. Index: ext/etc/etc.c =================================================================== --- ext/etc/etc.c (revision 48079) +++ ext/etc/etc.c (revision 48080) @@ -916,7 +916,7 @@ etc_nprocessors_affin(void) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L916 cpu_set_t *cpuset; size_t size; int ret; - int ncpus; + int n; /* * XXX: @@ -934,24 +934,31 @@ etc_nprocessors_affin(void) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L934 * 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; - } - size = CPU_ALLOC_SIZE(ncpus); - CPU_ZERO_S(size, cpuset); + ret = sched_getaffinity(0, size, cpuset); + if (ret == 0) { + /* On success, count number of cpus. */ + ret = CPU_COUNT_S(size, cpuset); + } - ret = sched_getaffinity(0, size, cpuset); - if (ret==-1) { - goto free; + if (size >= 1024) { + xfree(cpuset); + } + if (ret > 0) { + return ret; + } } - ret = CPU_COUNT_S(size, cpuset); - free: - CPU_FREE(cpuset); - return ret; } #endif -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/