Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #ifndef AOS_REALTIME_H_ |
| 2 | #define AOS_REALTIME_H_ |
| 3 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 4 | #include <sched.h> |
Austin Schuh | de97329 | 2021-10-12 18:09:49 -0700 | [diff] [blame] | 5 | |
Philipp Schrader | 36d7793 | 2024-02-01 18:31:19 -0800 | [diff] [blame] | 6 | #include <ostream> |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 7 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 9 | #include "glog/logging.h" |
| 10 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | namespace aos { |
| 12 | |
| 13 | // Locks everything into memory and sets the limits. This plus InitNRT are |
| 14 | // everything you need to do before SetCurrentThreadRealtimePriority will make |
| 15 | // your thread RT. Called as part of ShmEventLoop::Run() |
| 16 | void InitRT(); |
| 17 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | // Sets up this process to write core dump files. |
| 19 | // This is called by Init*, but it's here for other files that want this |
| 20 | // behavior without calling Init*. |
| 21 | void WriteCoreDumps(); |
| 22 | |
| 23 | void LockAllMemory(); |
| 24 | |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 25 | void ExpandStackSize(); |
| 26 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 27 | // Sets the name of the current thread. |
| 28 | // This will displayed by `top -H`, dump_rtprio, and show up in logs. |
| 29 | // name can have a maximum of 16 characters. |
| 30 | void SetCurrentThreadName(const std::string_view name); |
| 31 | |
Philipp Schrader | 36d7793 | 2024-02-01 18:31:19 -0800 | [diff] [blame] | 32 | // Stringifies the cpu_set_t for streams. |
| 33 | std::ostream &operator<<(std::ostream &stream, const cpu_set_t &cpuset); |
| 34 | |
Austin Schuh | 9014e3b | 2020-11-21 14:26:07 -0800 | [diff] [blame] | 35 | // Creates a cpu_set_t from a list of CPUs. |
| 36 | inline cpu_set_t MakeCpusetFromCpus(std::initializer_list<int> cpus) { |
| 37 | cpu_set_t result; |
| 38 | CPU_ZERO(&result); |
| 39 | for (int cpu : cpus) { |
| 40 | CPU_SET(cpu, &result); |
| 41 | } |
| 42 | return result; |
| 43 | } |
| 44 | |
Austin Schuh | 070019a | 2022-12-20 22:23:09 -0800 | [diff] [blame] | 45 | // Returns the affinity representing all the CPUs. |
| 46 | inline cpu_set_t DefaultAffinity() { |
| 47 | cpu_set_t result; |
| 48 | for (int i = 0; i < CPU_SETSIZE; ++i) { |
| 49 | CPU_SET(i, &result); |
| 50 | } |
| 51 | return result; |
| 52 | } |
| 53 | |
Austin Schuh | de97329 | 2021-10-12 18:09:49 -0700 | [diff] [blame] | 54 | // Returns the current thread's CPU affinity. |
| 55 | cpu_set_t GetCurrentThreadAffinity(); |
| 56 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 57 | // Sets the current thread's scheduling affinity. |
| 58 | void SetCurrentThreadAffinity(const cpu_set_t &cpuset); |
| 59 | |
| 60 | // Everything below here needs AOS to be initialized before it will work |
| 61 | // properly. |
| 62 | |
| 63 | // Sets the current thread's realtime priority. |
| 64 | void SetCurrentThreadRealtimePriority(int priority); |
| 65 | |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 66 | // Unsets all threads realtime priority in preparation for exploding. |
| 67 | void FatalUnsetRealtimePriority(); |
| 68 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 69 | // Sets the current thread back down to non-realtime priority. |
| 70 | void UnsetCurrentThreadRealtimePriority(); |
| 71 | |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 72 | // Registers our hooks which crash on RT malloc. |
| 73 | void RegisterMallocHook(); |
| 74 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 75 | // CHECKs that we are (or are not) running on the RT scheduler. Useful for |
| 76 | // enforcing that operations which are or are not bounded shouldn't be run. This |
| 77 | // works both in simulation and when running against the real target. |
| 78 | void CheckRealtime(); |
| 79 | void CheckNotRealtime(); |
| 80 | |
| 81 | // Marks that we are or are not running on the realtime scheduler. Returns the |
| 82 | // previous state. |
| 83 | // |
| 84 | // Note: this shouldn't be used directly. The event loop primitives should be |
| 85 | // used instead. |
| 86 | bool MarkRealtime(bool realtime); |
| 87 | |
| 88 | // Class which restores the current RT state when destructed. |
| 89 | class ScopedRealtimeRestorer { |
| 90 | public: |
| 91 | ScopedRealtimeRestorer(); |
| 92 | ~ScopedRealtimeRestorer() { MarkRealtime(prior_); } |
| 93 | |
| 94 | private: |
| 95 | const bool prior_; |
| 96 | }; |
| 97 | |
| 98 | // Class which marks us as on the RT scheduler until it goes out of scope. |
| 99 | // Note: this shouldn't be needed for most applications. |
| 100 | class ScopedRealtime { |
| 101 | public: |
| 102 | ScopedRealtime() : prior_(MarkRealtime(true)) {} |
| 103 | ~ScopedRealtime() { |
| 104 | CHECK(MarkRealtime(prior_)) << ": Priority was modified"; |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | const bool prior_; |
| 109 | }; |
| 110 | |
| 111 | // Class which marks us as not on the RT scheduler until it goes out of scope. |
| 112 | // Note: this shouldn't be needed for most applications. |
| 113 | class ScopedNotRealtime { |
| 114 | public: |
| 115 | ScopedNotRealtime() : prior_(MarkRealtime(false)) {} |
| 116 | ~ScopedNotRealtime() { |
| 117 | CHECK(!MarkRealtime(prior_)) << ": Priority was modified"; |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | const bool prior_; |
| 122 | }; |
| 123 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 124 | } // namespace aos |
| 125 | |
| 126 | #endif // AOS_REALTIME_H_ |