Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
useless cleanups
[simgrid.git] / src / msg / msg_gos.c
index e6bd45a..73c91df 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (c) 2004-2012. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2004-2014. The SimGrid Team.
+ * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -57,7 +58,7 @@ msg_error_t MSG_parallel_task_execute(msg_task_t task)
 
   xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
              "This task is executed somewhere else. Go fix your code! %d",
-             task->simdata->isused);
+             task->simdata->isused!=NULL);
 
   XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
 
@@ -70,8 +71,10 @@ msg_error_t MSG_parallel_task_execute(msg_task_t task)
 
 
   TRY {
-
-    simdata->isused=1;
+    if (msg_global->debug_multiple_use)
+      MSG_BT(simdata->isused, "Using Backtrace");
+    else
+      simdata->isused = (void*)1;
 
     if (simdata->host_nb > 0) {
       simdata->compute = simcall_host_parallel_execute(task->name,
@@ -82,10 +85,16 @@ msg_error_t MSG_parallel_task_execute(msg_task_t task)
                                                        1.0, -1.0);
       XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
     } else {
+      unsigned long affinity_mask = (unsigned long) xbt_dict_get_or_null_ext(simdata->affinity_mask_db, (char *) p_simdata->m_host, sizeof(msg_host_t));
+      XBT_DEBUG("execute %s@%s with affinity(0x%04lx)", MSG_task_get_name(task), MSG_host_get_name(p_simdata->m_host), affinity_mask);
+
       simdata->compute = simcall_host_execute(task->name,
                                               p_simdata->m_host,
                                               simdata->computation_amount,
-                                              simdata->priority);
+                                              simdata->priority,
+                                              simdata->bound,
+                                              affinity_mask
+                                              );
 
     }
 #ifdef HAVE_TRACING
@@ -96,7 +105,9 @@ msg_error_t MSG_parallel_task_execute(msg_task_t task)
 
     p_simdata->waiting_action = NULL;
 
-    simdata->isused=0;
+    if (msg_global->debug_multiple_use && simdata->isused!=0)
+      xbt_ex_free(*(xbt_ex_t*)simdata->isused);
+    simdata->isused = 0;
 
     XBT_DEBUG("Execution task '%s' finished in state %d",
               task->name, (int)comp_state);
@@ -106,6 +117,9 @@ msg_error_t MSG_parallel_task_execute(msg_task_t task)
     case cancel_error:
       status = MSG_TASK_CANCELED;
       break;
+    case host_error:
+      status = MSG_HOST_FAILURE;
+      break;
     default:
       RETHROW;
     }
@@ -146,7 +160,7 @@ msg_error_t MSG_process_sleep(double nb_sec)
   /*proc->simdata->waiting_action = act_sleep;
 
   FIXME: check if not setting the waiting_action breaks something on msg
-  
+
   proc->simdata->waiting_action = NULL;*/
 
   TRY {
@@ -155,6 +169,13 @@ msg_error_t MSG_process_sleep(double nb_sec)
   CATCH(e) {
     switch (e.category) {
     case cancel_error:
+      XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, WTF here ?"); 
+      // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
+      // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
+      // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
+      // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
+      // and did not change anythings at the C level.
+      // See comment in the jmsg_process.c file, function JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos) 
       status = MSG_TASK_CANCELED;
       break;
     default:
@@ -222,7 +243,7 @@ MSG_task_receive_from_host(msg_task_t * task, const char *alias,
  */
 msg_error_t
 MSG_task_receive_from_host_bounded(msg_task_t * task, const char *alias,
-                           msg_host_t host, double rate)
+                                   msg_host_t host, double rate)
 {
   return MSG_task_receive_ext_bounded(task, alias, -1, host, rate);
 }
@@ -257,7 +278,8 @@ msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
  * #MSG_OK if the task was successfully received,
  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
  */
-msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
+msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias,
+                                     double rate)
 {
   return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
 }
@@ -299,9 +321,9 @@ MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
  */
 msg_error_t
 MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias,
-                              double timeout,double rate)
+                                      double timeout,double rate)
 {
-  return MSG_task_receive_ext_bounded(task, alias, timeout, NULL,rate);
+  return MSG_task_receive_ext_bounded(task, alias, timeout, NULL, rate);
 }
 
 /** \ingroup msg_task_usage
@@ -363,15 +385,86 @@ MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
  */
 msg_error_t
 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
-                     msg_host_t host, double rate)
+                             msg_host_t host, double rate)
 {
   XBT_DEBUG
       ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
        alias);
   return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
