Title: [97731] trunk/Source/_javascript_Core
Revision
97731
Author
[email protected]
Date
2011-10-18 02:18:18 -0700 (Tue, 18 Oct 2011)

Log Message

ParallelJobs: maximum number of threads should be determined dynamically
https://bugs.webkit.org/show_bug.cgi?id=68540

Reviewed by Zoltan Herczeg.

Add logic to determine the number of cores and use this as
the maximum number of threads. The implementation currently
covers Linux, Darwin, Windows, AIX, Solaris, OpenBSD and NetBSD.
The patch was tested on Linux, Mac and Windows which was enough to
cover all code path. It should work on the rest accoring to the
documentation of those OS's. The hard coded constant is still used
on uncovered OS's which should be fixed in the future.

* wtf/ParallelJobs.h: Removed the default value of the requestedJobNumber
argument because clients should always fill it and the 0 default value
was incorrect anyway.
(WTF::ParallelJobs::ParallelJobs):
* wtf/ParallelJobsGeneric.cpp:
(WTF::ParallelEnvironment::determineMaxNumberOfParallelThreads):
* wtf/ParallelJobsGeneric.h:
(WTF::ParallelEnvironment::ParallelEnvironment):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (97730 => 97731)


--- trunk/Source/_javascript_Core/ChangeLog	2011-10-18 09:11:48 UTC (rev 97730)
+++ trunk/Source/_javascript_Core/ChangeLog	2011-10-18 09:18:18 UTC (rev 97731)
@@ -1,3 +1,27 @@
+2011-10-18  Balazs Kelemen  <[email protected]>
+
+        ParallelJobs: maximum number of threads should be determined dynamically
+        https://bugs.webkit.org/show_bug.cgi?id=68540
+
+        Reviewed by Zoltan Herczeg.
+
+        Add logic to determine the number of cores and use this as
+        the maximum number of threads. The implementation currently
+        covers Linux, Darwin, Windows, AIX, Solaris, OpenBSD and NetBSD.
+        The patch was tested on Linux, Mac and Windows which was enough to
+        cover all code path. It should work on the rest accoring to the
+        documentation of those OS's. The hard coded constant is still used
+        on uncovered OS's which should be fixed in the future.
+
+        * wtf/ParallelJobs.h: Removed the default value of the requestedJobNumber
+        argument because clients should always fill it and the 0 default value
+        was incorrect anyway.
+        (WTF::ParallelJobs::ParallelJobs):
+        * wtf/ParallelJobsGeneric.cpp:
+        (WTF::ParallelEnvironment::determineMaxNumberOfParallelThreads):
+        * wtf/ParallelJobsGeneric.h:
+        (WTF::ParallelEnvironment::ParallelEnvironment):
+
 2011-10-17  Gavin Barraclough  <[email protected]>
 
         Reverted r997709, this caused test failures.

Modified: trunk/Source/_javascript_Core/wtf/ParallelJobs.h (97730 => 97731)


--- trunk/Source/_javascript_Core/wtf/ParallelJobs.h	2011-10-18 09:11:48 UTC (rev 97730)
+++ trunk/Source/_javascript_Core/wtf/ParallelJobs.h	2011-10-18 09:18:18 UTC (rev 97731)
@@ -80,7 +80,7 @@
 public:
     typedef void (*WorkerFunction)(Type*);
 
