Bug 1333391 - move GetCPUCount to the threading layer. r=luke
authorLars T Hansen <lhansen@mozilla.com>
Tue, 24 Jan 2017 12:24:49 +0100
changeset 330969 2e13f21663bba2af75c9184966cf24bf56d90dad
parent 330968 34c57b2a36e3fd5410b5ff9b5bea836599798b08
child 330970 57bd56bce11aa7612cc6978ad0a1447103c04b54
push id86125
push userlhansen@mozilla.com
push dateWed, 25 Jan 2017 08:35:33 +0000
treeherdermozilla-inbound@2e13f21663bb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
bugs1333391
milestone54.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1333391 - move GetCPUCount to the threading layer. r=luke
js/src/gc/Allocator.cpp
js/src/jsgc.cpp
js/src/jsgc.h
js/src/moz.build
js/src/threading/CpuCount.h
js/src/threading/posix/CpuCount.cpp
js/src/threading/windows/CpuCount.cpp
js/src/vm/HelperThreads.cpp
--- a/js/src/gc/Allocator.cpp
+++ b/js/src/gc/Allocator.cpp
@@ -7,16 +7,17 @@
 #include "gc/Allocator.h"
 
 #include "jscntxt.h"
 
 #include "gc/GCInternals.h"
 #include "gc/GCTrace.h"
 #include "gc/Nursery.h"
 #include "jit/JitCompartment.h"
+#include "threading/CpuCount.h"
 #include "vm/Runtime.h"
 #include "vm/String.h"
 
 #include "jsobjinlines.h"
 
 #include "gc/Heap-inl.h"
 
 using namespace js;
--- a/js/src/jsgc.cpp
+++ b/js/src/jsgc.cpp
@@ -3200,38 +3200,16 @@ GCRuntime::assertBackgroundSweepingFinis
             MOZ_ASSERT(!zone->arenas.arenaListsToSweep[i]);
             MOZ_ASSERT(zone->arenas.doneBackgroundFinalize(i));
         }
     }
     MOZ_ASSERT(blocksToFreeAfterSweeping.computedSizeOfExcludingThis() == 0);
 #endif
 }
 
-unsigned
-js::GetCPUCount()
-{
-    static unsigned ncpus = 0;
-    if (ncpus == 0) {
-#if defined(XP_WIN)
-        SYSTEM_INFO sysinfo;
-        GetSystemInfo(&sysinfo);
-        ncpus = unsigned(sysinfo.dwNumberOfProcessors);
-#elif (defined(JS_CODEGEN_ARM) || defined(JS_CODEGEN_ARM64)) && defined(_SC_NPROCESSORS_CONF)
-        // See bug 1333059: _SC_NPROCESSORS_ONLN is not reliable on ARM
-        // due to power saving mode.
-        long n = sysconf(_SC_NPROCESSORS_CONF);
-        ncpus = (n > 0) ? unsigned(n) : 1;
-#else
-        long n = sysconf(_SC_NPROCESSORS_ONLN);
-        ncpus = (n > 0) ? unsigned(n) : 1;
-#endif
-    }
-    return ncpus;
-}
-
 void
 GCHelperState::finish()
 {
     // Wait for any lingering background sweeping to finish.
     waitBackgroundSweepEnd();
 }
 
 GCHelperState::State
