Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bugfix. Remove the task from the remote host fifo when there is a
[simgrid.git] / src / msg / gos.c
index f61ed61..2d12f65 100644 (file)
@@ -8,7 +8,7 @@
 #include "private.h"
 #include "xbt/sysdep.h"
 #include "xbt/log.h"
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gos, msg,
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
                                "Logging specific to MSG (gos)");
 
 /** \defgroup msg_gos_functions MSG Operating System Functions
@@ -24,6 +24,7 @@ static MSG_error_t __MSG_task_get_with_time_out_from_host(m_task_t * task,
   m_process_t process = MSG_process_self();
   m_task_t t = NULL;
   m_host_t h = NULL;
+  m_task_t task_to_wait_for;
   simdata_task_t t_simdata = NULL;
   simdata_host_t h_simdata = NULL;
   int first_time = 1;
@@ -62,24 +63,26 @@ static MSG_error_t __MSG_task_get_with_time_out_from_host(m_task_t * task,
                                                       
     if(max_duration>0) {
       if(!first_time) {
-       MSG_RETURN(MSG_OK);
+       PAJE_PROCESS_POP_STATE(process);
+       MSG_RETURN(MSG_TRANSFER_FAILURE);
       }
     }
-    xbt_assert2(!(h_simdata->sleeping[channel]),
-               "A process (%s(%d)) is already blocked on this channel",
+    xbt_assert3(!(h_simdata->sleeping[channel]),
+               "A process (%s(%d)) is already blocked on channel %d",
                h_simdata->sleeping[channel]->name,
-               h_simdata->sleeping[channel]->simdata->PID);
+               h_simdata->sleeping[channel]->simdata->PID,
+               channel);
     h_simdata->sleeping[channel] = process; /* I'm waiting. Wake me up when you're ready */
     if(max_duration>0) {
-      __MSG_process_block(max_duration);
+      __MSG_process_block(max_duration,"");
     } else {
-      __MSG_process_block(-1);
+      __MSG_process_block(-1,"");
     }
+    h_simdata->sleeping[channel] = NULL;
+    first_time = 0;
     if(surf_workstation_resource->extension_public->get_state(h_simdata->host) 
        == SURF_CPU_OFF)
       MSG_RETURN(MSG_HOST_FAILURE);
-    h_simdata->sleeping[channel] = NULL;
-    first_time = 0;
     /* OK, we should both be ready now. Are you there ? */
   }
 
