blob: be28e8569d56861adb6e5420d331824f0178f7db [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
53// Sets the current thread back down to non-realtime priority.
54void UnsetCurrentThreadRealtimePriority();
55
Austin Schuh62288252020-11-18 23:26:04 -080056// Registers our hooks which crash on RT malloc.
57void RegisterMallocHook();
58
Austin Schuhcc6070c2020-10-10 20:25:56 -070059// CHECKs that we are (or are not) running on the RT scheduler. Useful for
60// enforcing that operations which are or are not bounded shouldn't be run. This
61// works both in simulation and when running against the real target.
62void CheckRealtime();
63void CheckNotRealtime();
64
65// Marks that we are or are not running on the realtime scheduler. Returns the
66// previous state.
67//
68// Note: this shouldn't be used directly. The event loop primitives should be
69// used instead.
70bool MarkRealtime(bool realtime);
71
72// Class which restores the current RT state when destructed.
73class ScopedRealtimeRestorer {
74 public:
75 ScopedRealtimeRestorer();
76 ~ScopedRealtimeRestorer() { MarkRealtime(prior_); }
77
78 private:
79 const bool prior_;
80};
81
82// Class which marks us as on the RT scheduler until it goes out of scope.
83// Note: this shouldn't be needed for most applications.
84class ScopedRealtime {
85 public:
86 ScopedRealtime() : prior_(MarkRealtime(true)) {}
87 ~ScopedRealtime() {
88 CHECK(MarkRealtime(prior_)) << ": Priority was modified";
89 }
90
91 private:
92 const bool prior_;
93};
94
95// Class which marks us as not on the RT scheduler until it goes out of scope.
96// Note: this shouldn't be needed for most applications.
97class ScopedNotRealtime {
98 public:
99 ScopedNotRealtime() : prior_(MarkRealtime(false)) {}
100 ~ScopedNotRealtime() {
101 CHECK(!MarkRealtime(prior_)) << ": Priority was modified";
102 }
103
104 private:
105 const bool prior_;
106};
107
Alex Perrycb7da4b2019-08-28 19:35:56 -0700108} // namespace aos
109
110#endif // AOS_REALTIME_H_