Logo AND Algorithmique Numérique Distribuée

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