Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a compilation problem with dsend and Chord
[simgrid.git] / examples / msg / chord / chord.c
index 6bccfd4..2ca3ac6 100644 (file)
@@ -10,6 +10,7 @@
 #include "xbt/log.h"
 #include "xbt/asserts.h"
 #include "mc/modelchecker.h"
+#include "mc/mc.h"
 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
                              "Messages specific for this msg example");
 
@@ -45,7 +46,6 @@ typedef struct node {
   char pred_mailbox[MAILBOX_NAME_SIZE];   // predecessor's mailbox name
   int next_finger_to_fix;                 // index of the next finger to fix in fix_fingers()
   msg_comm_t comm_receive;                // current communication to receive
-  xbt_dynar_t comms;                      // current communications being sent
   double last_change_date;                // last time I changed a finger or my predecessor
 } s_node_t, *node_t;
 
@@ -81,7 +81,7 @@ static void chord_initialize(void);
 static int normalize(int id);
 static int is_in_interval(int id, int start, int end);
 static void get_mailbox(int host_id, char* mailbox);
-static void task_data_destroy(task_data_t task_data);
+static void task_free(void* task);
 static void print_finger_table(node_t node);
 static void set_finger(node_t node, int finger_index, int id);
 static void set_predecessor(node_t node, int predecessor_id);
@@ -130,14 +130,8 @@ static void chord_initialize(void)
  */
 static int normalize(int id)
 {
-  // make sure id >= 0
-  while (id < 0) {
-    id += nb_keys;
-  }
-  // make sure id < nb_keys
-  id = id % nb_keys;
-
-  return id;
+  // like id % nb_keys, but works with negatives numbers (and faster)
+  return id & (nb_keys - 1);
 }
 
 /**
@@ -186,12 +180,14 @@ static void get_mailbox(int node_id, char* mailbox)
 }
 
 /**
- * \brief Frees the memory used by some task data.
- * \param task_data the task data to destroy
+ * \brief Frees the memory used by a task.
+ * \param task the MSG task to destroy
  */
-static void task_data_destroy(task_data_t task_data)
+static void task_free(void* task)
 {
-  xbt_free(task_data);
+  // TODO add a parameter data_free_function to MSG_task_create?
+  xbt_free(MSG_task_get_data(task));
+  MSG_task_destroy(task);
 }
 
 /**
@@ -255,12 +251,16 @@ static void set_predecessor(node_t node, int predecessor_id)
  */
 int node(int argc, char *argv[])
 {
+  /* Reduce the run size for the MC */
+  if(MC_IS_ENABLED){
+    periodic_stabilize_delay = 8;
+    periodic_fix_fingers_delay = 8;
+    periodic_check_predecessor_delay = 8;
+  }
+
   double init_time = MSG_get_clock();
-  /*m_task_t task = NULL;*/
   m_task_t task_received = NULL;
-  msg_comm_t comm_send = NULL;
   int i;
-  int index;
   int join_success = 0;
   double deadline;
   double next_stabilize_date = init_time + periodic_stabilize_delay;
@@ -275,7 +275,6 @@ int node(int argc, char *argv[])
   node.id = atoi(argv[1]);
   get_mailbox(node.id, node.mailbox);
   node.next_finger_to_fix = 0;
-  node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
   node.fingers = xbt_new0(s_finger_t, nb_bits);
   node.last_change_date = init_time;
 
@@ -357,20 +356,27 @@ int node(int argc, char *argv[])
       }
 
       // see if some communications are finished
