1 /* Copyright (c) 2019-2021. 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"
17 class SimcallObserver {
18 ActorImpl* const issuer_;
21 explicit SimcallObserver(ActorImpl* issuer) : issuer_(issuer) {}
22 ActorImpl* get_issuer() const { return issuer_; }
23 /** Whether this transition can currently be taken without blocking.
25 * For example, a mutex_lock is not enabled when the mutex is not free.
26 * A comm_receive is not enabled before the corresponding send has been issued.
28 virtual bool is_enabled() const { return true; }
30 /** Returns the amount of time that this transition can be used.
32 * If it's 1 (as with send/wait), there is no need to fork the state space exploration on this point.
33 * If it's more than one (as with mc_random or waitany), we need to consider this transition several times to start
36 virtual int get_max_consider() const { return 1; }
38 /** Prepares the simcall to be used.
40 * For most simcalls, this does nothing. Once enabled, there is nothing to do to prepare a send().
42 * It is useful only for the simcalls that can be used several times, such as waitany() or random().
43 * For them, prepare() selects the right outcome for the time being considered.
45 * The first time a simcall is considered, times_considered is 0, not 1.
47 virtual void prepare(int times_considered)
48 { /* Nothing to do by default */
51 /** Some simcalls may only be observable under some circumstances.
52 * Most simcalls are not visible from the MC because they don't have an observer at all. */
53 virtual bool is_visible() const { return true; }
54 virtual std::string to_string(int times_considered) const = 0;
55 virtual std::string dot_label() const = 0;
58 class RandomSimcall : public SimcallObserver {
64 RandomSimcall(smx_actor_t actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max) {}
65 int get_max_consider() const override;
66 void prepare(int times_considered) override;
67 std::string to_string(int times_considered) const override;
68 std::string dot_label() const override;
69 int get_value() const { return next_value_; }
72 class MutexUnlockSimcall : public SimcallObserver {
73 using SimcallObserver::SimcallObserver;
76 std::string to_string(int times_considered) const override;
77 std::string dot_label() const override;
80 class MutexLockSimcall : public SimcallObserver {
81 activity::MutexImpl* const mutex_;
85 MutexLockSimcall(smx_actor_t actor, activity::MutexImpl* mutex, bool blocking = true)
86 : SimcallObserver(actor), mutex_(mutex), blocking_(blocking)
89 bool is_enabled() const override;
90 std::string to_string(int times_considered) const override;
91 std::string dot_label() const override;
92 activity::MutexImpl* get_mutex() const { return mutex_; }
95 class ConditionWaitSimcall : public SimcallObserver {
96 friend activity::ConditionVariableImpl;
98 activity::ConditionVariableImpl* const cond_;
99 activity::MutexImpl* const mutex_;
100 const double timeout_;
101 bool result_ = false; // default result for simcall, will be set to 'true' on timeout
103 void set_result(bool res) { result_ = res; }
106 ConditionWaitSimcall(smx_actor_t actor, activity::ConditionVariableImpl* cond, activity::MutexImpl* mutex,
107 double timeout = -1.0)
108 : SimcallObserver(actor), cond_(cond), mutex_(mutex), timeout_(timeout)
111 bool is_enabled() const override;
112 bool is_visible() const override { return false; }
113 std::string to_string(int times_considered) const override;
114 std::string dot_label() const override;
115 activity::ConditionVariableImpl* get_cond() const { return cond_; }
116 activity::MutexImpl* get_mutex() const { return mutex_; }
117 double get_timeout() const { return timeout_; }
119 bool get_result() const { return result_; }
122 class SemAcquireSimcall : public SimcallObserver {
123 friend activity::SemaphoreImpl;
125 activity::SemaphoreImpl* const sem_;
126 const double timeout_;
127 bool result_ = false; // default result for simcall, will be set to 'true' on timeout
129 void set_result(bool res) { result_ = res; }
132 SemAcquireSimcall(smx_actor_t actor, activity::SemaphoreImpl* sem, double timeout = -1.0)
133 : SimcallObserver(actor), sem_(sem), timeout_(timeout)
136 bool is_enabled() const override;
137 bool is_visible() const override { return false; }
138 std::string to_string(int times_considered) const override;
139 std::string dot_label() const override;
140 activity::SemaphoreImpl* get_sem() const { return sem_; }
141 double get_timeout() const { return timeout_; }
143 bool get_result() const { return result_; }
146 class ExecutionWaitanySimcall : public SimcallObserver {
147 friend activity::ExecImpl;
149 const std::vector<activity::ExecImpl*>& execs_;
150 const double timeout_;
151 int result_ = -1; // default result for simcall
153 void set_result(int res) { result_ = res; }
156 ExecutionWaitanySimcall(smx_actor_t actor, const std::vector<activity::ExecImpl*>& execs, double timeout)
157 : SimcallObserver(actor), execs_(execs), timeout_(timeout)
160 bool is_visible() const override { return false; }
161 std::string to_string(int times_considered) const override;
162 std::string dot_label() const override;
163 const std::vector<activity::ExecImpl*>& get_execs() const { return execs_; }
164 double get_timeout() const { return timeout_; }
166 int get_result() const { return result_; }
169 } // namespace kernel
170 } // namespace simgrid