Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / kernel / EngineImpl.hpp
1 /* Copyright (c) 2016-2023. 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 #ifndef SIMGRID_KERNEL_ENGINEIMPL_HPP
7 #define SIMGRID_KERNEL_ENGINEIMPL_HPP
8
9 #include <simgrid/kernel/resource/Model.hpp>
10 #include <simgrid/s4u/Engine.hpp>
11 #include <simgrid/s4u/NetZone.hpp>
12 #include <simgrid/simix.hpp>
13 #include <xbt/dynar.h>
14 #include <xbt/functional.hpp>
15
16 #include "src/kernel/activity/ExecImpl.hpp"
17 #include "src/kernel/activity/IoImpl.hpp"
18 #include "src/kernel/activity/MailboxImpl.hpp"
19 #include "src/kernel/activity/MessageQueueImpl.hpp"
20 #include "src/kernel/activity/SleepImpl.hpp"
21 #include "src/kernel/activity/Synchro.hpp"
22 #include "src/kernel/actor/ActorImpl.hpp"
23
24 #include <boost/intrusive/list.hpp>
25 #include <map>
26 #include <mutex>
27 #include <set>
28 #include <string>
29 #include <unordered_map>
30 #include <vector>
31
32 namespace simgrid::kernel {
33
34 class EngineImpl {
35   std::unordered_map<std::string, routing::NetPoint*> netpoints_;
36   std::unordered_map<std::string, activity::MailboxImpl*> mailboxes_;
37   std::unordered_map<std::string, activity::MessageQueueImpl*> mqueues_;
38
39   std::unordered_map<std::string, actor::ActorCodeFactory> registered_functions; // Maps function names to actor code
40   actor::ActorCodeFactory default_function; // Function to use as a fallback when the provided name matches nothing
41   std::vector<resource::Model*> models_;
42   std::unordered_map<std::string, std::shared_ptr<resource::Model>> models_prio_;
43   routing::NetZoneImpl* netzone_root_ = nullptr;
44   std::set<actor::ActorImpl*> daemons_;
45   std::vector<actor::ActorImpl*> actors_to_run_;
46   std::vector<actor::ActorImpl*> actors_that_ran_;
47   std::map<aid_t, actor::ActorImpl*> actor_list_;
48   boost::intrusive::list<actor::ActorImpl,
49                          boost::intrusive::member_hook<actor::ActorImpl, boost::intrusive::list_member_hook<>,
50                                                        &actor::ActorImpl::kernel_destroy_list_hook>>
51       actors_to_destroy_;
52
53   static double now_;
54   static EngineImpl* instance_;
55   actor::ActorImpl* maestro_ = nullptr;
56   context::ContextFactory* context_factory_ = nullptr;
57
58   std::unique_ptr<void, std::function<int(void*)>> platf_handle_; //!< handle for platform library
59   friend s4u::Engine;
60
61   std::vector<std::string> cmdline_; // Copy of the argv we got (including argv[0])
62
63 public:
64   EngineImpl() = default;
65
66   /* Currently, only one instance is allowed to exist. This is why you can't copy or move it */
67 #ifndef DOXYGEN
68   EngineImpl(const EngineImpl&) = delete;
69   EngineImpl& operator=(const EngineImpl&) = delete;
70   virtual ~EngineImpl();
71   static void shutdown();
72 #endif
73
74   void initialize(int* argc, char** argv);
75   const std::vector<std::string>& get_cmdline() const
76   {
77     return cmdline_;
78   }
79   void load_platform(const std::string& platf);
80   void load_deployment(const std::string& file) const;
81   void seal_platform() const;
82   void register_function(const std::string& name, const actor::ActorCodeFactory& code);
83   void register_default(const actor::ActorCodeFactory& code);
84
85   bool is_maestro(const actor::ActorImpl* actor) const { return actor == maestro_; }
86   void set_maestro(actor::ActorImpl* actor) { maestro_ = actor; }
87   actor::ActorImpl* get_maestro() const { return maestro_; }
88
89   context::ContextFactory* get_context_factory() const { return context_factory_; }
90   void set_context_factory(context::ContextFactory* factory) { context_factory_ = factory; }
91   bool has_context_factory() const { return context_factory_ != nullptr; }
92
93   void context_mod_init() const;
94   /**
95    * @brief Add a model to engine list
96    *
97    * @param model Pointer to model
98    * @param list  List of dependencies for this model
99    */
100   void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
101                  const std::vector<resource::Model*>& dep_models = {});
102
103   /** @brief Get list of all models managed by this engine */
104   const std::vector<resource::Model*>& get_all_models() const { return models_; }
105
106   static bool has_instance() { return s4u::Engine::has_instance(); }
107   static EngineImpl* get_instance()
108   {
109     return s4u::Engine::get_instance()->pimpl_;
110   }
111   static EngineImpl* get_instance(int* argc, char** argv)
112   {
113     return s4u::Engine::get_instance(argc, argv)->pimpl_;
114   }
115
116   actor::ActorCodeFactory get_function(const std::string& name)
117   {
118     auto res = registered_functions.find(name);
119     if (res == registered_functions.end())
120       return default_function;
121     else
122       return res->second;
123   }
124
125   routing::NetZoneImpl* get_netzone_root() const { return netzone_root_; }
126
127   void add_daemon(actor::ActorImpl* d) { daemons_.insert(d); }
128   void remove_daemon(actor::ActorImpl* d);
129   void add_actor_to_run_list(actor::ActorImpl* actor);
130   void add_actor_to_run_list_no_check(actor::ActorImpl* actor);
131   void add_actor_to_destroy_list(actor::ActorImpl& actor) { actors_to_destroy_.push_back(actor); }
132
133   bool has_actors_to_run() const { return not actors_to_run_.empty(); }
134   const actor::ActorImpl* get_first_actor_to_run() const { return actors_to_run_.front(); }
135   const actor::ActorImpl* get_actor_to_run_at(unsigned long int i) const { return actors_to_run_[i]; }
136   unsigned long int get_actor_to_run_count() const { return actors_to_run_.size(); }
137   size_t get_actor_count() const { return actor_list_.size(); }
138   actor::ActorImpl* get_actor_by_pid(aid_t pid);
139   void add_actor(aid_t pid, actor::ActorImpl* actor) { actor_list_[pid] = actor; }
140   void remove_actor(aid_t pid) { actor_list_.erase(pid); }
141
142   const std::map<aid_t, actor::ActorImpl*>& get_actor_list() const { return actor_list_; }
143   const std::vector<actor::ActorImpl*>& get_actors_to_run() const { return actors_to_run_; }
144   const std::vector<actor::ActorImpl*>& get_actors_that_ran() const { return actors_that_ran_; }
145
146   void handle_ended_actions() const;
147   /**
148    * Garbage collection
149    *
150    * Should be called some time to time to free the memory allocated for actors that have finished (or killed).
151    */
152   void empty_trash();
153   void display_all_actor_status() const;
154   void run_all_actors();
155
156   /** @brief Performs a part of the simulation
157    *  @param max_date Maximum date to update the simulation to, or -1
158    *  @return the elapsed time, or -1.0 if no event could be executed
159    *
160    *  This function execute all possible events, update the action states  and returns the time elapsed.
161    *  When you call execute or communicate on a model, the corresponding actions are not executed immediately but only
162    *  when you call solve().
163    *  Note that the returned elapsed time can be zero.
164    */
165   double solve(double max_date) const;
166
167   /** @brief Run the main simulation loop until the specified date (or infinitly if max_date is negative). */
168   void run(double max_date);
169
170   /** @brief Return the current time in milliseconds. */
171   static double get_clock();
172 };
173
174 } // namespace simgrid::kernel
175
176 #endif