Logo AND Algorithmique Numérique Distribuée

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