Logo AND Algorithmique Numérique Distribuée

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