Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] dump *before* destroy container, correct trace file
[simgrid.git] / src / instr / instr_paje.c
index de8945d..330eb55 100644 (file)
 
 #ifdef HAVE_TRACING
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje, instr, "Paje tracing event system");
-
-static FILE *tracing_file = NULL;
-
-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;
-#define UNUSED001 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)
-{
-  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);
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje, instr, "Paje tracing event system (data structures)");
 
-  /* output header */
-  TRACE_paje_create_header();
-}
+static type_t rootType = NULL;              /* the root type */
+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 */
 
-void TRACE_paje_end(void)
+void instr_paje_init (container_t root)
 {
-  fclose(tracing_file);
-  char *filename = TRACE_get_filename();
-  DEBUG1("Filename %s is closed", filename);
+  allContainers = xbt_dict_new ();
+  trivaNodeTypes = xbt_dict_new ();
+  trivaEdgeTypes = xbt_dict_new ();
+  rootContainer = root;
 }
 
-void TRACE_paje_create_header(void)
+static long long int newTypeId ()
 {
-  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\
-%%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, pajeDefineEventTypeId, pajeDefineLinkTypeId, pajeCreateContainerId, pajeDestroyContainerId, pajeSetStateId, pajePopStateId, pajeStartLinkId, pajeEndLinkId, pajePushStateId, pajeSetVariableId, pajeAddVariableId, pajeSubVariableId, pajeDefineVariableTypeId, pajeDefineVariableTypeWithColorId, pajeStartLinkWithVolumeId, pajeNewEventId);
+  static long long int counter = 0;
+  return counter++;
 }
 
-/* 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)
+static type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
 {
-  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);
+  type_t ret = xbt_new0(s_type_t, 1);
+  ret->name = xbt_strdup (typename);
+  ret->father = father;
+  ret->kind = kind;
+  ret->children = xbt_dict_new ();
+  ret->color = xbt_strdup (color);
+
+  long long int id = newTypeId();
+  char str_id[INSTR_DEFAULT_STR_SIZE];
+  snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", id);
+  ret->id = xbt_strdup (str_id);
+
+  if (father != NULL){
+    xbt_dict_set (father->children, key, ret, NULL);
+    DEBUG2("new type %s, child of %s", typename, father->name);
   }
+  return ret;
 }
 
-static void __pajeSetState(char *output, int len, int eventid, double time,
-                           const char *entityType, const char *container,
-                           const char *value)
+type_t getRootType ()
 {
-  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);
-  }
+  return rootType;
 }
 
-static void __pajeSetVariable(char *output, int len, int eventid,
-                              double time, const char *entityType,
-                              const char *container, const char *value)
+type_t getContainerType (const char *typename, type_t father)
 {
-  if (time == 0){
-    snprintf(output, len, "%d 0 %s %s %s", eventid, entityType,
-           container, value);
+  type_t ret;
+  if (father == NULL){
+    ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
+    if (father) new_pajeDefineContainerType (ret);
+    rootType = ret;
   }else{
-    snprintf(output, len, "%d %lf %s %s %s", eventid, time, entityType,
-           container, value);
+    //check if my father type already has my typename
+    ret = (type_t)xbt_dict_get_or_null (father->children, typename);
+    if (ret == NULL){
+      ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
+      new_pajeDefineContainerType (ret);
+    }
   }
+  return ret;
 }
 
-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)
+type_t getEventType (const char *typename, const char *color, type_t father)
 {
-  if (time == 0){
-    snprintf(output, len, "%d 0 %s %s %s %s %s", eventid, entityType,
-           container, value, sourceContainer, key);
-  }else{
-    snprintf(output, len, "%d %lf %s %s %s %s %s", eventid, time, entityType,
-           container, value, sourceContainer, key);
+  type_t ret = xbt_dict_get_or_null (father->children, typename);
+  if (ret == NULL){
+    char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
+    if (!color){
+      ret = newType (typename, typename, white, TYPE_EVENT, father);
+    }else{
+      ret = newType (typename, typename, color, TYPE_EVENT, father);
+    }
+    DEBUG4("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
+    new_pajeDefineEventType(ret);
   }
+  return ret;
 }
 
-/* internal do the instrumentation module */
-void pajeDefineContainerType(const char *alias, const char *containerType,
-                             const char *name)
+type_t getVariableType (const char *typename, const char *color, type_t father)
 {
-  fprintf(tracing_file, "%d %s %s %s\n", pajeDefineContainerTypeId, alias,
-          containerType, name);
+  type_t ret = xbt_dict_get_or_null (father->children, typename);
+  if (ret == NULL){
+    char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
+    if (!color){
+      ret = newType (typename, typename, white, TYPE_VARIABLE, father);
+    }else{
+      ret = newType (typename, typename, color, TYPE_VARIABLE, father);
+    }
+    DEBUG4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
+    new_pajeDefineVariableType (ret);
+  }
+  return ret;
 }
 
