Do superstructure initialization before going realtime
It does a lot of math in the constructor, which eventually runs into the
realtime resource limit.
Change-Id: Ie57dcc956019ce291f495c1af9a9040b706e7fcf
diff --git a/aos/linux_code/init.cc b/aos/linux_code/init.cc
index 47c7f35..ce557e4 100644
--- a/aos/linux_code/init.cc
+++ b/aos/linux_code/init.cc
@@ -62,6 +62,10 @@
const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME";
+bool ShouldBeRealtime() {
+ return getenv(kNoRealtimeEnvironmentVariable) == nullptr;
+}
+
} // namespace
void LockAllMemory() {
@@ -95,9 +99,9 @@
free(heap_data);
}
-void InitNRT() {
+void InitNRT(bool for_realtime) {
InitStart();
- aos_core_create_shared_mem(false, false);
+ aos_core_create_shared_mem(false, for_realtime && ShouldBeRealtime());
logging::RegisterQueueImplementation();
LOG(INFO, "%s initialized non-realtime\n", program_invocation_short_name);
}
@@ -110,8 +114,14 @@
}
void Init(int relative_priority) {
- bool realtime = getenv(kNoRealtimeEnvironmentVariable) == nullptr;
- if (realtime) {
+ InitStart();
+ aos_core_create_shared_mem(false, ShouldBeRealtime());
+ logging::RegisterQueueImplementation();
+ GoRT(relative_priority);
+}
+
+void GoRT(int relative_priority) {
+ if (ShouldBeRealtime()) {
LockAllMemory();
// Only let rt processes run for 3 seconds straight.
@@ -133,9 +143,6 @@
printf("no realtime for %s. see stderr\n", program_invocation_short_name);
}
- InitStart();
- aos_core_create_shared_mem(false, realtime);
- logging::RegisterQueueImplementation();
LOG(INFO, "%s initialized realtime\n", program_invocation_short_name);
}
diff --git a/aos/linux_code/init.h b/aos/linux_code/init.h
index e34ecfd..91b8c19 100644
--- a/aos/linux_code/init.h
+++ b/aos/linux_code/init.h
@@ -10,7 +10,8 @@
// them again after fork(2)ing.
// Does the non-realtime parts of the initialization process.
-void InitNRT();
+// If for_realtime is true, this sets up to call GoRT later.
+void InitNRT(bool for_realtime = false);
// 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).
@@ -22,6 +23,9 @@
// exit gracefully).
void Cleanup();
+// Performs the realtime parts of initialization after InitNRT(true) has been called.
+void GoRT(int relative_priority = 0);
+
// 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*.
diff --git a/y2018/control_loops/superstructure/superstructure_main.cc b/y2018/control_loops/superstructure/superstructure_main.cc
index 04d245e..e8e1c7b 100644
--- a/y2018/control_loops/superstructure/superstructure_main.cc
+++ b/y2018/control_loops/superstructure/superstructure_main.cc
@@ -3,8 +3,9 @@
#include "aos/linux_code/init.h"
int main() {
- ::aos::Init();
+ ::aos::InitNRT(true);
::y2018::control_loops::superstructure::Superstructure superstructure;
+ ::aos::GoRT();
superstructure.Run();
::aos::Cleanup();
return 0;