Add -j to dex2oat.

Change-Id: I80f3f2605b8afcdf9adf9382716b041f10efa104
diff --git a/src/dex2oat.cc b/src/dex2oat.cc
index 25c1d82..f78579f 100644
--- a/src/dex2oat.cc
+++ b/src/dex2oat.cc
@@ -100,18 +100,17 @@
 class Dex2Oat {
  public:
 
-  static Dex2Oat* Create(Runtime::Options& options) {
+  static Dex2Oat* Create(Runtime::Options& options, size_t thread_count) {
     UniquePtr<Runtime> runtime(CreateRuntime(options));
     if (runtime.get() == NULL) {
       return NULL;
     }
-    return new Dex2Oat(runtime.release());
+    return new Dex2Oat(runtime.release(), thread_count);
   }
 
   ~Dex2Oat() {
     delete runtime_;
-    const size_t thread_count = static_cast<size_t>(sysconf(_SC_NPROCESSORS_ONLN));
-    LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count << ")";
+    LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
   }
 
   // Make a list of descriptors for classes to include in the image
@@ -199,7 +198,7 @@
       class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
     }
 
-    Compiler compiler(instruction_set_, image, image_classes);
+    Compiler compiler(instruction_set_, image, thread_count_, image_classes);
     compiler.CompileAll(class_loader->get(), dex_files);
 
     if (!OatWriter::Create(oat_file, class_loader->get(), dex_files, compiler)) {
@@ -240,7 +239,8 @@
 
  private:
 
-  explicit Dex2Oat(Runtime* runtime) : runtime_(runtime), start_ns_(NanoTime()) {
+  explicit Dex2Oat(Runtime* runtime, size_t thread_count)
+      : runtime_(runtime), thread_count_(thread_count), start_ns_(NanoTime()) {
   }
 
   static Runtime* CreateRuntime(Runtime::Options& options) {
@@ -364,6 +364,7 @@
   static const InstructionSet instruction_set_ = kThumb2;
 
   Runtime* runtime_;
+  size_t thread_count_;
   uint64_t start_ns_;
 
   DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
@@ -416,6 +417,7 @@
   uintptr_t image_base = 0;
   std::string host_prefix;
   std::vector<const char*> runtime_args;
+  int thread_count = 2;
 
   for (int i = 0; i < argc; i++) {
     const StringPiece option(argv[i]);
@@ -441,6 +443,12 @@
         fprintf(stderr, "could not parse --oat-fd argument '%s' as an integer\n", oat_fd_str);
         usage();
       }
+    } else if (option.starts_with("-j")) {
+      const char* thread_count_str = option.substr(strlen("-j")).data();
+      if (!ParseInt(thread_count_str, &thread_count)) {
+        fprintf(stderr, "could not parse -j argument '%s' as an integer\n", thread_count_str);
+        usage();
+      }
     } else if (option.starts_with("--oat-name=")) {
       oat_name = option.substr(strlen("--oat-name=")).data();
     } else if (option.starts_with("--image=")) {
@@ -595,7 +603,7 @@
     options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
   }
 
-  UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options));
+  UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, thread_count));
 
   // If --image-classes was specified, calculate the full list of classes to include in the image
   UniquePtr<const std::set<std::string> > image_classes(NULL);