Simplify aos::Init

Most of the variants were to deal with core, which has been gone for a
year.  Simplify those all into InitGoogle.

InitGoogle should be renamed to Init at some point.  It does everything
now.

Change-Id: I738ee03e9c5b2e6348ef33302835f915df68011f
diff --git a/aos/events/BUILD b/aos/events/BUILD
index b4c2385..c408b7b 100644
--- a/aos/events/BUILD
+++ b/aos/events/BUILD
@@ -234,6 +234,7 @@
         ":event_loop_fbs",
         ":test_message_fbs",
         ":timing_statistics",
+        "//aos:init",
         "//aos:realtime",
         "//aos/ipc_lib:lockless_queue",
         "//aos/ipc_lib:signalfd",
@@ -311,6 +312,7 @@
         ":aos_logging",
         ":event_loop",
         ":simple_channel",
+        "//aos:init",
         "//aos:realtime",
         "//aos/events/logging:logger_fbs",
         "//aos/ipc_lib:index",
diff --git a/aos/events/shm_event_loop.cc b/aos/events/shm_event_loop.cc
index 6e152cc..fed5f4c 100644
--- a/aos/events/shm_event_loop.cc
+++ b/aos/events/shm_event_loop.cc
@@ -17,6 +17,7 @@
 #include "aos/events/epoll.h"
 #include "aos/events/event_loop_generated.h"
 #include "aos/events/timing_statistics.h"
+#include "aos/init.h"
 #include "aos/ipc_lib/lockless_queue.h"
 #include "aos/ipc_lib/signalfd.h"
 #include "aos/realtime.h"
