Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] a source file to keep functions to deal with data structures for paje tracing
[simgrid.git] / src / instr / instr_routing.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 #include "surf/surf_private.h"
11 #include "surf/network_private.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
14
15 extern xbt_dict_t defined_types; /* from instr_interface.c */
16
17 static int platform_created = 0;            /* indicate whether the platform file has been traced */
18 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
19
20 static void instr_routing_parse_start_AS (void);
21 static void instr_routing_parse_end_AS (void);
22 static void instr_routing_parse_start_link (void);
23 static void instr_routing_parse_end_link (void);
24 static void instr_routing_parse_start_host (void);
25 static void instr_routing_parse_end_host (void);
26 static void instr_routing_parse_start_router (void);
27 static void instr_routing_parse_end_router (void);
28 static void instr_routing_parse_end_platform (void);
29
30 void instr_routing_define_callbacks ()
31 {
32   if (!TRACE_is_active())
33     return;
34   surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
35   surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
36   surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
37   surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
38   surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
39   surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
40   surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
41   surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
42   surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
43 }
44
45 static container_t findChild (container_t root, container_t a1)
46 {
47   if (root == a1) return root;
48
49   xbt_dict_cursor_t cursor = NULL;
50   container_t child;
51   char *child_name;
52   xbt_dict_foreach(root->children, cursor, child_name, child) {
53     if (findChild (child, a1)) return child;
54   }
55   return NULL;
56 }
57
58 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
59 {
60   if (a1->father == a2->father) return a1->father;
61
62   xbt_dict_cursor_t cursor = NULL;
63   container_t child;
64   char *child_name;
65   container_t a1_try = NULL;
66   container_t a2_try = NULL;
67   xbt_dict_foreach(root->children, cursor, child_name, child) {
68     a1_try = findChild (child, a1);
69     a2_try = findChild (child, a2);
70     if (a1_try && a2_try) return child;
71   }
72   return NULL;
73 }
74
75 static void linkContainers (const char *a1, const char *a2)
76 {
77   //ignore loopback
78   if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
79     return;
80
81   container_t a1_container = getContainerByName (a1);
82   type_t a1_type = a1_container->type;
83
84   container_t a2_container = getContainerByName (a2);
85   type_t a2_type = a2_container->type;
86
87   container_t container = findCommonFather (getRootContainer(), a1_container, a2_container);
88   xbt_assert0 (container != NULL, "common father not found");
89
90   //declare type
91   char link_typename[INSTR_DEFAULT_STR_SIZE];
92   snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
93   type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
94
95   //create the link
96   static long long counter = 0;
97   char key[INSTR_DEFAULT_STR_SIZE];
98   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
99   pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
100   pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
101 }
102
103 static void recursiveGraphExtraction (container_t container)
104 {
105   if (xbt_dict_length(container->children)){
106     xbt_dict_cursor_t cursor = NULL;
107     container_t child;
108     char *child_name;
109     //bottom-up recursion
110     xbt_dict_foreach(container->children, cursor, child_name, child) {
111       recursiveGraphExtraction (child);
112     }
113
114     //let's get routes
115     xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
116     container_t child1, child2;
117     const char *child_name1, *child_name2;
118
119     xbt_dict_t filter = xbt_dict_new ();
120
121     xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
122       xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
123         //check if we already register this pair (we only need one direction)
124         char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
125         snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
126         snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
127         if (xbt_dict_get_or_null (filter, aux1)) continue;
128         if (xbt_dict_get_or_null (filter, aux2)) continue;
129
130         //ok, not found, register it
131         xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
132         xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
133
134         if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
135             (child2->kind == INSTR_HOST  || child2->kind == INSTR_ROUTER)){
136
137           //getting route
138           xbt_dynar_t route = NULL;
139           xbt_ex_t exception;
140           TRY {
141             route = global_routing->get_route (child_name1, child_name2);
142           }CATCH(exception) {
143             //no route between them, that's possible
144             continue;
145           }
146
147           //link the route members
148           unsigned int cpt;
149           void *link;
150           char *previous_entity_name = (char*)child_name1;
151           xbt_dynar_foreach (route, cpt, link) {
152             char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
153             linkContainers (previous_entity_name, link_name);
154             previous_entity_name = link_name;
155           }
156           linkContainers (previous_entity_name, child_name2);
157         }else if (child1->kind == INSTR_AS &&
158                   child2->kind == INSTR_AS &&
159                   strcmp(child_name1, child_name2) != 0){
160
161           //getting route
162           routing_component_t root = global_routing->root;
163           route_extended_t route = NULL;
164           xbt_ex_t exception;
165           TRY {
166             route = root->get_route (root, child_name1, child_name2);
167           }CATCH(exception) {
168             //no route between them, that's possible
169             continue;
170           }
171           xbt_assert2(route!=NULL,
172               "there is no ASroute between %s and %s", child_name1, child_name2);
173           unsigned int cpt;
174           void *link;
175           char *previous_entity_name = route->src_gateway;
176           xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
177             char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
178             linkContainers (previous_entity_name, link_name);
179             previous_entity_name = link_name;
180           }
181           linkContainers (previous_entity_name, route->dst_gateway);
182         }
183       }
184     }
185     xbt_dict_free(&filter);
186   }
187 }
188
189 /*
190  * Callbacks
191  */
192 static void instr_routing_parse_start_AS ()
193 {
194   if (getRootContainer() == NULL){
195     container_t root = newContainer ("0", INSTR_AS, NULL);
196     instr_paje_init (root);
197
198     currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
199     xbt_dynar_push (currentContainer, &root);
200
201     if (TRACE_smpi_is_enabled()) {
202       if (!TRACE_smpi_is_grouped()){
203         container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
204         type_t mpi = getContainerType("MPI", father->type);
205         getStateType ("MPI_STATE", mpi);
206         getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
207       }
208     }
209   }
210   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
211   container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
212
213   //push
214   xbt_dynar_push (currentContainer, &new);
215 }
216
217 static void instr_routing_parse_end_AS ()
218 {
219   xbt_dynar_pop_ptr (currentContainer);
220 }
221
222 static void instr_routing_parse_start_link ()
223 {
224   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
225   container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
226
227   type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
228   type_t latency = getVariableType ("latency", NULL, new->type);
229   pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
230   pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
231   if (TRACE_uncategorized()){
232     getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
233   }
234 }
235
236 static void instr_routing_parse_end_link ()
237 {
238 }
239
240 static void instr_routing_parse_start_host ()
241 {
242   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
243   container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
244
245   type_t power = getVariableType ("power", NULL, new->type);
246   pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
247   if (TRACE_uncategorized()){
248     getVariableType ("power_used", "0.5 0.5 0.5", new->type);
249   }
250
251   if (TRACE_smpi_is_enabled()) {
252     if (TRACE_smpi_is_grouped()){
253       type_t mpi = getContainerType("MPI", new->type);
254       getStateType ("MPI_STATE", mpi);
255       getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
256     }
257   }
258
259   if (TRACE_msg_process_is_enabled()) {
260     type_t msg_process = getContainerType("MSG_PROCESS", new->type);
261     getStateType ("MSG_PROCESS_STATE", msg_process);
262     getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
263   }
264
265   if (TRACE_msg_task_is_enabled()) {
266     type_t msg_task = getContainerType ("MSG_TASK", new->type);
267     getStateType ("MSG_TASK_STATE", msg_task);
268     getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
269   }
270 }
271
272 static void instr_routing_parse_end_host ()
273 {
274 }
275
276 static void instr_routing_parse_start_router ()
277 {
278   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
279   newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
280 }
281
282 static void instr_routing_parse_end_router ()
283 {
284 }
285
286 static void instr_routing_parse_end_platform ()
287 {
288   xbt_dynar_free(&currentContainer);
289   currentContainer = NULL;
290   recursiveGraphExtraction (getRootContainer());
291   platform_created = 1;
292 }
293
294 /*
295  * Support functions
296  */
297 int instr_link_is_traced (const char *name)
298 {
299   if (getContainerByName(name)){
300     return 1;
301   } else {
302     return 0;
303   }
304 }
305
306 char *instr_variable_type (const char *name, const char *resource)
307 {
308   container_t container = getContainerByName(resource);
309   xbt_dict_cursor_t cursor = NULL;
310   type_t type;
311   char *type_name;
312   xbt_dict_foreach(container->type->children, cursor, type_name, type) {
313     if (strcmp (name, type->name) == 0) return type->id;
314   }
315   return NULL;
316 }
317
318 char *instr_resource_type (const char *resource_name)
319 {
320   return getContainerByName(resource_name)->id;
321 }
322
323 static void recursiveDestroyContainer (container_t container)
324 {
325   xbt_dict_cursor_t cursor = NULL;
326   container_t child;
327   char *child_name;
328   xbt_dict_foreach(container->children, cursor, child_name, child) {
329     recursiveDestroyContainer (child);
330   }
331   destroyContainer (container);
332 }
333
334 static void recursiveDestroyType (type_t type)
335 {
336   xbt_dict_cursor_t cursor = NULL;
337   type_t child;
338   char *child_name;
339   xbt_dict_foreach(type->children, cursor, child_name, child) {
340     recursiveDestroyType (child);
341   }
342   xbt_free (type->name);
343   xbt_free (type->id);
344   xbt_free (type->children);
345   xbt_free (type);
346   type = NULL;
347 }
348
349 void instr_destroy_platform ()
350 {
351   if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
352   if (getRootType()) recursiveDestroyType (getRootType());
353 }
354
355 /*
356  * user categories support
357  */
358 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
359 {
360   if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
361     getVariableType(new_typename, color, root);
362   }
363   xbt_dict_cursor_t cursor = NULL;
364   type_t child_type;
365   char *name;
366   xbt_dict_foreach(root->children, cursor, name, child_type) {
367     recursiveNewUserVariableType (new_typename, color, child_type);
368   }
369 }
370
371 void instr_new_user_variable_type (const char *new_typename, const char *color)
372 {
373   recursiveNewUserVariableType (new_typename, color, getRootType());
374 }
375
376 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
377 {
378   if (!strcmp (root->name, "LINK")){
379     getVariableType(new_typename, color, root);
380   }
381   xbt_dict_cursor_t cursor = NULL;
382   type_t child_type;
383   char *name;
384   xbt_dict_foreach(root->children, cursor, name, child_type) {
385     recursiveNewUserLinkVariableType (new_typename, color, child_type);
386   }
387 }
388
389 void instr_new_user_link_variable_type  (const char *new_typename, const char *color)
390 {
391   recursiveNewUserLinkVariableType (new_typename, color, getRootType());
392 }
393
394
395 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
396 {
397   if (!strcmp (root->name, "HOST")){
398     getVariableType(new_typename, color, root);
399   }
400   xbt_dict_cursor_t cursor = NULL;
401   type_t child_type;
402   char *name;
403   xbt_dict_foreach(root->children, cursor, name, child_type) {
404     recursiveNewUserHostVariableType (new_typename, color, child_type);
405   }
406 }
407
408 void instr_new_user_host_variable_type  (const char *new_typename, const char *color)
409 {
410   recursiveNewUserHostVariableType (new_typename, color, getRootType());
411 }
412
413 int instr_platform_traced ()
414 {
415   return platform_created;
416 }
417
418 #endif /* HAVE_TRACING */
419