blob: 3779c8417db7b1035930463bd4e0f00949695a48 [file] [log] [blame]
Tyler Chatowa79419d2020-08-12 20:12:11 -07001#ifndef AOS_STARTER_STARTERD_LIB_H_
2#define AOS_STARTER_STARTERD_LIB_H_
3
Tyler Chatowa79419d2020-08-12 20:12:11 -07004#include <sys/signalfd.h>
5#include <sys/wait.h>
6
Tyler Chatowbf0609c2021-07-31 16:13:27 -07007#include <csignal>
8#include <cstdio>
Tyler Chatowa79419d2020-08-12 20:12:11 -07009#include <string>
10#include <unordered_map>
11#include <vector>
12
13#include "aos/configuration.h"
Austin Schuh4d275fc2022-09-16 15:42:45 -070014#include "aos/ipc_lib/memory_mapped_queue.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070015#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 Kuszmaul3224b8e2022-01-07 19:00:39 -080019#include "aos/starter/subprocess.h"
James Kuszmaul6295a642022-03-22 15:23:59 -070020#include "aos/util/top.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070021
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080022namespace aos::starter {
Tyler Chatowa79419d2020-08-12 20:12:11 -070023
James Kuszmaul293b2172021-11-10 16:20:48 -080024const aos::Channel *StatusChannelForNode(const aos::Configuration *config,
25 const aos::Node *node);
26const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config,
27 const aos::Node *node);
28
Tyler Chatowa79419d2020-08-12 20:12:11 -070029class 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 Kuszmaul6b35e3a2022-04-06 15:00:39 -070043 // EventLoop that we use for running the code. Mostly exposed for testing
44 // purposes.
45 EventLoop *event_loop() { return &event_loop_; }
46
Tyler Chatowa79419d2020-08-12 20:12:11 -070047 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 Kuszmaul293b2172021-11-10 16:20:48 -080054 void HandleStarterRpc(const StarterRpc &command);
Tyler Chatowa79419d2020-08-12 20:12:11 -070055
James Kuszmaul6295a642022-03-22 15:23:59 -070056 // 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 Schuhfc304942021-10-16 14:20:05 -070060
James Kuszmaul8544c492023-07-31 15:00:38 -070061 // Called periodically to run through the timing report fetcher and alert all
62 // the Application's to the new messages.
James Kuszmaul2c10e052023-08-09 10:22:36 -070063 void ServiceTimingReportFetcher(int elapsed_cycles);
James Kuszmaul8544c492023-07-31 15:00:38 -070064
Tyler Chatowa79419d2020-08-12 20:12:11 -070065 void SendStatus();
66
Austin Schuh4d275fc2022-09-16 15:42:45 -070067 // 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 Chatowa79419d2020-08-12 20:12:11 -070072 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 Kuszmaul2c10e052023-08-09 10:22:36 -070077 aos::PhasedLoopHandler *status_timer_;
Tyler Chatowa79419d2020-08-12 20:12:11 -070078 aos::TimerHandler *cleanup_timer_;
79
Austin Schuhfc304942021-10-16 14:20:05 -070080 int status_count_ = 0;
81 const int max_status_count_;
82
James Kuszmaul8544c492023-07-31 15:00:38 -070083 aos::Fetcher<aos::timing::Report> timing_report_fetcher_;
84
Tyler Chatowa79419d2020-08-12 20:12:11 -070085 std::unordered_map<std::string, Application> applications_;
Austin Schuh816a1162023-05-31 16:29:47 -070086
87 // Lock and list of all the queues. This makes it so we can initialize the
88 // queues in parallel, and also so starterd owns the memory for all the
89 // queues from cgroup's point of view.
90 std::mutex queue_mutex_;
Austin Schuh4d275fc2022-09-16 15:42:45 -070091 std::vector<std::unique_ptr<aos::ipc_lib::MemoryMappedQueue>> shm_queues_;
92
93 // Capture the --shm_base flag at construction time. This makes it much
94 // easier to make different shared memory regions for doing things like
95 // multi-node tests.
96 std::string shm_base_;
Tyler Chatowa79419d2020-08-12 20:12:11 -070097
98 // Set to true on cleanup to block rpc commands and ensure cleanup only
99 // happens once.
100 bool exiting_ = false;
101
102 SignalListener listener_;
103
James Kuszmaul6295a642022-03-22 15:23:59 -0700104 util::Top top_;
105
Tyler Chatowa79419d2020-08-12 20:12:11 -0700106 DISALLOW_COPY_AND_ASSIGN(Starter);
107};
108
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800109} // namespace aos::starter
Tyler Chatowa79419d2020-08-12 20:12:11 -0700110
111#endif // AOS_STARTER_STARTERD_LIB_H_