Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] sanity checks
[simgrid.git] / src / instr / instr_paje.c
index aed13c0..dee0203 100644 (file)
 
 #ifdef HAVE_TRACING
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje, instr, "Paje tracing event system");
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje, instr, "Paje tracing event system (data structures)");
 
-static FILE *tracing_file = NULL;
+static container_t rootContainer = NULL;    /* the root container */
+static xbt_dict_t allContainers = NULL;     /* all created containers indexed by name */
+xbt_dict_t trivaNodeTypes = NULL;     /* all link types defined */
+xbt_dict_t trivaEdgeTypes = NULL;     /* all host types defined */
 
-static int pajeDefineContainerTypeId = 0;
-static int pajeDefineStateTypeId = 1;
-static int pajeDefineEntityValueId = 2;
-static int pajeDefineEventTypeId = 3;
-static int pajeDefineLinkTypeId = 4;
-static int pajeCreateContainerId = 5;
-static int pajeSetStateId = 6;
-#define UNUSED007 7
-static int pajePopStateId = 8;
-static int pajeDestroyContainerId = 9;
-#define UNUSED006 10
-#define UNUSED003 11
-static int pajeStartLinkId = 12;
-static int pajeEndLinkId = 13;
-#define UNUSED000 14
-#define UNUSED004 15
-#define UNUSED008 16
-#define UNUSED009 17
-#define UNUSED005 18
-static int pajePushStateId = 19;
-static int pajeDefineEventTypeWithColorId = 20;
-static int pajeDefineVariableTypeWithColorId = 21;
-static int pajeSetVariableId = 22;
-static int pajeAddVariableId = 23;
-static int pajeSubVariableId = 24;
-static int pajeDefineVariableTypeId = 25;
-static int pajeStartLinkWithVolumeId = 26;
-static int pajeNewEventId = 27;
-
-#define TRACE_LINE_SIZE 1000
-
-void TRACE_paje_start(void)
+long long int instr_new_paje_id (void)
 {
-  char *filename = TRACE_get_filename();
-  tracing_file = fopen(filename, "w");
-  xbt_assert1 (tracing_file != NULL, "Tracefile %s could not be opened for writing.", filename);
-
-  DEBUG1("Filename %s is open for writing", filename);
-
-  /* output header */
-  TRACE_paje_create_header();
+  static long long int type_id = 0;
+  return type_id++;
 }
 
-void TRACE_paje_end(void)
+void PJ_container_alloc (void)
 {
-  fclose(tracing_file);
-  char *filename = TRACE_get_filename();
-  DEBUG1("Filename %s is closed", filename);
+  allContainers = xbt_dict_new_homogeneous(NULL);
+  trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free);
+  trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free);
 }
 
