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 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 */
20 static const char *instr_node_name (xbt_node_t node)
22 void *data = xbt_graph_node_get_data(node);
23 char *str = (char*)data;
28 static container_t lowestCommonAncestor (container_t a1, container_t a2)
30 //this is only an optimization (since most of a1 and a2 share the same parent)
31 if (a1->father == a2->father) return a1->father;
33 //create an array with all ancestors of a1
34 xbt_dynar_t ancestors_a1 = xbt_dynar_new(sizeof(container_t), NULL);
38 xbt_dynar_push_as (ancestors_a1, container_t, p);
42 //create an array with all ancestors of a2
43 xbt_dynar_t ancestors_a2 = xbt_dynar_new(sizeof(container_t), NULL);
46 xbt_dynar_push_as (ancestors_a2, container_t, p);
50 //find the lowest ancestor
52 int i = xbt_dynar_length (ancestors_a1) - 1;
53 int j = xbt_dynar_length (ancestors_a2) - 1;
54 while (i >= 0 && j >= 0){
55 container_t a1p = *(container_t*)xbt_dynar_get_ptr (ancestors_a1, i);
56 container_t a2p = *(container_t*)xbt_dynar_get_ptr (ancestors_a2, j);
65 xbt_dynar_free (&ancestors_a1);
66 xbt_dynar_free (&ancestors_a2);
70 static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
73 if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0){
74 XBT_DEBUG (" linkContainers: ignoring loopback link");
79 container_t father = lowestCommonAncestor (src, dst);
81 xbt_die ("common father unknown, this is a tracing problem");
85 //check if we already register this pair (we only need one direction)
86 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
87 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", src->name, dst->name);
88 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", dst->name, src->name);
89 if (xbt_dict_get_or_null (filter, aux1)){
90 XBT_DEBUG (" linkContainers: already registered %s <-> %s (1)", src->name, dst->name);
93 if (xbt_dict_get_or_null (filter, aux2)){
94 XBT_DEBUG (" linkContainers: already registered %s <-> %s (2)", dst->name, src->name);
98 //ok, not found, register it
99 xbt_dict_set (filter, aux1, xbt_strdup ("1"), NULL);
100 xbt_dict_set (filter, aux2, xbt_strdup ("1"), NULL);
104 char link_typename[INSTR_DEFAULT_STR_SIZE];
105 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s%s-%s%s",
107 src->type->name, src->type->id,
108 dst->type->name, dst->type->id);
109 type_t link_type = PJ_type_get_or_null (link_typename, father->type);
110 if (link_type == NULL){
111 link_type = PJ_type_link_new (link_typename, father->type, src->type, dst->type);
114 //register EDGE types for triva configuration
115 xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), NULL);
118 static long long counter = 0;
121 MC_ignore_data_bss(&counter, sizeof(counter));
123 char key[INSTR_DEFAULT_STR_SIZE];
124 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
125 new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "topology", key);
126 new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "topology", key);
128 XBT_DEBUG (" linkContainers %s <-> %s", src->name, dst->name);
131 static void recursiveGraphExtraction (AS_t rc, container_t container, xbt_dict_t filter)
133 if (!TRACE_platform_topology()){
134 XBT_DEBUG("Graph extraction disabled by user.");
137 XBT_DEBUG ("Graph extraction for routing_component = %s", rc->name);
138 if (!xbt_dict_is_empty(rc->routing_sons)){
139 xbt_dict_cursor_t cursor = NULL;
142 //bottom-up recursion
143 xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
144 container_t child_container = xbt_dict_get (container->children, rc_son->name);
145 recursiveGraphExtraction (rc_son, child_container, filter);
150 xbt_graph_t graph = xbt_graph_new_graph (0, NULL);
151 xbt_dict_t nodes = xbt_dict_new_homogeneous(NULL);
152 xbt_dict_t edges = xbt_dict_new_homogeneous(NULL);
153 xbt_edge_t edge = NULL;
155 xbt_dict_cursor_t cursor = NULL;
158 rc->get_graph(graph,nodes,edges,rc);
159 xbt_dict_foreach(edges,cursor,edge_name,edge) {
160 linkContainers(PJ_container_get(edge->src->data), PJ_container_get(edge->dst->data), filter);
162 xbt_dict_free (&nodes);
163 xbt_dict_free (&edges);
164 xbt_graph_free_graph(graph,xbt_free, xbt_free, NULL);
171 static void instr_routing_parse_start_AS (sg_platf_AS_cbarg_t AS)
173 const char*id = AS->id;
175 if (PJ_container_get_root() == NULL){
176 PJ_container_alloc ();
178 container_t root = PJ_container_new (id, INSTR_AS, NULL);
179 PJ_container_set_root (root);
181 if (TRACE_smpi_is_enabled()) {
182 if (!TRACE_smpi_is_grouped()){
183 type_t mpi = PJ_type_get_or_null ("MPI", root->type);
185 mpi = PJ_type_container_new("MPI", root->type);
186 PJ_type_state_new ("MPI_STATE", mpi);
187 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
192 if (TRACE_needs_platform()){
193 currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
194 xbt_dynar_push (currentContainer, &root);
199 if (TRACE_needs_platform()){
200 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
201 container_t new = PJ_container_new (id, INSTR_AS, father);
202 xbt_dynar_push (currentContainer, &new);
206 static void instr_routing_parse_end_AS ()
208 if (TRACE_needs_platform()){
209 xbt_dynar_pop_ptr (currentContainer);
213 static void instr_routing_parse_start_link (sg_platf_link_cbarg_t link)
215 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
217 double bandwidth_value = link->bandwidth;
218 double latency_value = link->latency;
219 xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
221 if (link->policy == SURF_LINK_FULLDUPLEX){
222 char *up = bprintf("%s_UP", link->id);
223 char *down = bprintf("%s_DOWN", link->id);
224 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
225 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
229 xbt_dynar_push_as (links_to_create, char*, strdup(link->id));
232 char *link_name = NULL;
234 xbt_dynar_foreach (links_to_create, i, link_name){
236 container_t new = PJ_container_new (link_name, INSTR_LINK, father);
238 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_link())) {
239 type_t bandwidth = PJ_type_get_or_null ("bandwidth", new->type);
240 if (bandwidth == NULL){
241 bandwidth = PJ_type_variable_new ("bandwidth", NULL, new->type);
243 type_t latency = PJ_type_get_or_null ("latency", new->type);
244 if (latency == NULL){
245 latency = PJ_type_variable_new ("latency", NULL, new->type);
247 new_pajeSetVariable (0, new, bandwidth, bandwidth_value);
248 new_pajeSetVariable (0, new, latency, latency_value);
250 if (TRACE_uncategorized()){
251 type_t bandwidth_used = PJ_type_get_or_null ("bandwidth_used", new->type);
252 if (bandwidth_used == NULL){
253 bandwidth_used = PJ_type_variable_new ("bandwidth_used", "0.5 0.5 0.5", new->type);
258 xbt_dynar_free (&links_to_create);
261 static void instr_routing_parse_start_host (sg_platf_host_cbarg_t host)
263 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
264 container_t new = PJ_container_new (host->id, INSTR_HOST, father);
266 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_power())) {
267 type_t power = PJ_type_get_or_null ("power", new->type);
269 power = PJ_type_variable_new ("power", NULL, new->type);
272 double current_power_state;
273 xbt_dynar_get_cpy(host->power_peak, host->pstate, ¤t_power_state);
274 new_pajeSetVariable (0, new, power, current_power_state);
276 if (TRACE_uncategorized()){
277 type_t power_used = PJ_type_get_or_null ("power_used", new->type);
278 if (power_used == NULL){
279 power_used = PJ_type_variable_new ("power_used", "0.5 0.5 0.5", new->type);
283 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
284 type_t mpi = PJ_type_get_or_null ("MPI", new->type);
286 mpi = PJ_type_container_new("MPI", new->type);
287 PJ_type_state_new ("MPI_STATE", mpi);
288 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
292 if (TRACE_msg_process_is_enabled()) {
293 type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", new->type);
294 if (msg_process == NULL){
295 msg_process = PJ_type_container_new("MSG_PROCESS", new->type);
296 type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
297 PJ_value_new ("suspend", "1 0 1", state);
298 PJ_value_new ("sleep", "1 1 0", state);
299 PJ_value_new ("receive", "1 0 0", state);
300 PJ_value_new ("send", "0 0 1", state);
301 PJ_value_new ("task_execute", "0 1 1", state);
302 PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
303 PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
307 if (TRACE_msg_vm_is_enabled()) {
308 type_t msg_vm = PJ_type_get_or_null ("MSG_VM", new->type);
310 msg_vm = PJ_type_container_new("MSG_VM", new->type);
311 type_t state = PJ_type_state_new ("MSG_VM_STATE", msg_vm);
312 PJ_value_new ("suspend", "1 0 1", state);
313 PJ_value_new ("sleep", "1 1 0", state);
314 PJ_value_new ("receive", "1 0 0", state);
315 PJ_value_new ("send", "0 0 1", state);
316 PJ_value_new ("task_execute", "0 1 1", state);
317 PJ_type_link_new ("MSG_VM_LINK", PJ_type_get_root(), msg_vm, msg_vm);
318 PJ_type_link_new ("MSG_VM_PROCESS_LINK", PJ_type_get_root(), msg_vm, msg_vm);
324 static void instr_routing_parse_start_router (sg_platf_router_cbarg_t router)
326 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
327 PJ_container_new (router->id, INSTR_ROUTER, father);
330 static void instr_routing_parse_end_platform ()
332 xbt_dynar_free(¤tContainer);
333 currentContainer = NULL;
334 xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free);
335 XBT_DEBUG ("Starting graph extraction.");
336 recursiveGraphExtraction (routing_platf->root, PJ_container_get_root(), filter);
337 XBT_DEBUG ("Graph extraction finished.");
338 xbt_dict_free(&filter);
339 platform_created = 1;
340 TRACE_paje_dump_buffer(1);
343 void instr_routing_define_callbacks ()
345 if (!TRACE_is_enabled()) return;
346 //always need the call backs to ASes (we need only the root AS),
347 //to create the rootContainer and the rootType properly
348 sg_platf_AS_begin_add_cb(instr_routing_parse_start_AS);
349 sg_platf_AS_end_add_cb(instr_routing_parse_end_AS);
350 if (!TRACE_needs_platform()) return;
351 sg_platf_link_add_cb(instr_routing_parse_start_link);
352 sg_platf_host_add_cb(instr_routing_parse_start_host);
353 sg_platf_router_add_cb(instr_routing_parse_start_router);
355 sg_platf_postparse_add_cb(instr_routing_parse_end_platform);
359 * user categories support
361 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
363 if (!strcmp (root->name, "HOST")){
364 char tnstr[INSTR_DEFAULT_STR_SIZE];
365 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
366 PJ_type_variable_new (tnstr, color, root);
368 if (!strcmp (root->name, "MSG_VM")){
369 char tnstr[INSTR_DEFAULT_STR_SIZE];
370 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
371 PJ_type_variable_new (tnstr, color, root);
373 if (!strcmp (root->name, "LINK")){
374 char tnstr[INSTR_DEFAULT_STR_SIZE];
375 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
376 PJ_type_variable_new (tnstr, color, root);
378 xbt_dict_cursor_t cursor = NULL;
381 xbt_dict_foreach(root->children, cursor, name, child_type) {
382 recursiveNewVariableType (new_typename, color, child_type);
386 void instr_new_variable_type (const char *new_typename, const char *color)
388 recursiveNewVariableType (new_typename, color, PJ_type_get_root());
391 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
393 if (!strcmp (root->name, father_type)){
394 PJ_type_variable_new (new_typename, color, root);
396 xbt_dict_cursor_t cursor = NULL;
399 xbt_dict_foreach(root->children, cursor, name, child_type) {
400 recursiveNewUserVariableType (father_type, new_typename, color, child_type);
404 void instr_new_user_variable_type (const char *father_type, const char *new_typename, const char *color)
406 recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
409 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
411 if (!strcmp (root->name, father_type)){
412 PJ_type_state_new (new_typename, root);
414 xbt_dict_cursor_t cursor = NULL;
417 xbt_dict_foreach(root->children, cursor, name, child_type) {
418 recursiveNewUserStateType (father_type, new_typename, child_type);
422 void instr_new_user_state_type (const char *father_type, const char *new_typename)
424 recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
427 static void recursiveNewValueForUserStateType (const char *typename, const char *value, const char *color, type_t root)
429 if (!strcmp (root->name, typename)){
430 PJ_value_new (value, color, root);
432 xbt_dict_cursor_t cursor = NULL;
435 xbt_dict_foreach(root->children, cursor, name, child_type) {
436 recursiveNewValueForUserStateType (typename, value, color, child_type);
440 void instr_new_value_for_user_state_type (const char *typename, const char *value, const char *color)
442 recursiveNewValueForUserStateType (typename, value, color, PJ_type_get_root());
445 int instr_platform_traced ()
447 return platform_created;
450 #define GRAPHICATOR_SUPPORT_FUNCTIONS
453 static void recursiveXBTGraphExtraction (xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
454 AS_t rc, container_t container)
456 if (!xbt_dict_is_empty(rc->routing_sons)){
457 xbt_dict_cursor_t cursor = NULL;
460 //bottom-up recursion
461 xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
462 container_t child_container = xbt_dict_get (container->children, rc_son->name);
463 recursiveXBTGraphExtraction (graph, nodes, edges, rc_son, child_container);
467 rc->get_graph(graph,nodes,edges,rc);
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_homogeneous(NULL);
474 xbt_dict_t edges = xbt_dict_new_homogeneous(NULL);
475 recursiveXBTGraphExtraction (ret, nodes, edges, routing_platf->root, PJ_container_get_root());
476 xbt_dict_free (&nodes);
477 xbt_dict_free (&edges);
481 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
483 unsigned int cursor = 0;
484 xbt_node_t node = NULL;
485 xbt_edge_t edge = NULL;
488 file = fopen(filename, "w");
489 xbt_assert(file, "Failed to open %s \n", filename);
492 fprintf(file, "digraph test {\n");
494 fprintf(file, "graph test {\n");
496 fprintf(file, " graph [overlap=scale]\n");
498 fprintf(file, " node [shape=box, style=filled]\n");
500 " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
502 xbt_dynar_foreach(g->nodes, cursor, node) {
503 fprintf(file, " \"%s\";\n", instr_node_name(node));
505 xbt_dynar_foreach(g->edges, cursor, edge) {
506 const char *src_s = instr_node_name (edge->src);
507 const char *dst_s = instr_node_name (edge->dst);
509 fprintf(file, " \"%s\" -> \"%s\";\n", src_s, dst_s);
511 fprintf(file, " \"%s\" -- \"%s\";\n", src_s, dst_s);
513 fprintf(file, "}\n");
518 #endif /* HAVE_TRACING */