Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Misc. cosmetic changes.
[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   /** We need to save the observer of simcalls as they get executed to later compute their dependencies in classical
52    * DPOR */
53   virtual SimcallObserver* clone() = 0;
54
55   /** Computes the dependency relation */
56   virtual bool depends(SimcallObserver* other);
57
58   /** Some simcalls may only be observable under some conditions.
59    * Most simcalls are not visible from the MC because they don't have an observer at all. */
60   virtual bool is_visible() const { return true; }
61   virtual std::string to_string(int times_considered) const = 0;
62   virtual std::string dot_label() const                     = 0;
63 };
64
65 template <class T> class ResultingSimcall : public SimcallObserver {
66   T result_;
67
68 public:
69   ResultingSimcall(smx_actor_t actor, T default_result) : SimcallObserver(actor), result_(default_result) {}
70   void set_result(T res) { result_ = res; }
71   T get_result() const { return result_; }
72 };
73
74 class RandomSimcall : public SimcallObserver {
75   const int min_;
76   const int max_;
77   int next_value_ = 0;
78
79 public:
80   RandomSimcall(smx_actor_t actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max) {}
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   int get_max_consider() const override;
88   void prepare(int times_considered) override;
89   std::string to_string(int times_considered) const override;
90   std::string dot_label() const override;
91   int get_value() const { return next_value_; }
92   bool depends(SimcallObserver* other) override;
93 };
94
95 class MutexSimcall : public SimcallObserver {
96   activity::MutexImpl* const mutex_;
97
98 public:
99   MutexSimcall(smx_actor_t actor, activity::MutexImpl* mutex) : SimcallObserver(actor), mutex_(mutex) {}
100   activity::MutexImpl* get_mutex() const { return mutex_; }
101   bool depends(SimcallObserver* other) override;
102 };
103
104 class MutexUnlockSimcall : public MutexSimcall {
105   using MutexSimcall::MutexSimcall;
106
107 public:
108   SimcallObserver* clone() override { return new MutexUnlockSimcall(get_issuer(), get_mutex()); }
109   std::string to_string(int times_considered) const override;
110   std::string dot_label() const override;
111 };
112
113 class MutexLockSimcall : public MutexSimcall {
114   const bool blocking_;
115
116 public:
117   MutexLockSimcall(smx_actor_t actor, activity::MutexImpl* mutex, bool blocking = true)
118       : MutexSimcall(actor, mutex), blocking_(blocking)
119   {
120   }
121   SimcallObserver* clone() override { return new MutexLockSimcall(get_issuer(), get_mutex(), blocking_); }
122   bool is_enabled() const override;
123   std::string to_string(int times_considered) const override;
124   std::string dot_label() const override;
125 };
126
127 class ConditionWaitSimcall : public ResultingSimcall<bool> {
128   activity::ConditionVariableImpl* const cond_;
129   activity::MutexImpl* const mutex_;
130   const double timeout_;
131
132 public:
133   ConditionWaitSimcall(smx_actor_t actor, activity::ConditionVariableImpl* cond, activity::MutexImpl* mutex,
134                        double timeout = -1.0)
135       : ResultingSimcall(actor, false), cond_(cond), mutex_(mutex), timeout_(timeout)
136   {
137   }
138   SimcallObserver* clone() override { return new ConditionWaitSimcall(get_issuer(), cond_, mutex_, timeout_); }
139   bool is_enabled() const override;
140   bool is_visible() const override { return false; }
141   std::string to_string(int times_considered) const override;
142   std::string dot_label() const override;
143   activity::ConditionVariableImpl* get_cond() const { return cond_; }
144   activity::MutexImpl* get_mutex() const { return mutex_; }
145   double get_timeout() const { return timeout_; }
146 };
147
148 class SemAcquireSimcall : public ResultingSimcall<bool> {
149   activity::SemaphoreImpl* const sem_;
150   const double timeout_;
151
152 public:
153   SemAcquireSimcall(smx_actor_t actor, activity::SemaphoreImpl* sem, double timeout = -1.0)
154       : ResultingSimcall(actor, false), sem_(sem), timeout_(timeout)
155   {
156   }
157   SimcallObserver* clone() override { return new SemAcquireSimcall(get_issuer(), sem_, timeout_); }
158   bool is_enabled() const override;
159   bool is_visible() const override { return false; }
160   std::string to_string(int times_considered) const override;
161   std::string dot_label() const override;
162   activity::SemaphoreImpl* get_sem() const { return sem_; }
163   double get_timeout() const { return timeout_; }
164 };
165
166 class ActivityWaitSimcall : public ResultingSimcall<bool> {
167   activity::ActivityImpl* const activity_;
168   const double timeout_;
169
170 public:
171   ActivityWaitSimcall(smx_actor_t actor, activity::ActivityImpl* activity, double timeout)
172       : ResultingSimcall(actor, false), activity_(activity), timeout_(timeout)
173   {
174   }
175   SimcallObserver* clone() override { return new ActivityWaitSimcall(get_issuer(), activity_, timeout_); }
176   bool is_visible() const override { return false; }
177   std::string to_string(int times_considered) const override { return SimcallObserver::to_string(times_considered); }
178   std::string dot_label() const override { return SimcallObserver::dot_label(); }
179   activity::ActivityImpl* get_activity() const { return activity_; }
180   double get_timeout() const { return timeout_; }
181 };
182
183 class ExecutionWaitanySimcall : public ResultingSimcall<ssize_t> {
184   const std::vector<activity::ExecImpl*>& execs_;
185   const double timeout_;
186
187 public:
188   ExecutionWaitanySimcall(smx_actor_t actor, const std::vector<activity::ExecImpl*>& execs, double timeout)
189       : ResultingSimcall(actor, -1), execs_(execs), timeout_(timeout)
190   {
191   }
192   SimcallObserver* clone() override { return new ExecutionWaitanySimcall(get_issuer(), execs_, timeout_); }
193   bool is_visible() const override { return false; }
194   std::string to_string(int times_considered) const override;
195   std::string dot_label() const override;
196   const std::vector<activity::ExecImpl*>& get_execs() const { return execs_; }
197   double get_timeout() const { return timeout_; }
198 };
199
200 class IoWaitanySimcall : public ResultingSimcall<ssize_t> {
201   const std::vector<activity::IoImpl*>& ios_;
202   const double timeout_;
203
204 public:
205   IoWaitanySimcall(smx_actor_t actor, const std::vector<activity::IoImpl*>& ios, double timeout)
206       : ResultingSimcall(actor, -1), ios_(ios), timeout_(timeout)
207   {
208   }
209   SimcallObserver* clone() override { return new IoWaitanySimcall(get_issuer(), ios_, timeout_); }
210   bool is_visible() const override { return false; }
211   std::string to_string(int times_considered) const override;
212   std::string dot_label() const override;
213   const std::vector<activity::IoImpl*>& get_ios() const { return ios_; }
214   double get_timeout() const { return timeout_; }
215 };
216 } // namespace actor
217 } // namespace kernel
218 } // namespace simgrid
219
220 #endif