Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Declare local variables inside the if statement.
[simgrid.git] / src / instr / instr_platform.cpp
1 /* Copyright (c) 2010-2022. 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 <simgrid/kernel/routing/NetPoint.hpp>
7 #include <simgrid/kernel/routing/NetZoneImpl.hpp>
8 #include <simgrid/s4u/Actor.hpp>
9 #include <simgrid/s4u/Comm.hpp>
10 #include <simgrid/s4u/Engine.hpp>
11 #include <simgrid/s4u/Exec.hpp>
12 #include <simgrid/s4u/Host.hpp>
13 #include <simgrid/s4u/VirtualMachine.hpp>
14 #include <xbt/graph.h>
15
16 #include "src/instr/instr_private.hpp"
17 #include "src/kernel/resource/CpuImpl.hpp"
18 #include "src/kernel/resource/NetworkModel.hpp"
19 #include "src/surf/surf_interface.hpp"
20
21 #include <fstream>
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_routing, instr, "Tracing platform hierarchy");
24
25 std::string instr_pid(simgrid::s4u::Actor const& proc)
26 {
27   return std::string(proc.get_name()) + "-" + std::to_string(proc.get_pid());
28 }
29
30 static simgrid::instr::Container* lowestCommonAncestor(const simgrid::instr::Container* a1,
31                                                        const simgrid::instr::Container* a2)
32 {
33   // this is only an optimization (since most of a1 and a2 share the same parent)
34   if (a1->get_parent() == a2->get_parent())
35     return a1->get_parent();
36
37   // create an array with all ancestors of a1
38   std::vector<simgrid::instr::Container*> ancestors_a1;
39   for (auto* p = a1->get_parent(); p != nullptr; p = p->get_parent())
40     ancestors_a1.push_back(p);
41
42   // create an array with all ancestors of a2
43   std::vector<simgrid::instr::Container*> ancestors_a2;
44   for (auto* p = a2->get_parent(); p != nullptr; p = p->get_parent())
45     ancestors_a2.push_back(p);
46
47   // find the lowest ancestor
48   simgrid::instr::Container* p = nullptr;
49   int i = static_cast<int>(ancestors_a1.size()) - 1;
50   int j = static_cast<int>(ancestors_a2.size()) - 1;
51   while (i >= 0 && j >= 0) {
52     simgrid::instr::Container* a1p       = ancestors_a1.at(i);
53     if (a1p != ancestors_a2.at(j))
54       break;
55     p = a1p;
56     i--;
57     j--;
58   }
59   return p;
60 }
61
62 static void linkContainers(simgrid::instr::Container* src, simgrid::instr::Container* dst,
63                            std::set<std::string, std::less<>>* filter)
64 {
65   // ignore loopback
66   if (src->get_name() == "__loopback__" || dst->get_name() == "__loopback__") {
67     XBT_DEBUG("  linkContainers: ignoring loopback link");
68     return;
69   }
70
71   // find common parent
72   simgrid::instr::Container* parent = lowestCommonAncestor(src, dst);
73   xbt_assert(parent, "common parent unknown, this is a tracing problem");
74
75   // check if we already register this pair (we only need one direction)
76   std::string aux1 = src->get_name() + dst->get_name();
77   std::string aux2 = dst->get_name() + src->get_name();
78   if (filter->find(aux1) != filter->end()) {
79     XBT_DEBUG("  linkContainers: already registered %s <-> %s (1)", src->get_cname(), dst->get_cname());
80     return;
81   }
82   if (filter->find(aux2) != filter->end()) {
83     XBT_DEBUG("  linkContainers: already registered %s <-> %s (2)", dst->get_cname(), src->get_cname());
84     return;
85   }
86
87   // ok, not found, register it
88   filter->insert(aux1);
89   filter->insert(aux2);
90
91   // declare type
92   std::string link_typename = parent->get_type()->get_name() + "-" + src->get_type()->get_name() +
93                               std::to_string(src->get_type()->get_id()) + "-" + dst->get_type()->get_name() +
94                               std::to_string(dst->get_type()->get_id());
95   simgrid::instr::LinkType* link =
96       parent->get_type()->by_name_or_create(link_typename, src->get_type(), dst->get_type());
97   link->set_calling_container(parent);
98
99   // create the link
100   static long long counter = 0;
101
102   std::string key = std::to_string(counter);
103   counter++;
104
105   link->start_event(src, "topology", key);
106   link->end_event(dst, "topology", key);
107
108   XBT_DEBUG("  linkContainers %s <-> %s", src->get_cname(), dst->get_cname());
109 }
110
111 static void recursiveGraphExtraction(const simgrid::s4u::NetZone* netzone, const simgrid::instr::Container* container,
112                                      std::set<std::string, std::less<>>* filter)
113 {
114   if (not TRACE_platform_topology()) {
115     XBT_DEBUG("Graph extraction disabled by user.");
116     return;
117   }
118   XBT_DEBUG("Graph extraction for NetZone = %s", netzone->get_cname());
119
120   // bottom-up recursion
121   for (auto const& nz_son : netzone->get_children()) {
122     const simgrid::instr::Container* child_container = container->get_child_by_name(nz_son->get_name());
123     recursiveGraphExtraction(nz_son, child_container, filter);
124   }
125
126   auto* graph = xbt_graph_new_graph(0, nullptr);
127   std::map<std::string, xbt_node_t, std::less<>> nodes;
128   std::map<std::string, xbt_edge_t, std::less<>> edges;
129
130   netzone->get_impl()->get_graph(graph, &nodes, &edges);
131   for (auto const& [_, edge] : edges) {
132     linkContainers(simgrid::instr::Container::by_name(static_cast<const char*>(edge->src->data)),
133                    simgrid::instr::Container::by_name(static_cast<const char*>(edge->dst->data)), filter);
134   }
135   xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, nullptr);
136 }
137
138 /*
139  * user categories support
140  */
141 static void recursiveNewVariableType(const std::string& new_typename, const std::string& color,
142                                      simgrid::instr::Type* root)
143 {
144   if (root->get_name() == "HOST" || root->get_name() == "VM")
145     root->by_name_or_create(std::string("p") + new_typename, color);
146
147   if (root->get_name() == "LINK")
148     root->by_name_or_create(std::string("b") + new_typename, color);
149
150   for (auto const& [_, child] : root->get_children()) {
151     recursiveNewVariableType(new_typename, color, child.get());
152   }
153 }
154
155 void instr_new_variable_type(const std::string& new_typename, const std::string& color)
156 {
157   recursiveNewVariableType(new_typename, color, simgrid::instr::Container::get_root()->get_type());
158 }
159
160 static void recursiveNewUserVariableType(const std::string& parent_type, const std::string& new_typename,
161                                          const std::string& color, simgrid::instr::Type* root)
162 {
163   if (root->get_name() == parent_type) {
164     root->by_name_or_create(new_typename, color);
165   }
166   for (auto const& [_, child] : root->get_children())
167     recursiveNewUserVariableType(parent_type, new_typename, color, child.get());
168 }
169
170 void instr_new_user_variable_type(const std::string& parent_type, const std::string& new_typename,
171                                   const std::string& color)
172 {
173   recursiveNewUserVariableType(parent_type, new_typename, color, simgrid::instr::Container::get_root()->get_type());
174 }
175
176 static void recursiveNewUserStateType(const std::string& parent_type, const std::string& new_typename,
177                                       simgrid::instr::Type* root)
178 {
179   if (root->get_name() == parent_type)
180     root->by_name_or_create<simgrid::instr::StateType>(new_typename);
181
182   for (auto const& [_, child] : root->get_children())
183     recursiveNewUserStateType(parent_type, new_typename, child.get());
184 }
185
186 void instr_new_user_state_type(const std::string& parent_type, const std::string& new_typename)
187 {
188   recursiveNewUserStateType(parent_type, new_typename, simgrid::instr::Container::get_root()->get_type());
189 }
190
191 static void recursiveNewValueForUserStateType(const std::string& type_name, const char* val, const std::string& color,
192                                               simgrid::instr::Type* root)
193 {
194   if (root->get_name() == type_name)
195     static_cast<simgrid::instr::StateType*>(root)->add_entity_value(val, color);
196
197   for (auto const& [_, child] : root->get_children())
198     recursiveNewValueForUserStateType(type_name, val, color, child.get());
199 }
200
201 void instr_new_value_for_user_state_type(const std::string& type_name, const char* value, const std::string& color)
202 {
203   recursiveNewValueForUserStateType(type_name, value, color, simgrid::instr::Container::get_root()->get_type());
204 }
205
206 namespace simgrid {
207 namespace instr {
208
209 /** @brief Creates a file with the topology of the platform file used for the simulator.
210  *
211  *  The graph topology will have the following properties: all hosts, links and routers of the platform file are mapped
212  *  to graph nodes; routes are mapped to edges. The platform's zones are not represented in the output.
213  */
214 void platform_graph_export_graphviz(const std::string& output_filename)
215 {
216   auto* g     = xbt_graph_new_graph(0, nullptr);
217   std::map<std::string, xbt_node_t, std::less<>> nodes;
218   std::map<std::string, xbt_edge_t, std::less<>> edges;
219   s4u::Engine::get_instance()->get_netzone_root()->extract_xbt_graph(g, &nodes, &edges);
220
221   std::ofstream fs;
222   fs.open(output_filename, std::ofstream::out);
223   xbt_assert(not fs.fail(), "Failed to open %s", output_filename.c_str());
224
225   if (g->directed)
226     fs << "digraph test {" << std::endl;
227   else
228     fs << "graph test {" << std::endl;
229
230   fs << "  graph [overlap=scale]" << std::endl;
231
232   fs << "  node [shape=box, style=filled]" << std::endl;
233   fs << "  node [width=.3, height=.3, style=filled, color=skyblue]" << std::endl << std::endl;
234
235   for (auto const& [node, _] : nodes)
236     fs << "  \"" << node << "\";" << std::endl;
237
238   for (auto const& [_, edge] : edges) {
239     const char* src_s = static_cast<char*>(edge->src->data);
240     const char* dst_s = static_cast<char*>(edge->dst->data);
241     if (g->directed)
242       fs << "  \"" << src_s << "\" -> \"" << dst_s << "\";" << std::endl;
243     else
244       fs << "  \"" << src_s << "\" -- \"" << dst_s << "\";" << std::endl;
245   }
246   fs << "}" << std::endl;
247   fs.close();
248
249   xbt_graph_free_graph(g, xbt_free_f, xbt_free_f, nullptr);
250 }
251
252 /* Callbacks */
253 static std::vector<NetZoneContainer*> currentContainer; /* push and pop, used only in creation */
254 static void on_netzone_creation(s4u::NetZone const& netzone)
255 {
256   std::string id = netzone.get_name();
257   if (Container::get_root() == nullptr) {
258     auto* root = new NetZoneContainer(id, 0, nullptr);
259     xbt_assert(Container::get_root() == root);
260
261     if (TRACE_smpi_is_enabled()) {
262       auto* mpi = root->get_type()->by_name_or_create<ContainerType>("MPI");
263       if (not TRACE_smpi_is_grouped())
264         mpi->by_name_or_create<StateType>("MPI_STATE");
265       root->get_type()->by_name_or_create("MPI_LINK", mpi, mpi);
266       root->get_type()->by_name_or_create("MIGRATE_LINK", mpi, mpi);
267       mpi->by_name_or_create<StateType>("MIGRATE_STATE");
268     }
269
270     if (TRACE_needs_platform()) {
271       currentContainer.push_back(root);
272     }
273     return;
274   }
275
276   if (TRACE_needs_platform()) {
277     auto level      = static_cast<unsigned>(currentContainer.size());
278     auto* container = new NetZoneContainer(id, level, currentContainer.back());
279     currentContainer.push_back(container);
280   }
281 }
282
283 static void on_link_creation(s4u::Link const& link)
284 {
285   if (currentContainer.empty()) // No ongoing parsing. Are you creating the loopback?
286     return;
287
288   auto* container = new Container(link.get_name(), "LINK", currentContainer.back());
289
290   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (not TRACE_disable_link())) {
291     VariableType* bandwidth = container->get_type()->by_name_or_create("bandwidth", "");
292     bandwidth->set_calling_container(container);
293     bandwidth->set_event(0, link.get_bandwidth());
294     VariableType* latency = container->get_type()->by_name_or_create("latency", "");
295     latency->set_calling_container(container);
296     latency->set_event(0, link.get_latency());
297   }
298
299   if (TRACE_uncategorized()) {
300     container->get_type()->by_name_or_create("bandwidth_used", "0.5 0.5 0.5");
301   }
302 }
303
304 static void on_host_creation(s4u::Host const& host)
305 {
306   if (Container::by_name_or_null(host.get_name())) // This host already exists, do nothing
307     return;
308
309   Container* container  = new HostContainer(host, currentContainer.back());
310   const Container* root = Container::get_root();
311
312   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (not TRACE_disable_speed())) {
313     VariableType* speed = container->get_type()->by_name_or_create("speed", "");
314     speed->set_calling_container(container);
315     speed->set_event(0, host.get_speed());
316
317     VariableType* cores = container->get_type()->by_name_or_create("core_count", "");
318     cores->set_calling_container(container);
319     cores->set_event(0, host.get_core_count());
320   }
321
322   if (TRACE_uncategorized())
323     container->get_type()->by_name_or_create("speed_used", "0.5 0.5 0.5");
324
325   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()) {
326     auto* mpi = container->get_type()->by_name_or_create<ContainerType>("MPI");
327     mpi->by_name_or_create<StateType>("MPI_STATE");
328     root->get_type()->by_name_or_create("MIGRATE_LINK", mpi, mpi);
329     mpi->by_name_or_create<StateType>("MIGRATE_STATE");
330   }
331 }
332
333 static void on_action_state_change(kernel::resource::Action const& action,
334                                    kernel::resource::Action::State /* previous */)
335 {
336   auto n = static_cast<unsigned>(action.get_variable()->get_number_of_constraint());
337
338   for (unsigned i = 0; i < n; i++) {
339     double value = action.get_rate() * action.get_variable()->get_constraint_weight(i);
340     /* Beware of composite actions: ptasks put links and cpus together. Extra pb: we cannot dynamic_cast from void* */
341     kernel::resource::Resource* resource = action.get_variable()->get_constraint(i)->get_id();
342     if (const auto* cpu = dynamic_cast<kernel::resource::CpuImpl*>(resource))
343       resource_set_utilization("HOST", "speed_used", cpu->get_cname(), action.get_category(), value,
344                                action.get_last_update(), simgrid_get_clock() - action.get_last_update());
345
346     if (const auto* link = dynamic_cast<kernel::resource::StandardLinkImpl*>(resource))
347       resource_set_utilization("LINK", "bandwidth_used", link->get_cname(), action.get_category(), value,
348                                action.get_last_update(), simgrid_get_clock() - action.get_last_update());
349   }
350 }
351
352 static void on_platform_created()
353 {
354   currentContainer.clear();
355   std::set<std::string, std::less<>> filter;
356   XBT_DEBUG("Starting graph extraction.");
357   recursiveGraphExtraction(s4u::Engine::get_instance()->get_netzone_root(), Container::get_root(), &filter);
358   XBT_DEBUG("Graph extraction finished.");
359   dump_buffer(true);
360 }
361
362 static void on_actor_creation(s4u::Actor const& actor)
363 {
364   const Container* root      = Container::get_root();
365   Container* container       = Container::by_name(actor.get_host()->get_name());
366   std::string container_name = instr_pid(actor);
367
368   container->create_child(container_name, "ACTOR");
369   auto* actor_type = container->get_type()->by_name_or_create<ContainerType>("ACTOR");
370   auto* state      = actor_type->by_name_or_create<StateType>("ACTOR_STATE");
371   state->add_entity_value("suspend", "1 0 1");
372   state->add_entity_value("sleep", "1 1 0");
373   state->add_entity_value("receive", "1 0 0");
374   state->add_entity_value("send", "0 0 1");
375   state->add_entity_value("execute", "0 1 1");
376   root->get_type()->by_name_or_create("ACTOR_LINK", actor_type, actor_type);
377
378   actor.on_exit([container_name](bool failed) {
379     if (failed)
380       // kill means that this actor no longer exists, let's destroy it
381       Container::by_name(container_name)->remove_from_parent();
382   });
383 }
384
385 static void on_actor_host_change(s4u::Actor const& actor, s4u::Host const& /*previous_location*/)
386 {
387   static long long int counter = 0;
388   Container* container         = Container::by_name(instr_pid(actor));
389   LinkType* link               = Container::get_root()->get_link("ACTOR_LINK");
390
391   // start link
392   link->start_event(container, "M", std::to_string(counter));
393   // destroy existing container of this process
394   container->remove_from_parent();
395   // create new container on the new_host location
396   Container::by_name(actor.get_host()->get_name())->create_child(instr_pid(actor), "ACTOR");
397   // end link
398   link->end_event(Container::by_name(instr_pid(actor)), "M", std::to_string(counter));
399   counter++;
400 }
401
402 static void on_vm_creation(s4u::Host const& host)
403 {
404   const Container* container = new HostContainer(host, currentContainer.back());
405   const Container* root      = Container::get_root();
406   auto* vm                   = container->get_type()->by_name_or_create<ContainerType>("VM");
407   auto* state                = vm->by_name_or_create<StateType>("VM_STATE");
408   state->add_entity_value("suspend", "1 0 1");
409   state->add_entity_value("sleep", "1 1 0");
410   state->add_entity_value("receive", "1 0 0");
411   state->add_entity_value("send", "0 0 1");
412   state->add_entity_value("execute", "0 1 1");
413   root->get_type()->by_name_or_create("VM_LINK", vm, vm);
414   root->get_type()->by_name_or_create("VM_ACTOR_LINK", vm, vm);
415 }
416
417 void define_callbacks()
418 {
419   // always need the callbacks to zones (we need only the root zone), to create the rootContainer and the rootType
420   // properly
421   if (TRACE_needs_platform()) {
422     s4u::Engine::on_platform_created_cb(on_platform_created);
423     s4u::Host::on_creation_cb(on_host_creation);
424     s4u::Host::on_speed_change_cb([](s4u::Host const& host) {
425       Container::by_name(host.get_name())
426           ->get_variable("speed")
427           ->set_event(simgrid_get_clock(), host.get_core_count() * host.get_available_speed());
428     });
429     s4u::Link::on_creation_cb(on_link_creation);
430     s4u::Link::on_bandwidth_change_cb([](s4u::Link const& link) {
431       Container::by_name(link.get_name())
432           ->get_variable("bandwidth")
433           ->set_event(simgrid_get_clock(), sg_bandwidth_factor * link.get_bandwidth());
434     });
435     s4u::NetZone::on_seal_cb([](s4u::NetZone const& /*netzone*/) { currentContainer.pop_back(); });
436     kernel::routing::NetPoint::on_creation.connect([](kernel::routing::NetPoint const& netpoint) {
437       if (netpoint.is_router())
438         new RouterContainer(netpoint.get_name(), currentContainer.back());
439     });
440   }
441
442   s4u::NetZone::on_creation_cb(on_netzone_creation);
443
444   kernel::resource::CpuAction::on_state_change.connect(on_action_state_change);
445   s4u::Link::on_communication_state_change_cb(on_action_state_change);
446
447   if (TRACE_actor_is_enabled()) {
448     s4u::Actor::on_creation_cb(on_actor_creation);
449     s4u::Actor::on_destruction_cb([](s4u::Actor const& actor) {
450       auto container = Container::by_name_or_null(instr_pid(actor));
451       if (container != nullptr)
452         container->remove_from_parent();
453     });
454     s4u::Actor::on_suspend_cb([](s4u::Actor const& actor) {
455       Container::by_name(instr_pid(actor))->get_state("ACTOR_STATE")->push_event("suspend");
456     });
457     s4u::Actor::on_resume_cb(
458         [](s4u::Actor const& actor) { Container::by_name(instr_pid(actor))->get_state("ACTOR_STATE")->pop_event(); });
459     s4u::Actor::on_sleep_cb([](s4u::Actor const& actor) {
460       Container::by_name(instr_pid(actor))->get_state("ACTOR_STATE")->push_event("sleep");
461     });
462     s4u::Actor::on_wake_up_cb(
463         [](s4u::Actor const& actor) { Container::by_name(instr_pid(actor))->get_state("ACTOR_STATE")->pop_event(); });
464     s4u::Exec::on_start_cb([](s4u::Exec const&) {
465       Container::by_name(instr_pid(*s4u::Actor::self()))->get_state("ACTOR_STATE")->push_event("execute");
466     });
467     s4u::Activity::on_completion_cb([](const s4u::Activity&) {
468       Container::by_name(instr_pid(*s4u::Actor::self()))->get_state("ACTOR_STATE")->pop_event();
469     });
470     s4u::Comm::on_send_cb([](s4u::Comm const&) {
471       Container::by_name(instr_pid(*s4u::Actor::self()))->get_state("ACTOR_STATE")->push_event("send");
472     });
473     s4u::Comm::on_recv_cb([](s4u::Comm const&) {
474       Container::by_name(instr_pid(*s4u::Actor::self()))->get_state("ACTOR_STATE")->push_event("receive");
475     });
476     s4u::Actor::on_host_change_cb(on_actor_host_change);
477   }
478
479   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing()) {
480     s4u::Exec::on_start_cb([](s4u::Exec const& exec) {
481       Container::by_name(std::string("rank-") + std::to_string(s4u::Actor::self()->get_pid()))
482           ->get_state("MPI_STATE")
483           ->push_event("computing", new CpuTIData("compute", exec.get_cost()));
484     });
485     s4u::Activity::on_completion_cb([](const s4u::Activity&) {
486       Container::by_name(std::string("rank-") + std::to_string(s4u::Actor::self()->get_pid()))
487           ->get_state("MPI_STATE")
488           ->pop_event();
489     });
490   }
491
492   if (TRACE_vm_is_enabled()) {
493     s4u::Host::on_creation_cb(on_vm_creation);
494     s4u::VirtualMachine::on_start_cb([](s4u::VirtualMachine const& vm) {
495       Container::by_name(vm.get_name())->get_state("VM_STATE")->push_event("start");
496     });
497     s4u::VirtualMachine::on_started_cb(
498         [](s4u::VirtualMachine const& vm) { Container::by_name(vm.get_name())->get_state("VM_STATE")->pop_event(); });
499     s4u::VirtualMachine::on_suspend_cb([](s4u::VirtualMachine const& vm) {
500       Container::by_name(vm.get_name())->get_state("VM_STATE")->push_event("suspend");
501     });
502     s4u::VirtualMachine::on_resume_cb(
503         [](s4u::VirtualMachine const& vm) { Container::by_name(vm.get_name())->get_state("VM_STATE")->pop_event(); });
504     s4u::Host::on_destruction_cb(
505         [](s4u::Host const& host) { Container::by_name(host.get_name())->remove_from_parent(); });
506   }
507 }
508 } // namespace instr
509 } // namespace simgrid