blob: a5aa406e10cfb7cd1edeb180fd3109a07314ed64 [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>
Tyler Chatowa79419d2020-08-12 20:12:11 -07005
Tyler Chatowbf0609c2021-07-31 16:13:27 -07006#include <csignal>
Stephan Pleinesf581a072024-05-23 20:59:27 -07007#include <memory>
8#include <mutex>
Tyler Chatowa79419d2020-08-12 20:12:11 -07009#include <string>
10#include <unordered_map>
11#include <vector>
12
13#include "aos/configuration.h"
Stephan Pleinesf581a072024-05-23 20:59:27 -070014#include "aos/events/event_loop.h"
15#include "aos/events/event_loop_generated.h"
16#include "aos/events/shm_event_loop.h"
17#include "aos/ftrace.h"
Austin Schuh4d275fc2022-09-16 15:42:45 -070018#include "aos/ipc_lib/memory_mapped_queue.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070019#include "aos/macros.h"
20#include "aos/starter/starter_generated.h"
21#include "aos/starter/starter_rpc_generated.h"
James Kuszmaul3224b8e2022-01-07 19:00:39 -080022#include "aos/starter/subprocess.h"
James Kuszmaul6295a642022-03-22 15:23:59 -070023#include "aos/util/top.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070024
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080025namespace aos::starter {
Tyler Chatowa79419d2020-08-12 20:12:11 -070026
James Kuszmaul293b2172021-11-10 16:20:48 -080027const aos::Channel *StatusChannelForNode(const aos::Configuration *config,
28 const aos::Node *node);
29const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config,
30 const aos::Node *node);
31
Tyler Chatowa79419d2020-08-12 20:12:11 -070032class Starter {
33 public:
34 Starter(const aos::Configuration *event_loop_config);
35
36 // Inserts a new application from config. Returns the inserted application if
37 // it was successful, otherwise nullptr if an application already exists
38 // with the given name.
39 Application *AddApplication(const aos::Application *application);
40
41 // Runs the event loop and starts all applications
42 void Run();
43
44 void Cleanup();
45
James Kuszmaul6b35e3a2022-04-06 15:00:39 -070046 // EventLoop that we use for running the code. Mostly exposed for testing
47 // purposes.
48 EventLoop *event_loop() { return &event_loop_; }
49
Tyler Chatowa79419d2020-08-12 20:12:11 -070050 private:
51 // Signals which indicate starter has died
52 static const inline std::vector<int> kStarterDeath = {
53 SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE,
54 SIGSEGV, SIGPIPE, SIGTERM, SIGBUS, SIGXCPU};
55
56 void OnSignal(signalfd_siginfo signal);
James Kuszmaul293b2172021-11-10 16:20:48 -080057 void HandleStarterRpc(const StarterRpc &command);
Tyler Chatowa79419d2020-08-12 20:12:11 -070058
James Kuszmaul6295a642022-03-22 15:23:59 -070059 // Handles any potential state change in the child applications.
60 // In particular, sends the Status message if it wouldn't exceed the rate
61 // limit.
62 void HandleStateChange();
Austin Schuhfc304942021-10-16 14:20:05 -070063
James Kuszmaul8544c492023-07-31 15:00:38 -070064 // Called periodically to run through the timing report fetcher and alert all
65 // the Application's to the new messages.
James Kuszmaul2c10e052023-08-09 10:22:36 -070066 void ServiceTimingReportFetcher(int elapsed_cycles);
James Kuszmaul8544c492023-07-31 15:00:38 -070067
Tyler Chatowa79419d2020-08-12 20:12:11 -070068 void SendStatus();
69
Austin Schuh4d275fc2022-09-16 15:42:45 -070070 // Creates a MemoryMappedQueue for the given channel, to pre-allocate shared
71 // memory to give this process credit for the memory instead of any other
72 // process that accesses it.
73 void AddChannel(const aos::Channel *channel);
74
Tyler Chatowa79419d2020-08-12 20:12:11 -070075 const std::string config_path_;
76 const aos::Configuration *config_msg_;
77
78 aos::ShmEventLoop event_loop_;
79 aos::Sender<aos::starter::Status> status_sender_;
James Kuszmaul2c10e052023-08-09 10:22:36 -070080 aos::PhasedLoopHandler *status_timer_;
Tyler Chatowa79419d2020-08-12 20:12:11 -070081 aos::TimerHandler *cleanup_timer_;
82
Pallavi Madhukaraa17d242023-12-20 13:42:41 -080083 aos::Ftrace ftrace_;
84
Austin Schuhfc304942021-10-16 14:20:05 -070085 int status_count_ = 0;
86 const int max_status_count_;
87
James Kuszmaul8544c492023-07-31 15:00:38 -070088 aos::Fetcher<aos::timing::Report> timing_report_fetcher_;
89
Tyler Chatowa79419d2020-08-12 20:12:11 -070090 std::unordered_map<std::string, Application> applications_;
Austin Schuh816a1162023-05-31 16:29:47 -070091
92 // Lock and list of all the queues. This makes it so we can initialize the
93 // queues in parallel, and also so starterd owns the memory for all the
94 // queues from cgroup's point of view.
95 std::mutex queue_mutex_;
Austin Schuh4d275fc2022-09-16 15:42:45 -070096 std::vector<std::unique_ptr<aos::ipc_lib::MemoryMappedQueue>> shm_queues_;
97
98 // Capture the --shm_base flag at construction time. This makes it much
99 // easier to make different shared memory regions for doing things like
100 // multi-node tests.
101 std::string shm_base_;
Tyler Chatowa79419d2020-08-12 20:12:11 -0700102
103 // Set to true on cleanup to block rpc commands and ensure cleanup only
104 // happens once.
105 bool exiting_ = false;
106
107 SignalListener listener_;
108
James Kuszmaul6295a642022-03-22 15:23:59 -0700109 util::Top top_;
110
Tyler Chatowa79419d2020-08-12 20:12:11 -0700111 DISALLOW_COPY_AND_ASSIGN(Starter);
112};
113
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800114} // namespace aos::starter
Tyler Chatowa79419d2020-08-12 20:12:11 -0700115
116#endif // AOS_STARTER_STARTERD_LIB_H_