blob: 47c7f355c48655edb9f69734e5c6319f2b66786f [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
65} // namespace
66
Brian Silverman6da04272014-05-18 18:47:48 -070067void LockAllMemory() {
Brian Silverman8a6dac92015-02-21 20:08:24 -050068 // Allow locking as much as we want into RAM.
69 SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, false);
70
brians343bc112013-02-10 01:53:46 +000071 InitStart();
72 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070073 PDie("%s-init: mlockall failed", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +000074 }
75
Brian Silverman40486622014-12-30 17:38:55 -080076 // Don't give freed memory back to the OS.
77 CHECK_EQ(1, mallopt(M_TRIM_THRESHOLD, -1));
78 // Don't use mmap for large malloc chunks.
79 CHECK_EQ(1, mallopt(M_MMAP_MAX, 0));
80
Brian Silverman26438442015-10-11 19:36:47 -040081 if (&FLAGS_tcmalloc_release_rate) {
82 // Tell tcmalloc not to return memory.
83 FLAGS_tcmalloc_release_rate = 0.0;
84 }
Brian Silverman40486622014-12-30 17:38:55 -080085
brians343bc112013-02-10 01:53:46 +000086 // Forces the memory pages for all the stack space that we're ever going to
87 // use to be loaded into memory (so it can be locked there).
88 uint8_t data[4096 * 8];
89 // Not 0 because linux might optimize that to a 0-filled page.
90 memset(data, 1, sizeof(data));
Brian Silverman40486622014-12-30 17:38:55 -080091
Austin Schuha96cdd92015-03-14 21:09:39 -070092 static const size_t kHeapPreallocSize = 512 * 1024;
Brian Silverman40486622014-12-30 17:38:55 -080093 char *const heap_data = static_cast<char *>(malloc(kHeapPreallocSize));
94 memset(heap_data, 1, kHeapPreallocSize);
95 free(heap_data);
brians343bc112013-02-10 01:53:46 +000096}
97
Brian Silverman8a6dac92015-02-21 20:08:24 -050098void InitNRT() {
99 InitStart();
100 aos_core_create_shared_mem(false, false);
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500101 logging::RegisterQueueImplementation();
Brian Silvermanfeb26532016-01-01 13:41:51 -0800102 LOG(INFO, "%s initialized non-realtime\n", program_invocation_short_name);
Brian Silverman8a6dac92015-02-21 20:08:24 -0500103}
104
105void InitCreate() {
106 InitStart();
107 aos_core_create_shared_mem(true, false);
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500108 logging::RegisterQueueImplementation();
Brian Silvermanfeb26532016-01-01 13:41:51 -0800109 LOG(INFO, "%s created shm\n", program_invocation_short_name);
Brian Silverman8a6dac92015-02-21 20:08:24 -0500110}
111
Brian Silvermanf3cfbd72013-10-28 16:26:09 -0700112void Init(int relative_priority) {
Brian Silverman8a6dac92015-02-21 20:08:24 -0500113 bool realtime = getenv(kNoRealtimeEnvironmentVariable) == nullptr;
114 if (realtime) {
brians343bc112013-02-10 01:53:46 +0000115 LockAllMemory();
Brian Silverman6da04272014-05-18 18:47:48 -0700116
Brian Silverman8dc9fd42014-02-10 13:35:43 -0800117 // Only let rt processes run for 3 seconds straight.
118 SetSoftRLimit(RLIMIT_RTTIME, 3000000, true);
Brian Silverman6da04272014-05-18 18:47:48 -0700119
brians343bc112013-02-10 01:53:46 +0000120 // Allow rt processes up to priority 40.
Brian Silverman80353cb2013-03-19 18:27:53 -0700121 SetSoftRLimit(RLIMIT_RTPRIO, 40, false);
Brian Silverman6da04272014-05-18 18:47:48 -0700122
123 // Set our process to the appropriate priority.
brians343bc112013-02-10 01:53:46 +0000124 struct sched_param param;
Brian Silvermanf3cfbd72013-10-28 16:26:09 -0700125 param.sched_priority = 30 + relative_priority;
brians343bc112013-02-10 01:53:46 +0000126 if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -0700127 PDie("%s-init: setting SCHED_FIFO failed", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +0000128 }
129 } else {
130 fprintf(stderr, "%s not doing realtime initialization because environment"
131 " variable %s is set\n", program_invocation_short_name,
132 kNoRealtimeEnvironmentVariable);
133 printf("no realtime for %s. see stderr\n", program_invocation_short_name);
134 }
135
Brian Silverman8a6dac92015-02-21 20:08:24 -0500136 InitStart();
137 aos_core_create_shared_mem(false, realtime);
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500138 logging::RegisterQueueImplementation();
Brian Silvermanfeb26532016-01-01 13:41:51 -0800139 LOG(INFO, "%s initialized realtime\n", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +0000140}
141
142void Cleanup() {
Brian Silverman01be0002014-05-10 15:44:38 -0700143 aos_core_free_shared_mem();
brians343bc112013-02-10 01:53:46 +0000144}
145
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700146void WriteCoreDumps() {
147 // Do create core files of unlimited size.
148 SetSoftRLimit(RLIMIT_CORE, RLIM_INFINITY, true);
149}
150
Brian Silvermanda45b6c2014-12-28 11:36:50 -0800151void SetCurrentThreadRealtimePriority(int priority) {
Brian Silvermane4d8b282015-12-24 13:44:48 -0800152 // Make sure we will only be allowed to run for 3 seconds straight.
153 SetSoftRLimit(RLIMIT_RTTIME, 3000000, true);
154
Brian Silvermanda45b6c2014-12-28 11:36:50 -0800155 struct sched_param param;
156 param.sched_priority = priority;
157 if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
Brian Silvermana70994f2017-03-16 22:32:55 -0700158 PLOG(FATAL, "sched_setscheduler(0, SCHED_FIFO, %d) failed\n", priority);
159 }
160}
161
162void UnsetCurrentThreadRealtimePriority() {
163 struct sched_param param;
164 param.sched_priority = 0;
165 if (sched_setscheduler(0, SCHED_OTHER, &param) == -1) {
166 PLOG(FATAL, "sched_setscheduler(0, SCHED_OTHER, 0) failed\n");
Brian Silvermanda45b6c2014-12-28 11:36:50 -0800167 }
168}
169
Brian Silvermane4d8b282015-12-24 13:44:48 -0800170void PinCurrentThreadToCPU(int number) {
171 cpu_set_t cpuset;
172 CPU_ZERO(&cpuset);
173 CPU_SET(number, &cpuset);
174 PRCHECK(pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset));
175}
176
Brian Silverman2fe007c2014-12-28 12:20:01 -0800177void SetCurrentThreadName(const ::std::string &name) {
178 if (name.size() > 16) {
179 LOG(FATAL, "thread name '%s' too long\n", name.c_str());
180 }
181 LOG(INFO, "this thread is changing to '%s'\n", name.c_str());
182 PCHECK(prctl(PR_SET_NAME, name.c_str()));
183 logging::internal::ReloadThreadName();
184}
185
brians343bc112013-02-10 01:53:46 +0000186} // namespace aos