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 /** Computes the dependency relation */
56 virtual bool depends(SimcallObserver* other);
58 /** Serialize to the given string buffer */
59 virtual void serialize(Simcall& type, std::stringstream& stream) { type = Simcall::UNKNOWN; }
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;
67 template <class T> class ResultingSimcall : public SimcallObserver {
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_; }
77 class RandomSimcall : public SimcallObserver {
83 RandomSimcall(ActorImpl* actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max)
85 xbt_assert(min < max);
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;
95 class MutexSimcall : public SimcallObserver {
96 activity::MutexImpl* const mutex_;
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;
104 class MutexUnlockSimcall : public MutexSimcall {
105 using MutexSimcall::MutexSimcall;
108 std::string dot_label(int times_considered) const override;
111 class MutexLockSimcall : public MutexSimcall {
112 const bool blocking_;
115 MutexLockSimcall(ActorImpl* actor, activity::MutexImpl* mutex, bool blocking = true)
116 : MutexSimcall(actor, mutex), blocking_(blocking)
119 bool is_enabled() const override;
120 std::string dot_label(int times_considered) const override;
123 class ConditionWaitSimcall : public ResultingSimcall<bool> {
124 activity::ConditionVariableImpl* const cond_;
125 activity::MutexImpl* const mutex_;
126 const double timeout_;
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)
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_; }
142 class SemAcquireSimcall : public ResultingSimcall<bool> {
143 activity::SemaphoreImpl* const sem_;
144 const double timeout_;
147 SemAcquireSimcall(ActorImpl* actor, activity::SemaphoreImpl* sem, double timeout = -1.0)
148 : ResultingSimcall(actor, false), sem_(sem), timeout_(timeout)
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_; }
158 class ActivityTestSimcall : public ResultingSimcall<bool> {
159 activity::ActivityImpl* const activity_;
162 ActivityTestSimcall(ActorImpl* actor, activity::ActivityImpl* activity)
163 : ResultingSimcall(actor, true), activity_(activity)
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_; }
172 class ActivityTestanySimcall : public ResultingSimcall<ssize_t> {
173 const std::vector<activity::ActivityImpl*>& activities_;
177 ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities)
178 : ResultingSimcall(actor, -1), activities_(activities)
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_; }
189 class ActivityWaitSimcall : public ResultingSimcall<bool> {
190 activity::ActivityImpl* activity_;
191 const double timeout_;
194 ActivityWaitSimcall(ActorImpl* actor, activity::ActivityImpl* activity, double timeout)
195 : ResultingSimcall(actor, false), activity_(activity), timeout_(timeout)
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_; }
207 class ActivityWaitanySimcall : public ResultingSimcall<ssize_t> {
208 const std::vector<activity::ActivityImpl*>& activities_;
209 const double timeout_;
213 ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities, double timeout)
214 : ResultingSimcall(actor, -1), activities_(activities), timeout_(timeout)
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_; }
227 class CommIsendSimcall : public SimcallObserver {
228 activity::MailboxImpl* mbox_;
229 double payload_size_;
231 unsigned char* src_buff_;
232 size_t src_buff_size_;
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
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)
248 , payload_size_(payload_size)
250 , src_buff_(src_buff)
251 , src_buff_size_(src_buff_size)
253 , detached_(detached)
254 , match_fun_(match_fun)
255 , clean_fun_(clean_fun)
256 , copy_data_fun_(copy_data_fun)
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
263 return SimcallObserver::dot_label(times_considered) + "iSend";
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_; }
274 class CommIrecvSimcall : public SimcallObserver {
275 activity::MailboxImpl* mbox_;
276 unsigned char* dst_buff_;
277 size_t* dst_buff_size_;
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
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)
290 , dst_buff_(dst_buff)
291 , dst_buff_size_(dst_buff_size)
294 , match_fun_(match_fun)
295 , copy_data_fun_(copy_data_fun)
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
302 return SimcallObserver::dot_label(times_considered) + "iRecv";
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_; }
312 } // namespace kernel
313 } // namespace simgrid