Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Killall can now be called from an agent
[simgrid.git] / src / msg / m_process.c
index af8e813..c80cc6b 100644 (file)
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(m_process, msg,
                                "Logging specific to MSG (process)");
 
+/** \defgroup m_process_management Management Functions of Agents
+ *  \brief This section describes the agent structure of MSG
+ *  (#m_process_t) and the functions for managing it.
+ *
+ *  We need to simulate many independent scheduling decisions, so
+ *  the concept of <em>process</em> is at the heart of the
+ *  simulator. A process may be defined as a <em>code</em>, with
+ *  some <em>private data</em>, executing in a <em>location</em>.
+ *  \see m_process_t
+ */
+
 /******************************** Process ************************************/
+/** \ingroup m_process_management
+ * \brief Creates and runs a new #m_process_t.
+ *
+ * Does exactly the same as #MSG_process_create_with_arguments but without 
+   providing standard arguments (\a argc, \a argv).
+ * \sa MSG_process_create_with_arguments
+ */
+m_process_t MSG_process_create(const char *name,
+                              m_process_code_t code, void *data,
+                              m_host_t host)
+{
+  return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
+}
+
+static void MSG_process_cleanup(void *arg)
+{
+
+  while(((m_process_t)arg)->simdata->paje_state) {
+    PAJE_PROCESS_POP_STATE((m_process_t)arg);
+  }
+
+  PAJE_PROCESS_FREE(arg);
+
+  xbt_fifo_remove(msg_global->process_list, arg);
+  xbt_fifo_remove(msg_global->process_to_run, arg);
+  xbt_fifo_remove(((m_process_t) arg)->simdata->host->simdata->process_list, arg);
+  free(((m_process_t) arg)->name);
+  free(((m_process_t) arg)->simdata);
+  free(arg);
+}
+
 /** \ingroup m_process_management
  * \brief Creates and runs a new #m_process_t.
 
@@ -31,27 +73,11 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(m_process, msg,
    object.  It is for user-level information and can be NULL. It can
    be retrieved with the function \ref MSG_process_get_data.
  * \param host the location where the new agent is executed.
+ * \param argc first argument passed to \a code
+ * \param argv second argument passed to \a code
  * \see m_process_t
  * \return The new corresponding object.
  */
-m_process_t MSG_process_create(const char *name,
-                              m_process_code_t code, void *data,
-                              m_host_t host)
-{
-  return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
-}
-
-static void MSG_process_cleanup(void *arg)
-{
-  xbt_fifo_remove(msg_global->process_list, arg);
-  xbt_fifo_remove(msg_global->process_to_run, arg);
-  xbt_fifo_remove(((m_process_t) arg)->simdata->host->simdata->process_list, arg);
-  xbt_free(((m_process_t) arg)->name);
-  xbt_free(((m_process_t) arg)->simdata);
-  xbt_free(arg);
-}
-
-
 m_process_t MSG_process_create_with_arguments(const char *name,
                                              m_process_code_t code, void *data,
                                              m_host_t host, int argc, char **argv)
