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"), NULL);
67 xbt_dict_set (filter, aux2, xbt_strdup ("1"), NULL);
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 = PJ_type_get_or_null (link_typename, father->type);
74 if (link_type == NULL){
75 link_type = PJ_type_link_new (link_typename, father->type, src->type, dst->type);
78 //register EDGE types for triva configuration
79 xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), NULL);
82 static long long counter = 0;
83 char key[INSTR_DEFAULT_STR_SIZE];
84 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
85 new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "G", key);
86 new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "G", key);
89 static void recursiveGraphExtraction (AS_t rc, container_t container, xbt_dict_t filter)
91 if (!xbt_dict_is_empty(rc->routing_sons)){
92 xbt_dict_cursor_t cursor = NULL;
96 xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
97 container_t child_container = xbt_dict_get (container->children, rc_son->name);
98 recursiveGraphExtraction (rc_son, child_container, filter);
103 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
104 container_t child1, child2;
105 const char *child1_name, *child2_name;
106 xbt_dict_foreach(container->children, cursor1, child1_name, child1) {
107 if (child1->kind == INSTR_LINK) continue;
108 xbt_dict_foreach(container->children, cursor2, child2_name, child2) {
109 if (child2->kind == INSTR_LINK) continue;
111 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
112 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER) &&
113 strcmp (child1_name, child2_name) != 0){
115 xbt_dynar_t route = NULL;
119 routing_get_route_and_latency(child1_name, child2_name, &route, NULL);
123 if (route == NULL) continue;
125 if (TRACE_onelink_only()){
126 if (xbt_dynar_length (route) > 1) continue;
128 container_t previous = child1;
130 for (i = 0; i < xbt_dynar_length(route); i++){
131 link_CM02_t *link = ((link_CM02_t*)xbt_dynar_get_ptr (route, i));
132 char *link_name = (*link)->lmm_resource.generic_resource.name;
133 container_t current = PJ_container_get(link_name);
134 linkContainers(container, previous, current, filter);
137 linkContainers(container, previous, child2, filter);
139 }else if (child1->kind == INSTR_AS &&
140 child2->kind == INSTR_AS &&
141 strcmp(child1_name, child2_name) != 0){
143 route_t route = xbt_new0(s_route_t,1);
144 route->link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
145 rc->get_route_and_latency (rc, child1_name, child2_name, route,NULL);
148 container_t previous = PJ_container_get(route->src_gateway);
149 xbt_dynar_foreach (route->link_list, cpt, link) {
150 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
151 container_t current = PJ_container_get(link_name);
152 linkContainers (container, previous, current, filter);
155 container_t last = PJ_container_get(route->dst_gateway);
156 linkContainers (container, previous, last, filter);
157 generic_free_route(route);
166 static void instr_routing_parse_start_AS (const char*id,const char*routing)
168 if (PJ_container_get_root() == NULL){
169 PJ_container_alloc ();
171 container_t root = PJ_container_new (id, INSTR_AS, NULL);
172 PJ_container_set_root (root);
174 if (TRACE_smpi_is_enabled()) {
175 if (!TRACE_smpi_is_grouped()){
176 type_t mpi = PJ_type_get_or_null ("MPI", root->type);
178 mpi = PJ_type_container_new("MPI", root->type);
179 PJ_type_state_new ("MPI_STATE", mpi);
180 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
185 if (TRACE_needs_platform()){
186 currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
187 xbt_dynar_push (currentContainer, &root);
192 if (TRACE_needs_platform()){
193 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
194 container_t new = PJ_container_new (id, INSTR_AS, father);
195 xbt_dynar_push (currentContainer, &new);
199 static void instr_routing_parse_end_AS ()
201 if (TRACE_needs_platform()){
202 xbt_dynar_pop_ptr (currentContainer);
206 static void instr_routing_parse_start_link (sg_platf_link_cbarg_t link)
208 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
210 double bandwidth_value = link->bandwidth;
211 double latency_value = link->latency;
212 xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
214 if (link->policy == SURF_LINK_FULLDUPLEX){
215 char *up = bprintf("%s_UP", link->id);
216 char *down = bprintf("%s_DOWN", link->id);
217 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
218 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
222 xbt_dynar_push_as (links_to_create, char*, strdup(link->id));
225 char *link_name = NULL;
227 xbt_dynar_foreach (links_to_create, i, link_name){
229 container_t new = PJ_container_new (link_name, INSTR_LINK, father);
231 if (TRACE_categorized() || TRACE_uncategorized()){
232 type_t bandwidth = PJ_type_get_or_null ("bandwidth", new->type);
233 if (bandwidth == NULL){
234 bandwidth = PJ_type_variable_new ("bandwidth", NULL, new->type);
236 type_t latency = PJ_type_get_or_null ("latency", new->type);
237 if (latency == NULL){
238 latency = PJ_type_variable_new ("latency", NULL, new->type);
240 new_pajeSetVariable (0, new, bandwidth, bandwidth_value);
241 new_pajeSetVariable (0, new, latency, latency_value);
243 if (TRACE_uncategorized()){
244 type_t bandwidth_used = PJ_type_get_or_null ("bandwidth_used", new->type);
245 if (bandwidth_used == NULL){
246 bandwidth_used = PJ_type_variable_new ("bandwidth_used", "0.5 0.5 0.5", new->type);
251 xbt_dynar_free (&links_to_create);
254 static void instr_routing_parse_start_host (sg_platf_host_cbarg_t host)
256 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
257 container_t new = PJ_container_new (host->id, INSTR_HOST, father);
259 if (TRACE_categorized() || TRACE_uncategorized()) {
260 type_t power = PJ_type_get_or_null ("power", new->type);
262 power = PJ_type_variable_new ("power", NULL, new->type);
264 new_pajeSetVariable (0, new, power, host->power_peak);
266 if (TRACE_uncategorized()){
267 type_t power_used = PJ_type_get_or_null ("power_used", new->type);
268 if (power_used == NULL){
269 power_used = PJ_type_variable_new ("power_used", "0.5 0.5 0.5", new->type);
273 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
274 type_t mpi = PJ_type_get_or_null ("MPI", new->type);
276 mpi = PJ_type_container_new("MPI", new->type);
277 PJ_type_state_new ("MPI_STATE", mpi);
278 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
282 if (TRACE_msg_process_is_enabled()) {
283 type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", new->type);
284 if (msg_process == NULL){
285 msg_process = PJ_type_container_new("MSG_PROCESS", new->type);
286 type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
287 PJ_value_new ("executing", "0 1 0", state);
288 PJ_value_new ("suspend", "1 0 1", state);
289 PJ_value_new ("sleep", "1 1 0", state);
290 PJ_value_new ("receive", "1 0 0", state);
291 PJ_value_new ("send", "0 0 1", state);
292 PJ_value_new ("task_execute", "0 1 1", state);
293 PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
294 PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
299 static void instr_routing_parse_start_router (sg_platf_router_cbarg_t router)
301 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
302 PJ_container_new (router->id, INSTR_ROUTER, father);
305 static void instr_routing_parse_end_platform ()
307 xbt_dynar_free(¤tContainer);
308 currentContainer = NULL;
309 xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free);
310 recursiveGraphExtraction (global_routing->root, PJ_container_get_root(), filter);
311 xbt_dict_free(&filter);
312 platform_created = 1;
313 TRACE_paje_dump_buffer(1);
316 void instr_routing_define_callbacks ()
318 if (!TRACE_is_enabled()) return;
319 //always need the call backs to ASes (we need only the root AS),
320 //to create the rootContainer and the rootType properly
321 sg_platf_AS_begin_add_cb(instr_routing_parse_start_AS);
322 sg_platf_AS_end_add_cb(instr_routing_parse_end_AS);
323 if (!TRACE_needs_platform()) return;
324 sg_platf_link_add_cb(instr_routing_parse_start_link);
325 sg_platf_host_add_cb(instr_routing_parse_start_host);
326 sg_platf_router_add_cb(instr_routing_parse_start_router);
328 sg_platf_postparse_add_cb(instr_routing_parse_end_platform);
332 * user categories support
334 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
336 if (!strcmp (root->name, "HOST")){
337 char tnstr[INSTR_DEFAULT_STR_SIZE];
338 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
339 PJ_type_variable_new (tnstr, color, root);
341 if (!strcmp (root->name, "LINK")){
342 char tnstr[INSTR_DEFAULT_STR_SIZE];
343 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
344 PJ_type_variable_new (tnstr, color, root);
346 xbt_dict_cursor_t cursor = NULL;
349 xbt_dict_foreach(root->children, cursor, name, child_type) {
350 recursiveNewVariableType (new_typename, color, child_type);
354 void instr_new_variable_type (const char *new_typename, const char *color)
356 recursiveNewVariableType (new_typename, color, PJ_type_get_root());
359 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
361 if (!strcmp (root->name, father_type)){
362 PJ_type_variable_new (new_typename, color, root);
364 xbt_dict_cursor_t cursor = NULL;
367 xbt_dict_foreach(root->children, cursor, name, child_type) {
368 recursiveNewUserVariableType (father_type, new_typename, color, child_type);
372 void instr_new_user_variable_type (const char *father_type, const char *new_typename, const char *color)
374 recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
379 int instr_platform_traced ()
381 return platform_created;
384 #define GRAPHICATOR_SUPPORT_FUNCTIONS
387 static xbt_node_t new_xbt_graph_node (xbt_graph_t graph, const char *name, xbt_dict_t nodes)
389 xbt_node_t ret = xbt_dict_get_or_null (nodes, name);
392 ret = xbt_graph_new_node (graph, xbt_strdup(name));
393 xbt_dict_set (nodes, name, ret, NULL);
397 static xbt_edge_t new_xbt_graph_edge (xbt_graph_t graph, xbt_node_t s, xbt_node_t d, xbt_dict_t edges)
402 const char *sn = TRACE_node_name (s);
403 const char *dn = TRACE_node_name (d);
405 name = bprintf ("%s%s", sn, dn);
406 ret = xbt_dict_get_or_null (edges, name);
409 name = bprintf ("%s%s", dn, sn);
410 ret = xbt_dict_get_or_null (edges, name);
413 ret = xbt_graph_new_edge(graph, s, d, NULL);
414 xbt_dict_set (edges, name, ret, NULL);
418 static void recursiveXBTGraphExtraction (xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
419 AS_t rc, container_t container)
421 if (!xbt_dict_is_empty(rc->routing_sons)){
422 xbt_dict_cursor_t cursor = NULL;
425 //bottom-up recursion
426 xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
427 container_t child_container = xbt_dict_get (container->children, rc_son->name);
428 recursiveXBTGraphExtraction (graph, nodes, edges, rc_son, child_container);
433 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
434 container_t child1, child2;
435 const char *child1_name, *child2_name;
436 xbt_dict_foreach(container->children, cursor1, child1_name, child1) {
437 if (child1->kind == INSTR_LINK) continue;
438 xbt_dict_foreach(container->children, cursor2, child2_name, child2) {
439 if (child2->kind == INSTR_LINK) continue;
441 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
442 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER) &&
443 strcmp (child1_name, child2_name) != 0){
445 // FIXME factorize route creation with else branch below (once possible)
446 xbt_dynar_t route=NULL;
447 routing_get_route_and_latency (child1_name, child2_name,&route,NULL);
448 if (TRACE_onelink_only()){
449 if (xbt_dynar_length (route) > 1) continue;
453 xbt_node_t current, previous = new_xbt_graph_node(graph, child1_name, nodes);
454 xbt_dynar_foreach (route, cpt, link) {
455 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
456 current = new_xbt_graph_node(graph, link_name, nodes);
457 new_xbt_graph_edge (graph, previous, current, edges);
458 //previous -> current
461 current = new_xbt_graph_node(graph, child2_name, nodes);
462 new_xbt_graph_edge (graph, previous, current, edges);
464 }else if (child1->kind == INSTR_AS &&
465 child2->kind == INSTR_AS &&
466 strcmp(child1_name, child2_name) != 0){
468 route_t route = xbt_new0(s_route_t,1);
469 route->link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
470 rc->get_route_and_latency (rc, child1_name, child2_name,route, NULL);
473 xbt_node_t current, previous = new_xbt_graph_node(graph, route->src_gateway, nodes);
474 xbt_dynar_foreach (route->link_list, cpt, link) {
475 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
476 current = new_xbt_graph_node(graph, link_name, nodes);
477 //previous -> current
480 current = new_xbt_graph_node(graph, route->dst_gateway, nodes);
481 new_xbt_graph_edge (graph, previous, current, edges);
482 generic_free_route(route);
489 xbt_graph_t instr_routing_platform_graph (void)
491 xbt_graph_t ret = xbt_graph_new_graph (0, NULL);
492 xbt_dict_t nodes = xbt_dict_new_homogeneous(NULL);
493 xbt_dict_t edges = xbt_dict_new_homogeneous(NULL);
494 recursiveXBTGraphExtraction (ret, nodes, edges, global_routing->root, PJ_container_get_root());
498 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
500 unsigned int cursor = 0;
501 xbt_node_t node = NULL;
502 xbt_edge_t edge = NULL;
505 file = fopen(filename, "w");
506 xbt_assert(file, "Failed to open %s \n", filename);
509 fprintf(file, "digraph test {\n");
511 fprintf(file, "graph test {\n");
513 fprintf(file, " graph [overlap=scale]\n");
515 fprintf(file, " node [shape=box, style=filled]\n");
517 " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
519 xbt_dynar_foreach(g->nodes, cursor, node) {
520 fprintf(file, " \"%s\";\n", TRACE_node_name(node));
522 xbt_dynar_foreach(g->edges, cursor, edge) {
523 const char *src_s = TRACE_node_name (edge->src);
524 const char *dst_s = TRACE_node_name (edge->dst);
526 fprintf(file, " \"%s\" -> \"%s\";\n", src_s, dst_s);
528 fprintf(file, " \"%s\" -- \"%s\";\n", src_s, dst_s);
530 fprintf(file, "}\n");
535 #endif /* HAVE_TRACING */