@@ -225,6 +226,7 @@
       shm_base_(FLAGS_shm_base),
       name_(FLAGS_application_name),
       node_(MaybeMyNode(configuration)) {
+  CHECK(IsInitialized()) << ": Need to initialize AOS first.";
   if (configuration->has_nodes()) {
     CHECK(node_ != nullptr) << ": Couldn't find node in config.";
   }
diff --git a/aos/events/simulated_event_loop.cc b/aos/events/simulated_event_loop.cc
index bc15643..0b919e4 100644
--- a/aos/events/simulated_event_loop.cc
+++ b/aos/events/simulated_event_loop.cc
@@ -8,6 +8,7 @@
 #include "absl/container/btree_map.h"
 #include "aos/events/aos_logging.h"
 #include "aos/events/simulated_network_bridge.h"
+#include "aos/init.h"
 #include "aos/json_to_flatbuffer.h"
 #include "aos/realtime.h"
 #include "aos/util/phased_loop.h"
@@ -969,6 +970,7 @@
     const Configuration *configuration)
     : configuration_(CHECK_NOTNULL(configuration)),
       nodes_(configuration::GetNodes(configuration_)) {
+  CHECK(IsInitialized()) << ": Need to initialize AOS first.";
   for (const Node *node : nodes_) {
     node_factories_.emplace_back(new NodeEventLoopFactory(
         &scheduler_scheduler_, this, node, &raw_event_loops_));
diff --git a/aos/init.cc b/aos/init.cc
index 71eeabd..b4036a3 100644
--- a/aos/init.cc
+++ b/aos/init.cc
@@ -9,101 +9,33 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <stdlib.h>
-#include <stdint.h>
-#include <sys/prctl.h>
-#include <malloc.h>
 
-#include "aos/die.h"
-#include "aos/logging/implementations.h"
 #include "aos/realtime.h"
-
-namespace FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead {
-extern double FLAGS_tcmalloc_release_rate __attribute__((weak));
-}
-using FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead::
-    FLAGS_tcmalloc_release_rate;
+#include "gflags/gflags.h"
+#include "glog/logging.h"
 
 DEFINE_bool(coredump, false, "If true, write core dumps on failure.");
 
 namespace aos {
-namespace logging {
-namespace internal {
-
-// Implemented in aos/logging/context.cc.
-void ReloadThreadName();
-
-}  // namespace internal
-}  // namespace logging
 namespace {
-
-// Common stuff that needs to happen at the beginning of both the realtime and
-// non-realtime initialization sequences. May be called twice.
-void InitStart() {
-  RegisterMallocHook();
-  if (FLAGS_coredump) {
-    WriteCoreDumps();
-  }
-  google::InstallFailureSignalHandler();
-}
-
-const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME";
-
-bool ShouldBeRealtime() {
-  return getenv(kNoRealtimeEnvironmentVariable) == nullptr;
-}
-
+bool initialized = false;
 }  // namespace
 
+bool IsInitialized() { return initialized; }
+
 void InitGoogle(int *argc, char ***argv) {
+  CHECK(!IsInitialized()) << "Only initialize once.";
   FLAGS_logtostderr = true;
   google::InitGoogleLogging((*argv)[0]);
   gflags::ParseCommandLineFlags(argc, argv, true);
   google::InstallFailureSignalHandler();
 
-  RegisterMallocHook();
-}
-
-void InitNRT() {
-  InitStart();
-  ExpandStackSize();
-}
-
-void InitCreate() {
-  InitStart();
-  AOS_LOG(INFO, "%s created shm\n", program_invocation_short_name);
-}
-
-void Init(int relative_priority) {
-  InitStart();
-  GoRT(relative_priority);
-}
-
-void GoRT(int relative_priority) {
-  if (ShouldBeRealtime()) {
-    InitRT();
-
-    // Set our process to the appropriate priority.
-    struct sched_param param;
-    param.sched_priority = 30 + relative_priority;
-    if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
-      PDie("%s-init: setting SCHED_FIFO failed", program_invocation_short_name);
-    }
-  } else {
-    fprintf(stderr,
-            "%s not doing realtime initialization because environment"
-            " variable %s is set\n",
-            program_invocation_short_name, kNoRealtimeEnvironmentVariable);
-    printf("no realtime for %s. see stderr\n", program_invocation_short_name);
+  if (FLAGS_coredump) {
+    WriteCoreDumps();
   }
 
-  AOS_LOG(INFO, "%s initialized realtime\n", program_invocation_short_name);
-}
-
-void PinCurrentThreadToCPU(int number) {
-  cpu_set_t cpuset;
-  CPU_ZERO(&cpuset);
-  CPU_SET(number, &cpuset);
-  AOS_PRCHECK(pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset));
+  RegisterMallocHook();
+  initialized = true;
 }
 
 }  // namespace aos
diff --git a/aos/init.h b/aos/init.h
index 98bc266..1e2b7a4 100644
--- a/aos/init.h
+++ b/aos/init.h
@@ -1,34 +1,14 @@
 #ifndef AOS_INIT_H_
 #define AOS_INIT_H_
 
-#include <string>
-
 namespace aos {
 
-// TODO(james): Clean up/unify the various init functions.
-
-// Initializes glog and gflags.
+// Initializes AOS.
 void InitGoogle(int *argc, char ***argv);
 
-// In order to use shared memory, one of the Init* functions must be called in
-// exactly 1 thread per process. It is OK to keep going without calling one of
-// them again after fork(2)ing.
-
-// Does the non-realtime parts of the initialization process.
-void InitNRT();
-// Initializes everything, including the realtime stuff.
-// relative_priority adjusts the priority of this process relative to all of the
-// other ones (positive for higher priority).
-void Init(int relative_priority = 0);
-// Same as InitNRT, except will remove an existing shared memory file and create
-// a new one.
-void InitCreate();
-
-// Performs the realtime parts of initialization after InitNRT() has been called.
-void GoRT(int relative_priority = 0);
-
-// Pins the current thread to CPU #number.
-void PinCurrentThreadToCPU(int number);
+// Returns true if we have been initialized.  This is mostly here so
+// ShmEventLoop can confirm the world was initialized before running.
+bool IsInitialized();
 
 }  // namespace aos
 
diff --git a/aos/input/joystick_input.h b/aos/input/joystick_input.h
index 7a6e8c3..aceda52 100644
--- a/aos/input/joystick_input.h
+++ b/aos/input/joystick_input.h
@@ -22,7 +22,7 @@
         "/aos", [this](const ::aos::JoystickState &joystick_state) {
           this->HandleData(&joystick_state);
         });
-    event_loop->SetRuntimeRealtimePriority(29);
+    event_loop->SetRuntimeRealtimePriority(28);
   }
 
  protected:
diff --git a/aos/ipc_lib/eventfd_latency.cc b/aos/ipc_lib/eventfd_latency.cc
index d146c13..47f6528 100644
--- a/aos/ipc_lib/eventfd_latency.cc
+++ b/aos/ipc_lib/eventfd_latency.cc
@@ -43,7 +43,13 @@
   // Sleep between 1 and 15 ms.
   ::std::uniform_int_distribution<> distribution(1000, 15000);
 
-  PinCurrentThreadToCPU(FLAGS_core);
+  {
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(FLAGS_core, &cpuset);
+
+    SetCurrentThreadAffinity(cpuset);
+  }
   SetCurrentThreadRealtimePriority(FLAGS_sender_priority);
   while (true) {
     const monotonic_clock::time_point wakeup_time =
@@ -117,7 +123,13 @@
     }
   });
 
