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/kernel/routing/AsImpl.hpp"
10 #include "simgrid/s4u/engine.hpp"
11 #include "surf/surf.h"
12 #include "src/surf/xml/platf_private.hpp"
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 = nullptr; /* 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;
27 static container_t lowestCommonAncestor (container_t a1, container_t a2)
29 //this is only an optimization (since most of a1 and a2 share the same parent)
30 if (a1->father == a2->father) return a1->father;
32 //create an array with all ancestors of a1
33 xbt_dynar_t ancestors_a1 = xbt_dynar_new(sizeof(container_t), nullptr);
37 xbt_dynar_push_as (ancestors_a1, container_t, p);
41 //create an array with all ancestors of a2
42 xbt_dynar_t ancestors_a2 = xbt_dynar_new(sizeof(container_t), nullptr);
45 xbt_dynar_push_as (ancestors_a2, container_t, p);
49 //find the lowest ancestor
51 int i = xbt_dynar_length (ancestors_a1) - 1;
52 int j = xbt_dynar_length (ancestors_a2) - 1;
53 while (i >= 0 && j >= 0){
54 container_t a1p = *(container_t*)xbt_dynar_get_ptr (ancestors_a1, i);
55 container_t a2p = *(container_t*)xbt_dynar_get_ptr (ancestors_a2, j);
64 xbt_dynar_free (&ancestors_a1);
65 xbt_dynar_free (&ancestors_a2);
69 static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
72 if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0){
73 XBT_DEBUG (" linkContainers: ignoring loopback link");
78 container_t father = lowestCommonAncestor (src, dst);
80 xbt_die ("common father unknown, this is a tracing problem");
83 if (filter != nullptr){
84 //check if we already register this pair (we only need one direction)
85 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
86 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", src->name, dst->name);
87 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", dst->name, src->name);
88 if (xbt_dict_get_or_null (filter, aux1)){
89 XBT_DEBUG (" linkContainers: already registered %s <-> %s (1)", src->name, dst->name);
92 if (xbt_dict_get_or_null (filter, aux2)){
93 XBT_DEBUG (" linkContainers: already registered %s <-> %s (2)", dst->name, src->name);
97 //ok, not found, register it
98 xbt_dict_set (filter, aux1, xbt_strdup ("1"), nullptr);
99 xbt_dict_set (filter, aux2, xbt_strdup ("1"), nullptr);
103 char link_typename[INSTR_DEFAULT_STR_SIZE];
104 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s%s-%s%s",
106 src->type->name, src->type->id,
107 dst->type->name, dst->type->id);
108 type_t link_type = PJ_type_get_or_null (link_typename, father->type);
109 if (link_type == nullptr){
110 link_type = PJ_type_link_new (link_typename, father->type, src->type, dst->type);
113 //register EDGE types for triva configuration
114 xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), nullptr);
117 static long long counter = 0;
119 char key[INSTR_DEFAULT_STR_SIZE];
120 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
121 new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "topology", key);
122 new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "topology", key);
124 XBT_DEBUG (" linkContainers %s <-> %s", src->name, dst->name);
127 static void recursiveGraphExtraction (simgrid::s4u::As *as, container_t container, xbt_dict_t filter)
129 if (!TRACE_platform_topology()){
130 XBT_DEBUG("Graph extraction disabled by user.");
133 XBT_DEBUG ("Graph extraction for routing_component = %s", as->name());
134 if (!xbt_dict_is_empty(as->children())){
135 xbt_dict_cursor_t cursor = nullptr;
138 //bottom-up recursion
139 xbt_dict_foreach(as->children(), cursor, child_name, rc_son) {
140 container_t child_container = (container_t) xbt_dict_get (container->children, rc_son->name());
141 recursiveGraphExtraction (rc_son, child_container, filter);
146 xbt_graph_t graph = xbt_graph_new_graph (0, nullptr);
147 xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
148 xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
149 xbt_edge_t edge = nullptr;
151 xbt_dict_cursor_t cursor = nullptr;
154 static_cast<simgrid::kernel::routing::AsImpl*>(as)->getGraph(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, nullptr);
169 void sg_instr_AS_begin(sg_platf_AS_cbarg_t AS)
171 const char*id = AS->id;
173 if (PJ_container_get_root() == nullptr){
174 PJ_container_alloc ();
176 container_t root = PJ_container_new (id, INSTR_AS, nullptr);
177 PJ_container_set_root (root);
179 if (TRACE_smpi_is_enabled()) {
180 type_t mpi = PJ_type_get_or_null ("MPI", root->type);
182 mpi = PJ_type_container_new("MPI", root->type);
183 if (!TRACE_smpi_is_grouped()) PJ_type_state_new ("MPI_STATE", mpi);
184 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
188 if (TRACE_needs_platform()){
189 currentContainer = xbt_dynar_new (sizeof(container_t), nullptr);
190 xbt_dynar_push (currentContainer, &root);
195 if (TRACE_needs_platform()){
196 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
197 container_t container = PJ_container_new (id, INSTR_AS, father);
198 xbt_dynar_push (currentContainer, &container);
202 void sg_instr_AS_end()
204 if (TRACE_needs_platform()){
205 xbt_dynar_pop_ptr (currentContainer);
209 static void instr_routing_parse_start_link (sg_platf_link_cbarg_t link)
211 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
213 double bandwidth_value = link->bandwidth;
214 double latency_value = link->latency;
215 xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
217 if (link->policy == SURF_LINK_FULLDUPLEX){
218 char *up = bprintf("%s_UP", link->id);
219 char *down = bprintf("%s_DOWN", link->id);
220 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
221 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
225 xbt_dynar_push_as (links_to_create, char*, strdup(link->id));
228 char *link_name = nullptr;
230 xbt_dynar_foreach (links_to_create, i, link_name){
232 container_t container = PJ_container_new (link_name, INSTR_LINK, father);
234 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_link())) {
235 type_t bandwidth = PJ_type_get_or_null ("bandwidth", container->type);
236 if (bandwidth == nullptr){
237 bandwidth = PJ_type_variable_new ("bandwidth", nullptr, container->type);
239 type_t latency = PJ_type_get_or_null ("latency", container->type);
240 if (latency == nullptr){
241 latency = PJ_type_variable_new ("latency", nullptr, container->type);
243 new_pajeSetVariable (0, container, bandwidth, bandwidth_value);
244 new_pajeSetVariable (0, container, latency, latency_value);
246 if (TRACE_uncategorized()){
247 type_t bandwidth_used = PJ_type_get_or_null ("bandwidth_used", container->type);
248 if (bandwidth_used == nullptr){
249 PJ_type_variable_new ("bandwidth_used", "0.5 0.5 0.5", container->type);
254 xbt_dynar_free (&links_to_create);
257 void sg_instr_new_host(sg_platf_host_cbarg_t host)
259 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
260 container_t container = PJ_container_new (host->id, INSTR_HOST, father);
262 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_speed())) {
263 type_t speed = PJ_type_get_or_null ("power", container->type);
264 if (speed == nullptr){
265 speed = PJ_type_variable_new ("power", nullptr, container->type);
268 double current_speed_state = host->speed_per_pstate[host->pstate];
269 new_pajeSetVariable (0, container, speed, current_speed_state);
271 if (TRACE_uncategorized()){
272 type_t speed_used = PJ_type_get_or_null ("power_used", container->type);
273 if (speed_used == nullptr){
274 PJ_type_variable_new ("power_used", "0.5 0.5 0.5", container->type);
278 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
279 type_t mpi = PJ_type_get_or_null ("MPI", container->type);
281 mpi = PJ_type_container_new("MPI", container->type);
282 PJ_type_state_new ("MPI_STATE", mpi);
286 if (TRACE_msg_process_is_enabled()) {
287 type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", container->type);
288 if (msg_process == nullptr){
289 msg_process = PJ_type_container_new("MSG_PROCESS", container->type);
290 type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
291 PJ_value_new ("suspend", "1 0 1", state);
292 PJ_value_new ("sleep", "1 1 0", state);
293 PJ_value_new ("receive", "1 0 0", state);
294 PJ_value_new ("send", "0 0 1", state);
295 PJ_value_new ("task_execute", "0 1 1", state);
296 PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
297 PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
301 if (TRACE_msg_vm_is_enabled()) {
302 type_t msg_vm = PJ_type_get_or_null ("MSG_VM", container->type);
303 if (msg_vm == nullptr){
304 msg_vm = PJ_type_container_new("MSG_VM", container->type);
305 type_t state = PJ_type_state_new ("MSG_VM_STATE", msg_vm);
306 PJ_value_new ("suspend", "1 0 1", state);
307 PJ_value_new ("sleep", "1 1 0", state);
308 PJ_value_new ("receive", "1 0 0", state);
309 PJ_value_new ("send", "0 0 1", state);
310 PJ_value_new ("task_execute", "0 1 1", state);
311 PJ_type_link_new ("MSG_VM_LINK", PJ_type_get_root(), msg_vm, msg_vm);
312 PJ_type_link_new ("MSG_VM_PROCESS_LINK", PJ_type_get_root(), msg_vm, msg_vm);
318 void sg_instr_new_router(sg_platf_router_cbarg_t router)
320 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
321 PJ_container_new (router->id, INSTR_ROUTER, father);
324 static void instr_routing_parse_end_platform ()
326 xbt_dynar_free(¤tContainer);
327 currentContainer = nullptr;
328 xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free_f);
329 XBT_DEBUG ("Starting graph extraction.");
330 recursiveGraphExtraction (simgrid::s4u::Engine::instance()->rootAs(), PJ_container_get_root(), filter);
331 XBT_DEBUG ("Graph extraction finished.");
332 xbt_dict_free(&filter);
333 platform_created = 1;
334 TRACE_paje_dump_buffer(1);
337 void instr_routing_define_callbacks ()
339 if (!TRACE_is_enabled()) return;
340 //always need the call backs to ASes (we need only the root AS),
341 //to create the rootContainer and the rootType properly
342 if (!TRACE_needs_platform()) return;
343 simgrid::surf::on_link.connect(instr_routing_parse_start_link);
344 simgrid::surf::on_postparse.connect(instr_routing_parse_end_platform);
348 * user categories support
350 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
352 if (!strcmp (root->name, "HOST")){
353 char tnstr[INSTR_DEFAULT_STR_SIZE];
354 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
355 PJ_type_variable_new (tnstr, color, root);
357 if (!strcmp (root->name, "MSG_VM")){
358 char tnstr[INSTR_DEFAULT_STR_SIZE];
359 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
360 PJ_type_variable_new (tnstr, color, root);
362 if (!strcmp (root->name, "LINK")){
363 char tnstr[INSTR_DEFAULT_STR_SIZE];
364 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
365 PJ_type_variable_new (tnstr, color, root);
367 xbt_dict_cursor_t cursor = nullptr;
370 xbt_dict_foreach(root->children, cursor, name, child_type) {
371 recursiveNewVariableType (new_typename, color, child_type);
375 void instr_new_variable_type (const char *new_typename, const char *color)
377 recursiveNewVariableType (new_typename, color, PJ_type_get_root());
380 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
382 if (!strcmp (root->name, father_type)){
383 PJ_type_variable_new (new_typename, color, root);
385 xbt_dict_cursor_t cursor = nullptr;
388 xbt_dict_foreach(root->children, cursor, name, child_type) {
389 recursiveNewUserVariableType (father_type, new_typename, color, child_type);
393 void instr_new_user_variable_type (const char *father_type, const char *new_typename, const char *color)
395 recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
398 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
400 if (!strcmp (root->name, father_type)){
401 PJ_type_state_new (new_typename, root);
403 xbt_dict_cursor_t cursor = nullptr;
406 xbt_dict_foreach(root->children, cursor, name, child_type) {
407 recursiveNewUserStateType (father_type, new_typename, child_type);
411 void instr_new_user_state_type (const char *father_type, const char *new_typename)
413 recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
416 static void recursiveNewValueForUserStateType (const char *type_name, const char *value, const char *color, type_t root)
418 if (!strcmp (root->name, type_name)){
419 PJ_value_new (value, color, root);
421 xbt_dict_cursor_t cursor = nullptr;
424 xbt_dict_foreach(root->children, cursor, name, child_type) {
425 recursiveNewValueForUserStateType (type_name, value, color, child_type);
429 void instr_new_value_for_user_state_type (const char *type_name, const char *value, const char *color)
431 recursiveNewValueForUserStateType (type_name, value, color, PJ_type_get_root());
434 int instr_platform_traced ()
436 return platform_created;
439 #define GRAPHICATOR_SUPPORT_FUNCTIONS
441 static void recursiveXBTGraphExtraction (xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
442 AS_t as, container_t container)
444 if (!xbt_dict_is_empty(as->children())){
445 xbt_dict_cursor_t cursor = nullptr;
448 //bottom-up recursion
449 xbt_dict_foreach(as->children(), cursor, child_name, as_child) {
450 container_t child_container = (container_t) xbt_dict_get (
451 container->children, as_child->name());
452 recursiveXBTGraphExtraction (graph, nodes, edges, as_child, child_container);
456 static_cast<simgrid::kernel::routing::AsImpl*>(as)->getGraph(graph, nodes, edges);
459 xbt_graph_t instr_routing_platform_graph ()
461 xbt_graph_t ret = xbt_graph_new_graph (0, nullptr);
462 xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
463 xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
464 recursiveXBTGraphExtraction (ret, nodes, edges, simgrid::s4u::Engine::instance()->rootAs(), PJ_container_get_root());
465 xbt_dict_free (&nodes);
466 xbt_dict_free (&edges);
470 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
472 unsigned int cursor = 0;
473 xbt_node_t node = nullptr;
474 xbt_edge_t edge = nullptr;
475 FILE *file = nullptr;
477 file = fopen(filename, "w");
478 xbt_assert(file, "Failed to open %s \n", filename);
481 fprintf(file, "digraph test {\n");
483 fprintf(file, "graph test {\n");
485 fprintf(file, " graph [overlap=scale]\n");
487 fprintf(file, " node [shape=box, style=filled]\n");
489 " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
491 xbt_dynar_foreach(g->nodes, cursor, node) {
492 fprintf(file, " \"%s\";\n", instr_node_name(node));
494 xbt_dynar_foreach(g->edges, cursor, edge) {
495 const char *src_s = instr_node_name (edge->src);
496 const char *dst_s = instr_node_name (edge->dst);
498 fprintf(file, " \"%s\" -> \"%s\";\n", src_s, dst_s);
500 fprintf(file, " \"%s\" -- \"%s\";\n", src_s, dst_s);
502 fprintf(file, "}\n");