Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SimcallObservers don't need to be cloned anymore
[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   /** Computes the dependency relation */
56   virtual bool depends(SimcallObserver* other);
57
58   /** Serialize to the given string buffer */
59   virtual void serialize(Simcall& type, std::stringstream& stream) { type = Simcall::UNKNOWN; }
60
61   /** Some simcalls may only be observable under some conditions.
62    * Most simcalls are not visible from the MC because they don't have an observer at all. */
63   virtual bool is_visible() const { return true; }
64   virtual std::string dot_label(int times_considered) const = 0;
65 };
66
67 template <class T> class ResultingSimcall : public SimcallObserver {
68   T result_;
69
70 public:
71   ResultingSimcall() = default;
72   ResultingSimcall(ActorImpl* actor, T default_result) : SimcallObserver(actor), result_(default_result) {}
73   void set_result(T res) { result_ = res; }
74   T get_result() const { return result_; }
75 };
76
77 class RandomSimcall : public SimcallObserver {
78   const int min_;
79   const int max_;
80   int next_value_ = 0;
81
82 public:
83   RandomSimcall(ActorImpl* actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max)
84   {
85     xbt_assert(min < max);
86   }
87   void serialize(Simcall& type, std::stringstream& stream) override;
88   int get_max_consider() const override;
89   void prepare(int times_considered) override;
90   std::string dot_label(int times_considered) const override;
91   int get_value() const { return next_value_; }
92   bool depends(SimcallObserver* other) override;
93 };
94
95 class MutexSimcall : public SimcallObserver {
96   activity::MutexImpl* const mutex_;
97
98 public:
99   MutexSimcall(ActorImpl* actor, activity::MutexImpl* mutex) : SimcallObserver(actor), mutex_(mutex) {}
100   activity::MutexImpl* get_mutex() const { return mutex_; }
101   bool depends(SimcallObserver* other) override;
102 };
103
104 class MutexUnlockSimcall : public MutexSimcall {
105   using MutexSimcall::MutexSimcall;
106
107 public:
108   std::string dot_label(int times_considered) const override;
109 };
110
111 class MutexLockSimcall : public MutexSimcall {
112   const bool blocking_;
113
114 public:
115   MutexLockSimcall(ActorImpl* actor, activity::MutexImpl* mutex, bool blocking = true)
116       : MutexSimcall(actor, mutex), blocking_(blocking)
117   {
118   }
119   bool is_enabled() const override;
120   std::string dot_label(int times_considered) const override;
121 };
122
123 class ConditionWaitSimcall : public ResultingSimcall<bool> {
124   activity::ConditionVariableImpl* const cond_;
125   activity::MutexImpl* const mutex_;
126   const double timeout_;
127
128 public:
129   ConditionWaitSimcall(ActorImpl* actor, activity::ConditionVariableImpl* cond, activity::MutexImpl* mutex,
130                        double timeout = -1.0)
131       : ResultingSimcall(actor, false), cond_(cond), mutex_(mutex), timeout_(timeout)
132   {
133   }
134   bool is_enabled() const override;
135   bool is_visible() const override { return false; }
136   std::string dot_label(int times_considered) const override;
137   activity::ConditionVariableImpl* get_cond() const { return cond_; }
138   activity::MutexImpl* get_mutex() const { return mutex_; }
139   double get_timeout() const { return timeout_; }
140 };
141
142 class SemAcquireSimcall : public ResultingSimcall<bool> {
143   activity::SemaphoreImpl* const sem_;
144   const double timeout_;
145
146 public:
147   SemAcquireSimcall(ActorImpl* actor, activity::SemaphoreImpl* sem, double timeout = -1.0)
148       : ResultingSimcall(actor, false), sem_(sem), timeout_(timeout)
149   {
150   }
151   bool is_enabled() const override;
152   bool is_visible() const override { return false; }
153   std::string dot_label(int times_considered) const override;
154   activity::SemaphoreImpl* get_sem() const { return sem_; }
155   double get_timeout() const { return timeout_; }
156 };
157
158 class ActivityTestSimcall : public ResultingSimcall<bool> {
159   activity::ActivityImpl* const activity_;
160
161 public:
162   ActivityTestSimcall(ActorImpl* actor, activity::ActivityImpl* activity)
163       : ResultingSimcall(actor, true), activity_(activity)
164   {
165   }
166   bool is_visible() const override { return true; }
167   bool depends(SimcallObserver* other) override;
168   std::string dot_label(int times_considered) const override;
169   activity::ActivityImpl* get_activity() const { return activity_; }
170 };
171
172 class ActivityTestanySimcall : public ResultingSimcall<ssize_t> {
173   const std::vector<activity::ActivityImpl*>& activities_;
174   int next_value_ = 0;
175
176 public:
177   ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities)
178       : ResultingSimcall(actor, -1), activities_(activities)
179   {
180   }
181   bool is_visible() const override { return true; }
182   int get_max_consider() const override;
183   void prepare(int times_considered) override;
184   std::string dot_label(int times_considered) const override;
185   const std::vector<activity::ActivityImpl*>& get_activities() const { return activities_; }
186   int get_value() const { return next_value_; }
187 };
188
189 class ActivityWaitSimcall : public ResultingSimcall<bool> {
190   activity::ActivityImpl* activity_;
191   const double timeout_;
192
193 public:
194   ActivityWaitSimcall(ActorImpl* actor, activity::ActivityImpl* activity, double timeout)
195       : ResultingSimcall(actor, false), activity_(activity), timeout_(timeout)
196   {
197   }
198   void serialize(Simcall& type, std::stringstream& stream) override;
199   bool is_visible() const override { return true; }
200   bool is_enabled() const override;
201   std::string dot_label(int times_considered) const override;
202   activity::ActivityImpl* get_activity() const { return activity_; }
203   void set_activity(activity::ActivityImpl* activity) { activity_ = activity; }
204   double get_timeout() const { return timeout_; }
205 };
206
207 class ActivityWaitanySimcall : public ResultingSimcall<ssize_t> {
208   const std::vector<activity::ActivityImpl*>& activities_;
209   const double timeout_;
210   int next_value_ = 0;
211
212 public:
213   ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities, double timeout)
214       : ResultingSimcall(actor, -1), activities_(activities), timeout_(timeout)
215   {
216   }
217   bool is_enabled() const override;
218   bool is_visible() const override { return true; }
219   void prepare(int times_considered) override;
220   int get_max_consider() const override;
221   std::string dot_label(int times_considered) const override;
222   const std::vector<activity::ActivityImpl*>& get_activities() const { return activities_; }
223   double get_timeout() const { return timeout_; }
224   int get_value() const { return next_value_; }
225 };
226
227 class CommIsendSimcall : public SimcallObserver {
228   activity::MailboxImpl* mbox_;
229   double payload_size_;
230   double rate_;
231   unsigned char* src_buff_;
232   size_t src_buff_size_;
233   void* payload_;
234   bool detached_;
235
236 public:
237   bool (*match_fun_)(void*, void*, activity::CommImpl*);
238   void (*clean_fun_)(void*); // used to free the synchro in case of problem after a detached send
239   void (*copy_data_fun_)(activity::CommImpl*, void*, size_t); // used to copy data if not default one
240
241   CommIsendSimcall(ActorImpl* actor, activity::MailboxImpl* mbox, double payload_size, double rate,
242                    unsigned char* src_buff, size_t src_buff_size, bool (*match_fun)(void*, void*, activity::CommImpl*),
243                    void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
244                    void (*copy_data_fun)(activity::CommImpl*, void*, size_t), // used to copy data if not default one
245                    void* payload, bool detached)
246       : SimcallObserver(actor)
247       , mbox_(mbox)
248       , payload_size_(payload_size)
249       , rate_(rate)
250       , src_buff_(src_buff)
251       , src_buff_size_(src_buff_size)
252       , payload_(payload)
253       , detached_(detached)
254       , match_fun_(match_fun)
255       , clean_fun_(clean_fun)
256       , copy_data_fun_(copy_data_fun)
257   {
258   }
259   void serialize(Simcall& type, std::stringstream& stream) override;
260   bool is_visible() const override { return true; }
261   std::string dot_label(int times_considered) const override
262   {
263     return SimcallObserver::dot_label(times_considered) + "iSend";
264   }
265   activity::MailboxImpl* get_mailbox() const { return mbox_; }
266   double get_payload_size() const { return payload_size_; }
267   double get_rate() const { return rate_; }
268   unsigned char* get_src_buff() const { return src_buff_; }
269   size_t get_src_buff_size() const { return src_buff_size_; }
270   void* get_payload() const { return payload_; }
271   bool is_detached() const { return detached_; }
272 };
273
274 class CommIrecvSimcall : public SimcallObserver {
275   activity::MailboxImpl* mbox_;
276   unsigned char* dst_buff_;
277   size_t* dst_buff_size_;
278   void* payload_;
279   double rate_;
280
281 public:
282   bool (*match_fun_)(void*, void*, activity::CommImpl*);
283   void (*copy_data_fun_)(activity::CommImpl*, void*, size_t); // used to copy data if not default one
284
285   CommIrecvSimcall(ActorImpl* actor, activity::MailboxImpl* mbox, unsigned char* dst_buff, size_t* dst_buff_size,
286                    bool (*match_fun)(void*, void*, activity::CommImpl*),
287                    void (*copy_data_fun)(activity::CommImpl*, void*, size_t), void* payload, double rate)
288       : SimcallObserver(actor)
289       , mbox_(mbox)
290       , dst_buff_(dst_buff)
291       , dst_buff_size_(dst_buff_size)
292       , payload_(payload)
293       , rate_(rate)
294       , match_fun_(match_fun)
295       , copy_data_fun_(copy_data_fun)
296   {
297   }
298   void serialize(Simcall& type, std::stringstream& stream) override;
299   bool is_visible() const override { return true; }
300   std::string dot_label(int times_considered) const override
301   {
302     return SimcallObserver::dot_label(times_considered) + "iRecv";
303   }
304   activity::MailboxImpl* get_mailbox() const { return mbox_; }
305   double get_rate() const { return rate_; }
306   unsigned char* get_dst_buff() const { return dst_buff_; }
307   size_t* get_dst_buff_size() const { return dst_buff_size_; }
308   void* get_payload() const { return payload_; }
309 };
310
311 } // namespace actor
312 } // namespace kernel
313 } // namespace simgrid
314
315 #endif