Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement TestAnyTransition
[simgrid.git] / src / kernel / actor / SimcallObserver.cpp
1 /* Copyright (c) 2019-2022. 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 #include "src/kernel/actor/SimcallObserver.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "src/kernel/activity/CommImpl.hpp"
9 #include "src/kernel/activity/MailboxImpl.hpp"
10 #include "src/kernel/activity/MutexImpl.hpp"
11 #include "src/kernel/actor/ActorImpl.hpp"
12 #include "src/mc/mc_config.hpp"
13
14 #include <sstream>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_observer, mc, "Logging specific to MC simcall observation");
17
18 namespace simgrid {
19 namespace kernel {
20 namespace actor {
21
22 void SimcallObserver::serialize(std::stringstream& stream) const
23 {
24   stream << (short)mc::Transition::Type::UNKNOWN;
25 }
26 bool SimcallObserver::depends(SimcallObserver* other)
27 {
28   THROW_UNIMPLEMENTED;
29 }
30 /* Random is only dependent when issued by the same actor (ie, always independent) */
31 bool RandomSimcall::depends(SimcallObserver* other)
32 {
33   return get_issuer() == other->get_issuer();
34 }
35 void RandomSimcall::serialize(std::stringstream& stream) const
36 {
37   stream << (short)mc::Transition::Type::RANDOM << ' ';
38   stream << min_ << ' ' << max_;
39 }
40
41 bool MutexSimcall::depends(SimcallObserver* other)
42 {
43   if (dynamic_cast<RandomSimcall*>(other) != nullptr)
44     return other->depends(this); /* Other is random, that is very permissive. Use that relation instead. */
45
46 #if 0 /* This code is currently broken and shouldn't be used. We must implement asynchronous locks before */
47   MutexSimcall* that = dynamic_cast<MutexSimcall*>(other);
48   if (that == nullptr)
49     return true; // Depends on anything we don't know
50
51   /* Theorem 4.4.7: Any pair of synchronization actions of distinct actors concerning distinct mutexes are independent */
52   if (this->get_issuer() != that->get_issuer() && this->get_mutex() != that->get_mutex())
53     return false;
54
55   /* Theorem 4.4.8 An AsyncMutexLock is independent with a MutexUnlock of another actor */
56   if (((dynamic_cast<MutexLockSimcall*>(this) != nullptr && dynamic_cast<MutexUnlockSimcall*>(that)) ||
57        (dynamic_cast<MutexLockSimcall*>(that) != nullptr && dynamic_cast<MutexUnlockSimcall*>(this))) &&
58       get_issuer() != other->get_issuer())
59     return false;
60 #endif
61   return true; // Depend on things we don't know for sure that they are independent
62 }
63
64 void RandomSimcall::prepare(int times_considered)
65 {
66   next_value_ = min_ + times_considered;
67   XBT_DEBUG("MC_RANDOM(%d, %d) will return %d after %d times", min_, max_, next_value_, times_considered);
68 }
69
70 int RandomSimcall::get_max_consider() const
71 {
72   return max_ - min_ + 1;
73 }
74
75 /*
76 std::string MutexLockSimcall::to_string(int times_considered) const
77 {
78   auto mutex      = get_mutex();
79   std::string res = SimcallObserver::to_string(times_considered) + (blocking_ ? "Mutex LOCK" : "Mutex TRYLOCK");
80   res += "(locked = " + std::to_string(mutex->is_locked());
81   res += ", owner = " + std::to_string(mutex->get_owner() ? mutex->get_owner()->get_pid() : -1);
82   res += ", sleeping = n/a)";
83   return res;
84 }*/
85
86 bool MutexLockSimcall::is_enabled() const
87 {
88   return not blocking_ || get_mutex()->get_owner() == nullptr || get_mutex()->get_owner() == get_issuer();
89 }
90
91 bool ConditionWaitSimcall::is_enabled() const
92 {
93   static bool warned = false;
94   if (not warned) {
95     XBT_INFO("Using condition variables in model-checked code is still experimental. Use at your own risk");
96     warned = true;
97   }
98   return true;
99 }
100
101 bool SemAcquireSimcall::is_enabled() const
102 {
103   static bool warned = false;
104   if (not warned) {
105     XBT_INFO("Using semaphore in model-checked code is still experimental. Use at your own risk");
106     warned = true;
107   }
108   return true;
109 }
110
111 ActivityTestanySimcall::ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities)
112     : ResultingSimcall(actor, -1), activities_(activities)
113 {
114   // list all the activities that are ready
115   for (unsigned i = 0; i < activities_.size(); i++)
116     if (activities_[i]->test(get_issuer()))
117       indexes_.push_back(i);
118 }
119
120 int ActivityTestanySimcall::get_max_consider() const
121 {
122   return indexes_.size() + 1;
123 }
124
125 void ActivityTestanySimcall::prepare(int times_considered)
126 {
127   if (times_considered < static_cast<int>(indexes_.size()))
128     next_value_ = indexes_.at(times_considered);
129   else
130     next_value_ = -1;
131 }
132 static void serialize_activity(const activity::ActivityImpl* act, std::stringstream& stream)
133 {
134   if (auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
135     stream << (short)mc::Transition::Type::COMM_TEST << ' ';
136     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
137     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
138     stream << ' ' << comm->get_mailbox_id();
139     stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_;
140   } else {
141     stream << (short)mc::Transition::Type::UNKNOWN;
142   }
143 }
144 void ActivityTestanySimcall::serialize(std::stringstream& stream) const
145 {
146   stream << (short)mc::Transition::Type::TESTANY << ' ' << activities_.size() << ' ';
147   for (auto const& act : activities_)
148     serialize_activity(act, stream);
149 }
150 void ActivityWaitSimcall::serialize(std::stringstream& stream) const
151 {
152   if (auto* comm = dynamic_cast<activity::CommImpl*>(activity_)) {
153     stream << (short)mc::Transition::Type::COMM_WAIT << ' ';
154     stream << (timeout_ > 0) << ' ' << comm;
155     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
156     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
157     stream << ' ' << comm->get_mailbox_id();
158     stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_;
159   } else {
160     stream << (short)mc::Transition::Type::UNKNOWN;
161   }
162 }
163 void ActivityTestSimcall::serialize(std::stringstream& stream) const
164 {
165   serialize_activity(activity_, stream);
166 }
167
168 bool ActivityWaitSimcall::is_enabled() const
169 {
170   /* FIXME: check also that src and dst processes are not suspended */
171   const auto* comm = dynamic_cast<activity::CommImpl*>(activity_);
172   if (comm == nullptr)
173     xbt_die("Only Comms are supported here for now");
174
175   if (comm->src_timeout_ || comm->dst_timeout_) {
176     /* If it has a timeout it will be always be enabled (regardless of who declared the timeout),
177      * because even if the communication is not ready, it can timeout and won't block. */
178     if (_sg_mc_timeout == 1)
179       return true;
180   }
181   /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
182   else if (comm->detached() && comm->src_actor_ == nullptr && comm->get_state() == activity::State::READY)
183     return (comm->dst_actor_ != nullptr);
184   return (comm->src_actor_ && comm->dst_actor_);
185 }
186
187 bool ActivityWaitanySimcall::is_enabled() const
188 {
189   // FIXME: deal with other kind of activities (Exec and I/Os)
190   // FIXME: Can be factored with ActivityWaitSimcall::is_enabled()
191   const auto* comm = dynamic_cast<activity::CommImpl*>(activities_[next_value_]);
192   if (comm == nullptr)
193     xbt_die("Only Comms are supported here for now");
194   if (comm->src_timeout_ || comm->dst_timeout_) {
195     /* If it has a timeout it will be always be enabled (regardless of who declared the timeout),
196      * because even if the communication is not ready, it can timeout and won't block. */
197     if (_sg_mc_timeout == 1)
198       return true;
199   }
200   /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
201   else if (comm->detached() && comm->src_actor_ == nullptr && comm->get_state() == activity::State::READY)
202     return (comm->dst_actor_ != nullptr);
203   return (comm->src_actor_ && comm->dst_actor_);
204 }
205
206 int ActivityWaitanySimcall::get_max_consider() const
207 {
208   return static_cast<int>(activities_.size());
209 }
210
211 void ActivityWaitanySimcall::prepare(int times_considered)
212 {
213   next_value_ = times_considered;
214 }
215
216 void CommIsendSimcall::serialize(std::stringstream& stream) const
217 {
218   stream << (short)mc::Transition::Type::COMM_SEND << ' ';
219   stream << mbox_->get_id() << ' ' << (void*)src_buff_ << ' ' << src_buff_size_;
220   XBT_DEBUG("SendObserver mbox:%u buff:%p size:%zu", mbox_->get_id(), src_buff_, src_buff_size_);
221 }
222
223 void CommIrecvSimcall::serialize(std::stringstream& stream) const
224 {
225   stream << (short)mc::Transition::Type::COMM_RECV << ' ';
226   stream << mbox_->get_id() << ' ' << dst_buff_;
227 }
228
229 } // namespace actor
230 } // namespace kernel
231 } // namespace simgrid