-  PinCurrentThreadToCPU(FLAGS_core);
+  {
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(FLAGS_core, &cpuset);
+
+    SetCurrentThreadAffinity(cpuset);
+  }
   SetCurrentThreadRealtimePriority(FLAGS_receiver_priority);
   epoll.Run();
   UnsetCurrentThreadRealtimePriority();
diff --git a/aos/ipc_lib/futex_latency.cc b/aos/ipc_lib/futex_latency.cc
index ef27e29..3d609f8 100644
--- a/aos/ipc_lib/futex_latency.cc
+++ b/aos/ipc_lib/futex_latency.cc
@@ -54,7 +54,13 @@
   // Sleep between 1 and 15 ms.
   ::std::uniform_int_distribution<> distribution(1000, 15000);
 
-  PinCurrentThreadToCPU(FLAGS_core);
+  {
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(FLAGS_core, &cpuset);
+
+    SetCurrentThreadAffinity(cpuset);
+  }
   SetCurrentThreadRealtimePriority(FLAGS_sender_priority);
   while (true) {
     const monotonic_clock::time_point wakeup_time =
@@ -91,7 +97,13 @@
   chrono::nanoseconds sum_latency = chrono::nanoseconds(0);
   int latency_count = 0;
 
-  PinCurrentThreadToCPU(FLAGS_core);
+  {
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(FLAGS_core, &cpuset);
+
+    SetCurrentThreadAffinity(cpuset);
+  }
   SetCurrentThreadRealtimePriority(FLAGS_receiver_priority);
   while (true) {
     chrono::nanoseconds wakeup_latency;
diff --git a/aos/ipc_lib/ipc_comparison.cc b/aos/ipc_lib/ipc_comparison.cc
index 6435ee7..f5614a6 100644
--- a/aos/ipc_lib/ipc_comparison.cc
+++ b/aos/ipc_lib/ipc_comparison.cc
@@ -811,7 +811,13 @@
     if (FLAGS_server_priority > 0) {
       SetCurrentThreadRealtimePriority(FLAGS_server_priority);
     }
-    PinCurrentThreadToCPU(FLAGS_server_cpu);
+    {
+      cpu_set_t cpuset;
+      CPU_ZERO(&cpuset);
+      CPU_SET(FLAGS_server_cpu, &cpuset);
+
+      SetCurrentThreadAffinity(cpuset);
+    }
 
     while (!done) {
       const PingPongerInterface::Data *data = ping_ponger->Wait();
@@ -826,7 +832,13 @@
   if (FLAGS_client_priority > 0) {
     SetCurrentThreadRealtimePriority(FLAGS_client_priority);
   }
-  PinCurrentThreadToCPU(FLAGS_client_cpu);
+  {
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(FLAGS_client_cpu, &cpuset);
+
+    SetCurrentThreadAffinity(cpuset);
+  }
 
   // Warm everything up.
   for (int i = 0; i < 1000; ++i) {
@@ -899,9 +911,7 @@
       "\ttcp\n"
       "\ttcp_nodelay\n"
       "\tudp\n");
-  ::gflags::ParseCommandLineFlags(&argc, &argv, true);
-
-  ::aos::InitNRT();
+  ::aos::InitGoogle(&argc, &argv);
 
   return ::aos::Main(argc, argv);
 }
diff --git a/aos/ipc_lib/named_pipe_latency.cc b/aos/ipc_lib/named_pipe_latency.cc
index 2bd24f3..030852c 100644
--- a/aos/ipc_lib/named_pipe_latency.cc
+++ b/aos/ipc_lib/named_pipe_latency.cc
@@ -47,7 +47,13 @@
   // Sleep between 1 and 15 ms.
   ::std::uniform_int_distribution<> distribution(1000, 15000);
 
-  PinCurrentThreadToCPU(FLAGS_core);
+  {
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(FLAGS_core, &cpuset);
+
+    SetCurrentThreadAffinity(cpuset);
+  }
   SetCurrentThreadRealtimePriority(FLAGS_sender_priority);
   while (true) {
     const monotonic_clock::time_point wakeup_time =
@@ -125,7 +131,13 @@
     }
   });
 
-  PinCurrentThreadToCPU(FLAGS_core);
+  {
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(FLAGS_core, &cpuset);
+
+    SetCurrentThreadAffinity(cpuset);
+  }
   SetCurrentThreadRealtimePriority(FLAGS_receiver_priority);
   epoll.Run();
   UnsetCurrentThreadRealtimePriority();
