Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5401f123ab56b3520bfeac9e09c0c4b57d4c802f
[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 #include "src/kernel/routing/NetZoneImpl.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   char *str = (char*)data;
26   return str;
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) return a1->father;
33
34   //create an array with all ancestors of a1
35   std::vector<container_t> ancestors_a1;
36   container_t p;
37   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], 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   new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "topology", key);
122   new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "topology", key);
123
124   XBT_DEBUG ("  linkContainers %s <-> %s", src->name, dst->name);
125 }
126
127 static void recursiveGraphExtraction(simgrid::s4u::NetZone* netzone, container_t container, xbt_dict_t filter)
128 {
129   if (!TRACE_platform_topology()){
130     XBT_DEBUG("Graph extraction disabled by user.");
131     return;
132   }
133   XBT_DEBUG("Graph extraction for NetZone = %s", netzone->name());
134   if (!xbt_dict_is_empty(netzone->children())) {
135     xbt_dict_cursor_t cursor = nullptr;
136     NetZone_t nz_son;
137     char *child_name;
138     //bottom-up recursion
139     xbt_dict_foreach (netzone->children(), cursor, child_name, nz_son) {
140       container_t child_container = (container_t)xbt_dict_get(container->children, nz_son->name());
141       recursiveGraphExtraction(nz_son, child_container, filter);
142     }
143   }
144
145   {
146     xbt_graph_t graph = xbt_graph_new_graph (0, nullptr);
147     xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
148     xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
149     xbt_edge_t edge = nullptr;
150
151     xbt_dict_cursor_t cursor = nullptr;
152     char *edge_name;
153
154     static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->getGraph(graph, nodes, edges);
155     xbt_dict_foreach(edges,cursor,edge_name,edge) {
156         linkContainers(
157           PJ_container_get((const char*) edge->src->data),
158           PJ_container_get((const char*) edge->dst->data), filter);
159     }
160     xbt_dict_free (&nodes);
161     xbt_dict_free (&edges);
162     xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, nullptr);
163   }
164 }
165
166 /*
167  * Callbacks
168  */
169 void sg_instr_AS_begin(sg_platf_AS_cbarg_t AS)
170 {
171   const char*id = AS->id;
172
173   if (PJ_container_get_root() == nullptr){
174     PJ_container_alloc ();
175     PJ_type_alloc();
176     container_t root = PJ_container_new (id, INSTR_AS, nullptr);
177     PJ_container_set_root (root);
178
179     if (TRACE_smpi_is_enabled()) {
180       type_t mpi = PJ_type_get_or_null ("MPI", root->type);
181       if (mpi == nullptr){
182         mpi = PJ_type_container_new("MPI", root->type);
183         if (!TRACE_smpi_is_grouped()) PJ_type_state_new ("MPI_STATE", mpi);
184         PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
185       }
186     }
187
188     if (TRACE_needs_platform()){
189       currentContainer.push_back(root);
190     }
191     return;
192   }
193
194   if (TRACE_needs_platform()){
195     container_t father = currentContainer.back();
196     container_t container = PJ_container_new (id, INSTR_AS, father);
197     currentContainer.push_back(container);
198   }
199 }
200
201 void sg_instr_AS_end()
202 {
203   if (TRACE_needs_platform()){
204     currentContainer.pop_back();
205   }
206 }
207
208 static void instr_routing_parse_start_link(simgrid::surf::Link* link)
209 {
210   if (currentContainer.empty()) // No ongoing parsing. Are you creating the loopback?
211     return;
212   container_t father = currentContainer.back();
213
214   double bandwidth_value = link->bandwidth();
215   double latency_value   = link->latency();
216
217   container_t container = PJ_container_new(link->getName(), INSTR_LINK, father);
218
219   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_link())) {
220     type_t bandwidth = PJ_type_get_or_null("bandwidth", container->type);
221     if (bandwidth == nullptr) {
222       bandwidth = PJ_type_variable_new("bandwidth", nullptr, container->type);
223     }
224     type_t latency = PJ_type_get_or_null("latency", container->type);
225     if (latency == nullptr) {
226       latency = PJ_type_variable_new("latency", nullptr, container->type);
227     }
228     new_pajeSetVariable(0, container, bandwidth, bandwidth_value);
229     new_pajeSetVariable(0, container, latency, latency_value);
230   }
231   if (TRACE_uncategorized()) {
232     type_t bandwidth_used = PJ_type_get_or_null("bandwidth_used", container->type);
233     if (bandwidth_used == nullptr) {
234       PJ_type_variable_new("bandwidth_used", "0.5 0.5 0.5", container->type);
235     }
236   }
237 }
238
239 void sg_instr_new_host(simgrid::s4u::Host& host)
240 {
241   container_t father = currentContainer.back();
242   container_t container = PJ_container_new(host.cname(), INSTR_HOST, father);
243
244   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_speed())) {
245     type_t speed = PJ_type_get_or_null ("power", container->type);
246     if (speed == nullptr){
247       speed = PJ_type_variable_new ("power", nullptr, container->type);
248     }
249
250     double current_speed_state = host.getPstateSpeedCurrent();
251     new_pajeSetVariable (0, container, speed, current_speed_state);
252   }
253   if (TRACE_uncategorized()){
254     type_t speed_used = PJ_type_get_or_null ("power_used", container->type);
255     if (speed_used == nullptr){
256       PJ_type_variable_new ("power_used", "0.5 0.5 0.5", container->type);
257     }
258   }
259
260   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
261     type_t mpi = PJ_type_get_or_null ("MPI", container->type);
262     if (mpi == nullptr){
263       mpi = PJ_type_container_new("MPI", container->type);
264       PJ_type_state_new ("MPI_STATE", mpi);
265     }
266   }
267
268   if (TRACE_msg_process_is_enabled()) {
269     type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", container->type);
270     if (msg_process == nullptr){
271       msg_process = PJ_type_container_new("MSG_PROCESS", container->type);
272       type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
273       PJ_value_new ("suspend", "1 0 1", state);
274       PJ_value_new ("sleep", "1 1 0", state);
275       PJ_value_new ("receive", "1 0 0", state);
276       PJ_value_new ("send", "0 0 1", state);
277       PJ_value_new ("task_execute", "0 1 1", state);
278       PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
279       PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
280     }
281   }
282
283   if (TRACE_msg_vm_is_enabled()) {
284     type_t msg_vm = PJ_type_get_or_null ("MSG_VM", container->type);
285     if (msg_vm == nullptr){
286       msg_vm = PJ_type_container_new("MSG_VM", container->type);
287       type_t state = PJ_type_state_new ("MSG_VM_STATE", msg_vm);
288       PJ_value_new ("suspend", "1 0 1", state);
289       PJ_value_new ("sleep", "1 1 0", state);
290       PJ_value_new ("receive", "1 0 0", state);
291       PJ_value_new ("send", "0 0 1", state);
292       PJ_value_new ("task_execute", "0 1 1", state);
293       PJ_type_link_new ("MSG_VM_LINK", PJ_type_get_root(), msg_vm, msg_vm);
294       PJ_type_link_new ("MSG_VM_PROCESS_LINK", PJ_type_get_root(), msg_vm, msg_vm);
295     }
296   }
297
298 }
299
300 void sg_instr_new_router(const char* name)
301 {
302   if (TRACE_is_enabled() && TRACE_needs_platform()) {
303     container_t father = currentContainer.back();
304     PJ_container_new(name, INSTR_ROUTER, father);
305   }
306 }
307
308 static void instr_routing_parse_end_platform ()
309 {
310   currentContainer.clear();
311   xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free_f);
312   XBT_DEBUG ("Starting graph extraction.");
313   recursiveGraphExtraction(simgrid::s4u::Engine::instance()->netRoot(), PJ_container_get_root(), filter);
314   XBT_DEBUG ("Graph extraction finished.");
315   xbt_dict_free(&filter);
316   platform_created = 1;
317   TRACE_paje_dump_buffer(1);
318 }
319
320 void instr_routing_define_callbacks ()
321 {
322   if (!TRACE_is_enabled()) return;
323   //always need the call backs to ASes (we need only the root AS),
324   //to create the rootContainer and the rootType properly
325   if (!TRACE_needs_platform()) return;
326   simgrid::surf::Link::onCreation.connect(instr_routing_parse_start_link);
327   simgrid::surf::on_postparse.connect(instr_routing_parse_end_platform);
328 }
329
330 /*
331  * user categories support
332  */
333 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
334 {
335   if (!strcmp (root->name, "HOST")){
336     char tnstr[INSTR_DEFAULT_STR_SIZE];
337     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
338     PJ_type_variable_new (tnstr, color, root);
339   }
340   if (!strcmp (root->name, "MSG_VM")){
341     char tnstr[INSTR_DEFAULT_STR_SIZE];
342     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
343     PJ_type_variable_new (tnstr, color, root);
344   }
345  if (!strcmp (root->name, "LINK")){
346     char tnstr[INSTR_DEFAULT_STR_SIZE];
347     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
348     PJ_type_variable_new (tnstr, color, root);
349   }
350   xbt_dict_cursor_t cursor = nullptr;
351   type_t child_type;
352   char *name;
353   xbt_dict_foreach(root->children, cursor, name, child_type) {
354     recursiveNewVariableType (new_typename, color, child_type);
355   }
356 }
357
358 void instr_new_variable_type (const char *new_typename, const char *color)
359 {
360   recursiveNewVariableType (new_typename, color, PJ_type_get_root());
361 }
362
363 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
364 {
365   if (!strcmp (root->name, father_type)){
366     PJ_type_variable_new (new_typename, color, root);
367   }
368   xbt_dict_cursor_t cursor = nullptr;
369   type_t child_type;
370   char *name;
371   xbt_dict_foreach(root->children, cursor, name, child_type) {
372     recursiveNewUserVariableType (father_type, new_typename, color, child_type);
373   }
374 }
375
376 void instr_new_user_variable_type  (const char *father_type, const char *new_typename, const char *color)
377 {
378   recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
379 }
380
381 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
382 {
383   if (!strcmp (root->name, father_type)){
384     PJ_type_state_new (new_typename, root);
385   }
386   xbt_dict_cursor_t cursor = nullptr;
387   type_t child_type;
388   char *name;
389   xbt_dict_foreach(root->children, cursor, name, child_type) {
390     recursiveNewUserStateType (father_type, new_typename, child_type);
391   }
392 }
393
394 void instr_new_user_state_type (const char *father_type, const char *new_typename)
395 {
396   recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
397 }
398
399 static void recursiveNewValueForUserStateType (const char *type_name, const char *value, const char *color, type_t root)
400 {
401   if (!strcmp (root->name, type_name)){
402     PJ_value_new (value, color, root);
403   }
404   xbt_dict_cursor_t cursor = nullptr;
405   type_t child_type;
406   char *name;
407   xbt_dict_foreach(root->children, cursor, name, child_type) {
408     recursiveNewValueForUserStateType (type_name, value, color, child_type);
409   }
410 }
411
412 void instr_new_value_for_user_state_type (const char *type_name, const char *value, const char *color)
413 {
414   recursiveNewValueForUserStateType (type_name, value, color, PJ_type_get_root());
415 }
416
417 int instr_platform_traced ()
418 {
419   return platform_created;
420 }
421
422 #define GRAPHICATOR_SUPPORT_FUNCTIONS
423
424 static void recursiveXBTGraphExtraction(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges, NetZone_t netzone,
425                                         container_t container)
426 {
427   if (!xbt_dict_is_empty(netzone->children())) {
428     xbt_dict_cursor_t cursor = nullptr;
429     NetZone_t netzone_child;
430     char *child_name;
431     //bottom-up recursion
432     xbt_dict_foreach (netzone->children(), cursor, child_name, netzone_child) {
433       container_t child_container = (container_t)xbt_dict_get(container->children, netzone_child->name());
434       recursiveXBTGraphExtraction(graph, nodes, edges, netzone_child, child_container);
435     }
436   }
437
438   static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->getGraph(graph, nodes, edges);
439 }
440
441 xbt_graph_t instr_routing_platform_graph ()
442 {
443   xbt_graph_t ret = xbt_graph_new_graph (0, nullptr);
444   xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
445   xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
446   recursiveXBTGraphExtraction(ret, nodes, edges, simgrid::s4u::Engine::instance()->netRoot(), PJ_container_get_root());
447   xbt_dict_free (&nodes);
448   xbt_dict_free (&edges);
449   return ret;
450 }
451
452 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
453 {
454   unsigned int cursor = 0;
455   xbt_node_t node = nullptr;
456   xbt_edge_t edge = nullptr;
457   FILE *file = nullptr;
458
459   file = fopen(filename, "w");
460   xbt_assert(file, "Failed to open %s \n", filename);
461
462   if (g->directed)
463     fprintf(file, "digraph test {\n");
464   else
465     fprintf(file, "graph test {\n");
466
467   fprintf(file, "  graph [overlap=scale]\n");
468
469   fprintf(file, "  node [shape=box, style=filled]\n");
470   fprintf(file,
471           "  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
487 }