-void pajeDefineStateType(const char *alias, const char *containerType,
-                         const char *name)
+char *getVariableTypeIdByName (const char *name, type_t father)
 {
-  fprintf(tracing_file, "%d %s %s %s\n", pajeDefineStateTypeId, alias,
-          containerType, name);
+  xbt_dict_cursor_t cursor = NULL;
+  type_t type;
+  char *key;
+  xbt_dict_foreach(father->children, cursor, key, type) {
+    if (strcmp (name, type->name) == 0) return type->id;
+  }
+  return NULL;
 }
 
-void pajeDefineEventType(const char *alias, const char *containerType,
-                         const char *name)
+type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
 {
-  fprintf(tracing_file, "%d %s %s %s\n", pajeDefineEventTypeId, alias,
-          containerType, name);
+  char key[INSTR_DEFAULT_STR_SIZE];
+  snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
+  type_t ret = xbt_dict_get_or_null (father->children, key);
+  if (ret == NULL){
+    ret = newType (typename, key, NULL, TYPE_LINK, father);
+    DEBUG8("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->name, ret->id, father->name, father->id, source->name, source->id, dest->name, dest->id);
+    new_pajeDefineLinkType(ret, source, dest);
+  }
+  return ret;
 }
 
-void pajeDefineLinkType(const char *alias, const char *containerType,
-                        const char *sourceContainerType,
-                        const char *destContainerType, const char *name)
+type_t getStateType (const char *typename, type_t father)
 {
-  fprintf(tracing_file, "%d %s %s %s %s %s\n", pajeDefineLinkTypeId, alias,
-          containerType, sourceContainerType, destContainerType, name);
+  type_t ret = xbt_dict_get_or_null (father->children, typename);
+  if (ret == NULL){
+    ret = newType (typename, typename, NULL, TYPE_STATE, father);
+    DEBUG4("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
+    new_pajeDefineStateType(ret);
+  }
+  return ret;
 }
 
-void pajeCreateContainer(double time, const char *alias, const char *type,
-                         const char *container, const char *name)
+
+static long long int newContainedId ()
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeCreateContainer(line, TRACE_LINE_SIZE, pajeCreateContainerId, time,
-                        alias, type, container, name);
-  fprintf(tracing_file, "%s\n", line);
+  static long long counter = 0;
+  return counter++;
 }
 
-void pajeDestroyContainer(double time, const char *type,
-                          const char *container)
+container_t newContainer (const char *name, e_container_types kind, container_t father)
 {
-  if (time == 0){
-    fprintf(tracing_file, "%d 0 %s %s\n", pajeDestroyContainerId,
-          type, container);
+  long long int counter = newContainedId();
+  char id_str[INSTR_DEFAULT_STR_SIZE];
+  snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
+
+  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;
+    DEBUG2("new container %s, child of %s", name, father->name);
   }else{
-    fprintf(tracing_file, "%d %lf %s %s\n", pajeDestroyContainerId, time,
-          type, container);
+    new->level = 0;
+  }
+  // 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 = getContainerType (as_typename, new->father->type);
+    }else{
+      new->type = getContainerType ("0", NULL);
+    }
+  }else{
+    //otherwise, the name is its kind
+    switch (new->kind){
+      case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
+      case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
+      case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
+      case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
+      case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
+      case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
+      default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
+    }
+  }
+  new->children = xbt_dict_new();
+  if (new->father){
+    xbt_dict_set(new->father->children, new->name, new, NULL);
+    new_pajeCreateContainer (new);
   }
+
+  //register hosts, routers, links containers
+  if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
+    xbt_dict_set (allContainers, new->name, new, NULL);
+
+    //register NODE types for triva configuration
+    xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), xbt_free);
+  }
+  return new;
 }
 
-void pajeSetState(double time, const char *entityType,
-                  const char *container, const char *value)
+static container_t recursiveGetContainer (const char *name, container_t root)
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeSetState(line, TRACE_LINE_SIZE, pajeSetStateId, time, entityType,
-                 container, value);
-  fprintf(tracing_file, "%s\n", line);
+  if (strcmp (root->name, name) == 0) return root;
+
+  xbt_dict_cursor_t cursor = NULL;
+  container_t child;
+  char *child_name;
+  xbt_dict_foreach(root->children, cursor, child_name, child) {
+    container_t ret = recursiveGetContainer(name, child);
+    if (ret) return ret;
+  }
+  return NULL;
 }
 
