blob: ca14ab3cdd9e98e44bf6e519d86d75f3cd86353d [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 Silverman14fd0fb2014-01-14 21:42:01 -080017#include "aos/linux_code/logging/linux_logging.h"
18#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 Silverman2fe007c2014-12-28 12:20:01 -080030// Implemented in aos/linux_code/logging/linux_interface.cc.
31void 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
Brian Silverman6da04272014-05-18 18:47:48 -070063void LockAllMemory() {
Brian Silverman8a6dac92015-02-21 20:08:24 -050064 // Allow locking as much as we want into RAM.
65 SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, false);
66
brians343bc112013-02-10 01:53:46 +000067 InitStart();
68 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070069 PDie("%s-init: mlockall failed", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +000070 }
71
Brian Silverman40486622014-12-30 17:38:55 -080072 // Don't give freed memory back to the OS.
73 CHECK_EQ(1, mallopt(M_TRIM_THRESHOLD, -1));
74 // Don't use mmap for large malloc chunks.
75 CHECK_EQ(1, mallopt(M_MMAP_MAX, 0));
76
Brian Silverman26438442015-10-11 19:36:47 -040077 if (&FLAGS_tcmalloc_release_rate) {
78 // Tell tcmalloc not to return memory.
79 FLAGS_tcmalloc_release_rate = 0.0;
80 }
Brian Silverman40486622014-12-30 17:38:55 -080081
brians343bc112013-02-10 01:53:46 +000082 // Forces the memory pages for all the stack space that we're ever going to
83 // use to be loaded into memory (so it can be locked there).
84 uint8_t data[4096 * 8];
85 // Not 0 because linux might optimize that to a 0-filled page.
86 memset(data, 1, sizeof(data));
Brian Silverman40486622014-12-30 17:38:55 -080087
Austin Schuha96cdd92015-03-14 21:09:39 -070088 static const size_t kHeapPreallocSize = 512 * 1024;
Brian Silverman40486622014-12-30 17:38:55 -080089 char *const heap_data = static_cast<char *>(malloc(kHeapPreallocSize));
90 memset(heap_data, 1, kHeapPreallocSize);
91 free(heap_data);
brians343bc112013-02-10 01:53:46 +000092}
93
brians343bc112013-02-10 01:53:46 +000094const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME";
95
96} // namespace
97
Brian Silverman8a6dac92015-02-21 20:08:24 -050098void InitNRT() {
99 InitStart();
100 aos_core_create_shared_mem(false, false);
101 logging::linux_code::Register();
102}
103
104void InitCreate() {
105 InitStart();
106 aos_core_create_shared_mem(true, false);
107 logging::linux_code::Register();
108}
109
Brian Silvermanf3cfbd72013-10-28 16:26:09 -0700110void Init(int relative_priority) {
Brian Silverman8a6dac92015-02-21 20:08:24 -0500111 bool realtime = getenv(kNoRealtimeEnvironmentVariable) == nullptr;
112 if (realtime) {
brians343bc112013-02-10 01:53:46 +0000113 LockAllMemory();
Brian Silverman6da04272014-05-18 18:47:48 -0700114
Brian Silverman8dc9fd42014-02-10 13:35:43 -0800115 // Only let rt processes run for 3 seconds straight.
116 SetSoftRLimit(RLIMIT_RTTIME, 3000000, true);
Brian Silverman6da04272014-05-18 18:47:48 -0700117
brians343bc112013-02-10 01:53:46 +0000118 // Allow rt processes up to priority 40.
Brian Silverman80353cb2013-03-19 18:27:53 -0700119 SetSoftRLimit(RLIMIT_RTPRIO, 40, false);
Brian Silverman6da04272014-05-18 18:47:48 -0700120
121 // Set our process to the appropriate priority.
brians343bc112013-02-10 01:53:46 +0000122 struct sched_param param;
Brian Silvermanf3cfbd72013-10-28 16:26:09 -0700123 param.sched_priority = 30 + relative_priority;
brians343bc112013-02-10 01:53:46 +0000124 if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -0700125 PDie("%s-init: setting SCHED_FIFO failed", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +0000126 }
127 } else {
128 fprintf(stderr, "%s not doing realtime initialization because environment"
129 " variable %s is set\n", program_invocation_short_name,
130 kNoRealtimeEnvironmentVariable);
131 printf("no realtime for %s. see stderr\n", program_invocation_short_name);
132 }
133
Brian Silverman8a6dac92015-02-21 20:08:24 -0500134 InitStart();
135 aos_core_create_shared_mem(false, realtime);
136 logging::linux_code::Register();
brians343bc112013-02-10 01:53:46 +0000137}
138
139void Cleanup() {
Brian Silverman01be0002014-05-10 15:44:38 -0700140 aos_core_free_shared_mem();
brians343bc112013-02-10 01:53:46 +0000141}
142
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700143void WriteCoreDumps() {
144 // Do create core files of unlimited size.
145 SetSoftRLimit(RLIMIT_CORE, RLIM_INFINITY, true);
146}
147
Brian Silvermanda45b6c2014-12-28 11:36:50 -0800148void SetCurrentThreadRealtimePriority(int priority) {
149 struct sched_param param;
150 param.sched_priority = priority;
151 if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
152 PLOG(FATAL, "sched_setscheduler(0, SCHED_FIFO, %d) failed", priority);
153 }
154}
155
Brian Silverman2fe007c2014-12-28 12:20:01 -0800156void SetCurrentThreadName(const ::std::string &name) {
157 if (name.size() > 16) {
158 LOG(FATAL, "thread name '%s' too long\n", name.c_str());
159 }
160 LOG(INFO, "this thread is changing to '%s'\n", name.c_str());
161 PCHECK(prctl(PR_SET_NAME, name.c_str()));
162 logging::internal::ReloadThreadName();
163}
164
brians343bc112013-02-10 01:53:46 +0000165} // namespace aos