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