Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Prefer \n over std::endl (no need to flush at each end-of-line).
[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::instr {
207
208 /** @brief Creates a file with the topology of the platform file used for the simulator.
209  *
210  *  The graph topology will have the following properties: all hosts, links and routers of the platform file are mapped
211  *  to graph nodes; routes are mapped to edges. The platform's zones are not represented in the output.
212  */
213 void platform_graph_export_graphviz(const std::string& output_filename)
214 {
215   auto* g     = xbt_graph_new_graph(0, nullptr);
216   std::map<std::string, xbt_node_t, std::less<>> nodes;
217   std::map<std::string, xbt_edge_t, std::less<>> edges;
218   s4u::Engine::get_instance()->get_netzone_root()->extract_xbt_graph(g, &nodes, &edges);
219
220   std::ofstream fs;
221   fs.open(output_filename, std::ofstream::out);
222   xbt_assert(not fs.fail(), "Failed to open %s", output_filename.c_str());
223
224   if (g->directed)
225     fs << "digraph test {\n";
226   else
227     fs << "graph test {\n";
228
229   fs << "  graph [overlap=scale]\n";
230
231   fs << "  node [shape=box, style=filled]\n";
232   fs << "  node [width=.3, height=.3, style=filled, color=skyblue]\n\n";
233
234   for (auto const& [node, _] : nodes)
235     fs << "  \"" << node << "\";\n";
236
237   for (auto const& [_, edge] : edges) {
238     const char* src_s = static_cast<char*>(edge->src->data);
239     const char* dst_s = static_cast<char*>(edge->dst->data);
240     if (g->directed)
241       fs << "  \"" << src_s << "\" -> \"" << dst_s << "\";\n";
242     else
243       fs << "  \"" << src_s << "\" -- \"" << dst_s << "\";\n";
244   }
245   fs << "}\n";
246   fs.close();
247
248   xbt_graph_free_graph(g, xbt_free_f, xbt_free_f, nullptr);
249 }
250
251 /* Callbacks */
252 static std::vector<NetZoneContainer*> currentContainer; /* push and pop, used only in creation */
253 static void on_netzone_creation(s4u::NetZone const& netzone)
254 {
255   std::string id = netzone.get_name();
256   if (Container::get_root() == nullptr) {
257     auto* root = new NetZoneContainer(id, 0, nullptr);
258     xbt_assert(Container::get_root() == root);
259
260     if (TRACE_smpi_is_enabled()) {
261       auto* mpi = root->get_type()->by_name_or_create<ContainerType>("MPI");
262       if (not TRACE_smpi_is_grouped())
263         mpi->by_name_or_create<StateType>("MPI_STATE");
264       root->get_type()->by_name_or_create("MPI_LINK", mpi, mpi);
265       root->get_type()->by_name_or_create("MIGRATE_LINK", mpi, mpi);
266       mpi->by_name_or_create<StateType>("MIGRATE_STATE");
267     }
268
269     if (TRACE_needs_platform()) {
270       currentContainer.push_back(root);
271     }
272     return;
273   }
274
275   if (TRACE_needs_platform()) {
276     auto level      = static_cast<unsigned>(currentContainer.size());
277     auto* container = new NetZoneContainer(id, level, currentContainer.back());
278     currentContainer.push_back(container);
279   }
280 }
281
282 static void on_link_creation(s4u::Link const& link)
283 {
284   if (currentContainer.empty()) // No ongoing parsing. Are you creating the loopback?
285     return;
286
287   auto* container = new Container(link.get_name(), "LINK", currentContainer.back());
288
289   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (not TRACE_disable_link())) {
290     VariableType* bandwidth = container->get_type()->by_name_or_create("bandwidth", "");
291     bandwidth->set_calling_container(container);
292     bandwidth->set_event(0, link.get_bandwidth());
293     VariableType* latency = container->get_type()->by_name_or_create("latency", "");
294     latency->set_calling_container(container);
295     latency->set_event(0, link.get_latency());
296   }
297
298   if (TRACE_uncategorized()) {
299     container->get_type()->by_name_or_create("bandwidth_used", "0.5 0.5 0.5");
300   }
301 }
302
303 static void on_host_creation(s4u::Host const& host)
304 {
305   if (Container::by_name_or_null(host.get_name())) // This host already exists, do nothing
306     return;
307
308   Container* container  = new HostContainer(host, currentContainer.back());
309   const Container* root = Container::get_root();
310
311   if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (not TRACE_disable_speed())) {
312     VariableType* speed = container->get_type()->by_name_or_create("speed", "");
313     speed->set_calling_container(container);
314     speed->set_event(0, host.get_speed());
315
316     VariableType* cores = container->get_type()->by_name_or_create("core_count", "");
317     cores->set_calling_container(container);
318     cores->set_event(0, host.get_core_count());
319   }
320
321   if (TRACE_uncategorized())
322     container->get_type()->by_name_or_create("speed_used", "0.5 0.5 0.5");
323
324   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()) {
325     auto* mpi = container->get_type()->by_name_or_create<ContainerType>("MPI");
326     mpi->by_name_or_create<StateType>("MPI_STATE");
327     root->get_type()->by_name_or_create("MIGRATE_LINK", mpi, mpi);
328     mpi->by_name_or_create<StateType>("MIGRATE_STATE");
329   }
330 }
331
332 static void on_action_state_change(kernel::resource::Action const& action,
333                                    kernel::resource::Action::State /* previous */)
334 {
335   auto n = static_cast<unsigned>(action.get_variable()->get_number_of_constraint());
336
337   for (unsigned i = 0; i < n; i++) {
338     double value = action.get_rate() * action.get_variable()->get_constraint_weight(i);
339     /* Beware of composite actions: ptasks put links and cpus together. Extra pb: we cannot dynamic_cast from void* */
340     kernel::resource::Resource* resource = action.get_variable()->get_constraint(i)->get_id();
341     if (const auto* cpu = dynamic_cast<kernel::resource::CpuImpl*>(resource))
342       resource_set_utilization("HOST", "speed_used", cpu->get_cname(), action.get_category(), value,
343                                action.get_last_update(), simgrid_get_clock() - action.get_last_update());
344
345     if (const auto* link = dynamic_cast<kernel::resource::StandardLinkImpl*>(resource))
346       resource_set_utilization("LINK", "bandwidth_used", link->get_cname(), action.get_category(), value,
347                                action.get_last_update(), simgrid_get_clock() - action.get_last_update());
348   }
349 }
350
351 static void on_platform_created()
352 {
353   currentContainer.clear();
354   std::set<std::string, std::less<>> filter;
355   XBT_DEBUG("Starting graph extraction.");
356   recursiveGraphExtraction(s4u::Engine::get_instance()->get_netzone_root(), Container::get_root(), &filter);
357   XBT_DEBUG("Graph extraction finished.");
358   dump_buffer(true);
359 }
360
361 static void on_actor_creation(s4u::Actor const& actor)
362 {
363   const Container* root      = Container::get_root();
364   Container* container       = Container::by_name(actor.get_host()->get_name());
365   std::string container_name = instr_pid(actor);
366
367   container->create_child(container_name, "ACTOR");
368   auto* actor_type = container->get_type()->by_name_or_create<ContainerType>("ACTOR");
369   auto* state      = actor_type->by_name_or_create<StateType>("ACTOR_STATE");
370   state->add_entity_value("suspend", "1 0 1");
371   state->add_entity_value("sleep", "1 1 0");
372   state->add_entity_value("receive", "1 0 0");
373   state->add_entity_value("send", "0 0 1");
374   state->add_entity_value("execute", "0 1 1");
375   root->get_type()->by_name_or_create("ACTOR_LINK", actor_type, actor_type);
376
377   actor.on_exit([container_name](bool failed) {
378     if (failed)
379       // kill means that this actor no longer exists, let's destroy it
380       Container::by_name(container_name)->remove_from_parent();
381   });
382 }
383
384 static void on_actor_host_change(s4u::Actor const& actor, s4u::Host const& /*previous_location*/)
385 {
386   static long long int counter = 0;
387   Container* container         = Container::by_name(instr_pid(actor));
388   LinkType* link               = Container::get_root()->get_link("ACTOR_LINK");
389
390   // start link
391   link->start_event(container, "M", std::to_string(counter));
392   // destroy existing container of this process
393   container->remove_from_parent();
394   // create new container on the new_host location
395   Container::by_name(actor.get_host()->get_name())->create_child(instr_pid(actor), "ACTOR");
396   // end link
397   link->end_event(Container::by_name(instr_pid(actor)), "M", std::to_string(counter));
398   counter++;
399 }
400
401 static void on_vm_creation(s4u::Host const& host)
402 {
403   const Container* container = new HostContainer(host, currentContainer.back());
404   const Container* root      = Container::get_root();
405   auto* vm                   = container->get_type()->by_name_or_create<ContainerType>("VM");
406   auto* state                = vm->by_name_or_create<StateType>("VM_STATE");
407   state->add_entity_value("suspend", "1 0 1");
408   state->add_entity_value("sleep", "1 1 0");
409   state->add_entity_value("receive", "1 0 0");
410   state->add_entity_value("send", "0 0 1");
411   state->add_entity_value("execute", "0 1 1");
412   root->get_type()->by_name_or_create("VM_LINK", vm, vm);
413   root->get_type()->by_name_or_create("VM_ACTOR_LINK", vm, vm);
414 }
415
416 void define_callbacks()
417 {
418   // always need the callbacks to zones (we need only the root zone), to create the rootContainer and the rootType
419   // properly
420   if (TRACE_needs_platform()) {
421     s4u::Engine::on_platform_created_cb(on_platform_created);
422     s4u::Host::on_creation_cb(on_host_creation);
423     s4u::Host::on_speed_change_cb([](s4u::Host const& host) {
424       Container::by_name(host.get_name())
425           ->get_variable("speed")
426           ->set_event(simgrid_get_clock(), host.get_core_count() * host.get_available_speed());
427     });
428     s4u::Link::on_creation_cb(on_link_creation);
429     s4u::Link::on_bandwidth_change_cb([](s4u::Link const& link) {
430       Container::by_name(link.get_name())
431           ->get_variable("bandwidth")
432           ->set_event(simgrid_get_clock(), sg_bandwidth_factor * link.get_bandwidth());
433     });
434     s4u::NetZone::on_seal_cb([](s4u::NetZone const& /*netzone*/) { currentContainer.pop_back(); });
435     kernel::routing::NetPoint::on_creation.connect([](kernel::routing::NetPoint const& netpoint) {
436       if (netpoint.is_router())
437         new RouterContainer(netpoint.get_name(), currentContainer.back());
438     });
439   }
440
441   s4u::NetZone::on_creation_cb(on_netzone_creation);
442
443   kernel::resource::CpuAction::on_state_change.connect(on_action_state_change);
444   s4u::Link::on_communication_state_change_cb(on_action_state_change);
445
446   if (TRACE_actor_is_enabled()) {
447     s4u::Actor::on_creation_cb(on_actor_creation);
448     s4u::Actor::on_destruction_cb([](s4u::Actor const& actor) {
449       auto container = Container::by_name_or_null(instr_pid(actor));
450       if (container != nullptr)
451         container->remove_from_parent();
452     });
453     s4u::Actor::on_suspend_cb([](s4u::Actor const& actor) {
454       Container::by_name(instr_pid(actor))->get_state("ACTOR_STATE")->push_event("suspend");
455     });
456     s4u::Actor::on_resume_cb(
457         [](s4u::Actor const& actor) { Container::by_name(instr_pid(actor))->get_state("ACTOR_STATE")->pop_event(); });
458     s4u::Actor::on_sleep_cb([](s4u::Actor const& actor) {
459       Container::by_name(instr_pid(actor))->get_state("ACTOR_STATE")->push_event("sleep");
460     });
461     s4u::Actor::on_wake_up_cb(
462         [](s4u::Actor const& actor) { Container::by_name(instr_pid(actor))->get_state("ACTOR_STATE")->pop_event(); });
463     s4u::Exec::on_start_cb([](s4u::Exec const&) {
464       Container::by_name(instr_pid(*s4u::Actor::self()))->get_state("ACTOR_STATE")->push_event("execute");
465     });
466     s4u::Activity::on_completion_cb([](const s4u::Activity&) {
467       Container::by_name(instr_pid(*s4u::Actor::self()))->get_state("ACTOR_STATE")->pop_event();
468     });
469     s4u::Comm::on_send_cb([](s4u::Comm const&) {
470       Container::by_name(instr_pid(*s4u::Actor::self()))->get_state("ACTOR_STATE")->push_event("send");
471     });
472     s4u::Comm::on_recv_cb([](s4u::Comm const&) {
473       Container::by_name(instr_pid(*s4u::Actor::self()))->get_state("ACTOR_STATE")->push_event("receive");
474     });
475     s4u::Actor::on_host_change_cb(on_actor_host_change);
476   }
477
478   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing()) {
479     s4u::Exec::on_start_cb([](s4u::Exec const& exec) {
480       Container::by_name(std::string("rank-") + std::to_string(s4u::Actor::self()->get_pid()))
481           ->get_state("MPI_STATE")
482           ->push_event("computing", new CpuTIData("compute", exec.get_cost()));
483     });
484     s4u::Activity::on_completion_cb([](const s4u::Activity&) {
485       Container::by_name(std::string("rank-") + std::to_string(s4u::Actor::self()->get_pid()))
486           ->get_state("MPI_STATE")
487           ->pop_event();
488     });
489   }
490
491   if (TRACE_vm_is_enabled()) {
492     s4u::Host::on_creation_cb(on_vm_creation);
493     s4u::VirtualMachine::on_start_cb([](s4u::VirtualMachine const& vm) {
494       Container::by_name(vm.get_name())->get_state("VM_STATE")->push_event("start");
495     });
496     s4u::VirtualMachine::on_started_cb(
497         [](s4u::VirtualMachine const& vm) { Container::by_name(vm.get_name())->get_state("VM_STATE")->pop_event(); });
498     s4u::VirtualMachine::on_suspend_cb([](s4u::VirtualMachine const& vm) {
499       Container::by_name(vm.get_name())->get_state("VM_STATE")->push_event("suspend");
500     });
501     s4u::VirtualMachine::on_resume_cb(
502         [](s4u::VirtualMachine const& vm) { Container::by_name(vm.get_name())->get_state("VM_STATE")->pop_event(); });
503     s4u::Host::on_destruction_cb(
504         [](s4u::Host const& host) { Container::by_name(host.get_name())->remove_from_parent(); });
505   }
506 }
507 } // namespace simgrid::instr