Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Clang-tidy: readability-qualified-auto.
[simgrid.git] / src / s4u / s4u_Engine.cpp
1 /* s4u::Engine Simulation Engine and global functions. */
2
3 /* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <simgrid/kernel/routing/NetPoint.hpp>
9 #include <simgrid/modelchecker.h>
10 #include <simgrid/s4u/Engine.hpp>
11
12 #include "src/instr/instr_private.hpp"
13 #include "src/kernel/EngineImpl.hpp"
14 #include "src/kernel/resource/HostImpl.hpp"
15 #include "src/kernel/resource/NetworkModel.hpp"
16 #include "src/kernel/resource/SplitDuplexLinkImpl.hpp"
17 #include "src/kernel/resource/StandardLinkImpl.hpp"
18 #include "src/mc/mc.h"
19 #include "src/mc/mc_replay.hpp"
20 #include "xbt/config.hpp"
21
22 #include <algorithm>
23 #include <string>
24
25 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
27
28 static simgrid::kernel::actor::ActorCode maestro_code;
29
30 namespace simgrid::s4u {
31 xbt::signal<void()> Engine::on_platform_creation;
32 xbt::signal<void()> Engine::on_platform_created;
33 xbt::signal<void()> Engine::on_simulation_start;
34 xbt::signal<void()> Engine::on_simulation_end;
35 xbt::signal<void(double)> Engine::on_time_advance;
36 xbt::signal<void(void)> Engine::on_deadlock;
37
38 Engine* Engine::instance_ = nullptr; /* This singleton is awful, but I don't see no other solution right now. */
39
40 void Engine::initialize(int* argc, char** argv)
41 {
42   xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
43   Engine::instance_ = this;
44   instr::init();
45   pimpl_->initialize(argc, argv);
46   // Either create a new context with maestro or create
47   // a context object with the current context maestro):
48   kernel::actor::create_maestro(maestro_code);
49 }
50
51 Engine::Engine(std::string name) : pimpl_(new kernel::EngineImpl())
52 {
53   int argc   = 1;
54   char* argv = &name[0];
55   initialize(&argc, &argv);
56 }
57
58 Engine::Engine(int* argc, char** argv) : pimpl_(new kernel::EngineImpl())
59 {
60   initialize(argc, argv);
61 }
62
63 Engine::~Engine()
64 {
65   kernel::EngineImpl::shutdown();
66   Engine::instance_ = nullptr;
67 }
68
69 /** @brief Retrieve the engine singleton */
70 Engine* Engine::get_instance()
71 {
72   int argc   = 0;
73   char* argv = nullptr;
74   return get_instance(&argc, &argv);
75 }
76 Engine* Engine::get_instance(int* argc, char** argv)
77 {
78   if (Engine::instance_ == nullptr) {
79     auto* e = new Engine(argc, argv);
80     xbt_assert(Engine::instance_ == e);
81   }
82   return Engine::instance_;
83 }
84 const std::vector<std::string>& Engine::get_cmdline() const
85 {
86   return pimpl_->get_cmdline();
87 }
88
89 void Engine::shutdown() // XBT_ATTRIB_DEPRECATED_v335
90 {
91   delete Engine::instance_;
92 }
93
94 double Engine::get_clock()
95 {
96   if (MC_is_active() || MC_record_replay_is_active()) {
97     return MC_process_clock_get(kernel::actor::ActorImpl::self());
98   } else {
99     return kernel::EngineImpl::get_clock();
100   }
101 }
102
103 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
104                        const std::vector<kernel::resource::Model*>& dependencies)
105 {
106   kernel::actor::simcall_answered([this, &model, &dependencies] { pimpl_->add_model(std::move(model), dependencies); });
107 }
108
109 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
110 {
111   return pimpl_->get_all_models();
112 }
113
114 void Engine::load_platform(const std::string& platf) const
115 {
116   pimpl_->load_platform(platf);
117 }
118
119 void Engine::seal_platform() const
120 {
121   pimpl_->seal_platform();
122 }
123
124 static void flatify_hosts(Engine const& engine, std::stringstream& ss)
125 {
126   // Regular hosts
127   std::vector<Host*> hosts = engine.get_all_hosts();
128
129   for (auto const* h : hosts) {
130     ss << "  <host id=\"" << h->get_name() << "\" speed=\"" << h->get_speed() << "\"";
131     const std::unordered_map<std::string, std::string>* props = h->get_properties();
132     if (h->get_core_count() > 1)
133       ss << " core=\"" << h->get_core_count() << "\"";
134
135     // Sort the properties before displaying them, so that the tests are perfectly reproducible
136     std::vector<std::string> keys;
137     for (auto const& [key, _] : *props)
138       keys.push_back(key);
139     if (not keys.empty()) {
140       ss << ">\n";
141       std::sort(keys.begin(), keys.end());
142       for (const std::string& key : keys)
143         ss << "    <prop id=\"" << key << "\" value=\"" << props->at(key) << "\"/>\n";
144       ss << "  </host>\n";
145     } else {
146       ss << "/>\n";
147     }
148   }
149
150   // Routers
151   std::vector<simgrid::kernel::routing::NetPoint*> netpoints = engine.get_all_netpoints();
152   std::sort(netpoints.begin(), netpoints.end(),
153             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
154               return a->get_name() < b->get_name();
155             });
156
157   for (auto const& src : netpoints)
158     if (src->is_router())
159       ss << "  <router id=\"" << src->get_name() << "\"/>\n";
160 }
161
162 static void flatify_links(Engine const& engine, std::stringstream& ss)
163 {
164   std::vector<Link*> links = engine.get_all_links();
165
166   std::sort(links.begin(), links.end(), [](const Link* a, const Link* b) { return a->get_name() < b->get_name(); });
167
168   for (auto const* link : links) {
169     ss << "  <link id=\"" << link->get_name() << "\"";
170     ss << " bandwidth=\"" << link->get_bandwidth() << "\"";
171     ss << " latency=\"" << link->get_latency() << "\"";
172     if (link->get_concurrency_limit() != -1)
173       ss << " concurrency=\"" << link->get_concurrency_limit() << "\"";
174     if (link->is_shared()) {
175       ss << "/>\n";
176     } else {
177       ss << " sharing_policy=\"FATPIPE\"/>\n";
178     }
179   }
180 }
181
182 static void flatify_routes(Engine const& engine, std::stringstream& ss)
183 {
184   auto hosts     = engine.get_all_hosts();
185   auto netpoints = engine.get_all_netpoints();
186   std::sort(netpoints.begin(), netpoints.end(),
187             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
188               return a->get_name() < b->get_name();
189             });
190
191   for (auto const* src_host : hosts) { // Routes from host
192     const simgrid::kernel::routing::NetPoint* src = src_host->get_netpoint();
193     for (auto const* dst_host : hosts) { // Routes to host
194       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
195       const simgrid::kernel::routing::NetPoint* dst = dst_host->get_netpoint();
196       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
197       if (route.empty())
198         continue;
199       ss << "  <route src=\"" << src_host->get_name() << "\" dst=\"" << dst_host->get_name() << "\">\n  ";
200       for (auto const& link : route)
201         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
202       ss << "\n  </route>\n";
203     }
204
205     for (auto const& dst : netpoints) { // to router
206       if (not dst->is_router())
207         continue;
208       ss << "  <route src=\"" << src_host->get_name() << "\" dst=\"" << dst->get_name() << "\">\n  ";
209       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
210       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
211       for (auto const& link : route)
212         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
213       ss << "\n  </route>\n";
214     }
215   }
216
217   for (auto const& value1 : netpoints) { // Routes from router
218     if (not value1->is_router())
219       continue;
220     for (auto const& value2 : netpoints) { // to router
221       if (not value2->is_router())
222         continue;
223       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
224       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
225       if (route.empty())
226         continue;
227       ss << "  <route src=\"" << value1->get_name() << "\" dst=\"" << value2->get_name() << "\">\n  ";
228       for (auto const& link : route)
229         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
230       ss << "\n  </route>\n";
231     }
232     for (auto const* dst_host : hosts) { // Routes to host
233       ss << "  <route src=\"" << value1->get_name() << "\" dst=\"" << dst_host->get_name() << "\">\n  ";
234       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
235       const simgrid::kernel::routing::NetPoint* netcardDst = dst_host->get_netpoint();
236       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
237       for (auto const& link : route)
238         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
239       ss << "\n  </route>\n";
240     }
241   }
242 }
243 std::string Engine::flatify_platform() const
244 {
245   std::string version = "4.1";
246   std::stringstream ss;
247
248   ss << "<?xml version='1.0'?>\n";
249   ss << "<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n";
250   ss << "<platform version=\"" << version << "\">\n";
251   ss << "<AS id=\"" << get_netzone_root()->get_name() << "\" routing=\"Full\">\n";
252
253   flatify_hosts(*this, ss);
254   flatify_links(*this, ss);
255   flatify_routes(*this, ss);
256
257   ss << "</AS>\n";
258   ss << "</platform>\n";
259   return ss.str();
260 }
261
262 /** Registers the main function of an actor that will be launched from the deployment file */
263 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
264 {
265   kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
266     return xbt::wrap_main(code, std::move(args));
267   };
268   register_function(name, code_factory);
269 }
270
271 /** Registers the main function of an actor that will be launched from the deployment file */
272 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
273 {
274   kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
275     return std::bind(std::move(code), std::move(args));
276   };
277   register_function(name, code_factory);
278 }
279 /** Registers a function as the default main function of actors
280  *
281  * It will be used as fallback when the function requested from the deployment file was not registered.
282  * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
283  */
284 void Engine::register_default(const std::function<void(int, char**)>& code)
285 {
286   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
287 }
288 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
289 {
290   simgrid::kernel::actor::simcall_answered([this, &code]() { pimpl_->register_default(code); });
291 }
292
293 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
294 {
295   simgrid::kernel::actor::simcall_answered([this, name, &code]() { pimpl_->register_function(name, code); });
296 }
297
298 /** Load a deployment file and launch the actors that it contains
299  *
300  * @beginrst
301  * See also: :ref:`deploy`.
302  * @endrst
303  */
304 void Engine::load_deployment(const std::string& deploy) const
305 {
306   pimpl_->load_deployment(deploy);
307 }
308
309 /** Returns the amount of hosts in the platform */
310 size_t Engine::get_host_count() const
311 {
312   return get_all_hosts().size();
313 }
314
315 std::vector<Host*> Engine::get_all_hosts() const
316 {
317   return get_filtered_hosts([](const Host*) { return true; });
318 }
319
320 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
321 {
322   std::vector<Host*> hosts;
323   if (pimpl_->netzone_root_) {
324     hosts = pimpl_->netzone_root_->get_filtered_hosts(filter);
325   }
326   /* Sort hosts in lexicographical order: keep same behavior when the hosts were saved on Engine
327    * Some tests do a get_all_hosts() and selects hosts in this order */
328   std::sort(hosts.begin(), hosts.end(), [](const auto* h1, const auto* h2) { return h1->get_name() < h2->get_name(); });
329
330   return hosts;
331 }
332
333 /** @brief Find a host from its name.
334  *
335  *  @throw std::invalid_argument if the searched host does not exist.
336  */
337 Host* Engine::host_by_name(const std::string& name) const
338 {
339   auto* host = host_by_name_or_null(name);
340   if (not host)
341     throw std::invalid_argument("Host not found: '" + name + "'");
342   return host;
343 }
344
345 /** @brief Find a host from its name (or nullptr if that host does not exist) */
346 Host* Engine::host_by_name_or_null(const std::string& name) const
347 {
348   Host* host = nullptr;
349   if (pimpl_->netzone_root_) {
350     auto* host_impl = pimpl_->netzone_root_->get_host_by_name_or_null(name);
351     if (host_impl)
352       host = host_impl->get_iface();
353   }
354   return host;
355 }
356
357 /** @brief Find a link from its name.
358  *
359  *  @throw std::invalid_argument if the searched link does not exist.
360  */
361 Link* Engine::link_by_name(const std::string& name) const
362 {
363   auto* link = link_by_name_or_null(name);
364   if (not link)
365     throw std::invalid_argument("Link not found: " + name);
366   return link;
367 }
368
369 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
370 {
371   auto* link_impl =
372       pimpl_->netzone_root_ ? pimpl_->netzone_root_->get_split_duplex_link_by_name_or_null(name) : nullptr;
373   if (not link_impl)
374     throw std::invalid_argument("Link not found: " + name);
375   return link_impl->get_iface();
376 }
377
378 /** @brief Find a link from its name (or nullptr if that link does not exist) */
379 Link* Engine::link_by_name_or_null(const std::string& name) const
380 {
381   Link* link = nullptr;
382   if (pimpl_->netzone_root_) {
383     /* keep behavior where internal __loopback__ link from network model is given to user */
384     if (name == "__loopback__")
385       return pimpl_->netzone_root_->get_network_model()->loopback_->get_iface();
386     auto* link_impl = pimpl_->netzone_root_->get_link_by_name_or_null(name);
387     if (link_impl)
388       link = link_impl->get_iface();
389   }
390   return link;
391 }
392
393 /** @brief Find a mailbox from its name or create one if it does not exist) */
394 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
395 {
396   /* two actors may have pushed the same mbox_create simcall at the same time */
397   kernel::activity::MailboxImpl* mbox = kernel::actor::simcall_answered([&name, this] {
398     auto [m, inserted] = pimpl_->mailboxes_.try_emplace(name, nullptr);
399     if (inserted) {
400       m->second = new kernel::activity::MailboxImpl(name);
401       XBT_DEBUG("Creating a mailbox at %p with name %s", m->second, name.c_str());
402     }
403     return m->second;
404   });
405   return mbox->get_iface();
406 }
407
408 /** @brief Returns the amount of links in the platform */
409 size_t Engine::get_link_count() const
410 {
411   int count = 0;
412   if (pimpl_->netzone_root_) {
413     count += pimpl_->netzone_root_->get_link_count();
414     /* keep behavior where internal __loopback__ link from network model is given to user */
415     count += pimpl_->netzone_root_->get_network_model()->loopback_ ? 1 : 0;
416   }
417   return count;
418 }
419
420 /** @brief Returns the list of all links found in the platform */
421 std::vector<Link*> Engine::get_all_links() const
422 {
423   return get_filtered_links([](const Link*) { return true; });
424 }
425
426 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
427 {
428   std::vector<Link*> res;
429   if (pimpl_->netzone_root_) {
430     res = pimpl_->netzone_root_->get_filtered_links(filter);
431     /* keep behavior where internal __loopback__ link from network model is given to user */
432     if (pimpl_->netzone_root_->get_network_model()->loopback_ &&
433         filter(pimpl_->netzone_root_->get_network_model()->loopback_->get_iface()))
434       res.push_back(pimpl_->netzone_root_->get_network_model()->loopback_->get_iface());
435   }
436   return res;
437 }
438
439 size_t Engine::get_actor_count() const
440 {
441   return pimpl_->get_actor_count();
442 }
443
444 std::vector<ActorPtr> Engine::get_all_actors() const
445 {
446   std::vector<ActorPtr> actor_list;
447   for (auto const& [_, actor] : pimpl_->get_actor_list()) {
448     actor_list.push_back(actor->get_iface());
449   }
450   return actor_list;
451 }
452
453 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
454 {
455   std::vector<ActorPtr> actor_list;
456   for (auto const& [_, actor] : pimpl_->get_actor_list()) {
457     if (filter(actor->get_iface()))
458       actor_list.push_back(actor->get_iface());
459   }
460   return actor_list;
461 }
462
463 void Engine::run() const
464 {
465   run_until(-1);
466 }
467 void Engine::run_until(double max_date) const
468 {
469   if (static bool callback_called = false; not callback_called) {
470     on_simulation_start();
471     callback_called = true;
472   }
473   /* Clean IO before the run */
474   fflush(stdout);
475   fflush(stderr);
476
477   pimpl_->run(max_date);
478 }
479
480 void Engine::track_vetoed_activities(std::set<Activity*>* vetoed_activities) const
481 {
482   Activity::set_vetoed_activities(vetoed_activities);
483 }
484
485 /** @brief Retrieve the root netzone, containing all others */
486 s4u::NetZone* Engine::get_netzone_root() const
487 {
488   if (pimpl_->netzone_root_)
489     return pimpl_->netzone_root_->get_iface();
490   return nullptr;
491 }
492 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
493 void Engine::set_netzone_root(const s4u::NetZone* netzone)
494 {
495   xbt_assert(pimpl_->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
496   pimpl_->netzone_root_ = netzone->get_impl();
497 }
498
499 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
500 {
501   if (current->get_name() == name)
502     return current;
503
504   for (auto const& elem : current->get_children()) {
505     NetZone* tmp = netzone_by_name_recursive(elem, name);
506     if (tmp != nullptr) {
507       return tmp;
508     }
509   }
510   return nullptr;
511 }
512
513 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
514 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
515 {
516   return netzone_by_name_recursive(get_netzone_root(), name);
517 }
518
519 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
520 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
521 {
522   auto netp = pimpl_->netpoints_.find(name);
523   return netp == pimpl_->netpoints_.end() ? nullptr : netp->second;
524 }
525
526 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
527 {
528   auto* netp = netpoint_by_name_or_null(name);
529   if (netp == nullptr) {
530     throw std::invalid_argument("Netpoint not found: " + name);
531   }
532   return netp;
533 }
534
535 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
536 {
537   std::vector<kernel::routing::NetPoint*> res;
538   for (auto const& [_, netpoint] : pimpl_->netpoints_)
539     res.push_back(netpoint);
540   return res;
541 }
542
543 /** @brief Register a new netpoint to the system */
544 void Engine::netpoint_register(kernel::routing::NetPoint* point)
545 {
546   simgrid::kernel::actor::simcall_answered([this, point] { pimpl_->netpoints_[point->get_name()] = point; });
547 }
548
549 /** @brief Unregister a given netpoint */
550 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
551 {
552   kernel::actor::simcall_answered([this, point] {
553     pimpl_->netpoints_.erase(point->get_name());
554     delete point;
555   });
556 }
557
558 bool Engine::is_initialized()
559 {
560   return Engine::instance_ != nullptr;
561 }
562 void Engine::set_config(const std::string& str)
563 {
564   config::set_parse(str);
565 }
566 void Engine::set_config(const std::string& name, int value)
567 {
568   config::set_value(name.c_str(), value);
569 }
570 void Engine::set_config(const std::string& name, double value)
571 {
572   config::set_value(name.c_str(), value);
573 }
574 void Engine::set_config(const std::string& name, bool value)
575 {
576   config::set_value(name.c_str(), value);
577 }
578 void Engine::set_config(const std::string& name, const std::string& value)
579 {
580   config::set_value(name.c_str(), value);
581 }
582
583 Engine* Engine::set_default_comm_data_copy_callback(
584     const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
585 {
586   kernel::activity::CommImpl::set_copy_data_callback(callback);
587   return this;
588 }
589
590 } // namespace simgrid::s4u
591
592 /* **************************** Public C interface *************************** */
593 void simgrid_init(int* argc, char** argv)
594 {
595   static simgrid::s4u::Engine e(argc, argv);
596 }
597 void simgrid_load_platform(const char* file)
598 {
599   simgrid::s4u::Engine::get_instance()->load_platform(file);
600 }
601
602 void simgrid_load_deployment(const char* file)
603 {
604   simgrid::s4u::Engine::get_instance()->load_deployment(file);
605 }
606 void simgrid_run()
607 {
608   simgrid::s4u::Engine::get_instance()->run();
609 }
610 void simgrid_run_until(double max_date)
611 {
612   simgrid::s4u::Engine::get_instance()->run_until(max_date);
613 }
614 void simgrid_register_function(const char* name, void (*code)(int, char**))
615 {
616   simgrid::s4u::Engine::get_instance()->register_function(name, code);
617 }
618 void simgrid_register_default(void (*code)(int, char**))
619 {
620   simgrid::s4u::Engine::get_instance()->register_default(code);
621 }
622 double simgrid_get_clock()
623 {
624   return simgrid::s4u::Engine::get_clock();
625 }
626
627 void simgrid_set_maestro(void (*code)(void*), void* data)
628 {
629   maestro_code = std::bind(code, data);
630 }