Logo AND Algorithmique Numérique Distribuée

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