-void pajePushState(double time, const char *entityType,
-                   const char *container, const char *value)
+container_t getContainer (const char *name)
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeSetState(line, TRACE_LINE_SIZE, pajePushStateId, time, entityType,
-                 container, value);
-  fprintf(tracing_file, "%s\n", line);
+  return recursiveGetContainer(name, rootContainer);
 }
 
-void pajePopState(double time, const char *entityType,
-                  const char *container)
+container_t getContainerByName (const char *name)
 {
-  fprintf(tracing_file, "%d %lf %s %s\n", pajePopStateId, time, entityType,
-          container);
+  return (container_t)xbt_dict_get (allContainers, name);
 }
 
-void pajeStartLink(double time, const char *entityType,
-                   const char *container, const char *value,
-                   const char *sourceContainer, const char *key)
+char *getContainerIdByName (const char *name)
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeStartLink(line, TRACE_LINE_SIZE, pajeStartLinkId, time, entityType,
-                  container, value, sourceContainer, key);
-  fprintf(tracing_file, "%s\n", line);
+  return getContainerByName(name)->id;
 }
 
-void pajeStartLinkWithVolume(double time, const char *entityType,
-                             const char *container, const char *value,
-                             const char *sourceContainer, const char *key,
-                             double volume)
+container_t getRootContainer ()
 {
-  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);
+  return rootContainer;
 }
 
-void pajeEndLink(double time, const char *entityType,
-                 const char *container, const char *value,
-                 const char *destContainer, const char *key)
+static type_t recursiveGetType (const char *name, type_t root)
 {
-  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);
+  if (strcmp (root->name, name) == 0) return root;
+
+  xbt_dict_cursor_t cursor = NULL;
+  type_t child;
+  char *child_name;
+  xbt_dict_foreach(root->children, cursor, child_name, child) {
+    type_t ret = recursiveGetType(name, child);
+    if (ret) return ret;
   }
+  return NULL;
 }
 
-void pajeDefineVariableType(const char *alias, const char *containerType,
-                            const char *name)
+type_t getType (const char *name)
 {
-  fprintf(tracing_file, "%d %s %s %s\n", pajeDefineVariableTypeId, alias,
-          containerType, name);
+  return recursiveGetType (name, rootType);
 }
 
-void pajeDefineVariableTypeWithColor(const char *alias, const char *containerType,
-                            const char *name, const char *color)
+void destroyContainer (container_t container)
 {
-  fprintf(tracing_file, "%d %s %s %s \"%s\"\n", pajeDefineVariableTypeWithColorId, alias,
-          containerType, name, color);
-}
+  //remove me from my father
+  if (container->father){
+    xbt_dict_remove(container->father->children, container->name);
+  }
 
-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);
+  DEBUG1("destroy container %s", container->name);
+
+  //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);
+
+  //trace my destruction
+  new_pajeDestroyContainer(container);
+
+  //free
+  xbt_free (container->name);
+  xbt_free (container->id);
+  xbt_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);
+  xbt_dict_cursor_t cursor = NULL;
+  container_t child;
+  char *child_name;
+  xbt_dict_foreach(container->children, cursor, child_name, child) {
+    recursiveDestroyContainer (child);
+  }
+  destroyContainer (container);
 }
 
-void pajeSubVariable(double time, const char *entityType,
-                     const char *container, const char *value)
+static void recursiveDestroyType (type_t type)
 {
-  char line[TRACE_LINE_SIZE];
-  __pajeSetVariable(line, TRACE_LINE_SIZE, pajeSubVariableId, time,
-                    entityType, container, value);
-  fprintf(tracing_file, "%s\n", line);
+  xbt_dict_cursor_t cursor = NULL;
+  type_t child;
+  char *child_name;
+  xbt_dict_foreach(type->children, cursor, child_name, child) {
+    recursiveDestroyType (child);
+  }
+  xbt_free (type->name);
+  xbt_free (type->id);
+  xbt_free (type->children);
+  xbt_free (type);
+  type = NULL;
 }
 
-void pajeNewEvent(double time, const char *entityType,
-                  const char *container, const char *value)
+void destroyAllContainers ()
 {
-  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);
-  }
+  if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
+  if (getRootType()) recursiveDestroyType (getRootType());
+  rootContainer = NULL;
+  rootType = NULL;
 }
 
+
 #endif /* HAVE_TRACING */