blob: c695adde1bbcf5e502630723019f08bf45348072 [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
Alex Perrycb7da4b2019-08-28 19:35:56 -070016// Sets up this process to write core dump files.
17// This is called by Init*, but it's here for other files that want this
18// behavior without calling Init*.
19void WriteCoreDumps();
20
21void LockAllMemory();
22
James Kuszmaulb4874eb2020-01-18 17:50:35 -080023void ExpandStackSize();
24
Austin Schuh094d09b2020-11-20 23:26:52 -080025// Sets the name of the current thread.
26// This will displayed by `top -H`, dump_rtprio, and show up in logs.
27// name can have a maximum of 16 characters.
28void SetCurrentThreadName(const std::string_view name);
29
30// Sets the current thread's scheduling affinity.
31void SetCurrentThreadAffinity(const cpu_set_t &cpuset);
32
33// Everything below here needs AOS to be initialized before it will work
34// properly.
35
36// Sets the current thread's realtime priority.
37void SetCurrentThreadRealtimePriority(int priority);
38
39// Sets the current thread back down to non-realtime priority.
40void UnsetCurrentThreadRealtimePriority();
41
Austin Schuh62288252020-11-18 23:26:04 -080042// Registers our hooks which crash on RT malloc.
43void RegisterMallocHook();
44
Austin Schuhcc6070c2020-10-10 20:25:56 -070045// CHECKs that we are (or are not) running on the RT scheduler. Useful for
46// enforcing that operations which are or are not bounded shouldn't be run. This
47// works both in simulation and when running against the real target.
48void CheckRealtime();
49void CheckNotRealtime();
50
51// Marks that we are or are not running on the realtime scheduler. Returns the
52// previous state.
53//
54// Note: this shouldn't be used directly. The event loop primitives should be
55// used instead.
56bool MarkRealtime(bool realtime);
57
58// Class which restores the current RT state when destructed.
59class ScopedRealtimeRestorer {
60 public:
61 ScopedRealtimeRestorer();
62 ~ScopedRealtimeRestorer() { MarkRealtime(prior_); }
63
64 private:
65 const bool prior_;
66};
67
68// Class which marks us as on the RT scheduler until it goes out of scope.
69// Note: this shouldn't be needed for most applications.
70class ScopedRealtime {
71 public:
72 ScopedRealtime() : prior_(MarkRealtime(true)) {}
73 ~ScopedRealtime() {
74 CHECK(MarkRealtime(prior_)) << ": Priority was modified";
75 }
76
77 private:
78 const bool prior_;
79};
80
81// Class which marks us as not on the RT scheduler until it goes out of scope.
82// Note: this shouldn't be needed for most applications.
83class ScopedNotRealtime {
84 public:
85 ScopedNotRealtime() : prior_(MarkRealtime(false)) {}
86 ~ScopedNotRealtime() {
87 CHECK(!MarkRealtime(prior_)) << ": Priority was modified";
88 }
89
90 private:
91 const bool prior_;
92};
93
Alex Perrycb7da4b2019-08-28 19:35:56 -070094} // namespace aos
95
96#endif // AOS_REALTIME_H_