blob: 55ef5f9a8f39a9d13d573ec942aa0d2d22b71872 [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>
Austin Schuhde973292021-10-12 18:09:49 -07005
James Kuszmaul57c2baa2020-01-19 14:52:52 -08006#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07007
Austin Schuhcc6070c2020-10-10 20:25:56 -07008#include "glog/logging.h"
9
Alex Perrycb7da4b2019-08-28 19:35:56 -070010namespace 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()
15void InitRT();
16
Alex Perrycb7da4b2019-08-28 19:35:56 -070017// 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*.
20void WriteCoreDumps();
21
22void LockAllMemory();
23
James Kuszmaulb4874eb2020-01-18 17:50:35 -080024void ExpandStackSize();
25
Austin Schuh094d09b2020-11-20 23:26:52 -080026// 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.
29void SetCurrentThreadName(const std::string_view name);
30
Austin Schuh9014e3b2020-11-21 14:26:07 -080031// Creates a cpu_set_t from a list of CPUs.
32inline 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 Schuhde973292021-10-12 18:09:49 -070041// Returns the current thread's CPU affinity.
42cpu_set_t GetCurrentThreadAffinity();
43
Austin Schuh094d09b2020-11-20 23:26:52 -080044// Sets the current thread's scheduling affinity.
45void SetCurrentThreadAffinity(const cpu_set_t &cpuset);
46
47// Everything below here needs AOS to be initialized before it will work
48// properly.
49
50// Sets the current thread's realtime priority.
51void SetCurrentThreadRealtimePriority(int priority);
52
Austin Schuh77f3f222022-06-10 16:49:21 -070053// Unsets all threads realtime priority in preparation for exploding.
54void FatalUnsetRealtimePriority();
55
Austin Schuh094d09b2020-11-20 23:26:52 -080056// Sets the current thread back down to non-realtime priority.
57void UnsetCurrentThreadRealtimePriority();
58
Austin Schuh62288252020-11-18 23:26:04 -080059// Registers our hooks which crash on RT malloc.
60void RegisterMallocHook();
61
Austin Schuhcc6070c2020-10-10 20:25:56 -070062// CHECKs that we are (or are not) running on the RT scheduler. Useful for
63// enforcing that operations which are or are not bounded shouldn't be run. This
64// works both in simulation and when running against the real target.
65void CheckRealtime();
66void CheckNotRealtime();
67
68// Marks that we are or are not running on the realtime scheduler. Returns the
69// previous state.
70//
71// Note: this shouldn't be used directly. The event loop primitives should be
72// used instead.
73bool MarkRealtime(bool realtime);
74
75// Class which restores the current RT state when destructed.
76class ScopedRealtimeRestorer {
77 public:
78 ScopedRealtimeRestorer();
79 ~ScopedRealtimeRestorer() { MarkRealtime(prior_); }
80
81 private:
82 const bool prior_;
83};
84
85// Class which marks us as on the RT scheduler until it goes out of scope.
86// Note: this shouldn't be needed for most applications.
87class ScopedRealtime {
88 public:
89 ScopedRealtime() : prior_(MarkRealtime(true)) {}
90 ~ScopedRealtime() {
91 CHECK(MarkRealtime(prior_)) << ": Priority was modified";
92 }
93
94 private:
95 const bool prior_;
96};
97
98// Class which marks us as not on the RT scheduler until it goes out of scope.
99// Note: this shouldn't be needed for most applications.
100class ScopedNotRealtime {
101 public:
102 ScopedNotRealtime() : prior_(MarkRealtime(false)) {}
103 ~ScopedNotRealtime() {
104 CHECK(!MarkRealtime(prior_)) << ": Priority was modified";
105 }
106
107 private:
108 const bool prior_;
109};
110
Alex Perrycb7da4b2019-08-28 19:35:56 -0700111} // namespace aos
112
113#endif // AOS_REALTIME_H_