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