-void TRACE_paje_create_header(void)
+void PJ_container_release (void)
 {
-  DEBUG0 ("Define paje header");
-  fprintf(tracing_file, "\
-%%EventDef PajeDefineContainerType %d \n\
-%%       Alias string \n\
-%%       ContainerType string \n\
-%%       Name string \n\
-%%EndEventDef \n\
-%%EventDef PajeDefineStateType %d \n\
-%%       Alias string \n\
-%%       ContainerType string \n\
-%%       Name string \n\
-%%EndEventDef \n\
-%%EventDef PajeDefineEntityValue %d \n\
-%%       Alias string \n\
-%%       EntityType string \n\
-%%       Name string \n\
-%%EndEventDef \n\
-%%EventDef PajeDefineEventType %d \n\
-%%       Alias string \n\
-%%       EntityType string \n\
-%%       Name string \n\
-%%       Color color \n\
-%%EndEventDef \n\
-%%EventDef PajeDefineEventType %d \n\
-%%       Alias string \n\
-%%       EntityType string \n\
-%%       Name string \n\
-%%EndEventDef \n\
-%%EventDef PajeDefineLinkType %d \n\
-%%       Alias string \n\
-%%       ContainerType string \n\
-%%       SourceContainerType string \n\
-%%       DestContainerType string \n\
-%%       Name string \n\
-%%EndEventDef \n\
-%%EventDef PajeCreateContainer %d \n\
-%%       Time date \n\
-%%       Alias string \n\
-%%       Type string \n\
-%%       Container string \n\
-%%       Name string \n\
-%%EndEventDef \n\
-%%EventDef PajeDestroyContainer %d \n\
-%%       Time date \n\
-%%       Type string \n\
-%%       Container string \n\
-%%EndEventDef \n\
-%%EventDef PajeSetState %d \n\
-%%       Time date \n\
-%%       EntityType string \n\
-%%       Container string \n\
-%%       Value string \n\
-%%EndEventDef\n\
-%%EventDef PajePopState %d \n\
-%%       Time date \n\
-%%       EntityType string \n\
-%%       Container string \n\
-%%EndEventDef\n\
-%%EventDef PajeStartLink %d \n\
-%%       Time date \n\
-%%       EntityType string \n\
-%%       Container string \n\
-%%       Value string \n\
-%%       SourceContainer string \n\
-%%       Key string \n\
-%%EndEventDef\n\
-%%EventDef PajeEndLink %d \n\
-%%       Time date \n\
-%%       EntityType string \n\
-%%       Container string \n\
-%%       Value string \n\
-%%       DestContainer string \n\
-%%       Key string \n\
-%%EndEventDef\n\
-%%EventDef PajePushState %d \n\
-%%       Time date \n\
-%%       EntityType string \n\
-%%       Container string \n\
-%%       Value string \n\
-%%EndEventDef\n\
-%%EventDef PajeSetVariable %d \n\
-%%       Time date \n\
-%%       EntityType string \n\
-%%       Container string \n\
-%%       Value string \n\
-%%EndEventDef\n\
-%%EventDef PajeAddVariable %d \n\
-%%       Time date \n\
-%%       EntityType string \n\
-%%       Container string \n\
-%%       Value string \n\
-%%EndEventDef\n\
-%%EventDef PajeSubVariable %d \n\
-%%       Time date \n\
-%%       EntityType string \n\
-%%       Container string \n\
-%%       Value string \n\
-%%EndEventDef\n\
-%%EventDef PajeDefineVariableType %d \n\
-%%       Alias string \n\
-%%       ContainerType string \n\
-%%       Name string \n\
-%%EndEventDef \n\
-%%EventDef PajeDefineVariableType %d \n\
-%%       Alias string \n\
-%%       ContainerType string \n\
-%%       Name string \n\
-%%       Color color \n\
-%%EndEventDef \n\
-%%EventDef PajeStartLink %d \n\
-%%       Time date \n\
-%%       EntityType string \n\
-%%       Container string \n\
-%%       Value string \n\
-%%       SourceContainer string \n\
-%%       Key string \n\
-%%       Volume string \n\
-%%EndEventDef\n\
-%%EventDef PajeNewEvent %d \n\
-%%       Time date \n\
-%%       EntityType string \n\
-%%       Container string \n\
-%%       Value string \n\
-%%EndEventDef\n", pajeDefineContainerTypeId, pajeDefineStateTypeId, pajeDefineEntityValueId, pajeDefineEventTypeWithColorId, pajeDefineEventTypeId, pajeDefineLinkTypeId, pajeCreateContainerId, pajeDestroyContainerId, pajeSetStateId, pajePopStateId, pajeStartLinkId, pajeEndLinkId, pajePushStateId, pajeSetVariableId, pajeAddVariableId, pajeSubVariableId, pajeDefineVariableTypeId, pajeDefineVariableTypeWithColorId, pajeStartLinkWithVolumeId, pajeNewEventId);
+  xbt_dict_free (&allContainers);
+  xbt_dict_free (&trivaNodeTypes);
+  xbt_dict_free (&trivaEdgeTypes);
 }
 
-/* internal to this file */
-static void __pajeCreateContainer(char *output, int len, int eventid,
-                                  double time, const char *alias,
-                                  const char *type, const char *container,
-                                  const char *name)
+void PJ_container_set_root (container_t root)
 {
-  if (time == 0){
-    snprintf(output, len, "%d 0 %s %s %s %s", eventid,
-           alias, type, container, name);
-  }else{
-    snprintf(output, len, "%d %lf %s %s %s %s", eventid, time,
-           alias, type, container, name);
-  }
+  rootContainer = root;
 }
 
