Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
55063c8633906135225b3307d717b837a16b0ec9
[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 (A_surfxml_AS_id, 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     return;
186   }
187   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
188   container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
189
190   //push
191   xbt_dynar_push (currentContainer, &new);
192 }
193
194 static void instr_routing_parse_end_AS ()
195 {
196   xbt_dynar_pop_ptr (currentContainer);
197 }
198
199 static void instr_routing_parse_start_link ()
200 {
201   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
202   const char *link_id = A_surfxml_link_id;
203
204   double bandwidth_value = atof(A_surfxml_link_bandwidth);
205   double latency_value = atof(A_surfxml_link_latency);
206   xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
207
208   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
209     char *up = bprintf("%s_UP", link_id);
210     char *down = bprintf("%s_DOWN", link_id);
211     xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
212     xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
213     free (up);
214     free (down);
215   }else{
216     xbt_dynar_push_as (links_to_create, char*, strdup(link_id));
217   }
218
219   char *link_name = NULL;
220   unsigned int i;
221   xbt_dynar_foreach (links_to_create, i, link_name){
222
223     container_t new = newContainer (link_name, INSTR_LINK, father);
224
225     type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
226     type_t latency = getVariableType ("latency", NULL, new->type);
227     new_pajeSetVariable (0, new, bandwidth, bandwidth_value);
228     new_pajeSetVariable (0, new, latency, latency_value);
229     if (TRACE_uncategorized()){
230       getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
231     }
232   }
233
234   xbt_dynar_free (&links_to_create);
235 }
236
237 static void instr_routing_parse_end_link ()
238 {
239 }
240
241 static void instr_routing_parse_start_host ()
242 {
243   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
244   container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
245
246   type_t power = getVariableType ("power", NULL, new->type);
247   new_pajeSetVariable (0, new, power, atof(A_surfxml_host_power));
248   if (TRACE_uncategorized()){
249     getVariableType ("power_used", "0.5 0.5 0.5", new->type);
250   }
251
252   if (TRACE_smpi_is_enabled()) {
253     if (TRACE_smpi_is_grouped()){
254       type_t mpi = getContainerType("MPI", new->type);
255       getStateType ("MPI_STATE", mpi);
256       getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
257     }
258   }
259
260   if (TRACE_msg_process_is_enabled()) {
261     type_t msg_process = getContainerType("MSG_PROCESS", new->type);
262     type_t state = getStateType ("MSG_PROCESS_STATE", msg_process);
263     getValue ("executing", "0 1 0", state);
264     getValue ("suspend", "1 0 1", state);
265     getValue ("sleep", "1 1 0", state);
266     getValue ("receive", "1 0 0", state);
267     getValue ("send", "0 0 1", state);
268     getValue ("task_execute", "0 1 1", state);
269     getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
270     getLinkType ("MSG_PROCESS_TASK_LINK", getRootType(), msg_process, msg_process);
271   }
272
273   if (TRACE_msg_task_is_enabled()) {
274     type_t msg_task = getContainerType ("MSG_TASK", new->type);
275     type_t state = getStateType ("MSG_TASK_STATE", msg_task);
276     getValue ("MSG_task_execute", "0 1 0", state);
277     getValue ("created", "1 1 0", state);
278     getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
279   }
280 }
281
282 static void instr_routing_parse_end_host ()
283 {
284 }
285
286 static void instr_routing_parse_start_router ()
287 {
288   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
289   newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
290 }
291
292 static void instr_routing_parse_end_router ()
293 {
294 }
295
296 static void instr_routing_parse_end_platform ()
297 {
298   xbt_dynar_free(&currentContainer);
299   currentContainer = NULL;
300   xbt_dict_t filter = xbt_dict_new ();
301   recursiveGraphExtraction (getRootContainer(), filter);
302   xbt_dict_free(&filter);
303   platform_created = 1;
304   TRACE_paje_dump_buffer(1);
305 }
306
307 void instr_routing_define_callbacks ()
308 {
309   if (!TRACE_is_active())
310     return;
311   surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
312   surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
313   surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
314   surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
315   surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
316   surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
317   surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
318   surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
319   surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
320 }
321
322 /*
323  * user categories support
324  */
325 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
326 {
327   if (!strcmp (root->name, "HOST")){
328     char tnstr[INSTR_DEFAULT_STR_SIZE];
329     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
330     getVariableType(tnstr, color, root);
331   }
332   if (!strcmp (root->name, "LINK")){
333     char tnstr[INSTR_DEFAULT_STR_SIZE];
334     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
335     getVariableType(tnstr, color, root);
336   }
337   xbt_dict_cursor_t cursor = NULL;
338   type_t child_type;
339   char *name;
340   xbt_dict_foreach(root->children, cursor, name, child_type) {
341     recursiveNewUserVariableType (new_typename, color, child_type);
342   }
343 }
344
345 void instr_new_user_variable_type (const char *new_typename, const char *color)
346 {
347   recursiveNewUserVariableType (new_typename, color, getRootType());
348 }
349
350 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
351 {
352   if (!strcmp (root->name, "LINK")){
353     getVariableType(new_typename, color, root);
354   }
355   xbt_dict_cursor_t cursor = NULL;
356   type_t child_type;
357   char *name;
358   xbt_dict_foreach(root->children, cursor, name, child_type) {
359     recursiveNewUserLinkVariableType (new_typename, color, child_type);
360   }
361 }
362
363 void instr_new_user_link_variable_type  (const char *new_typename, const char *color)
364 {
365   recursiveNewUserLinkVariableType (new_typename, color, getRootType());
366 }
367
368
369 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
370 {
371   if (!strcmp (root->name, "HOST")){
372     getVariableType(new_typename, color, root);
373   }
374   xbt_dict_cursor_t cursor = NULL;
375   type_t child_type;
376   char *name;
377   xbt_dict_foreach(root->children, cursor, name, child_type) {
378     recursiveNewUserHostVariableType (new_typename, color, child_type);
379   }
380 }
381
382 void instr_new_user_host_variable_type  (const char *new_typename, const char *color)
383 {
384   recursiveNewUserHostVariableType (new_typename, color, getRootType());
385 }
386
387 int instr_platform_traced ()
388 {
389   return platform_created;
390 }
391
392 #endif /* HAVE_TRACING */
393