Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reduce some overly verbose comments
[simgrid.git] / src / mc / 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/mc_client.h"
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 extern "C" {
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
29
30 mc_client_t mc_client;
31
32 void MC_client_init(void)
33 {
34   if (mc_mode != MC_MODE_NONE)
35     return;
36   if (!getenv(MC_ENV_SOCKET_FD))
37     return;
38   mc_mode = MC_MODE_CLIENT;
39
40   if (mc_client) {
41     XBT_WARN("MC_client_init called more than once.");
42     return;
43   }
44
45   char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
46   if (!fd_env)
47     xbt_die("MC socket not found");
48
49   int fd = xbt_str_parse_int(fd_env,bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD));
50   XBT_DEBUG("Model-checked application found socket FD %i", fd);
51
52   int type;
53   socklen_t socklen = sizeof(type);
54   if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
55     xbt_die("Could not check socket type");
56   if (type != SOCK_DGRAM)
57     xbt_die("Unexpected socket type %i", type);
58   XBT_DEBUG("Model-checked application found expected socket type");
59
60   mc_client = xbt_new0(s_mc_client_t, 1);
61   mc_client->fd = fd;
62   mc_client->active = 1;
63
64   // Waiting for the model-checker:
65   if (ptrace(PTRACE_TRACEME, 0, nullptr, NULL) == -1 || raise(SIGSTOP) != 0)
66     xbt_die("Could not wait for the model-checker");
67   MC_client_handle_messages();
68 }
69
70 void MC_client_send_message(void* message, size_t size)
71 {
72   if (MC_protocol_send(mc_client->fd, message, size))
73     xbt_die("Could not send message %i", (int) ((mc_message_t)message)->type);
74 }
75
76 void MC_client_send_simple_message(e_mc_message_type type)
77 {
78   if (MC_protocol_send_simple_message(mc_client->fd, type))
79     xbt_die("Could not send message %i", type);
80 }
81
82 void MC_client_handle_messages(void)
83 {
84   while (1) {
85     XBT_DEBUG("Waiting messages from model-checker");
86
87     char message_buffer[MC_MESSAGE_LENGTH];
88     ssize_t s;
89     if ((s = MC_receive_message(mc_client->fd, &message_buffer, sizeof(message_buffer), 0)) < 0)
90       xbt_die("Could not receive commands from the model-checker");
91
92     s_mc_message_t message;
93     if ((size_t) s < sizeof(message))
94       xbt_die("Received message is too small");
95     memcpy(&message, message_buffer, sizeof(message));
96     switch (message.type) {
97
98     case MC_MESSAGE_DEADLOCK_CHECK:
99       {
100         int result = MC_deadlock_check();
101         s_mc_int_message_t answer;
102         answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
103         answer.value = result;
104         if (MC_protocol_send(mc_client->fd, &answer, sizeof(answer)))
105           xbt_die("Could not send response");
106       }
107       break;
108
109     case MC_MESSAGE_CONTINUE:
110       return;
111
112     case MC_MESSAGE_SIMCALL_HANDLE:
113       {
114         s_mc_simcall_handle_message_t message;
115         if (s != sizeof(message))
116           xbt_die("Unexpected size for SIMCALL_HANDLE");
117         memcpy(&message, message_buffer, sizeof(message));
118         smx_process_t process = SIMIX_process_from_PID(message.pid);
119         if (!process)
120           xbt_die("Invalid pid %lu", (unsigned long) message.pid);
121         SIMIX_simcall_handle(&process->simcall, message.value);
122         MC_protocol_send_simple_message(mc_client->fd, MC_MESSAGE_WAITING);
123       }
124       break;
125
126     case MC_MESSAGE_RESTORE:
127       {
128         s_mc_restore_message_t message;
129         if (s != sizeof(message))
130           xbt_die("Unexpected size for SIMCALL_HANDLE");
131         memcpy(&message, message_buffer, sizeof(message));
132         smpi_really_switch_data_segment(message.index);
133       }
134       break;
135
136     default:
137       xbt_die("%s received unexpected message %s (%i)",
138         MC_mode_name(mc_mode),
139         MC_message_type_name(message.type),
140         message.type
141       );
142     }
143   }
144 }
145
146 void MC_client_main_loop(void)
147 {
148   while (1) {
149     MC_protocol_send_simple_message(mc_client->fd, MC_MESSAGE_WAITING);
150     MC_client_handle_messages();
151     simgrid::mc::wait_for_requests();
152   }
153 }
154
155 }