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