blob: 7405237f8b4d1bf8b85b4399d00abf5b0bbc2796 [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>
13
brians343bc112013-02-10 01:53:46 +000014#include "aos/common/die.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -080015#include "aos/linux_code/logging/linux_logging.h"
16#include "aos/linux_code/ipc_lib/shared_mem.h"
brians343bc112013-02-10 01:53:46 +000017
18namespace aos {
19
20namespace {
21
Brian Silverman80353cb2013-03-19 18:27:53 -070022void SetSoftRLimit(int resource, rlim64_t soft, bool set_for_root) {
23 bool am_root = getuid() == 0;
24 if (set_for_root || !am_root) {
brians343bc112013-02-10 01:53:46 +000025 struct rlimit64 rlim;
26 if (getrlimit64(resource, &rlim) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070027 PDie("%s-init: getrlimit64(%d) failed",
28 program_invocation_short_name, resource);
brians343bc112013-02-10 01:53:46 +000029 }
30 rlim.rlim_cur = soft;
31 if (setrlimit64(resource, &rlim) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070032 PDie("%s-init: setrlimit64(%d, {cur=%ju,max=%ju}) failed",
33 program_invocation_short_name, resource, (uintmax_t)rlim.rlim_cur,
34 (uintmax_t)rlim.rlim_max);
brians343bc112013-02-10 01:53:46 +000035 }
36 }
37}
38
39// Common stuff that needs to happen at the beginning of both the realtime and
40// non-realtime initialization sequences. May be called twice.
41void InitStart() {
Brian Silvermanff485782014-06-18 19:59:09 -070042 ::aos::logging::Init();
brians343bc112013-02-10 01:53:46 +000043 // Allow locking as much as we want into RAM.
Brian Silverman80353cb2013-03-19 18:27:53 -070044 SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, false);
Brian Silvermanfe1ef172014-04-12 17:12:45 -070045 WriteCoreDumps();
brians343bc112013-02-10 01:53:46 +000046}
47
Brian Silverman6da04272014-05-18 18:47:48 -070048void LockAllMemory() {
brians343bc112013-02-10 01:53:46 +000049 InitStart();
50 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070051 PDie("%s-init: mlockall failed", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +000052 }
53
54 // Forces the memory pages for all the stack space that we're ever going to
55 // use to be loaded into memory (so it can be locked there).
56 uint8_t data[4096 * 8];
57 // Not 0 because linux might optimize that to a 0-filled page.
58 memset(data, 1, sizeof(data));
brians343bc112013-02-10 01:53:46 +000059}
60
61// Do the initialization code that is necessary for both realtime and
62// non-realtime processes.
63void DoInitNRT(aos_core_create create) {
64 InitStart();
Brian Silverman01be0002014-05-10 15:44:38 -070065 aos_core_create_shared_mem(create);
Brian Silverman14fd0fb2014-01-14 21:42:01 -080066 logging::linux_code::Register();
brians343bc112013-02-10 01:53:46 +000067}
68
69const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME";
70
71} // namespace
72
73void InitNRT() { DoInitNRT(aos_core_create::reference); }
74void InitCreate() { DoInitNRT(aos_core_create::create); }
Brian Silvermanf3cfbd72013-10-28 16:26:09 -070075void Init(int relative_priority) {
brians343bc112013-02-10 01:53:46 +000076 if (getenv(kNoRealtimeEnvironmentVariable) == NULL) { // if nobody set it
77 LockAllMemory();
Brian Silverman6da04272014-05-18 18:47:48 -070078
Brian Silverman8dc9fd42014-02-10 13:35:43 -080079 // Only let rt processes run for 3 seconds straight.
80 SetSoftRLimit(RLIMIT_RTTIME, 3000000, true);
Brian Silverman6da04272014-05-18 18:47:48 -070081
brians343bc112013-02-10 01:53:46 +000082 // Allow rt processes up to priority 40.
Brian Silverman80353cb2013-03-19 18:27:53 -070083 SetSoftRLimit(RLIMIT_RTPRIO, 40, false);
Brian Silverman6da04272014-05-18 18:47:48 -070084
85 // Set our process to the appropriate priority.
brians343bc112013-02-10 01:53:46 +000086 struct sched_param param;
Brian Silvermanf3cfbd72013-10-28 16:26:09 -070087 param.sched_priority = 30 + relative_priority;
brians343bc112013-02-10 01:53:46 +000088 if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -070089 PDie("%s-init: setting SCHED_FIFO failed", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +000090 }
91 } else {
92 fprintf(stderr, "%s not doing realtime initialization because environment"
93 " variable %s is set\n", program_invocation_short_name,
94 kNoRealtimeEnvironmentVariable);
95 printf("no realtime for %s. see stderr\n", program_invocation_short_name);
96 }
97
98 InitNRT();
99}
100
101void Cleanup() {
Brian Silverman01be0002014-05-10 15:44:38 -0700102 aos_core_free_shared_mem();
brians343bc112013-02-10 01:53:46 +0000103}
104
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700105void WriteCoreDumps() {
106 // Do create core files of unlimited size.
107 SetSoftRLimit(RLIMIT_CORE, RLIM_INFINITY, true);
108}
109
brians343bc112013-02-10 01:53:46 +0000110} // namespace aos