Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement reforks by forking the application, to save the app exec time
[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   std::unique_ptr<RemoteProcessMemory> remote_memory_;
25
26   Channel channel_;
27   bool running_ = false;
28   pid_t pid_;
29   // When forking (no meminfo), the real app is our grandchild. In this case,
30   // child_checker_ is a CheckerSide to our child that can waitpid our grandchild on our behalf
31   CheckerSide* child_checker_ = nullptr;
32
33   void setup_events(bool socket_only); // Part of the initialization
34   void clear_memory_cache();
35   void handle_dead_child(int status); // Launched when the dying child is the PID we follow
36   void handle_waitpid();              // Launched when receiving a sigchild
37
38 public:
39   explicit CheckerSide(int socket, CheckerSide* child_checker);
40   explicit CheckerSide(const std::vector<char*>& args, bool need_memory_introspection);
41   ~CheckerSide();
42
43   // No copy:
44   CheckerSide(CheckerSide const&) = delete;
45   CheckerSide& operator=(CheckerSide const&) = delete;
46   CheckerSide& operator=(CheckerSide&&) = delete;
47
48   /* Communicating with the application */
49   Channel const& get_channel() const { return channel_; }
50   Channel& get_channel() { return channel_; }
51
52   bool handle_message(const char* buffer, ssize_t size);
53   void dispatch_events() const;
54   void break_loop() const;
55   void wait_for_requests();
56
57   /* Create a new CheckerSide by forking the currently existing one, and connect it through the master_socket */
58   std::unique_ptr<CheckerSide> clone(int master_socket);
59
60   /** Ask the application to run post-mortem analysis, and maybe to stop ASAP */
61   void finalize(bool terminate_asap = false);
62
63   /* Interacting with the application process */
64   pid_t get_pid() const { return pid_; }
65   bool running() const { return running_; }
66   void terminate() { running_ = false; }
67   RemoteProcessMemory* get_remote_memory() { return remote_memory_.get(); }
68 };
69
70 } // namespace simgrid::mc
71
72 #endif