Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't initialize the contextes manually, it will be done by xbt_init on need
[simgrid.git] / src / msg / global.c
index 7694c1e..44f01c1 100644 (file)
@@ -5,11 +5,12 @@
 /* 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. */
 
-#include"private.h"
-#include"xbt/sysdep.h"
-#include "xbt/error.h"
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(global, msg,
-                               "Logging specific to MSG (global)");
+#include "private.h"
+#include "xbt/sysdep.h"
+#include "xbt/log.h"
+#include "xbt/ex.h" /* ex_backtrace_display */
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg,
+                               "Logging specific to MSG (kernel)");
 
 int __stop_at_time = -1.0 ;
 
@@ -22,6 +23,7 @@ MSG_Global_t msg_global = NULL;
  *  \brief This section describes the functions you need to know to
  *  set up a simulation. You should have a look at \ref MSG_examples 
  *  to have an overview of their usage.
+ *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Simulation functions" --> \endhtmlonly
  */
 
 /********************************* MSG **************************************/
@@ -29,26 +31,21 @@ MSG_Global_t msg_global = NULL;
 /** \ingroup msg_simulation
  * \brief Initialize some MSG internal data.
  */
-void MSG_global_init(void)
+void MSG_global_init_args(int *argc, char **argv)
 {
-  int argc=0;
-  char **argv=NULL;
-
-  CRITICAL0("Function MSG_global_init() is deprecated by MSG_global_init_args().");
-  MSG_global_init_args(&argc,argv);
+  MSG_global_init(argc,argv);
 }
 
 /** \ingroup msg_simulation
  * \brief Initialize some MSG internal data.
  */
-void MSG_global_init_args(int *argc, char **argv)
+void MSG_global_init(int *argc, char **argv)
 {
   if (!msg_global) {
     surf_init(argc, argv);     /* Initialize some common structures. Warning, it sets msg_global=NULL */
      
     msg_global = xbt_new0(s_MSG_Global_t,1);
 
-    xbt_context_init();
     msg_global->host = xbt_fifo_new();
     msg_global->process_to_run = xbt_fifo_new();
     msg_global->process_list = xbt_fifo_new();
@@ -56,6 +53,10 @@ void MSG_global_init_args(int *argc, char **argv)
     msg_global->current_process = NULL;
     msg_global->registered_functions = xbt_dict_new();
     msg_global->PID = 1;
+    msg_global->task_mallocator = xbt_mallocator_new(256,
+                                                    (pvoid_f_void_t*) task_mallocator_new_f,
+                                                    (void_f_pvoid_t*) task_mallocator_free_f,
+                                                    (void_f_pvoid_t*) task_mallocator_reset_f);
   }
 }
 
