Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 1 | #include "aos/starter/starterd_lib.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 2 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 3 | #include <algorithm> |
| 4 | #include <utility> |
| 5 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 6 | #include "absl/strings/str_format.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 7 | #include "glog/logging.h" |
| 8 | #include "glog/stl_logging.h" |
| 9 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 10 | #include "aos/json_to_flatbuffer.h" |
| 11 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 12 | // FLAGS_shm_base is defined elsewhere, declare it here so it can be used |
| 13 | // to override the shared memory folder for unit testing. |
| 14 | DECLARE_string(shm_base); |
| 15 | // FLAGS_permissions is defined elsewhere, declare it here so it can be used |
| 16 | // to set the file permissions on the shared memory block. |
| 17 | DECLARE_uint32(permissions); |
| 18 | |
Austin Schuh | 816a116 | 2023-05-31 16:29:47 -0700 | [diff] [blame] | 19 | DEFINE_uint32(queue_initialization_threads, 0, |
| 20 | "Number of threads to spin up to initialize the queue. 0 means " |
| 21 | "use the main thread."); |
Pallavi Madhukar | aa17d24 | 2023-12-20 13:42:41 -0800 | [diff] [blame] | 22 | DECLARE_bool(enable_ftrace); |
Austin Schuh | 816a116 | 2023-05-31 16:29:47 -0700 | [diff] [blame] | 23 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 24 | namespace aos::starter { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 25 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 26 | const aos::Channel *StatusChannelForNode(const aos::Configuration *config, |
| 27 | const aos::Node *node) { |
| 28 | return configuration::GetChannel<Status>(config, "/aos", "", node); |
| 29 | } |
| 30 | const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config, |
| 31 | const aos::Node *node) { |
| 32 | return configuration::GetChannel<StarterRpc>(config, "/aos", "", node); |
| 33 | } |
| 34 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 35 | Starter::Starter(const aos::Configuration *event_loop_config) |
| 36 | : config_msg_(event_loop_config), |
| 37 | event_loop_(event_loop_config), |
| 38 | status_sender_(event_loop_.MakeSender<aos::starter::Status>("/aos")), |
James Kuszmaul | 2c10e05 | 2023-08-09 10:22:36 -0700 | [diff] [blame] | 39 | status_timer_(event_loop_.AddPhasedLoop( |
| 40 | [this](int elapsed_cycles) { |
| 41 | ServiceTimingReportFetcher(elapsed_cycles); |
| 42 | SendStatus(); |
| 43 | status_count_ = 0; |
| 44 | }, |
| 45 | std::chrono::milliseconds(1000))), |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 46 | cleanup_timer_(event_loop_.AddTimer([this] { |
| 47 | event_loop_.Exit(); |
| 48 | LOG(INFO) << "Starter event loop exit finished."; |
| 49 | })), |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 50 | max_status_count_( |
| 51 | event_loop_.GetChannel<aos::starter::Status>("/aos")->frequency() - |
| 52 | 1), |
James Kuszmaul | 8544c49 | 2023-07-31 15:00:38 -0700 | [diff] [blame] | 53 | timing_report_fetcher_( |
| 54 | event_loop_.MakeFetcher<aos::timing::Report>("/aos")), |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 55 | shm_base_(FLAGS_shm_base), |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 56 | listener_(&event_loop_, |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 57 | [this](signalfd_siginfo signal) { OnSignal(signal); }), |
| 58 | top_(&event_loop_) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 59 | event_loop_.SkipAosLog(); |
| 60 | |
James Kuszmaul | 06a8f35 | 2024-03-15 14:15:57 -0700 | [diff] [blame^] | 61 | cleanup_timer_->set_name("cleanup"); |
| 62 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 63 | if (!aos::configuration::MultiNode(config_msg_)) { |
| 64 | event_loop_.MakeWatcher( |
| 65 | "/aos", |
| 66 | [this](const aos::starter::StarterRpc &cmd) { HandleStarterRpc(cmd); }); |
| 67 | } else { |
| 68 | for (const aos::Node *node : aos::configuration::GetNodes(config_msg_)) { |
| 69 | const Channel *channel = StarterRpcChannelForNode(config_msg_, node); |
| 70 | CHECK(channel != nullptr) << ": Failed to find channel /aos for " |
| 71 | << StarterRpc::GetFullyQualifiedName() << " on " |
| 72 | << node->name()->string_view(); |
| 73 | if (!aos::configuration::ChannelIsReadableOnNode(channel, |
| 74 | event_loop_.node())) { |
| 75 | LOG(INFO) << "StarterRpc channel " |
| 76 | << aos::configuration::StrippedChannelToString(channel) |
| 77 | << " is not readable on " |
| 78 | << event_loop_.node()->name()->string_view(); |
| 79 | } else { |
| 80 | event_loop_.MakeWatcher(channel->name()->string_view(), |
| 81 | [this](const aos::starter::StarterRpc &cmd) { |
| 82 | HandleStarterRpc(cmd); |
| 83 | }); |
| 84 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 85 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 86 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 87 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 88 | // Catalogue all the applications for this node, so we can keep an eye on |
| 89 | // them. |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 90 | if (config_msg_->has_applications()) { |
| 91 | const flatbuffers::Vector<flatbuffers::Offset<aos::Application>> |
| 92 | *applications = config_msg_->applications(); |
Ravago Jones | 7e2dd32 | 2020-11-21 15:58:58 -0800 | [diff] [blame] | 93 | |
| 94 | if (aos::configuration::MultiNode(config_msg_)) { |
| 95 | std::string_view current_node = event_loop_.node()->name()->string_view(); |
| 96 | for (const aos::Application *application : *applications) { |
Austin Schuh | 228609b | 2023-03-21 15:43:11 -0700 | [diff] [blame] | 97 | CHECK(application->has_nodes()) |
| 98 | << ": Missing nodes on " << aos::FlatbufferToJson(application); |
Ravago Jones | 7e2dd32 | 2020-11-21 15:58:58 -0800 | [diff] [blame] | 99 | for (const flatbuffers::String *node : *application->nodes()) { |
| 100 | if (node->string_view() == current_node) { |
| 101 | AddApplication(application); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } else { |
| 107 | for (const aos::Application *application : *applications) { |
| 108 | AddApplication(application); |
| 109 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 112 | |
| 113 | // Catalogue all the intranode channels for this node, and create |
| 114 | // MemoryMappedQueues for each one to allocate the shared memory before |
| 115 | // spawning any shasta process. |
| 116 | if (config_msg_->has_channels()) { |
Austin Schuh | 816a116 | 2023-05-31 16:29:47 -0700 | [diff] [blame] | 117 | LOG(INFO) << "Starting to initialize shared memory."; |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 118 | const aos::Node *this_node = event_loop_.node(); |
Austin Schuh | 816a116 | 2023-05-31 16:29:47 -0700 | [diff] [blame] | 119 | std::vector<const aos::Channel *> channels_to_construct; |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 120 | for (const aos::Channel *channel : *config_msg_->channels()) { |
| 121 | if (aos::configuration::ChannelIsReadableOnNode(channel, this_node)) { |
Austin Schuh | 816a116 | 2023-05-31 16:29:47 -0700 | [diff] [blame] | 122 | if (FLAGS_queue_initialization_threads == 0) { |
| 123 | AddChannel(channel); |
| 124 | } else { |
| 125 | channels_to_construct.push_back(channel); |
| 126 | } |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
Austin Schuh | 816a116 | 2023-05-31 16:29:47 -0700 | [diff] [blame] | 129 | |
| 130 | if (FLAGS_queue_initialization_threads != 0) { |
| 131 | std::mutex pool_mutex; |
| 132 | std::vector<std::thread> threads; |
| 133 | threads.reserve(FLAGS_queue_initialization_threads); |
| 134 | for (size_t i = 0; i < FLAGS_queue_initialization_threads; ++i) { |
| 135 | threads.emplace_back([this, &pool_mutex, &channels_to_construct]() { |
| 136 | while (true) { |
| 137 | const aos::Channel *channel; |
| 138 | { |
| 139 | std::unique_lock<std::mutex> locker(pool_mutex); |
| 140 | if (channels_to_construct.empty()) { |
| 141 | return; |
| 142 | } |
| 143 | channel = channels_to_construct.back(); |
| 144 | channels_to_construct.pop_back(); |
| 145 | } |
| 146 | AddChannel(channel); |
| 147 | } |
| 148 | }); |
| 149 | } |
| 150 | for (size_t i = 0; i < FLAGS_queue_initialization_threads; ++i) { |
| 151 | threads[i].join(); |
| 152 | } |
| 153 | } |
| 154 | LOG(INFO) << "Starting applications."; |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 155 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 156 | } |
| 157 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 158 | void Starter::HandleStarterRpc(const StarterRpc &command) { |
| 159 | if (!command.has_command() || !command.has_name() || exiting_) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | LOG(INFO) << "Received " << aos::FlatbufferToJson(&command); |
| 164 | |
| 165 | if (command.has_nodes()) { |
| 166 | CHECK(aos::configuration::MultiNode(config_msg_)); |
| 167 | bool relevant_to_this_node = false; |
| 168 | for (const flatbuffers::String *node : *command.nodes()) { |
| 169 | if (node->string_view() == event_loop_.node()->name()->string_view()) { |
| 170 | relevant_to_this_node = true; |
| 171 | } |
| 172 | } |
| 173 | if (!relevant_to_this_node) { |
| 174 | return; |
| 175 | } |
| 176 | } |
| 177 | // If not populated, restart regardless of node. |
| 178 | |
| 179 | auto search = applications_.find(command.name()->str()); |
| 180 | if (search != applications_.end()) { |
| 181 | // If an applicatione exists by the given name, dispatch the command |
| 182 | search->second.HandleCommand(command.command()); |
| 183 | } |
| 184 | } |
| 185 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 186 | void Starter::HandleStateChange() { |
| 187 | std::set<pid_t> all_pids; |
| 188 | for (const auto &pair : applications_) { |
| 189 | if (pair.second.get_pid() > 0 && |
| 190 | pair.second.status() != aos::starter::State::STOPPED) { |
| 191 | all_pids.insert(pair.second.get_pid()); |
| 192 | } |
| 193 | } |
| 194 | top_.set_track_pids(all_pids); |
| 195 | |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 196 | if (status_count_ < max_status_count_) { |
| 197 | SendStatus(); |
| 198 | ++status_count_; |
| 199 | } else { |
| 200 | VLOG(1) << "That's enough " << status_count_ << " " << max_status_count_; |
| 201 | } |
| 202 | } |
| 203 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 204 | void Starter::Cleanup() { |
| 205 | if (exiting_) { |
| 206 | return; |
| 207 | } |
| 208 | exiting_ = true; |
| 209 | for (auto &application : applications_) { |
| 210 | application.second.Terminate(); |
| 211 | } |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 212 | cleanup_timer_->Schedule(event_loop_.monotonic_now() + |
| 213 | std::chrono::milliseconds(1500)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void Starter::OnSignal(signalfd_siginfo info) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 217 | if (info.ssi_signo == SIGCHLD) { |
| 218 | // SIGCHLD messages can be collapsed if multiple are received, so all |
| 219 | // applications must check their status. |
Pallavi Madhukar | aa17d24 | 2023-12-20 13:42:41 -0800 | [diff] [blame] | 220 | if (FLAGS_enable_ftrace) { |
| 221 | ftrace_.FormatMessage("SIGCHLD"); |
| 222 | ftrace_.TurnOffOrDie(); |
| 223 | } |
| 224 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 225 | for (auto iter = applications_.begin(); iter != applications_.end();) { |
| 226 | if (iter->second.MaybeHandleSignal()) { |
| 227 | iter = applications_.erase(iter); |
| 228 | } else { |
| 229 | ++iter; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (exiting_ && applications_.empty()) { |
| 234 | event_loop_.Exit(); |
| 235 | } |
Austin Schuh | 3204b33 | 2021-10-16 14:20:10 -0700 | [diff] [blame] | 236 | } else { |
| 237 | LOG(INFO) << "Received signal '" << strsignal(info.ssi_signo) << "'"; |
| 238 | |
| 239 | if (std::find(kStarterDeath.begin(), kStarterDeath.end(), info.ssi_signo) != |
| 240 | kStarterDeath.end()) { |
| 241 | LOG(WARNING) << "Starter shutting down"; |
| 242 | Cleanup(); |
| 243 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | Application *Starter::AddApplication(const aos::Application *application) { |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 248 | auto [iter, success] = applications_.try_emplace( |
| 249 | application->name()->str(), application, &event_loop_, |
| 250 | [this]() { HandleStateChange(); }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 251 | if (success) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 252 | // We should be catching and handling SIGCHLD correctly in the starter, so |
| 253 | // don't leave in the crutch for polling for the child process status (this |
| 254 | // is less about efficiency, and more about making sure bit rot doesn't |
| 255 | // result in the signal handling breaking). |
| 256 | iter->second.DisableChildDeathPolling(); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 257 | return &(iter->second); |
| 258 | } |
| 259 | return nullptr; |
| 260 | } |
| 261 | |
| 262 | void Starter::Run() { |
Tyler Chatow | 03fdb2a | 2020-12-26 18:39:36 -0800 | [diff] [blame] | 263 | #ifdef AOS_ARCHITECTURE_arm_frc |
| 264 | PCHECK(setuid(0) == 0) << "Failed to change user to root"; |
| 265 | #endif |
| 266 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 267 | for (auto &application : applications_) { |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 268 | if (application.second.autostart()) { |
| 269 | application.second.Start(); |
| 270 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | event_loop_.Run(); |
| 274 | } |
| 275 | |
James Kuszmaul | 2c10e05 | 2023-08-09 10:22:36 -0700 | [diff] [blame] | 276 | void Starter::ServiceTimingReportFetcher(int elapsed_cycles) { |
| 277 | // If there is any chance that it has been longer than one cycle since we last |
| 278 | // serviced the fetcher, call Fetch(). This reduces the chances that the |
| 279 | // fetcher falls behind when the system is under heavy load. Dropping a few |
| 280 | // timing report messages when the system is under stress is fine. |
| 281 | if (timing_report_fetcher_.get() == nullptr || elapsed_cycles > 1) { |
| 282 | timing_report_fetcher_.Fetch(); |
| 283 | } |
James Kuszmaul | 8544c49 | 2023-07-31 15:00:38 -0700 | [diff] [blame] | 284 | while (timing_report_fetcher_.FetchNext()) { |
| 285 | for (auto &application : applications_) { |
| 286 | application.second.ObserveTimingReport( |
| 287 | timing_report_fetcher_.context().monotonic_event_time, |
| 288 | timing_report_fetcher_.get()); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 293 | void Starter::SendStatus() { |
| 294 | aos::Sender<aos::starter::Status>::Builder builder = |
| 295 | status_sender_.MakeBuilder(); |
| 296 | |
| 297 | std::vector<flatbuffers::Offset<aos::starter::ApplicationStatus>> statuses; |
| 298 | |
| 299 | for (auto &application : applications_) { |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 300 | statuses.push_back(application.second.PopulateStatus(builder.fbb(), &top_)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | auto statuses_fbs = builder.fbb()->CreateVector(statuses); |
| 304 | |
| 305 | aos::starter::Status::Builder status_builder(*builder.fbb()); |
| 306 | status_builder.add_statuses(statuses_fbs); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 307 | builder.CheckOk(builder.Send(status_builder.Finish())); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 310 | void Starter::AddChannel(const aos::Channel *channel) { |
| 311 | CHECK_NOTNULL(channel); |
Austin Schuh | 816a116 | 2023-05-31 16:29:47 -0700 | [diff] [blame] | 312 | std::unique_ptr<aos::ipc_lib::MemoryMappedQueue> queue = |
| 313 | std::make_unique<aos::ipc_lib::MemoryMappedQueue>( |
| 314 | shm_base_, FLAGS_permissions, event_loop_.configuration(), channel); |
| 315 | |
| 316 | { |
| 317 | std::unique_lock<std::mutex> locker(queue_mutex_); |
| 318 | shm_queues_.emplace_back(std::move(queue)); |
| 319 | } |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 320 | VLOG(1) << "Created MemoryMappedQueue for " |
| 321 | << aos::configuration::StrippedChannelToString(channel) << " under " |
| 322 | << shm_base_; |
| 323 | } |
| 324 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 325 | } // namespace aos::starter |