Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
implement CommTestTransition
[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(mc::Transition::Type& type, std::stringstream& stream)
32 {
33   type = mc::Transition::Type::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 void RandomSimcall::prepare(int times_considered)
61 {
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);
64 }
65
66 int RandomSimcall::get_max_consider() const
67 {
68   return max_ - min_ + 1;
69 }
70
71 /*
72 std::string MutexLockSimcall::to_string(int times_considered) const
73 {
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)";
79   return res;
80 }*/
81
82 bool MutexLockSimcall::is_enabled() const
83 {
84   return not blocking_ || get_mutex()->get_owner() == nullptr || get_mutex()->get_owner() == get_issuer();
85 }
86
87 bool ConditionWaitSimcall::is_enabled() const
88 {
89   static bool warned = false;
90   if (not warned) {
91     XBT_INFO("Using condition variables in model-checked code is still experimental. Use at your own risk");
92     warned = true;
93   }
94   return true;
95 }
96
97 bool SemAcquireSimcall::is_enabled() const
98 {
99   static bool warned = false;
100   if (not warned) {
101     XBT_INFO("Using semaphore in model-checked code is still experimental. Use at your own risk");
102     warned = true;
103   }
104   return true;
105 }
106
107 int ActivityTestanySimcall::get_max_consider() const
108 {
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_.
111   int count = 0;
112   for (const auto& act : activities_)
113     if (dynamic_cast<activity::CommImpl*>(act) != nullptr)
114       count++;
115   return count;
116 }
117
118 void ActivityTestanySimcall::prepare(int times_considered)
119 {
120   next_value_ = times_considered;
121 }
122
123 /*
124 std::string ActivityTestanySimcall::to_string(int times_considered) const
125 {
126   std::string res = SimcallObserver::to_string(times_considered);
127   if (times_considered == -1) {
128     res += "TestAny FALSE(-)";
129   } else {
130     res += "TestAny(" + xbt::string_printf("(%d of %zu)", times_considered + 1, activities_.size());
131   }
132
133   return res;
134 }*/
135 void ActivityWaitSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
136 {
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_;
144   } else {
145     type = mc::Transition::Type::UNKNOWN;
146   }
147 }
148 void ActivityTestSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
149 {
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_;
156   } else {
157     type = mc::Transition::Type::UNKNOWN;
158   }
159 }
160
161 bool ActivityWaitSimcall::is_enabled() const
162 {
163   /* FIXME: check also that src and dst processes are not suspended */
164   const auto* comm = dynamic_cast<activity::CommImpl*>(activity_);
165   if (comm == nullptr)
166     xbt_die("Only Comms are supported here for now");
167
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)
172       return true;
173   }
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_);
178 }
179
180 bool ActivityWaitanySimcall::is_enabled() const
181 {
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_]);
185   if (comm == nullptr)
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)
191       return true;
192   }
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_);
197 }
198
199 int ActivityWaitanySimcall::get_max_consider() const
200 {
201   return static_cast<int>(activities_.size());
202 }
203
204 void ActivityWaitanySimcall::prepare(int times_considered)
205 {
206   next_value_ = times_considered;
207 }
208
209 void CommIsendSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
210 {
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_);
214 }
215
216 void CommIrecvSimcall::serialize(mc::Transition::Type& type, std::stringstream& stream)
217 {
218   type = mc::Transition::Type::COMM_RECV;
219   stream << mbox_->get_id() << dst_buff_;
220 }
221
222 } // namespace actor
223 } // namespace kernel
224 } // namespace simgrid