@@ -65,30 +66,17 @@ void MSG_global_init_args(int *argc, char **argv)
 void MSG_paje_output(const char *filename)
 {
   int i;
-  const char *paje_preembule="%EventDef        SetLimits       0\n"
-    "% StartTime       date\n"
-    "% EndTime date\n"
-    "%EndEventDef\n"
+  const char *paje_preembule=
     "%EventDef PajeDefineContainerType 1\n"
     "% NewType string\n"
     "% ContainerType   string\n"
     "% NewName string\n"
     "%EndEventDef\n"
-    "%EventDef PajeDefineEventType     2\n"
-    "% NewType string\n"
-    "% ContainerType   string\n"
-    "% NewName string\n"
-    "%EndEventDef\n"
     "%EventDef PajeDefineStateType     3\n"
     "% NewType string\n"
     "% ContainerType   string\n"
     "% NewName string\n"
     "%EndEventDef\n"
-    "%EventDef PajeDefineVariableType  4\n"
-    "% NewType string\n"
-    "% ContainerType   string\n"
-    "% NewName string\n"
-    "%EndEventDef\n"
     "%EventDef PajeDefineLinkType      5\n"
     "% NewType string\n"
     "% ContainerType   string\n"
@@ -113,63 +101,24 @@ void MSG_paje_output(const char *filename)
     "% Name    string\n"
     "% Type    string\n"
     "%EndEventDef\n"
-    "%EventDef PajeNewEvent    9\n"
-    "% Time    date\n"
-    "% EntityType      string\n"
-    "% Container       string\n"
-    "% Value   string\n"
-    "%EndEventDef\n"
     "%EventDef PajeSetState    10\n"
     "% Time    date\n"
     "% EntityType      string\n"
     "% Container       string\n"
     "% Value   string\n"
     "%EndEventDef\n"
-    "%EventDef PajeSetState    101\n"
-    "% Time    date\n"
-    "% EntityType      string\n"
-    "% Container       string\n"
-    "% Value   string\n"
-    "% FileName        string\n"
-    "% LineNumber      int\n"
-    "%EndEventDef\n"
-    "%EventDef PajePushState   111\n"
-    "% Time    date\n"
-    "% EntityType      string\n"
-    "% Container       string\n"
-    "% Value   string\n"
-    "% FileName        string\n"
-    "% LineNumber      int\n"
-    "%EndEventDef\n"
     "%EventDef PajePushState   11\n"
     "% Time    date\n"
     "% EntityType      string\n"
     "% Container       string\n"
     "% Value   string\n"
+    "% TaskName        string\n"
     "%EndEventDef\n"
     "%EventDef PajePopState    12\n"
     "% Time    date\n"
     "% EntityType      string\n"
     "% Container       string\n"
     "%EndEventDef\n"
-    "%EventDef PajeSetVariable 13\n"
-    "% Time    date\n"
-    "% EntityType      string\n"
-    "% Container       string\n"
-    "% Value   double\n"
-    "%EndEventDef\n"
-    "%EventDef PajeAddVariable 14\n"
-    "% Time    date\n"
-    "% EntityType      string\n"
-    "% Container       string\n"
-    "% Value   double\n"
-    "%EndEventDef\n"
-    "%EventDef PajeSubVariable 15\n"
-    "% Time    date\n"
-    "% EntityType      string\n"
-    "% Container       string\n"
-    "% Value   double\n"
-    "%EndEventDef\n"
     "%EventDef PajeStartLink   16\n"
     "% Time    date\n"
     "% EntityType      string\n"
@@ -209,7 +158,7 @@ void MSG_paje_output(const char *filename)
 
   len = strlen(filename);
   if((len<ext_len) || (strncmp(filename+len-ext_len,ext,ext_len))) {
-    CRITICAL2("%s does not end by \"%s\". It may cause troubles when using Paje\n",
+    CRITICAL2("The name of the Paje trace file \"%s\" does not end by \"%s\". Paje will cause difficulties to read it.\n",
              filename,ext);
   }
 
@@ -237,17 +186,11 @@ void MSG_paje_output(const char *filename)
   }
 }
 
