Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into no_simix_global
[simgrid.git] / src / kernel / EngineImpl.hpp
1 /* Copyright (c) 2016-2021. 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/SleepImpl.hpp"
20 #include "src/kernel/activity/SynchroRaw.hpp"
21 #include "src/kernel/actor/ActorImpl.hpp"
22 #include "src/surf/SplitDuplexLinkImpl.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 {
33 namespace kernel {
34
35 class EngineImpl {
36   std::map<std::string, s4u::Host*, std::less<>> hosts_;
37   std::map<std::string, resource::LinkImpl*, std::less<>> links_;
38   /* save split-duplex links separately, keep links_ with only LinkImpl* seen by the user
39    * members of a split-duplex are saved in the links_ */
40   std::map<std::string, std::unique_ptr<resource::SplitDuplexLinkImpl>, std::less<>> split_duplex_links_;
41   std::unordered_map<std::string, routing::NetPoint*> netpoints_;
42   std::unordered_map<std::string, activity::MailboxImpl*> mailboxes_;
43
44   std::unordered_map<std::string, actor::ActorCodeFactory> registered_functions; // Maps function names to actor code
45   actor::ActorCodeFactory default_function; // Function to use as a fallback when the provided name matches nothing
46   std::vector<resource::Model*> models_;
47   std::unordered_map<std::string, std::shared_ptr<resource::Model>> models_prio_;
48   routing::NetZoneImpl* netzone_root_ = nullptr;
49   std::set<actor::ActorImpl*> daemons_;
50   std::vector<actor::ActorImpl*> actors_to_run_;
51   std::vector<actor::ActorImpl*> actors_that_ran_;
52   std::map<aid_t, actor::ActorImpl*> actor_list_;
53   boost::intrusive::list<actor::ActorImpl,
54                          boost::intrusive::member_hook<actor::ActorImpl, boost::intrusive::list_member_hook<>,
55                                                        &actor::ActorImpl::kernel_destroy_list_hook>>
56       actors_to_destroy_;
57 #if SIMGRID_HAVE_MC
58   /* MCer cannot read members actor_list_ and actors_to_destroy_ above in the remote process, so we copy the info it
59    * needs in a dynar.
60    * FIXME: This is supposed to be a temporary hack.
61    * A better solution would be to change the split between MCer and MCed, where the responsibility
62    *   to compute the list of the enabled transitions goes to the MCed.
63    * That way, the MCer would not need to have the list of actors on its side.
64    * These info could be published by the MCed to the MCer in a way inspired of vd.so
65    */
66   xbt_dynar_t actors_vector_      = xbt_dynar_new(sizeof(actor::ActorImpl*), nullptr);
67   xbt_dynar_t dead_actors_vector_ = xbt_dynar_new(sizeof(actor::ActorImpl*), nullptr);
68 #endif
69
70   std::vector<xbt::Task<void()>> tasks;
71
72   std::mutex mutex_;
73   static EngineImpl* instance_;
74
75   std::unique_ptr<void, std::function<int(void*)>> platf_handle_; //!< handle for platform library
76   friend s4u::Engine;
77
78 public:
79   EngineImpl() = default;
80
81   /* Currently, only one instance is allowed to exist. This is why you can't copy or move it */
82 #ifndef DOXYGEN
83   EngineImpl(const EngineImpl&) = delete;
84   EngineImpl& operator=(const EngineImpl&) = delete;
85   virtual ~EngineImpl();
86   static void shutdown();
87 #endif
88
89   void load_platform(const std::string& platf);
90   void load_deployment(const std::string& file) const;
91   void register_function(const std::string& name, const actor::ActorCodeFactory& code);
92   void register_default(const actor::ActorCodeFactory& code);
93
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 EngineImpl* get_instance() { return simgrid::s4u::Engine::get_instance()->pimpl; }
107
108   actor::ActorCodeFactory get_function(const std::string& name)
109   {
110     auto res = registered_functions.find(name);
111     if (res == registered_functions.end())
112       return default_function;
113     else
114       return res->second;
115   }
116   void add_daemon(actor::ActorImpl* d) { daemons_.insert(d); }
117   void remove_daemon(actor::ActorImpl* d);
118   void add_actor_to_run_list(actor::ActorImpl* actor);
119   void add_actor_to_run_list_no_check(actor::ActorImpl* actor);
120   void add_actor_to_destroy_list(actor::ActorImpl& actor) { actors_to_destroy_.push_back(actor); }
121
122   bool has_actors_to_run() const { return not actors_to_run_.empty(); }
123   const actor::ActorImpl* get_first_actor_to_run() const { return actors_to_run_.front(); }
124   const actor::ActorImpl* get_actor_to_run_at(unsigned long int i) const { return actors_to_run_[i]; }
125   unsigned long int get_actor_to_run_count() const { return actors_to_run_.size(); }
126   size_t get_actor_count() const { return actor_list_.size(); }
127   actor::ActorImpl* get_actor_by_pid(aid_t pid);
128   void add_actor(aid_t pid, actor::ActorImpl* actor) { actor_list_[pid] = actor; }
129   void remove_actor(aid_t pid) { actor_list_.erase(pid); }
130   void add_split_duplex_link(const std::string& name, std::unique_ptr<resource::SplitDuplexLinkImpl> link);
131
132 #if SIMGRID_HAVE_MC
133   xbt_dynar_t get_actors_vector() const { return actors_vector_; }
134   xbt_dynar_t get_dead_actors_vector() const { return dead_actors_vector_; }
135   void reset_actor_dynar() { xbt_dynar_reset(actors_vector_); }
136   void add_actor_to_dynar(actor::ActorImpl* actor) { xbt_dynar_push_as(actors_vector_, actor::ActorImpl*, actor); }
137   void add_dead_actor_to_dynar(actor::ActorImpl* actor)
138   {
139     xbt_dynar_push_as(dead_actors_vector_, actor::ActorImpl*, actor);
140   }
141 #endif
142
143   const std::map<aid_t, actor::ActorImpl*>& get_actor_list() const { return actor_list_; }
144   const std::vector<actor::ActorImpl*>& get_actors_to_run() const { return actors_to_run_; }
145   const std::vector<actor::ActorImpl*>& get_actors_that_ran() const { return actors_that_ran_; }
146
147   std::mutex& get_mutex() { return mutex_; }
148   bool execute_tasks();
149   void add_task(xbt::Task<void()>&& t) { tasks.push_back(std::move(t)); }
150   void wake_all_waiting_actors() const;
151   /**
152    * Garbage collection
153    *
154    * Should be called some time to time to free the memory allocated for actors that have finished (or killed).
155    */
156   void empty_trash();
157   void display_all_actor_status() const;
158   void run_all_actors();
159
160   /** @brief Run the main simulation loop. */
161   void run();
162 };
163
164 } // namespace kernel
165 } // namespace simgrid
166
167 #endif