Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move a public header in the simgrid/ directory to fight the namespace polution
[simgrid.git] / examples / msg / chord / chord.c
index 90dd698..de95ba8 100644 (file)
@@ -9,8 +9,7 @@
 #include "msg/msg.h"
 #include "xbt/log.h"
 #include "xbt/asserts.h"
-#include "mc/modelchecker.h"
-#include "mc/mc.h"
+#include "simgrid/modelchecker.h"
 #include "xbt/xbt_os_time.h"
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
@@ -82,6 +81,7 @@ static int *powers2;
 
 // utility functions
 static void chord_initialize(void);
+static void chord_exit(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);
@@ -127,6 +127,11 @@ static void chord_initialize(void)
   XBT_DEBUG("Sets nb_keys to %d", nb_keys);
 }
 
+static void chord_exit(void)
+{
+  xbt_free(powers2);
+}
+
 /**
  * \brief Turns an id into an equivalent id in [0, nb_keys).
  * \param id an id
@@ -139,7 +144,7 @@ static int normalize(int id)
 }
 
 /**
- * \brief Returns whether a id belongs to the interval [start, end].
+ * \brief Returns whether an id belongs to the interval [start, end].
  *
  * The parameters are noramlized to make sure they are between 0 and nb_keys - 1).
  * 1 belongs to [62, 3]
@@ -272,7 +277,7 @@ int node(int argc, char *argv[])
   double next_check_predecessor_date = init_time + periodic_check_predecessor_delay;
   double next_lookup_date = init_time + periodic_lookup_delay;
 
-  xbt_assert0(argc == 3 || argc == 5, "Wrong number of arguments for this node");
+  xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
 
   // initialize my node
   s_node_t node = {0};
@@ -342,13 +347,16 @@ int node(int argc, char *argv[])
           MSG_process_sleep(5);
         }
       }
-      else {
+
+      if (node.comm_receive && MSG_comm_test(node.comm_receive)) {
+
         // a transfer has occured
 
         MSG_error_t status = MSG_comm_get_status(node.comm_receive);
 
         if (status != MSG_OK) {
           XBT_DEBUG("Failed to receive a task. Nevermind.");
+          MSG_comm_destroy(node.comm_receive);
           node.comm_receive = NULL;
         }
         else {
@@ -358,35 +366,12 @@ int node(int argc, char *argv[])
           handle_task(&node, task_received);
         }
       }
-
-      // 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);
-        XBT_DEBUG("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) {
-      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);
-      MSG_comm_destroy(comm_send);
-      // FIXME: the task is actually not destroyed because MSG thinks that the other side (whose process is dead) is still using it
-    }*/
+    if (node.comm_receive) {
+      MSG_comm_destroy(node.comm_receive);
+      node.comm_receive = NULL;
+    }
 
     // leave the ring
     leave(&node);
@@ -394,7 +379,6 @@ int node(int argc, char *argv[])
 
   // stop the simulation
   xbt_free(node.fingers);
-  XBT_INFO("Messages created: %lu", smx_total_comms);
   return 0;
 }
 
@@ -507,10 +491,12 @@ static int join(node_t node, int known_id)
   XBT_INFO("Joining the ring with id %d, knowing node %d", node->id, known_id);
   set_predecessor(node, -1); // no predecessor (yet)
 
+  /*
   int i;
   for (i = 0; i < nb_bits; i++) {
     set_finger(node, i, known_id);
   }
+  */
 
   int successor_id = remote_find_successor(node, known_id, node->id);
   if (successor_id == -1) {
@@ -889,16 +875,13 @@ static void random_lookup(node_t node)
  */
 int main(int argc, char *argv[])
 {
-  xbt_os_timer_t timer = xbt_os_timer_new();
-
+  MSG_global_init(&argc, argv);
   if (argc < 3) {
     printf("Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n", argv[0]);
     printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
     exit(1);
   }
 
-  MSG_global_init(&argc, argv);
-
   char **options = &argv[1];
   while (!strncmp(options[0], "-", 1)) {
 
@@ -915,7 +898,7 @@ int main(int argc, char *argv[])
        XBT_DEBUG("Set timeout to %d", timeout);
       }
       else {
-       xbt_die(bprintf("Invalid chord option '%s'", options[0]));
+       xbt_die("Invalid chord option '%s'", options[0]);
       }
     }
     options++;
@@ -926,19 +909,17 @@ int main(int argc, char *argv[])
 
   chord_initialize();
 
-  MSG_set_channel_number(0);
   MSG_create_environment(platform_file);
 
   MSG_function_register("node", node);
   MSG_launch_application(application_file);
 
-  xbt_os_timer_start(timer);
   MSG_error_t res = MSG_main();
-  xbt_os_timer_stop(timer);
-  XBT_CRITICAL("Simulation time %lf", xbt_os_timer_elapsed(timer));
+  XBT_CRITICAL("Messages created: %ld", smx_total_comms);
   XBT_INFO("Simulated time: %g", MSG_get_clock());
 
   MSG_clean();
+  chord_exit();
 
   if (res == MSG_OK)
     return 0;