Change spec to launch process using new API

Also init gflags in main

Change-Id: I3127ed6b63f79da63f2c5529fcfc7cebe640b2da
diff --git a/benchmarks/BUILD.gn b/benchmarks/BUILD.gn
index 01c729f..7b61a32 100644
--- a/benchmarks/BUILD.gn
+++ b/benchmarks/BUILD.gn
@@ -17,6 +17,7 @@
   deps = [
     ":spec_utils",
     "//third_party/benchmark",
+    "//third_party/gflags",
     "//third_party/glog",
   ]
 }
diff --git a/benchmarks/main.cc b/benchmarks/main.cc
index e6714f1..eef73ef 100644
--- a/benchmarks/main.cc
+++ b/benchmarks/main.cc
@@ -4,12 +4,13 @@
 
 #include <benchmark/benchmark.h>
 #include <fstream>
+#include <gflags/gflags.h>
+#include <glog/logging.h>
 #include <libgen.h>
 #include <spec_fixture.h>
 #include <spec_utils.h>
 #include <sys/stat.h>
 #include <unistd.h>
-#include <glog/logging.h>
 
 namespace {
 
@@ -260,6 +261,7 @@
 int main(int argc, char* argv[]) {
   ::benchmark::Initialize(&argc, argv);
   google::InitGoogleLogging(argv[0]);
+  gflags::ParseCommandLineFlags(&argc, &argv, true);
   char* cwd;
   CHECK_NOTNULL((cwd = get_current_dir_name()));
   executableDir = std::string(cwd) + "/" + dirname(argv[0]);
diff --git a/benchmarks/spec_fixture.cc b/benchmarks/spec_fixture.cc
index ffb9c02..c047fb3 100644
--- a/benchmarks/spec_fixture.cc
+++ b/benchmarks/spec_fixture.cc
@@ -50,8 +50,8 @@
   run_dir_name = tmpDir + "/" + benchmark_name;
 
   DLOG(INFO) << "Changing dir to: " << executableDir.c_str();
-  CHECK_EQ(chdir(executableDir.c_str()), 0) << "Not able to change dir to "
-                                            << executableDir;
+  CHECK_EQ(chdir(executableDir.c_str()), 0)
+      << "Not able to change dir to " << executableDir;
   CHECK_EQ(mkdir(run_dir_name.c_str(), 0700), 0)
       << "Not able to create dir: " << run_dir_name
       << ", Error: " << strerror(errno);
@@ -69,9 +69,9 @@
 }
 
 void SpecFixture::TearDown(benchmark::State& st) {
-  CHECK_EQ(DeleteDir(run_dir_name), 0) << "Error Deleting directory "
-                                       << run_dir_name
-                                       << ", Error: " << strerror(errno);
+  CHECK_EQ(DeleteDir(run_dir_name), 0)
+      << "Error Deleting directory " << run_dir_name
+      << ", Error: " << strerror(errno);
 }
 
 int SpecFixture::RunSpec(const char* args[],
@@ -129,7 +129,7 @@
   delete[] argv;
   if (status != 0) {
     // Dump output
-    LOG(ERROR) << "Error while running benchmark";
+    LOG(ERROR) << "Error while running benchmark: " << benchmark_name;
     ssize_t len;
     do {
       len = read(out_p[0], buffer, page_size);
@@ -162,45 +162,26 @@
   launchpad_t* lp = NULL;
   mx_handle_t job_to_child = MX_HANDLE_INVALID;
   mx_handle_t job = mxio_get_startup_handle(MX_HND_INFO(MX_HND_TYPE_JOB, 0));
-  if (job != MX_HANDLE_INVALID)
+  if (job != MX_HANDLE_INVALID) {
     mx_handle_duplicate(job, MX_RIGHT_SAME_RIGHTS, &job_to_child);
-  mx_status_t status = launchpad_create(job_to_child, filename, &lp);
-  if (status != NO_ERROR)
-    return status;
-  ScopedGuard guard([&]() { launchpad_destroy(lp); });
-  status = launchpad_elf_load(lp, launchpad_vmo_from_file(filename));
-  if (status != NO_ERROR)
-    return status;
-  status = launchpad_load_vdso(lp, MX_HANDLE_INVALID);
-  if (status != NO_ERROR)
-    return status;
-  status = launchpad_add_vdso_vmo(lp);
-  if (status != NO_ERROR)
-    return status;
-  status = launchpad_arguments(lp, argc, argv);
-  if (status != NO_ERROR)
-    return status;
-  status = launchpad_environ(lp, (const char**)environ);
-  if (status != NO_ERROR)
-    return status;
-  status = launchpad_clone_mxio_root(lp);
-  if (status != NO_ERROR)
-    return status;
-  status = launchpad_clone_mxio_cwd(lp);
-  if (status != NO_ERROR)
-    return status;
+  }
+  launchpad_create(job_to_child, filename, &lp);
+  launchpad_load_from_file(lp, filename);
+  launchpad_set_args(lp, argc, argv);
+  launchpad_set_environ(lp, (const char**)environ);
+  launchpad_clone_mxio_root(lp);
+  launchpad_clone_mxio_cwd(lp);
   launchpad_clone_fd(lp, STDIN_FILENO, STDIN_FILENO);
   launchpad_clone_fd(lp, STDOUT_FILENO, STDOUT_FILENO);
   launchpad_clone_fd(lp, STDERR_FILENO, STDERR_FILENO);
-  if (status == NO_ERROR && input != -1) {
-    status = launchpad_transfer_fd(lp, input, STDIN_FILENO);
+  if(input != -1) {
+    launchpad_transfer_fd(lp, input, STDIN_FILENO);
   }
+  const char* errmsg;
+  mx_status_t status = launchpad_go(lp, process, &errmsg);
   if (status == NO_ERROR) {
-    mx_handle_t result = launchpad_start(lp);
-    if (result > 0)
-      *process = result;
-    else
-      status = result;
+    LOG(ERROR) << "Failed to launch: " << filename << ", error: " << errmsg
+               << ": " << status;
   }
   return status;
 }
@@ -224,8 +205,7 @@
   });
   mx_status_t status = launch(args[0], args_length, args, input, &handle);
   if (status != NO_ERROR) {
-    LOG(ERROR) << "Failed to launch " << args[0] << ":" << status;
-    return handle;
+    return status;
   }
   status =
       mx_handle_wait_one(handle, MX_PROCESS_SIGNALED, MX_TIME_INFINITE, NULL);