Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move dot_label() from Observer to Transition (+ some reorgs)
[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 bool SimcallObserver::depends(SimcallObserver* other)
23 {
24   THROW_UNIMPLEMENTED;
25 }
26 /* Random is only dependent when issued by the same actor (ie, always independent) */
27 bool RandomSimcall::depends(SimcallObserver* other)
28 {
29   return get_issuer() == other->get_issuer();
30 }
31 void RandomSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
32 {
33   type = mc::Transition::Type::RANDOM;
34   stream << min_ << ' ' << max_;
35 }
36
37 bool MutexSimcall::depends(SimcallObserver* other)
38 {
39   if (dynamic_cast<RandomSimcall*>(other) != nullptr)
40     return other->depends(this); /* Other is random, that is very permissive. Use that relation instead. */
41
42 #if 0 /* This code is currently broken and shouldn't be used. We must implement asynchronous locks before */
43   MutexSimcall* that = dynamic_cast<MutexSimcall*>(other);
44   if (that == nullptr)
45     return true; // Depends on anything we don't know
46
47   /* Theorem 4.4.7: Any pair of synchronization actions of distinct actors concerning distinct mutexes are independent */
48   if (this->get_issuer() != that->get_issuer() && this->get_mutex() != that->get_mutex())
49     return false;
50
51   /* Theorem 4.4.8 An AsyncMutexLock is independent with a MutexUnlock of another actor */
52   if (((dynamic_cast<MutexLockSimcall*>(this) != nullptr && dynamic_cast<MutexUnlockSimcall*>(that)) ||
53        (dynamic_cast<MutexLockSimcall*>(that) != nullptr && dynamic_cast<MutexUnlockSimcall*>(this))) &&
54       get_issuer() != other->get_issuer())
55     return false;
56 #endif
57   return true; // Depend on things we don't know for sure that they are independent
58 }
59
60 void RandomSimcall::prepare(int times_considered)
61 {
62   next_value_ = min_ + times_considered;
63   XBT_DEBUG("MC_RANDOM(%d, %d) will return %d after %d times", min_, max_, next_value_, times_considered);
64 }
65
66 int RandomSimcall::get_max_consider() const
67 {
68   return max_ - min_ + 1;
69 }
70
71 /*
72 std::string MutexLockSimcall::to_string(int times_considered) const
73 {
74   auto mutex      = get_mutex();
75   std::string res = SimcallObserver::to_string(times_considered) + (blocking_ ? "Mutex LOCK" : "Mutex TRYLOCK");
76   res += "(locked = " + std::to_string(mutex->is_locked());
77   res += ", owner = " + std::to_string(mutex->get_owner() ? mutex->get_owner()->get_pid() : -1);
78   res += ", sleeping = n/a)";
79   return res;
80 }*/
81
82 bool MutexLockSimcall::is_enabled() const
83 {
84   return not blocking_ || get_mutex()->get_owner() == nullptr || get_mutex()->get_owner() == get_issuer();
85 }
86
87 bool ConditionWaitSimcall::is_enabled() const
88 {
89   static bool warned = false;
90   if (not warned) {
91     XBT_INFO("Using condition variables in model-checked code is still experimental. Use at your own risk");
92     warned = true;
93   }
94   return true;
95 }
96
97 bool SemAcquireSimcall::is_enabled() const
98 {
99   static bool warned = false;
100   if (not warned) {
101     XBT_INFO("Using semaphore in model-checked code is still experimental. Use at your own risk");
102     warned = true;
103   }
104   return true;
105 }
106
107 int ActivityTestanySimcall::get_max_consider() const
108 {
109   // Only Comms are of interest to MC for now. When all types of activities can be consider, this function can simply
110   // return the size of activities_.
111   int count = 0;
112   for (const auto& act : activities_)
113     if (dynamic_cast<activity::CommImpl*>(act) != nullptr)
114       count++;
115   return count;
116 }
117
118 void ActivityTestanySimcall::prepare(int times_considered)
119 {
120   next_value_ = times_considered;
121 }
122
123 /*
124 std::string ActivityTestanySimcall::to_string(int times_considered) const
125 {
126   std::string res = SimcallObserver::to_string(times_considered);
127   if (times_considered == -1) {
128     res += "TestAny FALSE(-)";
129   } else {
130     res += "TestAny(" + xbt::string_printf("(%d of %zu)", times_considered + 1, activities_.size());
131   }
132
133   return res;
134 }*/
135
136 bool ActivityTestSimcall::depends(SimcallObserver* other)
137 {
138   if (get_issuer() == other->get_issuer())
139     return false;
140
141   if (dynamic_cast<ActivityTestSimcall*>(other))
142     return true;
143
144   const auto* comm1 = dynamic_cast<activity::CommImpl*>(activity_);
145   if (comm1 == nullptr)
146     return false;
147
148   if (dynamic_cast<ActivityWaitSimcall*>(other) != nullptr &&
149       (comm1->src_actor_.get() == nullptr || comm1->dst_actor_.get() == nullptr))
150     return false;
151
152   if (comm1->src_buff_ == nullptr || comm1->dst_buff_ == nullptr)
153     return false;
154
155   if (const auto* test = dynamic_cast<ActivityTestSimcall*>(other)) {
156     const auto* comm2 = dynamic_cast<activity::CommImpl*>(test->get_activity());
157     if (comm2 == nullptr)
158       return false;
159     else if (comm2->src_buff_ == nullptr || comm2->dst_buff_ == nullptr)
160       return false;
161   }
162
163   if (auto* wait = dynamic_cast<ActivityWaitSimcall*>(other)) {
164     auto* comm2 = dynamic_cast<activity::CommImpl*>(wait->get_activity());
165     if (comm2 == nullptr)
166       return false;
167     if (comm1->src_buff_ == comm2->src_buff_ && comm1->dst_buff_ == comm2->dst_buff_)
168       return false;
169     if (comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr && comm2->src_buff_ != nullptr &&
170         comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ && comm1->dst_buff_ != comm2->dst_buff_ &&
171         comm2->dst_buff_ != comm1->src_buff_)
172       return false;
173   }
174
175   return true;
176 }
177 void ActivityWaitSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
178 {
179   if (auto* comm = dynamic_cast<activity::CommImpl*>(activity_)) {
180     type = mc::Transition::Type::COMM_WAIT;
181     stream << (timeout_ > 0) << ' ' << comm;
182     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
183     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
184     stream << ' ' << comm->get_mailbox_id();
185     stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_;
186   } else {
187     type = mc::Transition::Type::UNKNOWN;
188   }
189 }
190
191 /*
192 std::string ActivityTestSimcall::to_string(int times_considered) const
193 {
194   std::string res = SimcallObserver::to_string(times_considered) + "Test ";
195   if (const auto* comm = dynamic_cast<activity::CommImpl*>(activity_)) {
196     if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
197       res += "FALSE(comm=";
198       res += XBT_LOG_ISENABLED(mc_observer, xbt_log_priority_verbose) ? xbt::string_printf("%p)", comm)
199                                                                       : "(verbose only))";
200     } else {
201       res += "TRUE(comm=";
202
203       auto src = comm->src_actor_;
204       auto dst = comm->dst_actor_;
205       res +=
206           XBT_LOG_ISENABLED(mc_observer, xbt_log_priority_verbose) ? xbt::string_printf("%p", comm) : "(verbose only) ";
207       res += xbt::string_printf("[(%ld)%s (%s) ", src->get_pid(), src->get_host()->get_cname(), src->get_cname()) +
208              "-> " +
209              xbt::string_printf("(%ld)%s (%s)])", dst->get_pid(), dst->get_host()->get_cname(), dst->get_cname());
210     }
211   } else
212     xbt_die("Only Comms are supported here for now");
213   return res;
214 }*/
215
216 bool ActivityWaitSimcall::is_enabled() const
217 {
218   /* FIXME: check also that src and dst processes are not suspended */
219   const auto* comm = dynamic_cast<activity::CommImpl*>(activity_);
220   if (comm == nullptr)
221     xbt_die("Only Comms are supported here for now");
222
223   if (comm->src_timeout_ || comm->dst_timeout_) {
224     /* If it has a timeout it will be always be enabled (regardless of who declared the timeout),
225      * because even if the communication is not ready, it can timeout and won't block. */
226     if (_sg_mc_timeout == 1)
227       return true;
228   }
229   /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
230   else if (comm->detached() && comm->src_actor_ == nullptr && comm->get_state() == activity::State::READY)
231     return (comm->dst_actor_ != nullptr);
232   return (comm->src_actor_ && comm->dst_actor_);
233 }
234
235 bool ActivityWaitanySimcall::is_enabled() const
236 {
237   // FIXME: deal with other kind of activities (Exec and I/Os)
238   // FIXME: Can be factored with ActivityWaitSimcall::is_enabled()
239   const auto* comm = dynamic_cast<activity::CommImpl*>(activities_[next_value_]);
240   if (comm == nullptr)
241     xbt_die("Only Comms are supported here for now");
242   if (comm->src_timeout_ || comm->dst_timeout_) {
243     /* If it has a timeout it will be always be enabled (regardless of who declared the timeout),
244      * because even if the communication is not ready, it can timeout and won't block. */
245     if (_sg_mc_timeout == 1)
246       return true;
247   }
248   /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
249   else if (comm->detached() && comm->src_actor_ == nullptr && comm->get_state() == activity::State::READY)
250     return (comm->dst_actor_ != nullptr);
251   return (comm->src_actor_ && comm->dst_actor_);
252 }
253
254 int ActivityWaitanySimcall::get_max_consider() const
255 {
256   return static_cast<int>(activities_.size());
257 }
258
259 void ActivityWaitanySimcall::prepare(int times_considered)
260 {
261   next_value_ = times_considered;
262 }
263
264 void CommIsendSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
265 {
266   type = mc::Transition::Type::ISEND;
267   stream << mbox_->get_id() << ' ' << (void*)src_buff_ << ' ' << src_buff_size_;
268   XBT_DEBUG("SendObserver mbox:%u buff:%p size:%zu", mbox_->get_id(), src_buff_, src_buff_size_);
269 }
270
271 void CommIrecvSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
272 {
273   type = mc::Transition::Type::IRECV;
274   stream << mbox_->get_id() << dst_buff_;
275 }
276
277 } // namespace actor
278 } // namespace kernel
279 } // namespace simgrid