Sometimes we need to user fewer CPUs in our threading calculations
authorsbarati@apple.com <sbarati@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 12 Apr 2019 23:26:43 +0000 (23:26 +0000)
committersbarati@apple.com <sbarati@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 12 Apr 2019 23:26:43 +0000 (23:26 +0000)
https://bugs.webkit.org/show_bug.cgi?id=196794
<rdar://problem/49389497>

Reviewed by Yusuke Suzuki.

Source/JavaScriptCore:

* JavaScriptCore.xcodeproj/project.pbxproj:
* Sources.txt:
* assembler/CPU.cpp: Added.
(JSC::isKernTCSMAvailable):
(JSC::enableKernTCSM):
(JSC::kernTCSMAwareNumberOfProcessorCores):
* assembler/CPU.h:
(JSC::isKernTCSMAvailable):
(JSC::enableKernTCSM):
(JSC::kernTCSMAwareNumberOfProcessorCores):
* heap/MachineStackMarker.h:
(JSC::MachineThreads::addCurrentThread):
* runtime/JSLock.cpp:
(JSC::JSLock::didAcquireLock):
* runtime/Options.cpp:
(JSC::computeNumberOfWorkerThreads):
(JSC::computePriorityDeltaOfWorkerThreads):
* wasm/WasmWorklist.cpp:
(JSC::Wasm::Worklist::Worklist):

Source/WebKit:

* WebProcess/com.apple.WebProcess.sb.in:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@244233 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
Source/JavaScriptCore/Sources.txt
Source/JavaScriptCore/assembler/CPU.cpp [new file with mode: 0644]
Source/JavaScriptCore/assembler/CPU.h
Source/JavaScriptCore/heap/MachineStackMarker.h
Source/JavaScriptCore/runtime/JSLock.cpp
Source/JavaScriptCore/runtime/Options.cpp
Source/JavaScriptCore/wasm/WasmWorklist.cpp
Source/WebKit/ChangeLog
Source/WebKit/WebProcess/com.apple.WebProcess.sb.in

index d628ec7..bff272e 100644 (file)
@@ -1,3 +1,31 @@
+2019-04-12  Saam barati  <sbarati@apple.com>
+
+        Sometimes we need to user fewer CPUs in our threading calculations
+        https://bugs.webkit.org/show_bug.cgi?id=196794
+        <rdar://problem/49389497>
+
+        Reviewed by Yusuke Suzuki.
+
+        * JavaScriptCore.xcodeproj/project.pbxproj:
+        * Sources.txt:
+        * assembler/CPU.cpp: Added.
+        (JSC::isKernTCSMAvailable):
+        (JSC::enableKernTCSM):
+        (JSC::kernTCSMAwareNumberOfProcessorCores):
+        * assembler/CPU.h:
+        (JSC::isKernTCSMAvailable):
+        (JSC::enableKernTCSM):
+        (JSC::kernTCSMAwareNumberOfProcessorCores):
+        * heap/MachineStackMarker.h:
+        (JSC::MachineThreads::addCurrentThread):
+        * runtime/JSLock.cpp:
+        (JSC::JSLock::didAcquireLock):
+        * runtime/Options.cpp:
+        (JSC::computeNumberOfWorkerThreads):
+        (JSC::computePriorityDeltaOfWorkerThreads):
+        * wasm/WasmWorklist.cpp:
+        (JSC::Wasm::Worklist::Worklist):
+
 2019-04-12  Robin Morisset  <rmorisset@apple.com>
 
         Use padding at end of ArrayBuffer
index cb38278..d51d7e6 100644 (file)
                521131F61F82BF11007CCEEE /* PolyProtoAccessChain.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PolyProtoAccessChain.h; sourceTree = "<group>"; };
                521322431ECBCE8200F65615 /* WebAssemblyFunctionBase.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = WebAssemblyFunctionBase.cpp; path = js/WebAssemblyFunctionBase.cpp; sourceTree = "<group>"; };
                521322441ECBCE8200F65615 /* WebAssemblyFunctionBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WebAssemblyFunctionBase.h; path = js/WebAssemblyFunctionBase.h; sourceTree = "<group>"; };
+               52335628225EB8E900268BD2 /* CPU.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CPU.cpp; sourceTree = "<group>"; };
                523FD88C225566C3003B3DCC /* WebAssemblyFunctionHeapCellType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = WebAssemblyFunctionHeapCellType.h; path = js/WebAssemblyFunctionHeapCellType.h; sourceTree = "<group>"; };
                523FD88D225566C4003B3DCC /* WebAssemblyFunctionHeapCellType.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = WebAssemblyFunctionHeapCellType.cpp; path = js/WebAssemblyFunctionHeapCellType.cpp; sourceTree = "<group>"; };
                524E9D7122092B4500A6BEEE /* AirAllocateRegistersAndStackAndGenerateCode.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = AirAllocateRegistersAndStackAndGenerateCode.cpp; path = b3/air/AirAllocateRegistersAndStackAndGenerateCode.cpp; sourceTree = "<group>"; };
                                43C392AA1C3BEB0000241F53 /* AssemblerCommon.h */,
                                86E116B00FE75AC800B512BC /* CodeLocation.h */,
                                0F30D7BF1D95D62F0053089D /* CPU.h */,
