Logo AND Algorithmique Numérique Distribuée

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