+      /*
       while ((index = MSG_comm_testany(node.comms)) != -1) {
         comm_send = xbt_dynar_get_as(node.comms, index, msg_comm_t);
         MSG_error_t status = MSG_comm_get_status(comm_send);
         xbt_dynar_remove_at(node.comms, index, &comm_send);
         DEBUG3("Communication %p is finished with status %d, dynar size is now %lu",
             comm_send, status, xbt_dynar_length(node.comms));
+       m_task_t task = MSG_comm_get_task(comm_send);
         MSG_comm_destroy(comm_send);
+       if (status != MSG_OK) {
+         task_data_destroy(MSG_task_get_data(task));
+         MSG_task_destroy(task);
+       }
       }
+      */
     }
 
     // clean unfinished comms sent
    /* unsigned int cursor;
     xbt_dynar_foreach(node.comms, cursor, comm_send) {
-      task = MSG_comm_get_task(comm_send);
+      m_task_t task = MSG_comm_get_task(comm_send);
       MSG_task_cancel(task);
       task_data_destroy(MSG_task_get_data(task));
       MSG_task_destroy(task);
@@ -383,7 +389,6 @@ int node(int argc, char *argv[])
   }
 
   // stop the simulation
-  xbt_dynar_free(&node.comms);
   xbt_free(node.fingers);
   return 0;
 }
@@ -397,8 +402,7 @@ int node(int argc, char *argv[])
 static void handle_task(node_t node, m_task_t task) {
 
   DEBUG1("Handling task %p", task);
-  msg_comm_t comm = NULL;
-  char* mailbox = NULL;
+  char mailbox[MAILBOX_NAME_SIZE];
   task_data_t task_data = (task_data_t) MSG_task_get_data(task);
   e_task_type_t type = task_data->type;
 
@@ -411,11 +415,11 @@ static void handle_task(node_t node, m_task_t task) {
       if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
         task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
         task_data->answer_id = node->fingers[0].id;
-        DEBUG3("Sending back a 'Find Successor Answer' to %s: the successor of %d is %d",
+        DEBUG4("Sending back a 'Find Successor Answer' to %s (mailbox %s): the successor of %d is %d",
             task_data->issuer_host_name,
+           task_data->answer_to,
             task_data->request_id, task_data->answer_id);
-        comm = MSG_task_isend(task, task_data->answer_to);
-        xbt_dynar_push(node->comms, &comm);
+        MSG_task_dsend(task, task_data->answer_to, task_free);
       }
       else {
         // otherwise, forward the request to the closest preceding finger in my table
@@ -423,9 +427,7 @@ static void handle_task(node_t node, m_task_t task) {
         DEBUG2("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
             task_data->request_id, closest);
         get_mailbox(closest, mailbox);
-        comm = MSG_task_isend(task, mailbox);
-        xbt_dynar_push(node->comms, &comm);
-        xbt_free(mailbox);
+        MSG_task_dsend(task, mailbox, task_free);
       }
       break;
 
@@ -436,16 +438,14 @@ static void handle_task(node_t node, m_task_t task) {
       DEBUG3("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
           task_data->issuer_host_name,
           task_data->answer_to, task_data->answer_id);
-      comm = MSG_task_isend(task, task_data->answer_to);
-      xbt_dynar_push(node->comms, &comm);
+      MSG_task_dsend(task, task_data->answer_to, task_free);
       break;
 
     case TASK_NOTIFY:
       // someone is telling me that he may be my new predecessor
       DEBUG1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
       notify(node, task_data->request_id);
-      task_data_destroy(task_data);
-      MSG_task_destroy(task);
+      task_free(task);
       break;
 
     case TASK_PREDECESSOR_LEAVING:
@@ -453,8 +453,7 @@ static void handle_task(node_t node, m_task_t task) {
       DEBUG1("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
       // modify my predecessor
       set_predecessor(node, task_data->request_id);
-      task_data_destroy(task_data);
-      MSG_task_destroy(task);
+      task_free(task);
       /*TODO :
       >> notify my new predecessor
       >> send a notify_predecessors !!
@@ -466,8 +465,7 @@ static void handle_task(node_t node, m_task_t task) {
       DEBUG1("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
       // modify my successor FIXME : this should be implicit ?
       set_finger(node, 0, task_data->request_id);
-      task_data_destroy(task_data);
-      MSG_task_destroy(task);
+      task_free(task);
       /* TODO
       >> notify my new successor
       >> update my table & predecessors table */
@@ -476,6 +474,7 @@ static void handle_task(node_t node, m_task_t task) {
     case TASK_FIND_SUCCESSOR_ANSWER:
     case TASK_GET_PREDECESSOR_ANSWER:
       DEBUG2("Ignoring unexpected task of type %d (%p)", type, task);
+      task_free(task);
       break;
   }
 }