+                               52335628225EB8E900268BD2 /* CPU.cpp */,
                                0F37308E1C0CD68500052BFA /* DisallowMacroScratchRegisterUsage.h */,
                                0FF4275615914A20004CB9FF /* LinkBuffer.cpp */,
                                86D3B3C110159D7F002865E7 /* LinkBuffer.h */,
index 3e5859c..f553444 100644 (file)
@@ -43,6 +43,7 @@ API/JSWeakValue.cpp
 API/OpaqueJSString.cpp
 
 assembler/AbstractMacroAssembler.cpp
+assembler/CPU.cpp
 assembler/LinkBuffer.cpp
 assembler/MacroAssembler.cpp
 assembler/MacroAssemblerARM64.cpp
diff --git a/Source/JavaScriptCore/assembler/CPU.cpp b/Source/JavaScriptCore/assembler/CPU.cpp
new file mode 100644 (file)
index 0000000..523e62c
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#include "config.h"
+#include "CPU.h"
+
+#if (CPU(X86) || CPU(X86_64)) && OS(DARWIN)
+#include <sys/sysctl.h>
+#endif
+
+namespace JSC {
+
+#if (CPU(X86) || CPU(X86_64)) && OS(DARWIN)
+bool isKernTCSMAvailable()
+{
+    uint32_t val = 0;
+    size_t valSize = sizeof(val);
+    int rc = sysctlbyname("kern.tcsm_available", &val, &valSize, NULL, 0);
+    if (rc < 0)
+        return false;
+    return !!val;
+}
+
+bool enableKernTCSM()
+{
+    uint32_t val = 1;
+    int rc = sysctlbyname("kern.tcsm_enable", NULL, 0, &val, sizeof(val));
+    if (rc < 0)
+        return false;
+    return true;
+}
+
+int kernTCSMAwareNumberOfProcessorCores()
+{
+    static std::once_flag onceFlag;
+    static int result;
+    std::call_once(onceFlag, [] {
+        result = WTF::numberOfProcessorCores();
+        if (result <= 1)
+            return;
+        if (isKernTCSMAvailable())
+            --result;
+    });
+    return result;
+}
+#endif // #if (CPU(X86) || CPU(X86_64)) && OS(DARWIN)
+
+} // namespace JSC
index 617993f..ddccd00 100644 (file)
@@ -26,6 +26,7 @@
 #pragma once
 
 #include "Options.h"
+#include <wtf/NumberOfCores.h>
 
 namespace JSC {
 
@@ -131,5 +132,15 @@ inline bool hasSensibleDoubleToInt()
     return optimizeForX86();
 }
 
+#if (CPU(X86) || CPU(X86_64)) && OS(DARWIN)
+bool isKernTCSMAvailable();
+bool enableKernTCSM();
+int kernTCSMAwareNumberOfProcessorCores();
+#else
+ALWAYS_INLINE bool isKernTCSMAvailable() { return false; }
+ALWAYS_INLINE bool enableKernTCSM() { return false; }
+ALWAYS_INLINE int kernTCSMAwareNumberOfProcessorCores() { return WTF::numberOfProcessorCores(); }
+#endif
+
 } // namespace JSC
 
index 5fe07c3..e29ee40 100644 (file)
@@ -48,7 +48,7 @@ public:
     void gatherConservativeRoots(ConservativeRoots&, JITStubRoutineSet&, CodeBlockSet&, CurrentThreadState*, Thread*);
 
     // Only needs to be called by clients that can use the same heap from multiple threads.
