1 /* Copyright (c) 2019-2022. The SimGrid Team. All rights reserved. */
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. */
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"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_observer, mc, "Logging specific to MC simcall observation");
22 bool SimcallObserver::depends(SimcallObserver* other)
26 /* Random is only dependent when issued by the same actor (ie, always independent) */
27 bool RandomSimcall::depends(SimcallObserver* other)
29 return get_issuer() == other->get_issuer();
31 void RandomSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
33 type = mc::Transition::Type::RANDOM;
34 stream << min_ << ' ' << max_;
37 bool MutexSimcall::depends(SimcallObserver* other)
39 if (dynamic_cast<RandomSimcall*>(other) != nullptr)
40 return other->depends(this); /* Other is random, that is very permissive. Use that relation instead. */
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);
45 return true; // Depends on anything we don't know
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())
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())
57 return true; // Depend on things we don't know for sure that they are independent
60 void RandomSimcall::prepare(int times_considered)
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);
66 int RandomSimcall::get_max_consider() const
68 return max_ - min_ + 1;
72 std::string MutexLockSimcall::to_string(int times_considered) const
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)";
82 bool MutexLockSimcall::is_enabled() const
84 return not blocking_ || get_mutex()->get_owner() == nullptr || get_mutex()->get_owner() == get_issuer();
87 bool ConditionWaitSimcall::is_enabled() const
89 static bool warned = false;
91 XBT_INFO("Using condition variables in model-checked code is still experimental. Use at your own risk");
97 bool SemAcquireSimcall::is_enabled() const
99 static bool warned = false;
101 XBT_INFO("Using semaphore in model-checked code is still experimental. Use at your own risk");
107 int ActivityTestanySimcall::get_max_consider() const
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_.
112 for (const auto& act : activities_)
113 if (dynamic_cast<activity::CommImpl*>(act) != nullptr)
118 void ActivityTestanySimcall::prepare(int times_considered)
120 next_value_ = times_considered;
124 std::string ActivityTestanySimcall::to_string(int times_considered) const
126 std::string res = SimcallObserver::to_string(times_considered);
127 if (times_considered == -1) {
128 res += "TestAny FALSE(-)";
130 res += "TestAny(" + xbt::string_printf("(%d of %zu)", times_considered + 1, activities_.size());
136 bool ActivityTestSimcall::depends(SimcallObserver* other)
138 if (get_issuer() == other->get_issuer())
141 if (dynamic_cast<ActivityTestSimcall*>(other))
144 const auto* comm1 = dynamic_cast<activity::CommImpl*>(activity_);
145 if (comm1 == nullptr)
148 if (dynamic_cast<ActivityWaitSimcall*>(other) != nullptr &&
149 (comm1->src_actor_.get() == nullptr || comm1->dst_actor_.get() == nullptr))
152 if (comm1->src_buff_ == nullptr || comm1->dst_buff_ == nullptr)
155 if (const auto* test = dynamic_cast<ActivityTestSimcall*>(other)) {
156 const auto* comm2 = dynamic_cast<activity::CommImpl*>(test->get_activity());
157 if (comm2 == nullptr)
159 else if (comm2->src_buff_ == nullptr || comm2->dst_buff_ == nullptr)
163 if (auto* wait = dynamic_cast<ActivityWaitSimcall*>(other)) {
164 auto* comm2 = dynamic_cast<activity::CommImpl*>(wait->get_activity());
165 if (comm2 == nullptr)
167 if (comm1->src_buff_ == comm2->src_buff_ && comm1->dst_buff_ == comm2->dst_buff_)
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_)
177 void ActivityWaitSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
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_;
187 type = mc::Transition::Type::UNKNOWN;
192 std::string ActivityTestSimcall::to_string(int times_considered) const
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)
203 auto src = comm->src_actor_;
204 auto dst = comm->dst_actor_;
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()) +
209 xbt::string_printf("(%ld)%s (%s)])", dst->get_pid(), dst->get_host()->get_cname(), dst->get_cname());
212 xbt_die("Only Comms are supported here for now");
216 bool ActivityWaitSimcall::is_enabled() const
218 /* FIXME: check also that src and dst processes are not suspended */
219 const auto* comm = dynamic_cast<activity::CommImpl*>(activity_);
221 xbt_die("Only Comms are supported here for now");
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)
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_);
235 bool ActivityWaitanySimcall::is_enabled() const
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_]);
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)
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_);
254 int ActivityWaitanySimcall::get_max_consider() const
256 return static_cast<int>(activities_.size());
259 void ActivityWaitanySimcall::prepare(int times_considered)
261 next_value_ = times_considered;
264 void CommIsendSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
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_);
271 void CommIrecvSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
273 type = mc::Transition::Type::IRECV;
274 stream << mbox_->get_id() << dst_buff_;
278 } // namespace kernel
279 } // namespace simgrid