-static void __pajeSetState(char *output, int len, int eventid, double time,
-                           const char *entityType, const char *container,
-                           const char *value)
+container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
 {
-  if (time == 0){
-    snprintf(output, len, "%d 0 %s %s %s", eventid, entityType,
-           container, value);
-  }else{
-    snprintf(output, len, "%d %lf %s %s %s", eventid, time, entityType,
-           container, value);
+  if (name == NULL){
+    THROWF (tracing_error, 0, "can't create a container with a NULL name");
   }
-}
 
-static void __pajeSetVariable(char *output, int len, int eventid,
-                              double time, const char *entityType,
-                              const char *container, const char *value)
-{
-  if (time == 0){
-    snprintf(output, len, "%d 0 %s %s %s", eventid, entityType,
-           container, value);
+  static long long int container_id = 0;
+  char id_str[INSTR_DEFAULT_STR_SIZE];
+  snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
+
+  container_t new = xbt_new0(s_container_t, 1);
+  new->name = xbt_strdup (name); // name of the container
+  new->id = xbt_strdup (id_str); // id (or alias) of the container
+  new->father = father;
+  // level depends on level of father
+  if (new->father){
+    new->level = new->father->level+1;
+    XBT_DEBUG("new container %s, child of %s", name, father->name);
   }else{
-    snprintf(output, len, "%d %lf %s %s %s", eventid, time, entityType,
-           container, value);
+    new->level = 0;
   }
-}
-
-static void __pajeStartLink(char *output, int len, int eventid,
-                            double time, const char *entityType,
-                            const char *container, const char *value,
-                            const char *sourceContainer, const char *key)
-{
-  if (time == 0){
-    snprintf(output, len, "%d 0 %s %s %s %s %s", eventid, entityType,
-           container, value, sourceContainer, key);
+  // type definition (method depends on kind of this new container)
+  new->kind = kind;
+  if (new->kind == INSTR_AS){
+    //if this container is of an AS, its type name depends on its level
+    char as_typename[INSTR_DEFAULT_STR_SIZE];
+    snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
+    if (new->father){
+      new->type = PJ_type_get (as_typename, new->father->type);
+      if (new->type == NULL){
+        new->type = PJ_type_container_new (as_typename, new->father->type);
+      }
+    }else{
+      new->type = PJ_type_container_new ("0", NULL);
+    }
   }else{
-    snprintf(output, len, "%d %lf %s %s %s %s %s", eventid, time, entityType,
-           container, value, sourceContainer, key);
+    //otherwise, the name is its kind
+    char typename[INSTR_DEFAULT_STR_SIZE];
+    switch (new->kind){
+      case INSTR_HOST:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "HOST");        break;
+      case INSTR_LINK:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "LINK");        break;
+      case INSTR_ROUTER:      snprintf (typename, INSTR_DEFAULT_STR_SIZE, "ROUTER");      break;
+      case INSTR_SMPI:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MPI");         break;
+      case INSTR_MSG_PROCESS: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS"); break;
+      case INSTR_MSG_TASK:    snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");    break;
+      default: THROWF (tracing_error, 0, "new container kind is unknown."); break;
+    }
+    type_t type = PJ_type_get (typename, new->father->type);
+    if (type == NULL){
+      new->type = PJ_type_container_new (typename, new->father->type);
+    }else{
+      new->type = type;
+    }
+  }
+  new->children = xbt_dict_new_homogeneous(NULL);
+  if (new->father){
+    xbt_dict_set(new->father->children, new->name, new, NULL);
+    new_pajeCreateContainer (new);
   }
-}
-
-/* internal do the instrumentation module */
-void pajeDefineContainerType(const char *alias, const char *containerType,
-                             const char *name)
-{
-  fprintf(tracing_file, "%d %s %s %s\n", pajeDefineContainerTypeId, alias,
-          containerType, name);
-}
-
-void pajeDefineStateType(const char *alias, const char *containerType,
-                         const char *name)
-{
-  fprintf(tracing_file, "%d %s %s %s\n", pajeDefineStateTypeId, alias,
-          containerType, name);
-}
-
-void pajeDefineEventTypeWithColor(const char *alias, const char *containerType,
-                         const char *name, const char *color)
-{
-  fprintf(tracing_file, "%d %s %s %s %s\n", pajeDefineEventTypeWithColorId, alias,
-          containerType, name, color);
-}
 
-void pajeDefineEventType(const char *alias, const char *containerType,
-                         const char *name)
-{
-  fprintf(tracing_file, "%d %s %s %s\n", pajeDefineEventTypeId, alias,
-          containerType, name);
-}
+  //register all kinds by name
+  if (xbt_dict_get_or_null(allContainers, new->name) != NULL){
+    THROWF(tracing_error, 1, "container %s already present in allContainers data structure", new->name);
+  }
+  xbt_dict_set (allContainers, new->name, new, NULL);
 
-void pajeDefineLinkType(const char *alias, const char *containerType,
-                        const char *sourceContainerType,
-                        const char *destContainerType, const char *name)
-{
-  fprintf(tracing_file, "%d %s %s %s %s %s\n", pajeDefineLinkTypeId, alias,
-          containerType, sourceContainerType, destContainerType, name);
+  //register NODE types for triva configuration
+  if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
+    xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), NULL);
+  }
+  return new;
 }
 