-                                  host, timeout, rate);
+                                          host, timeout, rate);
+}
+
+/* Internal function used to factorize code between
+ * MSG_task_isend_with_matching() and MSG_task_dsend().
+ */
+static XBT_INLINE
+msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
+                                   int (*match_fun)(void*,void*, smx_action_t),
+                                   void *match_data, void_f_pvoid_t cleanup,
+                                   int detached)
+{
+  simdata_task_t t_simdata = NULL;
+  msg_process_t process = MSG_process_self();
+  msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
+
+#ifdef HAVE_TRACING
+  int call_end = TRACE_msg_task_put_start(task);
+#endif
+
+  /* Prepare the task to send */
+  t_simdata = task->simdata;
+  t_simdata->sender = process;
+  t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
+
+  if (t_simdata->isused != 0) {
+    if (msg_global->debug_multiple_use){
+      XBT_ERROR("This task is already used in there:");
+      xbt_backtrace_display(t_simdata->isused);
+      XBT_ERROR("And you try to reuse it from here:");
+      xbt_backtrace_display_current();
+    } else {
+      xbt_assert(t_simdata->isused == 0,
+                 "This task is still being used somewhere else. You cannot send it now. Go fix your code! (use --cfg=msg/debug_multiple_use:on to get the backtrace of the other process)");
+    }
+  }
+
+  if (msg_global->debug_multiple_use)
+    MSG_BT(t_simdata->isused, "Using Backtrace");
+  else
+    t_simdata->isused = (void*)1;
+  t_simdata->comm = NULL;
+  msg_global->sent_msg++;
+
+  /* Send it by calling SIMIX network layer */
+  smx_action_t act = simcall_comm_isend(SIMIX_process_self(), mailbox, t_simdata->message_size,
+                                        t_simdata->rate, task, sizeof(void *),
+                                        match_fun, cleanup, NULL, match_data,detached);
+  t_simdata->comm = act; /* FIXME: is the field t_simdata->comm still useful? */
+
+  msg_comm_t comm;
+  if (detached) {
+    comm = NULL;
+  } else {
+    comm = xbt_new0(s_msg_comm_t, 1);
+    comm->task_sent = task;
+    comm->task_received = NULL;
+    comm->status = MSG_OK;
+    comm->s_comm = act;
+  }
+
+#ifdef HAVE_TRACING
+  if (TRACE_is_enabled()) {
+    simcall_set_category(act, task->category);
+  }
+
+  if (call_end)
+    TRACE_msg_task_put_end();
+#endif
+
+  return comm;
 }
 
+
 /** \ingroup msg_task_usage
  * \brief Sends a task on a mailbox.
  *
@@ -384,7 +477,7 @@ MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeou
  */
 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
 {
-  return MSG_task_isend_with_matching(task,alias,NULL,NULL);
+  return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
 }
 
 /** \ingroup msg_task_usage
@@ -399,10 +492,11 @@ msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
  * \param maxrate the maximum communication rate for sending this task .
  * \return the msg_comm_t communication created
  */
-msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
+msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias,
+                                  double maxrate)
 {
   task->simdata->rate = maxrate;
-  return MSG_task_isend_with_matching(task,alias,NULL,NULL);
+  return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
 }
 
 
@@ -421,51 +515,12 @@ msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double max
  * \param match_data user provided data passed to match_fun
  * \return the msg_comm_t communication created
  */
