Logo AND Algorithmique Numérique Distribuée

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