blob: e8bcba1d10902f985c709b1e0611d34b0f5b156e [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) {
27 Die("%s-init: getrlimit64(%d) failed with %d (%s)\n",
28 program_invocation_short_name, resource, errno, strerror(errno));
29 }
30 rlim.rlim_cur = soft;
31 if (setrlimit64(resource, &rlim) == -1) {
Brian Silvermanafb71d32014-01-01 13:34:31 -080032 Die("%s-init: setrlimit64(%d, {cur=%ju,max=%ju})"
brians343bc112013-02-10 01:53:46 +000033 " failed with %d (%s)\n", program_invocation_short_name,
Brian Silvermanafb71d32014-01-01 13:34:31 -080034 resource, (uintmax_t)rlim.rlim_cur, (uintmax_t)rlim.rlim_max,
brians343bc112013-02-10 01:53:46 +000035 errno, strerror(errno));
36 }
37 }
38}
39
40// Common stuff that needs to happen at the beginning of both the realtime and
41// non-realtime initialization sequences. May be called twice.
42void InitStart() {
43 // 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
48int LockAllMemory() {
49 InitStart();
50 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
51 Die("%s-init: mlockall failed with %d (%s)\n",
52 program_invocation_short_name, errno, strerror(errno));
53 }
54
55 // Forces the memory pages for all the stack space that we're ever going to
56 // use to be loaded into memory (so it can be locked there).
57 uint8_t data[4096 * 8];
58 // Not 0 because linux might optimize that to a 0-filled page.
59 memset(data, 1, sizeof(data));
60
61 return 0;
62}
63
64// Do the initialization code that is necessary for both realtime and
65// non-realtime processes.
66void DoInitNRT(aos_core_create create) {
67 InitStart();
68 if (aos_core_create_shared_mem(create)) {
69 Die("%s-init: creating shared memory reference failed\n",
70 program_invocation_short_name);
71 }
Brian Silverman14fd0fb2014-01-14 21:42:01 -080072 logging::linux_code::Register();
brians343bc112013-02-10 01:53:46 +000073}
74
75const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME";
76
77} // namespace
78
79void InitNRT() { DoInitNRT(aos_core_create::reference); }
80void InitCreate() { DoInitNRT(aos_core_create::create); }
Brian Silvermanf3cfbd72013-10-28 16:26:09 -070081void Init(int relative_priority) {
brians343bc112013-02-10 01:53:46 +000082 if (getenv(kNoRealtimeEnvironmentVariable) == NULL) { // if nobody set it
83 LockAllMemory();
Brian Silverman8dc9fd42014-02-10 13:35:43 -080084 // Only let rt processes run for 3 seconds straight.
85 SetSoftRLimit(RLIMIT_RTTIME, 3000000, true);
brians343bc112013-02-10 01:53:46 +000086 // Allow rt processes up to priority 40.
Brian Silverman80353cb2013-03-19 18:27:53 -070087 SetSoftRLimit(RLIMIT_RTPRIO, 40, false);
brians343bc112013-02-10 01:53:46 +000088 // Set our process to priority 40.
89 struct sched_param param;
Brian Silvermanf3cfbd72013-10-28 16:26:09 -070090 param.sched_priority = 30 + relative_priority;
brians343bc112013-02-10 01:53:46 +000091 if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
92 Die("%s-init: setting SCHED_FIFO failed with %d (%s)\n",
93 program_invocation_short_name, errno, strerror(errno));
94 }
95 } else {
96 fprintf(stderr, "%s not doing realtime initialization because environment"
97 " variable %s is set\n", program_invocation_short_name,
98 kNoRealtimeEnvironmentVariable);
99 printf("no realtime for %s. see stderr\n", program_invocation_short_name);
100 }
101
102 InitNRT();
103}
104
105void Cleanup() {
106 if (aos_core_free_shared_mem()) {
107 Die("%s-init: freeing shared mem failed\n",
108 program_invocation_short_name);
109 }
110}
111
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700112void WriteCoreDumps() {
113 // Do create core files of unlimited size.
114 SetSoftRLimit(RLIMIT_CORE, RLIM_INFINITY, true);
115}
116
brians343bc112013-02-10 01:53:46 +0000117} // namespace aos