Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / mc / remote / CheckerSide.hpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_REMOTE_EVENTLOOP_HPP
7 #define SIMGRID_MC_REMOTE_EVENTLOOP_HPP
8
9 #include "src/mc/mc_forward.hpp"
10 #include "src/mc/remote/Channel.hpp"
11
12 #include <event2/event.h>
13 #include <functional>
14 #include <memory>
15
16 namespace simgrid::mc {
17
18 /* CheckerSide: All what the checker needs to interact with a given application process */
19
20 class CheckerSide {
21   event* socket_event_;
22   event* signal_event_;
23   std::unique_ptr<event_base, decltype(&event_base_free)> base_{nullptr, &event_base_free};
24
25   Channel channel_;
26   bool running_ = false;
27   pid_t pid_;
28   // When forking (no meminfo), the real app is our grandchild. In this case,
29   // child_checker_ is a CheckerSide to our child that can waitpid our grandchild on our behalf
30   CheckerSide* child_checker_ = nullptr;
31
32   void setup_events(bool socket_only); // Part of the initialization
33   void handle_dead_child(int status); // Launched when the dying child is the PID we follow
34   void handle_waitpid();              // Launched when receiving a sigchild
35
36 public:
37   explicit CheckerSide(int socket, CheckerSide* child_checker);
38   explicit CheckerSide(const std::vector<char*>& args);
39   ~CheckerSide();
40
41   // No copy:
42   CheckerSide(CheckerSide const&) = delete;
43   CheckerSide& operator=(CheckerSide const&) = delete;
44   CheckerSide& operator=(CheckerSide&&) = delete;
45
46   /* Communicating with the application */
47   Channel const& get_channel() const { return channel_; }
48   Channel& get_channel() { return channel_; }
49
50   bool handle_message(const char* buffer, ssize_t size);
51   void dispatch_events() const;
52   void break_loop() const;
53   void wait_for_requests();
54
55   /* Create a new CheckerSide by forking the currently existing one, and connect it through the master_socket */
56   std::unique_ptr<CheckerSide> clone(int master_socket, const std::string& master_socket_name);
57
58   /** Ask the application to run post-mortem analysis, and maybe to stop ASAP */
59   void finalize(bool terminate_asap = false);
60
61   /* Interacting with the application process */
62   pid_t get_pid() const { return pid_; }
63   bool running() const { return running_; }
64   void terminate() { running_ = false; }
65 };
66
67 } // namespace simgrid::mc
68
69 #endif