Logo AND Algorithmique Numérique Distribuée

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