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