Logo AND Algorithmique Numérique Distribuée

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