Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'toufic' of github.com:Takishipp/simgrid
[simgrid.git] / src / surf / instr_routing.cpp
1 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/instr/instr_private.h"
7
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"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
17
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 */
20
21 static const char *instr_node_name (xbt_node_t node)
22 {
23   void *data = xbt_graph_node_get_data(node);
24   return static_cast<char*>(data);
25 }
26
27 static container_t lowestCommonAncestor (container_t a1, container_t a2)
28 {
29   //this is only an optimization (since most of a1 and a2 share the same parent)
30   if (a1->father == a2->father)
31     return a1->father;
32
33   //create an array with all ancestors of a1
34   std::vector<container_t> ancestors_a1;
35   container_t p = a1->father;
36   while (p){
37     ancestors_a1.push_back(p);
38     p = p->father;
39   }
40
41   //create an array with all ancestors of a2
42   std::vector<container_t> ancestors_a2;
43   p = a2->father;
44   while (p){
45     ancestors_a2.push_back(p);
46     p = p->father;
47   }
48
49   //find the lowest ancestor
50   p = nullptr;
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);
56     if (a1p == a2p){
57       p = a1p;
58     }else{
59       break;
60     }
61     i--;
62     j--;
63   }
64   return p;
65 }
66
67 static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
68 {
69   //ignore loopback
70   if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0){
71     XBT_DEBUG ("  linkContainers: ignoring loopback link");
72     return;
73   }
74
75   //find common father
76   container_t father = lowestCommonAncestor (src, dst);
77   if (!father){
78     xbt_die ("common father unknown, this is a tracing problem");
79   }
80
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);
89       return;
90     }
91     if (xbt_dict_get_or_null (filter, aux2)){
92       XBT_DEBUG ("  linkContainers: already registered %s <-> %s (2)", dst->name, src->name);
93       return;
94     }
95
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);
99   }
100
101   //declare type
102   char link_typename[INSTR_DEFAULT_STR_SIZE];
103   snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s%s-%s%s",
104             father->type->name,
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);
110   }
111
112   //register EDGE types for triva configuration
113   xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), nullptr);
114
115   //create the link
116   static long long counter = 0;
117
118   char key[INSTR_DEFAULT_STR_SIZE];
119   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
120   counter++;
121
122   new StartLinkEvent(SIMIX_get_clock(), father, link_type, src, "topology", key);
123   new EndLinkEvent(SIMIX_get_clock(), father, link_type, dst, "topology", key);
124
125   XBT_DEBUG ("  linkContainers %s <-> %s", src->name, dst->name);
126 }
127
128 static void recursiveGraphExtraction(simgrid::s4u::NetZone* netzone, container_t container, xbt_dict_t filter)
129 {
130   if (!TRACE_platform_topology()){
131     XBT_DEBUG("Graph extraction disabled by user.");
132     return;
133   }
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);
140     }
141   }
142
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;
147
148   xbt_dict_cursor_t cursor = nullptr;
149   char *edge_name;
150
151   static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->getGraph(graph, nodes, edges);
152   xbt_dict_foreach(edges,cursor,edge_name,edge) {
153     linkContainers(
154           PJ_container_get(static_cast<const char*>(edge->src->data)),
155           PJ_container_get(static_cast<const char*>(edge->dst->data)), filter);
156   }
157   xbt_dict_free (&nodes);
158   xbt_dict_free (&edges);
159   xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, nullptr);
160 }
161
162 /*
163  * Callbacks
164  */
165 void sg_instr_AS_begin(sg_platf_AS_cbarg_t AS)
166 {
167   const char*id = AS->id;
168
169   if (PJ_container_get_root() == nullptr){
170     PJ_container_alloc ();
171     PJ_type_alloc();
172     container_t root = PJ_container_new (id, INSTR_AS, nullptr);
173     PJ_container_set_root (root);
174
175     if (TRACE_smpi_is_enabled()) {
176       type_t mpi = PJ_type_get_or_null ("MPI", root->type);
177       if (mpi == nullptr){
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);
182       }
183     }
184
185     if (TRACE_needs_platform()){
186       currentContainer.push_back(root);
187     }
188     return;
189   }
190
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);
195   }
196 }
197
198 void sg_instr_AS_end()
199 {
200   if (TRACE_needs_platform()){
201     currentContainer.pop_back();
202   }
203 }
204
205 static void instr_routing_parse_start_link(simgrid::s4u::Link& link)
206 {
207   if (currentContainer.empty()) // No ongoing parsing. Are you creating the loopback?
208     return;
209   container_t father = currentContainer.back();
210
211   double bandwidth_value = link.bandwidth();
212   double latency_value   = link.latency();
213
214   container_t container = PJ_container_new(link.name(), INSTR_LINK, father);
215
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);
220     }
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);
224     }
225     new SetVariableEvent(0, container, bandwidth, bandwidth_value);
226     new SetVariableEvent(0, container, latency, latency_value);
227   }
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);
232     }
233   }
234 }
235
236 void sg_instr_new_host(simgrid::s4u::Host& host)
237 {
238   container_t father = currentContainer.back();
239   container_t container = PJ_container_new(host.cname(), INSTR_HOST, father);
240
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);
245     }
246
247     double current_speed_state = host.speed();
248     new SetVariableEvent (0, container, speed, current_speed_state);
249   }
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);
254     }
255   }
256
257   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
258     type_t mpi = PJ_type_get_or_null ("MPI", container->type);
259     if (mpi == nullptr){
260       mpi = PJ_type_container_new("MPI", container->type);
261       PJ_type_state_new ("MPI_STATE", mpi);
262     }
263   }
264
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);
277     }
278   }
279
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);
292     }
293   }
294
295 }
296
297 void sg_instr_new_router(const char* name)
298 {
299   if (TRACE_is_enabled() && TRACE_needs_platform()) {
300     container_t father = currentContainer.back();
301     PJ_container_new(name, INSTR_ROUTER, father);
302   }
303 }
304
305 static void instr_routing_parse_end_platform ()
306 {
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);
315 }
316
317 void instr_routing_define_callbacks ()
318 {
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())
322     return;
323   simgrid::s4u::Link::onCreation.connect(instr_routing_parse_start_link);
324   simgrid::s4u::onPlatformCreated.connect(instr_routing_parse_end_platform);
325 }
326
327 /*
328  * user categories support
329  */
330 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
331 {
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);
336   }
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);
341   }
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);
346   }
347   xbt_dict_cursor_t cursor = nullptr;
348   type_t child_type;
349   char *name;
350   xbt_dict_foreach(root->children, cursor, name, child_type) {
351     recursiveNewVariableType (new_typename, color, child_type);
352   }
353 }
354
355 void instr_new_variable_type (const char *new_typename, const char *color)
356 {
357   recursiveNewVariableType (new_typename, color, PJ_type_get_root());
358 }
359
360 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
361 {
362   if (!strcmp (root->name, father_type)){
363     PJ_type_variable_new (new_typename, color, root);
364   }
365   xbt_dict_cursor_t cursor = nullptr;
366   type_t child_type;
367   char *name;
368   xbt_dict_foreach(root->children, cursor, name, child_type) {
369     recursiveNewUserVariableType (father_type, new_typename, color, child_type);
370   }
371 }
372
373 void instr_new_user_variable_type  (const char *father_type, const char *new_typename, const char *color)
374 {
375   recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
376 }
377
378 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
379 {
380   if (!strcmp (root->name, father_type)){
381     PJ_type_state_new (new_typename, root);
382   }
383   xbt_dict_cursor_t cursor = nullptr;
384   type_t child_type;
385   char *name;
386   xbt_dict_foreach(root->children, cursor, name, child_type) {
387     recursiveNewUserStateType (father_type, new_typename, child_type);
388   }
389 }
390
391 void instr_new_user_state_type (const char *father_type, const char *new_typename)
392 {
393   recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
394 }
395
396 static void recursiveNewValueForUserStateType (const char *type_name, const char *value, const char *color, type_t root)
397 {
398   if (!strcmp (root->name, type_name)){
399     PJ_value_new (value, color, root);
400   }
401   xbt_dict_cursor_t cursor = nullptr;
402   type_t child_type;
403   char *name;
404   xbt_dict_foreach(root->children, cursor, name, child_type) {
405     recursiveNewValueForUserStateType (type_name, value, color, child_type);
406   }
407 }
408
409 void instr_new_value_for_user_state_type (const char *type_name, const char *value, const char *color)
410 {
411   recursiveNewValueForUserStateType (type_name, value, color, PJ_type_get_root());
412 }
413
414 int instr_platform_traced ()
415 {
416   return platform_created;
417 }
418
419 #define GRAPHICATOR_SUPPORT_FUNCTIONS
420
421 static void recursiveXBTGraphExtraction(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges, sg_netzone_t netzone,
422                                         container_t container)
423 {
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);
429     }
430   }
431
432   static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->getGraph(graph, nodes, edges);
433 }
434
435 xbt_graph_t instr_routing_platform_graph ()
436 {
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);
443   return ret;
444 }
445
446 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
447 {
448   unsigned int cursor = 0;
449   xbt_node_t node = nullptr;
450   xbt_edge_t edge = nullptr;
451
452   FILE *file = fopen(filename, "w");
453   xbt_assert(file, "Failed to open %s \n", filename);
454
455   if (g->directed)
456     fprintf(file, "digraph test {\n");
457   else
458     fprintf(file, "graph test {\n");
459
460   fprintf(file, "  graph [overlap=scale]\n");
461
462   fprintf(file, "  node [shape=box, style=filled]\n");
463   fprintf(file, "  node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
464
465   xbt_dynar_foreach(g->nodes, cursor, node) {
466     fprintf(file, "  \"%s\";\n", instr_node_name(node));
467   }
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);
471     if (g->directed)
472       fprintf(file, "  \"%s\" -> \"%s\";\n", src_s, dst_s);
473     else
474       fprintf(file, "  \"%s\" -- \"%s\";\n", src_s, dst_s);
475   }
476   fprintf(file, "}\n");
477   fclose(file);
478 }