--- a/js/src/jsgc.h
+++ b/js/src/jsgc.h
@@ -20,17 +20,16 @@
 #include "js/Vector.h"
 #include "threading/ConditionVariable.h"
 #include "threading/Thread.h"
 #include "vm/NativeObject.h"
 
 namespace js {
 
 class AutoLockHelperThreadState;
-unsigned GetCPUCount();
 
 namespace gcstats {
 struct Statistics;
 } // namespace gcstats
 
 class Nursery;
 
 namespace gc {
--- a/js/src/moz.build
+++ b/js/src/moz.build
@@ -582,25 +582,27 @@ elif CONFIG['JS_CODEGEN_MIPS32'] or CONF
             UNIFIED_SOURCES += [
                 'jit/mips64/Simulator-mips64.cpp'
             ]
 
 if CONFIG['OS_ARCH'] == 'WINNT':
     SOURCES += [
         'jit/ExecutableAllocatorWin.cpp',
         'threading/windows/ConditionVariable.cpp',
+        'threading/windows/CpuCount.cpp',
         'threading/windows/MutexImpl.cpp',
         'threading/windows/Thread.cpp',
     ]
     # _CRT_RAND_S must be #defined before #including stdlib.h to get rand_s()
     DEFINES['_CRT_RAND_S'] = True
 else:
     SOURCES += [
         'jit/ExecutableAllocatorPosix.cpp',
         'threading/posix/ConditionVariable.cpp',
+        'threading/posix/CpuCount.cpp',
         'threading/posix/MutexImpl.cpp',
         'threading/posix/Thread.cpp',
     ]
 
 if CONFIG['JS_HAS_CTYPES']:
     SOURCES += [
         'ctypes/CTypes.cpp',
         'ctypes/Library.cpp',
new file mode 100644
--- /dev/null
+++ b/js/src/threading/CpuCount.h
@@ -0,0 +1,16 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include <inttypes.h>
+
+namespace js {
+
+/*
+ * Return the number of cores on the system.  If the system provides logical
+ * cores (such as hyperthreads) then count each logical core as an actual core.
+ */
+uint32_t GetCPUCount();
+
+}
new file mode 100644
--- /dev/null
+++ b/js/src/threading/posix/CpuCount.cpp
@@ -0,0 +1,30 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include <unistd.h>
+
+#include "threading/CpuCount.h"
+
+uint32_t
+js::GetCPUCount()
+{
+    static uint32_t ncpus = 0;
+
+    // _SC_NPROCESSORS_CONF and _SC_NPROCESSORS_ONLN are common, but not
+    // standard.
+    if (ncpus == 0) {
+#if defined(_SC_NPROCESSORS_CONF)
+        long n = sysconf(_SC_NPROCESSORS_CONF);
+        ncpus = (n > 0) ? uint32_t(n) : 1;
+#elif defined(_SC_NPROCESSORS_ONLN)
+        long n = sysconf(_SC_NPROCESSORS_ONLN);
+        ncpus = (n > 0) ? uint32_t(n) : 1;
+#else
+        ncpus = 1;
+#endif
+    }
+
+    return ncpus;
+}
new file mode 100644
--- /dev/null
+++ b/js/src/threading/windows/CpuCount.cpp
@@ -0,0 +1,22 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "jswin.h"
+
+#include "threading/CpuCount.h"
+
+uint32_t
+js::GetCPUCount()
+{
+    static uint32_t ncpus = 0;
+
+    if (ncpus == 0) {
+        SYSTEM_INFO sysinfo;
+        GetSystemInfo(&sysinfo);
+        ncpus = uint32_t(sysinfo.dwNumberOfProcessors);
+    }
+
+    return ncpus;
+}
--- a/js/src/vm/HelperThreads.cpp
+++ b/js/src/vm/HelperThreads.cpp
@@ -11,16 +11,17 @@
 
 #include "jsnativestack.h"
 #include "jsnum.h" // For FIX_FPU()
 
 #include "builtin/Promise.h"
 #include "frontend/BytecodeCompiler.h"
 #include "gc/GCInternals.h"
 #include "jit/IonBuilder.h"
+#include "threading/CpuCount.h"
 #include "vm/Debugger.h"
 #include "vm/SharedImmutableStringsCache.h"
 #include "vm/Time.h"
 #include "vm/TraceLogging.h"
 
 #include "jscntxtinlines.h"
 #include "jscompartmentinlines.h"
 #include "jsobjinlines.h"