Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/realtime.h" |
| 2 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 3 | #include <errno.h> |
| 4 | #include <malloc.h> |
| 5 | #include <sched.h> |
| 6 | #include <stdint.h> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 8 | #include <stdlib.h> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include <string.h> |
| 10 | #include <sys/mman.h> |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 11 | #include <sys/prctl.h> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | #include <sys/resource.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <unistd.h> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 15 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 16 | #include "aos/thread_local.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include "glog/logging.h" |
| 18 | |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 19 | DEFINE_bool(skip_realtime_scheduler, false, |
| 20 | "If true, skip changing the scheduler. Pretend that we changed " |
| 21 | "the scheduler instead."); |
| 22 | DEFINE_bool(skip_locking_memory, false, |
| 23 | "If true, skip locking memory. Pretend that we did it instead."); |
| 24 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | namespace FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead { |
| 26 | extern double FLAGS_tcmalloc_release_rate __attribute__((weak)); |
| 27 | } |
| 28 | using FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead:: |
| 29 | FLAGS_tcmalloc_release_rate; |
| 30 | |
| 31 | namespace aos { |
| 32 | namespace logging { |
| 33 | namespace internal { |
| 34 | |
| 35 | // Implemented in aos/logging/context.cc. |
| 36 | void ReloadThreadName() __attribute__((weak)); |
| 37 | |
| 38 | } // namespace internal |
| 39 | } // namespace logging |
| 40 | |
| 41 | namespace { |
| 42 | |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 43 | enum class SetLimitForRoot { kYes, kNo }; |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 44 | |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 45 | enum class AllowSoftLimitDecrease { kYes, kNo }; |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 46 | |
| 47 | void SetSoftRLimit( |
| 48 | int resource, rlim64_t soft, SetLimitForRoot set_for_root, |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 49 | std::string_view help_string, |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 50 | AllowSoftLimitDecrease allow_decrease = AllowSoftLimitDecrease::kYes) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 51 | bool am_root = getuid() == 0; |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 52 | if (set_for_root == SetLimitForRoot::kYes || !am_root) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 53 | struct rlimit64 rlim; |
| 54 | PCHECK(getrlimit64(resource, &rlim) == 0) |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 55 | << ": getting limit for " << resource; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 56 | |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 57 | if (allow_decrease == AllowSoftLimitDecrease::kYes) { |
| 58 | rlim.rlim_cur = soft; |
| 59 | } else { |
| 60 | rlim.rlim_cur = std::max(rlim.rlim_cur, soft); |
| 61 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 62 | rlim.rlim_max = ::std::max(rlim.rlim_max, soft); |
| 63 | |
| 64 | PCHECK(setrlimit64(resource, &rlim) == 0) |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 65 | << ": changing limit for " << resource << " to " << rlim.rlim_cur |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 66 | << " with max of " << rlim.rlim_max << help_string; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | } // namespace |
| 71 | |
| 72 | void LockAllMemory() { |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 73 | CheckNotRealtime(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | // Allow locking as much as we want into RAM. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 75 | SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, SetLimitForRoot::kNo, |
| 76 | "use --skip_locking_memory to not lock memory."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 77 | |
| 78 | WriteCoreDumps(); |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 79 | PCHECK(mlockall(MCL_CURRENT | MCL_FUTURE) == 0) |
| 80 | << ": Failed to lock memory, use --skip_locking_memory to bypass this. " |
| 81 | "Bypassing will impact RT performance."; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 82 | |
Brian Silverman | 4dbbcce | 2020-09-18 15:27:38 -0700 | [diff] [blame] | 83 | #if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | // Don't give freed memory back to the OS. |
| 85 | CHECK_EQ(1, mallopt(M_TRIM_THRESHOLD, -1)); |
| 86 | // Don't use mmap for large malloc chunks. |
| 87 | CHECK_EQ(1, mallopt(M_MMAP_MAX, 0)); |
Austin Schuh | 85faf67 | 2020-09-10 22:58:46 -0700 | [diff] [blame] | 88 | #endif |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 89 | |
| 90 | if (&FLAGS_tcmalloc_release_rate) { |
| 91 | // Tell tcmalloc not to return memory. |
| 92 | FLAGS_tcmalloc_release_rate = 0.0; |
| 93 | } |
| 94 | |
| 95 | // Forces the memory pages for all the stack space that we're ever going to |
| 96 | // use to be loaded into memory (so it can be locked there). |
| 97 | uint8_t data[4096 * 8]; |
| 98 | // Not 0 because linux might optimize that to a 0-filled page. |
| 99 | memset(data, 1, sizeof(data)); |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 100 | __asm__ __volatile__("" ::"m"(data)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 101 | |
| 102 | static const size_t kHeapPreallocSize = 512 * 1024; |
| 103 | char *const heap_data = static_cast<char *>(malloc(kHeapPreallocSize)); |
| 104 | memset(heap_data, 1, kHeapPreallocSize); |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 105 | __asm__ __volatile__("" ::"m"(heap_data)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 106 | free(heap_data); |
| 107 | } |
| 108 | |
| 109 | void InitRT() { |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 110 | if (FLAGS_skip_locking_memory) { |
| 111 | LOG(WARNING) << "Ignoring request to lock all memory due to " |
| 112 | "--skip_locking_memory."; |
| 113 | return; |
| 114 | } |
| 115 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 116 | CheckNotRealtime(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 117 | LockAllMemory(); |
| 118 | |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 119 | if (FLAGS_skip_realtime_scheduler) { |
| 120 | return; |
| 121 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 122 | // Only let rt processes run for 3 seconds straight. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 123 | SetSoftRLimit( |
| 124 | RLIMIT_RTTIME, 3000000, SetLimitForRoot::kYes, |
| 125 | ", use --skip_realtime_scheduler to stay non-rt and bypass this " |
| 126 | "warning."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 127 | |
| 128 | // Allow rt processes up to priority 40. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 129 | SetSoftRLimit( |
| 130 | RLIMIT_RTPRIO, 40, SetLimitForRoot::kNo, |
| 131 | ", use --skip_realtime_scheduler to stay non-rt and bypass this " |
| 132 | "warning."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void UnsetCurrentThreadRealtimePriority() { |
| 136 | struct sched_param param; |
| 137 | param.sched_priority = 0; |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 138 | PCHECK(sched_setscheduler(0, SCHED_OTHER, ¶m) == 0); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 139 | MarkRealtime(false); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void SetCurrentThreadAffinity(const cpu_set_t &cpuset) { |
| 143 | PCHECK(sched_setaffinity(0, sizeof(cpuset), &cpuset) == 0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 144 | } |
| 145 | |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 146 | void SetCurrentThreadName(const std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 147 | CHECK_LE(name.size(), 16u) << ": thread name '" << name << "' too long"; |
| 148 | VLOG(1) << "This thread is changing to '" << name << "'"; |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 149 | std::string string_name(name); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 150 | PCHECK(prctl(PR_SET_NAME, string_name.c_str()) == 0) |
| 151 | << ": changing name to " << string_name; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 152 | if (&logging::internal::ReloadThreadName != nullptr) { |
| 153 | logging::internal::ReloadThreadName(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void SetCurrentThreadRealtimePriority(int priority) { |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 158 | if (FLAGS_skip_realtime_scheduler) { |
| 159 | LOG(WARNING) << "Ignoring request to switch to the RT scheduler due to " |
| 160 | "--skip_realtime_scheduler."; |
| 161 | return; |
| 162 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 163 | // Make sure we will only be allowed to run for 3 seconds straight. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 164 | SetSoftRLimit( |
| 165 | RLIMIT_RTTIME, 3000000, SetLimitForRoot::kYes, |
| 166 | ", use --skip_realtime_scheduler to stay non-rt and bypass this " |
| 167 | "warning."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 168 | |
Brian Silverman | b3826f5 | 2020-07-02 19:41:18 -0700 | [diff] [blame] | 169 | // Raise our soft rlimit if necessary. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 170 | SetSoftRLimit( |
| 171 | RLIMIT_RTPRIO, priority, SetLimitForRoot::kNo, |
| 172 | ", use --skip_realtime_scheduler to stay non-rt and bypass this " |
| 173 | "warning.", |
| 174 | AllowSoftLimitDecrease::kNo); |
Brian Silverman | b3826f5 | 2020-07-02 19:41:18 -0700 | [diff] [blame] | 175 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 176 | struct sched_param param; |
| 177 | param.sched_priority = priority; |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 178 | MarkRealtime(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 179 | PCHECK(sched_setscheduler(0, SCHED_FIFO, ¶m) == 0) |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 180 | << ": changing to SCHED_FIFO with " << priority |
| 181 | << ", if you want to bypass this check for testing, use " |
| 182 | "--skip_realtime_scheduler"; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void WriteCoreDumps() { |
| 186 | // Do create core files of unlimited size. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 187 | SetSoftRLimit(RLIMIT_CORE, RLIM_INFINITY, SetLimitForRoot::kYes, ""); |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void ExpandStackSize() { |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame^] | 191 | SetSoftRLimit(RLIMIT_STACK, 1000000, SetLimitForRoot::kYes, "", |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 192 | AllowSoftLimitDecrease::kNo); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 195 | namespace { |
| 196 | AOS_THREAD_LOCAL bool is_realtime = false; |
| 197 | } |
| 198 | |
| 199 | bool MarkRealtime(bool realtime) { |
| 200 | const bool prior = is_realtime; |
| 201 | is_realtime = realtime; |
| 202 | return prior; |
| 203 | } |
| 204 | |
| 205 | void CheckRealtime() { CHECK(is_realtime); } |
| 206 | |
| 207 | void CheckNotRealtime() { CHECK(!is_realtime); } |
| 208 | |
| 209 | ScopedRealtimeRestorer::ScopedRealtimeRestorer() : prior_(is_realtime) {} |
| 210 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 211 | } // namespace aos |