blob: ce557e45fe698f9df381ef9da6450730e7890baf [file] [log] [blame]
Brian Silverman14fd0fb2014-01-14 21:42:01 -08001#include "aos/linux_code/init.h"
brians343bc112013-02-10 01:53:46 +00002
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>
brians343bc112013-02-10 01:53:46 +00009#include <sys/types.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <stdint.h>
Brian Silverman2fe007c2014-12-28 12:20:01 -080013#include <sys/prctl.h>
Brian Silverman40486622014-12-30 17:38:55 -080014#include <malloc.h>
brians343bc112013-02-10 01:53:46 +000015
brians343bc112013-02-10 01:53:46 +000016#include "aos/common/die.h"
Brian Silvermancb5da1f2015-12-05 22:19:58 -050017#include "aos/common/logging/implementations.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -080018#include "aos/linux_code/ipc_lib/shared_mem.h"
brians343bc112013-02-10 01:53:46 +000019
Brian Silverman40486622014-12-30 17:38:55 -080020namespace FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead {
Brian Silverman26438442015-10-11 19:36:47 -040021extern double FLAGS_tcmalloc_release_rate __attribute__((weak));
Brian Silverman40486622014-12-30 17:38:55 -080022}
23using FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead::
24 FLAGS_tcmalloc_release_rate;
Brian Silverman40486622014-12-30 17:38:55 -080025
brians343bc112013-02-10 01:53:46 +000026namespace aos {
Brian Silverman2fe007c2014-12-28 12:20:01 -080027namespace logging {
28namespace internal {
brians343bc112013-02-10 01:53:46 +000029
Brian Silvermancb5da1f2015-12-05 22:19:58 -050030// Implemented in aos/common/logging/context.cc.
Brian Silverman2fe007c2014-12-28 12:20:01 -080031void ReloadThreadName();
32
33} // namespace internal
34} // namespace logging
brians343bc112013-02-10 01:53:46 +000035namespace {
36
Brian Silverman80353cb2013-03-19 18:27:53 -070037void SetSoftRLimit(int resource, rlim64_t soft, bool set_for_root) {
38 bool am_root = getuid() == 0;
39 if (set_for_root || !am_root) {
brians343bc112013-02-10 01:53:46 +000040 struct rlimit64 rlim;
41 if (getrlimit64(resource, &rlim) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070042 PDie("%s-init: getrlimit64(%d) failed",
43 program_invocation_short_name, resource);
brians343bc112013-02-10 01:53:46 +000044 }
45 rlim.rlim_cur = soft;
Austin Schuh6c7edbf2014-10-21 22:26:58 -070046 rlim.rlim_max = ::std::max(rlim.rlim_max, soft);
47
brians343bc112013-02-10 01:53:46 +000048 if (setrlimit64(resource, &rlim) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070049 PDie("%s-init: setrlimit64(%d, {cur=%ju,max=%ju}) failed",
50 program_invocation_short_name, resource, (uintmax_t)rlim.rlim_cur,
51 (uintmax_t)rlim.rlim_max);
brians343bc112013-02-10 01:53:46 +000052 }
53 }
54}
55
56// Common stuff that needs to happen at the beginning of both the realtime and
57// non-realtime initialization sequences. May be called twice.
58void InitStart() {
Brian Silvermanff485782014-06-18 19:59:09 -070059 ::aos::logging::Init();
Brian Silvermanfe1ef172014-04-12 17:12:45 -070060 WriteCoreDumps();
brians343bc112013-02-10 01:53:46 +000061}
62
Austin Schuh3d4d5df2015-10-17 15:51:41 -070063const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME";
64
Brian Silverman8f8debf2018-03-11 19:30:23 -070065bool ShouldBeRealtime() {
66 return getenv(kNoRealtimeEnvironmentVariable) == nullptr;
67}
68
Austin Schuh3d4d5df2015-10-17 15:51:41 -070069} // namespace
70
Brian Silverman6da04272014-05-18 18:47:48 -070071void LockAllMemory() {
Brian Silverman8a6dac92015-02-21 20:08:24 -050072 // Allow locking as much as we want into RAM.
73 SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, false);
74
brians343bc112013-02-10 01:53:46 +000075 InitStart();
76 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070077 PDie("%s-init: mlockall failed", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +000078 }
79
Brian Silverman40486622014-12-30 17:38:55 -080080 // Don't give freed memory back to the OS.
81 CHECK_EQ(1, mallopt(M_TRIM_THRESHOLD, -1));
82 // Don't use mmap for large malloc chunks.
83 CHECK_EQ(1, mallopt(M_MMAP_MAX, 0));
84
Brian Silverman26438442015-10-11 19:36:47 -040085 if (&FLAGS_tcmalloc_release_rate) {
86 // Tell tcmalloc not to return memory.
87 FLAGS_tcmalloc_release_rate = 0.0;
88 }
Brian Silverman40486622014-12-30 17:38:55 -080089
brians343bc112013-02-10 01:53:46 +000090 // Forces the memory pages for all the stack space that we're ever going to
91 // use to be loaded into memory (so it can be locked there).
92 uint8_t data[4096 * 8];
93 // Not 0 because linux might optimize that to a 0-filled page.
94 memset(data, 1, sizeof(data));
Brian Silverman40486622014-12-30 17:38:55 -080095
Austin Schuha96cdd92015-03-14 21:09:39 -070096 static const size_t kHeapPreallocSize = 512 * 1024;
Brian Silverman40486622014-12-30 17:38:55 -080097 char *const heap_data = static_cast<char *>(malloc(kHeapPreallocSize));
98 memset(heap_data, 1, kHeapPreallocSize);
99 free(heap_data);
brians343bc112013-02-10 01:53:46 +0000100}
101
Brian Silverman8f8debf2018-03-11 19:30:23 -0700102void InitNRT(bool for_realtime) {
Brian Silverman8a6dac92015-02-21 20:08:24 -0500103 InitStart();
Brian Silverman8f8debf2018-03-11 19:30:23 -0700104 aos_core_create_shared_mem(false, for_realtime && ShouldBeRealtime());
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500105 logging::RegisterQueueImplementation();
Brian Silvermanfeb26532016-01-01 13:41:51 -0800106 LOG(INFO, "%s initialized non-realtime\n", program_invocation_short_name);
Brian Silverman8a6dac92015-02-21 20:08:24 -0500107}
108
109void InitCreate() {
110 InitStart();
111 aos_core_create_shared_mem(true, false);
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500112 logging::RegisterQueueImplementation();
Brian Silvermanfeb26532016-01-01 13:41:51 -0800113 LOG(INFO, "%s created shm\n", program_invocation_short_name);
Brian Silverman8a6dac92015-02-21 20:08:24 -0500114}
115
Brian Silvermanf3cfbd72013-10-28 16:26:09 -0700116void Init(int relative_priority) {
Brian Silverman8f8debf2018-03-11 19:30:23 -0700117 InitStart();
118 aos_core_create_shared_mem(false, ShouldBeRealtime());
119 logging::RegisterQueueImplementation();
120 GoRT(relative_priority);
121}
122
123void GoRT(int relative_priority) {
124 if (ShouldBeRealtime()) {
brians343bc112013-02-10 01:53:46 +0000125 LockAllMemory();
Brian Silverman6da04272014-05-18 18:47:48 -0700126
Brian Silverman8dc9fd42014-02-10 13:35:43 -0800127 // Only let rt processes run for 3 seconds straight.
128 SetSoftRLimit(RLIMIT_RTTIME, 3000000, true);
Brian Silverman6da04272014-05-18 18:47:48 -0700129
brians343bc112013-02-10 01:53:46 +0000130 // Allow rt processes up to priority 40.
Brian Silverman80353cb2013-03-19 18:27:53 -0700131 SetSoftRLimit(RLIMIT_RTPRIO, 40, false);
Brian Silverman6da04272014-05-18 18:47:48 -0700132
133 // Set our process to the appropriate priority.
brians343bc112013-02-10 01:53:46 +0000134 struct sched_param param;
Brian Silvermanf3cfbd72013-10-28 16:26:09 -0700135 param.sched_priority = 30 + relative_priority;
brians343bc112013-02-10 01:53:46 +0000136 if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -0700137 PDie("%s-init: setting SCHED_FIFO failed", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +0000138 }
139 } else {
140 fprintf(stderr, "%s not doing realtime initialization because environment"
141 " variable %s is set\n", program_invocation_short_name,
142 kNoRealtimeEnvironmentVariable);
143 printf("no realtime for %s. see stderr\n", program_invocation_short_name);
144 }
145
Brian Silvermanfeb26532016-01-01 13:41:51 -0800146 LOG(INFO, "%s initialized realtime\n", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +0000147}
148
149void Cleanup() {
Brian Silverman01be0002014-05-10 15:44:38 -0700150 aos_core_free_shared_mem();
brians343bc112013-02-10 01:53:46 +0000151}
152
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700153void WriteCoreDumps() {
154 // Do create core files of unlimited size.
155 SetSoftRLimit(RLIMIT_CORE, RLIM_INFINITY, true);
156}
157
Brian Silvermanda45b6c2014-12-28 11:36:50 -0800158void SetCurrentThreadRealtimePriority(int priority) {
Brian Silvermane4d8b282015-12-24 13:44:48 -0800159 // Make sure we will only be allowed to run for 3 seconds straight.
160 SetSoftRLimit(RLIMIT_RTTIME, 3000000, true);
161
Brian Silvermanda45b6c2014-12-28 11:36:50 -0800162 struct sched_param param;
163 param.sched_priority = priority;
164 if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
Brian Silvermana70994f2017-03-16 22:32:55 -0700165 PLOG(FATAL, "sched_setscheduler(0, SCHED_FIFO, %d) failed\n", priority);
166 }
167}
168
169void UnsetCurrentThreadRealtimePriority() {
170 struct sched_param param;
171 param.sched_priority = 0;
172 if (sched_setscheduler(0, SCHED_OTHER, &param) == -1) {
173 PLOG(FATAL, "sched_setscheduler(0, SCHED_OTHER, 0) failed\n");
Brian Silvermanda45b6c2014-12-28 11:36:50 -0800174 }
175}
176
Brian Silvermane4d8b282015-12-24 13:44:48 -0800177void PinCurrentThreadToCPU(int number) {
178 cpu_set_t cpuset;
179 CPU_ZERO(&cpuset);
180 CPU_SET(number, &cpuset);
181 PRCHECK(pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset));
182}
183
Brian Silverman2fe007c2014-12-28 12:20:01 -0800184void SetCurrentThreadName(const ::std::string &name) {
185 if (name.size() > 16) {
186 LOG(FATAL, "thread name '%s' too long\n", name.c_str());
187 }
188 LOG(INFO, "this thread is changing to '%s'\n", name.c_str());
189 PCHECK(prctl(PR_SET_NAME, name.c_str()));
190 logging::internal::ReloadThreadName();
191}
192
brians343bc112013-02-10 01:53:46 +0000193} // namespace aos