Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Better fix for the security warning from sonar: hide the char* buffer
[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(Simcall& type, std::stringstream& stream)
32 {
33   type = Simcall::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 /*
61 std::string SimcallObserver::to_string(int) const
62 {
63   return simgrid::xbt::string_printf("[(%ld)%s (%s)] ", issuer_->get_pid(), issuer_->get_host()->get_cname(),
64                                      issuer_->get_cname());
65 }*/
66
67 std::string SimcallObserver::dot_label(int /*times_considered*/) const
68 {
69   if (issuer_->get_host())
70     return xbt::string_printf("[(%ld)%s] ", issuer_->get_pid(), issuer_->get_host()->get_cname());
71   return xbt::string_printf("[(%ld)] ", issuer_->get_pid());
72 }
73
74 std::string RandomSimcall::dot_label(int times_considered) const
75 {
76   return SimcallObserver::dot_label(times_considered) + "MC_RANDOM(" + std::to_string(next_value_) + ")";
77 }
78
79 void RandomSimcall::prepare(int times_considered)
80 {
81   next_value_ = min_ + times_considered;
82   XBT_DEBUG("MC_RANDOM(%d, %d) will return %d after %d times", min_, max_, next_value_, times_considered);
83 }
84
85 int RandomSimcall::get_max_consider() const
86 {
87   return max_ - min_ + 1;
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 /*
96 std::string MutexLockSimcall::to_string(int times_considered) const
97 {
98   auto mutex      = get_mutex();
99   std::string res = SimcallObserver::to_string(times_considered) + (blocking_ ? "Mutex LOCK" : "Mutex TRYLOCK");
100   res += "(locked = " + std::to_string(mutex->is_locked());
101   res += ", owner = " + std::to_string(mutex->get_owner() ? mutex->get_owner()->get_pid() : -1);
102   res += ", sleeping = n/a)";
103   return res;
104 }*/
105
106 std::string MutexLockSimcall::dot_label(int times_considered) const
107 {
108   return SimcallObserver::dot_label(times_considered) + (blocking_ ? "Mutex LOCK" : "Mutex TRYLOCK");
109 }
110
111 bool MutexLockSimcall::is_enabled() const
112 {
113   return not blocking_ || get_mutex()->get_owner() == nullptr || get_mutex()->get_owner() == get_issuer();
114 }
115
116 std::string ConditionWaitSimcall::dot_label(int times_considered) const
117 {
118   return SimcallObserver::dot_label(times_considered) + "Condition WAIT";
119 }
120
121 bool ConditionWaitSimcall::is_enabled() const
122 {
123   static bool warned = false;
124   if (not warned) {
125     XBT_INFO("Using condition variables in model-checked code is still experimental. Use at your own risk");
126     warned = true;
127   }
128   return true;
129 }
130
131 std::string SemAcquireSimcall::dot_label(int times_considered) const
132 {
133   return SimcallObserver::dot_label(times_considered) + "Sem ACQUIRE";
134 }
135
136 bool SemAcquireSimcall::is_enabled() const
137 {
138   static bool warned = false;
139   if (not warned) {
140     XBT_INFO("Using semaphore in model-checked code is still experimental. Use at your own risk");
141     warned = true;
142   }
143   return true;
144 }
145
146 int ActivityTestanySimcall::get_max_consider() const
147 {
148   // Only Comms are of interest to MC for now. When all types of activities can be consider, this function can simply
149   // return the size of activities_.
150   int count = 0;
151   for (const auto& act : activities_)
152     if (dynamic_cast<activity::CommImpl*>(act) != nullptr)
153       count++;
154   return count;
155 }
156
157 void ActivityTestanySimcall::prepare(int times_considered)
158 {
159   next_value_ = times_considered;
160 }
161
162 /*
163 std::string ActivityTestanySimcall::to_string(int times_considered) const
164 {
165   std::string res = SimcallObserver::to_string(times_considered);
166   if (times_considered == -1) {
167     res += "TestAny FALSE(-)";
168   } else {
169     res += "TestAny(" + xbt::string_printf("(%d of %zu)", times_considered + 1, activities_.size());
170   }
171
172   return res;
173 }*/
174
175 std::string ActivityTestanySimcall::dot_label(int times_considered) const
176 {
177   std::string res = SimcallObserver::dot_label(times_considered) + "TestAny ";
178   if (times_considered == -1) {
179     res += "FALSE";
180   } else {
181     res += xbt::string_printf("TRUE [%d of %zu]", times_considered + 1, activities_.size());
182   }
183   return res;
184 }
185
186 bool ActivityTestSimcall::depends(SimcallObserver* other)
187 {
188   if (get_issuer() == other->get_issuer())
189     return false;
190
191   if (dynamic_cast<ActivityTestSimcall*>(other))
192     return true;
193
194   const auto* comm1 = dynamic_cast<activity::CommImpl*>(activity_);
195   if (comm1 == nullptr)
196     return false;
197
198   if (dynamic_cast<ActivityWaitSimcall*>(other) != nullptr &&
199       (comm1->src_actor_.get() == nullptr || comm1->dst_actor_.get() == nullptr))
200     return false;
201
202   if (comm1->src_buff_ == nullptr || comm1->dst_buff_ == nullptr)
203     return false;
204
205   if (const auto* test = dynamic_cast<ActivityTestSimcall*>(other)) {
206     const auto* comm2 = dynamic_cast<activity::CommImpl*>(test->get_activity());
207     if (comm2 == nullptr)
208       return false;
209     else if (comm2->src_buff_ == nullptr || comm2->dst_buff_ == nullptr)
210       return false;
211   }
212
213   if (auto* wait = dynamic_cast<ActivityWaitSimcall*>(other)) {
214     auto* comm2 = dynamic_cast<activity::CommImpl*>(wait->get_activity());
215     if (comm2 == nullptr)
216       return false;
217     if (comm1->src_buff_ == comm2->src_buff_ && comm1->dst_buff_ == comm2->dst_buff_)
218       return false;
219     if (comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr && comm2->src_buff_ != nullptr &&
220         comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ && comm1->dst_buff_ != comm2->dst_buff_ &&
221         comm2->dst_buff_ != comm1->src_buff_)
222       return false;
223   }
224
225   return true;
226 }
227 void ActivityWaitSimcall::serialize(Simcall& type, std::stringstream& stream)
228 {
229   if (auto* comm = dynamic_cast<activity::CommImpl*>(activity_)) {
230     type = Simcall::COMM_WAIT;
231     stream << (timeout_ > 0) << ' ' << comm;
232     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
233     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
234     stream << ' ' << comm->get_mailbox_id();
235     stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_;
236   } else {
237     type = Simcall::UNKNOWN;
238   }
239 }
240
241 /*
242 std::string ActivityTestSimcall::to_string(int times_considered) const
243 {
244   std::string res = SimcallObserver::to_string(times_considered) + "Test ";
245   if (const auto* comm = dynamic_cast<activity::CommImpl*>(activity_)) {
246     if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
247       res += "FALSE(comm=";
248       res += XBT_LOG_ISENABLED(mc_observer, xbt_log_priority_verbose) ? xbt::string_printf("%p)", comm)
249                                                                       : "(verbose only))";
250     } else {
251       res += "TRUE(comm=";
252
253       auto src = comm->src_actor_;
254       auto dst = comm->dst_actor_;
255       res +=
256           XBT_LOG_ISENABLED(mc_observer, xbt_log_priority_verbose) ? xbt::string_printf("%p", comm) : "(verbose only) ";
257       res += xbt::string_printf("[(%ld)%s (%s) ", src->get_pid(), src->get_host()->get_cname(), src->get_cname()) +
258              "-> " +
259              xbt::string_printf("(%ld)%s (%s)])", dst->get_pid(), dst->get_host()->get_cname(), dst->get_cname());
260     }
261   } else
262     xbt_die("Only Comms are supported here for now");
263   return res;
264 }*/
265
266 std::string ActivityTestSimcall::dot_label(int times_considered) const
267 {
268   std::string res  = SimcallObserver::dot_label(times_considered) + "Test ";
269   const auto* comm = dynamic_cast<activity::CommImpl*>(activity_);
270   if (comm && (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr)) {
271     res += "FALSE";
272   } else {
273     res += "TRUE";
274   }
275   return res;
276 }
277
278 bool ActivityWaitSimcall::is_enabled() const
279 {
280   /* FIXME: check also that src and dst processes are not suspended */
281   const auto* comm = dynamic_cast<activity::CommImpl*>(activity_);
282   if (comm == nullptr)
283     xbt_die("Only Comms are supported here for now");
284
285   if (comm->src_timeout_ || comm->dst_timeout_) {
286     /* If it has a timeout it will be always be enabled (regardless of who declared the timeout),
287      * because even if the communication is not ready, it can timeout and won't block. */
288     if (_sg_mc_timeout == 1)
289       return true;
290   }
291   /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
292   else if (comm->detached() && comm->src_actor_ == nullptr && comm->get_state() == activity::State::READY)
293     return (comm->dst_actor_ != nullptr);
294   return (comm->src_actor_ && comm->dst_actor_);
295 }
296
297 std::string ActivityWaitSimcall::dot_label(int times_considered) const
298 {
299   std::string res = SimcallObserver::dot_label(times_considered);
300   res += (times_considered == -1) ? "WaitTimeout " : "Wait ";
301
302   const auto* comm = dynamic_cast<activity::CommImpl*>(activity_);
303   if (comm) {
304     auto src = comm->src_actor_;
305     auto dst = comm->dst_actor_;
306     res += " [(" + std::to_string(src ? src->get_pid() : 0) + ")";
307     res += "->(" + std::to_string(dst ? dst->get_pid() : 0) + ")]";
308   } else
309     xbt_die("Only Comms are supported here for now");
310   return res;
311 }
312
313 std::string ActivityWaitanySimcall::dot_label(int times_considered) const
314 {
315   return SimcallObserver::dot_label(times_considered) +
316          xbt::string_printf("WaitAny [%d of %zu]", times_considered + 1, activities_.size());
317 }
318
319 bool ActivityWaitanySimcall::is_enabled() const
320 {
321   // FIXME: deal with other kind of activities (Exec and I/Os)
322   // FIXME: Can be factored with ActivityWaitSimcall::is_enabled()
323   const auto* comm = dynamic_cast<activity::CommImpl*>(activities_[next_value_]);
324   if (comm == nullptr)
325     xbt_die("Only Comms are supported here for now");
326   if (comm->src_timeout_ || comm->dst_timeout_) {
327     /* If it has a timeout it will be always be enabled (regardless of who declared the timeout),
328      * because even if the communication is not ready, it can timeout and won't block. */
329     if (_sg_mc_timeout == 1)
330       return true;
331   }
332   /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
333   else if (comm->detached() && comm->src_actor_ == nullptr && comm->get_state() == activity::State::READY)
334     return (comm->dst_actor_ != nullptr);
335   return (comm->src_actor_ && comm->dst_actor_);
336 }
337
338 int ActivityWaitanySimcall::get_max_consider() const
339 {
340   return static_cast<int>(activities_.size());
341 }
342
343 void ActivityWaitanySimcall::prepare(int times_considered)
344 {
345   next_value_ = times_considered;
346 }
347
348 void CommIsendSimcall::serialize(Simcall& type, std::stringstream& stream)
349 {
350   type = Simcall::ISEND;
351   stream << mbox_->get_id() << ' ' << (void*)src_buff_ << ' ' << src_buff_size_;
352   XBT_DEBUG("SendObserver mbox:%u buff:%p size:%zu", mbox_->get_id(), src_buff_, src_buff_size_);
353 }
354
355 void CommIrecvSimcall::serialize(Simcall& type, std::stringstream& stream)
356 {
357   type = Simcall::IRECV;
358   stream << mbox_->get_id() << dst_buff_;
359 }
360
361 } // namespace actor
362 } // namespace kernel
363 } // namespace simgrid