blob: 47762619ceb938f6f8ab9805c6b2854502d42831 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/atom_code/init.h"
2
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>
9#include <asm-generic/resource.h> // for RLIMIT_RTTIME
10#include <sys/types.h>
11#include <unistd.h>
12#include <stdlib.h>
13#include <stdint.h>
14
15#include "aos/aos_core.h"
16#include "aos/common/die.h"
17
18namespace aos {
19
20namespace {
21
22void SetSoftRLimit(int resource, rlim64_t soft) {
23 // If we're root, then we don't need to change the soft limits because none of
24 // the ones we care about get checked for root.
25 if (getuid() != 0) {
26 struct rlimit64 rlim;
27 if (getrlimit64(resource, &rlim) == -1) {
28 Die("%s-init: getrlimit64(%d) failed with %d (%s)\n",
29 program_invocation_short_name, resource, errno, strerror(errno));
30 }
31 rlim.rlim_cur = soft;
32 if (setrlimit64(resource, &rlim) == -1) {
33 Die("%s-init: setrlimit64(%d, {cur=%jd,max=%jd})"
34 " failed with %d (%s)\n", program_invocation_short_name,
35 resource, (intmax_t)rlim.rlim_cur, (intmax_t)rlim.rlim_max,
36 errno, strerror(errno));
37 }
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() {
44 // Allow locking as much as we want into RAM.
45 SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY);
46}
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 }
72 if (log_init(program_invocation_short_name) != 0) {
73 Die("%s-init: initializing logging failed\n",
74 program_invocation_short_name);
75 }
76}
77
78const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME";
79
80} // namespace
81
82void InitNRT() { DoInitNRT(aos_core_create::reference); }
83void InitCreate() { DoInitNRT(aos_core_create::create); }
84void Init() {
85 if (getenv(kNoRealtimeEnvironmentVariable) == NULL) { // if nobody set it
86 LockAllMemory();
87 // Only let rt processes run for 1 second straight.
88 SetSoftRLimit(RLIMIT_RTTIME, 1000000);
89 // Allow rt processes up to priority 40.
90 SetSoftRLimit(RLIMIT_RTPRIO, 40);
91 // Set our process to priority 40.
92 struct sched_param param;
93 param.sched_priority = 40;
94 if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
95 Die("%s-init: setting SCHED_FIFO failed with %d (%s)\n",
96 program_invocation_short_name, errno, strerror(errno));
97 }
98 } else {
99 fprintf(stderr, "%s not doing realtime initialization because environment"
100 " variable %s is set\n", program_invocation_short_name,
101 kNoRealtimeEnvironmentVariable);
102 printf("no realtime for %s. see stderr\n", program_invocation_short_name);
103 }
104
105 InitNRT();
106}
107
108void Cleanup() {
109 if (aos_core_free_shared_mem()) {
110 Die("%s-init: freeing shared mem failed\n",
111 program_invocation_short_name);
112 }
113}
114
115} // namespace aos