]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
[pzstd] Smart default # of threads (#331)
authorNick Terrell <terrelln@fb.com>
Tue, 6 Sep 2016 19:40:59 +0000 (12:40 -0700)
committerNick Terrell <terrelln@fb.com>
Tue, 6 Sep 2016 19:41:36 +0000 (12:41 -0700)
contrib/pzstd/Options.cpp
contrib/pzstd/test/OptionsTest.cpp

index 61613099691db551a9e1112dabc1e71e56c7b769..122f4fb3662a33b7f33c183cf3557c9f1e65afdb 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <cstdio>
 #include <cstring>
+#include <thread>
 
 namespace pzstd {
 
@@ -103,6 +104,7 @@ bool Options::parse(int argc, const char** argv) {
         numThreads = parseUnsigned(argv[i]);
         if (numThreads == 0) {
           std::fprintf(stderr, "Invalid argument: # of threads must be > 0.\n");
+          return false;
         }
         break;
       case 'p':
@@ -169,12 +171,17 @@ bool Options::parse(int argc, const char** argv) {
     if (compressionLevel > maxCLevel) {
       std::fprintf(
           stderr, "Invalid compression level %u.\n", compressionLevel);
+      return false;
     }
   }
   // Check that numThreads is set
   if (numThreads == 0) {
-    std::fprintf(stderr, "Invalid arguments: # of threads not specified.\n");
-    return false;
+    numThreads = std::thread::hardware_concurrency();
+    if (numThreads == 0) {
+      std::fprintf(stderr, "Invalid arguments: # of threads not specified "
+                           "and unable to determine hardware concurrency.\n");
+      return false;
+    }
   }
   return true;
 }
index 87e79d59ebe42694067e1494a520ce79878a1263..b87358c04e7867632ad607644800b2635dffc594 100644 (file)
@@ -118,11 +118,11 @@ TEST(Options, ValidInputs) {
   }
 }
 
-TEST(Options, BadNumThreads) {
+TEST(Options, NumThreads) {
   {
     Options options;
     std::array<const char*, 3> args = {{nullptr, "-o", "-"}};
-    EXPECT_FALSE(options.parse(args.size(), args.data()));
+    EXPECT_TRUE(options.parse(args.size(), args.data()));
   }
   {
     Options options;