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"
11 #include "surf/surf_private.h"
12 #include "surf/network_private.h"
13 #include "xbt/graph.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
17 extern xbt_dict_t defined_types; /* from instr_interface.c */
19 static int platform_created = 0; /* indicate whether the platform file has been traced */
20 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
22 static const char *instr_node_name (xbt_node_t node)
24 void *data = xbt_graph_node_get_data(node);
25 char *str = (char*)data;
30 static container_t lowestCommonAncestor (container_t a1, container_t a2)
32 //this is only an optimization (since most of a1 and a2 share the same parent)
33 if (a1->father == a2->father) return a1->father;
35 //create an array with all ancestors of a1
36 xbt_dynar_t ancestors_a1 = xbt_dynar_new(sizeof(container_t), NULL);
40 xbt_dynar_push_as (ancestors_a1, container_t, p);
44 //create an array with all ancestors of a2
45 xbt_dynar_t ancestors_a2 = xbt_dynar_new(sizeof(container_t), NULL);
48 xbt_dynar_push_as (ancestors_a2, container_t, p);
52 //find the lowest ancestor
54 int i = xbt_dynar_length (ancestors_a1) - 1;
55 int j = xbt_dynar_length (ancestors_a2) - 1;
56 while (i >= 0 && j >= 0){
57 container_t a1p = *(container_t*)xbt_dynar_get_ptr (ancestors_a1, i);
58 container_t a2p = *(container_t*)xbt_dynar_get_ptr (ancestors_a2, j);
67 xbt_dynar_free (&ancestors_a1);
68 xbt_dynar_free (&ancestors_a2);
72 static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
75 if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0){
76 XBT_DEBUG (" linkContainers: ignoring loopback link");
81 container_t father = lowestCommonAncestor (src, dst);
83 xbt_die ("common father unknown, this is a tracing problem");
87 //check if we already register this pair (we only need one direction)
88 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
89 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", src->name, dst->name);
90 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", dst->name, src->name);
91 if (xbt_dict_get_or_null (filter, aux1)){
92 XBT_DEBUG (" linkContainers: already registered %s <-> %s (1)", src->name, dst->name);
95 if (xbt_dict_get_or_null (filter, aux2)){
96 XBT_DEBUG (" linkContainers: already registered %s <-> %s (2)", dst->name, src->name);
100 //ok, not found, register it
101 xbt_dict_set (filter, aux1, xbt_strdup ("1"), NULL);
102 xbt_dict_set (filter, aux2, xbt_strdup ("1"), NULL);
106 char link_typename[INSTR_DEFAULT_STR_SIZE];
107 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s%s-%s%s",
109 src->type->name, src->type->id,
110 dst->type->name, dst->type->id);
111 type_t link_type = PJ_type_get_or_null (link_typename, father->type);
112 if (link_type == NULL){
113 link_type = PJ_type_link_new (link_typename, father->type, src->type, dst->type);
116 //register EDGE types for triva configuration
117 xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), NULL);
120 static long long counter = 0;
123 MC_ignore_data_bss(&counter, sizeof(counter));
125 char key[INSTR_DEFAULT_STR_SIZE];
126 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
127 new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "topology", key);
128 new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "topology", key);
130 XBT_DEBUG (" linkContainers %s <-> %s", src->name, dst->name);
133 static int graph_extraction_filter_out (container_t c1, container_t c2)
135 if (c1->kind == INSTR_LINK ||
136 c1->kind == INSTR_SMPI ||
137 c1->kind == INSTR_MSG_PROCESS ||
138 c1->kind == INSTR_MSG_TASK ||
139 (c2 && strcmp (c1->name, c2->name) == 0))
145 static void recursiveGraphExtraction (AS_t rc, container_t container, xbt_dict_t filter)
147 if (!TRACE_platform_topology()){
148 XBT_DEBUG("Graph extracing disable by user.");
151 XBT_DEBUG ("Graph extraction for routing_component = %s", rc->name);
152 if (!xbt_dict_is_empty(rc->routing_sons)){
153 xbt_dict_cursor_t cursor = NULL;
156 //bottom-up recursion
157 xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
158 container_t child_container = xbt_dict_get (container->children, rc_son->name);
159 recursiveGraphExtraction (rc_son, child_container, filter);
164 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
165 container_t child1, child2;
166 const char *child1_name, *child2_name;
167 xbt_dict_foreach(container->children, cursor1, child1_name, child1) {
168 if (graph_extraction_filter_out (child1, NULL)) continue;
169 xbt_dict_foreach(container->children, cursor2, child2_name, child2) {
170 if (graph_extraction_filter_out (child2, child1)) continue;
171 XBT_DEBUG ("get_route from %s to %s", child1_name, child2_name);
174 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t,1);
175 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t),NULL);
176 rc->get_route_and_latency(rc, child1->net_elm, child2->net_elm,
179 //user might want to extract a graph using routes with only one link
180 //see --cfg=tracing/onelink_only:1 or --help-tracing for details
181 if (TRACE_onelink_only() && xbt_dynar_length (route->link_list) > 1){
182 generic_free_route(route);
186 //traverse the route connecting the containers
189 container_t current, previous;
191 previous = PJ_container_get(route->gw_src->name);
196 xbt_dynar_foreach (route->link_list, cpt, link) {
197 //FIXME (TODO): Should have a cleaner way to get the link name
198 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
199 current = PJ_container_get(link_name);
200 linkContainers(previous, current, filter);
204 current = PJ_container_get(route->gw_dst->name);
208 linkContainers(previous, current, filter);
209 generic_free_route(route);
217 static void instr_routing_parse_start_AS (sg_platf_AS_cbarg_t AS)
219 const char*id = AS->id;
221 if (PJ_container_get_root() == NULL){
222 PJ_container_alloc ();
224 container_t root = PJ_container_new (id, INSTR_AS, NULL);
225 PJ_container_set_root (root);
227 if (TRACE_smpi_is_enabled()) {
228 if (!TRACE_smpi_is_grouped()){
229 type_t mpi = PJ_type_get_or_null ("MPI", root->type);
231 mpi = PJ_type_container_new("MPI", root->type);
232 PJ_type_state_new ("MPI_STATE", mpi);
233 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
238 if (TRACE_needs_platform()){
239 currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
240 xbt_dynar_push (currentContainer, &root);
245 if (TRACE_needs_platform()){
246 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
247 container_t new = PJ_container_new (id, INSTR_AS, father);
248 xbt_dynar_push (currentContainer, &new);
252 static void instr_routing_parse_end_AS ()
254 if (TRACE_needs_platform()){
255 xbt_dynar_pop_ptr (currentContainer);
259 static void instr_routing_parse_start_link (sg_platf_link_cbarg_t link)
261 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
263 double bandwidth_value = link->bandwidth;
264 double latency_value = link->latency;
265 xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
267 if (link->policy == SURF_LINK_FULLDUPLEX){
268 char *up = bprintf("%s_UP", link->id);
269 char *down = bprintf("%s_DOWN", link->id);
270 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
271 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
275 xbt_dynar_push_as (links_to_create, char*, strdup(link->id));
278 char *link_name = NULL;
280 xbt_dynar_foreach (links_to_create, i, link_name){
282 container_t new = PJ_container_new (link_name, INSTR_LINK, father);
284 if (TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()){
285 type_t bandwidth = PJ_type_get_or_null ("bandwidth", new->type);
286 if (bandwidth == NULL){
287 bandwidth = PJ_type_variable_new ("bandwidth", NULL, new->type);
289 type_t latency = PJ_type_get_or_null ("latency", new->type);
290 if (latency == NULL){
291 latency = PJ_type_variable_new ("latency", NULL, new->type);
293 new_pajeSetVariable (0, new, bandwidth, bandwidth_value);
294 new_pajeSetVariable (0, new, latency, latency_value);
296 if (TRACE_uncategorized()){
297 type_t bandwidth_used = PJ_type_get_or_null ("bandwidth_used", new->type);
298 if (bandwidth_used == NULL){
299 bandwidth_used = PJ_type_variable_new ("bandwidth_used", "0.5 0.5 0.5", new->type);
304 xbt_dynar_free (&links_to_create);
307 static void instr_routing_parse_start_host (sg_platf_host_cbarg_t host)
309 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
310 container_t new = PJ_container_new (host->id, INSTR_HOST, father);
312 if (TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) {
313 type_t power = PJ_type_get_or_null ("power", new->type);
315 power = PJ_type_variable_new ("power", NULL, new->type);
317 new_pajeSetVariable (0, new, power, host->power_peak);
319 if (TRACE_uncategorized()){
320 type_t power_used = PJ_type_get_or_null ("power_used", new->type);
321 if (power_used == NULL){
322 power_used = PJ_type_variable_new ("power_used", "0.5 0.5 0.5", new->type);
326 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
327 type_t mpi = PJ_type_get_or_null ("MPI", new->type);
329 mpi = PJ_type_container_new("MPI", new->type);
330 PJ_type_state_new ("MPI_STATE", mpi);
331 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
335 if (TRACE_msg_process_is_enabled()) {
336 type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", new->type);
337 if (msg_process == NULL){
338 msg_process = PJ_type_container_new("MSG_PROCESS", new->type);
339 type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
340 PJ_value_new ("suspend", "1 0 1", state);
341 PJ_value_new ("sleep", "1 1 0", state);
342 PJ_value_new ("receive", "1 0 0", state);
343 PJ_value_new ("send", "0 0 1", state);
344 PJ_value_new ("task_execute", "0 1 1", state);
345 PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
346 PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
351 static void instr_routing_parse_start_router (sg_platf_router_cbarg_t router)
353 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
354 PJ_container_new (router->id, INSTR_ROUTER, father);
357 static void instr_routing_parse_end_platform ()
359 xbt_dynar_free(¤tContainer);
360 currentContainer = NULL;
361 xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free);
362 XBT_DEBUG ("Starting graph extraction.");
363 recursiveGraphExtraction (routing_platf->root, PJ_container_get_root(), filter);
364 XBT_DEBUG ("Graph extraction finished.");
365 xbt_dict_free(&filter);
366 platform_created = 1;
367 TRACE_paje_dump_buffer(1);
370 void instr_routing_define_callbacks ()
372 if (!TRACE_is_enabled()) return;
373 //always need the call backs to ASes (we need only the root AS),
374 //to create the rootContainer and the rootType properly
375 sg_platf_AS_begin_add_cb(instr_routing_parse_start_AS);
376 sg_platf_AS_end_add_cb(instr_routing_parse_end_AS);
377 if (!TRACE_needs_platform()) return;
378 sg_platf_link_add_cb(instr_routing_parse_start_link);
379 sg_platf_host_add_cb(instr_routing_parse_start_host);
380 sg_platf_router_add_cb(instr_routing_parse_start_router);
382 sg_platf_postparse_add_cb(instr_routing_parse_end_platform);
386 * user categories support
388 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
390 if (!strcmp (root->name, "HOST")){
391 char tnstr[INSTR_DEFAULT_STR_SIZE];
392 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
393 PJ_type_variable_new (tnstr, color, root);
395 if (!strcmp (root->name, "LINK")){
396 char tnstr[INSTR_DEFAULT_STR_SIZE];
397 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
398 PJ_type_variable_new (tnstr, color, root);
400 xbt_dict_cursor_t cursor = NULL;
403 xbt_dict_foreach(root->children, cursor, name, child_type) {
404 recursiveNewVariableType (new_typename, color, child_type);
408 void instr_new_variable_type (const char *new_typename, const char *color)
410 recursiveNewVariableType (new_typename, color, PJ_type_get_root());
413 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
415 if (!strcmp (root->name, father_type)){
416 PJ_type_variable_new (new_typename, color, root);
418 xbt_dict_cursor_t cursor = NULL;
421 xbt_dict_foreach(root->children, cursor, name, child_type) {
422 recursiveNewUserVariableType (father_type, new_typename, color, child_type);
426 void instr_new_user_variable_type (const char *father_type, const char *new_typename, const char *color)
428 recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
431 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
433 if (!strcmp (root->name, father_type)){
434 PJ_type_state_new (new_typename, root);
436 xbt_dict_cursor_t cursor = NULL;
439 xbt_dict_foreach(root->children, cursor, name, child_type) {
440 recursiveNewUserStateType (father_type, new_typename, child_type);
444 void instr_new_user_state_type (const char *father_type, const char *new_typename)
446 recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
449 static void recursiveNewValueForUserStateType (const char *typename, const char *value, const char *color, type_t root)
451 if (!strcmp (root->name, typename)){
452 PJ_value_new (value, color, root);
454 xbt_dict_cursor_t cursor = NULL;
457 xbt_dict_foreach(root->children, cursor, name, child_type) {
458 recursiveNewValueForUserStateType (typename, value, color, child_type);
462 void instr_new_value_for_user_state_type (const char *typename, const char *value, const char *color)
464 recursiveNewValueForUserStateType (typename, value, color, PJ_type_get_root());
467 int instr_platform_traced ()
469 return platform_created;
472 #define GRAPHICATOR_SUPPORT_FUNCTIONS
475 static xbt_node_t new_xbt_graph_node (xbt_graph_t graph, const char *name, xbt_dict_t nodes)
477 xbt_node_t ret = xbt_dict_get_or_null (nodes, name);
480 ret = xbt_graph_new_node (graph, xbt_strdup(name));
481 xbt_dict_set (nodes, name, ret, NULL);
485 static xbt_edge_t new_xbt_graph_edge (xbt_graph_t graph, xbt_node_t s, xbt_node_t d, xbt_dict_t edges)
489 const char *sn = instr_node_name (s);
490 const char *dn = instr_node_name (d);
491 int len = strlen(sn)+strlen(dn)+1;
492 char *name = (char*)malloc(len * sizeof(char));
495 snprintf (name, len, "%s%s", sn, dn);
496 ret = xbt_dict_get_or_null (edges, name);
498 snprintf (name, len, "%s%s", dn, sn);
499 ret = xbt_dict_get_or_null (edges, name);
503 ret = xbt_graph_new_edge(graph, s, d, NULL);
504 xbt_dict_set (edges, name, ret, NULL);
510 static void recursiveXBTGraphExtraction (xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
511 AS_t rc, container_t container)
513 if (!xbt_dict_is_empty(rc->routing_sons)){
514 xbt_dict_cursor_t cursor = NULL;
517 //bottom-up recursion
518 xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
519 container_t child_container = xbt_dict_get (container->children, rc_son->name);
520 recursiveXBTGraphExtraction (graph, nodes, edges, rc_son, child_container);
525 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
526 container_t child1, child2;
527 const char *child1_name, *child2_name;
528 xbt_dict_foreach(container->children, cursor1, child1_name, child1) {
529 //if child1 is not a link, a smpi node, a msg process or a msg task
530 if (child1->kind == INSTR_LINK || child1->kind == INSTR_SMPI || child1->kind == INSTR_MSG_PROCESS || child1->kind == INSTR_MSG_TASK) continue;
532 xbt_dict_foreach(container->children, cursor2, child2_name, child2) {
533 //if child2 is not a link, a smpi node, a msg process or a msg task
534 if (child2->kind == INSTR_LINK || child2->kind == INSTR_SMPI || child2->kind == INSTR_MSG_PROCESS || child2->kind == INSTR_MSG_TASK) continue;
536 //if child1 is not child2
537 if (strcmp (child1_name, child2_name) == 0) continue;
540 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t,1);
541 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t),NULL);
542 rc->get_route_and_latency(rc, child1->net_elm, child2->net_elm,
545 //user might want to extract a graph using routes with only one link
546 //see --cfg=tracing/onelink_only:1 or --help-tracing for details
547 if (TRACE_onelink_only() && xbt_dynar_length (route->link_list) > 1) continue;
549 //traverse the route connecting the containers
552 xbt_node_t current, previous;
554 previous = new_xbt_graph_node(graph, route->gw_src->name, nodes);
556 previous = new_xbt_graph_node(graph, child1_name, nodes);
559 xbt_dynar_foreach (route->link_list, cpt, link) {
560 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
561 current = new_xbt_graph_node(graph, link_name, nodes);
562 new_xbt_graph_edge (graph, previous, current, edges);
563 //previous -> current
567 current = new_xbt_graph_node(graph, route->gw_dst->name, nodes);
569 current = new_xbt_graph_node(graph, child2_name, nodes);
571 new_xbt_graph_edge (graph, previous, current, edges);
572 generic_free_route(route);
578 xbt_graph_t instr_routing_platform_graph (void)
580 xbt_graph_t ret = xbt_graph_new_graph (0, NULL);
581 xbt_dict_t nodes = xbt_dict_new_homogeneous(NULL);
582 xbt_dict_t edges = xbt_dict_new_homogeneous(NULL);
583 recursiveXBTGraphExtraction (ret, nodes, edges, routing_platf->root, PJ_container_get_root());
584 xbt_dict_free (&nodes);
585 xbt_dict_free (&edges);
589 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
591 unsigned int cursor = 0;
592 xbt_node_t node = NULL;
593 xbt_edge_t edge = NULL;
596 file = fopen(filename, "w");
597 xbt_assert(file, "Failed to open %s \n", filename);
600 fprintf(file, "digraph test {\n");
602 fprintf(file, "graph test {\n");
604 fprintf(file, " graph [overlap=scale]\n");
606 fprintf(file, " node [shape=box, style=filled]\n");
608 " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
610 xbt_dynar_foreach(g->nodes, cursor, node) {
611 fprintf(file, " \"%s\";\n", instr_node_name(node));
613 xbt_dynar_foreach(g->edges, cursor, edge) {
614 const char *src_s = instr_node_name (edge->src);
615 const char *dst_s = instr_node_name (edge->dst);
617 fprintf(file, " \"%s\" -> \"%s\";\n", src_s, dst_s);
619 fprintf(file, " \"%s\" -- \"%s\";\n", src_s, dst_s);
621 fprintf(file, "}\n");
626 #endif /* HAVE_TRACING */