-    ParallelJobs(WorkerFunction func, int requestedJobNumber = 0) :
+    ParallelJobs(WorkerFunction func, int requestedJobNumber) :
         m_parallelEnvironment(reinterpret_cast<ParallelEnvironment::ThreadFunction>(func), sizeof(Type), requestedJobNumber)
     {
         m_parameters.grow(m_parallelEnvironment.numberOfJobs());

Modified: trunk/Source/_javascript_Core/wtf/ParallelJobsGeneric.cpp (97730 => 97731)


--- trunk/Source/_javascript_Core/wtf/ParallelJobsGeneric.cpp	2011-10-18 09:11:48 UTC (rev 97730)
+++ trunk/Source/_javascript_Core/wtf/ParallelJobsGeneric.cpp	2011-10-18 09:18:18 UTC (rev 97731)
@@ -30,11 +30,49 @@
 #if ENABLE(PARALLEL_JOBS) && ENABLE(THREADING_GENERIC)
 
 #include "ParallelJobs.h"
+#include "UnusedParam.h"
 
+#if OS(DARWIN) || OS(OPENBSD) || OS(NETBSD)
+#include <sys/sysctl.h>
+#include <sys/types.h>
+#elif OS(LINUX) || OS(AIX) || OS(SOLARIS)
+#include <unistd.h>
+#elif OS(WINDOWS)
+#include <Windows.h>
+#endif
+
 namespace WTF {
 
 Vector< RefPtr<ParallelEnvironment::ThreadPrivate> >* ParallelEnvironment::s_threadPool = 0;
 
+int ParallelEnvironment::s_maxNumberOfParallelThreads = -1;
+
+void ParallelEnvironment::determineMaxNumberOfParallelThreads()
+{
+    const int defaultIfUnavailable = 2;
+#if OS(DARWIN) || OS(OPENBSD) || OS(NETBSD)
+    unsigned result;
+    size_t length = sizeof(result);
+    int name[] = {
+        CTL_HW,
+        HW_NCPU
+    };
+    int sysctlResult = sysctl(name, sizeof(name) / sizeof(int), &result, &length, 0, 0);
+    s_maxNumberOfParallelThreads = sysctlResult < 0 ? defaultIfUnavailable : result;
+#elif OS(LINUX) || OS(AIX) || OS(SOLARIS)
+    long sysconfResult = sysconf(_SC_NPROCESSORS_ONLN);
+    s_maxNumberOfParallelThreads = sysconfResult < 0 ? defaultIfUnavailable : static_cast<int>(sysconfResult);
+#elif OS(WINDOWS)
+    UNUSED_PARAM(defaultIfUnavailable);
+
+    SYSTEM_INFO sysInfo;
+    GetSystemInfo(&sysInfo);
+    s_maxNumberOfParallelThreads = sysInfo.dwNumberOfProcessors;
+#else
+    s_maxNumberOfParallelThreads = defaultIfUnavailable;
+#endif
+}
+
 bool ParallelEnvironment::ThreadPrivate::tryLockFor(ParallelEnvironment* parent)
 {
     bool locked = m_mutex.tryLock();

Modified: trunk/Source/_javascript_Core/wtf/ParallelJobsGeneric.h (97730 => 97731)


--- trunk/Source/_javascript_Core/wtf/ParallelJobsGeneric.h	2011-10-18 09:11:48 UTC (rev 97730)
+++ trunk/Source/_javascript_Core/wtf/ParallelJobsGeneric.h	2011-10-18 09:18:18 UTC (rev 97731)
@@ -35,27 +35,30 @@
 
 namespace WTF {
 
-static const unsigned int maxParallelThreads = 2;
-
 class ParallelEnvironment {
     WTF_MAKE_FAST_ALLOCATED;
 public:
     typedef void (*ThreadFunction)(void*);
 
-    ParallelEnvironment(ThreadFunction threadFunction, size_t sizeOfParameter, unsigned int requestedJobNumber) :
+    ParallelEnvironment(ThreadFunction threadFunction, size_t sizeOfParameter, int requestedJobNumber) :
         m_threadFunction(threadFunction),
         m_sizeOfParameter(sizeOfParameter)
     {
-        if (!requestedJobNumber || requestedJobNumber > maxParallelThreads)
-            requestedJobNumber = maxParallelThreads;
+        ASSERT_ARG(requestedJobNumber, requestedJobNumber >= 1);
 
+        if (s_maxNumberOfParallelThreads == -1)
+            determineMaxNumberOfParallelThreads();
+
+        if (!requestedJobNumber || requestedJobNumber > s_maxNumberOfParallelThreads)
+            requestedJobNumber = static_cast<unsigned>(s_maxNumberOfParallelThreads);
+
         if (!s_threadPool)
             s_threadPool = new Vector< RefPtr<ThreadPrivate> >();
 
         // The main thread should be also a worker.
-        unsigned int maxNewThreads = requestedJobNumber - 1;
+        int maxNumberOfNewThreads = requestedJobNumber - 1;
 
-        for (unsigned int i = 0; i < maxParallelThreads && m_threads.size() < maxNewThreads; ++i) {
+        for (int i = 0; i < s_maxNumberOfParallelThreads && m_threads.size() < maxNumberOfNewThreads; ++i) {
             if (s_threadPool->size() < i + 1)
                 s_threadPool->append(ThreadPrivate::create());
 
@@ -122,12 +125,15 @@
     };
 
 private:
+    static void determineMaxNumberOfParallelThreads();
+
     ThreadFunction m_threadFunction;
     size_t m_sizeOfParameter;
     int m_numberOfJobs;
 
     Vector< RefPtr<ThreadPrivate> > m_threads;
     static Vector< RefPtr<ThreadPrivate> >* s_threadPool;
+    static int s_maxNumberOfParallelThreads;
 };
 
 } // namespace WTF
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to