1 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/instr/instr_private.h"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/kernel/routing/NetZoneImpl.hpp"
11 #include "src/surf/network_interface.hpp"
12 #include "src/surf/xml/platf_private.hpp"
13 #include "surf/surf.h"
14 #include "xbt/graph.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
18 static int platform_created = 0; /* indicate whether the platform file has been traced */
19 static std::vector<container_t> currentContainer; /* push and pop, used only in creation */
21 static const char *instr_node_name (xbt_node_t node)
23 void *data = xbt_graph_node_get_data(node);
24 return static_cast<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)
33 //create an array with all ancestors of a1
34 std::vector<container_t> ancestors_a1;
35 container_t p = a1->father;
37 ancestors_a1.push_back(p);
41 //create an array with all ancestors of a2
42 std::vector<container_t> ancestors_a2;
45 ancestors_a2.push_back(p);
49 //find the lowest ancestor
51 int i = ancestors_a1.size() - 1;
52 int j = ancestors_a2.size() - 1;
53 while (i >= 0 && j >= 0){
54 container_t a1p = ancestors_a1.at(i);
55 container_t a2p = ancestors_a2.at(j);
67 static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
70 if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0){
71 XBT_DEBUG (" linkContainers: ignoring loopback link");
76 container_t father = lowestCommonAncestor (src, dst);
78 xbt_die ("common father unknown, this is a tracing problem");
81 if (filter != nullptr){
82 //check if we already register this pair (we only need one direction)
83 char aux1[INSTR_DEFAULT_STR_SIZE];
84 char 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"), nullptr);
98 xbt_dict_set (filter, aux2, xbt_strdup ("1"), nullptr);
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 == nullptr){
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"), nullptr);
116 static long long counter = 0;
118 char key[INSTR_DEFAULT_STR_SIZE];
119 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
122 new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "topology", key);
123 new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "topology", key);
125 XBT_DEBUG (" linkContainers %s <-> %s", src->name, dst->name);
128 static void recursiveGraphExtraction(simgrid::s4u::NetZone* netzone, container_t container, xbt_dict_t filter)
130 if (!TRACE_platform_topology()){
131 XBT_DEBUG("Graph extraction disabled by user.");
134 XBT_DEBUG("Graph extraction for NetZone = %s", netzone->name());
135 if (!netzone->children()->empty()) {
136 //bottom-up recursion
137 for (auto nz_son : *netzone->children()) {
138 container_t child_container = static_cast<container_t>(xbt_dict_get(container->children, nz_son->name()));
139 recursiveGraphExtraction(nz_son, child_container, filter);
143 xbt_graph_t graph = xbt_graph_new_graph (0, nullptr);
144 xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
145 xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
146 xbt_edge_t edge = nullptr;
148 xbt_dict_cursor_t cursor = nullptr;
151 static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->getGraph(graph, nodes, edges);
152 xbt_dict_foreach(edges,cursor,edge_name,edge) {
154 PJ_container_get(static_cast<const char*>(edge->src->data)),
155 PJ_container_get(static_cast<const char*>(edge->dst->data)), filter);
157 xbt_dict_free (&nodes);
158 xbt_dict_free (&edges);
159 xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, nullptr);
165 void sg_instr_AS_begin(sg_platf_AS_cbarg_t AS)
167 const char*id = AS->id;
169 if (PJ_container_get_root() == nullptr){
170 PJ_container_alloc ();
172 container_t root = PJ_container_new (id, INSTR_AS, nullptr);
173 PJ_container_set_root (root);
175 if (TRACE_smpi_is_enabled()) {
176 type_t mpi = PJ_type_get_or_null ("MPI", root->type);
178 mpi = PJ_type_container_new("MPI", root->type);
179 if (!TRACE_smpi_is_grouped())
180 PJ_type_state_new ("MPI_STATE", mpi);
181 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
185 if (TRACE_needs_platform()){
186 currentContainer.push_back(root);
191 if (TRACE_needs_platform()){
192 container_t father = currentContainer.back();
193 container_t container = PJ_container_new (id, INSTR_AS, father);
194 currentContainer.push_back(container);
198 void sg_instr_AS_end()
200 if (TRACE_needs_platform()){
201 currentContainer.pop_back();
205 static void instr_routing_parse_start_link(simgrid::s4u::Link& link)
207 if (currentContainer.empty()) // No ongoing parsing. Are you creating the loopback?
209 container_t father = currentContainer.back();
211 double bandwidth_value = link.bandwidth();
212 double latency_value = link.latency();
214 container_t container = PJ_container_new(link.name(), INSTR_LINK, father);
216 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_link())) {
217 type_t bandwidth = PJ_type_get_or_null("bandwidth", container->type);
218 if (bandwidth == nullptr) {
219 bandwidth = PJ_type_variable_new("bandwidth", nullptr, container->type);
221 type_t latency = PJ_type_get_or_null("latency", container->type);
222 if (latency == nullptr) {
223 latency = PJ_type_variable_new("latency", nullptr, container->type);
225 new_pajeSetVariable(0, container, bandwidth, bandwidth_value);
226 new_pajeSetVariable(0, container, latency, latency_value);
228 if (TRACE_uncategorized()) {
229 type_t bandwidth_used = PJ_type_get_or_null("bandwidth_used", container->type);
230 if (bandwidth_used == nullptr) {
231 PJ_type_variable_new("bandwidth_used", "0.5 0.5 0.5", container->type);
236 void sg_instr_new_host(simgrid::s4u::Host& host)
238 container_t father = currentContainer.back();
239 container_t container = PJ_container_new(host.cname(), INSTR_HOST, father);
241 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_speed())) {
242 type_t speed = PJ_type_get_or_null ("power", container->type);
243 if (speed == nullptr){
244 speed = PJ_type_variable_new ("power", nullptr, container->type);
247 double current_speed_state = host.speed();
248 new_pajeSetVariable (0, container, speed, current_speed_state);
250 if (TRACE_uncategorized()){
251 type_t speed_used = PJ_type_get_or_null ("power_used", container->type);
252 if (speed_used == nullptr){
253 PJ_type_variable_new ("power_used", "0.5 0.5 0.5", container->type);
257 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
258 type_t mpi = PJ_type_get_or_null ("MPI", container->type);
260 mpi = PJ_type_container_new("MPI", container->type);
261 PJ_type_state_new ("MPI_STATE", mpi);
265 if (TRACE_msg_process_is_enabled()) {
266 type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", container->type);
267 if (msg_process == nullptr){
268 msg_process = PJ_type_container_new("MSG_PROCESS", container->type);
269 type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
270 PJ_value_new ("suspend", "1 0 1", state);
271 PJ_value_new ("sleep", "1 1 0", state);
272 PJ_value_new ("receive", "1 0 0", state);
273 PJ_value_new ("send", "0 0 1", state);
274 PJ_value_new ("task_execute", "0 1 1", state);
275 PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
276 PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
280 if (TRACE_msg_vm_is_enabled()) {
281 type_t msg_vm = PJ_type_get_or_null ("MSG_VM", container->type);
282 if (msg_vm == nullptr){
283 msg_vm = PJ_type_container_new("MSG_VM", container->type);
284 type_t state = PJ_type_state_new ("MSG_VM_STATE", msg_vm);
285 PJ_value_new ("suspend", "1 0 1", state);
286 PJ_value_new ("sleep", "1 1 0", state);
287 PJ_value_new ("receive", "1 0 0", state);
288 PJ_value_new ("send", "0 0 1", state);
289 PJ_value_new ("task_execute", "0 1 1", state);
290 PJ_type_link_new ("MSG_VM_LINK", PJ_type_get_root(), msg_vm, msg_vm);
291 PJ_type_link_new ("MSG_VM_PROCESS_LINK", PJ_type_get_root(), msg_vm, msg_vm);
297 void sg_instr_new_router(const char* name)
299 if (TRACE_is_enabled() && TRACE_needs_platform()) {
300 container_t father = currentContainer.back();
301 PJ_container_new(name, INSTR_ROUTER, father);
305 static void instr_routing_parse_end_platform ()
307 currentContainer.clear();
308 xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free_f);
309 XBT_DEBUG ("Starting graph extraction.");
310 recursiveGraphExtraction(simgrid::s4u::Engine::instance()->netRoot(), PJ_container_get_root(), filter);
311 XBT_DEBUG ("Graph extraction finished.");
312 xbt_dict_free(&filter);
313 platform_created = 1;
314 TRACE_paje_dump_buffer(1);
317 void instr_routing_define_callbacks ()
319 //always need the call backs to ASes (we need only the root AS),
320 //to create the rootContainer and the rootType properly
321 if (!TRACE_is_enabled() || !TRACE_needs_platform())
323 simgrid::s4u::Link::onCreation.connect(instr_routing_parse_start_link);
324 simgrid::s4u::onPlatformCreated.connect(instr_routing_parse_end_platform);
328 * user categories support
330 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
332 if (!strcmp (root->name, "HOST")){
333 char tnstr[INSTR_DEFAULT_STR_SIZE];
334 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
335 PJ_type_variable_new (tnstr, color, root);
337 if (!strcmp (root->name, "MSG_VM")){
338 char tnstr[INSTR_DEFAULT_STR_SIZE];
339 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
340 PJ_type_variable_new (tnstr, color, root);
342 if (!strcmp (root->name, "LINK")){
343 char tnstr[INSTR_DEFAULT_STR_SIZE];
344 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
345 PJ_type_variable_new (tnstr, color, root);
347 xbt_dict_cursor_t cursor = nullptr;
350 xbt_dict_foreach(root->children, cursor, name, child_type) {
351 recursiveNewVariableType (new_typename, color, child_type);
355 void instr_new_variable_type (const char *new_typename, const char *color)
357 recursiveNewVariableType (new_typename, color, PJ_type_get_root());
360 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
362 if (!strcmp (root->name, father_type)){
363 PJ_type_variable_new (new_typename, color, root);
365 xbt_dict_cursor_t cursor = nullptr;
368 xbt_dict_foreach(root->children, cursor, name, child_type) {
369 recursiveNewUserVariableType (father_type, new_typename, color, child_type);
373 void instr_new_user_variable_type (const char *father_type, const char *new_typename, const char *color)
375 recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
378 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
380 if (!strcmp (root->name, father_type)){
381 PJ_type_state_new (new_typename, root);
383 xbt_dict_cursor_t cursor = nullptr;
386 xbt_dict_foreach(root->children, cursor, name, child_type) {
387 recursiveNewUserStateType (father_type, new_typename, child_type);
391 void instr_new_user_state_type (const char *father_type, const char *new_typename)
393 recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
396 static void recursiveNewValueForUserStateType (const char *type_name, const char *value, const char *color, type_t root)
398 if (!strcmp (root->name, type_name)){
399 PJ_value_new (value, color, root);
401 xbt_dict_cursor_t cursor = nullptr;
404 xbt_dict_foreach(root->children, cursor, name, child_type) {
405 recursiveNewValueForUserStateType (type_name, value, color, child_type);
409 void instr_new_value_for_user_state_type (const char *type_name, const char *value, const char *color)
411 recursiveNewValueForUserStateType (type_name, value, color, PJ_type_get_root());
414 int instr_platform_traced ()
416 return platform_created;
419 #define GRAPHICATOR_SUPPORT_FUNCTIONS
421 static void recursiveXBTGraphExtraction(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges, sg_netzone_t netzone,
422 container_t container)
424 if (!netzone->children()->empty()) {
425 //bottom-up recursion
426 for (auto netzone_child : *netzone->children()) {
427 container_t child_container = static_cast<container_t>(xbt_dict_get(container->children, netzone_child->name()));
428 recursiveXBTGraphExtraction(graph, nodes, edges, netzone_child, child_container);
432 static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->getGraph(graph, nodes, edges);
435 xbt_graph_t instr_routing_platform_graph ()
437 xbt_graph_t ret = xbt_graph_new_graph (0, nullptr);
438 xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
439 xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
440 recursiveXBTGraphExtraction(ret, nodes, edges, simgrid::s4u::Engine::instance()->netRoot(), PJ_container_get_root());
441 xbt_dict_free (&nodes);
442 xbt_dict_free (&edges);
446 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
448 unsigned int cursor = 0;
449 xbt_node_t node = nullptr;
450 xbt_edge_t edge = nullptr;
452 FILE *file = fopen(filename, "w");
453 xbt_assert(file, "Failed to open %s \n", filename);
456 fprintf(file, "digraph test {\n");
458 fprintf(file, "graph test {\n");
460 fprintf(file, " graph [overlap=scale]\n");
462 fprintf(file, " node [shape=box, style=filled]\n");
463 fprintf(file, " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
465 xbt_dynar_foreach(g->nodes, cursor, node) {
466 fprintf(file, " \"%s\";\n", instr_node_name(node));
468 xbt_dynar_foreach(g->edges, cursor, edge) {
469 const char *src_s = instr_node_name (edge->src);
470 const char *dst_s = instr_node_name (edge->dst);
472 fprintf(file, " \"%s\" -> \"%s\";\n", src_s, dst_s);
474 fprintf(file, " \"%s\" -- \"%s\";\n", src_s, dst_s);
476 fprintf(file, "}\n");