Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 1 | #ifndef AOS_STARTER_STARTERD_LIB_H_ |
| 2 | #define AOS_STARTER_STARTERD_LIB_H_ |
| 3 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 4 | #include <sys/signalfd.h> |
| 5 | #include <sys/wait.h> |
| 6 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <csignal> |
| 8 | #include <cstdio> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <unordered_map> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "aos/configuration.h" |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 14 | #include "aos/ipc_lib/memory_mapped_queue.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 15 | #include "aos/ipc_lib/signalfd.h" |
| 16 | #include "aos/macros.h" |
| 17 | #include "aos/starter/starter_generated.h" |
| 18 | #include "aos/starter/starter_rpc_generated.h" |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 19 | #include "aos/starter/subprocess.h" |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 20 | #include "aos/util/top.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 21 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 22 | namespace aos::starter { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 23 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 24 | const aos::Channel *StatusChannelForNode(const aos::Configuration *config, |
| 25 | const aos::Node *node); |
| 26 | const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config, |
| 27 | const aos::Node *node); |
| 28 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 29 | class Starter { |
| 30 | public: |
| 31 | Starter(const aos::Configuration *event_loop_config); |
| 32 | |
| 33 | // Inserts a new application from config. Returns the inserted application if |
| 34 | // it was successful, otherwise nullptr if an application already exists |
| 35 | // with the given name. |
| 36 | Application *AddApplication(const aos::Application *application); |
| 37 | |
| 38 | // Runs the event loop and starts all applications |
| 39 | void Run(); |
| 40 | |
| 41 | void Cleanup(); |
| 42 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 43 | // EventLoop that we use for running the code. Mostly exposed for testing |
| 44 | // purposes. |
| 45 | EventLoop *event_loop() { return &event_loop_; } |
| 46 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 47 | private: |
| 48 | // Signals which indicate starter has died |
| 49 | static const inline std::vector<int> kStarterDeath = { |
| 50 | SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, |
| 51 | SIGSEGV, SIGPIPE, SIGTERM, SIGBUS, SIGXCPU}; |
| 52 | |
| 53 | void OnSignal(signalfd_siginfo signal); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 54 | void HandleStarterRpc(const StarterRpc &command); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 55 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 56 | // Handles any potential state change in the child applications. |
| 57 | // In particular, sends the Status message if it wouldn't exceed the rate |
| 58 | // limit. |
| 59 | void HandleStateChange(); |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 60 | |
James Kuszmaul | 8544c49 | 2023-07-31 15:00:38 -0700 | [diff] [blame] | 61 | // Called periodically to run through the timing report fetcher and alert all |
| 62 | // the Application's to the new messages. |
James Kuszmaul | 2c10e05 | 2023-08-09 10:22:36 -0700 | [diff] [blame] | 63 | void ServiceTimingReportFetcher(int elapsed_cycles); |
James Kuszmaul | 8544c49 | 2023-07-31 15:00:38 -0700 | [diff] [blame] | 64 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 65 | void SendStatus(); |
| 66 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 67 | // Creates a MemoryMappedQueue for the given channel, to pre-allocate shared |
| 68 | // memory to give this process credit for the memory instead of any other |
| 69 | // process that accesses it. |
| 70 | void AddChannel(const aos::Channel *channel); |
| 71 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 72 | const std::string config_path_; |
| 73 | const aos::Configuration *config_msg_; |
| 74 | |
| 75 | aos::ShmEventLoop event_loop_; |
| 76 | aos::Sender<aos::starter::Status> status_sender_; |
James Kuszmaul | 2c10e05 | 2023-08-09 10:22:36 -0700 | [diff] [blame] | 77 | aos::PhasedLoopHandler *status_timer_; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 78 | aos::TimerHandler *cleanup_timer_; |
| 79 | |
Pallavi Madhukar | aa17d24 | 2023-12-20 13:42:41 -0800 | [diff] [blame^] | 80 | aos::Ftrace ftrace_; |
| 81 | |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 82 | int status_count_ = 0; |
| 83 | const int max_status_count_; |
| 84 | |
James Kuszmaul | 8544c49 | 2023-07-31 15:00:38 -0700 | [diff] [blame] | 85 | aos::Fetcher<aos::timing::Report> timing_report_fetcher_; |
| 86 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 87 | std::unordered_map<std::string, Application> applications_; |
Austin Schuh | 816a116 | 2023-05-31 16:29:47 -0700 | [diff] [blame] | 88 | |
| 89 | // Lock and list of all the queues. This makes it so we can initialize the |
| 90 | // queues in parallel, and also so starterd owns the memory for all the |
| 91 | // queues from cgroup's point of view. |
| 92 | std::mutex queue_mutex_; |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 93 | std::vector<std::unique_ptr<aos::ipc_lib::MemoryMappedQueue>> shm_queues_; |
| 94 | |
| 95 | // Capture the --shm_base flag at construction time. This makes it much |
| 96 | // easier to make different shared memory regions for doing things like |
| 97 | // multi-node tests. |
| 98 | std::string shm_base_; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 99 | |
| 100 | // Set to true on cleanup to block rpc commands and ensure cleanup only |
| 101 | // happens once. |
| 102 | bool exiting_ = false; |
| 103 | |
| 104 | SignalListener listener_; |
| 105 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 106 | util::Top top_; |
| 107 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 108 | DISALLOW_COPY_AND_ASSIGN(Starter); |
| 109 | }; |
| 110 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 111 | } // namespace aos::starter |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 112 | |
| 113 | #endif // AOS_STARTER_STARTERD_LIB_H_ |