Add tools/busy_threads for spawning N busy-wait threads for debugging

Change-Id: I7deec1f6499f32077bc6db1b4a3afa01785617fe
diff --git a/BUILD.gn b/BUILD.gn
index ee9ed13..3b5e5e7 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -73,6 +73,7 @@
       ]
       if (is_linux || is_android) {
         deps += [ "tools/skippy" ]
+        deps += [ "tools/busy_threads" ]
       }
       if (is_fuzzer) {
         deps += [ ":fuzzers" ]
diff --git a/tools/busy_threads/BUILD.gn b/tools/busy_threads/BUILD.gn
new file mode 100644
index 0000000..0f33776
--- /dev/null
+++ b/tools/busy_threads/BUILD.gn
@@ -0,0 +1,24 @@
+# Copyright (C) 2019 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+executable("busy_threads") {
+  testonly = true
+  deps = [
+    "../../gn:default_deps",
+    "../../src/base",
+  ]
+  sources = [
+    "busy_threads.cc",
+  ]
+}
diff --git a/tools/busy_threads/busy_threads.cc b/tools/busy_threads/busy_threads.cc
new file mode 100644
index 0000000..03677cd
--- /dev/null
+++ b/tools/busy_threads/busy_threads.cc
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <getopt.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include <thread>
+
+#include "perfetto/base/logging.h"
+#include "perfetto/base/time.h"
+
+// Spawns the requested number threads that busy-wait for a fixed duration, and
+// then yield.
+
+namespace perfetto {
+namespace {
+
+constexpr int64_t kBusyWaitPeriodNs = 1000 * 1000;
+
+__attribute__((noreturn)) void BusyWait() {
+  while (1) {
+    base::TimeNanos start = base::GetWallTimeNs();
+    while ((base::GetWallTimeNs() - start).count() < kBusyWaitPeriodNs) {
+      for (int i = 0; i < 10000; i++) {
+        asm volatile("" ::: "memory");
+      }
+    }
+    std::this_thread::yield();
+  }
+}
+
+int BusyThreadsMain(int argc, char** argv) {
+  int num_threads = -1;
+
+  static struct option long_options[] = {
+      {"threads", required_argument, nullptr, 't'}, {nullptr, 0, nullptr, 0}};
+  int option_index;
+  int c;
+  while ((c = getopt_long(argc, argv, "", long_options, &option_index)) != -1) {
+    switch (c) {
+      case 't':
+        num_threads = atoi(optarg);
+        break;
+      default:
+        break;
+    }
+  }
+  if (num_threads == -1) {
+    PERFETTO_ELOG("Usage: %s [--threads=N]", argv[0]);
+    return 1;
+  }
+
+  PERFETTO_LOG("Spawning %d threads.", num_threads);
+  for (int i = 0; i < num_threads; i++) {
+    std::thread th(BusyWait);
+    th.detach();
+  }
+  PERFETTO_LOG("Threads spawned, Ctrl-C to stop.");
+  while (sleep(600))
+    ;
+
+  return 0;
+}
+
+}  // namespace
+}  // namespace perfetto
+
+int main(int argc, char** argv) {
+  return perfetto::BusyThreadsMain(argc, argv);
+}