@@ -614,8 +613,7 @@ static int remote_find_successor(node_t node, int ask_to, int id)
   if (res != MSG_OK) {
     DEBUG3("Failed to send the 'Find Successor' request (task %p) to %d for id %d",
         task_sent, ask_to, id);
-    MSG_task_destroy(task_sent);
-    task_data_destroy(req_data);
+    task_free(task_sent);
   }
   else {
 
@@ -635,17 +633,22 @@ static int remote_find_successor(node_t node, int ask_to, int id)
         DEBUG2("Failed to receive the answer to my 'Find Successor' request (task %p): %d",
             task_sent, res);
         stop = 1;
-        //MSG_comm_destroy(node->comm_receive);
+       MSG_comm_destroy(node->comm_receive);
+       node->comm_receive = NULL;
       }
       else {
         m_task_t task_received = MSG_comm_get_task(node->comm_receive);
         DEBUG1("Received a task (%p)", task_received);
         task_data_t ans_data = MSG_task_get_data(task_received);
 
-        MC_assert(task_received == task_sent);
+       if (MC_IS_ENABLED) {
+         MC_assert(task_received == task_sent);
+       }
 
         if (task_received != task_sent) {
           // this is not the expected answer
+         MSG_comm_destroy(node->comm_receive);
+         node->comm_receive = NULL;
           handle_task(node, task_received);
         }
         else {
@@ -654,12 +657,11 @@ static int remote_find_successor(node_t node, int ask_to, int id)
               ans_data->request_id, task_received, id, ans_data->answer_id);
           successor = ans_data->answer_id;
           stop = 1;
-          MSG_task_destroy(task_received);
-          task_data_destroy(req_data);
+         MSG_comm_destroy(node->comm_receive);
+         node->comm_receive = NULL;
+          task_free(task_received);
         }
       }
-      MSG_comm_destroy(node->comm_receive);
-      node->comm_receive = NULL;
     } while (!stop);
   }
 
@@ -692,8 +694,7 @@ static int remote_get_predecessor(node_t node, int ask_to)
   if (res != MSG_OK) {
     DEBUG2("Failed to send the 'Get Predecessor' request (task %p) to %d",
         task_sent, ask_to);
-    MSG_task_destroy(task_sent);
-    task_data_destroy(req_data);
+    task_free(task_sent);
   }
   else {
 
@@ -713,15 +714,20 @@ static int remote_get_predecessor(node_t node, int ask_to)
         DEBUG2("Failed to receive the answer to my 'Get Predecessor' request (task %p): %d",
             task_sent, res);
         stop = 1;
-        //MSG_comm_destroy(node->comm_receive);
+       MSG_comm_destroy(node->comm_receive);
+       node->comm_receive = NULL;
       }
       else {
         m_task_t task_received = MSG_comm_get_task(node->comm_receive);
         task_data_t ans_data = MSG_task_get_data(task_received);
 
-        MC_assert(task_received == task_sent);
+       if (MC_IS_ENABLED) {
+         MC_assert(task_received == task_sent);
+       }
 
         if (task_received != task_sent) {
+         MSG_comm_destroy(node->comm_receive);
+         node->comm_receive = NULL;
           handle_task(node, task_received);
         }
         else {
@@ -729,12 +735,11 @@ static int remote_get_predecessor(node_t node, int ask_to)
               task_received, ask_to, ans_data->answer_id);
           predecessor_id = ans_data->answer_id;
           stop = 1;
-          MSG_task_destroy(task_received);
-          task_data_destroy(req_data);
+         MSG_comm_destroy(node->comm_receive);
+         node->comm_receive = NULL;
+          task_free(task_received);
         }
       }
-      MSG_comm_destroy(node->comm_receive);
-      node->comm_receive = NULL;
     } while (!stop);
   }
 
@@ -824,8 +829,7 @@ static void remote_notify(node_t node, int notify_id, int predecessor_candidate_
   DEBUG2("Sending a 'Notify' request (task %p) to %d", task, notify_id);
   char mailbox[MAILBOX_NAME_SIZE];
   get_mailbox(notify_id, mailbox);
-  msg_comm_t comm = MSG_task_isend(task, mailbox);
-  xbt_dynar_push(node->comms, &comm);
+  MSG_task_dsend(task, mailbox, task_free);
 }
 
 /**