Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move the routing stuff to its namespace
[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/surf/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 xbt_dynar_t currentContainer = nullptr; /* 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
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) return a1->father;
32
33   //create an array with all ancestors of a1
34   xbt_dynar_t ancestors_a1 = xbt_dynar_new(sizeof(container_t), nullptr);
35   container_t p;
36   p = a1->father;
37   while (p){
38     xbt_dynar_push_as (ancestors_a1, container_t, p);
39     p = p->father;
40   }
41
42   //create an array with all ancestors of a2
43   xbt_dynar_t ancestors_a2 = xbt_dynar_new(sizeof(container_t), nullptr);
44   p = a2->father;
45   while (p){
46     xbt_dynar_push_as (ancestors_a2, container_t, p);
47     p = p->father;
48   }
49
50   //find the lowest ancestor
51   p = nullptr;
52   int i = xbt_dynar_length (ancestors_a1) - 1;
53   int j = xbt_dynar_length (ancestors_a2) - 1;
54   while (i >= 0 && j >= 0){
55     container_t a1p = *(container_t*)xbt_dynar_get_ptr (ancestors_a1, i);
56     container_t a2p = *(container_t*)xbt_dynar_get_ptr (ancestors_a2, j);
57     if (a1p == a2p){
58       p = a1p;
59     }else{
60       break;
61     }
62     i--;
63     j--;
64   }
65   xbt_dynar_free (&ancestors_a1);
66   xbt_dynar_free (&ancestors_a2);
67   return p;
68 }
69
70 static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
71 {
72   //ignore loopback
73   if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0){
74     XBT_DEBUG ("  linkContainers: ignoring loopback link");
75     return;
76   }
77
78   //find common father
79   container_t father = lowestCommonAncestor (src, dst);
80   if (!father){
81     xbt_die ("common father unknown, this is a tracing problem");
82   }
83
84   if (filter != nullptr){
85     //check if we already register this pair (we only need one direction)
86     char aux1[INSTR_DEFAULT_STR_SIZE], 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   new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "topology", key);
123   new_pajeEndLink(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::As *as, 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 routing_component = %s", as->name());
135   if (!xbt_dict_is_empty(as->children())){
136     xbt_dict_cursor_t cursor = nullptr;
137     AS_t rc_son;
138     char *child_name;
139     //bottom-up recursion
140     xbt_dict_foreach(as->children(), cursor, child_name, rc_son) {
141       container_t child_container = (container_t) xbt_dict_get (container->children, rc_son->name());
142       recursiveGraphExtraction (rc_son, child_container, filter);
143     }
144   }
145
146   {
147     xbt_graph_t graph = xbt_graph_new_graph (0, nullptr);
148     xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
149     xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
150     xbt_edge_t edge = nullptr;
151
152     xbt_dict_cursor_t cursor = nullptr;
153     char *edge_name;
154
155     static_cast<simgrid::routing::AsImpl*>(as)->getGraph(graph, nodes, edges);
156     xbt_dict_foreach(edges,cursor,edge_name,edge) {
157         linkContainers(
158           PJ_container_get((const char*) edge->src->data),
159           PJ_container_get((const char*) edge->dst->data), filter);
160     }
161     xbt_dict_free (&nodes);
162     xbt_dict_free (&edges);
163     xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, nullptr);
164   }
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       if (!TRACE_smpi_is_grouped()){
182         type_t mpi = PJ_type_get_or_null ("MPI", root->type);
183         if (mpi == nullptr){
184           mpi = PJ_type_container_new("MPI", root->type);
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
191     if (TRACE_needs_platform()){
192       currentContainer = xbt_dynar_new (sizeof(container_t), nullptr);
193       xbt_dynar_push (currentContainer, &root);
194     }
195     return;
196   }
197
198   if (TRACE_needs_platform()){
199     container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
200     container_t container = PJ_container_new (id, INSTR_AS, father);
201     xbt_dynar_push (currentContainer, &container);
202   }
203 }
204
205 void sg_instr_AS_end()
206 {
207   if (TRACE_needs_platform()){
208     xbt_dynar_pop_ptr (currentContainer);
209   }
210 }
211
212 static void instr_routing_parse_start_link (sg_platf_link_cbarg_t link)
213 {
214   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
215
216   double bandwidth_value = link->bandwidth;
217   double latency_value = link->latency;
218   xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
219
220   if (link->policy == SURF_LINK_FULLDUPLEX){
221     char *up = bprintf("%s_UP", link->id);
222     char *down = bprintf("%s_DOWN", link->id);
223     xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
224     xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
225     free (up);
226     free (down);
227   }else{
228     xbt_dynar_push_as (links_to_create, char*, strdup(link->id));
229   }
230
231   char *link_name = nullptr;
232   unsigned int i;
233   xbt_dynar_foreach (links_to_create, i, link_name){
234
235     container_t container = PJ_container_new (link_name, INSTR_LINK, father);
236
237     if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_link())) {
238       type_t bandwidth = PJ_type_get_or_null ("bandwidth", container->type);
239       if (bandwidth == nullptr){
240         bandwidth = PJ_type_variable_new ("bandwidth", nullptr, container->type);
241       }
242       type_t latency = PJ_type_get_or_null ("latency", container->type);
243       if (latency == nullptr){
244         latency = PJ_type_variable_new ("latency", nullptr, container->type);
245       }
246       new_pajeSetVariable (0, container, bandwidth, bandwidth_value);
247       new_pajeSetVariable (0, container, latency, latency_value);
248     }
249     if (TRACE_uncategorized()){
250       type_t bandwidth_used = PJ_type_get_or_null ("bandwidth_used", container->type);
251       if (bandwidth_used == nullptr){
252         PJ_type_variable_new ("bandwidth_used", "0.5 0.5 0.5", container->type);
253       }
254     }
255   }
256
257   xbt_dynar_free (&links_to_create);
258 }
259
260 void sg_instr_new_host(sg_platf_host_cbarg_t host)
261 {
262   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
263   container_t container = PJ_container_new (host->id, INSTR_HOST, father);
264
265   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (! TRACE_disable_speed())) {
266     type_t speed = PJ_type_get_or_null ("power", container->type);
267     if (speed == nullptr){
268       speed = PJ_type_variable_new ("power", nullptr, container->type);
269     }
270
271     double current_speed_state;
272     xbt_dynar_get_cpy(host->speed_per_pstate, host->pstate, &current_speed_state);
273     new_pajeSetVariable (0, container, speed, current_speed_state);
274   }
275   if (TRACE_uncategorized()){
276     type_t speed_used = PJ_type_get_or_null ("power_used", container->type);
277     if (speed_used == nullptr){
278       PJ_type_variable_new ("power_used", "0.5 0.5 0.5", container->type);
279     }
280   }
281
282   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
283     type_t mpi = PJ_type_get_or_null ("MPI", container->type);
284     if (mpi == nullptr){
285       mpi = PJ_type_container_new("MPI", container->type);
286       PJ_type_state_new ("MPI_STATE", mpi);
287       PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
288     }
289   }
290
291   if (TRACE_msg_process_is_enabled()) {
292     type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", container->type);
293     if (msg_process == nullptr){
294       msg_process = PJ_type_container_new("MSG_PROCESS", container->type);
295       type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
296       PJ_value_new ("suspend", "1 0 1", state);
297       PJ_value_new ("sleep", "1 1 0", state);
298       PJ_value_new ("receive", "1 0 0", state);
299       PJ_value_new ("send", "0 0 1", state);
300       PJ_value_new ("task_execute", "0 1 1", state);
301       PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
302       PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
303     }
304   }
305
306   if (TRACE_msg_vm_is_enabled()) {
307     type_t msg_vm = PJ_type_get_or_null ("MSG_VM", container->type);
308     if (msg_vm == nullptr){
309       msg_vm = PJ_type_container_new("MSG_VM", container->type);
310       type_t state = PJ_type_state_new ("MSG_VM_STATE", msg_vm);
311       PJ_value_new ("suspend", "1 0 1", state);
312       PJ_value_new ("sleep", "1 1 0", state);
313       PJ_value_new ("receive", "1 0 0", state);
314       PJ_value_new ("send", "0 0 1", state);
315       PJ_value_new ("task_execute", "0 1 1", state);
316       PJ_type_link_new ("MSG_VM_LINK", PJ_type_get_root(), msg_vm, msg_vm);
317       PJ_type_link_new ("MSG_VM_PROCESS_LINK", PJ_type_get_root(), msg_vm, msg_vm);
318     }
319   }
320
321 }
322
323 void sg_instr_new_router(sg_platf_router_cbarg_t router)
324 {
325   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
326   PJ_container_new (router->id, INSTR_ROUTER, father);
327 }
328
329 static void instr_routing_parse_end_platform ()
330 {
331   xbt_dynar_free(&currentContainer);
332   currentContainer = nullptr;
333   xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free_f);
334   XBT_DEBUG ("Starting graph extraction.");
335   recursiveGraphExtraction (simgrid::s4u::Engine::instance()->rootAs(), PJ_container_get_root(), filter);
336   XBT_DEBUG ("Graph extraction finished.");
337   xbt_dict_free(&filter);
338   platform_created = 1;
339   TRACE_paje_dump_buffer(1);
340 }
341
342 void instr_routing_define_callbacks ()
343 {
344   if (!TRACE_is_enabled()) return;
345   //always need the call backs to ASes (we need only the root AS),
346   //to create the rootContainer and the rootType properly
347   if (!TRACE_needs_platform()) return;
348   simgrid::surf::on_link.connect(instr_routing_parse_start_link);
349   simgrid::surf::on_postparse.connect(instr_routing_parse_end_platform);
350 }
351
352 /*
353  * user categories support
354  */
355 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
356 {
357   if (!strcmp (root->name, "HOST")){
358     char tnstr[INSTR_DEFAULT_STR_SIZE];
359     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
360     PJ_type_variable_new (tnstr, color, root);
361   }
362   if (!strcmp (root->name, "MSG_VM")){
363     char tnstr[INSTR_DEFAULT_STR_SIZE];
364     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
365     PJ_type_variable_new (tnstr, color, root);
366   }
367  if (!strcmp (root->name, "LINK")){
368     char tnstr[INSTR_DEFAULT_STR_SIZE];
369     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
370     PJ_type_variable_new (tnstr, 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     recursiveNewVariableType (new_typename, color, child_type);
377   }
378 }
379
380 void instr_new_variable_type (const char *new_typename, const char *color)
381 {
382   recursiveNewVariableType (new_typename, color, PJ_type_get_root());
383 }
384
385 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
386 {
387   if (!strcmp (root->name, father_type)){
388     PJ_type_variable_new (new_typename, color, 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     recursiveNewUserVariableType (father_type, new_typename, color, child_type);
395   }
396 }
397
398 void instr_new_user_variable_type  (const char *father_type, const char *new_typename, const char *color)
399 {
400   recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
401 }
402
403 static void recursiveNewUserStateType (const char *father_type, const char *new_typename, type_t root)
404 {
405   if (!strcmp (root->name, father_type)){
406     PJ_type_state_new (new_typename, 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     recursiveNewUserStateType (father_type, new_typename, child_type);
413   }
414 }
415
416 void instr_new_user_state_type (const char *father_type, const char *new_typename)
417 {
418   recursiveNewUserStateType (father_type, new_typename, PJ_type_get_root());
419 }
420
421 static void recursiveNewValueForUserStateType (const char *type_name, const char *value, const char *color, type_t root)
422 {
423   if (!strcmp (root->name, type_name)){
424     PJ_value_new (value, color, root);
425   }
426   xbt_dict_cursor_t cursor = nullptr;
427   type_t child_type;
428   char *name;
429   xbt_dict_foreach(root->children, cursor, name, child_type) {
430     recursiveNewValueForUserStateType (type_name, value, color, child_type);
431   }
432 }
433
434 void instr_new_value_for_user_state_type (const char *type_name, const char *value, const char *color)
435 {
436   recursiveNewValueForUserStateType (type_name, value, color, PJ_type_get_root());
437 }
438
439 int instr_platform_traced ()
440 {
441   return platform_created;
442 }
443
444 #define GRAPHICATOR_SUPPORT_FUNCTIONS
445
446
447 static void recursiveXBTGraphExtraction (xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
448     AS_t as, container_t container)
449 {
450   if (!xbt_dict_is_empty(as->children())){
451     xbt_dict_cursor_t cursor = nullptr;
452     AS_t as_child;
453     char *child_name;
454     //bottom-up recursion
455     xbt_dict_foreach(as->children(), cursor, child_name, as_child) {
456       container_t child_container = (container_t) xbt_dict_get (
457         container->children, as_child->name());
458       recursiveXBTGraphExtraction (graph, nodes, edges, as_child, child_container);
459     }
460   }
461
462   static_cast<simgrid::routing::AsImpl*>(as)->getGraph(graph, nodes, edges);
463 }
464
465 xbt_graph_t instr_routing_platform_graph (void)
466 {
467   xbt_graph_t ret = xbt_graph_new_graph (0, nullptr);
468   xbt_dict_t nodes = xbt_dict_new_homogeneous(nullptr);
469   xbt_dict_t edges = xbt_dict_new_homogeneous(nullptr);
470   recursiveXBTGraphExtraction (ret, nodes, edges, simgrid::s4u::Engine::instance()->rootAs(), PJ_container_get_root());
471   xbt_dict_free (&nodes);
472   xbt_dict_free (&edges);
473   return ret;
474 }
475
476 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
477 {
478   unsigned int cursor = 0;
479   xbt_node_t node = nullptr;
480   xbt_edge_t edge = nullptr;
481   FILE *file = nullptr;
482
483   file = fopen(filename, "w");
484   xbt_assert(file, "Failed to open %s \n", filename);
485
486   if (g->directed)
487     fprintf(file, "digraph test {\n");
488   else
489     fprintf(file, "graph test {\n");
490
491   fprintf(file, "  graph [overlap=scale]\n");
492
493   fprintf(file, "  node [shape=box, style=filled]\n");
494   fprintf(file,
495           "  node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
496
497   xbt_dynar_foreach(g->nodes, cursor, node) {
498     fprintf(file, "  \"%s\";\n", instr_node_name(node));
499   }
500   xbt_dynar_foreach(g->edges, cursor, edge) {
501     const char *src_s = instr_node_name (edge->src);
502     const char *dst_s = instr_node_name (edge->dst);
503     if (g->directed)
504       fprintf(file, "  \"%s\" -> \"%s\";\n", src_s, dst_s);
505     else
506       fprintf(file, "  \"%s\" -- \"%s\";\n", src_s, dst_s);
507   }
508   fprintf(file, "}\n");
509   fclose(file);
510
511 }