Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: add a clone() method to all Observers
[simgrid.git] / src / kernel / actor / SimcallObserver.hpp
1 /* Copyright (c) 2019-2021. 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
11 #include <string>
12
13 namespace simgrid {
14 namespace kernel {
15 namespace actor {
16
17 class SimcallObserver {
18   ActorImpl* const issuer_;
19
20 public:
21   explicit SimcallObserver(ActorImpl* issuer) : issuer_(issuer) {}
22   ActorImpl* get_issuer() const { return issuer_; }
23   /** Whether this transition can currently be taken without blocking.
24    *
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.
27    */
28   virtual bool is_enabled() const { return true; }
29
30   /** Returns the amount of time that this transition can be used.
31    *
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
34    * differing branches
35    */
36   virtual int get_max_consider() const { return 1; }
37
38   /** Prepares the simcall to be used.
39    *
40    * For most simcalls, this does nothing. Once enabled, there is nothing to do to prepare a send().
41    *
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.
44    *
45    * The first time a simcall is considered, times_considered is 0, not 1.
46    */
47   virtual void prepare(int times_considered)
48   { /* Nothing to do by default */
49   }
50
51   virtual SimcallObserver* clone() = 0;
52
53   /** Some simcalls may only be observable under some circumstances.
54    * Most simcalls are not visible from the MC because they don't have an observer at all. */
55   virtual bool is_visible() const { return true; }
56   virtual std::string to_string(int times_considered) const = 0;
57   virtual std::string dot_label() const                     = 0;
58 };
59
60 template <class T> class ResultingSimcall : public SimcallObserver {
61   T result_;
62
63 public:
64   ResultingSimcall(smx_actor_t actor, T default_result) : SimcallObserver(actor), result_(default_result) {}
65   void set_result(T res) { result_ = res; }
66   T get_result() const { return result_; }
67 };
68
69 class RandomSimcall : public SimcallObserver {
70   const int min_;
71   const int max_;
72   int next_value_ = 0;
73
74 public:
75   RandomSimcall(smx_actor_t actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max) {}
76   int get_max_consider() const override;
77   void prepare(int times_considered) override;
78   std::string to_string(int times_considered) const override;
79   std::string dot_label() const override;
80   int get_value() const { return next_value_; }
81   SimcallObserver* clone() override
82   {
83     auto res         = new RandomSimcall(get_issuer(), min_, max_);
84     res->next_value_ = next_value_;
85     return res;
86   }
87 };
88
89 class MutexUnlockSimcall : public SimcallObserver {
90   using SimcallObserver::SimcallObserver;
91
92 public:
93   SimcallObserver* clone() override { return new MutexUnlockSimcall(get_issuer()); }
94   std::string to_string(int times_considered) const override;
95   std::string dot_label() const override;
96 };
97
98 class MutexLockSimcall : public SimcallObserver {
99   activity::MutexImpl* const mutex_;
100   const bool blocking_;
101
102 public:
103   MutexLockSimcall(smx_actor_t actor, activity::MutexImpl* mutex, bool blocking = true)
104       : SimcallObserver(actor), mutex_(mutex), blocking_(blocking)
105   {
106   }
107   SimcallObserver* clone() override { return new MutexLockSimcall(get_issuer(), mutex_, blocking_); }
108   bool is_enabled() const override;
109   std::string to_string(int times_considered) const override;
110   std::string dot_label() const override;
111   activity::MutexImpl* get_mutex() const { return mutex_; }
112 };
113
114 class ConditionWaitSimcall : public ResultingSimcall<bool> {
115   activity::ConditionVariableImpl* const cond_;
116   activity::MutexImpl* const mutex_;
117   const double timeout_;
118
119 public:
120   ConditionWaitSimcall(smx_actor_t actor, activity::ConditionVariableImpl* cond, activity::MutexImpl* mutex,
121                        double timeout = -1.0)
122       : ResultingSimcall(actor, false), cond_(cond), mutex_(mutex), timeout_(timeout)
123   {
124   }
125   SimcallObserver* clone() override { return new ConditionWaitSimcall(get_issuer(), cond_, mutex_, timeout_); }
126   bool is_enabled() const override;
127   bool is_visible() const override { return false; }
128   std::string to_string(int times_considered) const override;
129   std::string dot_label() const override;
130   activity::ConditionVariableImpl* get_cond() const { return cond_; }
131   activity::MutexImpl* get_mutex() const { return mutex_; }
132   double get_timeout() const { return timeout_; }
133 };
134
135 class SemAcquireSimcall : public ResultingSimcall<bool> {
136   activity::SemaphoreImpl* const sem_;
137   const double timeout_;
138
139 public:
140   SemAcquireSimcall(smx_actor_t actor, activity::SemaphoreImpl* sem, double timeout = -1.0)
141       : ResultingSimcall(actor, false), sem_(sem), timeout_(timeout)
142   {
143   }
144   SimcallObserver* clone() override { return new SemAcquireSimcall(get_issuer(), sem_, timeout_); }
145   bool is_enabled() const override;
146   bool is_visible() const override { return false; }
147   std::string to_string(int times_considered) const override;
148   std::string dot_label() const override;
149   activity::SemaphoreImpl* get_sem() const { return sem_; }
150   double get_timeout() const { return timeout_; }
151 };
152
153 class ActivityWaitSimcall : public ResultingSimcall<bool> {
154   activity::ActivityImpl* const activity_;
155   const double timeout_;
156
157 public:
158   ActivityWaitSimcall(smx_actor_t actor, activity::ActivityImpl* activity, double timeout)
159       : ResultingSimcall(actor, false), activity_(activity), timeout_(timeout)
160   {
161   }
162   SimcallObserver* clone() override { return new ActivityWaitSimcall(get_issuer(), activity_, timeout_); }
163   bool is_visible() const override { return false; }
164   std::string to_string(int times_considered) const override { return SimcallObserver::to_string(times_considered); }
165   std::string dot_label() const override { return SimcallObserver::dot_label(); }
166   activity::ActivityImpl* get_activity() const { return activity_; }
167   double get_timeout() const { return timeout_; }
168 };
169
170 class ExecutionWaitanySimcall : public ResultingSimcall<int> {
171   const std::vector<activity::ExecImpl*>& execs_;
172   const double timeout_;
173
174 public:
175   ExecutionWaitanySimcall(smx_actor_t actor, const std::vector<activity::ExecImpl*>& execs, double timeout)
176       : ResultingSimcall(actor, -1), execs_(execs), timeout_(timeout)
177   {
178   }
179   SimcallObserver* clone() override { return new ExecutionWaitanySimcall(get_issuer(), execs_, timeout_); }
180   bool is_visible() const override { return false; }
181   std::string to_string(int times_considered) const override;
182   std::string dot_label() const override;
183   const std::vector<activity::ExecImpl*>& get_execs() const { return execs_; }
184   double get_timeout() const { return timeout_; }
185 };
186
187 class IoWaitanySimcall : public ResultingSimcall<int> {
188   const std::vector<activity::IoImpl*>& ios_;
189   const double timeout_;
190
191 public:
192   IoWaitanySimcall(smx_actor_t actor, const std::vector<activity::IoImpl*>& ios, double timeout)
193       : ResultingSimcall(actor, -1), ios_(ios), timeout_(timeout)
194   {
195   }
196   SimcallObserver* clone() override { return new IoWaitanySimcall(get_issuer(), ios_, timeout_); }
197   bool is_visible() const override { return false; }
198   std::string to_string(int times_considered) const override;
199   std::string dot_label() const override;
200   const std::vector<activity::IoImpl*>& get_ios() const { return ios_; }
201   double get_timeout() const { return timeout_; }
202 };
203 } // namespace actor
204 } // namespace kernel
205 } // namespace simgrid
206
207 #endif