1 /* Copyright (c) 2010, 2012-2015. 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 "src/instr/instr_private.h"
9 #include "src/surf/surf_routing.hpp"
10 #include "src/surf/xml/platf_private.hpp"
11 #include "xbt/graph.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
15 static int platform_created = 0; /* indicate whether the platform file has been traced */
16 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
18 static const char *instr_node_name (xbt_node_t node)
20 void *data = xbt_graph_node_get_data(node);
21 char *str = (char*)data;
26 static container_t lowestCommonAncestor (container_t a1, container_t a2)
28 //this is only an optimization (since most of a1 and a2 share the same parent)
29 if (a1->father == a2->father) return a1->father;
31 //create an array with all ancestors of a1
32 xbt_dynar_t ancestors_a1 = xbt_dynar_new(sizeof(container_t), NULL);
36 xbt_dynar_push_as (ancestors_a1, container_t, p);
40 //create an array with all ancestors of a2
41 xbt_dynar_t ancestors_a2 = xbt_dynar_new(sizeof(container_t), NULL);
44 xbt_dynar_push_as (ancestors_a2, container_t, p);
48 //find the lowest ancestor
50 int i = xbt_dynar_length (ancestors_a1) - 1;
51 int j = xbt_dynar_length (ancestors_a2) - 1;
52 while (i >= 0 && j >= 0){
53 container_t a1p = *(container_t*)xbt_dynar_get_ptr (ancestors_a1, i);
54 container_t a2p = *(container_t*)xbt_dynar_get_ptr (ancestors_a2, j);
63 xbt_dynar_free (&ancestors_a1);
64 xbt_dynar_free (&ancestors_a2);
68 static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
71 if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0){
72 XBT_DEBUG (" linkContainers: ignoring loopback link");
77 container_t father = lowestCommonAncestor (src, dst);
79 xbt_die ("common father unknown, this is a tracing problem");
83 //check if we already register this pair (we only need one direction)
84 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
85 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", src->name, dst->name);
86 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", dst->name, src->name);
87 if (xbt_dict_get_or_null (filter, aux1)){
88 XBT_DEBUG (" linkContainers: already registered %s <-> %s (1)", src->name, dst->name);
91 if (xbt_dict_get_or_null (filter, aux2)){
92 XBT_DEBUG (" linkContainers: already registered %s <-> %s (2)", dst->name, src->name);
96 //ok, not found, register it
97 xbt_dict_set (filter, aux1, xbt_strdup ("1"), NULL);
98 xbt_dict_set (filter, aux2, xbt_strdup ("1"), NULL);
102 char link_typename[INSTR_DEFAULT_STR_SIZE];
103 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s%s-%s%s",
105 src->type->name, src->type->id,
106 dst->type->name, dst->type->id);
107 type_t link_type = PJ_type_get_or_null (link_typename, father->type);
108 if (link_type == NULL){
109 link_type = PJ_type_link_new (link_typename, father->type, src->type, dst->type);
112 //register EDGE types for triva configuration
113 xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), NULL);
116 static long long counter = 0;
118 char key[INSTR_DEFAULT_STR_SIZE];
119 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
120 new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "topology", key);
121 new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "topology", key);
123 XBT_DEBUG (" linkContainers %s <-> %s", src->name, dst->name);
126 static void recursiveGraphExtraction (simgrid::surf::As *rc, container_t container, xbt_dict_t filter)
128 if (!TRACE_platform_topology()){
129 XBT_DEBUG("Graph extraction disabled by user.");
132 XBT_DEBUG ("Graph extraction for routing_component = %s", rc->name_);
133 if (!xbt_dict_is_empty(rc->children_)){
134 xbt_dict_cursor_t cursor = NULL;
137 //bottom-up recursion
138 xbt_dict_foreach(surf_AS_get_children(rc), cursor, child_name, rc_son) {
139 container_t child_container = (container_t) xbt_dict_get (
140 container->children, surf_AS_get_name(rc_son));
141 recursiveGraphExtraction (rc_son, child_container, filter);
146 xbt_graph_t graph = xbt_graph_new_graph (0, NULL);
147 xbt_dict_t nodes = xbt_dict_new_homogeneous(NULL);
148 xbt_dict_t edges = xbt_dict_new_homogeneous(NULL);
149 xbt_edge_t edge = NULL;
151 xbt_dict_cursor_t cursor = NULL;
154 surf_AS_get_graph(rc, graph, nodes, edges);
155 xbt_dict_foreach(edges,cursor,edge_name,edge) {
157 PJ_container_get((const char*) edge->src->data),
158 PJ_container_get((const char*) edge->dst->data), filter);
160 xbt_dict_free (&nodes);
161 xbt_dict_free (&edges);
162 xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, NULL);
169 void sg_instr_AS_begin(sg_platf_AS_cbarg_t AS)
171 const char*id = AS->id;
173 if (PJ_container_get_root() == NULL){
174 PJ_container_alloc ();
176 container_t root = PJ_container_new (id, INSTR_AS, NULL);
177 PJ_container_set_root (root);
179 if (TRACE_smpi_is_enabled()) {
180 if (!TRACE_smpi_is_grouped()){
181 type_t mpi = PJ_type_get_or_null ("MPI", root->type);
183 mpi = PJ_type_container_new("MPI", root->type);
184 PJ_type_state_new ("MPI_STATE", mpi);
185 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
190 if (TRACE_needs_platform()){
191 currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
192 xbt_dynar_push (currentContainer, &root);
197 if (TRACE_needs_platform()){
198 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
199 container_t container = PJ_container_new (id, INSTR_AS, father);
200 xbt_dynar_push (currentContainer, &container);
204 void sg_instr_AS_end()
206 if (TRACE_needs_platform()){
207 xbt_dynar_pop_ptr (currentContainer);
211 static void instr_routing_parse_start_link (sg_platf_link_cbarg_t link)
213 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
215 double bandwidth_value = link->bandwidth;
216 double latency_value = link->latency;
217 xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
219 if (link->policy == SURF_LINK_FULLDUPLEX){
220 char *up = bprintf("%s_UP", link->id);
221 char *down = bprintf("%s_DOWN", link->id);
222 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
223 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
227 xbt_dynar_push_as (links_to_create, char*, strdup(link->id));
230 char *link_name = NULL;
232 xbt_dynar_foreach (links_to_create, i, link_name){
234 container_t container = PJ_container_new (link_name, INSTR_LINK, father);
236 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_link())) {
237 type_t bandwidth = PJ_type_get_or_null ("bandwidth", container->type);
238 if (bandwidth == NULL){
239 bandwidth = PJ_type_variable_new ("bandwidth", NULL, container->type);
241 type_t latency = PJ_type_get_or_null ("latency", container->type);
242 if (latency == NULL){
243 latency = PJ_type_variable_new ("latency", NULL, container->type);
245 new_pajeSetVariable (0, container, bandwidth, bandwidth_value);
246 new_pajeSetVariable (0, container, latency, latency_value);
248 if (TRACE_uncategorized()){
249 type_t bandwidth_used = PJ_type_get_or_null ("bandwidth_used", container->type);
250 if (bandwidth_used == NULL){
251 PJ_type_variable_new ("bandwidth_used", "0.5 0.5 0.5", container->type);
256 xbt_dynar_free (&links_to_create);
259 void sg_instr_new_host(sg_platf_host_cbarg_t host)
261 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
262 container_t container = PJ_container_new (host->id, INSTR_HOST, father);
264 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_speed())) {
265 type_t speed = PJ_type_get_or_null ("power", container->type);
267 speed = PJ_type_variable_new ("power", NULL, container->type);
270 double current_speed_state;
271 xbt_dynar_get_cpy(host->speed_peak, host->pstate, ¤t_speed_state);
272 new_pajeSetVariable (0, container, speed, current_speed_state);
274 if (TRACE_uncategorized()){
275 type_t speed_used = PJ_type_get_or_null ("power_used", container->type);
276 if (speed_used == NULL){
277 PJ_type_variable_new ("power_used", "0.5 0.5 0.5", container->type);
281 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
282 type_t mpi = PJ_type_get_or_null ("MPI", container->type);
284 mpi = PJ_type_container_new("MPI", container->type);
285 PJ_type_state_new ("MPI_STATE", mpi);
286 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
290 if (TRACE_msg_process_is_enabled()) {
291 type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", container->type);
292 if (msg_process == NULL){
293 msg_process = PJ_type_container_new("MSG_PROCESS", container->type);
294 type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
295 PJ_value_new ("suspend", "1 0 1", state);
296 PJ_value_new ("sleep", "1 1 0", state);
297 PJ_value_new ("receive", "1 0 0", state);
298 PJ_value_new ("send", "0 0 1", state);
299 PJ_value_new ("task_execute", "0 1 1", state);
300 PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
301 PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
305 if (TRACE_msg_vm_is_enabled()) {
306 type_t msg_vm = PJ_type_get_or_null ("MSG_VM", container->type);
308 msg_vm = PJ_type_container_new("MSG_VM", container->type);
309 type_t state = PJ_type_state_new ("MSG_VM_STATE", msg_vm);
310 PJ_value_new ("suspend", "1 0 1", state);
311 PJ_value_new ("sleep", "1 1 0", state);
312 PJ_value_new ("receive", "1 0 0", state);
313 PJ_value_new ("send", "0 0 1", state);
314 PJ_value_new ("task_execute", "0 1 1", state);
315 PJ_type_link_new ("MSG_VM_LINK", PJ_type_get_root(), msg_vm, msg_vm);
316 PJ_type_link_new ("MSG_VM_PROCESS_LINK", PJ_type_get_root(), msg_vm, msg_vm);
322 void sg_instr_new_router(sg_platf_router_cbarg_t router)
324 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
325 PJ_container_new (router->id, INSTR_ROUTER, father);
328 static void instr_routing_parse_end_platform ()
330 xbt_dynar_free(¤tContainer);
331 currentContainer = NULL;
332 xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free_f);
333 XBT_DEBUG ("Starting graph extraction.");
334 recursiveGraphExtraction (surf_AS_get_routing_root(), PJ_container_get_root(), filter);
335 XBT_DEBUG ("Graph extraction finished.");
336 xbt_dict_free(&filter);
337 platform_created = 1;
338 TRACE_paje_dump_buffer(1);
341 void instr_routing_define_callbacks ()
343 if (!TRACE_is_enabled()) return;
344 //always need the call backs to ASes (we need only the root AS),
345 //to create the rootContainer and the rootType properly
346 if (!TRACE_needs_platform()) return;
347 simgrid::surf::on_link.connect(instr_routing_parse_start_link);
348 simgrid::surf::on_postparse.connect(instr_routing_parse_end_platform);
352 * user categories support
354 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
356 if (!strcmp (root->name, "HOST")){
357 char tnstr[INSTR_DEFAULT_STR_SIZE];
358 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
359 PJ_type_variable_new (tnstr, color, root);
361 if (!strcmp (root->name, "MSG_VM")){
362 char tnstr[INSTR_DEFAULT_STR_SIZE];
363 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
364 PJ_type_variable_new (tnstr, color, root);
366 if (!strcmp (root->name, "LINK")){
367 char tnstr[INSTR_DEFAULT_STR_SIZE];
368 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
369 PJ_type_variable_new (tnstr, color, root);
371 xbt_dict_cursor_t cursor = NULL;
374 xbt_dict_foreach(root->children, cursor, name, child_type) {
375 recursiveNewVariableType (new_typename, color, child_type);
379 void instr_new_variable_type (const char *new_typename, const char *color)
381 recursiveNewVariableType (new_typename, color, PJ_type_get_root());
384 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
386 if (!strcmp (root->name, father_type)){
387 PJ_type_variable_new (new_typename, color, root);
389 xbt_dict_cursor_t cursor = NULL;
392 xbt_dict_foreach(root->children, cursor, name, child_type) {
393 recursiveNewUserVariableType (father_type, new_typename, color, child_type);
397 void instr_new_user_variable_type (const char *father_type, const char *new_typename, const char *color)
399 recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
402 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
404 if (!strcmp (root->name, father_type)){
405 PJ_type_state_new (new_typename, root);
407 xbt_dict_cursor_t cursor = NULL;
410 xbt_dict_foreach(root->children, cursor, name, child_type) {
411 recursiveNewUserStateType (father_type, new_typename, child_type);
415 void instr_new_user_state_type (const char *father_type, const char *new_typename)
417 recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
420 static void recursiveNewValueForUserStateType (const char *type_name, const char *value, const char *color, type_t root)
422 if (!strcmp (root->name, type_name)){
423 PJ_value_new (value, color, root);
425 xbt_dict_cursor_t cursor = NULL;
428 xbt_dict_foreach(root->children, cursor, name, child_type) {
429 recursiveNewValueForUserStateType (type_name, value, color, child_type);
433 void instr_new_value_for_user_state_type (const char *type_name, const char *value, const char *color)
435 recursiveNewValueForUserStateType (type_name, value, color, PJ_type_get_root());
438 int instr_platform_traced ()
440 return platform_created;
443 #define GRAPHICATOR_SUPPORT_FUNCTIONS
446 static void recursiveXBTGraphExtraction (xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
447 AS_t rc, container_t container)
449 if (!xbt_dict_is_empty(surf_AS_get_children(rc))){
450 xbt_dict_cursor_t cursor = NULL;
453 //bottom-up recursion
454 xbt_dict_foreach(surf_AS_get_children(rc), cursor, child_name, rc_son) {
455 container_t child_container = (container_t) xbt_dict_get (
456 container->children, surf_AS_get_name(rc_son));
457 recursiveXBTGraphExtraction (graph, nodes, edges, rc_son, child_container);
461 surf_AS_get_graph(rc, graph, nodes, edges);
464 xbt_graph_t instr_routing_platform_graph (void)
466 xbt_graph_t ret = xbt_graph_new_graph (0, NULL);
467 xbt_dict_t nodes = xbt_dict_new_homogeneous(NULL);
468 xbt_dict_t edges = xbt_dict_new_homogeneous(NULL);
469 recursiveXBTGraphExtraction (ret, nodes, edges, surf_AS_get_routing_root(), PJ_container_get_root());
470 xbt_dict_free (&nodes);
471 xbt_dict_free (&edges);
475 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
477 unsigned int cursor = 0;
478 xbt_node_t node = NULL;
479 xbt_edge_t edge = NULL;
482 file = fopen(filename, "w");
483 xbt_assert(file, "Failed to open %s \n", filename);
486 fprintf(file, "digraph test {\n");
488 fprintf(file, "graph test {\n");
490 fprintf(file, " graph [overlap=scale]\n");
492 fprintf(file, " node [shape=box, style=filled]\n");
494 " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
496 xbt_dynar_foreach(g->nodes, cursor, node) {
497 fprintf(file, " \"%s\";\n", instr_node_name(node));
499 xbt_dynar_foreach(g->edges, cursor, edge) {
500 const char *src_s = instr_node_name (edge->src);
501 const char *dst_s = instr_node_name (edge->dst);
503 fprintf(file, " \"%s\" -> \"%s\";\n", src_s, dst_s);
505 fprintf(file, " \"%s\" -- \"%s\";\n", src_s, dst_s);
507 fprintf(file, "}\n");