Logo AND Algorithmique Numérique Distribuée

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