blob: bb5ea84f9cf54af8fe1ecb7de0f0b8430054a904 [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
16// Sets the current thread back down to non-realtime priority.
17void UnsetCurrentThreadRealtimePriority();
18
19// Sets the name of the current thread.
20// This will displayed by `top -H`, dump_rtprio, and show up in logs.
21// name can have a maximum of 16 characters.
James Kuszmaul57c2baa2020-01-19 14:52:52 -080022void SetCurrentThreadName(const std::string_view name);
Alex Perrycb7da4b2019-08-28 19:35:56 -070023
24// Sets the current thread's realtime priority.
25void SetCurrentThreadRealtimePriority(int priority);
26
Brian Silverman6a54ff32020-04-28 16:41:39 -070027// Sets the current thread's scheduling affinity.
28void SetCurrentThreadAffinity(const cpu_set_t &cpuset);
29
Alex Perrycb7da4b2019-08-28 19:35:56 -070030// Sets up this process to write core dump files.
31// This is called by Init*, but it's here for other files that want this
32// behavior without calling Init*.
33void WriteCoreDumps();
34
35void LockAllMemory();
36
James Kuszmaulb4874eb2020-01-18 17:50:35 -080037void ExpandStackSize();
38
Austin Schuh62288252020-11-18 23:26:04 -080039// Registers our hooks which crash on RT malloc.
40void RegisterMallocHook();
41
Austin Schuhcc6070c2020-10-10 20:25:56 -070042// CHECKs that we are (or are not) running on the RT scheduler. Useful for
43// enforcing that operations which are or are not bounded shouldn't be run. This
44// works both in simulation and when running against the real target.
45void CheckRealtime();
46void CheckNotRealtime();
47
48// Marks that we are or are not running on the realtime scheduler. Returns the
49// previous state.
50//
51// Note: this shouldn't be used directly. The event loop primitives should be
52// used instead.
53bool MarkRealtime(bool realtime);
54
55// Class which restores the current RT state when destructed.
56class ScopedRealtimeRestorer {
57 public:
58 ScopedRealtimeRestorer();
59 ~ScopedRealtimeRestorer() { MarkRealtime(prior_); }
60
61 private:
62 const bool prior_;
63};
64
65// Class which marks us as on the RT scheduler until it goes out of scope.
66// Note: this shouldn't be needed for most applications.
67class ScopedRealtime {
68 public:
69 ScopedRealtime() : prior_(MarkRealtime(true)) {}
70 ~ScopedRealtime() {
71 CHECK(MarkRealtime(prior_)) << ": Priority was modified";
72 }
73
74 private:
75 const bool prior_;
76};
77
78// Class which marks us as not on the RT scheduler until it goes out of scope.
79// Note: this shouldn't be needed for most applications.
80class ScopedNotRealtime {
81 public:
82 ScopedNotRealtime() : prior_(MarkRealtime(false)) {}
83 ~ScopedNotRealtime() {
84 CHECK(!MarkRealtime(prior_)) << ": Priority was modified";
85 }
86
87 private:
88 const bool prior_;
89};
90
Alex Perrycb7da4b2019-08-28 19:35:56 -070091} // namespace aos
92
93#endif // AOS_REALTIME_H_