Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/realtime.h" |
| 2 | |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 3 | #include <dirent.h> |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 4 | #include <malloc.h> |
| 5 | #include <sched.h> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | #include <sys/mman.h> |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 7 | #include <sys/prctl.h> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include <sys/resource.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <unistd.h> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 12 | #include <cerrno> |
| 13 | #include <cstdint> |
| 14 | #include <cstdio> |
| 15 | #include <cstdlib> |
| 16 | #include <cstring> |
| 17 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | #include "glog/logging.h" |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 19 | #include "glog/raw_logging.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | |
James Kuszmaul | a791b76 | 2023-07-13 14:56:21 -0700 | [diff] [blame] | 21 | #include "aos/uuid.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 22 | |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 23 | DEFINE_bool( |
Austin Schuh | bd93820 | 2023-05-16 22:42:11 -0700 | [diff] [blame] | 24 | die_on_malloc, true, |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 25 | "If true, die when the application allocates memory in a RT section."); |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 26 | DEFINE_bool(skip_realtime_scheduler, false, |
| 27 | "If true, skip changing the scheduler. Pretend that we changed " |
| 28 | "the scheduler instead."); |
| 29 | DEFINE_bool(skip_locking_memory, false, |
| 30 | "If true, skip locking memory. Pretend that we did it instead."); |
| 31 | |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 32 | extern "C" { |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 33 | typedef void (*MallocHook_NewHook)(const void *ptr, size_t size); |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 34 | int MallocHook_AddNewHook(MallocHook_NewHook hook) __attribute__((weak)); |
| 35 | int MallocHook_RemoveNewHook(MallocHook_NewHook hook) __attribute__((weak)); |
| 36 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 37 | typedef void (*MallocHook_DeleteHook)(const void *ptr); |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 38 | int MallocHook_AddDeleteHook(MallocHook_DeleteHook hook) __attribute__((weak)); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 39 | int MallocHook_RemoveDeleteHook(MallocHook_DeleteHook hook) |
| 40 | __attribute__((weak)); |
Tyler Chatow | 582c6c7 | 2021-07-16 13:45:07 -0700 | [diff] [blame] | 41 | |
| 42 | // Declare tc_malloc weak so we can check if it exists. |
| 43 | void *tc_malloc(size_t size) __attribute__((weak)); |
| 44 | |
| 45 | void *__libc_malloc(size_t size); |
| 46 | void __libc_free(void *ptr); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 47 | } // extern "C" |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 48 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 49 | namespace FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead { |
| 50 | extern double FLAGS_tcmalloc_release_rate __attribute__((weak)); |
| 51 | } |
| 52 | using FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead:: |
| 53 | FLAGS_tcmalloc_release_rate; |
| 54 | |
| 55 | namespace aos { |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 56 | |
| 57 | namespace logging::internal { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 58 | |
| 59 | // Implemented in aos/logging/context.cc. |
| 60 | void ReloadThreadName() __attribute__((weak)); |
| 61 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 62 | } // namespace logging::internal |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 63 | |
| 64 | namespace { |
| 65 | |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 66 | enum class SetLimitForRoot { kYes, kNo }; |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 67 | |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 68 | enum class AllowSoftLimitDecrease { kYes, kNo }; |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 69 | |
| 70 | void SetSoftRLimit( |
| 71 | int resource, rlim64_t soft, SetLimitForRoot set_for_root, |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 72 | std::string_view help_string, |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 73 | AllowSoftLimitDecrease allow_decrease = AllowSoftLimitDecrease::kYes) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | bool am_root = getuid() == 0; |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 75 | if (set_for_root == SetLimitForRoot::kYes || !am_root) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 76 | struct rlimit64 rlim; |
| 77 | PCHECK(getrlimit64(resource, &rlim) == 0) |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 78 | << ": getting limit for " << resource; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 79 | |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 80 | if (allow_decrease == AllowSoftLimitDecrease::kYes) { |
| 81 | rlim.rlim_cur = soft; |
| 82 | } else { |
| 83 | rlim.rlim_cur = std::max(rlim.rlim_cur, soft); |
| 84 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 85 | rlim.rlim_max = ::std::max(rlim.rlim_max, soft); |
| 86 | |
| 87 | PCHECK(setrlimit64(resource, &rlim) == 0) |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 88 | << ": changing limit for " << resource << " to " << rlim.rlim_cur |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 89 | << " with max of " << rlim.rlim_max << help_string; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | } // namespace |
| 94 | |
| 95 | void LockAllMemory() { |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 96 | CheckNotRealtime(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 97 | // Allow locking as much as we want into RAM. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 98 | SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, SetLimitForRoot::kNo, |
| 99 | "use --skip_locking_memory to not lock memory."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 100 | |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 101 | PCHECK(mlockall(MCL_CURRENT | MCL_FUTURE) == 0) |
| 102 | << ": Failed to lock memory, use --skip_locking_memory to bypass this. " |
| 103 | "Bypassing will impact RT performance."; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 104 | |
Brian Silverman | 4dbbcce | 2020-09-18 15:27:38 -0700 | [diff] [blame] | 105 | #if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 106 | // Don't give freed memory back to the OS. |
| 107 | CHECK_EQ(1, mallopt(M_TRIM_THRESHOLD, -1)); |
| 108 | // Don't use mmap for large malloc chunks. |
| 109 | CHECK_EQ(1, mallopt(M_MMAP_MAX, 0)); |
Austin Schuh | 85faf67 | 2020-09-10 22:58:46 -0700 | [diff] [blame] | 110 | #endif |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 111 | |
| 112 | if (&FLAGS_tcmalloc_release_rate) { |
| 113 | // Tell tcmalloc not to return memory. |
| 114 | FLAGS_tcmalloc_release_rate = 0.0; |
| 115 | } |
| 116 | |
| 117 | // Forces the memory pages for all the stack space that we're ever going to |
| 118 | // use to be loaded into memory (so it can be locked there). |
| 119 | uint8_t data[4096 * 8]; |
| 120 | // Not 0 because linux might optimize that to a 0-filled page. |
| 121 | memset(data, 1, sizeof(data)); |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 122 | __asm__ __volatile__("" ::"m"(data)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 123 | |
| 124 | static const size_t kHeapPreallocSize = 512 * 1024; |
| 125 | char *const heap_data = static_cast<char *>(malloc(kHeapPreallocSize)); |
| 126 | memset(heap_data, 1, kHeapPreallocSize); |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 127 | __asm__ __volatile__("" ::"m"(heap_data)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 128 | free(heap_data); |
| 129 | } |
| 130 | |
| 131 | void InitRT() { |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 132 | if (FLAGS_skip_locking_memory) { |
| 133 | LOG(WARNING) << "Ignoring request to lock all memory due to " |
| 134 | "--skip_locking_memory."; |
| 135 | return; |
| 136 | } |
| 137 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 138 | CheckNotRealtime(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 139 | LockAllMemory(); |
| 140 | |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 141 | if (FLAGS_skip_realtime_scheduler) { |
| 142 | return; |
| 143 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 144 | // Only let rt processes run for 3 seconds straight. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 145 | SetSoftRLimit( |
| 146 | RLIMIT_RTTIME, 3000000, SetLimitForRoot::kYes, |
| 147 | ", use --skip_realtime_scheduler to stay non-rt and bypass this " |
| 148 | "warning."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 149 | |
| 150 | // Allow rt processes up to priority 40. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 151 | SetSoftRLimit( |
| 152 | RLIMIT_RTPRIO, 40, SetLimitForRoot::kNo, |
| 153 | ", use --skip_realtime_scheduler to stay non-rt and bypass this " |
| 154 | "warning."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void UnsetCurrentThreadRealtimePriority() { |
| 158 | struct sched_param param; |
| 159 | param.sched_priority = 0; |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 160 | PCHECK(sched_setscheduler(0, SCHED_OTHER, ¶m) == 0); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 161 | MarkRealtime(false); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void SetCurrentThreadAffinity(const cpu_set_t &cpuset) { |
| 165 | PCHECK(sched_setaffinity(0, sizeof(cpuset), &cpuset) == 0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 168 | void SetCurrentThreadName(const std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 169 | CHECK_LE(name.size(), 16u) << ": thread name '" << name << "' too long"; |
| 170 | VLOG(1) << "This thread is changing to '" << name << "'"; |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 171 | std::string string_name(name); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 172 | PCHECK(prctl(PR_SET_NAME, string_name.c_str()) == 0) |
| 173 | << ": changing name to " << string_name; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 174 | if (&logging::internal::ReloadThreadName != nullptr) { |
| 175 | logging::internal::ReloadThreadName(); |
| 176 | } |
| 177 | } |
| 178 | |
Austin Schuh | de97329 | 2021-10-12 18:09:49 -0700 | [diff] [blame] | 179 | cpu_set_t GetCurrentThreadAffinity() { |
| 180 | cpu_set_t result; |
| 181 | PCHECK(sched_getaffinity(0, sizeof(result), &result) == 0); |
| 182 | return result; |
| 183 | } |
| 184 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 185 | void SetCurrentThreadRealtimePriority(int priority) { |
James Kuszmaul | a791b76 | 2023-07-13 14:56:21 -0700 | [diff] [blame] | 186 | // Ensure that we won't get expensive reads of /dev/random when the realtime |
| 187 | // scheduler is running. |
| 188 | UUID::Random(); |
| 189 | |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 190 | if (FLAGS_skip_realtime_scheduler) { |
| 191 | LOG(WARNING) << "Ignoring request to switch to the RT scheduler due to " |
| 192 | "--skip_realtime_scheduler."; |
| 193 | return; |
| 194 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 195 | // 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] | 196 | SetSoftRLimit( |
| 197 | RLIMIT_RTTIME, 3000000, SetLimitForRoot::kYes, |
| 198 | ", use --skip_realtime_scheduler to stay non-rt and bypass this " |
| 199 | "warning."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 200 | |
Brian Silverman | b3826f5 | 2020-07-02 19:41:18 -0700 | [diff] [blame] | 201 | // Raise our soft rlimit if necessary. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 202 | SetSoftRLimit( |
| 203 | RLIMIT_RTPRIO, priority, SetLimitForRoot::kNo, |
| 204 | ", use --skip_realtime_scheduler to stay non-rt and bypass this " |
| 205 | "warning.", |
| 206 | AllowSoftLimitDecrease::kNo); |
Brian Silverman | b3826f5 | 2020-07-02 19:41:18 -0700 | [diff] [blame] | 207 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 208 | struct sched_param param; |
| 209 | param.sched_priority = priority; |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 210 | MarkRealtime(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 211 | PCHECK(sched_setscheduler(0, SCHED_FIFO, ¶m) == 0) |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 212 | << ": changing to SCHED_FIFO with " << priority |
| 213 | << ", if you want to bypass this check for testing, use " |
| 214 | "--skip_realtime_scheduler"; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void WriteCoreDumps() { |
| 218 | // Do create core files of unlimited size. |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 219 | SetSoftRLimit(RLIMIT_CORE, RLIM_INFINITY, SetLimitForRoot::kYes, ""); |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void ExpandStackSize() { |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 223 | SetSoftRLimit(RLIMIT_STACK, 1000000, SetLimitForRoot::kYes, "", |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 224 | AllowSoftLimitDecrease::kNo); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 227 | namespace { |
Austin Schuh | f239e33 | 2021-07-30 15:27:26 -0700 | [diff] [blame] | 228 | // Bool to track if malloc hooks have failed to be configured. |
| 229 | bool has_malloc_hook = true; |
Austin Schuh | f7bfb65 | 2023-08-25 14:22:50 -0700 | [diff] [blame] | 230 | thread_local bool is_realtime = false; |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 231 | } // namespace |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 232 | |
| 233 | bool MarkRealtime(bool realtime) { |
Austin Schuh | f239e33 | 2021-07-30 15:27:26 -0700 | [diff] [blame] | 234 | if (realtime) { |
| 235 | // For some applications (generally tools built for the host in Bazel), we |
| 236 | // don't have malloc hooks available, but we also don't go realtime. Delay |
| 237 | // complaining in that case until we try to go RT and it matters. |
Victor Valencia | b009bfc | 2022-09-30 13:30:11 -0700 | [diff] [blame] | 238 | #if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer) |
Austin Schuh | f239e33 | 2021-07-30 15:27:26 -0700 | [diff] [blame] | 239 | CHECK(has_malloc_hook) |
| 240 | << ": Failed to register required malloc hooks before going realtime. " |
| 241 | "Disable --die_on_malloc to continue."; |
Victor Valencia | b009bfc | 2022-09-30 13:30:11 -0700 | [diff] [blame] | 242 | #endif |
Austin Schuh | f239e33 | 2021-07-30 15:27:26 -0700 | [diff] [blame] | 243 | } |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 244 | const bool prior = is_realtime; |
| 245 | is_realtime = realtime; |
| 246 | return prior; |
| 247 | } |
| 248 | |
| 249 | void CheckRealtime() { CHECK(is_realtime); } |
| 250 | |
| 251 | void CheckNotRealtime() { CHECK(!is_realtime); } |
| 252 | |
| 253 | ScopedRealtimeRestorer::ScopedRealtimeRestorer() : prior_(is_realtime) {} |
| 254 | |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 255 | void NewHook(const void *ptr, size_t size) { |
| 256 | if (is_realtime) { |
| 257 | is_realtime = false; |
| 258 | RAW_LOG(FATAL, "Malloced %p -> %zu bytes", ptr, size); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void DeleteHook(const void *ptr) { |
Austin Schuh | f239e33 | 2021-07-30 15:27:26 -0700 | [diff] [blame] | 263 | // It is legal to call free(nullptr) unconditionally and assume that it won't |
| 264 | // do anything. Eigen does this. So, if we are RT, ignore any of these |
| 265 | // calls. |
| 266 | if (is_realtime && ptr != nullptr) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 267 | is_realtime = false; |
| 268 | RAW_LOG(FATAL, "Delete Hook %p", ptr); |
| 269 | } |
| 270 | } |
| 271 | |
Tyler Chatow | 582c6c7 | 2021-07-16 13:45:07 -0700 | [diff] [blame] | 272 | extern "C" { |
| 273 | |
| 274 | // malloc hooks for libc. Tcmalloc will replace everything it finds (malloc, |
| 275 | // __libc_malloc, etc.), so we need its specific hook above as well. |
| 276 | void *aos_malloc_hook(size_t size) { |
| 277 | if (FLAGS_die_on_malloc && aos::is_realtime) { |
| 278 | aos::is_realtime = false; |
| 279 | RAW_LOG(FATAL, "Malloced %zu bytes", size); |
| 280 | return nullptr; |
| 281 | } else { |
| 282 | return __libc_malloc(size); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void aos_free_hook(void *ptr) { |
| 287 | if (FLAGS_die_on_malloc && aos::is_realtime && ptr != nullptr) { |
| 288 | aos::is_realtime = false; |
| 289 | RAW_LOG(FATAL, "Deleted %p", ptr); |
| 290 | } else { |
| 291 | __libc_free(ptr); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void *malloc(size_t size) __attribute__((weak, alias("aos_malloc_hook"))); |
| 296 | void free(void *ptr) __attribute__((weak, alias("aos_free_hook"))); |
Tyler Chatow | 582c6c7 | 2021-07-16 13:45:07 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 299 | void FatalUnsetRealtimePriority() { |
Austin Schuh | 4737ad6 | 2022-12-30 14:47:03 -0800 | [diff] [blame] | 300 | int saved_errno = errno; |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 301 | // Drop our priority first. We are about to do lots of work to undo |
| 302 | // everything, don't get overly clever. |
| 303 | struct sched_param param; |
Austin Schuh | 4737ad6 | 2022-12-30 14:47:03 -0800 | [diff] [blame] | 304 | param.sched_priority = 0; |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 305 | sched_setscheduler(0, SCHED_OTHER, ¶m); |
| 306 | |
| 307 | is_realtime = false; |
| 308 | |
| 309 | // Put all sub-tasks back to non-rt priority too. |
| 310 | DIR *dirp = opendir("/proc/self/task"); |
| 311 | if (dirp) { |
| 312 | struct dirent *directory_entry; |
| 313 | while ((directory_entry = readdir(dirp)) != NULL) { |
| 314 | int thread_id = std::atoi(directory_entry->d_name); |
| 315 | |
| 316 | // ignore . and .. which are zeroes for some reason |
| 317 | if (thread_id != 0) { |
| 318 | struct sched_param param; |
Austin Schuh | 4737ad6 | 2022-12-30 14:47:03 -0800 | [diff] [blame] | 319 | param.sched_priority = 0; |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 320 | sched_setscheduler(thread_id, SCHED_OTHER, ¶m); |
| 321 | } |
| 322 | } |
| 323 | closedir(dirp); |
| 324 | } |
Austin Schuh | 4737ad6 | 2022-12-30 14:47:03 -0800 | [diff] [blame] | 325 | errno = saved_errno; |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 328 | void RegisterMallocHook() { |
| 329 | if (FLAGS_die_on_malloc) { |
Tyler Chatow | 582c6c7 | 2021-07-16 13:45:07 -0700 | [diff] [blame] | 330 | // tcmalloc redefines __libc_malloc, so use this as a feature test. |
| 331 | if (&__libc_malloc == &tc_malloc) { |
James Kuszmaul | 4af7fa6 | 2023-08-05 13:33:22 -0700 | [diff] [blame] | 332 | if (VLOG_IS_ON(1)) { |
| 333 | RAW_LOG(INFO, "Hooking tcmalloc for die_on_malloc"); |
| 334 | } |
Tyler Chatow | 582c6c7 | 2021-07-16 13:45:07 -0700 | [diff] [blame] | 335 | if (&MallocHook_AddNewHook != nullptr) { |
| 336 | CHECK(MallocHook_AddNewHook(&NewHook)); |
| 337 | } else { |
| 338 | has_malloc_hook = false; |
| 339 | } |
| 340 | if (&MallocHook_AddDeleteHook != nullptr) { |
| 341 | CHECK(MallocHook_AddDeleteHook(&DeleteHook)); |
| 342 | } else { |
| 343 | has_malloc_hook = false; |
| 344 | } |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 345 | } else { |
Austin Schuh | 6d8afc0 | 2023-05-28 13:27:42 -0700 | [diff] [blame] | 346 | if (VLOG_IS_ON(1)) { |
| 347 | RAW_LOG(INFO, "Replacing glibc malloc"); |
| 348 | } |
Tyler Chatow | 582c6c7 | 2021-07-16 13:45:07 -0700 | [diff] [blame] | 349 | if (&malloc != &aos_malloc_hook) { |
| 350 | has_malloc_hook = false; |
| 351 | } |
| 352 | if (&free != &aos_free_hook) { |
| 353 | has_malloc_hook = false; |
| 354 | } |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 359 | } // namespace aos |