Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a36075f6b682b7fde7dbbbe70b69ffc53226cfa7
[simgrid.git] / src / mc / Client.cpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <cstdlib>
8 #include <cerrno>
9
10 #include <sys/types.h>
11 #include <sys/ptrace.h>
12 #include <sys/socket.h>
13
14 #include <xbt/log.h>
15 #include <xbt/sysdep.h>
16 #include <xbt/mmalloc.h>
17
18 #include "src/mc/mc_protocol.h"
19 #include "src/mc/Client.hpp"
20
21 // We won't need those once the separation MCer/MCed is complete:
22 #include "src/mc/mc_ignore.h"
23 #include "src/mc/mc_private.h" // MC_deadlock_check()
24 #include "src/mc/mc_smx.h"
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
27
28 namespace simgrid {
29 namespace mc {
30
31 std::unique_ptr<Client> Client::client_;
32
33 Client* Client::initialize()
34 {
35   // We are not in MC mode:
36   // TODO, handle this more gracefully.
37   if (!getenv(MC_ENV_SOCKET_FD))
38     return nullptr;
39
40   // Do not break if we are called multiple times:
41   if (client_)
42     return client_.get();
43
44   // Check and set the mode:
45   if (mc_mode != MC_MODE_NONE)
46     abort();
47   mc_mode = MC_MODE_CLIENT;
48
49   // Fetch socket from MC_ENV_SOCKET_FD:
50   char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
51   if (!fd_env)
52     xbt_die("No MC socket passed in the environment");
53   int fd = xbt_str_parse_int(fd_env, bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD));
54   XBT_DEBUG("Model-checked application found socket FD %i", fd);
55
56   // Check the socket type/validity:
57   int type;
58   socklen_t socklen = sizeof(type);
59   if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
60     xbt_die("Could not check socket type");
61   if (type != SOCK_DGRAM)
62     xbt_die("Unexpected socket type %i", type);
63   XBT_DEBUG("Model-checked application found expected socket type");
64
65   client_ = std::unique_ptr<Client>(new simgrid::mc::Client(fd));
66
67   // Wait for the model-checker:
68   if (ptrace(PTRACE_TRACEME, 0, nullptr, NULL) == -1 || raise(SIGSTOP) != 0)
69     xbt_die("Could not wait for the model-checker");
70
71   client_->handleMessages();
72   return client_.get();
73 }
74
75 void Client::handleMessages()
76 {
77   while (1) {
78     XBT_DEBUG("Waiting messages from model-checker");
79
80     char message_buffer[MC_MESSAGE_LENGTH];
81     ssize_t s;
82
83     if ((s = channel_.receive(&message_buffer, sizeof(message_buffer))) < 0)
84       xbt_die("Could not receive commands from the model-checker");
85
86     s_mc_message_t message;
87     if ((size_t) s < sizeof(message))
88       xbt_die("Received message is too small");
89     memcpy(&message, message_buffer, sizeof(message));
90     switch (message.type) {
91
92     case MC_MESSAGE_DEADLOCK_CHECK:
93       {
94         int result = MC_deadlock_check();
95         s_mc_int_message_t answer;
96         answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
97         answer.value = result;
98         if (channel_.send(answer))
99           xbt_die("Could not send response");
100       }
101       break;
102
103     case MC_MESSAGE_CONTINUE:
104       return;
105
106     case MC_MESSAGE_SIMCALL_HANDLE:
107       {
108         s_mc_simcall_handle_message_t message;
109         if (s != sizeof(message))
110           xbt_die("Unexpected size for SIMCALL_HANDLE");
111         memcpy(&message, message_buffer, sizeof(message));
112         smx_process_t process = SIMIX_process_from_PID(message.pid);
113         if (!process)
114           xbt_die("Invalid pid %lu", (unsigned long) message.pid);
115         SIMIX_simcall_handle(&process->simcall, message.value);
116         if (channel_.send(MC_MESSAGE_WAITING))
117           xbt_die("Could not send MESSAGE_WAITING to model-checker");
118       }
119       break;
120
121     case MC_MESSAGE_RESTORE:
122       {
123         s_mc_restore_message_t message;
124         if (s != sizeof(message))
125           xbt_die("Unexpected size for SIMCALL_HANDLE");
126         memcpy(&message, message_buffer, sizeof(message));
127         smpi_really_switch_data_segment(message.index);
128       }
129       break;
130
131     default:
132       xbt_die("%s received unexpected message %s (%i)",
133         MC_mode_name(mc_mode),
134         MC_message_type_name(message.type),
135         message.type
136       );
137     }
138   }
139 }
140
141 void Client::mainLoop(void)
142 {
143   while (1) {
144     if (channel_.send(MC_MESSAGE_WAITING))
145       xbt_die("Could not send WAITING mesage to model-checker");
146     this->handleMessages();
147     simgrid::mc::wait_for_requests();
148   }
149 }
150
151 }
152 }