blob: a4e72dbcd2df070f87f7f5363c6cea7f6d2e5800 [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;
Austin Schuh6c7edbf2014-10-21 22:26:58 -070031 rlim.rlim_max = ::std::max(rlim.rlim_max, soft);
32
brians343bc112013-02-10 01:53:46 +000033 if (setrlimit64(resource, &rlim) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070034 PDie("%s-init: setrlimit64(%d, {cur=%ju,max=%ju}) failed",
35 program_invocation_short_name, resource, (uintmax_t)rlim.rlim_cur,
36 (uintmax_t)rlim.rlim_max);
brians343bc112013-02-10 01:53:46 +000037 }
38 }
39}
40
41// Common stuff that needs to happen at the beginning of both the realtime and
42// non-realtime initialization sequences. May be called twice.
43void InitStart() {
Brian Silvermanff485782014-06-18 19:59:09 -070044 ::aos::logging::Init();
brians343bc112013-02-10 01:53:46 +000045 // Allow locking as much as we want into RAM.
Brian Silverman80353cb2013-03-19 18:27:53 -070046 SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, false);
Brian Silvermanfe1ef172014-04-12 17:12:45 -070047 WriteCoreDumps();
brians343bc112013-02-10 01:53:46 +000048}
49
Brian Silverman6da04272014-05-18 18:47:48 -070050void LockAllMemory() {
brians343bc112013-02-10 01:53:46 +000051 InitStart();
52 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070053 PDie("%s-init: mlockall failed", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +000054 }
55
56 // Forces the memory pages for all the stack space that we're ever going to
57 // use to be loaded into memory (so it can be locked there).
58 uint8_t data[4096 * 8];
59 // Not 0 because linux might optimize that to a 0-filled page.
60 memset(data, 1, sizeof(data));
brians343bc112013-02-10 01:53:46 +000061}
62
63// Do the initialization code that is necessary for both realtime and
64// non-realtime processes.
65void DoInitNRT(aos_core_create create) {
66 InitStart();
Brian Silverman01be0002014-05-10 15:44:38 -070067 aos_core_create_shared_mem(create);
Brian Silverman14fd0fb2014-01-14 21:42:01 -080068 logging::linux_code::Register();
brians343bc112013-02-10 01:53:46 +000069}
70
71const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME";
72
73} // namespace
74
75void InitNRT() { DoInitNRT(aos_core_create::reference); }
76void InitCreate() { DoInitNRT(aos_core_create::create); }
Brian Silvermanf3cfbd72013-10-28 16:26:09 -070077void Init(int relative_priority) {
brians343bc112013-02-10 01:53:46 +000078 if (getenv(kNoRealtimeEnvironmentVariable) == NULL) { // if nobody set it
79 LockAllMemory();
Brian Silverman6da04272014-05-18 18:47:48 -070080
Brian Silverman8dc9fd42014-02-10 13:35:43 -080081 // Only let rt processes run for 3 seconds straight.
82 SetSoftRLimit(RLIMIT_RTTIME, 3000000, true);
Brian Silverman6da04272014-05-18 18:47:48 -070083
brians343bc112013-02-10 01:53:46 +000084 // Allow rt processes up to priority 40.
Brian Silverman80353cb2013-03-19 18:27:53 -070085 SetSoftRLimit(RLIMIT_RTPRIO, 40, false);
Brian Silverman6da04272014-05-18 18:47:48 -070086
87 // Set our process to the appropriate priority.
brians343bc112013-02-10 01:53:46 +000088 struct sched_param param;
Brian Silvermanf3cfbd72013-10-28 16:26:09 -070089 param.sched_priority = 30 + relative_priority;
brians343bc112013-02-10 01:53:46 +000090 if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -070091 PDie("%s-init: setting SCHED_FIFO failed", program_invocation_short_name);
brians343bc112013-02-10 01:53:46 +000092 }
93 } else {
94 fprintf(stderr, "%s not doing realtime initialization because environment"
95 " variable %s is set\n", program_invocation_short_name,
96 kNoRealtimeEnvironmentVariable);
97 printf("no realtime for %s. see stderr\n", program_invocation_short_name);
98 }
99
100 InitNRT();
101}
102
103void Cleanup() {
Brian Silverman01be0002014-05-10 15:44:38 -0700104 aos_core_free_shared_mem();
brians343bc112013-02-10 01:53:46 +0000105}
106
Brian Silvermanfe1ef172014-04-12 17:12:45 -0700107void WriteCoreDumps() {
108 // Do create core files of unlimited size.
109 SetSoftRLimit(RLIMIT_CORE, RLIM_INFINITY, true);
110}
111
Brian Silvermanda45b6c2014-12-28 11:36:50 -0800112void SetCurrentThreadRealtimePriority(int priority) {
113 struct sched_param param;
114 param.sched_priority = priority;
115 if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
116 PLOG(FATAL, "sched_setscheduler(0, SCHED_FIFO, %d) failed", priority);
117 }
118}
119
brians343bc112013-02-10 01:53:46 +0000120} // namespace aos