1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "instr/instr_private.h"
10 #include "surf/surf_private.h"
11 #include "surf/network_private.h"
12 #include "xbt/graph.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
16 extern xbt_dict_t defined_types; /* from instr_interface.c */
18 static int platform_created = 0; /* indicate whether the platform file has been traced */
19 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
21 static container_t findChild (container_t root, container_t a1)
23 if (root == a1) return root;
25 xbt_dict_cursor_t cursor = NULL;
28 xbt_dict_foreach(root->children, cursor, child_name, child) {
29 if (findChild (child, a1)) return child;
34 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
36 if (a1->father == a2->father) return a1->father;
38 xbt_dict_cursor_t cursor = NULL;
41 container_t a1_try = NULL;
42 container_t a2_try = NULL;
43 xbt_dict_foreach(root->children, cursor, child_name, child) {
44 a1_try = findChild (child, a1);
45 a2_try = findChild (child, a2);
46 if (a1_try && a2_try) return child;
51 static void linkContainers (container_t father, container_t src, container_t dst, xbt_dict_t filter)
54 if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0)
58 //check if we already register this pair (we only need one direction)
59 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
60 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", src->name, dst->name);
61 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", dst->name, src->name);
62 if (xbt_dict_get_or_null (filter, aux1)) return;
63 if (xbt_dict_get_or_null (filter, aux2)) return;
65 //ok, not found, register it
66 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
67 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
71 char link_typename[INSTR_DEFAULT_STR_SIZE];
72 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", src->type->name, dst->type->name);
73 type_t link_type = getLinkType (link_typename, father->type, src->type, dst->type);
75 //register EDGE types for triva configuration
76 xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), xbt_free);
79 static long long counter = 0;
80 char key[INSTR_DEFAULT_STR_SIZE];
81 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
82 new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "G", key);
83 new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "G", key);
86 static void recursiveGraphExtraction (routing_component_t rc, container_t container, xbt_dict_t filter)
88 if (xbt_dict_length (rc->routing_sons)){
89 xbt_dict_cursor_t cursor = NULL;
90 routing_component_t rc_son;
93 xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
94 container_t child_container = xbt_dict_get (container->children, rc_son->name);
95 recursiveGraphExtraction (rc_son, child_container, filter);
100 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
101 container_t child1, child2;
102 const char *child1_name, *child2_name;
103 xbt_dict_foreach(container->children, cursor1, child1_name, child1) {
104 if (child1->kind == INSTR_LINK) continue;
105 xbt_dict_foreach(container->children, cursor2, child2_name, child2) {
106 if (child2->kind == INSTR_LINK) continue;
108 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
109 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER) &&
110 strcmp (child1_name, child2_name) != 0){
112 xbt_dynar_t route = global_routing->get_route (child1_name, child2_name);
115 container_t previous = child1;
116 xbt_dynar_foreach (route, cpt, link) {
117 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
118 container_t current = getContainerByName(link_name);
119 linkContainers(container, previous, current, filter);
122 linkContainers(container, previous, child2, filter);
124 }else if (child1->kind == INSTR_AS &&
125 child2->kind == INSTR_AS &&
126 strcmp(child1_name, child2_name) != 0){
128 route_extended_t route = rc->get_route (rc, child1_name, child2_name);
131 container_t previous = getContainerByName(route->src_gateway);
132 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
133 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
134 container_t current = getContainerByName(link_name);
135 linkContainers (container, previous, current, filter);
138 container_t last = getContainerByName(route->dst_gateway);
139 linkContainers (container, previous, last, filter);
148 static void instr_routing_parse_start_AS ()
150 if (getRootContainer() == NULL){
151 container_t root = newContainer (A_surfxml_AS_id, INSTR_AS, NULL);
152 instr_paje_init (root);
154 currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
155 xbt_dynar_push (currentContainer, &root);
157 if (TRACE_smpi_is_enabled()) {
158 if (!TRACE_smpi_is_grouped()){
159 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
160 type_t mpi = getContainerType("MPI", father->type);
161 getStateType ("MPI_STATE", mpi);
162 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
168 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
169 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
172 xbt_dynar_push (currentContainer, &new);
175 static void instr_routing_parse_end_AS ()
177 xbt_dynar_pop_ptr (currentContainer);
180 static void instr_routing_parse_start_link ()
182 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
183 const char *link_id = A_surfxml_link_id;
185 double bandwidth_value = atof(A_surfxml_link_bandwidth);
186 double latency_value = atof(A_surfxml_link_latency);
187 xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
189 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
190 char *up = bprintf("%s_UP", link_id);
191 char *down = bprintf("%s_DOWN", link_id);
192 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
193 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
197 xbt_dynar_push_as (links_to_create, char*, strdup(link_id));
200 char *link_name = NULL;
202 xbt_dynar_foreach (links_to_create, i, link_name){
204 container_t new = newContainer (link_name, INSTR_LINK, father);
206 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
207 type_t latency = getVariableType ("latency", NULL, new->type);
208 new_pajeSetVariable (0, new, bandwidth, bandwidth_value);
209 new_pajeSetVariable (0, new, latency, latency_value);
210 if (TRACE_uncategorized()){
211 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
215 xbt_dynar_free (&links_to_create);
218 static void instr_routing_parse_end_link ()
222 static void instr_routing_parse_start_host ()
224 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
225 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
227 type_t power = getVariableType ("power", NULL, new->type);
228 new_pajeSetVariable (0, new, power, atof(A_surfxml_host_power));
229 if (TRACE_uncategorized()){
230 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
233 if (TRACE_smpi_is_enabled()) {
234 if (TRACE_smpi_is_grouped()){
235 type_t mpi = getContainerType("MPI", new->type);
236 getStateType ("MPI_STATE", mpi);
237 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
241 if (TRACE_msg_process_is_enabled()) {
242 type_t msg_process = getContainerType("MSG_PROCESS", new->type);
243 type_t state = getStateType ("MSG_PROCESS_STATE", msg_process);
244 getValue ("executing", "0 1 0", state);
245 getValue ("suspend", "1 0 1", state);
246 getValue ("sleep", "1 1 0", state);
247 getValue ("receive", "1 0 0", state);
248 getValue ("send", "0 0 1", state);
249 getValue ("task_execute", "0 1 1", state);
250 getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
251 getLinkType ("MSG_PROCESS_TASK_LINK", getRootType(), msg_process, msg_process);
254 if (TRACE_msg_task_is_enabled()) {
255 type_t msg_task = getContainerType ("MSG_TASK", new->type);
256 type_t state = getStateType ("MSG_TASK_STATE", msg_task);
257 getValue ("MSG_task_execute", "0 1 0", state);
258 getValue ("created", "1 1 0", state);
259 getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
263 static void instr_routing_parse_end_host ()
267 static void instr_routing_parse_start_router ()
269 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
270 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
273 static void instr_routing_parse_end_router ()
277 static void instr_routing_parse_end_platform ()
279 xbt_dynar_free(¤tContainer);
280 currentContainer = NULL;
281 xbt_dict_t filter = xbt_dict_new ();
282 recursiveGraphExtraction (global_routing->root, getRootContainer(), filter);
283 xbt_dict_free(&filter);
284 platform_created = 1;
285 TRACE_paje_dump_buffer(1);
288 void instr_routing_define_callbacks ()
290 if (!TRACE_is_active())
292 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
293 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
294 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
295 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
296 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
297 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
298 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
299 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
300 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
304 * user categories support
306 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
308 if (!strcmp (root->name, "HOST")){
309 char tnstr[INSTR_DEFAULT_STR_SIZE];
310 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
311 getVariableType(tnstr, color, root);
313 if (!strcmp (root->name, "LINK")){
314 char tnstr[INSTR_DEFAULT_STR_SIZE];
315 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
316 getVariableType(tnstr, color, root);
318 xbt_dict_cursor_t cursor = NULL;
321 xbt_dict_foreach(root->children, cursor, name, child_type) {
322 recursiveNewUserVariableType (new_typename, color, child_type);
326 void instr_new_user_variable_type (const char *new_typename, const char *color)
328 recursiveNewUserVariableType (new_typename, color, getRootType());
331 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
333 if (!strcmp (root->name, "LINK")){
334 getVariableType(new_typename, color, root);
336 xbt_dict_cursor_t cursor = NULL;
339 xbt_dict_foreach(root->children, cursor, name, child_type) {
340 recursiveNewUserLinkVariableType (new_typename, color, child_type);
344 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
346 recursiveNewUserLinkVariableType (new_typename, color, getRootType());
350 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
352 if (!strcmp (root->name, "HOST")){
353 getVariableType(new_typename, color, root);
355 xbt_dict_cursor_t cursor = NULL;
358 xbt_dict_foreach(root->children, cursor, name, child_type) {
359 recursiveNewUserHostVariableType (new_typename, color, child_type);
363 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
365 recursiveNewUserHostVariableType (new_typename, color, getRootType());
368 int instr_platform_traced ()
370 return platform_created;
373 #define GRAPHICATOR_SUPPORT_FUNCTIONS
376 static xbt_node_t new_xbt_graph_node (xbt_graph_t graph, const char *name, xbt_dict_t nodes)
378 xbt_node_t ret = xbt_dict_get_or_null (nodes, name);
381 ret = xbt_graph_new_node (graph, xbt_strdup(name));
382 xbt_dict_set (nodes, name, ret, NULL);
386 static xbt_edge_t new_xbt_graph_edge (xbt_graph_t graph, xbt_node_t s, xbt_node_t d, xbt_dict_t edges)
391 const char *sn = TRACE_node_name (s);
392 const char *dn = TRACE_node_name (d);
394 name = bprintf ("%s%s", sn, dn);
395 ret = xbt_dict_get_or_null (edges, name);
398 name = bprintf ("%s%s", dn, sn);
399 ret = xbt_dict_get_or_null (edges, name);
402 ret = xbt_graph_new_edge(graph, s, d, NULL);
403 xbt_dict_set (edges, name, ret, NULL);
407 static void recursiveXBTGraphExtraction (xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
408 routing_component_t rc, container_t container)
410 if (xbt_dict_length (rc->routing_sons)){
411 xbt_dict_cursor_t cursor = NULL;
412 routing_component_t rc_son;
414 //bottom-up recursion
415 xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
416 container_t child_container = xbt_dict_get (container->children, rc_son->name);
417 recursiveXBTGraphExtraction (graph, nodes, edges, rc_son, child_container);
422 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
423 container_t child1, child2;
424 const char *child1_name, *child2_name;
425 xbt_dict_foreach(container->children, cursor1, child1_name, child1) {
426 if (child1->kind == INSTR_LINK) continue;
427 xbt_dict_foreach(container->children, cursor2, child2_name, child2) {
428 if (child2->kind == INSTR_LINK) continue;
430 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
431 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER) &&
432 strcmp (child1_name, child2_name) != 0){
434 xbt_dynar_t route = global_routing->get_route (child1_name, child2_name);
437 xbt_node_t current, previous = new_xbt_graph_node(graph, child1_name, nodes);
438 xbt_dynar_foreach (route, cpt, link) {
439 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
440 current = new_xbt_graph_node(graph, link_name, nodes);
441 new_xbt_graph_edge (graph, previous, current, edges);
442 //previous -> current
445 current = new_xbt_graph_node(graph, child2_name, nodes);
446 new_xbt_graph_edge (graph, previous, current, edges);
448 }else if (child1->kind == INSTR_AS &&
449 child2->kind == INSTR_AS &&
450 strcmp(child1_name, child2_name) != 0){
452 route_extended_t route = rc->get_route (rc, child1_name, child2_name);
455 xbt_node_t current, previous = new_xbt_graph_node(graph, route->src_gateway, nodes);
456 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
457 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
458 current = new_xbt_graph_node(graph, link_name, nodes);
459 //previous -> current
462 current = new_xbt_graph_node(graph, route->dst_gateway, nodes);
463 new_xbt_graph_edge (graph, previous, current, edges);
470 xbt_graph_t instr_routing_platform_graph (void)
472 xbt_graph_t ret = xbt_graph_new_graph (0, NULL);
473 xbt_dict_t nodes = xbt_dict_new ();
474 xbt_dict_t edges = xbt_dict_new ();
475 recursiveXBTGraphExtraction (ret, nodes, edges, global_routing->root, getRootContainer());
479 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
481 unsigned int cursor = 0;
482 xbt_node_t node = NULL;
483 xbt_edge_t edge = NULL;
486 file = fopen(filename, "w");
487 xbt_assert(file, "Failed to open %s \n", filename);
490 fprintf(file, "digraph test {\n");
492 fprintf(file, "graph test {\n");
494 fprintf(file, " graph [overlap=scale]\n");
496 fprintf(file, " node [shape=box, style=filled]\n");
498 " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
500 xbt_dynar_foreach(g->nodes, cursor, node) {
501 fprintf(file, " \"%s\";\n", TRACE_node_name(node));
503 xbt_dynar_foreach(g->edges, cursor, edge) {
504 const char *src_s = TRACE_node_name (edge->src);
505 const char *dst_s = TRACE_node_name (edge->dst);
507 fprintf(file, " \"%s\" -> \"%s\";\n", src_s, dst_s);
509 fprintf(file, " \"%s\" -- \"%s\";\n", src_s, dst_s);
511 fprintf(file, "}\n");
516 #endif /* HAVE_TRACING */