Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dee02034c363bf18d62df0f3eaf4db4c2694d065
[simgrid.git] / src / instr / instr_paje.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "instr/instr_private.h"
8
9 #ifdef HAVE_TRACING
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje, instr, "Paje tracing event system (data structures)");
12
13 static container_t rootContainer = NULL;    /* the root container */
14 static xbt_dict_t allContainers = NULL;     /* all created containers indexed by name */
15 xbt_dict_t trivaNodeTypes = NULL;     /* all link types defined */
16 xbt_dict_t trivaEdgeTypes = NULL;     /* all host types defined */
17
18 long long int instr_new_paje_id (void)
19 {
20   static long long int type_id = 0;
21   return type_id++;
22 }
23
24 void PJ_container_alloc (void)
25 {
26   allContainers = xbt_dict_new_homogeneous(NULL);
27   trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free);
28   trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free);
29 }
30
31 void PJ_container_release (void)
32 {
33   xbt_dict_free (&allContainers);
34   xbt_dict_free (&trivaNodeTypes);
35   xbt_dict_free (&trivaEdgeTypes);
36 }
37
38 void PJ_container_set_root (container_t root)
39 {
40   rootContainer = root;
41 }
42
43 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
44 {
45   if (name == NULL){
46     THROWF (tracing_error, 0, "can't create a container with a NULL name");
47   }
48
49   static long long int container_id = 0;
50   char id_str[INSTR_DEFAULT_STR_SIZE];
51   snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
52
53   container_t new = xbt_new0(s_container_t, 1);
54   new->name = xbt_strdup (name); // name of the container
55   new->id = xbt_strdup (id_str); // id (or alias) of the container
56   new->father = father;
57   // level depends on level of father
58   if (new->father){
59     new->level = new->father->level+1;
60     XBT_DEBUG("new container %s, child of %s", name, father->name);
61   }else{
62     new->level = 0;
63   }
64   // type definition (method depends on kind of this new container)
65   new->kind = kind;
66   if (new->kind == INSTR_AS){
67     //if this container is of an AS, its type name depends on its level
68     char as_typename[INSTR_DEFAULT_STR_SIZE];
69     snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
70     if (new->father){
71       new->type = PJ_type_get (as_typename, new->father->type);
72       if (new->type == NULL){
73         new->type = PJ_type_container_new (as_typename, new->father->type);
74       }
75     }else{
76       new->type = PJ_type_container_new ("0", NULL);
77     }
78   }else{
79     //otherwise, the name is its kind
80     char typename[INSTR_DEFAULT_STR_SIZE];
81     switch (new->kind){
82       case INSTR_HOST:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "HOST");        break;
83       case INSTR_LINK:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "LINK");        break;
84       case INSTR_ROUTER:      snprintf (typename, INSTR_DEFAULT_STR_SIZE, "ROUTER");      break;
85       case INSTR_SMPI:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MPI");         break;
86       case INSTR_MSG_PROCESS: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS"); break;
87       case INSTR_MSG_TASK:    snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");    break;
88       default: THROWF (tracing_error, 0, "new container kind is unknown."); break;
89     }
90     type_t type = PJ_type_get (typename, new->father->type);
91     if (type == NULL){
92       new->type = PJ_type_container_new (typename, new->father->type);
93     }else{
94       new->type = type;
95     }
96   }
97   new->children = xbt_dict_new_homogeneous(NULL);
98   if (new->father){
99     xbt_dict_set(new->father->children, new->name, new, NULL);
100     new_pajeCreateContainer (new);
101   }
102
103   //register all kinds by name
104   if (xbt_dict_get_or_null(allContainers, new->name) != NULL){
105     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", new->name);
106   }
107   xbt_dict_set (allContainers, new->name, new, NULL);
108
109   //register NODE types for triva configuration
110   if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
111     xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), NULL);
112   }
113   return new;
114 }
115
116 container_t PJ_container_get (const char *name)
117 {
118   container_t ret = PJ_container_get_or_null (name);
119   if (ret == NULL){
120     THROWF(tracing_error, 1, "container with name %s not found", name);
121   }
122   return ret;
123 }
124
125 container_t PJ_container_get_or_null (const char *name)
126 {
127   if (name == NULL) return NULL;
128   container_t ret = xbt_dict_get_or_null (allContainers, name);
129   if (ret == NULL){
130     return NULL;
131   }
132   return ret;
133 }
134
135 container_t PJ_container_get_root ()
136 {
137   return rootContainer;
138 }
139
140 void PJ_container_remove_from_parent (container_t child)
141 {
142   if (child == NULL){
143     THROWF (tracing_error, 0, "can't remove from parent with a NULL child");
144   }
145
146   container_t parent = child->father;
147   if (parent){
148     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
149         child->name,
150         parent->name);
151     xbt_dict_remove (parent->children, child->name);
152   }
153 }
154
155 void PJ_container_free (container_t container)
156 {
157   if (container == NULL){
158     THROWF (tracing_error, 0, "trying to free a NULL container");
159   }
160   XBT_DEBUG("destroy container %s", container->name);
161
162   //obligation to dump previous events because they might
163   //reference the container that is about to be destroyed
164   TRACE_last_timestamp_to_dump = surf_get_clock();
165   TRACE_paje_dump_buffer(1);
166
167   //trace my destruction
168   if (!TRACE_disable_destroy()){
169     //do not trace the container destruction if user requests
170     new_pajeDestroyContainer(container);
171   }
172
173   //remove it from allContainers data structure
174   xbt_dict_remove (allContainers, container->name);
175
176   //free
177   xbt_free (container->name);
178   xbt_free (container->id);
179   xbt_dict_free (&container->children);
180   xbt_free (container);
181   container = NULL;
182 }
183
184 static void recursiveDestroyContainer (container_t container)
185 {
186   if (container == NULL){
187     THROWF (tracing_error, 0, "trying to recursively destroy a NULL container");
188   }
189   XBT_DEBUG("recursiveDestroyContainer %s", container->name);
190   xbt_dict_cursor_t cursor = NULL;
191   container_t child;
192   char *child_name;
193   xbt_dict_foreach(container->children, cursor, child_name, child) {
194     recursiveDestroyContainer (child);
195   }
196   PJ_container_free (container);
197 }
198
199 void PJ_container_free_all ()
200 {
201   container_t root = PJ_container_get_root();
202   if (root == NULL){
203     THROWF (tracing_error, 0, "trying to free all containers, but root is NULL");
204   }
205   recursiveDestroyContainer (root);
206   rootContainer = NULL;
207
208   //checks
209   if (xbt_dict_length(allContainers) != 0){
210     THROWF(tracing_error, 0, "some containers still present even after destroying all of them");
211   }
212 }
213
214 #endif /* HAVE_TRACING */