Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix compilation error
[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 (container_t father, container_t src, container_t dst, xbt_dict_t filter)
51 {
52   //ignore loopback
53   if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0)
54     return;
55
56   if (filter != NULL){
57     //check if we already register this pair (we only need one direction)
58     char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
59     snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", src->name, dst->name);
60     snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", dst->name, src->name);
61     if (xbt_dict_get_or_null (filter, aux1)) return;
62     if (xbt_dict_get_or_null (filter, aux2)) return;
63
64     //ok, not found, register it
65     xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
66     xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
67   }
68
69   //declare type
70   char link_typename[INSTR_DEFAULT_STR_SIZE];
71   snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", src->type->name, dst->type->name);
72   type_t link_type = getLinkType (link_typename, father->type, src->type, dst->type);
73
74   //register EDGE types for triva configuration
75   xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), xbt_free);
76
77   //create the link
78   static long long counter = 0;
79   char key[INSTR_DEFAULT_STR_SIZE];
80   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
81   new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "G", key);
82   new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "G", key);
83 }
84
85 static void recursiveGraphExtraction (routing_component_t rc, container_t container, xbt_dict_t filter)
86 {
87   if (xbt_dict_length (rc->routing_sons)){
88     xbt_dict_cursor_t cursor = NULL;
89     routing_component_t rc_son;
90     char *child_name;
91     //bottom-up recursion
92     xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
93       container_t child_container = xbt_dict_get (container->children, rc_son->name);
94       recursiveGraphExtraction (rc_son, child_container, filter);
95     }
96   }
97
98   //let's get routes
99   xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
100   container_t child1, child2;
101   const char *child1_name, *child2_name;
102   xbt_dict_foreach(container->children, cursor1, child1_name, child1) {
103     if (child1->kind == INSTR_LINK) continue;
104     xbt_dict_foreach(container->children, cursor2, child2_name, child2) {
105       if (child2->kind == INSTR_LINK) continue;
106
107       if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
108           (child2->kind == INSTR_HOST  || child2->kind == INSTR_ROUTER) &&
109           strcmp (child1_name, child2_name) != 0){
110
111         xbt_dynar_t route = global_routing->get_route (child1_name, child2_name);
112         unsigned int cpt;
113         void *link;
114         container_t previous = child1;
115         xbt_dynar_foreach (route, cpt, link) {
116           char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
117           container_t current = getContainerByName(link_name);
118           linkContainers(container, previous, current, filter);
119           previous = current;
120         }
121         linkContainers(container, previous, child2, filter);
122
123       }else if (child1->kind == INSTR_AS &&
124                 child2->kind == INSTR_AS &&
125                 strcmp(child1_name, child2_name) != 0){
126
127         route_extended_t route = rc->get_route (rc, child1_name, child2_name);
128         unsigned int cpt;
129         void *link;
130         container_t previous = getContainerByName(route->src_gateway);
131         xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
132           char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
133           container_t current = getContainerByName(link_name);
134           linkContainers (container, previous, current, filter);
135           previous = current;
136         }
137         container_t last = getContainerByName(route->dst_gateway);
138         linkContainers (container, previous, last, filter);
139       }
140     }
141   }
142 }
143
144 /*
145  * Callbacks
146  */
147 static void instr_routing_parse_start_AS ()
148 {
149   if (getRootContainer() == NULL){
150     container_t root = newContainer (A_surfxml_AS_id, INSTR_AS, NULL);
151     instr_paje_init (root);
152
153     currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
154     xbt_dynar_push (currentContainer, &root);
155
156     if (TRACE_smpi_is_enabled()) {
157       if (!TRACE_smpi_is_grouped()){
158         container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
159         type_t mpi = getContainerType("MPI", father->type);
160         getStateType ("MPI_STATE", mpi);
161         getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
162       }
163     }
164
165     return;
166   }
167   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
168   container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
169
170   //push
171   xbt_dynar_push (currentContainer, &new);
172 }
173
174 static void instr_routing_parse_end_AS ()
175 {
176   xbt_dynar_pop_ptr (currentContainer);
177 }
178
179 static void instr_routing_parse_start_link ()
180 {
181   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
182   const char *link_id = A_surfxml_link_id;
183
184   double bandwidth_value = atof(A_surfxml_link_bandwidth);
185   double latency_value = atof(A_surfxml_link_latency);
186   xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
187
188   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
189     char *up = bprintf("%s_UP", link_id);
190     char *down = bprintf("%s_DOWN", link_id);
191     xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
192     xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
193     free (up);
194     free (down);
195   }else{
196     xbt_dynar_push_as (links_to_create, char*, strdup(link_id));
197   }
198
199   char *link_name = NULL;
200   unsigned int i;
201   xbt_dynar_foreach (links_to_create, i, link_name){
202
203     container_t new = newContainer (link_name, 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, bandwidth_value);
208     new_pajeSetVariable (0, new, latency, latency_value);
209     if (TRACE_uncategorized()){
210       getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
211     }
212   }
213
214   xbt_dynar_free (&links_to_create);
215 }
216
217 static void instr_routing_parse_end_link ()
218 {
219 }
220
221 static void instr_routing_parse_start_host ()
222 {
223   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
224   container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
225
226   type_t power = getVariableType ("power", NULL, new->type);
227   new_pajeSetVariable (0, new, power, atof(A_surfxml_host_power));
228   if (TRACE_uncategorized()){
229     getVariableType ("power_used", "0.5 0.5 0.5", new->type);
230   }
231
232   if (TRACE_smpi_is_enabled()) {
233     if (TRACE_smpi_is_grouped()){
234       type_t mpi = getContainerType("MPI", new->type);
235       getStateType ("MPI_STATE", mpi);
236       getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
237     }
238   }
239
240   if (TRACE_msg_process_is_enabled()) {
241     type_t msg_process = getContainerType("MSG_PROCESS", new->type);
242     type_t state = getStateType ("MSG_PROCESS_STATE", msg_process);
243     getValue ("executing", "0 1 0", state);
244     getValue ("suspend", "1 0 1", state);
245     getValue ("sleep", "1 1 0", state);
246     getValue ("receive", "1 0 0", state);
247     getValue ("send", "0 0 1", state);
248     getValue ("task_execute", "0 1 1", state);
249     getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
250     getLinkType ("MSG_PROCESS_TASK_LINK", getRootType(), msg_process, msg_process);
251   }
252
253   if (TRACE_msg_task_is_enabled()) {
254     type_t msg_task = getContainerType ("MSG_TASK", new->type);
255     type_t state = getStateType ("MSG_TASK_STATE", msg_task);
256     getValue ("MSG_task_execute", "0 1 0", state);
257     getValue ("created", "1 1 0", state);
258     getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
259   }
260 }
261
262 static void instr_routing_parse_end_host ()
263 {
264 }
265
266 static void instr_routing_parse_start_router ()
267 {
268   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
269   newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
270 }
271
272 static void instr_routing_parse_end_router ()
273 {
274 }
275
276 static void instr_routing_parse_end_platform ()
277 {
278   xbt_dynar_free(&currentContainer);
279   currentContainer = NULL;
280   xbt_dict_t filter = xbt_dict_new ();
281   recursiveGraphExtraction (global_routing->root, getRootContainer(), filter);
282   xbt_dict_free(&filter);
283   platform_created = 1;
284   TRACE_paje_dump_buffer(1);
285 }
286
287 void instr_routing_define_callbacks ()
288 {
289   if (!TRACE_is_active())
290     return;
291   surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
292   surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
293   surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
294   surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
295   surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
296   surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
297   surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
298   surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
299   surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
300 }
301
302 /*
303  * user categories support
304  */
305 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
306 {
307   if (!strcmp (root->name, "HOST")){
308     char tnstr[INSTR_DEFAULT_STR_SIZE];
309     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
310     getVariableType(tnstr, color, root);
311   }
312   if (!strcmp (root->name, "LINK")){
313     char tnstr[INSTR_DEFAULT_STR_SIZE];
314     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
315     getVariableType(tnstr, color, root);
316   }
317   xbt_dict_cursor_t cursor = NULL;
318   type_t child_type;
319   char *name;
320   xbt_dict_foreach(root->children, cursor, name, child_type) {
321     recursiveNewUserVariableType (new_typename, color, child_type);
322   }
323 }
324
325 void instr_new_user_variable_type (const char *new_typename, const char *color)
326 {
327   recursiveNewUserVariableType (new_typename, color, getRootType());
328 }
329
330 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
331 {
332   if (!strcmp (root->name, "LINK")){
333     getVariableType(new_typename, color, root);
334   }
335   xbt_dict_cursor_t cursor = NULL;
336   type_t child_type;
337   char *name;
338   xbt_dict_foreach(root->children, cursor, name, child_type) {
339     recursiveNewUserLinkVariableType (new_typename, color, child_type);
340   }
341 }
342
343 void instr_new_user_link_variable_type  (const char *new_typename, const char *color)
344 {
345   recursiveNewUserLinkVariableType (new_typename, color, getRootType());
346 }
347
348
349 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
350 {
351   if (!strcmp (root->name, "HOST")){
352     getVariableType(new_typename, color, root);
353   }
354   xbt_dict_cursor_t cursor = NULL;
355   type_t child_type;
356   char *name;
357   xbt_dict_foreach(root->children, cursor, name, child_type) {
358     recursiveNewUserHostVariableType (new_typename, color, child_type);
359   }
360 }
361
362 void instr_new_user_host_variable_type  (const char *new_typename, const char *color)
363 {
364   recursiveNewUserHostVariableType (new_typename, color, getRootType());
365 }
366
367 int instr_platform_traced ()
368 {
369   return platform_created;
370 }
371
372 #endif /* HAVE_TRACING */
373