-    void addCurrentThread() { m_threadGroup->addCurrentThread(); }
+    bool addCurrentThread() { return m_threadGroup->addCurrentThread() == ThreadGroupAddResult::NewlyAdded; }
 
     WordLock& getLock() { return m_threadGroup->getLock(); }
     const ListHashSet<Ref<Thread>>& threads(const AbstractLocker& locker) const { return m_threadGroup->threads(locker); }
index 24de74e..000969c 100644 (file)
@@ -148,7 +148,11 @@ void JSLock::didAcquireLock()
     void* p = currentStackPointer();
     m_vm->setStackPointerAtVMEntry(p);
 
-    m_vm->heap.machineThreads().addCurrentThread();
+    if (m_vm->heap.machineThreads().addCurrentThread()) {
+        if (isKernTCSMAvailable())
+            enableKernTCSM();
+    }
+
 #if ENABLE(WEBASSEMBLY)
     if (Wasm::isSupported())
         Wasm::startTrackingCurrentThread();
index 8e8d3ae..609efab 100644 (file)
@@ -205,7 +205,7 @@ bool Options::overrideAliasedOptionWithHeuristic(const char* name)
 
 static unsigned computeNumberOfWorkerThreads(int maxNumberOfWorkerThreads, int minimum = 1)
 {
-    int cpusToUse = std::min(WTF::numberOfProcessorCores(), maxNumberOfWorkerThreads);
+    int cpusToUse = std::min(kernTCSMAwareNumberOfProcessorCores(), maxNumberOfWorkerThreads);
 
     // Be paranoid, it is the OS we're dealing with, after all.
     ASSERT(cpusToUse >= 1);
@@ -214,7 +214,7 @@ static unsigned computeNumberOfWorkerThreads(int maxNumberOfWorkerThreads, int m
 
 static int32_t computePriorityDeltaOfWorkerThreads(int32_t twoCorePriorityDelta, int32_t multiCorePriorityDelta)
 {
-    if (WTF::numberOfProcessorCores() <= 2)
+    if (kernTCSMAwareNumberOfProcessorCores() <= 2)
         return twoCorePriorityDelta;
 
     return multiCorePriorityDelta;
index abab021..36d74c1 100644 (file)
 
 #if ENABLE(WEBASSEMBLY)
 
+#include "CPU.h"
 #include "WasmPlan.h"
 
-#include <wtf/NumberOfCores.h>
-
 namespace JSC { namespace Wasm {
 
 namespace WasmWorklistInternal {
@@ -207,7 +206,7 @@ Worklist::Worklist()
     : m_lock(Box<Lock>::create())
     , m_planEnqueued(AutomaticThreadCondition::create())
 {
-    unsigned numberOfCompilationThreads = Options::useConcurrentJIT() ? WTF::numberOfProcessorCores() : 1;
+    unsigned numberOfCompilationThreads = Options::useConcurrentJIT() ? kernTCSMAwareNumberOfProcessorCores() : 1;
     m_threads.reserveCapacity(numberOfCompilationThreads);
     LockHolder locker(*m_lock);
     for (unsigned i = 0; i < numberOfCompilationThreads; i++)
index f2d36c5..89fba0a 100644 (file)
@@ -1,3 +1,13 @@
+2019-04-12  Saam barati  <sbarati@apple.com>
+
+        Sometimes we need to user fewer CPUs in our threading calculations
+        https://bugs.webkit.org/show_bug.cgi?id=196794
+        <rdar://problem/49389497>
+
+        Reviewed by Yusuke Suzuki.
+
+        * WebProcess/com.apple.WebProcess.sb.in:
+
 2019-04-12  Devin Rousso  <drousso@apple.com>
 
         WebDriver: evaluating javascript shouldn't fail if a dialog is shown
index 736ccaa..add00d2 100644 (file)
         "kern.version"
         "machdep.cpu.brand_string"
         "security.mac.sandbox.sentinel"
+        "kern.tcsm_enable"
+        "kern.tcsm_available"
         "vm.footprint_suspend")
     (sysctl-name-regex #"^hw.(active|avail)cpu")
     (sysctl-name-regex #"^hw.(busfrequency|cachelinesize|cpufrequency(|_max)|pagesize|tbfrequency)(|_compat)")
     (sysctl-name-regex #"^net.routetable")
 )
 
+(allow sysctl-write
+    (sysctl-name
+        "kern.tcsm_enable"))
+
 (deny iokit-get-properties)
 (allow iokit-get-properties
     (iokit-property "AAPL,LCD-PowerState-ON") ;; <rdar://problem/47738015>