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());
135 void ActivityWaitSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
137 if (auto* comm = dynamic_cast<activity::CommImpl*>(activity_)) {
138 type = mc::Transition::Type::COMM_WAIT;
139 stream << (timeout_ > 0) << ' ' << comm;
140 stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
141 stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
142 stream << ' ' << comm->get_mailbox_id();
143 stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_;
145 type = mc::Transition::Type::UNKNOWN;
148 void ActivityTestSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
150 if (auto* comm = dynamic_cast<activity::CommImpl*>(activity_)) {
151 type = mc::Transition::Type::COMM_TEST;
152 stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
153 stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
154 stream << ' ' << comm->get_mailbox_id();
155 stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_;
157 type = mc::Transition::Type::UNKNOWN;
161 bool ActivityWaitSimcall::is_enabled() const
163 /* FIXME: check also that src and dst processes are not suspended */
164 const auto* comm = dynamic_cast<activity::CommImpl*>(activity_);
166 xbt_die("Only Comms are supported here for now");
168 if (comm->src_timeout_ || comm->dst_timeout_) {
169 /* If it has a timeout it will be always be enabled (regardless of who declared the timeout),
170 * because even if the communication is not ready, it can timeout and won't block. */
171 if (_sg_mc_timeout == 1)
174 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
175 else if (comm->detached() && comm->src_actor_ == nullptr && comm->get_state() == activity::State::READY)
176 return (comm->dst_actor_ != nullptr);
177 return (comm->src_actor_ && comm->dst_actor_);
180 bool ActivityWaitanySimcall::is_enabled() const
182 // FIXME: deal with other kind of activities (Exec and I/Os)
183 // FIXME: Can be factored with ActivityWaitSimcall::is_enabled()
184 const auto* comm = dynamic_cast<activity::CommImpl*>(activities_[next_value_]);
186 xbt_die("Only Comms are supported here for now");
187 if (comm->src_timeout_ || comm->dst_timeout_) {
188 /* If it has a timeout it will be always be enabled (regardless of who declared the timeout),
189 * because even if the communication is not ready, it can timeout and won't block. */
190 if (_sg_mc_timeout == 1)
193 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
194 else if (comm->detached() && comm->src_actor_ == nullptr && comm->get_state() == activity::State::READY)
195 return (comm->dst_actor_ != nullptr);
196 return (comm->src_actor_ && comm->dst_actor_);
199 int ActivityWaitanySimcall::get_max_consider() const
201 return static_cast<int>(activities_.size());
204 void ActivityWaitanySimcall::prepare(int times_considered)
206 next_value_ = times_considered;
209 void CommIsendSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
211 type = mc::Transition::Type::COMM_SEND;
212 stream << mbox_->get_id() << ' ' << (void*)src_buff_ << ' ' << src_buff_size_;
213 XBT_DEBUG("SendObserver mbox:%u buff:%p size:%zu", mbox_->get_id(), src_buff_, src_buff_size_);
216 void CommIrecvSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
218 type = mc::Transition::Type::COMM_RECV;
219 stream << mbox_->get_id() << dst_buff_;
223 } // namespace kernel
224 } // namespace simgrid