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 "simgrid/s4u/engine.hpp"
10 #include "simgrid/s4u/host.hpp"
11 #include "src/kernel/routing/NetZoneImpl.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 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 std::vector<container_t> ancestors_a1;
38 ancestors_a1.push_back(p);
42 //create an array with all ancestors of a2
43 std::vector<container_t> ancestors_a2;
46 ancestors_a2.push_back(p);
50 //find the lowest ancestor
52 int i = ancestors_a1.size() - 1;
53 int j = ancestors_a2.size() - 1;
54 while (i >= 0 && j >= 0){
55 container_t a1p = ancestors_a1.at(i);
56 container_t a2p = ancestors_a2.at(j);
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");
82 if (filter != nullptr){
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"), 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++);
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::s4u::As *as, 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", as->name());
133 if (!xbt_dict_is_empty(as->children())){
134 xbt_dict_cursor_t cursor = nullptr;
137 //bottom-up recursion
138 xbt_dict_foreach(as->children(), cursor, child_name, rc_son) {
139 container_t child_container = (container_t) xbt_dict_get (container->children, rc_son->name());
140 recursiveGraphExtraction (rc_son, child_container, filter);
145 xbt_graph_t graph = xbt_graph_new_graph (0, nullptr);
146 xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
147 xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
148 xbt_edge_t edge = nullptr;
150 xbt_dict_cursor_t cursor = nullptr;
153 static_cast<simgrid::kernel::routing::AsImpl*>(as)->getGraph(graph, nodes, edges);
154 xbt_dict_foreach(edges,cursor,edge_name,edge) {
156 PJ_container_get((const char*) edge->src->data),
157 PJ_container_get((const char*) edge->dst->data), filter);
159 xbt_dict_free (&nodes);
160 xbt_dict_free (&edges);
161 xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, nullptr);
168 void sg_instr_AS_begin(sg_platf_AS_cbarg_t AS)
170 const char*id = AS->id;
172 if (PJ_container_get_root() == nullptr){
173 PJ_container_alloc ();
175 container_t root = PJ_container_new (id, INSTR_AS, nullptr);
176 PJ_container_set_root (root);
178 if (TRACE_smpi_is_enabled()) {
179 type_t mpi = PJ_type_get_or_null ("MPI", root->type);
181 mpi = PJ_type_container_new("MPI", root->type);
182 if (!TRACE_smpi_is_grouped()) PJ_type_state_new ("MPI_STATE", mpi);
183 PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
187 if (TRACE_needs_platform()){
188 currentContainer.push_back(root);
193 if (TRACE_needs_platform()){
194 container_t father = currentContainer.back();
195 container_t container = PJ_container_new (id, INSTR_AS, father);
196 currentContainer.push_back(container);
200 void sg_instr_AS_end()
202 if (TRACE_needs_platform()){
203 currentContainer.pop_back();
207 static void instr_routing_parse_start_link (sg_platf_link_cbarg_t link)
209 container_t father = currentContainer.back();
211 double bandwidth_value = link->bandwidth;
212 double latency_value = link->latency;
213 std::vector<std::string> links_to_create;
215 if (link->policy == SURF_LINK_FULLDUPLEX){
216 std::string id (link->id);
217 std::string up = id + "_UP";
218 std::string down = id + "_DOWN";
219 links_to_create.push_back(up);
220 links_to_create.push_back(down);
222 links_to_create.push_back(link->id);
225 for (auto link_name: links_to_create){
226 container_t container = PJ_container_new (link_name.c_str(), INSTR_LINK, father);
228 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_link())) {
229 type_t bandwidth = PJ_type_get_or_null ("bandwidth", container->type);
230 if (bandwidth == nullptr){
231 bandwidth = PJ_type_variable_new ("bandwidth", nullptr, container->type);
233 type_t latency = PJ_type_get_or_null ("latency", container->type);
234 if (latency == nullptr){
235 latency = PJ_type_variable_new ("latency", nullptr, container->type);
237 new_pajeSetVariable (0, container, bandwidth, bandwidth_value);
238 new_pajeSetVariable (0, container, latency, latency_value);
240 if (TRACE_uncategorized()){
241 type_t bandwidth_used = PJ_type_get_or_null ("bandwidth_used", container->type);
242 if (bandwidth_used == nullptr){
243 PJ_type_variable_new ("bandwidth_used", "0.5 0.5 0.5", container->type);
249 void sg_instr_new_host(simgrid::s4u::Host& host)
251 container_t father = currentContainer.back();
252 container_t container = PJ_container_new(host.cname(), INSTR_HOST, father);
254 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_speed())) {
255 type_t speed = PJ_type_get_or_null ("power", container->type);
256 if (speed == nullptr){
257 speed = PJ_type_variable_new ("power", nullptr, container->type);
260 double current_speed_state = host.getPstateSpeedCurrent();
261 new_pajeSetVariable (0, container, speed, current_speed_state);
263 if (TRACE_uncategorized()){
264 type_t speed_used = PJ_type_get_or_null ("power_used", container->type);
265 if (speed_used == nullptr){
266 PJ_type_variable_new ("power_used", "0.5 0.5 0.5", container->type);
270 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
271 type_t mpi = PJ_type_get_or_null ("MPI", container->type);
273 mpi = PJ_type_container_new("MPI", container->type);
274 PJ_type_state_new ("MPI_STATE", mpi);
278 if (TRACE_msg_process_is_enabled()) {
279 type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", container->type);
280 if (msg_process == nullptr){
281 msg_process = PJ_type_container_new("MSG_PROCESS", container->type);
282 type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
283 PJ_value_new ("suspend", "1 0 1", state);
284 PJ_value_new ("sleep", "1 1 0", state);
285 PJ_value_new ("receive", "1 0 0", state);
286 PJ_value_new ("send", "0 0 1", state);
287 PJ_value_new ("task_execute", "0 1 1", state);
288 PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
289 PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
293 if (TRACE_msg_vm_is_enabled()) {
294 type_t msg_vm = PJ_type_get_or_null ("MSG_VM", container->type);
295 if (msg_vm == nullptr){
296 msg_vm = PJ_type_container_new("MSG_VM", container->type);
297 type_t state = PJ_type_state_new ("MSG_VM_STATE", msg_vm);
298 PJ_value_new ("suspend", "1 0 1", state);
299 PJ_value_new ("sleep", "1 1 0", state);
300 PJ_value_new ("receive", "1 0 0", state);
301 PJ_value_new ("send", "0 0 1", state);
302 PJ_value_new ("task_execute", "0 1 1", state);
303 PJ_type_link_new ("MSG_VM_LINK", PJ_type_get_root(), msg_vm, msg_vm);
304 PJ_type_link_new ("MSG_VM_PROCESS_LINK", PJ_type_get_root(), msg_vm, msg_vm);
310 void sg_instr_new_router(sg_platf_router_cbarg_t router)
312 container_t father = currentContainer.back();
313 PJ_container_new (router->id, INSTR_ROUTER, father);
316 static void instr_routing_parse_end_platform ()
318 currentContainer.clear();
319 xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free_f);
320 XBT_DEBUG ("Starting graph extraction.");
321 recursiveGraphExtraction (simgrid::s4u::Engine::instance()->rootAs(), PJ_container_get_root(), filter);
322 XBT_DEBUG ("Graph extraction finished.");
323 xbt_dict_free(&filter);
324 platform_created = 1;
325 TRACE_paje_dump_buffer(1);
328 void instr_routing_define_callbacks ()
330 if (!TRACE_is_enabled()) return;
331 //always need the call backs to ASes (we need only the root AS),
332 //to create the rootContainer and the rootType properly
333 if (!TRACE_needs_platform()) return;
334 simgrid::surf::on_link.connect(instr_routing_parse_start_link);
335 simgrid::surf::on_postparse.connect(instr_routing_parse_end_platform);
339 * user categories support
341 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
343 if (!strcmp (root->name, "HOST")){
344 char tnstr[INSTR_DEFAULT_STR_SIZE];
345 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
346 PJ_type_variable_new (tnstr, color, root);
348 if (!strcmp (root->name, "MSG_VM")){
349 char tnstr[INSTR_DEFAULT_STR_SIZE];
350 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
351 PJ_type_variable_new (tnstr, color, root);
353 if (!strcmp (root->name, "LINK")){
354 char tnstr[INSTR_DEFAULT_STR_SIZE];
355 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
356 PJ_type_variable_new (tnstr, color, root);
358 xbt_dict_cursor_t cursor = nullptr;
361 xbt_dict_foreach(root->children, cursor, name, child_type) {
362 recursiveNewVariableType (new_typename, color, child_type);
366 void instr_new_variable_type (const char *new_typename, const char *color)
368 recursiveNewVariableType (new_typename, color, PJ_type_get_root());
371 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
373 if (!strcmp (root->name, father_type)){
374 PJ_type_variable_new (new_typename, color, root);
376 xbt_dict_cursor_t cursor = nullptr;
379 xbt_dict_foreach(root->children, cursor, name, child_type) {
380 recursiveNewUserVariableType (father_type, new_typename, color, child_type);
384 void instr_new_user_variable_type (const char *father_type, const char *new_typename, const char *color)
386 recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
389 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
391 if (!strcmp (root->name, father_type)){
392 PJ_type_state_new (new_typename, root);
394 xbt_dict_cursor_t cursor = nullptr;
397 xbt_dict_foreach(root->children, cursor, name, child_type) {
398 recursiveNewUserStateType (father_type, new_typename, child_type);
402 void instr_new_user_state_type (const char *father_type, const char *new_typename)
404 recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
407 static void recursiveNewValueForUserStateType (const char *type_name, const char *value, const char *color, type_t root)
409 if (!strcmp (root->name, type_name)){
410 PJ_value_new (value, color, root);
412 xbt_dict_cursor_t cursor = nullptr;
415 xbt_dict_foreach(root->children, cursor, name, child_type) {
416 recursiveNewValueForUserStateType (type_name, value, color, child_type);
420 void instr_new_value_for_user_state_type (const char *type_name, const char *value, const char *color)
422 recursiveNewValueForUserStateType (type_name, value, color, PJ_type_get_root());
425 int instr_platform_traced ()
427 return platform_created;
430 #define GRAPHICATOR_SUPPORT_FUNCTIONS
432 static void recursiveXBTGraphExtraction (xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
433 AS_t as, container_t container)
435 if (!xbt_dict_is_empty(as->children())){
436 xbt_dict_cursor_t cursor = nullptr;
439 //bottom-up recursion
440 xbt_dict_foreach(as->children(), cursor, child_name, as_child) {
441 container_t child_container = (container_t) xbt_dict_get (
442 container->children, as_child->name());
443 recursiveXBTGraphExtraction (graph, nodes, edges, as_child, child_container);
447 static_cast<simgrid::kernel::routing::AsImpl*>(as)->getGraph(graph, nodes, edges);
450 xbt_graph_t instr_routing_platform_graph ()
452 xbt_graph_t ret = xbt_graph_new_graph (0, nullptr);
453 xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
454 xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
455 recursiveXBTGraphExtraction (ret, nodes, edges, simgrid::s4u::Engine::instance()->rootAs(), PJ_container_get_root());
456 xbt_dict_free (&nodes);
457 xbt_dict_free (&edges);
461 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
463 unsigned int cursor = 0;
464 xbt_node_t node = nullptr;
465 xbt_edge_t edge = nullptr;
466 FILE *file = nullptr;
468 file = fopen(filename, "w");
469 xbt_assert(file, "Failed to open %s \n", filename);
472 fprintf(file, "digraph test {\n");
474 fprintf(file, "graph test {\n");
476 fprintf(file, " graph [overlap=scale]\n");
478 fprintf(file, " node [shape=box, style=filled]\n");
480 " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
482 xbt_dynar_foreach(g->nodes, cursor, node) {
483 fprintf(file, " \"%s\";\n", instr_node_name(node));
485 xbt_dynar_foreach(g->edges, cursor, edge) {
486 const char *src_s = instr_node_name (edge->src);
487 const char *dst_s = instr_node_name (edge->dst);
489 fprintf(file, " \"%s\" -> \"%s\";\n", src_s, dst_s);
491 fprintf(file, " \"%s\" -- \"%s\";\n", src_s, dst_s);
493 fprintf(file, "}\n");