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 void SimcallObserver::serialize(std::stringstream& stream) const
24 stream << (short)mc::Transition::Type::UNKNOWN;
26 bool SimcallObserver::depends(SimcallObserver* other)
30 /* Random is only dependent when issued by the same actor (ie, always independent) */
31 bool RandomSimcall::depends(SimcallObserver* other)
33 return get_issuer() == other->get_issuer();
35 void RandomSimcall::serialize(std::stringstream& stream) const
37 stream << (short)mc::Transition::Type::RANDOM << ' ';
38 stream << min_ << ' ' << max_;
41 bool MutexSimcall::depends(SimcallObserver* other)
43 if (dynamic_cast<RandomSimcall*>(other) != nullptr)
44 return other->depends(this); /* Other is random, that is very permissive. Use that relation instead. */
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);
49 return true; // Depends on anything we don't know
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())
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())
61 return true; // Depend on things we don't know for sure that they are independent
64 void RandomSimcall::prepare(int times_considered)
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);
70 int RandomSimcall::get_max_consider() const
72 return max_ - min_ + 1;
76 std::string MutexLockSimcall::to_string(int times_considered) const
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)";
86 bool MutexLockSimcall::is_enabled() const
88 return not blocking_ || get_mutex()->get_owner() == nullptr || get_mutex()->get_owner() == get_issuer();
91 bool ConditionWaitSimcall::is_enabled() const
93 static bool warned = false;
95 XBT_INFO("Using condition variables in model-checked code is still experimental. Use at your own risk");
101 bool SemAcquireSimcall::is_enabled() const
103 static bool warned = false;
105 XBT_INFO("Using semaphore in model-checked code is still experimental. Use at your own risk");
111 int ActivityTestanySimcall::get_max_consider() const
113 // Only Comms are of interest to MC for now. When all types of activities can be consider, this function can simply
114 // return the size of activities_.
116 for (const auto& act : activities_)
117 if (dynamic_cast<activity::CommImpl*>(act) != nullptr)
122 void ActivityTestanySimcall::prepare(int times_considered)
124 next_value_ = times_considered;
128 std::string ActivityTestanySimcall::to_string(int times_considered) const
130 std::string res = SimcallObserver::to_string(times_considered);
131 if (times_considered == -1) {
132 res += "TestAny FALSE(-)";
134 res += "TestAny(" + xbt::string_printf("(%d of %zu)", times_considered + 1, activities_.size());
139 void ActivityWaitSimcall::serialize(std::stringstream& stream) const
141 if (auto* comm = dynamic_cast<activity::CommImpl*>(activity_)) {
142 stream << (short)mc::Transition::Type::COMM_WAIT << ' ';
143 stream << (timeout_ > 0) << ' ' << comm;
144 stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
145 stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
146 stream << ' ' << comm->get_mailbox_id();
147 stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_;
149 stream << (short)mc::Transition::Type::UNKNOWN;
152 void ActivityTestSimcall::serialize(std::stringstream& stream) const
154 if (auto* comm = dynamic_cast<activity::CommImpl*>(activity_)) {
155 stream << (short)mc::Transition::Type::COMM_TEST << ' ';
156 stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
157 stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
158 stream << ' ' << comm->get_mailbox_id();
159 stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_;
161 stream << (short)mc::Transition::Type::UNKNOWN;
165 bool ActivityWaitSimcall::is_enabled() const
167 /* FIXME: check also that src and dst processes are not suspended */
168 const auto* comm = dynamic_cast<activity::CommImpl*>(activity_);
170 xbt_die("Only Comms are supported here for now");
172 if (comm->src_timeout_ || comm->dst_timeout_) {
173 /* If it has a timeout it will be always be enabled (regardless of who declared the timeout),
174 * because even if the communication is not ready, it can timeout and won't block. */
175 if (_sg_mc_timeout == 1)
178 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
179 else if (comm->detached() && comm->src_actor_ == nullptr && comm->get_state() == activity::State::READY)
180 return (comm->dst_actor_ != nullptr);
181 return (comm->src_actor_ && comm->dst_actor_);
184 bool ActivityWaitanySimcall::is_enabled() const
186 // FIXME: deal with other kind of activities (Exec and I/Os)
187 // FIXME: Can be factored with ActivityWaitSimcall::is_enabled()
188 const auto* comm = dynamic_cast<activity::CommImpl*>(activities_[next_value_]);
190 xbt_die("Only Comms are supported here for now");
191 if (comm->src_timeout_ || comm->dst_timeout_) {
192 /* If it has a timeout it will be always be enabled (regardless of who declared the timeout),
193 * because even if the communication is not ready, it can timeout and won't block. */
194 if (_sg_mc_timeout == 1)
197 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
198 else if (comm->detached() && comm->src_actor_ == nullptr && comm->get_state() == activity::State::READY)
199 return (comm->dst_actor_ != nullptr);
200 return (comm->src_actor_ && comm->dst_actor_);
203 int ActivityWaitanySimcall::get_max_consider() const
205 return static_cast<int>(activities_.size());
208 void ActivityWaitanySimcall::prepare(int times_considered)
210 next_value_ = times_considered;
213 void CommIsendSimcall::serialize(std::stringstream& stream) const
215 stream << (short)mc::Transition::Type::COMM_SEND << ' ';
216 stream << mbox_->get_id() << ' ' << (void*)src_buff_ << ' ' << src_buff_size_;
217 XBT_DEBUG("SendObserver mbox:%u buff:%p size:%zu", mbox_->get_id(), src_buff_, src_buff_size_);
220 void CommIrecvSimcall::serialize(std::stringstream& stream) const
222 stream << (short)mc::Transition::Type::COMM_RECV << ' ';
223 stream << mbox_->get_id() << ' ' << dst_buff_;
227 } // namespace kernel
228 } // namespace simgrid