Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics
[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 type_t rootType = NULL;              /* the root type */
14 static container_t rootContainer = NULL;    /* the root container */
15 static xbt_dict_t allContainers = NULL;     /* all created containers indexed by name */
16 xbt_dict_t trivaNodeTypes = NULL;     /* all link types defined */
17 xbt_dict_t trivaEdgeTypes = NULL;     /* all host types defined */
18
19 void instr_paje_init (container_t root)
20 {
21   allContainers = xbt_dict_new ();
22   trivaNodeTypes = xbt_dict_new ();
23   trivaEdgeTypes = xbt_dict_new ();
24   rootContainer = root;
25 }
26
27 static long long int newTypeId ()
28 {
29   static long long int counter = 0;
30   return counter++;
31 }
32
33 static type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
34 {
35   type_t ret = xbt_new0(s_type_t, 1);
36   ret->name = xbt_strdup (typename);
37   ret->father = father;
38   ret->kind = kind;
39   ret->children = xbt_dict_new ();
40   ret->color = xbt_strdup (color);
41
42   long long int id = newTypeId();
43   char str_id[INSTR_DEFAULT_STR_SIZE];
44   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", id);
45   ret->id = xbt_strdup (str_id);
46
47   if (father != NULL){
48     xbt_dict_set (father->children, key, ret, NULL);
49     DEBUG2("new type %s, child of %s", typename, father->name);
50   }
51   return ret;
52 }
53
54 type_t getRootType ()
55 {
56   return rootType;
57 }
58
59 type_t getContainerType (const char *typename, type_t father)
60 {
61   type_t ret;
62   if (father == NULL){
63     ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
64     if (father) new_pajeDefineContainerType (ret);
65     rootType = ret;
66   }else{
67     //check if my father type already has my typename
68     ret = (type_t)xbt_dict_get_or_null (father->children, typename);
69     if (ret == NULL){
70       ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
71       new_pajeDefineContainerType (ret);
72     }
73   }
74   return ret;
75 }
76
77 type_t getEventType (const char *typename, const char *color, type_t father)
78 {
79   type_t ret = xbt_dict_get_or_null (father->children, typename);
80   if (ret == NULL){
81     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
82     if (!color){
83       ret = newType (typename, typename, white, TYPE_EVENT, father);
84     }else{
85       ret = newType (typename, typename, color, TYPE_EVENT, father);
86     }
87     DEBUG4("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
88     new_pajeDefineEventType(ret);
89   }
90   return ret;
91 }
92
93 type_t getVariableType (const char *typename, const char *color, type_t father)
94 {
95   type_t ret = xbt_dict_get_or_null (father->children, typename);
96   if (ret == NULL){
97     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
98     if (!color){
99       ret = newType (typename, typename, white, TYPE_VARIABLE, father);
100     }else{
101       ret = newType (typename, typename, color, TYPE_VARIABLE, father);
102     }
103     DEBUG4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
104     new_pajeDefineVariableType (ret);
105   }
106   return ret;
107 }
108
109 char *getVariableTypeIdByName (const char *name, type_t father)
110 {
111   xbt_dict_cursor_t cursor = NULL;
112   type_t type;
113   char *key;
114   xbt_dict_foreach(father->children, cursor, key, type) {
115     if (strcmp (name, type->name) == 0) return type->id;
116   }
117   return NULL;
118 }
119
120 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
121 {
122   char key[INSTR_DEFAULT_STR_SIZE];
123   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
124   type_t ret = xbt_dict_get_or_null (father->children, key);
125   if (ret == NULL){
126     ret = newType (typename, key, NULL, TYPE_LINK, father);
127     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);
128     new_pajeDefineLinkType(ret, source, dest);
129   }
130   return ret;
131 }
132
133 type_t getStateType (const char *typename, type_t father)
134 {
135   type_t ret = xbt_dict_get_or_null (father->children, typename);
136   if (ret == NULL){
137     ret = newType (typename, typename, NULL, TYPE_STATE, father);
138     DEBUG4("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
139     new_pajeDefineStateType(ret);
140   }
141   return ret;
142 }
143
144
145 static long long int newContainedId ()
146 {
147   static long long counter = 0;
148   return counter++;
149 }
150
151 container_t newContainer (const char *name, e_container_types kind, container_t father)
152 {
153   long long int counter = newContainedId();
154   char id_str[INSTR_DEFAULT_STR_SIZE];
155   snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
156
157   container_t new = xbt_new0(s_container_t, 1);
158   new->name = xbt_strdup (name); // name of the container
159   new->id = xbt_strdup (id_str); // id (or alias) of the container
160   new->father = father;
161   // level depends on level of father
162   if (new->father){
163     new->level = new->father->level+1;
164     DEBUG2("new container %s, child of %s", name, father->name);
165   }else{
166     new->level = 0;
167   }
168   // type definition (method depends on kind of this new container)
169   new->kind = kind;
170   if (new->kind == INSTR_AS){
171     //if this container is of an AS, its type name depends on its level
172     char as_typename[INSTR_DEFAULT_STR_SIZE];
173     snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
174     if (new->father){
175       new->type = getContainerType (as_typename, new->father->type);
176     }else{
177       new->type = getContainerType ("0", NULL);
178     }
179   }else{
180     //otherwise, the name is its kind
181     switch (new->kind){
182       case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
183       case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
184       case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
185       case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
186       case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
187       case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
188       default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
189     }
190   }
191   new->children = xbt_dict_new();
192   if (new->father){
193     xbt_dict_set(new->father->children, new->name, new, NULL);
194     new_pajeCreateContainer (new);
195   }
196
197   //register hosts, routers, links containers
198   if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
199     xbt_dict_set (allContainers, new->name, new, NULL);
200
201     //register NODE types for triva configuration
202     xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), xbt_free);
203   }
204   return new;
205 }
206
207 static container_t recursiveGetContainer (const char *name, container_t root)
208 {
209   if (strcmp (root->name, name) == 0) return root;
210
211   xbt_dict_cursor_t cursor = NULL;
212   container_t child;
213   char *child_name;
214   xbt_dict_foreach(root->children, cursor, child_name, child) {
215     container_t ret = recursiveGetContainer(name, child);
216     if (ret) return ret;
217   }
218   return NULL;
219 }
220
221 container_t getContainer (const char *name)
222 {
223   return recursiveGetContainer(name, rootContainer);
224 }
225
226 container_t getContainerByName (const char *name)
227 {
228   return (container_t)xbt_dict_get (allContainers, name);
229 }
230
231 char *getContainerIdByName (const char *name)
232 {
233   return getContainerByName(name)->id;
234 }
235
236 container_t getRootContainer ()
237 {
238   return rootContainer;
239 }
240
241 static type_t recursiveGetType (const char *name, type_t root)
242 {
243   if (strcmp (root->name, name) == 0) return root;
244
245   xbt_dict_cursor_t cursor = NULL;
246   type_t child;
247   char *child_name;
248   xbt_dict_foreach(root->children, cursor, child_name, child) {
249     type_t ret = recursiveGetType(name, child);
250     if (ret) return ret;
251   }
252   return NULL;
253 }
254
255 type_t getType (const char *name)
256 {
257   return recursiveGetType (name, rootType);
258 }
259
260 void destroyContainer (container_t container)
261 {
262   //remove me from my father
263   if (container->father){
264     xbt_dict_remove(container->father->children, container->name);
265   }
266
267   DEBUG1("destroy container %s", container->name);
268
269   //obligation to dump previous events because they might
270   //reference the container that is about to be destroyed
271   TRACE_last_timestamp_to_dump = surf_get_clock();
272   TRACE_paje_dump_buffer(1);
273
274   //trace my destruction
275   new_pajeDestroyContainer(container);
276
277   //free
278   xbt_free (container->name);
279   xbt_free (container->id);
280   xbt_free (container->children);
281   xbt_free (container);
282   container = NULL;
283 }
284
285 static void recursiveDestroyContainer (container_t container)
286 {
287   xbt_dict_cursor_t cursor = NULL;
288   container_t child;
289   char *child_name;
290   xbt_dict_foreach(container->children, cursor, child_name, child) {
291     recursiveDestroyContainer (child);
292   }
293   destroyContainer (container);
294 }
295
296 static void recursiveDestroyType (type_t type)
297 {
298   xbt_dict_cursor_t cursor = NULL;
299   type_t child;
300   char *child_name;
301   xbt_dict_foreach(type->children, cursor, child_name, child) {
302     recursiveDestroyType (child);
303   }
304   xbt_free (type->name);
305   xbt_free (type->id);
306   xbt_free (type->children);
307   xbt_free (type);
308   type = NULL;
309 }
310
311 void destroyAllContainers ()
312 {
313   if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
314   if (getRootType()) recursiveDestroyType (getRootType());
315   rootContainer = NULL;
316   rootType = NULL;
317 }
318
319
320 #endif /* HAVE_TRACING */