-void pajeCreateContainer(double time, const char *alias, const char *type,
-                         const char *container, const char *name)
+container_t PJ_container_get (const char *name)
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeCreateContainer(line, TRACE_LINE_SIZE, pajeCreateContainerId, time,
-                        alias, type, container, name);
-  fprintf(tracing_file, "%s\n", line);
+  container_t ret = PJ_container_get_or_null (name);
+  if (ret == NULL){
+    THROWF(tracing_error, 1, "container with name %s not found", name);
+  }
+  return ret;
 }
 
-void pajeDestroyContainer(double time, const char *type,
-                          const char *container)
+container_t PJ_container_get_or_null (const char *name)
 {
-  if (time == 0){
-    fprintf(tracing_file, "%d 0 %s %s\n", pajeDestroyContainerId,
-          type, container);
-  }else{
-    fprintf(tracing_file, "%d %lf %s %s\n", pajeDestroyContainerId, time,
-          type, container);
+  if (name == NULL) return NULL;
+  container_t ret = xbt_dict_get_or_null (allContainers, name);
+  if (ret == NULL){
+    return NULL;
   }
+  return ret;
 }
 
-void pajeSetState(double time, const char *entityType,
-                  const char *container, const char *value)
+container_t PJ_container_get_root ()
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeSetState(line, TRACE_LINE_SIZE, pajeSetStateId, time, entityType,
-                 container, value);
-  fprintf(tracing_file, "%s\n", line);
+  return rootContainer;
 }
 
-void pajePushState(double time, const char *entityType,
-                   const char *container, const char *value)
+void PJ_container_remove_from_parent (container_t child)
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeSetState(line, TRACE_LINE_SIZE, pajePushStateId, time, entityType,
-                 container, value);
-  fprintf(tracing_file, "%s\n", line);
-}
+  if (child == NULL){
+    THROWF (tracing_error, 0, "can't remove from parent with a NULL child");
+  }
 
-void pajePopState(double time, const char *entityType,
-                  const char *container)
-{
-  fprintf(tracing_file, "%d %lf %s %s\n", pajePopStateId, time, entityType,
-          container);
+  container_t parent = child->father;
+  if (parent){
+    XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
+        child->name,
+        parent->name);
+    xbt_dict_remove (parent->children, child->name);
+  }
 }
 
-void pajeStartLink(double time, const char *entityType,
-                   const char *container, const char *value,
-                   const char *sourceContainer, const char *key)
+void PJ_container_free (container_t container)
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeStartLink(line, TRACE_LINE_SIZE, pajeStartLinkId, time, entityType,
-                  container, value, sourceContainer, key);
-  fprintf(tracing_file, "%s\n", line);
-}
+  if (container == NULL){
+    THROWF (tracing_error, 0, "trying to free a NULL container");
+  }
+  XBT_DEBUG("destroy container %s", container->name);
 
