blob: caf0daadb9a8b70fad2c329251c55ac6da090fde [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 Schuh070019a2022-12-20 22:23:09 -080041// Returns the affinity representing all the CPUs.
42inline 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 Schuhde973292021-10-12 18:09:49 -070050// Returns the current thread's CPU affinity.
51cpu_set_t GetCurrentThreadAffinity();
52
Austin Schuh094d09b2020-11-20 23:26:52 -080053// Sets the current thread's scheduling affinity.
54void 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.
60void SetCurrentThreadRealtimePriority(int priority);
61
Austin Schuh77f3f222022-06-10 16:49:21 -070062// Unsets all threads realtime priority in preparation for exploding.
63void FatalUnsetRealtimePriority();
64
Austin Schuh094d09b2020-11-20 23:26:52 -080065// Sets the current thread back down to non-realtime priority.
66void UnsetCurrentThreadRealtimePriority();
67
Austin Schuh62288252020-11-18 23:26:04 -080068// Registers our hooks which crash on RT malloc.
69void RegisterMallocHook();
70
Austin Schuhcc6070c2020-10-10 20:25:56 -070071// 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.
74void CheckRealtime();
75void 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.
82bool MarkRealtime(bool realtime);
83
84// Class which restores the current RT state when destructed.
85class 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.
96class 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.
109class 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 Perrycb7da4b2019-08-28 19:35:56 -0700120} // namespace aos
121
122#endif // AOS_REALTIME_H_