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