Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a89924b6524367b2577be3bd78983485de36b682
[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
23 #include <boost/intrusive/list.hpp>
24 #include <map>
25 #include <mutex>
26 #include <set>
27 #include <string>
28 #include <unordered_map>
29 #include <vector>
30
31 namespace simgrid {
32 namespace kernel {
33
34 class EngineImpl {
35   std::map<std::string, s4u::Host*, std::less<>> hosts_;
36   std::map<std::string, resource::LinkImpl*, std::less<>> links_;
37   std::unordered_map<std::string, routing::NetPoint*> netpoints_;
38   std::unordered_map<std::string, activity::MailboxImpl*> mailboxes_;
39
40   std::unordered_map<std::string, actor::ActorCodeFactory> registered_functions; // Maps function names to actor code
41   actor::ActorCodeFactory default_function; // Function to use as a fallback when the provided name matches nothing
42   std::vector<resource::Model*> models_;
43   std::unordered_map<std::string, std::shared_ptr<resource::Model>> models_prio_;
44   routing::NetZoneImpl* netzone_root_ = nullptr;
45   std::set<actor::ActorImpl*> daemons_;
46   std::vector<actor::ActorImpl*> actors_to_run_;
47   std::vector<actor::ActorImpl*> actors_that_ran_;
48   std::map<aid_t, actor::ActorImpl*> actor_list_;
49   boost::intrusive::list<actor::ActorImpl,
50                          boost::intrusive::member_hook<actor::ActorImpl, boost::intrusive::list_member_hook<>,
51                                                        &actor::ActorImpl::kernel_destroy_list_hook>>
52       actors_to_destroy_;
53 #if SIMGRID_HAVE_MC
54   /* MCer cannot read members actor_list_ and actors_to_destroy_ above in the remote process, so we copy the info it
55    * needs in a dynar.
56    * FIXME: This is supposed to be a temporary hack.
57    * A better solution would be to change the split between MCer and MCed, where the responsibility
58    *   to compute the list of the enabled transitions goes to the MCed.
59    * That way, the MCer would not need to have the list of actors on its side.
60    * These info could be published by the MCed to the MCer in a way inspired of vd.so
61    */
62   xbt_dynar_t actors_vector_      = xbt_dynar_new(sizeof(actor::ActorImpl*), nullptr);
63   xbt_dynar_t dead_actors_vector_ = xbt_dynar_new(sizeof(actor::ActorImpl*), nullptr);
64 #endif
65
66   std::vector<xbt::Task<void()>> tasks;
67   std::vector<xbt::Task<void()>> tasksTemp;
68
69   std::mutex mutex_;
70   friend s4u::Engine;
71
72 public:
73   EngineImpl() = default;
74
75   EngineImpl(const EngineImpl&) = delete;
76   EngineImpl& operator=(const EngineImpl&) = delete;
77   virtual ~EngineImpl();
78
79   void load_deployment(const std::string& file) const;
80   void register_function(const std::string& name, const actor::ActorCodeFactory& code);
81   void register_default(const actor::ActorCodeFactory& code);
82
83   /**
84    * @brief Add a model to engine list
85    *
86    * @param model Pointer to model
87    * @param list  List of dependencies for this model
88    */
89   void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
90                  const std::vector<resource::Model*>& dep_models = {});
91
92   /** @brief Get list of all models managed by this engine */
93   const std::vector<resource::Model*>& get_all_models() const { return models_; }
94
95   static EngineImpl* get_instance() { return simgrid::s4u::Engine::get_instance()->pimpl; }
96   actor::ActorCodeFactory get_function(const std::string& name)
97   {
98     auto res = registered_functions.find(name);
99     if (res == registered_functions.end())
100       return default_function;
101     else
102       return res->second;
103   }
104   void add_daemon(actor::ActorImpl* d) { daemons_.insert(d); }
105   void remove_daemon(actor::ActorImpl* d);
106   void add_actor_to_run_list(actor::ActorImpl* actor);
107   void add_actor_to_run_list_no_check(actor::ActorImpl* actor);
108   void add_actor_to_destroy_list(actor::ActorImpl& actor) { actors_to_destroy_.push_back(actor); }
109
110   bool has_actors_to_run() const { return not actors_to_run_.empty(); }
111   const actor::ActorImpl* get_first_actor_to_run() const { return actors_to_run_.front(); }
112   const actor::ActorImpl* get_actor_to_run_at(unsigned long int i) const { return actors_to_run_[i]; }
113   unsigned long int get_actor_to_run_count() const { return actors_to_run_.size(); }
114   size_t get_actor_count() const { return actor_list_.size(); }
115   actor::ActorImpl* get_actor_by_pid(aid_t pid);
116   void add_actor(aid_t pid, actor::ActorImpl* actor) { actor_list_[pid] = actor; }
117   void remove_actor(aid_t pid) { actor_list_.erase(pid); }
118
119 #if SIMGRID_HAVE_MC
120   xbt_dynar_t get_actors_vector() const { return actors_vector_; }
121   xbt_dynar_t get_dead_actors_vector() const { return dead_actors_vector_; }
122   void reset_actor_dynar() { xbt_dynar_reset(actors_vector_); }
123   void add_actor_to_dynar(actor::ActorImpl* actor) { xbt_dynar_push_as(actors_vector_, actor::ActorImpl*, actor); }
124   void add_dead_actor_to_dynar(actor::ActorImpl* actor)
125   {
126     xbt_dynar_push_as(dead_actors_vector_, actor::ActorImpl*, actor);
127   }
128 #endif
129
130   const std::map<aid_t, actor::ActorImpl*>& get_actor_list() const { return actor_list_; }
131   const std::vector<actor::ActorImpl*>& get_actors_to_run() const { return actors_to_run_; }
132   const std::vector<actor::ActorImpl*>& get_actors_that_ran() const { return actors_that_ran_; }
133
134   std::mutex& get_mutex() { return mutex_; }
135   bool execute_tasks();
136   void add_task(xbt::Task<void()>&& t) { tasks.push_back(std::move(t)); }
137   void wake_all_waiting_actors() const;
138   /**
139    * Garbage collection
140    *
141    * Should be called some time to time to free the memory allocated for actors that have finished (or killed).
142    */
143   void empty_trash();
144   void display_all_actor_status() const;
145   void run_all_actors();
146
147   /** @brief Run the main simulation loop. */
148   void run();
149 };
150
151 } // namespace kernel
152 } // namespace simgrid
153
154 #endif