-XBT_INLINE msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
-    int (*match_fun)(void*,void*, smx_action_t),
-    void *match_data)
+msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
+                                        int (*match_fun)(void*, void*,
+                                                         smx_action_t),
+                                        void *match_data)
 {
-  simdata_task_t t_simdata = NULL;
-  msg_process_t process = MSG_process_self();
-  msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
-
-#ifdef HAVE_TRACING
-  int call_end = TRACE_msg_task_put_start(task);
-#endif
-
-  /* Prepare the task to send */
-  t_simdata = task->simdata;
-  t_simdata->sender = process;
-  t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
-
-  xbt_assert(t_simdata->isused == 0,
-              "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
-
-  t_simdata->isused = 1;
-  t_simdata->comm = NULL;
-  msg_global->sent_msg++;
-
-  /* Send it by calling SIMIX network layer */
-  msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
-  comm->task_sent = task;
-  comm->task_received = NULL;
-  comm->status = MSG_OK;
-  comm->s_comm =
-    simcall_comm_isend(mailbox, t_simdata->message_size,
-                         t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
-  t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
-#ifdef HAVE_TRACING
-    if (TRACE_is_enabled()) {
-      simcall_set_category(comm->s_comm, task->category);
-    }
-#endif
-
-#ifdef HAVE_TRACING
-  if (call_end)
-    TRACE_msg_task_put_end();
-#endif
-
-  return comm;
+  return MSG_task_isend_internal(task, alias, match_fun, match_data, NULL, 0);
 }
 
 /** \ingroup msg_task_usage
@@ -487,43 +542,9 @@ XBT_INLINE msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *
  */
 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
 {
-  simdata_task_t t_simdata = NULL;
-  msg_process_t process = MSG_process_self();
-  msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
-
-  /* Prepare the task to send */
-  t_simdata = task->simdata;
-  t_simdata->sender = process;
-  t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
-
-  xbt_assert(t_simdata->isused == 0,
-              "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
-
-  t_simdata->isused = 1;
-  t_simdata->comm = NULL;
-  msg_global->sent_msg++;
-
-#ifdef HAVE_TRACING
-  int call_end = TRACE_msg_task_put_start(task);
-#endif
-
-  /* Send it by calling SIMIX network layer */
-  smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
-                       t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
-  t_simdata->comm = comm;
-#ifdef HAVE_TRACING
-    if (TRACE_is_enabled()) {
-      simcall_set_category(comm, task->category);
-    }
-#endif
-
-#ifdef HAVE_TRACING
-  if (call_end)
-    TRACE_msg_task_put_end();
-#endif
+  MSG_task_isend_internal(task, alias, NULL, NULL, cleanup, 1);
 }
 
-
 /** \ingroup msg_task_usage
  * \brief Sends a task on a mailbox with a maximal rate.
  *
@@ -541,46 +562,13 @@ void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
  * communication fails, e.g. MSG_task_destroy
  * (if NULL, no function will be called)
  * \param maxrate the maximum communication rate for sending this task
- * 
+ *
  */
-void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
+void MSG_task_dsend_bounded(msg_task_t task, const char *alias,
+                            void_f_pvoid_t cleanup, double maxrate)
 {
   task->simdata->rate = maxrate;
-  
-  simdata_task_t t_simdata = NULL;
-  msg_process_t process = MSG_process_self();
-  msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
-
-  /* Prepare the task to send */
-  t_simdata = task->simdata;
-  t_simdata->sender = process;
-  t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
-
-  xbt_assert(t_simdata->isused == 0,
-              "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
-
-  t_simdata->isused = 1;
-  t_simdata->comm = NULL;
-  msg_global->sent_msg++;
-
-#ifdef HAVE_TRACING
-  int call_end = TRACE_msg_task_put_start(task);
-#endif
-
-  /* Send it by calling SIMIX network layer */
-  smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
-                       t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
-  t_simdata->comm = comm;
-#ifdef HAVE_TRACING
-    if (TRACE_is_enabled()) {
-      simcall_set_category(comm, task->category);
-    }
-#endif
-
-#ifdef HAVE_TRACING
-  if (call_end)
-    TRACE_msg_task_put_end();
-#endif
+  MSG_task_dsend(task, alias, cleanup);
 }
 
 /** \ingroup msg_task_usage
@@ -588,32 +576,14 @@ void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t c
  *
  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
  * to end the communication.
- * 
+ *
  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
  * \param name of the mailbox to receive the task on
  * \return the msg_comm_t communication created
  */
 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
 {
-  smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
-
-  /* FIXME: these functions are not traceable */
-
-  /* Sanity check */
-  xbt_assert(task, "Null pointer for the task storage");
-
-  if (*task)
-    XBT_CRITICAL
-        ("MSG_task_irecv() was asked to write in a non empty task struct.");
-
-  /* Try to receive it by calling SIMIX network layer */
-  msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
-  comm->task_sent = NULL;
-  comm->task_received = task;
-  comm->status = MSG_OK;
-  comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
-
-  return comm;
+  return MSG_task_irecv_bounded(task, name, -1.0);
 }
 
 /** \ingroup msg_task_usage
@@ -625,10 +595,9 @@ msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
  * \param rate limit the bandwidth to the given rate
  * \return the msg_comm_t communication created
  */
-msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
+msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name,
+                                  double rate)
 {
-
-
   smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
 
   /* FIXME: these functions are not traceable */
@@ -645,7 +614,7 @@ msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rat
   comm->task_sent = NULL;
   comm->task_received = task;
   comm->status = MSG_OK;
-  comm->s_comm = simcall_comm_irecv_bounded(rdv, task, NULL, NULL, NULL, rate);
+  comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL, NULL, rate);
 
   return comm;
 }
@@ -668,6 +637,8 @@ int MSG_comm_test(msg_comm_t comm)
 
     if (finished && comm->task_received != NULL) {
       /* I am the receiver */
+      if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
+        xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
       (*comm->task_received)->simdata->isused = 0;
     }
   }
@@ -742,6 +713,8 @@ int MSG_comm_testany(xbt_dynar_t comms)
 
     if (status == MSG_OK && comm->task_received != NULL) {
       /* I am the receiver */
+      if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
+        xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
       (*comm->task_received)->simdata->isused = 0;
     }
   }
@@ -763,7 +736,7 @@ void MSG_comm_destroy(msg_comm_t comm)
  *
  * It takes two parameters.
  * \param comm the communication to wait.
- * \param timeout Wait until the communication terminates or the timeout 
+ * \param timeout Wait until the communication terminates or the timeout
  * occurs. You can provide a -1 timeout to obtain an infinite timeout.
  * \return msg_error_t
  */
@@ -775,6 +748,8 @@ msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
 
     if (comm->task_received != NULL) {
       /* I am the receiver */
+      if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
+        xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
       (*comm->task_received)->simdata->isused = 0;
     }
 
@@ -862,6 +837,8 @@ int MSG_comm_waitany(xbt_dynar_t comms)
 
   if (comm->task_received != NULL) {
     /* I am the receiver */
+    if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
+      xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
     (*comm->task_received)->simdata->isused = 0;
   }
 
@@ -1222,7 +1199,7 @@ MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
               && (channel < msg_global->max_channel), "Invalid channel %d",
               channel);
 
-  XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
+  XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", MSG_host_get_name(dest));
   return
       MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
                                    (dest, channel), task, timeout);