Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Split TransitionAny and TransitionRandom to their own files
[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()
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()
87 {
88   return not blocking_ || get_mutex()->get_owner() == nullptr || get_mutex()->get_owner() == get_issuer();
89 }
90
91 bool ConditionWaitSimcall::is_enabled()
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()
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 }
115
116 int ActivityTestanySimcall::get_max_consider()
117 {
118   indexes_.clear();
119   // list all the activities that are ready
120   for (unsigned i = 0; i < activities_.size(); i++)
121     if (activities_[i]->test(get_issuer()))
122       indexes_.push_back(i);
123   return indexes_.size() + 1;
124 }
125
126 void ActivityTestanySimcall::prepare(int times_considered)
127 {
128   if (times_considered < static_cast<int>(indexes_.size()))
129     next_value_ = indexes_.at(times_considered);
130   else
131     next_value_ = -1;
132 }
133 static void serialize_activity_test(const activity::ActivityImpl* act, std::stringstream& stream)
134 {
135   if (auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
136     stream << "  " << (short)mc::Transition::Type::COMM_TEST;
137     stream << ' ' << (uintptr_t)comm;
138     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
139     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
140     stream << ' ' << comm->get_mailbox_id();
141     stream << ' ' << (uintptr_t)comm->src_buff_ << ' ' << (uintptr_t)comm->dst_buff_ << ' ' << comm->src_buff_size_;
142   } else {
143     stream << (short)mc::Transition::Type::UNKNOWN;
144   }
145 }
146 void ActivityTestanySimcall::serialize(std::stringstream& stream) const
147 {
148   stream << (short)mc::Transition::Type::TESTANY << ' ' << activities_.size() << ' ';
149   for (auto const& act : activities_) {
150     serialize_activity_test(act, stream);
151     stream << ' ';
152   }
153 }
154 void ActivityTestSimcall::serialize(std::stringstream& stream) const
155 {
156   serialize_activity_test(activity_, stream);
157 }
158 static void serialize_activity_wait(const activity::ActivityImpl* act, bool timeout, std::stringstream& stream)
159 {
160   if (auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
161     stream << (short)mc::Transition::Type::COMM_WAIT << ' ';
162     stream << timeout << ' ' << (uintptr_t)comm;
163
164     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
165     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
166     stream << ' ' << comm->get_mailbox_id();
167     stream << ' ' << (uintptr_t)comm->src_buff_ << ' ' << (uintptr_t)comm->dst_buff_ << ' ' << comm->src_buff_size_;
168   } else {
169     stream << (short)mc::Transition::Type::UNKNOWN;
170   }
171 }
172
173 void ActivityWaitSimcall::serialize(std::stringstream& stream) const
174 {
175   serialize_activity_wait(activity_, timeout_ > 0, stream);
176 }
177 void ActivityWaitanySimcall::serialize(std::stringstream& stream) const
178 {
179   stream << (short)mc::Transition::Type::WAITANY << ' ' << activities_.size() << ' ';
180   for (auto const& act : activities_) {
181     serialize_activity_wait(act, timeout_ > 0, stream);
182     stream << ' ';
183   }
184 }
185 ActivityWaitanySimcall::ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities,
186                                                double timeout)
187     : ResultingSimcall(actor, -1), activities_(activities), timeout_(timeout)
188 {
189 }
190
191 bool ActivityWaitSimcall::is_enabled()
192 {
193   // FIXME: if _sg_mc_timeout == 1 and if we have either a sender or receiver timeout, the transition is enabled
194   // because even if the communication is not ready, it can timeout and won't block.
195
196   return activity_->test(get_issuer());
197 }
198
199 bool ActivityWaitanySimcall::is_enabled()
200 {
201   // list all the activities that are ready
202   indexes_.clear();
203   for (unsigned i = 0; i < activities_.size(); i++)
204     if (activities_[i]->test(get_issuer()))
205       indexes_.push_back(i);
206
207   //  if (_sg_mc_timeout && timeout_)  FIXME: deal with the potential timeout of the WaitAny
208
209   // FIXME: even if the WaitAny has no timeout, some of the activities may still have one.
210   // we should iterate over the vector searching for them
211   return not indexes_.empty();
212 }
213
214 int ActivityWaitanySimcall::get_max_consider()
215 {
216   // list all the activities that are ready
217   indexes_.clear();
218   for (unsigned i = 0; i < activities_.size(); i++)
219     if (activities_[i]->test(get_issuer()))
220       indexes_.push_back(i);
221
222   int res = indexes_.size();
223   //  if (_sg_mc_timeout && timeout_)
224   //    res++;
225
226   return res;
227 }
228
229 void ActivityWaitanySimcall::prepare(int times_considered)
230 {
231   if (times_considered < static_cast<int>(indexes_.size()))
232     next_value_ = indexes_.at(times_considered);
233   else
234     next_value_ = -1;
235 }
236
237 void CommIsendSimcall::serialize(std::stringstream& stream) const
238 {
239   stream << (short)mc::Transition::Type::COMM_SEND << ' ';
240   stream << (uintptr_t)comm_ << ' ' << mbox_->get_id() << ' ' << (uintptr_t)src_buff_ << ' ' << src_buff_size_ << ' '
241          << tag_;
242   XBT_DEBUG("SendObserver comm:%p mbox:%u buff:%p size:%zu tag:%d", comm_, mbox_->get_id(), src_buff_, src_buff_size_,
243             tag_);
244 }
245
246 void CommIrecvSimcall::serialize(std::stringstream& stream) const
247 {
248   stream << (short)mc::Transition::Type::COMM_RECV << ' ';
249   stream << (uintptr_t)comm_ << ' ' << mbox_->get_id() << ' ' << (uintptr_t)dst_buff_ << ' ' << tag_;
250   XBT_DEBUG("RecvObserver comm:%p mbox:%u buff:%p tag:%d", comm_, mbox_->get_id(), dst_buff_, tag_);
251 }
252
253 } // namespace actor
254 } // namespace kernel
255 } // namespace simgrid