blob: 81a4deb2efd0b72d8006bb49eb8a6312d6379f01 [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
22namespace aos {
23namespace starter {
24
James Kuszmaul293b2172021-11-10 16:20:48 -080025const aos::Channel *StatusChannelForNode(const aos::Configuration *config,
26 const aos::Node *node);
27const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config,
28 const aos::Node *node);
29
Tyler Chatowa79419d2020-08-12 20:12:11 -070030class Starter {
31 public:
32 Starter(const aos::Configuration *event_loop_config);
33
34 // Inserts a new application from config. Returns the inserted application if
35 // it was successful, otherwise nullptr if an application already exists
36 // with the given name.
37 Application *AddApplication(const aos::Application *application);
38
39 // Runs the event loop and starts all applications
40 void Run();
41
42 void Cleanup();
43
James Kuszmaul6b35e3a2022-04-06 15:00:39 -070044 // EventLoop that we use for running the code. Mostly exposed for testing
45 // purposes.
46 EventLoop *event_loop() { return &event_loop_; }
47
Tyler Chatowa79419d2020-08-12 20:12:11 -070048 private:
49 // Signals which indicate starter has died
50 static const inline std::vector<int> kStarterDeath = {
51 SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE,
52 SIGSEGV, SIGPIPE, SIGTERM, SIGBUS, SIGXCPU};
53
54 void OnSignal(signalfd_siginfo signal);
James Kuszmaul293b2172021-11-10 16:20:48 -080055 void HandleStarterRpc(const StarterRpc &command);
Tyler Chatowa79419d2020-08-12 20:12:11 -070056
James Kuszmaul6295a642022-03-22 15:23:59 -070057 // Handles any potential state change in the child applications.
58 // In particular, sends the Status message if it wouldn't exceed the rate
59 // limit.
60 void HandleStateChange();
Austin Schuhfc304942021-10-16 14:20:05 -070061
James Kuszmaul8544c492023-07-31 15:00:38 -070062 // Called periodically to run through the timing report fetcher and alert all
63 // the Application's to the new messages.
64 void ServiceTimingReportFetcher();
65
Tyler Chatowa79419d2020-08-12 20:12:11 -070066 void SendStatus();
67
Austin Schuh4d275fc2022-09-16 15:42:45 -070068 // Creates a MemoryMappedQueue for the given channel, to pre-allocate shared
69 // memory to give this process credit for the memory instead of any other
70 // process that accesses it.
71 void AddChannel(const aos::Channel *channel);
72
Tyler Chatowa79419d2020-08-12 20:12:11 -070073 const std::string config_path_;
74 const aos::Configuration *config_msg_;
75
76 aos::ShmEventLoop event_loop_;
77 aos::Sender<aos::starter::Status> status_sender_;
78 aos::TimerHandler *status_timer_;
79 aos::TimerHandler *cleanup_timer_;
80
Austin Schuhfc304942021-10-16 14:20:05 -070081 int status_count_ = 0;
82 const int max_status_count_;
83
James Kuszmaul8544c492023-07-31 15:00:38 -070084 aos::Fetcher<aos::timing::Report> timing_report_fetcher_;
85
Tyler Chatowa79419d2020-08-12 20:12:11 -070086 std::unordered_map<std::string, Application> applications_;
Austin Schuh816a1162023-05-31 16:29:47 -070087
88 // Lock and list of all the queues. This makes it so we can initialize the
89 // queues in parallel, and also so starterd owns the memory for all the
90 // queues from cgroup's point of view.
91 std::mutex queue_mutex_;
Austin Schuh4d275fc2022-09-16 15:42:45 -070092 std::vector<std::unique_ptr<aos::ipc_lib::MemoryMappedQueue>> shm_queues_;
93
94 // Capture the --shm_base flag at construction time. This makes it much
95 // easier to make different shared memory regions for doing things like
96 // multi-node tests.
97 std::string shm_base_;
Tyler Chatowa79419d2020-08-12 20:12:11 -070098
99 // Set to true on cleanup to block rpc commands and ensure cleanup only
100 // happens once.
101 bool exiting_ = false;
102
103 SignalListener listener_;
104
James Kuszmaul6295a642022-03-22 15:23:59 -0700105 util::Top top_;
106
Tyler Chatowa79419d2020-08-12 20:12:11 -0700107 DISALLOW_COPY_AND_ASSIGN(Starter);
108};
109
110} // namespace starter
111} // namespace aos
112
113#endif // AOS_STARTER_STARTERD_LIB_H_