Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : declare variable at the right place
[simgrid.git] / src / mc / mc_client.c
index c903246..7403eb2 100644 (file)
@@ -22,6 +22,8 @@
 #include "mc_mmalloc.h"
 #include "mc_ignore.h"
 #include "mc_model_checker.h"
+#include "mc_private.h" // MC_deadlock_check()
+#include "mc_smx.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
 
@@ -44,7 +46,7 @@ void MC_client_init(void)
   int type;
   socklen_t socklen = sizeof(type);
   if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
-    xbt_die("Could not check socket type: %s", strerror(errno));
+    xbt_die("Could not check socket type");
   if (type != SOCK_DGRAM)
     xbt_die("Unexpected socket type %i", type);
   XBT_DEBUG("Model-checked application found expected socket type");
@@ -56,10 +58,8 @@ void MC_client_init(void)
 
 void MC_client_hello(void)
 {
-  XBT_DEBUG("Greeting the MC server");
   if (MC_protocol_hello(mc_client->fd) != 0)
     xbt_die("Could not say hello the MC server");
-  XBT_DEBUG("Greeted the MC server");
 }
 
 void MC_client_send_message(void* message, size_t size)
@@ -68,6 +68,12 @@ void MC_client_send_message(void* message, size_t size)
     xbt_die("Could not send message %i", (int) ((mc_message_t)message)->type);
 }
 
+void MC_client_send_simple_message(int type)
+{
+  if (MC_protocol_send_simple_message(mc_client->fd, type))
+    xbt_die("Could not send message %i", type);
+}
+
 void MC_client_handle_messages(void)
 {
   while (1) {
@@ -75,38 +81,58 @@ void MC_client_handle_messages(void)
 
     char message_buffer[MC_MESSAGE_LENGTH];
     size_t s;
-    if ((s = recv(mc_client->fd, &message_buffer, sizeof(message_buffer), 0)) == -1)
-      xbt_die("Could not receive commands from the model-checker: %s",
-        strerror(errno));
+    if ((s = MC_receive_message(mc_client->fd, &message_buffer, sizeof(message_buffer), 0)) == -1)
+      xbt_die("Could not receive commands from the model-checker");
 
-    XBT_DEBUG("Receive message from model-checker");
     s_mc_message_t message;
     if (s < sizeof(message))
-      xbt_die("Message is too short");
+      xbt_die("Received message is too small");
     memcpy(&message, message_buffer, sizeof(message));
     switch (message.type) {
+
+    case MC_MESSAGE_DEADLOCK_CHECK:
+      {
+        int result = MC_deadlock_check();
+        s_mc_int_message_t answer;
+        answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
+        answer.value = result;
+        if (MC_protocol_send(mc_client->fd, &answer, sizeof(answer)))
+          xbt_die("Could not send response");
+      }
+      break;
+
     case MC_MESSAGE_CONTINUE:
       return;
+
+    case MC_MESSAGE_SIMCALL_HANDLE:
+      {
+        s_mc_simcall_handle_message_t message;
+        if (s != sizeof(message))
+          xbt_die("Unexpected size for SIMCALL_HANDLE");
+        memcpy(&message, message_buffer, sizeof(message));
+        smx_process_t process = SIMIX_process_from_PID(message.pid);
+        if (!process)
+          xbt_die("Invalid pid %lu", (unsigned long) message.pid);
+        SIMIX_simcall_handle(&process->simcall, message.value);
+        MC_protocol_send_simple_message(mc_client->fd, MC_MESSAGE_WAITING);
+      }
+      break;
+
     default:
-      xbt_die("Unexpected message from model-checker %i", message.type);
+      xbt_die("%s received unexpected message %s (%i)",
+        MC_mode_name(mc_mode),
+        MC_message_type_name(message.type),
+        message.type
+      );
     }
   }
 }
 
-void MC_ignore(void* addr, size_t size)
+void MC_client_main_loop(void)
 {
-  if (mc_mode == MC_MODE_CLIENT) {
-    s_mc_ignore_memory_message_t message;
-    message.type = MC_MESSAGE_IGNORE_MEMORY;
-    message.addr = addr;
-    message.size = size;
-    MC_client_send_message(&message, sizeof(message));
+  while (1) {
+    MC_protocol_send_simple_message(mc_client->fd, MC_MESSAGE_WAITING);
+    MC_client_handle_messages();
+    MC_wait_for_requests();
   }
-
-  // TODO, remove this once the migration has been completed
-  int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
-  MC_SET_MC_HEAP;
-  MC_process_ignore_memory(&mc_model_checker->process, addr, size);
-  if (!raw_mem_set)
-    MC_SET_STD_HEAP;
 }