Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 1 | #include "aos/linux_code/init.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <stdint.h> |
| 13 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | #include "aos/common/die.h" |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 15 | #include "aos/linux_code/logging/linux_logging.h" |
| 16 | #include "aos/linux_code/ipc_lib/shared_mem.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 17 | |
| 18 | namespace aos { |
| 19 | |
| 20 | namespace { |
| 21 | |
Brian Silverman | 80353cb | 2013-03-19 18:27:53 -0700 | [diff] [blame] | 22 | void SetSoftRLimit(int resource, rlim64_t soft, bool set_for_root) { |
| 23 | bool am_root = getuid() == 0; |
| 24 | if (set_for_root || !am_root) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 25 | struct rlimit64 rlim; |
| 26 | if (getrlimit64(resource, &rlim) == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 27 | PDie("%s-init: getrlimit64(%d) failed", |
| 28 | program_invocation_short_name, resource); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 29 | } |
| 30 | rlim.rlim_cur = soft; |
Austin Schuh | 6c7edbf | 2014-10-21 22:26:58 -0700 | [diff] [blame] | 31 | rlim.rlim_max = ::std::max(rlim.rlim_max, soft); |
| 32 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 33 | if (setrlimit64(resource, &rlim) == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 34 | 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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. |
| 43 | void InitStart() { |
Brian Silverman | ff48578 | 2014-06-18 19:59:09 -0700 | [diff] [blame] | 44 | ::aos::logging::Init(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 45 | // Allow locking as much as we want into RAM. |
Brian Silverman | 80353cb | 2013-03-19 18:27:53 -0700 | [diff] [blame] | 46 | SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, false); |
Brian Silverman | fe1ef17 | 2014-04-12 17:12:45 -0700 | [diff] [blame] | 47 | WriteCoreDumps(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 50 | void LockAllMemory() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 51 | InitStart(); |
| 52 | if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 53 | PDie("%s-init: mlockall failed", program_invocation_short_name); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 54 | } |
| 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)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Do the initialization code that is necessary for both realtime and |
| 64 | // non-realtime processes. |
| 65 | void DoInitNRT(aos_core_create create) { |
| 66 | InitStart(); |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 67 | aos_core_create_shared_mem(create); |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 68 | logging::linux_code::Register(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME"; |
| 72 | |
| 73 | } // namespace |
| 74 | |
| 75 | void InitNRT() { DoInitNRT(aos_core_create::reference); } |
| 76 | void InitCreate() { DoInitNRT(aos_core_create::create); } |
Brian Silverman | f3cfbd7 | 2013-10-28 16:26:09 -0700 | [diff] [blame] | 77 | void Init(int relative_priority) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 78 | if (getenv(kNoRealtimeEnvironmentVariable) == NULL) { // if nobody set it |
| 79 | LockAllMemory(); |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 80 | |
Brian Silverman | 8dc9fd4 | 2014-02-10 13:35:43 -0800 | [diff] [blame] | 81 | // Only let rt processes run for 3 seconds straight. |
| 82 | SetSoftRLimit(RLIMIT_RTTIME, 3000000, true); |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 83 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 84 | // Allow rt processes up to priority 40. |
Brian Silverman | 80353cb | 2013-03-19 18:27:53 -0700 | [diff] [blame] | 85 | SetSoftRLimit(RLIMIT_RTPRIO, 40, false); |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 86 | |
| 87 | // Set our process to the appropriate priority. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 88 | struct sched_param param; |
Brian Silverman | f3cfbd7 | 2013-10-28 16:26:09 -0700 | [diff] [blame] | 89 | param.sched_priority = 30 + relative_priority; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 90 | if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 91 | PDie("%s-init: setting SCHED_FIFO failed", program_invocation_short_name); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 92 | } |
| 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 | |
| 103 | void Cleanup() { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 104 | aos_core_free_shared_mem(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Brian Silverman | fe1ef17 | 2014-04-12 17:12:45 -0700 | [diff] [blame] | 107 | void WriteCoreDumps() { |
| 108 | // Do create core files of unlimited size. |
| 109 | SetSoftRLimit(RLIMIT_CORE, RLIM_INFINITY, true); |
| 110 | } |
| 111 | |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame^] | 112 | void SetCurrentThreadRealtimePriority(int priority) { |
| 113 | struct sched_param param; |
| 114 | param.sched_priority = priority; |
| 115 | if (sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) { |
| 116 | PLOG(FATAL, "sched_setscheduler(0, SCHED_FIFO, %d) failed", priority); |
| 117 | } |
| 118 | } |
| 119 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 120 | } // namespace aos |