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"
18 class SimcallObserver {
19 ActorImpl* const issuer_;
22 explicit SimcallObserver(ActorImpl* issuer) : issuer_(issuer) {}
23 ActorImpl* get_issuer() const { return issuer_; }
24 /** Whether this transition can currently be taken without blocking.
26 * For example, a mutex_lock is not enabled when the mutex is not free.
27 * A comm_receive is not enabled before the corresponding send has been issued.
29 virtual bool is_enabled() const { return true; }
31 /** Returns the amount of time that this transition can be used.
33 * If it's 1 (as with send/wait), there is no need to fork the state space exploration on this point.
34 * If it's more than one (as with mc_random or waitany), we need to consider this transition several times to start
37 virtual int get_max_consider() const { return 1; }
39 /** Prepares the simcall to be used.
41 * For most simcalls, this does nothing. Once enabled, there is nothing to do to prepare a send().
43 * It is useful only for the simcalls that can be used several times, such as waitany() or random().
44 * For them, prepare() selects the right outcome for the time being considered.
46 * The first time a simcall is considered, times_considered is 0, not 1.
48 virtual void prepare(int times_considered)
49 { /* Nothing to do by default */
52 /** We need to save the observer of simcalls as they get executed to later compute their dependencies in classical
54 virtual SimcallObserver* clone() = 0;
56 /** Computes the dependency relation */
57 virtual bool depends(SimcallObserver* other);
59 /** Some simcalls may only be observable under some conditions.
60 * Most simcalls are not visible from the MC because they don't have an observer at all. */
61 virtual bool is_visible() const { return true; }
62 virtual std::string to_string(int times_considered) const = 0;
63 virtual std::string dot_label(int times_considered) const = 0;
66 template <class T> class ResultingSimcall : public SimcallObserver {
70 ResultingSimcall(ActorImpl* actor, T default_result) : SimcallObserver(actor), result_(default_result) {}
71 void set_result(T res) { result_ = res; }
72 T get_result() const { return result_; }
75 class RandomSimcall : public SimcallObserver {
81 RandomSimcall(ActorImpl* actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max)
83 xbt_assert(min < max);
85 SimcallObserver* clone() override
87 auto res = new RandomSimcall(get_issuer(), min_, max_);
88 res->next_value_ = next_value_;
91 int get_max_consider() const override;
92 void prepare(int times_considered) override;
93 std::string to_string(int times_considered) const override;
94 std::string dot_label(int times_considered) const override;
95 int get_value() const { return next_value_; }
96 bool depends(SimcallObserver* other) override;
99 class MutexSimcall : public SimcallObserver {
100 activity::MutexImpl* const mutex_;
103 MutexSimcall(ActorImpl* actor, activity::MutexImpl* mutex) : SimcallObserver(actor), mutex_(mutex) {}
104 activity::MutexImpl* get_mutex() const { return mutex_; }
105 bool depends(SimcallObserver* other) override;
108 class MutexUnlockSimcall : public MutexSimcall {
109 using MutexSimcall::MutexSimcall;
112 SimcallObserver* clone() override { return new MutexUnlockSimcall(get_issuer(), get_mutex()); }
113 std::string to_string(int times_considered) const override;
114 std::string dot_label(int times_considered) const override;
117 class MutexLockSimcall : public MutexSimcall {
118 const bool blocking_;
121 MutexLockSimcall(ActorImpl* actor, activity::MutexImpl* mutex, bool blocking = true)
122 : MutexSimcall(actor, mutex), blocking_(blocking)
125 SimcallObserver* clone() override { return new MutexLockSimcall(get_issuer(), get_mutex(), blocking_); }
126 bool is_enabled() const override;
127 std::string to_string(int times_considered) const override;
128 std::string dot_label(int times_considered) const override;
131 class ConditionWaitSimcall : public ResultingSimcall<bool> {
132 activity::ConditionVariableImpl* const cond_;
133 activity::MutexImpl* const mutex_;
134 const double timeout_;
137 ConditionWaitSimcall(ActorImpl* actor, activity::ConditionVariableImpl* cond, activity::MutexImpl* mutex,
138 double timeout = -1.0)
139 : ResultingSimcall(actor, false), cond_(cond), mutex_(mutex), timeout_(timeout)
142 SimcallObserver* clone() override { return new ConditionWaitSimcall(get_issuer(), cond_, mutex_, timeout_); }
143 bool is_enabled() const override;
144 bool is_visible() const override { return false; }
145 std::string to_string(int times_considered) const override;
146 std::string dot_label(int times_considered) const override;
147 activity::ConditionVariableImpl* get_cond() const { return cond_; }
148 activity::MutexImpl* get_mutex() const { return mutex_; }
149 double get_timeout() const { return timeout_; }
152 class SemAcquireSimcall : public ResultingSimcall<bool> {
153 activity::SemaphoreImpl* const sem_;
154 const double timeout_;
157 SemAcquireSimcall(ActorImpl* actor, activity::SemaphoreImpl* sem, double timeout = -1.0)
158 : ResultingSimcall(actor, false), sem_(sem), timeout_(timeout)
161 SimcallObserver* clone() override { return new SemAcquireSimcall(get_issuer(), sem_, timeout_); }
162 bool is_enabled() const override;
163 bool is_visible() const override { return false; }
164 std::string to_string(int times_considered) const override;
165 std::string dot_label(int times_considered) const override;
166 activity::SemaphoreImpl* get_sem() const { return sem_; }
167 double get_timeout() const { return timeout_; }
170 class ActivityTestSimcall : public ResultingSimcall<bool> {
171 activity::ActivityImpl* const activity_;
174 ActivityTestSimcall(ActorImpl* actor, activity::ActivityImpl* activity)
175 : ResultingSimcall(actor, true), activity_(activity)
178 SimcallObserver* clone() override { return new ActivityTestSimcall(get_issuer(), activity_); }
179 bool is_visible() const override { return true; }
180 std::string to_string(int times_considered) const override;
181 std::string dot_label(int times_considered) const override;
182 activity::ActivityImpl* get_activity() const { return activity_; }
185 class ActivityTestanySimcall : public ResultingSimcall<ssize_t> {
186 const std::vector<activity::ActivityImpl*>& activities_;
187 int next_value_ = -1;
190 ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities)
191 : ResultingSimcall(actor, -1), activities_(activities)
194 SimcallObserver* clone() override { return new ActivityTestanySimcall(get_issuer(), activities_); }
195 bool is_visible() const override { return true; }
196 int get_max_consider() const override;
197 void prepare(int times_considered) override;
198 std::string to_string(int times_considered) const override;
199 std::string dot_label(int times_considered) const override;
200 const std::vector<activity::ActivityImpl*>& get_activities() const { return activities_; }
201 int get_value() const { return next_value_; }
204 class ActivityWaitSimcall : public ResultingSimcall<bool> {
205 activity::ActivityImpl* activity_;
206 const double timeout_;
209 ActivityWaitSimcall(ActorImpl* actor, activity::ActivityImpl* activity, double timeout)
210 : ResultingSimcall(actor, false), activity_(activity), timeout_(timeout)
213 SimcallObserver* clone() override { return new ActivityWaitSimcall(get_issuer(), activity_, timeout_); }
214 bool is_visible() const override { return true; }
215 bool is_enabled() const override;
216 std::string to_string(int times_considered) const override;
217 std::string dot_label(int times_considered) const override;
218 activity::ActivityImpl* get_activity() const { return activity_; }
219 void set_activity(activity::ActivityImpl* activity) { activity_ = activity; }
220 double get_timeout() const { return timeout_; }
223 class ActivityWaitanySimcall : public ResultingSimcall<ssize_t> {
224 const std::vector<activity::ActivityImpl*>& activities_;
225 const double timeout_;
226 int next_value_ = -1;
229 ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities, double timeout)
230 : ResultingSimcall(actor, -1), activities_(activities), timeout_(timeout)
233 SimcallObserver* clone() override { return new ActivityWaitanySimcall(get_issuer(), activities_, timeout_); }
234 bool is_enabled() const override;
235 bool is_visible() const override { return true; }
236 void prepare(int times_considered) override;
237 int get_max_consider() const override;
238 std::string to_string(int times_considered) 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_; }
246 } // namespace kernel
247 } // namespace simgrid