Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Split TransitionAny and TransitionRandom to their own files
[simgrid.git] / src / kernel / actor / SimcallObserver.hpp
1 /* Copyright (c) 2019-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 #ifndef SIMGRID_MC_SIMCALL_OBSERVER_HPP
7 #define SIMGRID_MC_SIMCALL_OBSERVER_HPP
8
9 #include "simgrid/forward.h"
10 #include "src/mc/transition/Transition.hpp"
11 #include "xbt/asserts.h"
12
13 #include <string>
14
15 namespace simgrid {
16 namespace kernel {
17 namespace actor {
18
19 class SimcallObserver {
20   ActorImpl* const issuer_;
21
22 public:
23   explicit SimcallObserver(ActorImpl* issuer) : issuer_(issuer) {}
24   ActorImpl* get_issuer() const { return issuer_; }
25   /** Whether this transition can currently be taken without blocking.
26    *
27    * For example, a mutex_lock is not enabled when the mutex is not free.
28    * A comm_receive is not enabled before the corresponding send has been issued.
29    */
30   virtual bool is_enabled() { return true; }
31
32   /** Returns the amount of time that this transition can be used.
33    *
34    * If it's 1 (as with send/wait), there is no need to fork the state space exploration on this point.
35    * If it's more than one (as with mc_random or waitany), we need to consider this transition several times to start
36    * differing branches
37    */
38   virtual int get_max_consider() { return 1; }
39
40   /** Prepares the simcall to be used.
41    *
42    * For most simcalls, this does nothing. Once enabled, there is nothing to do to prepare a send().
43    *
44    * It is useful only for the simcalls that can be used several times, such as waitany() or random().
45    * For them, prepare() selects the right outcome for the time being considered.
46    *
47    * The first time a simcall is considered, times_considered is 0, not 1.
48    */
49   virtual void prepare(int times_considered)
50   { /* Nothing to do by default */
51   }
52
53   /** Computes the dependency relation */
54   virtual bool depends(SimcallObserver* other);
55
56   /** Serialize to the given string buffer */
57   virtual void serialize(std::stringstream& stream) const;
58
59   /** Some simcalls may only be observable under some conditions.
60    * Most simcalls are not visible from the MC because they don't have an observer at all. */
61   virtual bool is_visible() const { return true; }
62 };
63
64 template <class T> class ResultingSimcall : public SimcallObserver {
65   T result_;
66
67 public:
68   ResultingSimcall() = default;
69   ResultingSimcall(ActorImpl* actor, T default_result) : SimcallObserver(actor), result_(default_result) {}
70   void set_result(T res) { result_ = res; }
71   T get_result() const { return result_; }
72 };
73
74 class RandomSimcall : public SimcallObserver {
75   const int min_;
76   const int max_;
77   int next_value_ = 0;
78
79 public:
80   RandomSimcall(ActorImpl* actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max)
81   {
82     xbt_assert(min < max);
83   }
84   void serialize(std::stringstream& stream) const override;
85   int get_max_consider() override;
86   void prepare(int times_considered) override;
87   int get_value() const { return next_value_; }
88   bool depends(SimcallObserver* other) override;
89 };
90
91 class MutexSimcall : public SimcallObserver {
92   activity::MutexImpl* const mutex_;
93
94 public:
95   MutexSimcall(ActorImpl* actor, activity::MutexImpl* mutex) : SimcallObserver(actor), mutex_(mutex) {}
96   activity::MutexImpl* get_mutex() const { return mutex_; }
97   bool depends(SimcallObserver* other) override;
98 };
99
100 class MutexUnlockSimcall : public MutexSimcall {
101   using MutexSimcall::MutexSimcall;
102 };
103
104 class MutexLockSimcall : public MutexSimcall {
105   const bool blocking_;
106
107 public:
108   MutexLockSimcall(ActorImpl* actor, activity::MutexImpl* mutex, bool blocking = true)
109       : MutexSimcall(actor, mutex), blocking_(blocking)
110   {
111   }
112   bool is_enabled() override;
113 };
114
115 class ConditionWaitSimcall : public ResultingSimcall<bool> {
116   activity::ConditionVariableImpl* const cond_;
117   activity::MutexImpl* const mutex_;
118   const double timeout_;
119
120 public:
121   ConditionWaitSimcall(ActorImpl* actor, activity::ConditionVariableImpl* cond, activity::MutexImpl* mutex,
122                        double timeout = -1.0)
123       : ResultingSimcall(actor, false), cond_(cond), mutex_(mutex), timeout_(timeout)
124   {
125   }
126   bool is_enabled() override;
127   bool is_visible() const override { return false; }
128   activity::ConditionVariableImpl* get_cond() const { return cond_; }
129   activity::MutexImpl* get_mutex() const { return mutex_; }
130   double get_timeout() const { return timeout_; }
131 };
132
133 class SemAcquireSimcall : public ResultingSimcall<bool> {
134   activity::SemaphoreImpl* const sem_;
135   const double timeout_;
136
137 public:
138   SemAcquireSimcall(ActorImpl* actor, activity::SemaphoreImpl* sem, double timeout = -1.0)
139       : ResultingSimcall(actor, false), sem_(sem), timeout_(timeout)
140   {
141   }
142   bool is_enabled() override;
143   bool is_visible() const override { return false; }
144   activity::SemaphoreImpl* get_sem() const { return sem_; }
145   double get_timeout() const { return timeout_; }
146 };
147
148 class ActivityTestSimcall : public ResultingSimcall<bool> {
149   activity::ActivityImpl* const activity_;
150
151 public:
152   ActivityTestSimcall(ActorImpl* actor, activity::ActivityImpl* activity)
153       : ResultingSimcall(actor, true), activity_(activity)
154   {
155   }
156   bool is_visible() const override { return true; }
157   activity::ActivityImpl* get_activity() const { return activity_; }
158   void serialize(std::stringstream& stream) const override;
159 };
160
161 class ActivityTestanySimcall : public ResultingSimcall<ssize_t> {
162   const std::vector<activity::ActivityImpl*>& activities_;
163   std::vector<int> indexes_; // indexes in activities_ pointing to ready activities (=whose test() is positive)
164   int next_value_ = 0;
165
166 public:
167   ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities);
168   bool is_visible() const override { return true; }
169   bool is_enabled() override { return true; /* can return -1 if no activity is ready */ }
170   void serialize(std::stringstream& stream) const override;
171   int get_max_consider() override;
172   void prepare(int times_considered) override;
173   const std::vector<activity::ActivityImpl*>& get_activities() const { return activities_; }
174   int get_value() const { return next_value_; }
175 };
176
177 class ActivityWaitSimcall : public ResultingSimcall<bool> {
178   activity::ActivityImpl* activity_;
179   const double timeout_;
180
181 public:
182   ActivityWaitSimcall(ActorImpl* actor, activity::ActivityImpl* activity, double timeout)
183       : ResultingSimcall(actor, false), activity_(activity), timeout_(timeout)
184   {
185   }
186   void serialize(std::stringstream& stream) const override;
187   bool is_visible() const override { return true; }
188   bool is_enabled() override;
189   activity::ActivityImpl* get_activity() const { return activity_; }
190   void set_activity(activity::ActivityImpl* activity) { activity_ = activity; }
191   double get_timeout() const { return timeout_; }
192 };
193
194 class ActivityWaitanySimcall : public ResultingSimcall<ssize_t> {
195   const std::vector<activity::ActivityImpl*>& activities_;
196   std::vector<int> indexes_; // indexes in activities_ pointing to ready activities (=whose test() is positive)
197   const double timeout_;
198   int next_value_ = 0;
199
200 public:
201   ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities, double timeout);
202   bool is_enabled() override;
203   void serialize(std::stringstream& stream) const override;
204   bool is_visible() const override { return true; }
205   void prepare(int times_considered) override;
206   int get_max_consider() override;
207   const std::vector<activity::ActivityImpl*>& get_activities() const { return activities_; }
208   double get_timeout() const { return timeout_; }
209   int get_value() const { return next_value_; }
210 };
211
212 class CommIsendSimcall : public SimcallObserver {
213   activity::MailboxImpl* mbox_;
214   double payload_size_;
215   double rate_;
216   unsigned char* src_buff_;
217   size_t src_buff_size_;
218   void* payload_;
219   bool detached_;
220   activity::CommImpl* comm_;
221   int tag_;
222
223   bool (*match_fun_)(void*, void*, activity::CommImpl*);
224   void (*clean_fun_)(void*); // used to free the synchro in case of problem after a detached send
225   void (*copy_data_fun_)(activity::CommImpl*, void*, size_t); // used to copy data if not default one
226
227 public:
228   CommIsendSimcall(ActorImpl* actor, activity::MailboxImpl* mbox, double payload_size, double rate,
229                    unsigned char* src_buff, size_t src_buff_size, bool (*match_fun)(void*, void*, activity::CommImpl*),
230                    void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
231                    void (*copy_data_fun)(activity::CommImpl*, void*, size_t), // used to copy data if not default one
232                    void* payload, bool detached)
233       : SimcallObserver(actor)
234       , mbox_(mbox)
235       , payload_size_(payload_size)
236       , rate_(rate)
237       , src_buff_(src_buff)
238       , src_buff_size_(src_buff_size)
239       , payload_(payload)
240       , detached_(detached)
241       , match_fun_(match_fun)
242       , clean_fun_(clean_fun)
243       , copy_data_fun_(copy_data_fun)
244   {
245   }
246   void serialize(std::stringstream& stream) const override;
247   bool is_visible() const override { return true; }
248   activity::MailboxImpl* get_mailbox() const { return mbox_; }
249   double get_payload_size() const { return payload_size_; }
250   double get_rate() const { return rate_; }
251   unsigned char* get_src_buff() const { return src_buff_; }
252   size_t get_src_buff_size() const { return src_buff_size_; }
253   void* get_payload() const { return payload_; }
254   bool is_detached() const { return detached_; }
255   void set_comm(activity::CommImpl* comm) { comm_ = comm; }
256   void set_tag(int tag) { tag_ = tag; }
257
258   auto get_match_fun() const { return match_fun_; }
259   auto get_clean_fun() const { return clean_fun_; }
260   auto get_copy_data_fun() const { return copy_data_fun_; }
261 };
262
263 class CommIrecvSimcall : public SimcallObserver {
264   activity::MailboxImpl* mbox_;
265   unsigned char* dst_buff_;
266   size_t* dst_buff_size_;
267   void* payload_;
268   double rate_;
269   int tag_;
270   activity::CommImpl* comm_;
271
272   bool (*match_fun_)(void*, void*, activity::CommImpl*);
273   void (*copy_data_fun_)(activity::CommImpl*, void*, size_t); // used to copy data if not default one
274
275 public:
276   CommIrecvSimcall(ActorImpl* actor, activity::MailboxImpl* mbox, unsigned char* dst_buff, size_t* dst_buff_size,
277                    bool (*match_fun)(void*, void*, activity::CommImpl*),
278                    void (*copy_data_fun)(activity::CommImpl*, void*, size_t), void* payload, double rate)
279       : SimcallObserver(actor)
280       , mbox_(mbox)
281       , dst_buff_(dst_buff)
282       , dst_buff_size_(dst_buff_size)
283       , payload_(payload)
284       , rate_(rate)
285       , match_fun_(match_fun)
286       , copy_data_fun_(copy_data_fun)
287   {
288   }
289   void serialize(std::stringstream& stream) const override;
290   bool is_visible() const override { return true; }
291   activity::MailboxImpl* get_mailbox() const { return mbox_; }
292   double get_rate() const { return rate_; }
293   unsigned char* get_dst_buff() const { return dst_buff_; }
294   size_t* get_dst_buff_size() const { return dst_buff_size_; }
295   void* get_payload() const { return payload_; }
296   void set_comm(activity::CommImpl* comm) { comm_ = comm; }
297   void set_tag(int tag) { tag_ = tag; }
298
299   auto get_match_fun() const { return match_fun_; };
300   auto get_copy_data_fun() const { return copy_data_fun_; }
301 };
302
303 } // namespace actor
304 } // namespace kernel
305 } // namespace simgrid
306
307 #endif