Add RT scheduler tracking
This interacts with event loops nicely, both in simulation and in the
real system. This gives us tools to enforce that certain pieces of code
are run in a RT world, and others are not.
Future work will be to enforce that Malloc is not called while realtime.
Change-Id: I3ce3dc287e25390095bac34aed4888434a82f06e
diff --git a/aos/realtime.cc b/aos/realtime.cc
index a41eb0d..9df7aca 100644
--- a/aos/realtime.cc
+++ b/aos/realtime.cc
@@ -1,18 +1,19 @@
#include "aos/realtime.h"
+#include <errno.h>
+#include <malloc.h>
+#include <sched.h>
+#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
-#include <errno.h>
-#include <sched.h>
+#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <unistd.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <sys/prctl.h>
-#include <malloc.h>
+#include "aos/thread_local.h"
#include "glog/logging.h"
namespace FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead {
@@ -68,6 +69,7 @@
} // namespace
void LockAllMemory() {
+ CheckNotRealtime();
// Allow locking as much as we want into RAM.
SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, SetLimitForRoot::kNo);
@@ -101,6 +103,7 @@
}
void InitRT() {
+ CheckNotRealtime();
LockAllMemory();
// Only let rt processes run for 3 seconds straight.
@@ -114,6 +117,7 @@
struct sched_param param;
param.sched_priority = 0;
PCHECK(sched_setscheduler(0, SCHED_OTHER, ¶m) == 0);
+ MarkRealtime(false);
}
void SetCurrentThreadAffinity(const cpu_set_t &cpuset) {
@@ -141,6 +145,7 @@
struct sched_param param;
param.sched_priority = priority;
+ MarkRealtime(true);
PCHECK(sched_setscheduler(0, SCHED_FIFO, ¶m) == 0)
<< ": changing to SCHED_FIFO with " << priority;
}
@@ -155,4 +160,20 @@
AllowSoftLimitDecrease::kNo);
}
+namespace {
+AOS_THREAD_LOCAL bool is_realtime = false;
+}
+
+bool MarkRealtime(bool realtime) {
+ const bool prior = is_realtime;
+ is_realtime = realtime;
+ return prior;
+}
+
+void CheckRealtime() { CHECK(is_realtime); }
+
+void CheckNotRealtime() { CHECK(!is_realtime); }
+
+ScopedRealtimeRestorer::ScopedRealtimeRestorer() : prior_(is_realtime) {}
+
} // namespace aos