Logo AND Algorithmique Numérique Distribuée

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