Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix MC which uses simgrid::xbt::string
[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/kernel/routing/NetPoint.hpp"
12 #include "src/surf/network_interface.hpp"
13 #include "src/surf/xml/platf_private.hpp"
14 #include "surf/surf.h"
15 #include "xbt/graph.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
18
19 static int platform_created = 0;            /* indicate whether the platform file has been traced */
20 static std::vector<container_t> currentContainer; /* push and pop, used only in creation */
21
22 static const char *instr_node_name (xbt_node_t node)
23 {
24   void *data = xbt_graph_node_get_data(node);
25   return static_cast<char*>(data);
26 }
27
28 static container_t lowestCommonAncestor (container_t a1, container_t a2)
29 {
30   //this is only an optimization (since most of a1 and a2 share the same parent)
31   if (a1->father == a2->father)
32     return a1->father;
33
34   //create an array with all ancestors of a1
35   std::vector<container_t> ancestors_a1;
36   container_t p = a1->father;
37   while (p){
38     ancestors_a1.push_back(p);
39     p = p->father;
40   }
41
42   //create an array with all ancestors of a2
43   std::vector<container_t> ancestors_a2;
44   p = a2->father;
45   while (p){
46     ancestors_a2.push_back(p);
47     p = p->father;
48   }
49
50   //find the lowest ancestor
51   p = nullptr;
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);
57     if (a1p == a2p){
58       p = a1p;
59     }else{
60       break;
61     }
62     i--;
63     j--;
64   }
65   return p;
66 }
67
68 static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
69 {
70   //ignore loopback
71   if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0){
72     XBT_DEBUG ("  linkContainers: ignoring loopback link");
73     return;
74   }
75
76   //find common father
77   container_t father = lowestCommonAncestor (src, dst);
78   if (not father) {
79     xbt_die ("common father unknown, this is a tracing problem");
80   }
81
82   if (filter != nullptr){
83     //check if we already register this pair (we only need one direction)
84     char aux1[INSTR_DEFAULT_STR_SIZE];
85     char 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);
90       return;
91     }
92     if (xbt_dict_get_or_null (filter, aux2)){
93       XBT_DEBUG ("  linkContainers: already registered %s <-> %s (2)", dst->name, src->name);
94       return;
95     }
96
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);
100   }
101
102   //declare type
103   char link_typename[INSTR_DEFAULT_STR_SIZE];
104   snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s%s-%s%s",
105             father->type->name,
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);
111   }
112
113   //register EDGE types for triva configuration
114   xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), nullptr);
115
116   //create the link
117   static long long counter = 0;
118
119   char key[INSTR_DEFAULT_STR_SIZE];
120   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
121   counter++;
122
123   new StartLinkEvent(SIMIX_get_clock(), father, link_type, src, "topology", key);
124   new EndLinkEvent(SIMIX_get_clock(), father, link_type, dst, "topology", key);
125
126   XBT_DEBUG ("  linkContainers %s <-> %s", src->name, dst->name);
127 }
128
129 static void recursiveGraphExtraction(simgrid::s4u::NetZone* netzone, container_t container, xbt_dict_t filter)
130 {
131   if (not TRACE_platform_topology()) {
132     XBT_DEBUG("Graph extraction disabled by user.");
133     return;
134   }
135   XBT_DEBUG("Graph extraction for NetZone = %s", netzone->getCname());
136   if (not netzone->getChildren()->empty()) {
137     //bottom-up recursion
138     for (auto nz_son : *netzone->getChildren()) {
139       container_t child_container = static_cast<container_t>(xbt_dict_get(container->children, nz_son->getCname()));
140       recursiveGraphExtraction(nz_son, child_container, filter);
141     }
142   }
143
144   xbt_graph_t graph = xbt_graph_new_graph (0, nullptr);
145   xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
146   xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
147   xbt_edge_t edge = nullptr;
148
149   xbt_dict_cursor_t cursor = nullptr;
150   char *edge_name;
151
152   static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->getGraph(graph, nodes, edges);
153   xbt_dict_foreach(edges,cursor,edge_name,edge) {
154     linkContainers(
155           PJ_container_get(static_cast<const char*>(edge->src->data)),
156           PJ_container_get(static_cast<const char*>(edge->dst->data)), filter);
157   }
158   xbt_dict_free (&nodes);
159   xbt_dict_free (&edges);
160   xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, nullptr);
161 }
162
163 /*
164  * Callbacks
165  */
166 static void sg_instr_AS_begin(simgrid::s4u::NetZone& netzone)
167 {
168   const char* id = netzone.getCname();
169
170   if (PJ_container_get_root() == nullptr){
171     PJ_container_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 (not 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 static void sg_instr_AS_end(simgrid::s4u::NetZone& /*netzone*/)
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()) && (not 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 static void sg_instr_new_host(simgrid::s4u::Host& host)
237 {
238   container_t father = currentContainer.back();
239   container_t container = PJ_container_new(host.getCname(), INSTR_HOST, father);
240
241   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (not 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.getSpeed();
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       value PJ_value("suspend", "1 0 1", state);
271       value::get_or_new("sleep", "1 1 0", state);
272       value::get_or_new("receive", "1 0 0", state);
273       value::get_or_new("send", "0 0 1", state);
274       value::get_or_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       value PJ_value("suspend", "1 0 1", state);
286       value::get_or_new("sleep", "1 1 0", state);
287       value::get_or_new("receive", "1 0 0", state);
288       value::get_or_new("send", "0 0 1", state);
289       value::get_or_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 static void sg_instr_new_router(simgrid::kernel::routing::NetPoint * netpoint)
298 {
299   if (not netpoint->isRouter())
300     return;
301   if (TRACE_is_enabled() && TRACE_needs_platform()) {
302     container_t father = currentContainer.back();
303     PJ_container_new(netpoint->cname(), INSTR_ROUTER, father);
304   }
305 }
306
307 static void instr_routing_parse_end_platform ()
308 {
309   currentContainer.clear();
310   xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free_f);
311   XBT_DEBUG ("Starting graph extraction.");
312   recursiveGraphExtraction(simgrid::s4u::Engine::getInstance()->getNetRoot(), PJ_container_get_root(), filter);
313   XBT_DEBUG ("Graph extraction finished.");
314   xbt_dict_free(&filter);
315   platform_created = 1;
316   TRACE_paje_dump_buffer(1);
317 }
318
319 void instr_routing_define_callbacks ()
320 {
321   //always need the call backs to ASes (we need only the root AS),
322   //to create the rootContainer and the rootType properly
323   if (not TRACE_is_enabled())
324     return;
325   if (TRACE_needs_platform()) {
326     simgrid::s4u::Link::onCreation.connect(instr_routing_parse_start_link);
327     simgrid::s4u::onPlatformCreated.connect(instr_routing_parse_end_platform);
328     simgrid::s4u::Host::onCreation.connect(sg_instr_new_host);
329   }
330   simgrid::s4u::NetZone::onCreation.connect(sg_instr_AS_begin);
331   simgrid::s4u::NetZone::onSeal.connect(sg_instr_AS_end);
332   simgrid::kernel::routing::NetPoint::onCreation.connect(&sg_instr_new_router);
333 }
334 /*
335  * user categories support
336  */
337 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
338 {
339   if (not strcmp(root->name, "HOST")) {
340     char tnstr[INSTR_DEFAULT_STR_SIZE];
341     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
342     PJ_type_variable_new (tnstr, color, root);
343   }
344   if (not strcmp(root->name, "MSG_VM")) {
345     char tnstr[INSTR_DEFAULT_STR_SIZE];
346     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
347     PJ_type_variable_new (tnstr, color, root);
348   }
349   if (not strcmp(root->name, "LINK")) {
350     char tnstr[INSTR_DEFAULT_STR_SIZE];
351     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
352     PJ_type_variable_new (tnstr, color, root);
353   }
354   xbt_dict_cursor_t cursor = nullptr;
355   type_t child_type;
356   char *name;
357   xbt_dict_foreach(root->children, cursor, name, child_type) {
358     recursiveNewVariableType (new_typename, color, child_type);
359   }
360 }
361
362 void instr_new_variable_type (const char *new_typename, const char *color)
363 {
364   recursiveNewVariableType (new_typename, color, PJ_type_get_root());
365 }
366
367 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
368 {
369   if (not strcmp(root->name, father_type)) {
370     PJ_type_variable_new (new_typename, color, root);
371   }
372   xbt_dict_cursor_t cursor = nullptr;
373   type_t child_type;
374   char *name;
375   xbt_dict_foreach(root->children, cursor, name, child_type) {
376     recursiveNewUserVariableType (father_type, new_typename, color, child_type);
377   }
378 }
379
380 void instr_new_user_variable_type  (const char *father_type, const char *new_typename, const char *color)
381 {
382   recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
383 }
384
385 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
386 {
387   if (not strcmp(root->name, father_type)) {
388     PJ_type_state_new (new_typename, root);
389   }
390   xbt_dict_cursor_t cursor = nullptr;
391   type_t child_type;
392   char *name;
393   xbt_dict_foreach(root->children, cursor, name, child_type) {
394     recursiveNewUserStateType (father_type, new_typename, child_type);
395   }
396 }
397
398 void instr_new_user_state_type (const char *father_type, const char *new_typename)
399 {
400   recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
401 }
402
403 static void recursiveNewValueForUserStateType(const char* type_name, const char* val, const char* color, type_t root)
404 {
405   if (not strcmp(root->name, type_name)) {
406     value PJ_value(val, color, root);
407   }
408   xbt_dict_cursor_t cursor = nullptr;
409   type_t child_type;
410   char *name;
411   xbt_dict_foreach(root->children, cursor, name, child_type) {
412     recursiveNewValueForUserStateType(type_name, val, color, child_type);
413   }
414 }
415
416 void instr_new_value_for_user_state_type (const char *type_name, const char *value, const char *color)
417 {
418   recursiveNewValueForUserStateType (type_name, value, color, PJ_type_get_root());
419 }
420
421 int instr_platform_traced ()
422 {
423   return platform_created;
424 }
425
426 #define GRAPHICATOR_SUPPORT_FUNCTIONS
427
428 static void recursiveXBTGraphExtraction(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges, sg_netzone_t netzone,
429                                         container_t container)
430 {
431   if (not netzone->getChildren()->empty()) {
432     //bottom-up recursion
433     for (auto netzone_child : *netzone->getChildren()) {
434       container_t child_container =
435           static_cast<container_t>(xbt_dict_get(container->children, netzone_child->getCname()));
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::getInstance()->getNetRoot(),
449                               PJ_container_get_root());
450   xbt_dict_free (&nodes);
451   xbt_dict_free (&edges);
452   return ret;
453 }
454
455 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
456 {
457   unsigned int cursor = 0;
458   xbt_node_t node = nullptr;
459   xbt_edge_t edge = nullptr;
460
461   FILE *file = fopen(filename, "w");
462   xbt_assert(file, "Failed to open %s \n", filename);
463
464   if (g->directed)
465     fprintf(file, "digraph test {\n");
466   else
467     fprintf(file, "graph test {\n");
468
469   fprintf(file, "  graph [overlap=scale]\n");
470
471   fprintf(file, "  node [shape=box, style=filled]\n");
472   fprintf(file, "  node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
473
474   xbt_dynar_foreach(g->nodes, cursor, node) {
475     fprintf(file, "  \"%s\";\n", instr_node_name(node));
476   }
477   xbt_dynar_foreach(g->edges, cursor, edge) {
478     const char *src_s = instr_node_name (edge->src);
479     const char *dst_s = instr_node_name (edge->dst);
480     if (g->directed)
481       fprintf(file, "  \"%s\" -> \"%s\";\n", src_s, dst_s);
482     else
483       fprintf(file, "  \"%s\" -- \"%s\";\n", src_s, dst_s);
484   }
485   fprintf(file, "}\n");
486   fclose(file);
487 }