-/** @deprecated Defines the verbosity of the simulation.
- *  @ingroup msg_simulation
- */
-void MSG_set_verbosity(MSG_outputmode_t mode)
-{
-  CRITICAL0("MSG_set_verbosity : Deprecated function. Use the XBT logging interface.");
-}
-
 /** \defgroup m_channel_management    Understanding channels
  *  \brief This section briefly describes the channel notion of MSG
  *  (#m_channel_t).
+ *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
+ * 
  *
  *  For convenience, the simulator provides the notion of channel
  *  that is close to the tag notion in MPI. A channel is not a
@@ -275,26 +218,6 @@ MSG_error_t MSG_set_channel_number(int number)
   return MSG_OK;
 }
 
-/** \ingroup m_simulation
- * \brief Set the sharing policy used for the links
- *
- * This function can be called to change the sharing policy used for the links 
-   (see \ref paper_tcp). By default the store and forward mechanism is used
-   with a parameter equal to 0.1. This function has to be called before creating 
-   any link. 
- * \param mode the sharing policy used for the links: can be either 
-   MSG_STORE_AND_FORWARD or MSG_TCP.
- * \param param a parameter for the sharing policy. It has to be >0. It is 
-   currently used only for the MSG_STORE_AND_FORWARD flavor and represents the
-   granularity of the communications (i.e. the packet size).
- */
-MSG_error_t MSG_set_sharing_policy(MSG_sharing_t mode, double param)
-{
-  CRITICAL0("MSG_set_sharing_policy: this function is now deprecated and useless. Store and forward does not exist anymore. Please stop using it.");
-  
-  return MSG_OK;
-}
-
 /** \ingroup m_channel_management
  * \brief Return the number of channel in the simulation.
  *
@@ -309,15 +232,84 @@ int MSG_get_channel_number(void)
   return msg_global->max_channel;
 }
 
+void __MSG_display_process_status(void)
+{
+   m_process_t process = NULL;
+   xbt_fifo_item_t item = NULL;
+   int i;
+   int nbprocess=xbt_fifo_size(msg_global->process_list);
+   
+   INFO1("MSG: %d processes are still running, waiting for something.",
+        nbprocess);
+   /*  List the process and their state */
+   INFO0("MSG: <process>(<pid>) on <host>: <status>.");
+   xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
+      simdata_process_t p_simdata = (simdata_process_t) process->simdata;
+      simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
+      char *who;
+       
+      asprintf(&who,"MSG:  %s(%d) on %s: %s",
+              process->name,p_simdata->PID,
+              p_simdata->host->name,
+              (process->simdata->blocked)?"[blocked] "
+              :((process->simdata->suspended)?"[suspended] ":""));
+      
+      for (i=0; i<msg_global->max_channel; i++) {
+        if (h_simdata->sleeping[i] == process) {
+           INFO2("%s\tListening on channel %d",who,i);
+           break;
+        }
+      }
+      if (i==msg_global->max_channel) {
+        if(p_simdata->waiting_task) {
+           if(p_simdata->waiting_task->simdata->compute) {
+              if(p_simdata->put_host) {
+                 INFO4("%s\tTrying to send the task '%s' to Host %s, channel %d.",
+                       who, p_simdata->waiting_task->name,p_simdata->put_host->name, p_simdata->put_channel);
+              } else {
+                 INFO2("%s\tWaiting for %s to finish.",who,p_simdata->waiting_task->name);
+              }
+           } else if (p_simdata->waiting_task->simdata->comm) {
+              INFO2("%s\tWaiting for %s to be finished transfered.",
+                    who,p_simdata->waiting_task->name);
+           } else {
+             INFO1("%s\tUNKNOWN STATUS. Please report this bug.",who);
+           }
+/*         The following would display the trace of where the maestro thread is, 
+            since this is the thread calling this. I'd like to get the other threads to 
+            run this to see where they were blocked, but I'm not sure of how to do this */
+/*         xbt_backtrace_display(); */
+        } else { /* Must be trying to put a task somewhere */
+           INFO1("%s\tUNKNOWN STATUS. Please report this bug.",who);
+        }
+      }
+      free(who);
+   }
+}
+
+/* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
+#include <signal.h>
+
+static void _XBT_CALL inthandler(int ignored)
+{
+   INFO0("CTRL-C pressed. Displaying status and bailing out");
+   __MSG_display_process_status();
+   exit(1);
+}
+
 /** \ingroup msg_simulation
  * \brief Launch the MSG simulation
  */
 MSG_error_t MSG_main(void)
 {
   m_process_t process = NULL;
-  int nbprocess,i;
+  int i;
   double elapsed_time = 0.0;
   int state_modifications = 1;
+   
+  /* Prepare to display some more info when dying on Ctrl-C pressing */
+  signal(SIGINT,inthandler);
+   
   /* Clean IO before the run */
   fflush(stdout);
   fflush(stderr);
@@ -329,12 +321,12 @@ MSG_error_t MSG_main(void)
     if(xbt_fifo_size(msg_global->process_to_run) && (elapsed_time>0)) {
       DEBUG0("**************************************************");
     }
-    if((__stop_at_time>0) && (MSG_getClock() >= __stop_at_time)) {
+    if((__stop_at_time>0) && (MSG_get_clock() >= __stop_at_time)) {
       DEBUG0("Let's stop here!");
     }
 
     while ((process = xbt_fifo_pop(msg_global->process_to_run))) {
-      DEBUG3("Scheduling  %s(%d) on %s",            
+      DEBUG3("Scheduling %s(%d) on %s",             
             process->name,process->simdata->PID,
             process->simdata->host->name);
       msg_global->current_process = process;
@@ -358,9 +350,9 @@ MSG_error_t MSG_main(void)
       }
       
       if(!state_modifications) {
-       DEBUG1("%g : Calling surf_solve",MSG_getClock());
+       DEBUG1("%f : Calling surf_solve",MSG_get_clock());
        elapsed_time = surf_solve();
-       DEBUG1("Elapsed_time %g",elapsed_time);
+       DEBUG1("Elapsed_time %f",elapsed_time);
        
        if (elapsed_time<0.0) {
          /*       fprintf(stderr, "We're done %g\n",elapsed_time); */
@@ -376,7 +368,7 @@ MSG_error_t MSG_main(void)
          process = MSG_process_create_with_arguments(args->name, args->code, 
                                                      args->data, args->host,
                                                      args->argc,args->argv);
-         if(args->kill_time > MSG_getClock()) {
+         if(args->kill_time > MSG_get_clock()) {
            surf_timer_resource->extension_public->set(args->kill_time, 
                                                       (void*) &MSG_process_kill,
                                                       (void*) process);
@@ -429,62 +421,19 @@ MSG_error_t MSG_main(void)
     state_modifications = 0;
   }
 
-  if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
-    fprintf(stderr,
-           "MSG: Congratulations ! Simulation terminated : all process are over\n");
+  if (xbt_fifo_size(msg_global->process_list) == 0) {
+    INFO0("Congratulations ! Simulation terminated : all processes are over");
     return MSG_OK;
   } else {
-    xbt_fifo_item_t item = NULL;
-    fprintf(stderr,"MSG: Oops ! Deadlock or code not perfectly clean.\n");
-    fprintf(stderr,"MSG: %d processes are still running, waiting for something.\n",
-           nbprocess);
-    /*  List the process and their state */
-    fprintf(stderr,"MSG: <process>(<pid>) on <host>: <status>.\n");
-    xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
-      simdata_process_t p_simdata = (simdata_process_t) process->simdata;
-      simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
-      
-
-      fprintf(stderr,"MSG:  %s(%d) on %s: ",
-            process->name,p_simdata->PID,
-            p_simdata->host->name);
-
-      if (process->simdata->blocked)     
-       fprintf(stderr,"[blocked] ");
-      if (process->simdata->suspended)           
-       fprintf(stderr,"[suspended] ");
-
-      for (i=0; i<msg_global->max_channel; i++) {
-       if (h_simdata->sleeping[i] == process) {
-         fprintf(stderr,"Listening on channel %d.\n",i);
-         break;
-       }
-      }
-      if (i==msg_global->max_channel) {
-       if(p_simdata->waiting_task) {
-         if(p_simdata->waiting_task->simdata->compute) {
-           if(p_simdata->put_host) 
-             fprintf(stderr,"Trying to send a task to Host %s, channel %d.\n",
-                     p_simdata->put_host->name, p_simdata->put_channel);
-           else 
-             fprintf(stderr,"Waiting for %s to finish.\n",p_simdata->waiting_task->name);
-         } else if (p_simdata->waiting_task->simdata->comm)
-           fprintf(stderr,"Waiting for %s to be finished transfered.\n",
-                   p_simdata->waiting_task->name);
-         else
-           fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
-       }
-       else { /* Must be trying to put a task somewhere */
-         fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
-       }
-      } 
-    }
+    INFO0("Oops ! Deadlock or code not perfectly clean.");
+    __MSG_display_process_status();
     if(XBT_LOG_ISENABLED(msg, xbt_log_priority_debug) ||
-       XBT_LOG_ISENABLED(global, xbt_log_priority_debug)) {
+       XBT_LOG_ISENABLED(msg_kernel, xbt_log_priority_debug)) {
       DEBUG0("Aborting!");
       xbt_abort();
     }
 
+    INFO0("Return a Warning.");
     return MSG_WARNING;
   }
 }
@@ -498,11 +447,10 @@ MSG_error_t MSG_main(void)
  */
 int MSG_process_killall(int reset_PIDs)
 {
-  xbt_fifo_item_t i = NULL;
   m_process_t p = NULL;
   m_process_t self = MSG_process_self();
 
-  while((p=xbt_fifo_shift(msg_global->process_list))) {
+  while((p=xbt_fifo_pop(msg_global->process_list))) {
     if(p!=self) MSG_process_kill(p);
   }
 
@@ -530,10 +478,9 @@ MSG_error_t MSG_clean(void)
   m_process_t p = NULL;
 
 
-  while((p=xbt_fifo_shift(msg_global->process_list))) {
+  while((p=xbt_fifo_pop(msg_global->process_list))) {
     MSG_process_kill(p);
   }
-  xbt_context_exit();
 
   xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
     __MSG_host_destroy(h);
@@ -542,26 +489,20 @@ MSG_error_t MSG_clean(void)
   xbt_fifo_free(msg_global->process_to_run);
   xbt_fifo_free(msg_global->process_list);
   xbt_dict_free(&(msg_global->registered_functions));
+  xbt_mallocator_free(msg_global->task_mallocator);
 
   if(msg_global->paje_output) {
     fclose(msg_global->paje_output);
     msg_global->paje_output = NULL;
   }
+  msg_config_finalize();
   free(msg_global);
-  surf_finalize();
+  surf_exit();
 
   return MSG_OK;
 }
 
 
-/** \ingroup msg_easier_life
- * \brief A clock (in second).
- * \deprecated Use MSG_get_clock
- */
-double MSG_getClock(void) {
-  return surf_get_clock();
-}
-
 /** \ingroup msg_easier_life
  * \brief A clock (in second).
  */