Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
allow to have TI data for popEvent
[simgrid.git] / src / instr / instr_platform.cpp
1 /* Copyright (c) 2010-2018. 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.hpp"
7
8 #include "simgrid/kernel/routing/NetPoint.hpp"
9 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "src/surf/network_interface.hpp"
13 #include "src/surf/xml/platf_private.hpp"
14 #include "surf/surf.hpp"
15 #include "xbt/graph.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_routing, instr, "Tracing platform hierarchy");
18
19 static std::vector<simgrid::instr::NetZoneContainer*> currentContainer; /* push and pop, used only in creation */
20
21 static const char* instr_node_name(xbt_node_t node)
22 {
23   return static_cast<char*>(xbt_graph_node_get_data(node));
24 }
25
26 static container_t lowestCommonAncestor(container_t a1, container_t a2)
27 {
28   // this is only an optimization (since most of a1 and a2 share the same parent)
29   if (a1->father_ == a2->father_)
30     return a1->father_;
31
32   // create an array with all ancestors of a1
33   std::vector<container_t> ancestors_a1;
34   container_t p = a1->father_;
35   while (p) {
36     ancestors_a1.push_back(p);
37     p = p->father_;
38   }
39
40   // create an array with all ancestors of a2
41   std::vector<container_t> ancestors_a2;
42   p = a2->father_;
43   while (p) {
44     ancestors_a2.push_back(p);
45     p = p->father_;
46   }
47
48   // find the lowest ancestor
49   p     = nullptr;
50   int i = ancestors_a1.size() - 1;
51   int j = ancestors_a2.size() - 1;
52   while (i >= 0 && j >= 0) {
53     container_t a1p = ancestors_a1.at(i);
54     container_t a2p = ancestors_a2.at(j);
55     if (a1p == a2p) {
56       p = a1p;
57     } else {
58       break;
59     }
60     i--;
61     j--;
62   }
63   return p;
64 }
65
66 static void linkContainers(container_t src, container_t dst, std::set<std::string>* filter)
67 {
68   // ignore loopback
69   if (src->get_name() == "__loopback__" || dst->get_name() == "__loopback__") {
70     XBT_DEBUG("  linkContainers: ignoring loopback link");
71     return;
72   }
73
74   // find common father
75   container_t father = lowestCommonAncestor(src, dst);
76   if (not father) {
77     xbt_die("common father unknown, this is a tracing problem");
78   }
79
80   // check if we already register this pair (we only need one direction)
81   std::string aux1 = src->get_name() + dst->get_name();
82   std::string aux2 = dst->get_name() + src->get_name();
83   if (filter->find(aux1) != filter->end()) {
84     XBT_DEBUG("  linkContainers: already registered %s <-> %s (1)", src->get_cname(), dst->get_cname());
85     return;
86   }
87   if (filter->find(aux2) != filter->end()) {
88     XBT_DEBUG("  linkContainers: already registered %s <-> %s (2)", dst->get_cname(), src->get_cname());
89     return;
90   }
91
92   // ok, not found, register it
93   filter->insert(aux1);
94   filter->insert(aux2);
95
96   // declare type
97   std::string link_typename = father->type_->get_name() + "-" + src->type_->get_name() +
98                               std::to_string(src->type_->get_id()) + "-" + dst->type_->get_name() +
99                               std::to_string(dst->type_->get_id());
100   simgrid::instr::LinkType* link = father->type_->getOrCreateLinkType(link_typename, src->type_, dst->type_);
101   link->setCallingContainer(father);
102
103   // register EDGE types for triva configuration
104   trivaEdgeTypes.insert(link->get_name());
105
106   // create the link
107   static long long counter = 0;
108
109   std::string key = std::to_string(counter);
110   counter++;
111
112   link->startEvent(src, "topology", key);
113   link->endEvent(dst, "topology", key);
114
115   XBT_DEBUG("  linkContainers %s <-> %s", src->get_cname(), dst->get_cname());
116 }
117
118 static void recursiveGraphExtraction(simgrid::s4u::NetZone* netzone, container_t container,
119                                      std::set<std::string>* filter)
120 {
121   if (not TRACE_platform_topology()) {
122     XBT_DEBUG("Graph extraction disabled by user.");
123     return;
124   }
125   XBT_DEBUG("Graph extraction for NetZone = %s", netzone->get_cname());
126   if (not netzone->getChildren()->empty()) {
127     // bottom-up recursion
128     for (auto const& nz_son : *netzone->getChildren()) {
129       container_t child_container = container->children_.at(nz_son->get_cname());
130       recursiveGraphExtraction(nz_son, child_container, filter);
131     }
132   }
133
134   xbt_graph_t graph = xbt_graph_new_graph(0, nullptr);
135   std::map<std::string, xbt_node_t>* nodes = new std::map<std::string, xbt_node_t>;
136   std::map<std::string, xbt_edge_t>* edges = new std::map<std::string, xbt_edge_t>;
137
138   static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->get_graph(graph, nodes, edges);
139   for (auto elm : *edges) {
140     xbt_edge_t edge = elm.second;
141     linkContainers(simgrid::instr::Container::byName(static_cast<const char*>(edge->src->data)),
142                    simgrid::instr::Container::byName(static_cast<const char*>(edge->dst->data)), filter);
143   }
144   delete nodes;
145   delete edges;
146   xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, nullptr);
147 }
148
149 /*
150  * Callbacks
151  */
152 static void instr_netzone_on_creation(simgrid::s4u::NetZone& netzone)
153 {
154   std::string id = netzone.get_name();
155   if (simgrid::instr::Container::getRoot() == nullptr) {
156     simgrid::instr::NetZoneContainer* root = new simgrid::instr::NetZoneContainer(id, 0, nullptr);
157
158     if (TRACE_smpi_is_enabled()) {
159       simgrid::instr::Type* mpi = root->type_->getOrCreateContainerType("MPI");
160       if (not TRACE_smpi_is_grouped())
161         mpi->getOrCreateStateType("MPI_STATE");
162       root->type_->getOrCreateLinkType("MPI_LINK", mpi, mpi);
163       // TODO See if we can move this to the LoadBalancer plugin
164       root->type_->getOrCreateLinkType("MIGRATE_LINK", mpi, mpi);
165       mpi->getOrCreateStateType("MIGRATE_STATE");
166     }
167
168     if (TRACE_needs_platform()) {
169       currentContainer.push_back(root);
170     }
171     return;
172   }
173
174   if (TRACE_needs_platform()) {
175     simgrid::instr::NetZoneContainer* container =
176         new simgrid::instr::NetZoneContainer(id, currentContainer.size(), currentContainer.back());
177     currentContainer.push_back(container);
178   }
179 }
180
181 static void instr_netzone_on_seal(simgrid::s4u::NetZone& /*netzone*/)
182 {
183   if (TRACE_needs_platform()) {
184     currentContainer.pop_back();
185   }
186 }
187
188 static void instr_link_on_creation(simgrid::s4u::Link& link)
189 {
190   if (currentContainer.empty()) // No ongoing parsing. Are you creating the loopback?
191     return;
192
193   container_t father    = currentContainer.back();
194   container_t container = new simgrid::instr::Container(link.get_name(), "LINK", father);
195
196   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (not TRACE_disable_link())) {
197     simgrid::instr::VariableType* bandwidth = container->type_->getOrCreateVariableType("bandwidth", "");
198     bandwidth->setCallingContainer(container);
199     bandwidth->setEvent(0, link.bandwidth());
200     simgrid::instr::VariableType* latency = container->type_->getOrCreateVariableType("latency", "");
201     latency->setCallingContainer(container);
202     latency->setEvent(0, link.latency());
203   }
204   if (TRACE_uncategorized()) {
205     container->type_->getOrCreateVariableType("bandwidth_used", "0.5 0.5 0.5");
206   }
207 }
208
209 static void instr_host_on_creation(simgrid::s4u::Host& host)
210 {
211   container_t container = new simgrid::instr::HostContainer(host, currentContainer.back());
212   container_t root      = simgrid::instr::Container::getRoot();
213
214   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (not TRACE_disable_speed())) {
215     simgrid::instr::VariableType* power = container->type_->getOrCreateVariableType("power", "");
216     power->setCallingContainer(container);
217     power->setEvent(0, host.getSpeed());
218   }
219
220   if (TRACE_uncategorized())
221     container->type_->getOrCreateVariableType("power_used", "0.5 0.5 0.5");
222
223   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()) {
224     simgrid::instr::ContainerType* mpi = container->type_->getOrCreateContainerType("MPI");
225     mpi->getOrCreateStateType("MPI_STATE");
226     // TODO See if we can move this to the LoadBalancer plugin
227     root->type_->getOrCreateLinkType("MIGRATE_LINK", mpi, mpi);
228     mpi->getOrCreateStateType("MIGRATE_STATE");
229   }
230
231   if (TRACE_actor_is_enabled()) {
232     simgrid::instr::ContainerType* actor = container->type_->getOrCreateContainerType("ACTOR");
233     simgrid::instr::StateType* state     = actor->getOrCreateStateType("ACTOR_STATE");
234     state->addEntityValue("suspend", "1 0 1");
235     state->addEntityValue("sleep", "1 1 0");
236     state->addEntityValue("receive", "1 0 0");
237     state->addEntityValue("send", "0 0 1");
238     state->addEntityValue("task_execute", "0 1 1");
239     root->type_->getOrCreateLinkType("ACTOR_LINK", actor, actor);
240     root->type_->getOrCreateLinkType("ACTOR_TASK_LINK", actor, actor);
241   }
242
243   if (TRACE_vm_is_enabled()) {
244     simgrid::instr::ContainerType* msg_vm = container->type_->getOrCreateContainerType("MSG_VM");
245     simgrid::instr::StateType* state      = msg_vm->getOrCreateStateType("MSG_VM_STATE");
246     state->addEntityValue("suspend", "1 0 1");
247     state->addEntityValue("sleep", "1 1 0");
248     state->addEntityValue("receive", "1 0 0");
249     state->addEntityValue("send", "0 0 1");
250     state->addEntityValue("task_execute", "0 1 1");
251     root->type_->getOrCreateLinkType("MSG_VM_LINK", msg_vm, msg_vm);
252     root->type_->getOrCreateLinkType("MSG_VM_ACTOR_LINK", msg_vm, msg_vm);
253   }
254 }
255
256 static void instr_netpoint_on_creation(simgrid::kernel::routing::NetPoint* netpoint)
257 {
258   if (netpoint->is_router() && TRACE_needs_platform() && TRACE_is_enabled())
259     new simgrid::instr::RouterContainer(netpoint->get_cname(), currentContainer.back());
260 }
261
262 static void instr_on_platform_created()
263 {
264   currentContainer.clear();
265   std::set<std::string>* filter = new std::set<std::string>;
266   XBT_DEBUG("Starting graph extraction.");
267   recursiveGraphExtraction(simgrid::s4u::Engine::getInstance()->getNetRoot(), simgrid::instr::Container::getRoot(),
268                            filter);
269   XBT_DEBUG("Graph extraction finished.");
270   delete filter;
271   TRACE_paje_dump_buffer(true);
272 }
273
274 void instr_define_callbacks()
275 {
276   // always need the callbacks to zones (we need only the root zone), to create the rootContainer and the rootType
277   // properly
278   if (TRACE_needs_platform()) {
279     simgrid::s4u::on_platform_created.connect(instr_on_platform_created);
280     simgrid::s4u::Host::onCreation.connect(instr_host_on_creation);
281     simgrid::s4u::Link::onCreation.connect(instr_link_on_creation);
282   }
283   simgrid::s4u::NetZone::onCreation.connect(instr_netzone_on_creation);
284   simgrid::s4u::NetZone::onSeal.connect(instr_netzone_on_seal);
285   simgrid::kernel::routing::NetPoint::onCreation.connect(instr_netpoint_on_creation);
286 }
287 /*
288  * user categories support
289  */
290 static void recursiveNewVariableType(std::string new_typename, std::string color, simgrid::instr::Type* root)
291 {
292   if (root->get_name() == "HOST" || root->get_name() == "MSG_VM")
293     root->getOrCreateVariableType(std::string("p") + new_typename, color);
294
295   if (root->get_name() == "LINK")
296     root->getOrCreateVariableType(std::string("b") + new_typename, color);
297
298   for (auto elm : root->children_) {
299     recursiveNewVariableType(new_typename, color, elm.second);
300   }
301 }
302
303 void instr_new_variable_type(std::string new_typename, std::string color)
304 {
305   recursiveNewVariableType(new_typename, color, simgrid::instr::Container::getRoot()->type_);
306 }
307
308 static void recursiveNewUserVariableType(std::string father_type, std::string new_typename, std::string color,
309                                          simgrid::instr::Type* root)
310 {
311   if (root->get_name() == father_type) {
312     root->getOrCreateVariableType(new_typename, color);
313   }
314   for (auto elm : root->children_)
315     recursiveNewUserVariableType(father_type, new_typename, color, elm.second);
316 }
317
318 void instr_new_user_variable_type(std::string father_type, std::string new_typename, std::string color)
319 {
320   recursiveNewUserVariableType(father_type, new_typename, color, simgrid::instr::Container::getRoot()->type_);
321 }
322
323 static void recursiveNewUserStateType(std::string father_type, std::string new_typename, simgrid::instr::Type* root)
324 {
325   if (root->get_name() == father_type)
326     root->getOrCreateStateType(new_typename);
327
328   for (auto elm : root->children_)
329     recursiveNewUserStateType(father_type, new_typename, elm.second);
330 }
331
332 void instr_new_user_state_type(std::string father_type, std::string new_typename)
333 {
334   recursiveNewUserStateType(father_type, new_typename, simgrid::instr::Container::getRoot()->type_);
335 }
336
337 static void recursiveNewValueForUserStateType(std::string type_name, const char* val, std::string color,
338                                               simgrid::instr::Type* root)
339 {
340   if (root->get_name() == type_name)
341     static_cast<simgrid::instr::StateType*>(root)->addEntityValue(val, color);
342
343   for (auto elm : root->children_)
344     recursiveNewValueForUserStateType(type_name, val, color, elm.second);
345 }
346
347 void instr_new_value_for_user_state_type(std::string type_name, const char* value, std::string color)
348 {
349   recursiveNewValueForUserStateType(type_name, value, color, simgrid::instr::Container::getRoot()->type_);
350 }
351
352 #define GRAPHICATOR_SUPPORT_FUNCTIONS
353
354 static void recursiveXBTGraphExtraction(xbt_graph_t graph, std::map<std::string, xbt_node_t>* nodes,
355                                         std::map<std::string, xbt_edge_t>* edges, sg_netzone_t netzone,
356                                         container_t container)
357 {
358   if (not netzone->getChildren()->empty()) {
359     // bottom-up recursion
360     for (auto const& netzone_child : *netzone->getChildren()) {
361       container_t child_container = container->children_.at(netzone_child->get_cname());
362       recursiveXBTGraphExtraction(graph, nodes, edges, netzone_child, child_container);
363     }
364   }
365
366   static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->get_graph(graph, nodes, edges);
367 }
368
369 xbt_graph_t instr_routing_platform_graph()
370 {
371   xbt_graph_t ret = xbt_graph_new_graph(0, nullptr);
372   std::map<std::string, xbt_node_t>* nodes = new std::map<std::string, xbt_node_t>;
373   std::map<std::string, xbt_edge_t>* edges = new std::map<std::string, xbt_edge_t>;
374   recursiveXBTGraphExtraction(ret, nodes, edges, simgrid::s4u::Engine::getInstance()->getNetRoot(),
375                               simgrid::instr::Container::getRoot());
376   delete nodes;
377   delete edges;
378   return ret;
379 }
380
381 void instr_routing_platform_graph_export_graphviz(xbt_graph_t g, const char* filename)
382 {
383   unsigned int cursor = 0;
384   xbt_node_t node     = nullptr;
385   xbt_edge_t edge     = nullptr;
386
387   FILE* file = fopen(filename, "w");
388   xbt_assert(file, "Failed to open %s \n", filename);
389
390   if (g->directed)
391     fprintf(file, "digraph test {\n");
392   else
393     fprintf(file, "graph test {\n");
394
395   fprintf(file, "  graph [overlap=scale]\n");
396
397   fprintf(file, "  node [shape=box, style=filled]\n");
398   fprintf(file, "  node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
399
400   xbt_dynar_foreach (g->nodes, cursor, node) {
401     fprintf(file, "  \"%s\";\n", instr_node_name(node));
402   }
403   xbt_dynar_foreach (g->edges, cursor, edge) {
404     const char* src_s = instr_node_name(edge->src);
405     const char* dst_s = instr_node_name(edge->dst);
406     if (g->directed)
407       fprintf(file, "  \"%s\" -> \"%s\";\n", src_s, dst_s);
408     else
409       fprintf(file, "  \"%s\" -- \"%s\";\n", src_s, dst_s);
410   }
411   fprintf(file, "}\n");
412   fclose(file);
413 }