1 /* Copyright (c) 2019-2022. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_MC_SIMCALL_OBSERVER_HPP
7 #define SIMGRID_MC_SIMCALL_OBSERVER_HPP
9 #include "simgrid/forward.h"
10 #include "xbt/asserts.h"
11 #include "xbt/utility.hpp"
19 class SimcallObserver {
20 ActorImpl* const issuer_;
23 XBT_DECLARE_ENUM_CLASS(Simcall, UNKNOWN, RANDOM, ISEND, IRECV, COMM_WAIT, COMM_TEST);
25 explicit SimcallObserver(ActorImpl* issuer) : issuer_(issuer) {}
26 ActorImpl* get_issuer() const { return issuer_; }
27 /** Whether this transition can currently be taken without blocking.
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.
32 virtual bool is_enabled() const { return true; }
34 /** Returns the amount of time that this transition can be used.
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
40 virtual int get_max_consider() const { return 1; }
42 /** Prepares the simcall to be used.
44 * For most simcalls, this does nothing. Once enabled, there is nothing to do to prepare a send().
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.
49 * The first time a simcall is considered, times_considered is 0, not 1.
51 virtual void prepare(int times_considered)
52 { /* Nothing to do by default */
55 /** We need to save the observer of simcalls as they get executed to later compute their dependencies in classical
57 virtual SimcallObserver* clone() = 0;
59 /** Computes the dependency relation */
60 virtual bool depends(SimcallObserver* other);
62 /** Serialize to the given buffer */
63 virtual void serialize(Simcall& type, char* buffer) { type = Simcall::UNKNOWN; }
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;
71 template <class T> class ResultingSimcall : public SimcallObserver {
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_; }
81 class RandomSimcall : public SimcallObserver {
87 RandomSimcall(ActorImpl* actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max)
89 xbt_assert(min < max);
91 SimcallObserver* clone() override
93 auto res = new RandomSimcall(get_issuer(), min_, max_);
94 res->next_value_ = next_value_;
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;
105 class MutexSimcall : public SimcallObserver {
106 activity::MutexImpl* const mutex_;
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;
114 class MutexUnlockSimcall : public MutexSimcall {
115 using MutexSimcall::MutexSimcall;
118 SimcallObserver* clone() override { return new MutexUnlockSimcall(get_issuer(), get_mutex()); }
119 std::string dot_label(int times_considered) const override;
122 class MutexLockSimcall : public MutexSimcall {
123 const bool blocking_;
126 MutexLockSimcall(ActorImpl* actor, activity::MutexImpl* mutex, bool blocking = true)
127 : MutexSimcall(actor, mutex), blocking_(blocking)
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;
135 class ConditionWaitSimcall : public ResultingSimcall<bool> {
136 activity::ConditionVariableImpl* const cond_;
137 activity::MutexImpl* const mutex_;
138 const double timeout_;
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)
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_; }
155 class SemAcquireSimcall : public ResultingSimcall<bool> {
156 activity::SemaphoreImpl* const sem_;
157 const double timeout_;
160 SemAcquireSimcall(ActorImpl* actor, activity::SemaphoreImpl* sem, double timeout = -1.0)
161 : ResultingSimcall(actor, false), sem_(sem), timeout_(timeout)
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_; }
172 class ActivityTestSimcall : public ResultingSimcall<bool> {
173 activity::ActivityImpl* const activity_;
176 ActivityTestSimcall(ActorImpl* actor, activity::ActivityImpl* activity)
177 : ResultingSimcall(actor, true), activity_(activity)
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_; }
187 class ActivityTestanySimcall : public ResultingSimcall<ssize_t> {
188 const std::vector<activity::ActivityImpl*>& activities_;
192 ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities)
193 : ResultingSimcall(actor, -1), activities_(activities)
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_; }
205 class ActivityWaitSimcall : public ResultingSimcall<bool> {
206 activity::ActivityImpl* activity_;
207 const double timeout_;
210 ActivityWaitSimcall(ActorImpl* actor, activity::ActivityImpl* activity, double timeout)
211 : ResultingSimcall(actor, false), activity_(activity), timeout_(timeout)
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_; }
224 class ActivityWaitanySimcall : public ResultingSimcall<ssize_t> {
225 const std::vector<activity::ActivityImpl*>& activities_;
226 const double timeout_;
230 ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities, double timeout)
231 : ResultingSimcall(actor, -1), activities_(activities), timeout_(timeout)
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_; }
245 class CommIsendSimcall : public SimcallObserver {
246 activity::MailboxImpl* mbox_;
247 double payload_size_;
249 unsigned char* src_buff_;
250 size_t src_buff_size_;
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
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)
266 , payload_size_(payload_size)
268 , src_buff_(src_buff)
269 , src_buff_size_(src_buff_size)
271 , detached_(detached)
272 , match_fun_(match_fun)
273 , clean_fun_(clean_fun)
274 , copy_data_fun_(copy_data_fun)
277 void serialize(Simcall& type, char* buffer) override;
278 CommIsendSimcall* clone() override
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_);
283 bool is_visible() const override { return true; }
284 std::string dot_label(int times_considered) const override
286 return SimcallObserver::dot_label(times_considered) + "iSend";
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_; }
297 class CommIrecvSimcall : public SimcallObserver {
298 activity::MailboxImpl* mbox_;
299 unsigned char* dst_buff_;
300 size_t* dst_buff_size_;
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
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)
313 , dst_buff_(dst_buff)
314 , dst_buff_size_(dst_buff_size)
317 , match_fun_(match_fun)
318 , copy_data_fun_(copy_data_fun)
321 CommIrecvSimcall* clone() override
323 return new CommIrecvSimcall(get_issuer(), mbox_, dst_buff_, dst_buff_size_, match_fun_, copy_data_fun_, payload_,
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
330 return SimcallObserver::dot_label(times_considered) + "iRecv";
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_; }
340 } // namespace kernel
341 } // namespace simgrid