Logo AND Algorithmique Numérique Distribuée

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