Add thread-per-core setting to SkThreadPool.
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 22 Apr 2013 15:23:14 +0000 (15:23 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 22 Apr 2013 15:23:14 +0000 (15:23 +0000)
BUG=
R=scroggo@google.com, caryclark@google.com

Author: mtklein@google.com

Review URL: https://chromiumcodereview.appspot.com/13855009

git-svn-id: http://skia.googlecode.com/svn/trunk@8802 2bbb7eff-a529-9590-31e7-b0007b416f81

include/utils/SkThreadPool.h
src/utils/SkThreadPool.cpp
tests/PathOpsExtendedTest.cpp
tests/skia_test.cpp

index cc45fc2..3c86158 100644 (file)
@@ -19,8 +19,9 @@ class SkThreadPool {
 
 public:
     /**
-     * Create a threadpool with exactly count (>=0) threads.
+     * Create a threadpool with count threads, or one thread per core if kThreadPerCore.
      */
+    static const int kThreadPerCore = -1;
     explicit SkThreadPool(int count);
     ~SkThreadPool();
 
index 78cb417..5fe1025 100644 (file)
@@ -5,12 +5,31 @@
  * found in the LICENSE file.
  */
 
-#include "SkThreadPool.h"
 #include "SkRunnable.h"
+#include "SkThreadPool.h"
 #include "SkThreadUtils.h"
+#include "SkTypes.h"
+
+#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_ANDROID)
+#include <unistd.h>
+#endif
+
+// Returns the number of cores on this machine.
+static int num_cores() {
+#if defined(SK_BUILD_FOR_WIN32)
+    SYSTEM_INFO sysinfo;
+    GetSystemInfo(&sysinfo);
+    return sysinfo.dwNumberOfProcessors;
+#elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_ANDROID)
+    return sysconf(_SC_NPROCESSORS_ONLN);
+#else
+    return 1;
+#endif
+}
 
-SkThreadPool::SkThreadPool(const int count)
+SkThreadPool::SkThreadPool(int count)
 : fDone(false) {
+    if (count < 0) count = num_cores();
     // Create count threads, all running SkThreadPool::Loop.
     for (int i = 0; i < count; i++) {
         SkThread* thread = SkNEW_ARGS(SkThread, (&SkThreadPool::Loop, this));
index 675918f..c5dbceb 100644 (file)
@@ -513,9 +513,6 @@ bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
     return result == 0;
 }
 
-const int maxThreadsAllocated = 64;
-static int maxThreads = 1;
-
 int initializeTests(skiatest::Reporter* reporter, const char* test) {
 #ifdef SK_DEBUG
     gDebugMaxWindSum = 4;
@@ -523,18 +520,6 @@ int initializeTests(skiatest::Reporter* reporter, const char* test) {
 #endif
     testName = test;
     size_t testNameSize = strlen(test);
-    if (reporter->allowThreaded()) {
-        int threads = -1;
-#ifdef SK_BUILD_FOR_MAC
-        size_t size = sizeof(threads);
-        sysctlbyname("hw.logicalcpu_max", &threads, &size, NULL, 0);
-#endif
-        if (threads > 0) {
-            maxThreads = threads;
-        } else {
-            maxThreads = 16;
-        }
-    }
     SkFILEStream inFile("../../experimental/Intersection/op.htm");
     if (inFile.isValid()) {
         SkTDArray<char> inData;
@@ -549,7 +534,7 @@ int initializeTests(skiatest::Reporter* reporter, const char* test) {
             testNumber = atoi(numLoc) + 1;
         }
     }
-    return maxThreads;
+    return reporter->allowThreaded() ? SkThreadPool::kThreadPerCore : 0;
 }
 
 void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
index 6330b32..20527b0 100644 (file)
@@ -141,8 +141,8 @@ DEFINE_string2(resourcePath, i, NULL, "directory for test resources.");
 DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
 DEFINE_bool2(threaded, z, false, "allow tests to use multiple threads internally.");
 DEFINE_bool2(verbose, v, false, "enable verbose output.");
-DEFINE_int32(threads, 8,
-             "If >0, run threadsafe tests on a threadpool with this many threads.");
+DEFINE_int32(threads, SkThreadPool::kThreadPerCore,
+             "Run threadsafe tests on a threadpool with this many threads.");
 
 // Deletes self when run.
 class SkTestRunnable : public SkRunnable {