John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 1 | #include "aos/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> |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 13 | #include <sys/prctl.h> |
Brian Silverman | 4048662 | 2014-12-30 17:38:55 -0800 | [diff] [blame] | 14 | #include <malloc.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 16 | #include "aos/die.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 17 | #include "aos/ipc_lib/shared_mem.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | #include "aos/logging/implementations.h" |
| 19 | #include "aos/realtime.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 20 | |
Brian Silverman | 4048662 | 2014-12-30 17:38:55 -0800 | [diff] [blame] | 21 | namespace FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead { |
Brian Silverman | 2643844 | 2015-10-11 19:36:47 -0400 | [diff] [blame] | 22 | extern double FLAGS_tcmalloc_release_rate __attribute__((weak)); |
Brian Silverman | 4048662 | 2014-12-30 17:38:55 -0800 | [diff] [blame] | 23 | } |
| 24 | using FLAG__namespace_do_not_use_directly_use_DECLARE_double_instead:: |
| 25 | FLAGS_tcmalloc_release_rate; |
Brian Silverman | 4048662 | 2014-12-30 17:38:55 -0800 | [diff] [blame] | 26 | |
Austin Schuh | 707c835 | 2020-03-15 14:27:25 -0700 | [diff] [blame^] | 27 | DEFINE_bool(coredump, false, "If true, write core dumps on failure."); |
| 28 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 29 | namespace aos { |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 30 | namespace logging { |
| 31 | namespace internal { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 32 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 33 | // Implemented in aos/logging/context.cc. |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 34 | void ReloadThreadName(); |
| 35 | |
| 36 | } // namespace internal |
| 37 | } // namespace logging |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 40 | // Common stuff that needs to happen at the beginning of both the realtime and |
| 41 | // non-realtime initialization sequences. May be called twice. |
| 42 | void InitStart() { |
Brian Silverman | ff48578 | 2014-06-18 19:59:09 -0700 | [diff] [blame] | 43 | ::aos::logging::Init(); |
Austin Schuh | 707c835 | 2020-03-15 14:27:25 -0700 | [diff] [blame^] | 44 | if (FLAGS_coredump) { |
| 45 | WriteCoreDumps(); |
| 46 | } |
Austin Schuh | e8bfc55 | 2019-12-03 23:48:23 -0800 | [diff] [blame] | 47 | google::InstallFailureSignalHandler(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Austin Schuh | 3d4d5df | 2015-10-17 15:51:41 -0700 | [diff] [blame] | 50 | const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME"; |
| 51 | |
Brian Silverman | 8f8debf | 2018-03-11 19:30:23 -0700 | [diff] [blame] | 52 | bool ShouldBeRealtime() { |
| 53 | return getenv(kNoRealtimeEnvironmentVariable) == nullptr; |
| 54 | } |
| 55 | |
Austin Schuh | 3d4d5df | 2015-10-17 15:51:41 -0700 | [diff] [blame] | 56 | } // namespace |
| 57 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 58 | void InitGoogle(int *argc, char ***argv) { |
| 59 | FLAGS_logtostderr = true; |
| 60 | google::InitGoogleLogging((*argv)[0]); |
| 61 | gflags::ParseCommandLineFlags(argc, argv, true); |
| 62 | google::InstallFailureSignalHandler(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 63 | } |
| 64 | |
James Kuszmaul | ad8a808 | 2020-02-14 21:21:58 -0800 | [diff] [blame] | 65 | void InitNRT() { |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 66 | InitStart(); |
James Kuszmaul | b4874eb | 2020-01-18 17:50:35 -0800 | [diff] [blame] | 67 | ExpandStackSize(); |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void InitCreate() { |
| 71 | InitStart(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 72 | AOS_LOG(INFO, "%s created shm\n", program_invocation_short_name); |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Brian Silverman | f3cfbd7 | 2013-10-28 16:26:09 -0700 | [diff] [blame] | 75 | void Init(int relative_priority) { |
Brian Silverman | 8f8debf | 2018-03-11 19:30:23 -0700 | [diff] [blame] | 76 | InitStart(); |
Brian Silverman | 8f8debf | 2018-03-11 19:30:23 -0700 | [diff] [blame] | 77 | GoRT(relative_priority); |
| 78 | } |
| 79 | |
| 80 | void GoRT(int relative_priority) { |
| 81 | if (ShouldBeRealtime()) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 82 | InitRT(); |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 83 | |
| 84 | // Set our process to the appropriate priority. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 85 | struct sched_param param; |
Brian Silverman | f3cfbd7 | 2013-10-28 16:26:09 -0700 | [diff] [blame] | 86 | param.sched_priority = 30 + relative_priority; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 87 | if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 88 | PDie("%s-init: setting SCHED_FIFO failed", program_invocation_short_name); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 89 | } |
| 90 | } else { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 91 | fprintf(stderr, |
| 92 | "%s not doing realtime initialization because environment" |
| 93 | " variable %s is set\n", |
| 94 | program_invocation_short_name, kNoRealtimeEnvironmentVariable); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 95 | printf("no realtime for %s. see stderr\n", program_invocation_short_name); |
| 96 | } |
| 97 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 98 | AOS_LOG(INFO, "%s initialized realtime\n", program_invocation_short_name); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void Cleanup() { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 102 | aos_core_free_shared_mem(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Brian Silverman | e4d8b28 | 2015-12-24 13:44:48 -0800 | [diff] [blame] | 105 | void PinCurrentThreadToCPU(int number) { |
| 106 | cpu_set_t cpuset; |
| 107 | CPU_ZERO(&cpuset); |
| 108 | CPU_SET(number, &cpuset); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 109 | AOS_PRCHECK(pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset)); |
Brian Silverman | e4d8b28 | 2015-12-24 13:44:48 -0800 | [diff] [blame] | 110 | } |
| 111 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 112 | } // namespace aos |