blob: 52db4a84f2abad74f9b0ddbc2c86f8798c031a39 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#ifndef AOS_REALTIME_H_
2#define AOS_REALTIME_H_
3
Brian Silverman6a54ff32020-04-28 16:41:39 -07004#include <sched.h>
James Kuszmaul57c2baa2020-01-19 14:52:52 -08005#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07006
Austin Schuhcc6070c2020-10-10 20:25:56 -07007#include "glog/logging.h"
8
Alex Perrycb7da4b2019-08-28 19:35:56 -07009namespace aos {
10
11// Locks everything into memory and sets the limits. This plus InitNRT are
12// everything you need to do before SetCurrentThreadRealtimePriority will make
13// your thread RT. Called as part of ShmEventLoop::Run()
14void InitRT();
15
16// Sets the current thread back down to non-realtime priority.
17void UnsetCurrentThreadRealtimePriority();
18
19// Sets the name of the current thread.
20// This will displayed by `top -H`, dump_rtprio, and show up in logs.
21// name can have a maximum of 16 characters.
James Kuszmaul57c2baa2020-01-19 14:52:52 -080022void SetCurrentThreadName(const std::string_view name);
Alex Perrycb7da4b2019-08-28 19:35:56 -070023
24// Sets the current thread's realtime priority.
25void SetCurrentThreadRealtimePriority(int priority);
26
Brian Silverman6a54ff32020-04-28 16:41:39 -070027// Sets the current thread's scheduling affinity.
28void SetCurrentThreadAffinity(const cpu_set_t &cpuset);
29
Alex Perrycb7da4b2019-08-28 19:35:56 -070030// Sets up this process to write core dump files.
31// This is called by Init*, but it's here for other files that want this
32// behavior without calling Init*.
33void WriteCoreDumps();
34
35void LockAllMemory();
36
James Kuszmaulb4874eb2020-01-18 17:50:35 -080037void ExpandStackSize();
38
Austin Schuhcc6070c2020-10-10 20:25:56 -070039// CHECKs that we are (or are not) running on the RT scheduler. Useful for
40// enforcing that operations which are or are not bounded shouldn't be run. This
41// works both in simulation and when running against the real target.
42void CheckRealtime();
43void CheckNotRealtime();
44
45// Marks that we are or are not running on the realtime scheduler. Returns the
46// previous state.
47//
48// Note: this shouldn't be used directly. The event loop primitives should be
49// used instead.
50bool MarkRealtime(bool realtime);
51
52// Class which restores the current RT state when destructed.
53class ScopedRealtimeRestorer {
54 public:
55 ScopedRealtimeRestorer();
56 ~ScopedRealtimeRestorer() { MarkRealtime(prior_); }
57
58 private:
59 const bool prior_;
60};
61
62// Class which marks us as on the RT scheduler until it goes out of scope.
63// Note: this shouldn't be needed for most applications.
64class ScopedRealtime {
65 public:
66 ScopedRealtime() : prior_(MarkRealtime(true)) {}
67 ~ScopedRealtime() {
68 CHECK(MarkRealtime(prior_)) << ": Priority was modified";
69 }
70
71 private:
72 const bool prior_;
73};
74
75// Class which marks us as not on the RT scheduler until it goes out of scope.
76// Note: this shouldn't be needed for most applications.
77class ScopedNotRealtime {
78 public:
79 ScopedNotRealtime() : prior_(MarkRealtime(false)) {}
80 ~ScopedNotRealtime() {
81 CHECK(!MarkRealtime(prior_)) << ": Priority was modified";
82 }
83
84 private:
85 const bool prior_;
86};
87
Alex Perrycb7da4b2019-08-28 19:35:56 -070088} // namespace aos
89
90#endif // AOS_REALTIME_H_