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