diff --git a/aos/ipc_lib/signal_stress.cc b/aos/ipc_lib/signal_stress.cc
index 60243fd..a9fdb94 100644
--- a/aos/ipc_lib/signal_stress.cc
+++ b/aos/ipc_lib/signal_stress.cc
@@ -56,7 +56,13 @@
   }
   AOS_LOG(INFO, "Current PID: %d\n", pid);
 
-  PinCurrentThreadToCPU(FLAGS_core);
+  {
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(FLAGS_core, &cpuset);
+
+    SetCurrentThreadAffinity(cpuset);
+  }
   SetCurrentThreadRealtimePriority(FLAGS_sender_priority);
   while (true) {
     const monotonic_clock::time_point wakeup_time =
@@ -145,7 +151,13 @@
     }
   });
 
-  PinCurrentThreadToCPU(FLAGS_core);
+  {
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(FLAGS_core, &cpuset);
+
+    SetCurrentThreadAffinity(cpuset);
+  }
   SetCurrentThreadRealtimePriority(FLAGS_receiver_priority);
   epoll.Run();
   UnsetCurrentThreadRealtimePriority();
diff --git a/aos/network/web_proxy_main.cc b/aos/network/web_proxy_main.cc
index 2d8b97b..78528ea 100644
--- a/aos/network/web_proxy_main.cc
+++ b/aos/network/web_proxy_main.cc
@@ -17,8 +17,6 @@
   // Make sure to reference this to force the linker to include it.
   findEmbeddedContent("");
 
-  aos::InitNRT();
-
   aos::FlatbufferDetachedBuffer<aos::Configuration> config =
       aos::configuration::ReadConfig(FLAGS_config);
 
diff --git a/aos/realtime.h b/aos/realtime.h
index bb5ea84..c695add 100644
--- a/aos/realtime.h
+++ b/aos/realtime.h
@@ -13,20 +13,6 @@
 // your thread RT.  Called as part of ShmEventLoop::Run()
 void InitRT();
 
-// Sets the current thread back down to non-realtime priority.
-void UnsetCurrentThreadRealtimePriority();
-
-// Sets the name of the current thread.
-// This will displayed by `top -H`, dump_rtprio, and show up in logs.
-// name can have a maximum of 16 characters.
-void SetCurrentThreadName(const std::string_view name);
-
-// Sets the current thread's realtime priority.
-void SetCurrentThreadRealtimePriority(int priority);
-
-// Sets the current thread's scheduling affinity.
-void SetCurrentThreadAffinity(const cpu_set_t &cpuset);
-
 // Sets up this process to write core dump files.
 // This is called by Init*, but it's here for other files that want this
 // behavior without calling Init*.
@@ -36,6 +22,23 @@
 
 void ExpandStackSize();
 
+// Sets the name of the current thread.
+// This will displayed by `top -H`, dump_rtprio, and show up in logs.
+// name can have a maximum of 16 characters.
+void SetCurrentThreadName(const std::string_view name);
+
+// Sets the current thread's scheduling affinity.
+void SetCurrentThreadAffinity(const cpu_set_t &cpuset);
+
+// Everything below here needs AOS to be initialized before it will work
+// properly.
+
+// Sets the current thread's realtime priority.
+void SetCurrentThreadRealtimePriority(int priority);
+
+// Sets the current thread back down to non-realtime priority.
+void UnsetCurrentThreadRealtimePriority();
+
 // Registers our hooks which crash on RT malloc.
 void RegisterMallocHook();
 
diff --git a/aos/starter/starter.cc b/aos/starter/starter.cc
index 9e149c0..340bbeb 100644
--- a/aos/starter/starter.cc
+++ b/aos/starter/starter.cc
@@ -770,9 +770,6 @@
 // This is the callback for when core creates the file indicating that it has
 // started.
 void Run() {
-  // It's safe now because core is up.
-  aos::InitNRT();
-
   std::ifstream list_file(child_list_file);
 
   while (true) {
@@ -806,6 +803,8 @@
 }  // namespace aos
 
 int main(int argc, char *argv[]) {
+  ::aos::InitGoogle(&argc, &argv);
+
   if (argc != 2) {
     aos::starter::PrintHelp();
     exit(EXIT_FAILURE);
diff --git a/aos/starter/starter_cmd.cc b/aos/starter/starter_cmd.cc
index abc2816..d076afc 100644
--- a/aos/starter/starter_cmd.cc
+++ b/aos/starter/starter_cmd.cc
@@ -16,7 +16,6 @@
 
 int main(int argc, char **argv) {
   aos::InitGoogle(&argc, &argv);
-  aos::InitNRT();
 
   CHECK(argc == 3) << "Invalid number of command arguments";