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