@@ -59,12 +85,11 @@ m_process_t MSG_process_create_with_arguments(const char *name,
   simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
   m_process_t process = xbt_new0(s_m_process_t,1);
   m_process_t self = NULL;
-  static int PID = 1;
 
   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
   /* Simulator Data */
 
-  simdata->PID = PID++;
+  simdata->PID = msg_global->PID++;
   simdata->host = host;
   simdata->waiting_task = NULL;
   simdata->argc = argc;
@@ -96,9 +121,54 @@ m_process_t MSG_process_create_with_arguments(const char *name,
   xbt_fifo_push(msg_global->process_list, process);
   xbt_fifo_push(msg_global->process_to_run, process);
 
+  PAJE_PROCESS_NEW(process);
+
   return process;
 }
 
+/** \ingroup m_process_management
+ * \param process poor victim
+ *
+ * This function simply kills a \a process... scarry isn't it ? :)
+ */
+void MSG_process_kill(m_process_t process)
+{
+  int i;
+  simdata_process_t p_simdata = process->simdata;
+  simdata_host_t h_simdata= p_simdata->host->simdata;
+
+/*   fprintf(stderr,"Killing %s(%d) on %s.\n",process->name, */
+/*       p_simdata->PID,p_simdata->host->name); */
+  
+  for (i=0; i<msg_global->max_channel; i++) {
+    if (h_simdata->sleeping[i] == process) {
+      h_simdata->sleeping[i] = NULL;
+      break;
+    }
+  }
+  if (i==msg_global->max_channel) {
+    if(p_simdata->waiting_task) {
+      if(p_simdata->waiting_task->simdata->compute)
+       surf_workstation_resource->common_public->
+         action_free(p_simdata->waiting_task->simdata->compute);
+      else if (p_simdata->waiting_task->simdata->comm)
+       surf_workstation_resource->common_public->
+         action_change_state(p_simdata->waiting_task->simdata->comm,SURF_ACTION_FAILED);
+      else
+       CRITICAL0("UNKNOWN STATUS. Please report this bug.");
+    } else { /* Must be trying to put a task somewhere */
+      if(process==MSG_process_self()) {
+       return;
+      } else {
+       CRITICAL0("UNKNOWN STATUS. Please report this bug.");
+      }
+    }
+  }
+
+  xbt_fifo_remove(msg_global->process_list,process);
+  xbt_context_free(process->simdata->context);
+}
+
 /** \ingroup m_process_management
  * \brief Migrates an agent to another location.
  *
@@ -197,7 +267,6 @@ int MSG_process_get_PID(m_process_t process)
   return (((simdata_process_t) process->simdata)->PID);
 }
 
-
 /** \ingroup m_process_management
  * \brief Returns the process ID of the parent of \a process.
  *
@@ -253,7 +322,7 @@ int MSG_process_self_PPID(void)
  */
 m_process_t MSG_process_self(void)
 {
-  return msg_global->current_process;
+  return msg_global ? msg_global->current_process : NULL;
 }
 
 /** \ingroup m_process_management
@@ -270,6 +339,8 @@ MSG_error_t MSG_process_suspend(m_process_t process)
 
   xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
 
+  PAJE_PROCESS_PUSH_STATE(process,"S");
+
   if(process!=MSG_process_self()) {
     simdata = process->simdata;
     
@@ -321,7 +392,10 @@ MSG_error_t MSG_process_resume(m_process_t process)
 
   simdata = process->simdata;
 
+
   if(simdata->blocked) {
+    PAJE_PROCESS_POP_STATE(process);
+
     simdata->suspended = 0; /* He'll wake up by itself */
     MSG_RETURN(MSG_OK);
   }
@@ -333,10 +407,14 @@ MSG_error_t MSG_process_resume(m_process_t process)
   simdata_task = simdata->waiting_task->simdata;
 
 
-  if(simdata_task->compute) 
+  if(simdata_task->compute) {
     surf_workstation_resource->common_public->resume(simdata_task->compute);
-  else 
+    PAJE_PROCESS_POP_STATE(process);
+  }
+  else {
+    PAJE_PROCESS_POP_STATE(process);
     surf_workstation_resource->common_public->resume(simdata_task->comm);
+  }
 
   MSG_RETURN(MSG_OK);
 }
@@ -354,10 +432,6 @@ int MSG_process_isSuspended(m_process_t process)
   return (process->simdata->suspended);
 }
 
-
-
-
-
 MSG_error_t __MSG_process_block(void)
 {
   m_process_t process = MSG_process_self();
@@ -365,6 +439,8 @@ MSG_error_t __MSG_process_block(void)
   m_task_t dummy = MSG_TASK_UNINITIALIZED;
   dummy = MSG_task_create("blocked", 0.0, 0, NULL);
   
+  PAJE_PROCESS_PUSH_STATE(process,"B");
+
   process->simdata->blocked=1;
   __MSG_task_execute(process,dummy);
   surf_workstation_resource->common_public->suspend(dummy->simdata->compute);
@@ -399,6 +475,8 @@ MSG_error_t __MSG_process_unblock(m_process_t process)
 
   surf_workstation_resource->common_public->resume(simdata_task->compute);
 
+  PAJE_PROCESS_POP_STATE(process);
+
   MSG_RETURN(MSG_OK);
 }