Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further ignorable cleanups
[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 "src/kernel/routing/AsImpl.hpp"
10 #include "simgrid/s4u/engine.hpp"
11 #include "surf/surf.h"
12 #include "src/surf/xml/platf_private.hpp"
13 #include "xbt/graph.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
16
17 static int platform_created = 0;            /* indicate whether the platform file has been traced */
18 static std::vector<container_t> currentContainer; /* push and pop, used only in creation */
19
20 static const char *instr_node_name (xbt_node_t node)
21 {
22   void *data = xbt_graph_node_get_data(node);
23   char *str = (char*)data;
24   return str;
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) return a1->father;
31
32   //create an array with all ancestors of a1
33   std::vector<container_t> ancestors_a1;
34   container_t p;
35   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], aux2[INSTR_DEFAULT_STR_SIZE];
84     snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", src->name, dst->name);
85     snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", dst->name, src->name);
86     if (xbt_dict_get_or_null (filter, aux1)){
87       XBT_DEBUG ("  linkContainers: already registered %s <-> %s (1)", src->name, dst->name);
88       return;
89     }
90     if (xbt_dict_get_or_null (filter, aux2)){
91       XBT_DEBUG ("  linkContainers: already registered %s <-> %s (2)", dst->name, src->name);
92       return;
93     }
94
95     //ok, not found, register it
96     xbt_dict_set (filter, aux1, xbt_strdup ("1"), nullptr);
97     xbt_dict_set (filter, aux2, xbt_strdup ("1"), nullptr);
98   }
99
100   //declare type
101   char link_typename[INSTR_DEFAULT_STR_SIZE];
102   snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s%s-%s%s",
103             father->type->name,
104             src->type->name, src->type->id,
105             dst->type->name, dst->type->id);
106   type_t link_type = PJ_type_get_or_null (link_typename, father->type);
107   if (link_type == nullptr){
108     link_type = PJ_type_link_new (link_typename, father->type, src->type, dst->type);
109   }
110
111   //register EDGE types for triva configuration
112   xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), nullptr);
113
114   //create the link
115   static long long counter = 0;
116
117   char key[INSTR_DEFAULT_STR_SIZE];
118   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
119   new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "topology", key);
120   new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "topology", key);
121
122   XBT_DEBUG ("  linkContainers %s <-> %s", src->name, dst->name);
123 }
124
125 static void recursiveGraphExtraction (simgrid::s4u::As *as, container_t container, xbt_dict_t filter)
126 {
127   if (!TRACE_platform_topology()){
128     XBT_DEBUG("Graph extraction disabled by user.");
129     return;
130   }
131   XBT_DEBUG ("Graph extraction for routing_component = %s", as->name());
132   if (!xbt_dict_is_empty(as->children())){
133     xbt_dict_cursor_t cursor = nullptr;
134     AS_t rc_son;
135     char *child_name;
136     //bottom-up recursion
137     xbt_dict_foreach(as->children(), cursor, child_name, rc_son) {
138       container_t child_container = (container_t) xbt_dict_get (container->children, rc_son->name());
139       recursiveGraphExtraction (rc_son, child_container, filter);
140     }
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::AsImpl*>(as)->getGraph(graph, nodes, edges);
153     xbt_dict_foreach(edges,cursor,edge_name,edge) {
154         linkContainers(
155           PJ_container_get((const char*) edge->src->data),
156           PJ_container_get((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 /*
165  * Callbacks
166  */
167 void sg_instr_AS_begin(sg_platf_AS_cbarg_t AS)
168 {
169   const char*id = AS->id;
170
171   if (PJ_container_get_root() == nullptr){
172     PJ_container_alloc ();
173     PJ_type_alloc();
174     container_t root = PJ_container_new (id, INSTR_AS, nullptr);
175     PJ_container_set_root (root);
176
177     if (TRACE_smpi_is_enabled()) {
178       type_t mpi = PJ_type_get_or_null ("MPI", root->type);
179       if (mpi == nullptr){
180         mpi = PJ_type_container_new("MPI", root->type);
181         if (!TRACE_smpi_is_grouped()) PJ_type_state_new ("MPI_STATE", mpi);
182         PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
183       }
184     }
185
186     if (TRACE_needs_platform()){
187       currentContainer.push_back(root);
188     }
189     return;
190   }
191
192   if (TRACE_needs_platform()){
193     container_t father = currentContainer.back();
194     container_t container = PJ_container_new (id, INSTR_AS, father);
195     currentContainer.push_back(container);
196   }
197 }
198
199 void sg_instr_AS_end()
200 {
201   if (TRACE_needs_platform()){
202     currentContainer.pop_back();
203   }
204 }
205
206 static void instr_routing_parse_start_link (sg_platf_link_cbarg_t link)
207 {
208   container_t father = currentContainer.back();
209
210   double bandwidth_value = link->bandwidth;
211   double latency_value = link->latency;
212   std::vector<std::string> links_to_create;
213
214   if (link->policy == SURF_LINK_FULLDUPLEX){
215     std::string id (link->id);
216     std::string up = id + "_UP";
217     std::string down = id + "_DOWN";
218     links_to_create.push_back(up);
219     links_to_create.push_back(down);
220   }else{
221     links_to_create.push_back(link->id);
222   }
223
224   for (auto link_name: links_to_create){
225     container_t container = PJ_container_new (link_name.c_str(), INSTR_LINK, father);
226
227     if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_link())) {
228       type_t bandwidth = PJ_type_get_or_null ("bandwidth", container->type);
229       if (bandwidth == nullptr){
230         bandwidth = PJ_type_variable_new ("bandwidth", nullptr, container->type);
231       }
232       type_t latency = PJ_type_get_or_null ("latency", container->type);
233       if (latency == nullptr){
234         latency = PJ_type_variable_new ("latency", nullptr, container->type);
235       }
236       new_pajeSetVariable (0, container, bandwidth, bandwidth_value);
237       new_pajeSetVariable (0, container, latency, latency_value);
238     }
239     if (TRACE_uncategorized()){
240       type_t bandwidth_used = PJ_type_get_or_null ("bandwidth_used", container->type);
241       if (bandwidth_used == nullptr){
242         PJ_type_variable_new ("bandwidth_used", "0.5 0.5 0.5", container->type);
243       }
244     }
245   }
246 }
247
248 void sg_instr_new_host(sg_platf_host_cbarg_t host)
249 {
250   container_t father = currentContainer.back();
251   container_t container = PJ_container_new (host->id, INSTR_HOST, father);
252
253   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_speed())) {
254     type_t speed = PJ_type_get_or_null ("power", container->type);
255     if (speed == nullptr){
256       speed = PJ_type_variable_new ("power", nullptr, container->type);
257     }
258
259     double current_speed_state = host->speed_per_pstate[host->pstate];
260     new_pajeSetVariable (0, container, speed, current_speed_state);
261   }
262   if (TRACE_uncategorized()){
263     type_t speed_used = PJ_type_get_or_null ("power_used", container->type);
264     if (speed_used == nullptr){
265       PJ_type_variable_new ("power_used", "0.5 0.5 0.5", container->type);
266     }
267   }
268
269   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
270     type_t mpi = PJ_type_get_or_null ("MPI", container->type);
271     if (mpi == nullptr){
272       mpi = PJ_type_container_new("MPI", container->type);
273       PJ_type_state_new ("MPI_STATE", mpi);
274     }
275   }
276
277   if (TRACE_msg_process_is_enabled()) {
278     type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", container->type);
279     if (msg_process == nullptr){
280       msg_process = PJ_type_container_new("MSG_PROCESS", container->type);
281       type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
282       PJ_value_new ("suspend", "1 0 1", state);
283       PJ_value_new ("sleep", "1 1 0", state);
284       PJ_value_new ("receive", "1 0 0", state);
285       PJ_value_new ("send", "0 0 1", state);
286       PJ_value_new ("task_execute", "0 1 1", state);
287       PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
288       PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
289     }
290   }
291
292   if (TRACE_msg_vm_is_enabled()) {
293     type_t msg_vm = PJ_type_get_or_null ("MSG_VM", container->type);
294     if (msg_vm == nullptr){
295       msg_vm = PJ_type_container_new("MSG_VM", container->type);
296       type_t state = PJ_type_state_new ("MSG_VM_STATE", msg_vm);
297       PJ_value_new ("suspend", "1 0 1", state);
298       PJ_value_new ("sleep", "1 1 0", state);
299       PJ_value_new ("receive", "1 0 0", state);
300       PJ_value_new ("send", "0 0 1", state);
301       PJ_value_new ("task_execute", "0 1 1", state);
302       PJ_type_link_new ("MSG_VM_LINK", PJ_type_get_root(), msg_vm, msg_vm);
303       PJ_type_link_new ("MSG_VM_PROCESS_LINK", PJ_type_get_root(), msg_vm, msg_vm);
304     }
305   }
306
307 }
308
309 void sg_instr_new_router(sg_platf_router_cbarg_t router)
310 {
311   container_t father = currentContainer.back();
312   PJ_container_new (router->id, INSTR_ROUTER, father);
313 }
314
315 static void instr_routing_parse_end_platform ()
316 {
317   currentContainer.clear();
318   xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free_f);
319   XBT_DEBUG ("Starting graph extraction.");
320   recursiveGraphExtraction (simgrid::s4u::Engine::instance()->rootAs(), PJ_container_get_root(), filter);
321   XBT_DEBUG ("Graph extraction finished.");
322   xbt_dict_free(&filter);
323   platform_created = 1;
324   TRACE_paje_dump_buffer(1);
325 }
326
327 void instr_routing_define_callbacks ()
328 {
329   if (!TRACE_is_enabled()) return;
330   //always need the call backs to ASes (we need only the root AS),
331   //to create the rootContainer and the rootType properly
332   if (!TRACE_needs_platform()) return;
333   simgrid::surf::on_link.connect(instr_routing_parse_start_link);
334   simgrid::surf::on_postparse.connect(instr_routing_parse_end_platform);
335 }
336
337 /*
338  * user categories support
339  */
340 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
341 {
342   if (!strcmp (root->name, "HOST")){
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, "MSG_VM")){
348     char tnstr[INSTR_DEFAULT_STR_SIZE];
349     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
350     PJ_type_variable_new (tnstr, color, root);
351   }
352  if (!strcmp (root->name, "LINK")){
353     char tnstr[INSTR_DEFAULT_STR_SIZE];
354     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
355     PJ_type_variable_new (tnstr, color, root);
356   }
357   xbt_dict_cursor_t cursor = nullptr;
358   type_t child_type;
359   char *name;
360   xbt_dict_foreach(root->children, cursor, name, child_type) {
361     recursiveNewVariableType (new_typename, color, child_type);
362   }
363 }
364
365 void instr_new_variable_type (const char *new_typename, const char *color)
366 {
367   recursiveNewVariableType (new_typename, color, PJ_type_get_root());
368 }
369
370 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
371 {
372   if (!strcmp (root->name, father_type)){
373     PJ_type_variable_new (new_typename, color, root);
374   }
375   xbt_dict_cursor_t cursor = nullptr;
376   type_t child_type;
377   char *name;
378   xbt_dict_foreach(root->children, cursor, name, child_type) {
379     recursiveNewUserVariableType (father_type, new_typename, color, child_type);
380   }
381 }
382
383 void instr_new_user_variable_type  (const char *father_type, const char *new_typename, const char *color)
384 {
385   recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
386 }
387
388 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
389 {
390   if (!strcmp (root->name, father_type)){
391     PJ_type_state_new (new_typename, root);
392   }
393   xbt_dict_cursor_t cursor = nullptr;
394   type_t child_type;
395   char *name;
396   xbt_dict_foreach(root->children, cursor, name, child_type) {
397     recursiveNewUserStateType (father_type, new_typename, child_type);
398   }
399 }
400
401 void instr_new_user_state_type (const char *father_type, const char *new_typename)
402 {
403   recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
404 }
405
406 static void recursiveNewValueForUserStateType (const char *type_name, const char *value, const char *color, type_t root)
407 {
408   if (!strcmp (root->name, type_name)){
409     PJ_value_new (value, color, root);
410   }
411   xbt_dict_cursor_t cursor = nullptr;
412   type_t child_type;
413   char *name;
414   xbt_dict_foreach(root->children, cursor, name, child_type) {
415     recursiveNewValueForUserStateType (type_name, value, color, child_type);
416   }
417 }
418
419 void instr_new_value_for_user_state_type (const char *type_name, const char *value, const char *color)
420 {
421   recursiveNewValueForUserStateType (type_name, value, color, PJ_type_get_root());
422 }
423
424 int instr_platform_traced ()
425 {
426   return platform_created;
427 }
428
429 #define GRAPHICATOR_SUPPORT_FUNCTIONS
430
431 static void recursiveXBTGraphExtraction (xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
432     AS_t as, container_t container)
433 {
434   if (!xbt_dict_is_empty(as->children())){
435     xbt_dict_cursor_t cursor = nullptr;
436     AS_t as_child;
437     char *child_name;
438     //bottom-up recursion
439     xbt_dict_foreach(as->children(), cursor, child_name, as_child) {
440       container_t child_container = (container_t) xbt_dict_get (
441         container->children, as_child->name());
442       recursiveXBTGraphExtraction (graph, nodes, edges, as_child, child_container);
443     }
444   }
445
446   static_cast<simgrid::kernel::routing::AsImpl*>(as)->getGraph(graph, nodes, edges);
447 }
448
449 xbt_graph_t instr_routing_platform_graph ()
450 {
451   xbt_graph_t ret = xbt_graph_new_graph (0, nullptr);
452   xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
453   xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
454   recursiveXBTGraphExtraction (ret, nodes, edges, simgrid::s4u::Engine::instance()->rootAs(), PJ_container_get_root());
455   xbt_dict_free (&nodes);
456   xbt_dict_free (&edges);
457   return ret;
458 }
459
460 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
461 {
462   unsigned int cursor = 0;
463   xbt_node_t node = nullptr;
464   xbt_edge_t edge = nullptr;
465   FILE *file = nullptr;
466
467   file = fopen(filename, "w");
468   xbt_assert(file, "Failed to open %s \n", filename);
469
470   if (g->directed)
471     fprintf(file, "digraph test {\n");
472   else
473     fprintf(file, "graph test {\n");
474
475   fprintf(file, "  graph [overlap=scale]\n");
476
477   fprintf(file, "  node [shape=box, style=filled]\n");
478   fprintf(file,
479           "  node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
480
481   xbt_dynar_foreach(g->nodes, cursor, node) {
482     fprintf(file, "  \"%s\";\n", instr_node_name(node));
483   }
484   xbt_dynar_foreach(g->edges, cursor, edge) {
485     const char *src_s = instr_node_name (edge->src);
486     const char *dst_s = instr_node_name (edge->dst);
487     if (g->directed)
488       fprintf(file, "  \"%s\" -> \"%s\";\n", src_s, dst_s);
489     else
490       fprintf(file, "  \"%s\" -- \"%s\";\n", src_s, dst_s);
491   }
492   fprintf(file, "}\n");
493   fclose(file);
494
495 }