Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
96098309c1c7a7a0db13e0d26dea58c05938b4fa
[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, xbt_dict_t filter)
51 {
52   //ignore loopback
53   if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
54     return;
55
56   //check if we already register this pair (we only need one direction)
57   char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
58   snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", a1, a2);
59   snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", a2, a1);
60   if (xbt_dict_get_or_null (filter, aux1)) return;
61   if (xbt_dict_get_or_null (filter, aux2)) return;
62
63   //ok, not found, register it
64   xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
65   xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
66
67   container_t a1_container = getContainerByName (a1);
68   type_t a1_type = a1_container->type;
69
70   container_t a2_container = getContainerByName (a2);
71   type_t a2_type = a2_container->type;
72
73   container_t container = findCommonFather (getRootContainer(), a1_container, a2_container);
74   xbt_assert (container != NULL, "common father not found");
75
76   //declare type
77   char link_typename[INSTR_DEFAULT_STR_SIZE];
78   snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
79   type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
80
81   //register EDGE types for triva configuration
82   xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), xbt_free);
83
84   //create the link
85   static long long counter = 0;
86   char key[INSTR_DEFAULT_STR_SIZE];
87   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
88   new_pajeStartLink(SIMIX_get_clock(), container, link_type, a1_container, "G", key);
89   new_pajeEndLink(SIMIX_get_clock(), container, link_type, a2_container, "G", key);
90 }
91
92 static void recursiveGraphExtraction (container_t container, xbt_dict_t filter)
93 {
94   if (xbt_dict_length(container->children)){
95     xbt_dict_cursor_t cursor = NULL;
96     container_t child;
97     char *child_name;
98     //bottom-up recursion
99     xbt_dict_foreach(container->children, cursor, child_name, child) {
100       recursiveGraphExtraction (child, filter);
101     }
102
103     //let's get routes
104     xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
105     container_t child1, child2;
106     const char *child_name1, *child_name2;
107
108     xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
109       xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
110         if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
111             (child2->kind == INSTR_HOST  || child2->kind == INSTR_ROUTER)){
112
113           //getting route
114           xbt_dynar_t route = NULL;
115           xbt_ex_t exception;
116           TRY {
117             route = global_routing->get_route (child_name1, child_name2);
118           }CATCH(exception) {
119             //no route between them, that's possible
120             continue;
121           }
122
123           //link the route members
124           unsigned int cpt;
125           void *link;
126           char *previous_entity_name = (char*)child_name1;
127           xbt_dynar_foreach (route, cpt, link) {
128             char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
129             linkContainers (previous_entity_name, link_name, filter);
130             previous_entity_name = link_name;
131           }
132           linkContainers (previous_entity_name, child_name2, filter);
133         }else if (child1->kind == INSTR_AS &&
134                   child2->kind == INSTR_AS &&
135                   strcmp(child_name1, child_name2) != 0){
136
137           //getting route
138           routing_component_t root = global_routing->root;
139           route_extended_t route = NULL;
140           xbt_ex_t exception;
141           TRY {
142             route = root->get_route (root, child_name1, child_name2);
143           }CATCH(exception) {
144             //no route between them, that's possible
145             continue;
146           }
147           xbt_assert(route!=NULL,
148               "there is no ASroute between %s and %s", child_name1, child_name2);
149           unsigned int cpt;
150           void *link;
151           char *previous_entity_name = route->src_gateway;
152           xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
153             char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
154             linkContainers (previous_entity_name, link_name, filter);
155             previous_entity_name = link_name;
156           }
157           linkContainers (previous_entity_name, route->dst_gateway, filter);
158         }
159       }
160     }
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   new_pajeSetVariable (0, new, bandwidth, atof(A_surfxml_link_bandwidth));
205   new_pajeSetVariable (0, new, latency, atof(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   new_pajeSetVariable (0, new, power, atof(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     type_t state = getStateType ("MSG_PROCESS_STATE", msg_process);
237     getValue ("executing", "0 1 0", state);
238     getValue ("suspend", "1 0 1", state);
239     getValue ("sleep", "1 1 0", state);
240     getValue ("receive", "1 0 0", state);
241     getValue ("send", "0 0 1", state);
242     getValue ("task_execute", "0 1 1", state);
243     getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
244     getLinkType ("MSG_PROCESS_TASK_LINK", getRootType(), msg_process, msg_process);
245   }
246
247   if (TRACE_msg_task_is_enabled()) {
248     type_t msg_task = getContainerType ("MSG_TASK", new->type);
249     type_t state = getStateType ("MSG_TASK_STATE", msg_task);
250     getValue ("MSG_task_execute", "0 1 0", state);
251     getValue ("created", "1 1 0", state);
252     getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
253   }
254 }
255
256 static void instr_routing_parse_end_host ()
257 {
258 }
259
260 static void instr_routing_parse_start_router ()
261 {
262   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
263   newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
264 }
265
266 static void instr_routing_parse_end_router ()
267 {
268 }
269
270 static void instr_routing_parse_end_platform ()
271 {
272   xbt_dynar_free(&currentContainer);
273   currentContainer = NULL;
274   xbt_dict_t filter = xbt_dict_new ();
275   recursiveGraphExtraction (getRootContainer(), filter);
276   xbt_dict_free(&filter);
277   platform_created = 1;
278   TRACE_paje_dump_buffer(1);
279 }
280
281 void instr_routing_define_callbacks ()
282 {
283   if (!TRACE_is_active())
284     return;
285   surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
286   surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
287   surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
288   surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
289   surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
290   surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
291   surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
292   surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
293   surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
294 }
295
296 /*
297  * user categories support
298  */
299 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
300 {
301   if (!strcmp (root->name, "HOST")){
302     char tnstr[INSTR_DEFAULT_STR_SIZE];
303     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
304     getVariableType(tnstr, color, root);
305   }
306   if (!strcmp (root->name, "LINK")){
307     char tnstr[INSTR_DEFAULT_STR_SIZE];
308     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
309     getVariableType(tnstr, color, root);
310   }
311   xbt_dict_cursor_t cursor = NULL;
312   type_t child_type;
313   char *name;
314   xbt_dict_foreach(root->children, cursor, name, child_type) {
315     recursiveNewUserVariableType (new_typename, color, child_type);
316   }
317 }
318
319 void instr_new_user_variable_type (const char *new_typename, const char *color)
320 {
321   recursiveNewUserVariableType (new_typename, color, getRootType());
322 }
323
324 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
325 {
326   if (!strcmp (root->name, "LINK")){
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     recursiveNewUserLinkVariableType (new_typename, color, child_type);
334   }
335 }
336
337 void instr_new_user_link_variable_type  (const char *new_typename, const char *color)
338 {
339   recursiveNewUserLinkVariableType (new_typename, color, getRootType());
340 }
341
342
343 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
344 {
345   if (!strcmp (root->name, "HOST")){
346     getVariableType(new_typename, color, root);
347   }
348   xbt_dict_cursor_t cursor = NULL;
349   type_t child_type;
350   char *name;
351   xbt_dict_foreach(root->children, cursor, name, child_type) {
352     recursiveNewUserHostVariableType (new_typename, color, child_type);
353   }
354 }
355
356 void instr_new_user_host_variable_type  (const char *new_typename, const char *color)
357 {
358   recursiveNewUserHostVariableType (new_typename, color, getRootType());
359 }
360
361 int instr_platform_traced ()
362 {
363   return platform_created;
364 }
365
366 #endif /* HAVE_TRACING */
367