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> |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 5 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 7 | #include "glog/logging.h" |
| 8 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | namespace 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() |
| 14 | void InitRT(); |
| 15 | |
| 16 | // Sets the current thread back down to non-realtime priority. |
| 17 | void 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 Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 22 | void SetCurrentThreadName(const std::string_view name); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | |
| 24 | // Sets the current thread's realtime priority. |
| 25 | void SetCurrentThreadRealtimePriority(int priority); |
| 26 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 27 | // Sets the current thread's scheduling affinity. |
| 28 | void SetCurrentThreadAffinity(const cpu_set_t &cpuset); |
| 29 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | // 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*. |
| 33 | void WriteCoreDumps(); |
| 34 | |
| 35 | void LockAllMemory(); |
| 36 | |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 37 | void ExpandStackSize(); |
| 38 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 39 | // 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. |
| 42 | void CheckRealtime(); |
| 43 | void 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. |
| 50 | bool MarkRealtime(bool realtime); |
| 51 | |
| 52 | // Class which restores the current RT state when destructed. |
| 53 | class 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. |
| 64 | class 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. |
| 77 | class 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 88 | } // namespace aos |
| 89 | |
| 90 | #endif // AOS_REALTIME_H_ |