blob: fb49cac1bf98a2cb598663620c17031c3d718670 [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
Austin Schuh9014e3b2020-11-21 14:26:07 -080030// Creates a cpu_set_t from a list of CPUs.
31inline cpu_set_t MakeCpusetFromCpus(std::initializer_list<int> cpus) {
32 cpu_set_t result;
33 CPU_ZERO(&result);
34 for (int cpu : cpus) {
35 CPU_SET(cpu, &result);
36 }
37 return result;
38}
39
Austin Schuh094d09b2020-11-20 23:26:52 -080040// Sets the current thread's scheduling affinity.
41void SetCurrentThreadAffinity(const cpu_set_t &cpuset);
42
43// Everything below here needs AOS to be initialized before it will work
44// properly.
45
46// Sets the current thread's realtime priority.
47void SetCurrentThreadRealtimePriority(int priority);
48
49// Sets the current thread back down to non-realtime priority.
50void UnsetCurrentThreadRealtimePriority();
51
Austin Schuh62288252020-11-18 23:26:04 -080052// Registers our hooks which crash on RT malloc.
53void RegisterMallocHook();
54
Austin Schuhcc6070c2020-10-10 20:25:56 -070055// CHECKs that we are (or are not) running on the RT scheduler. Useful for
56// enforcing that operations which are or are not bounded shouldn't be run. This
57// works both in simulation and when running against the real target.
58void CheckRealtime();
59void CheckNotRealtime();
60
61// Marks that we are or are not running on the realtime scheduler. Returns the
62// previous state.
63//
64// Note: this shouldn't be used directly. The event loop primitives should be
65// used instead.
66bool MarkRealtime(bool realtime);
67
68// Class which restores the current RT state when destructed.
69class ScopedRealtimeRestorer {
70 public:
71 ScopedRealtimeRestorer();
72 ~ScopedRealtimeRestorer() { MarkRealtime(prior_); }
73
74 private:
75 const bool prior_;
76};
77
78// Class which marks us as on the RT scheduler until it goes out of scope.
79// Note: this shouldn't be needed for most applications.
80class ScopedRealtime {
81 public:
82 ScopedRealtime() : prior_(MarkRealtime(true)) {}
83 ~ScopedRealtime() {
84 CHECK(MarkRealtime(prior_)) << ": Priority was modified";
85 }
86
87 private:
88 const bool prior_;
89};
90
91// Class which marks us as not on the RT scheduler until it goes out of scope.
92// Note: this shouldn't be needed for most applications.
93class ScopedNotRealtime {
94 public:
95 ScopedNotRealtime() : prior_(MarkRealtime(false)) {}
96 ~ScopedNotRealtime() {
97 CHECK(!MarkRealtime(prior_)) << ": Priority was modified";
98 }
99
100 private:
101 const bool prior_;
102};
103
Alex Perrycb7da4b2019-08-28 19:35:56 -0700104} // namespace aos
105
106#endif // AOS_REALTIME_H_