Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Those damn VM keep getting in our way
[simgrid.git] / src / surf / instr_routing.cpp
1 /* Copyright (c) 2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "src/instr/instr_private.h"
8
9 #include "simgrid/s4u/engine.hpp"
10 #include "simgrid/s4u/host.hpp"
11
12 #include "src/kernel/routing/NetZoneImpl.hpp"
13 #include "src/surf/network_interface.hpp"
14 #include "src/surf/xml/platf_private.hpp"
15 #include "surf/surf.h"
16 #include "xbt/graph.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
19
20 static int platform_created = 0;            /* indicate whether the platform file has been traced */
21 static std::vector<container_t> currentContainer; /* push and pop, used only in creation */
22
23 static const char *instr_node_name (xbt_node_t node)
24 {
25   void *data = xbt_graph_node_get_data(node);
26   return static_cast<char*>(data);
27 }
28
29 static container_t lowestCommonAncestor (container_t a1, container_t a2)
30 {
31   //this is only an optimization (since most of a1 and a2 share the same parent)
32   if (a1->father == a2->father)
33     return a1->father;
34
35   //create an array with all ancestors of a1
36   std::vector<container_t> ancestors_a1;
37   container_t p = a1->father;
38   while (p){
39     ancestors_a1.push_back(p);
40     p = p->father;
41   }
42
43   //create an array with all ancestors of a2
44   std::vector<container_t> ancestors_a2;
45   p = a2->father;
46   while (p){
47     ancestors_a2.push_back(p);
48     p = p->father;
49   }
50
51   //find the lowest ancestor
52   p = nullptr;
53   int i = ancestors_a1.size() - 1;
54   int j = ancestors_a2.size() - 1;
55   while (i >= 0 && j >= 0){
56     container_t a1p = ancestors_a1.at(i);
57     container_t a2p = ancestors_a2.at(j);
58     if (a1p == a2p){
59       p = a1p;
60     }else{
61       break;
62     }
63     i--;
64     j--;
65   }
66   return p;
67 }
68
69 static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
70 {
71   //ignore loopback
72   if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0){
73     XBT_DEBUG ("  linkContainers: ignoring loopback link");
74     return;
75   }
76
77   //find common father
78   container_t father = lowestCommonAncestor (src, dst);
79   if (!father){
80     xbt_die ("common father unknown, this is a tracing problem");
81   }
82
83   if (filter != nullptr){
84     //check if we already register this pair (we only need one direction)
85     char aux1[INSTR_DEFAULT_STR_SIZE];
86     char 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);
91       return;
92     }
93     if (xbt_dict_get_or_null (filter, aux2)){
94       XBT_DEBUG ("  linkContainers: already registered %s <-> %s (2)", dst->name, src->name);
95       return;
96     }
97
98     //ok, not found, register it
99     xbt_dict_set (filter, aux1, xbt_strdup ("1"), nullptr);
100     xbt_dict_set (filter, aux2, xbt_strdup ("1"), nullptr);
101   }
102
103   //declare type
104   char link_typename[INSTR_DEFAULT_STR_SIZE];
105   snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s%s-%s%s",
106             father->type->name,
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 == nullptr){
111     link_type = PJ_type_link_new (link_typename, father->type, src->type, dst->type);
112   }
113
114   //register EDGE types for triva configuration
115   xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), nullptr);
116
117   //create the link
118   static long long counter = 0;
119
120   char key[INSTR_DEFAULT_STR_SIZE];
121   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
122   counter++;
123
124   new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "topology", key);
125   new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "topology", key);
126
127   XBT_DEBUG ("  linkContainers %s <-> %s", src->name, dst->name);
128 }
129
130 static void recursiveGraphExtraction(simgrid::s4u::NetZone* netzone, container_t container, xbt_dict_t filter)
131 {
132   if (!TRACE_platform_topology()){
133     XBT_DEBUG("Graph extraction disabled by user.");
134     return;
135   }
136   XBT_DEBUG("Graph extraction for NetZone = %s", netzone->name());
137   if (!xbt_dict_is_empty(netzone->children())) {
138     xbt_dict_cursor_t cursor = nullptr;
139     sg_netzone_t nz_son;
140     char *child_name;
141     //bottom-up recursion
142     xbt_dict_foreach (netzone->children(), cursor, child_name, nz_son) {
143       container_t child_container = static_cast<container_t>(xbt_dict_get(container->children, nz_son->name()));
144       recursiveGraphExtraction(nz_son, child_container, filter);
145     }
146   }
147
148   xbt_graph_t graph = xbt_graph_new_graph (0, nullptr);
149   xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
150   xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
151   xbt_edge_t edge = nullptr;
152
153   xbt_dict_cursor_t cursor = nullptr;
154   char *edge_name;
155
156   static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->getGraph(graph, nodes, edges);
157   xbt_dict_foreach(edges,cursor,edge_name,edge) {
158     linkContainers(
159           PJ_container_get(static_cast<const char*>(edge->src->data)),
160           PJ_container_get(static_cast<const char*>(edge->dst->data)), filter);
161   }
162   xbt_dict_free (&nodes);
163   xbt_dict_free (&edges);
164   xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, nullptr);
165 }
166
167 /*
168  * Callbacks
169  */
170 void sg_instr_AS_begin(sg_platf_AS_cbarg_t AS)
171 {
172   const char*id = AS->id;
173
174   if (PJ_container_get_root() == nullptr){
175     PJ_container_alloc ();
176     PJ_type_alloc();
177     container_t root = PJ_container_new (id, INSTR_AS, nullptr);
178     PJ_container_set_root (root);
179
180     if (TRACE_smpi_is_enabled()) {
181       type_t mpi = PJ_type_get_or_null ("MPI", root->type);
182       if (mpi == nullptr){
183         mpi = PJ_type_container_new("MPI", root->type);
184         if (!TRACE_smpi_is_grouped())
185           PJ_type_state_new ("MPI_STATE", mpi);
186         PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
187       }
188     }
189
190     if (TRACE_needs_platform()){
191       currentContainer.push_back(root);
192     }
193     return;
194   }
195
196   if (TRACE_needs_platform()){
197     container_t father = currentContainer.back();
198     container_t container = PJ_container_new (id, INSTR_AS, father);
199     currentContainer.push_back(container);
200   }
201 }
202
203 void sg_instr_AS_end()
204 {
205   if (TRACE_needs_platform()){
206     currentContainer.pop_back();
207   }
208 }
209
210 static void instr_routing_parse_start_link(simgrid::s4u::Link& link)
211 {
212   if (currentContainer.empty()) // No ongoing parsing. Are you creating the loopback?
213     return;
214   container_t father = currentContainer.back();
215
216   double bandwidth_value = link.bandwidth();
217   double latency_value   = link.latency();
218
219   container_t container = PJ_container_new(link.name(), INSTR_LINK, father);
220
221   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_link())) {
222     type_t bandwidth = PJ_type_get_or_null("bandwidth", container->type);
223     if (bandwidth == nullptr) {
224       bandwidth = PJ_type_variable_new("bandwidth", nullptr, container->type);
225     }
226     type_t latency = PJ_type_get_or_null("latency", container->type);
227     if (latency == nullptr) {
228       latency = PJ_type_variable_new("latency", nullptr, container->type);
229     }
230     new_pajeSetVariable(0, container, bandwidth, bandwidth_value);
231     new_pajeSetVariable(0, container, latency, latency_value);
232   }
233   if (TRACE_uncategorized()) {
234     type_t bandwidth_used = PJ_type_get_or_null("bandwidth_used", container->type);
235     if (bandwidth_used == nullptr) {
236       PJ_type_variable_new("bandwidth_used", "0.5 0.5 0.5", container->type);
237     }
238   }
239 }
240
241 void sg_instr_new_host(simgrid::s4u::Host& host)
242 {
243   container_t father = currentContainer.back();
244   container_t container = PJ_container_new(host.cname(), INSTR_HOST, father);
245
246   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_speed())) {
247     type_t speed = PJ_type_get_or_null ("power", container->type);
248     if (speed == nullptr){
249       speed = PJ_type_variable_new ("power", nullptr, container->type);
250     }
251
252     double current_speed_state = host.getPstateSpeedCurrent();
253     new_pajeSetVariable (0, container, speed, current_speed_state);
254   }
255   if (TRACE_uncategorized()){
256     type_t speed_used = PJ_type_get_or_null ("power_used", container->type);
257     if (speed_used == nullptr){
258       PJ_type_variable_new ("power_used", "0.5 0.5 0.5", container->type);
259     }
260   }
261
262   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
263     type_t mpi = PJ_type_get_or_null ("MPI", container->type);
264     if (mpi == nullptr){
265       mpi = PJ_type_container_new("MPI", container->type);
266       PJ_type_state_new ("MPI_STATE", mpi);
267     }
268   }
269
270   if (TRACE_msg_process_is_enabled()) {
271     type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", container->type);
272     if (msg_process == nullptr){
273       msg_process = PJ_type_container_new("MSG_PROCESS", container->type);
274       type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
275       PJ_value_new ("suspend", "1 0 1", state);
276       PJ_value_new ("sleep", "1 1 0", state);
277       PJ_value_new ("receive", "1 0 0", state);
278       PJ_value_new ("send", "0 0 1", state);
279       PJ_value_new ("task_execute", "0 1 1", state);
280       PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
281       PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
282     }
283   }
284
285   if (TRACE_msg_vm_is_enabled()) {
286     type_t msg_vm = PJ_type_get_or_null ("MSG_VM", container->type);
287     if (msg_vm == nullptr){
288       msg_vm = PJ_type_container_new("MSG_VM", container->type);
289       type_t state = PJ_type_state_new ("MSG_VM_STATE", msg_vm);
290       PJ_value_new ("suspend", "1 0 1", state);
291       PJ_value_new ("sleep", "1 1 0", state);
292       PJ_value_new ("receive", "1 0 0", state);
293       PJ_value_new ("send", "0 0 1", state);
294       PJ_value_new ("task_execute", "0 1 1", state);
295       PJ_type_link_new ("MSG_VM_LINK", PJ_type_get_root(), msg_vm, msg_vm);
296       PJ_type_link_new ("MSG_VM_PROCESS_LINK", PJ_type_get_root(), msg_vm, msg_vm);
297     }
298   }
299
300 }
301
302 void sg_instr_new_router(const char* name)
303 {
304   if (TRACE_is_enabled() && TRACE_needs_platform()) {
305     container_t father = currentContainer.back();
306     PJ_container_new(name, INSTR_ROUTER, father);
307   }
308 }
309
310 static void instr_routing_parse_end_platform ()
311 {
312   currentContainer.clear();
313   xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free_f);
314   XBT_DEBUG ("Starting graph extraction.");
315   recursiveGraphExtraction(simgrid::s4u::Engine::instance()->netRoot(), PJ_container_get_root(), filter);
316   XBT_DEBUG ("Graph extraction finished.");
317   xbt_dict_free(&filter);
318   platform_created = 1;
319   TRACE_paje_dump_buffer(1);
320 }
321
322 void instr_routing_define_callbacks ()
323 {
324   //always need the call backs to ASes (we need only the root AS),
325   //to create the rootContainer and the rootType properly
326   if (!TRACE_is_enabled() || !TRACE_needs_platform())
327     return;
328   simgrid::s4u::Link::onCreation.connect(instr_routing_parse_start_link);
329   simgrid::s4u::onPlatformCreated.connect(instr_routing_parse_end_platform);
330 }
331
332 /*
333  * user categories support
334  */
335 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
336 {
337   if (!strcmp (root->name, "HOST")){
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);
341   }
342   if (!strcmp (root->name, "MSG_VM")){
343     char tnstr[INSTR_DEFAULT_STR_SIZE];
344     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
345     PJ_type_variable_new (tnstr, color, root);
346   }
347  if (!strcmp (root->name, "LINK")){
348     char tnstr[INSTR_DEFAULT_STR_SIZE];
349     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
350     PJ_type_variable_new (tnstr, color, root);
351   }
352   xbt_dict_cursor_t cursor = nullptr;
353   type_t child_type;
354   char *name;
355   xbt_dict_foreach(root->children, cursor, name, child_type) {
356     recursiveNewVariableType (new_typename, color, child_type);
357   }
358 }
359
360 void instr_new_variable_type (const char *new_typename, const char *color)
361 {
362   recursiveNewVariableType (new_typename, color, PJ_type_get_root());
363 }
364
365 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
366 {
367   if (!strcmp (root->name, father_type)){
368     PJ_type_variable_new (new_typename, color, root);
369   }
370   xbt_dict_cursor_t cursor = nullptr;
371   type_t child_type;
372   char *name;
373   xbt_dict_foreach(root->children, cursor, name, child_type) {
374     recursiveNewUserVariableType (father_type, new_typename, color, child_type);
375   }
376 }
377
378 void instr_new_user_variable_type  (const char *father_type, const char *new_typename, const char *color)
379 {
380   recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
381 }
382
383 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
384 {
385   if (!strcmp (root->name, father_type)){
386     PJ_type_state_new (new_typename, root);
387   }
388   xbt_dict_cursor_t cursor = nullptr;
389   type_t child_type;
390   char *name;
391   xbt_dict_foreach(root->children, cursor, name, child_type) {
392     recursiveNewUserStateType (father_type, new_typename, child_type);
393   }
394 }
395
396 void instr_new_user_state_type (const char *father_type, const char *new_typename)
397 {
398   recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
399 }
400
401 static void recursiveNewValueForUserStateType (const char *type_name, const char *value, const char *color, type_t root)
402 {
403   if (!strcmp (root->name, type_name)){
404     PJ_value_new (value, color, root);
405   }
406   xbt_dict_cursor_t cursor = nullptr;
407   type_t child_type;
408   char *name;
409   xbt_dict_foreach(root->children, cursor, name, child_type) {
410     recursiveNewValueForUserStateType (type_name, value, color, child_type);
411   }
412 }
413
414 void instr_new_value_for_user_state_type (const char *type_name, const char *value, const char *color)
415 {
416   recursiveNewValueForUserStateType (type_name, value, color, PJ_type_get_root());
417 }
418
419 int instr_platform_traced ()
420 {
421   return platform_created;
422 }
423
424 #define GRAPHICATOR_SUPPORT_FUNCTIONS
425
426 static void recursiveXBTGraphExtraction(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges, sg_netzone_t netzone,
427                                         container_t container)
428 {
429   if (!xbt_dict_is_empty(netzone->children())) {
430     xbt_dict_cursor_t cursor = nullptr;
431     sg_netzone_t netzone_child;
432     char *child_name;
433     //bottom-up recursion
434     xbt_dict_foreach (netzone->children(), cursor, child_name, netzone_child) {
435       container_t child_container = static_cast<container_t>(xbt_dict_get(container->children, netzone_child->name()));
436       recursiveXBTGraphExtraction(graph, nodes, edges, netzone_child, child_container);
437     }
438   }
439
440   static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->getGraph(graph, nodes, edges);
441 }
442
443 xbt_graph_t instr_routing_platform_graph ()
444 {
445   xbt_graph_t ret = xbt_graph_new_graph (0, nullptr);
446   xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
447   xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
448   recursiveXBTGraphExtraction(ret, nodes, edges, simgrid::s4u::Engine::instance()->netRoot(), PJ_container_get_root());
449   xbt_dict_free (&nodes);
450   xbt_dict_free (&edges);
451   return ret;
452 }
453
454 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
455 {
456   unsigned int cursor = 0;
457   xbt_node_t node = nullptr;
458   xbt_edge_t edge = nullptr;
459
460   FILE *file = fopen(filename, "w");
461   xbt_assert(file, "Failed to open %s \n", filename);
462
463   if (g->directed)
464     fprintf(file, "digraph test {\n");
465   else
466     fprintf(file, "graph test {\n");
467
468   fprintf(file, "  graph [overlap=scale]\n");
469
470   fprintf(file, "  node [shape=box, style=filled]\n");
471   fprintf(file, "  node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
472
473   xbt_dynar_foreach(g->nodes, cursor, node) {
474     fprintf(file, "  \"%s\";\n", instr_node_name(node));
475   }
476   xbt_dynar_foreach(g->edges, cursor, edge) {
477     const char *src_s = instr_node_name (edge->src);
478     const char *dst_s = instr_node_name (edge->dst);
479     if (g->directed)
480       fprintf(file, "  \"%s\" -> \"%s\";\n", src_s, dst_s);
481     else
482       fprintf(file, "  \"%s\" -- \"%s\";\n", src_s, dst_s);
483   }
484   fprintf(file, "}\n");
485   fclose(file);
486 }