-void pajeStartLinkWithVolume(double time, const char *entityType,
-                             const char *container, const char *value,
-                             const char *sourceContainer, const char *key,
-                             double volume)
-{
-  char line[TRACE_LINE_SIZE];
-  __pajeStartLink(line, TRACE_LINE_SIZE, pajeStartLinkWithVolumeId, time,
-                  entityType, container, value, sourceContainer, key);
-  fprintf(tracing_file, "%s %f\n", line, volume);
-}
+  //obligation to dump previous events because they might
+  //reference the container that is about to be destroyed
+  TRACE_last_timestamp_to_dump = surf_get_clock();
+  TRACE_paje_dump_buffer(1);
 
-void pajeEndLink(double time, const char *entityType,
-                 const char *container, const char *value,
-                 const char *destContainer, const char *key)
-{
-  if (time == 0){
-    fprintf(tracing_file, "%d 0 %s %s %s %s %s\n", pajeEndLinkId,
-            entityType, container, value, destContainer, key);
-  }else {
-    fprintf(tracing_file, "%d %lf %s %s %s %s %s\n", pajeEndLinkId, time,
-          entityType, container, value, destContainer, key);
+  //trace my destruction
+  if (!TRACE_disable_destroy()){
+    //do not trace the container destruction if user requests
+    new_pajeDestroyContainer(container);
   }
-}
 
-void pajeDefineVariableType(const char *alias, const char *containerType,
-                            const char *name)
-{
-  fprintf(tracing_file, "%d %s %s %s\n", pajeDefineVariableTypeId, alias,
-          containerType, name);
-}
+  //remove it from allContainers data structure
+  xbt_dict_remove (allContainers, container->name);
 
-void pajeDefineVariableTypeWithColor(const char *alias, const char *containerType,
-                            const char *name, const char *color)
-{
-  fprintf(tracing_file, "%d %s %s %s \"%s\"\n", pajeDefineVariableTypeWithColorId, alias,
-          containerType, name, color);
-}
-
-void pajeSetVariable(double time, const char *entityType,
-                     const char *container, const char *value)
-{
-  char line[TRACE_LINE_SIZE];
-  __pajeSetVariable(line, TRACE_LINE_SIZE, pajeSetVariableId, time,
-                    entityType, container, value);
-  fprintf(tracing_file, "%s\n", line);
+  //free
+  xbt_free (container->name);
+  xbt_free (container->id);
+  xbt_dict_free (&container->children);
+  xbt_free (container);
+  container = NULL;
 }
 
-void pajeAddVariable(double time, const char *entityType,
-                     const char *container, const char *value)
+static void recursiveDestroyContainer (container_t container)
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeSetVariable(line, TRACE_LINE_SIZE, pajeAddVariableId, time,
-                    entityType, container, value);
-  fprintf(tracing_file, "%s\n", line);
+  if (container == NULL){
+    THROWF (tracing_error, 0, "trying to recursively destroy a NULL container");
+  }
+  XBT_DEBUG("recursiveDestroyContainer %s", container->name);
+  xbt_dict_cursor_t cursor = NULL;
+  container_t child;
+  char *child_name;
+  xbt_dict_foreach(container->children, cursor, child_name, child) {
+    recursiveDestroyContainer (child);
+  }
+  PJ_container_free (container);
 }
 
-void pajeSubVariable(double time, const char *entityType,
-                     const char *container, const char *value)
+void PJ_container_free_all ()
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeSetVariable(line, TRACE_LINE_SIZE, pajeSubVariableId, time,
-                    entityType, container, value);
-  fprintf(tracing_file, "%s\n", line);
-}
+  container_t root = PJ_container_get_root();
+  if (root == NULL){
+    THROWF (tracing_error, 0, "trying to free all containers, but root is NULL");
+  }
+  recursiveDestroyContainer (root);
+  rootContainer = NULL;
 
-void pajeNewEvent(double time, const char *entityType,
-                  const char *container, const char *value)
-{
-  if (time == 0){
-    fprintf(tracing_file, "%d 0 %s %s %s\n", pajeNewEventId,
-          entityType, container, value);
-  }else{
-    fprintf(tracing_file, "%d %lf %s %s %s\n", pajeNewEventId, time,
-          entityType, container, value);
+  //checks
+  if (xbt_dict_length(allContainers) != 0){
+    THROWF(tracing_error, 0, "some containers still present even after destroying all of them");
   }
 }