@@ -95,7 +98,7 @@ static MSG_error_t __MSG_task_get_with_time_out_from_host(m_task_t * task,
   while(MSG_process_is_suspended(t_simdata->sender)) {
     DEBUG1("Oooups, the sender (%s) has been suspended in the meantime. Let's wait for him", 
           t_simdata->sender->name);
-    m_task_t task_to_wait_for = t_simdata->sender->simdata->waiting_task;
+    task_to_wait_for = t_simdata->sender->simdata->waiting_task;
     if(__MSG_process_isBlocked(t_simdata->sender)) {
       DEBUG0("He's blocked. Let's wait for him to go in the suspended state");
       __MSG_process_unblock(t_simdata->sender);
@@ -124,7 +127,7 @@ static MSG_error_t __MSG_task_get_with_time_out_from_host(m_task_t * task,
     __MSG_process_unblock(t_simdata->sender);
   }
 
-  PAJE_PROCESS_PUSH_STATE(process,"C");  
+  PAJE_PROCESS_PUSH_STATE(process,"C",t);  
 
   do {
     DEBUG0("Waiting for action termination");
@@ -189,8 +192,9 @@ MSG_error_t MSG_task_get(m_task_t * task,
    listening. This value has to be >=0 and < than the maximal
    number of channels fixed with MSG_set_channel_number().
  * \param max_duration the maximum time to wait for a task before giving
-    up. In such a case, \a task will not be modified and will still be
-    equal to \c NULL when returning.
+    up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task 
+    will not be modified and will still be
+    equal to \c NULL when returning. 
  * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
    if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
  */
@@ -234,14 +238,12 @@ MSG_error_t MSG_task_get_from_host(m_task_t * task, int channel,
 int MSG_task_Iprobe(m_channel_t channel)
 {
   m_host_t h = NULL;
-  simdata_host_t h_simdata = NULL;
 
   xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
   DEBUG2("Probing on channel %d (%s)", channel,h->name);
   CHECK_HOST();
   h = MSG_host_self();
-  h_simdata = h->simdata;
-  return(xbt_fifo_get_first_item(h_simdata->mbox[channel])!=NULL);
+  return(xbt_fifo_get_first_item(h->simdata->mbox[channel])!=NULL);
 }
 
 /** \ingroup msg_gos_functions
@@ -256,14 +258,12 @@ int MSG_task_Iprobe(m_channel_t channel)
 int MSG_task_probe_from(m_channel_t channel)
 {
   m_host_t h = NULL;
-  simdata_host_t h_simdata = NULL;
   xbt_fifo_item_t item;
   m_task_t t;
 
   xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
   CHECK_HOST();
   h = MSG_host_self();
-  h_simdata = h->simdata;
 
   DEBUG2("Probing on channel %d (%s)", channel,h->name);
    
@@ -277,7 +277,7 @@ int MSG_task_probe_from(m_channel_t channel)
 /** \ingroup msg_gos_functions
  * \brief Wait for at most \a max_duration second for a task reception
    on \a channel. *\a PID is updated with the PID of the first process
-   that triggered this event is any.
+   that triggered this event if any.
  *
  * It takes three parameters:
  * \param channel the channel on which the agent should be
@@ -306,7 +306,8 @@ MSG_error_t MSG_channel_select_from(m_channel_t channel, double max_duration,
   }
 
   if(max_duration==0.0) {
-    return MSG_task_probe_from(channel);
+    *PID = MSG_task_probe_from(channel);
+    MSG_RETURN(MSG_OK);
   } else {
     CHECK_HOST();
     h = MSG_host_self();
@@ -325,9 +326,9 @@ MSG_error_t MSG_channel_select_from(m_channel_t channel, double max_duration,
                  h_simdata->sleeping[channel]->simdata->PID);
       h_simdata->sleeping[channel] = process; /* I'm waiting. Wake me up when you're ready */
       if(max_duration>0) {
-       __MSG_process_block(max_duration);
+       __MSG_process_block(max_duration,"");
       } else {
-       __MSG_process_block(-1);
+       __MSG_process_block(-1,"");
       }
       if(surf_workstation_resource->extension_public->get_state(h_simdata->host) 
         == SURF_CPU_OFF) {
@@ -362,7 +363,6 @@ MSG_error_t MSG_channel_select_from(m_channel_t channel, double max_duration,
  */
 int MSG_task_probe_from_host(int channel, m_host_t host)
 {
-  simdata_host_t h_simdata = NULL;
   xbt_fifo_item_t item;
   m_task_t t;
   int count = 0;
@@ -371,7 +371,6 @@ int MSG_task_probe_from_host(int channel, m_host_t host)
   xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
   CHECK_HOST();
   h = MSG_host_self();
-  h_simdata = h->simdata;
 
   DEBUG2("Probing on channel %d (%s)", channel,h->name);
    
@@ -382,12 +381,12 @@ int MSG_task_probe_from_host(int channel, m_host_t host)
   return count;
 }
 
-/** \ingroup msg_gos_functions
- * \brief Put a task on a channel of an host and waits for the end of the
- * transmission.
+/** \ingroup msg_gos_functions \brief Put a task on a channel of an
+ * host (with a timeout on the waiting of the destination host) and
+ * waits for the end of the transmission.
  *
  * This function is used for describing the behavior of an agent. It
- * takes three parameter.
+ * takes four parameter.
  * \param task a #m_task_t to send on another location. This task
    will not be usable anymore when the function will return. There is
    no automatic task duplication and you have to save your parameters
@@ -402,17 +401,24 @@ int MSG_task_probe_from_host(int channel, m_host_t host)
  * \param channel the channel on which the agent should put this
    task. This value has to be >=0 and < than the maximal number of
    channels fixed with MSG_set_channel_number().
+ * \param max_duration the maximum time to wait for a task before giving
+    up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task 
+    will not be modified 
  * \return #MSG_FATAL if \a task is not properly initialized and
- * #MSG_OK otherwise.
+   #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
+   this function was called was shut down. Returns
+   #MSG_TRANSFER_FAILURE if the transfer could not be properly done
+   (network failure, dest failure, timeout...)
  */
-MSG_error_t MSG_task_put(m_task_t task,
-                        m_host_t dest, m_channel_t channel)
+MSG_error_t MSG_task_put_with_timeout(m_task_t task, m_host_t dest, 
+                                     m_channel_t channel, double max_duration)
 {
   m_process_t process = MSG_process_self();
   simdata_task_t task_simdata = NULL;
   e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
   m_host_t local_host = NULL;
   m_host_t remote_host = NULL;
+  int first_time = 1;
 
   CHECK_HOST();
 
@@ -421,14 +427,15 @@ MSG_error_t MSG_task_put(m_task_t task,
   task_simdata = task->simdata;
   task_simdata->sender = process;
   task_simdata->source = MSG_process_get_host(process);
-  xbt_assert0(task_simdata->using==1,"Gargl!");
+  xbt_assert0(task_simdata->using==1,
+             "This taks is still being used somewhere else. You cannot send it now. Go fix your code!");
   task_simdata->comm = NULL;
   
   local_host = ((simdata_process_t) process->simdata)->host;
   remote_host = dest;
 
-  DEBUG4("Trying to send a task (%g Mb) from %s to %s on channel %d", 
-        task->simdata->message_size,local_host->name, remote_host->name, channel);
+  DEBUG4("Trying to send a task (%g kB) from %s to %s on channel %d", 
+        task->simdata->message_size/1000,local_host->name, remote_host->name, channel);
 
   xbt_fifo_push(((simdata_host_t) remote_host->simdata)->
                mbox[channel], task);
@@ -443,8 +450,32 @@ MSG_error_t MSG_task_put(m_task_t task,
   process->simdata->put_host = dest;
   process->simdata->put_channel = channel;
   while(!(task_simdata->comm)) {
+    if(max_duration>0) {
+      if(!first_time) {
+       xbt_fifo_remove(((simdata_host_t) remote_host->simdata)->mbox[channel],
+                       task);
+       PAJE_PROCESS_POP_STATE(process);
+       PAJE_COMM_STOP(process,task,channel);
+       MSG_RETURN(MSG_TRANSFER_FAILURE);
+      }
+    }
     DEBUG0("Communication not initiated yet. Let's block!");
-    __MSG_process_block(-1);
+    if(max_duration>0)
+      __MSG_process_block(max_duration,task->name);
+    else
+      __MSG_process_block(-1,task->name);
+
+    first_time = 0;
+
+    if(surf_workstation_resource->extension_public->
+       get_state(local_host->simdata->host) == SURF_CPU_OFF) {
+      xbt_fifo_remove(((simdata_host_t) remote_host->simdata)->mbox[channel],
+                     task);
+      PAJE_PROCESS_POP_STATE(process);
+      PAJE_COMM_STOP(process,task,channel);
+      MSG_task_destroy(task);
+      MSG_RETURN(MSG_HOST_FAILURE);
+    }
   }
   DEBUG0("Registering to this communication");
   surf_workstation_resource->common_public->action_use(task_simdata->comm);
@@ -452,7 +483,7 @@ MSG_error_t MSG_task_put(m_task_t task,
   process->simdata->put_channel = -1;
 
 
-  PAJE_PROCESS_PUSH_STATE(process,"C");  
+  PAJE_PROCESS_PUSH_STATE(process,"C",task);  
 
   state=surf_workstation_resource->common_public->action_get_state(task_simdata->comm);
   while (state==SURF_ACTION_RUNNING) {
@@ -461,6 +492,7 @@ MSG_error_t MSG_task_put(m_task_t task,
     state=surf_workstation_resource->common_public->action_get_state(task_simdata->comm);
   }
   DEBUG0("Action terminated");
+  task->simdata->rate=-1.0; /* Sets the rate back to default */
 
   PAJE_PROCESS_POP_STATE(process);  
 
@@ -482,6 +514,37 @@ MSG_error_t MSG_task_put(m_task_t task,
     MSG_RETURN(MSG_TRANSFER_FAILURE);
   }
 }
+/** \ingroup msg_gos_functions
+ * \brief Put a task on a channel of an host and waits for the end of the
+ * transmission.
+ *
+ * This function is used for describing the behavior of an agent. It
+ * takes three parameter.
+ * \param task a #m_task_t to send on another location. This task
+   will not be usable anymore when the function will return. There is
+   no automatic task duplication and you have to save your parameters
+   before calling this function. Tasks are unique and once it has been
+   sent to another location, you should not access it anymore. You do
+   not need to call MSG_task_destroy() but to avoid using, as an
+   effect of inattention, this task anymore, you definitely should
+   renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
+   can be transfered iff it has been correctly created with
+   MSG_task_create().
+ * \param dest the destination of the message
+ * \param channel the channel on which the agent should put this
+   task. This value has to be >=0 and < than the maximal number of
+   channels fixed with MSG_set_channel_number().
+ * \return #MSG_FATAL if \a task is not properly initialized and
+ * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
+ * this function was called was shut down. Returns
+ * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
+ * (network failure, dest failure)
+ */
+MSG_error_t MSG_task_put(m_task_t task,
+                        m_host_t dest, m_channel_t channel)
+{
+  return MSG_task_put_with_timeout(task, dest, channel, -1.0);
+}
 
 /** \ingroup msg_gos_functions
  * \brief Does exactly the same as MSG_task_put but with a bounded transmition 
@@ -496,7 +559,6 @@ MSG_error_t MSG_task_put_bounded(m_task_t task,
   MSG_error_t res = MSG_OK;
   task->simdata->rate=max_rate;
   res = MSG_task_put(task, dest, channel);
-  task->simdata->rate=-1.0;
   return(res);
 }
 
@@ -519,7 +581,7 @@ MSG_error_t MSG_task_execute(m_task_t task)
 
   __MSG_task_execute(process, task);
 
-  PAJE_PROCESS_PUSH_STATE(process,"E");  
+  PAJE_PROCESS_PUSH_STATE(process,"E",task);  
   res = __MSG_wait_for_computation(process,task);
   PAJE_PROCESS_POP_STATE(process);
   return res;
@@ -532,7 +594,9 @@ void __MSG_task_execute(m_process_t process, m_task_t task)
   CHECK_HOST();
 
   simdata = task->simdata;
-
+  xbt_assert0((!simdata->compute)&&(task->simdata->using==1),
+             "This taks is executed somewhere else. Go fix your code!");
+  simdata->using++;
   simdata->compute = surf_workstation_resource->extension_public->
     execute(MSG_process_get_host(process)->simdata->host,
            simdata->computation_amount);
@@ -540,6 +604,7 @@ void __MSG_task_execute(m_process_t process, m_task_t task)
     set_priority(simdata->compute, simdata->priority);
 
   surf_workstation_resource->common_public->action_set_data(simdata->compute,task);
+  simdata->using--;
 }
 
 MSG_error_t __MSG_wait_for_computation(m_process_t process, m_task_t task)
@@ -682,6 +747,8 @@ MSG_error_t MSG_process_sleep(double nb_sec)
   m_process_t process = MSG_process_self();
   m_task_t dummy = NULL;
   simdata_task_t simdata = NULL;
+  
+  xbt_assert1(nb_sec>=0,"Invalid duration %g",nb_sec);
 
   CHECK_HOST();
   dummy = MSG_task_create("MSG_sleep", nb_sec, 0.0, NULL);
@@ -726,7 +793,7 @@ MSG_error_t MSG_process_sleep(double nb_sec)
 }
 
 /** \ingroup msg_gos_functions
- * \brief Return the number of MSG tasks currently running on a
+ * \brief Return the number of MSG tasks currently running on
  * the host of the current running process.
  */
 static int MSG_get_msgload(void) 
@@ -742,7 +809,7 @@ static int MSG_get_msgload(void)
 
 /** \ingroup msg_gos_functions
  *
- * \brief Return the the last value returned by a MSG function (except
+ * \brief Return the last value returned by a MSG function (except
  * MSG_get_errno...).
  */
 MSG_error_t MSG_get_errno(void)