Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/realtime.h" |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <sys/mman.h> |
| 6 | #include <errno.h> |
| 7 | #include <sched.h> |
| 8 | #include <sys/resource.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <stdint.h> |
| 13 | #include <sys/prctl.h> |
| 14 | #include <malloc.h> |
| 15 | |
| 16 | #include "glog/logging.h" |
| 17 | |
| 18 | namespace FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead { |
| 19 | extern double FLAGS_tcmalloc_release_rate __attribute__((weak)); |
| 20 | } |
| 21 | using FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead:: |
| 22 | FLAGS_tcmalloc_release_rate; |
| 23 | |
| 24 | namespace aos { |
| 25 | namespace logging { |
| 26 | namespace internal { |
| 27 | |
| 28 | // Implemented in aos/logging/context.cc. |
| 29 | void ReloadThreadName() __attribute__((weak)); |
| 30 | |
| 31 | } // namespace internal |
| 32 | } // namespace logging |
| 33 | |
| 34 | namespace { |
| 35 | |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 36 | enum class SetLimitForRoot { |
| 37 | kYes, |
| 38 | kNo |
| 39 | }; |
| 40 | |
| 41 | enum class AllowSoftLimitDecrease { |
| 42 | kYes, |
| 43 | kNo |
| 44 | }; |
| 45 | |
| 46 | void SetSoftRLimit( |
| 47 | int resource, rlim64_t soft, SetLimitForRoot set_for_root, |
| 48 | AllowSoftLimitDecrease allow_decrease = AllowSoftLimitDecrease::kYes) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 49 | bool am_root = getuid() == 0; |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 50 | if (set_for_root == SetLimitForRoot::kYes || !am_root) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 51 | struct rlimit64 rlim; |
| 52 | PCHECK(getrlimit64(resource, &rlim) == 0) |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 53 | << ": getting limit for " << resource; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 54 | |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 55 | if (allow_decrease == AllowSoftLimitDecrease::kYes) { |
| 56 | rlim.rlim_cur = soft; |
| 57 | } else { |
| 58 | rlim.rlim_cur = std::max(rlim.rlim_cur, soft); |
| 59 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 60 | rlim.rlim_max = ::std::max(rlim.rlim_max, soft); |
| 61 | |
| 62 | PCHECK(setrlimit64(resource, &rlim) == 0) |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 63 | << ": changing limit for " << resource << " to " << rlim.rlim_cur |
| 64 | << " with max of " << rlim.rlim_max; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | } // namespace |
| 69 | |
| 70 | void LockAllMemory() { |
| 71 | // Allow locking as much as we want into RAM. |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 72 | SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, SetLimitForRoot::kNo); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 73 | |
| 74 | WriteCoreDumps(); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 75 | PCHECK(mlockall(MCL_CURRENT | MCL_FUTURE) == 0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 76 | |
| 77 | // Don't give freed memory back to the OS. |
| 78 | CHECK_EQ(1, mallopt(M_TRIM_THRESHOLD, -1)); |
| 79 | // Don't use mmap for large malloc chunks. |
| 80 | CHECK_EQ(1, mallopt(M_MMAP_MAX, 0)); |
| 81 | |
| 82 | if (&FLAGS_tcmalloc_release_rate) { |
| 83 | // Tell tcmalloc not to return memory. |
| 84 | FLAGS_tcmalloc_release_rate = 0.0; |
| 85 | } |
| 86 | |
| 87 | // Forces the memory pages for all the stack space that we're ever going to |
| 88 | // use to be loaded into memory (so it can be locked there). |
| 89 | uint8_t data[4096 * 8]; |
| 90 | // Not 0 because linux might optimize that to a 0-filled page. |
| 91 | memset(data, 1, sizeof(data)); |
Lee Mracek | 6afc0ac | 2020-03-06 12:42:33 -0500 | [diff] [blame] | 92 | __asm__ __volatile__("" :: "m" (data)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 93 | |
| 94 | static const size_t kHeapPreallocSize = 512 * 1024; |
| 95 | char *const heap_data = static_cast<char *>(malloc(kHeapPreallocSize)); |
| 96 | memset(heap_data, 1, kHeapPreallocSize); |
Lee Mracek | 6afc0ac | 2020-03-06 12:42:33 -0500 | [diff] [blame] | 97 | __asm__ __volatile__("" :: "m" (heap_data)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 98 | free(heap_data); |
| 99 | } |
| 100 | |
| 101 | void InitRT() { |
| 102 | LockAllMemory(); |
| 103 | |
| 104 | // Only let rt processes run for 3 seconds straight. |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 105 | SetSoftRLimit(RLIMIT_RTTIME, 3000000, SetLimitForRoot::kYes); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 106 | |
| 107 | // Allow rt processes up to priority 40. |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 108 | SetSoftRLimit(RLIMIT_RTPRIO, 40, SetLimitForRoot::kNo); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void UnsetCurrentThreadRealtimePriority() { |
| 112 | struct sched_param param; |
| 113 | param.sched_priority = 0; |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 114 | PCHECK(sched_setscheduler(0, SCHED_OTHER, ¶m) == 0); |
| 115 | } |
| 116 | |
| 117 | void SetCurrentThreadAffinity(const cpu_set_t &cpuset) { |
| 118 | PCHECK(sched_setaffinity(0, sizeof(cpuset), &cpuset) == 0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 119 | } |
| 120 | |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 121 | void SetCurrentThreadName(const std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 122 | CHECK_LE(name.size(), 16u) << ": thread name '" << name << "' too long"; |
| 123 | VLOG(1) << "This thread is changing to '" << name << "'"; |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 124 | std::string string_name(name); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 125 | PCHECK(prctl(PR_SET_NAME, string_name.c_str()) == 0) |
| 126 | << ": changing name to " << string_name; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 127 | if (&logging::internal::ReloadThreadName != nullptr) { |
| 128 | logging::internal::ReloadThreadName(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void SetCurrentThreadRealtimePriority(int priority) { |
| 133 | // Make sure we will only be allowed to run for 3 seconds straight. |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 134 | SetSoftRLimit(RLIMIT_RTTIME, 3000000, SetLimitForRoot::kYes); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 135 | |
Brian Silverman | b3826f5 | 2020-07-02 19:41:18 -0700 | [diff] [blame] | 136 | // Raise our soft rlimit if necessary. |
| 137 | SetSoftRLimit(RLIMIT_RTPRIO, priority, SetLimitForRoot::kNo, |
| 138 | AllowSoftLimitDecrease::kNo); |
| 139 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 140 | struct sched_param param; |
| 141 | param.sched_priority = priority; |
| 142 | PCHECK(sched_setscheduler(0, SCHED_FIFO, ¶m) == 0) |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 143 | << ": changing to SCHED_FIFO with " << priority; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void WriteCoreDumps() { |
| 147 | // Do create core files of unlimited size. |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 148 | SetSoftRLimit(RLIMIT_CORE, RLIM_INFINITY, SetLimitForRoot::kYes); |
| 149 | } |
| 150 | |
| 151 | void ExpandStackSize() { |
| 152 | SetSoftRLimit(RLIMIT_STACK, 1000000, SetLimitForRoot::kYes, |
| 153 | AllowSoftLimitDecrease::kNo); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace aos |