1 /* Copyright (c) 2007-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 <simgrid/modelchecker.h>
7 #include <simgrid/s4u/Engine.hpp>
9 #include "src/kernel/activity/ActivityImpl.hpp"
10 #include "src/kernel/activity/CommImpl.hpp"
11 #include "src/kernel/activity/SynchroRaw.hpp"
12 #include "src/kernel/actor/ActorImpl.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14 #include "src/kernel/resource/CpuImpl.hpp"
15 #include "src/mc/mc_replay.hpp"
17 #include <boost/range/algorithm.hpp>
18 #include <cmath> // isfinite()
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_activity, kernel, "Kernel activity-related synchronization");
26 ActivityImpl::~ActivityImpl()
29 XBT_DEBUG("Destroy activity %p", this);
32 void ActivityImpl::register_simcall(smx_simcall_t simcall)
34 simcalls_.push_back(simcall);
35 simcall->issuer_->waiting_synchro_ = this;
38 void ActivityImpl::unregister_simcall(smx_simcall_t simcall)
40 // Remove the first occurrence of simcall:
41 auto j = boost::range::find(simcalls_, simcall);
42 if (j != simcalls_.end())
46 void ActivityImpl::clean_action()
49 surf_action_->unref();
50 surf_action_ = nullptr;
54 double ActivityImpl::get_remaining() const
56 return surf_action_ ? surf_action_->get_remains() : 0;
59 const char* ActivityImpl::get_state_str() const
61 return to_c_str(state_);
64 bool ActivityImpl::test(actor::ActorImpl* issuer)
66 // Associate this simcall to the synchro
67 auto* observer = dynamic_cast<kernel::actor::ActivityTestSimcall*>(issuer->simcall_.observer_);
69 register_simcall(&issuer->simcall_);
71 if (state_ != State::WAITING && state_ != State::RUNNING) {
73 issuer->exception_ = nullptr; // Do not propagate exception in that case
78 observer->set_result(false);
79 issuer->waiting_synchro_ = nullptr;
80 unregister_simcall(&issuer->simcall_);
81 issuer->simcall_answer();
86 ssize_t ActivityImpl::test_any(actor::ActorImpl* issuer, const std::vector<ActivityImpl*>& activities)
88 auto* observer = dynamic_cast<kernel::actor::ActivityTestanySimcall*>(issuer->simcall_.observer_);
89 xbt_assert(observer != nullptr);
91 if (MC_is_active() || MC_record_replay_is_active()) {
92 int idx = observer->get_value();
93 xbt_assert(idx == -1 || activities[idx]->test(issuer));
97 for (std::size_t i = 0; i < activities.size(); ++i) {
98 if (activities[i]->test(issuer)) {
99 observer->set_result(i);
100 issuer->simcall_answer();
104 issuer->simcall_answer();
108 void ActivityImpl::wait_for(actor::ActorImpl* issuer, double timeout)
110 XBT_DEBUG("Wait for execution of synchro %p, state %s", this, get_state_str());
111 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
113 /* Associate this simcall to the synchro */
114 register_simcall(&issuer->simcall_);
116 xbt_assert(not MC_is_active() && not MC_record_replay_is_active(), "MC is currently not supported here.");
118 /* If the synchro is already finished then perform the error handling */
119 if (state_ != State::WAITING && state_ != State::RUNNING) {
122 /* we need a sleep action (even when the timeout is infinite) to be notified of host failures */
123 /* Comms handle that a bit differently of the other activities */
124 auto* comm = dynamic_cast<CommImpl*>(this);
125 if (comm != nullptr) {
126 resource::Action* sleep = issuer->get_host()->get_cpu()->sleep(timeout);
127 sleep->set_activity(comm);
129 if (issuer == comm->src_actor_)
130 comm->src_timeout_ = sleep;
132 comm->dst_timeout_ = sleep;
134 RawImplPtr synchro(new RawImpl([this, issuer]() {
135 this->unregister_simcall(&issuer->simcall_);
136 issuer->waiting_synchro_ = nullptr;
137 issuer->exception_ = nullptr;
138 auto* observer = dynamic_cast<kernel::actor::ActivityWaitSimcall*>(issuer->simcall_.observer_);
139 xbt_assert(observer != nullptr);
140 observer->set_result(true);
142 synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
143 synchro->register_simcall(&issuer->simcall_);
148 void ActivityImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<ActivityImpl*>& activities, double timeout)
150 XBT_DEBUG("Wait for execution of any synchro");
151 if (MC_is_active() || MC_record_replay_is_active()) {
152 auto* observer = dynamic_cast<kernel::actor::ActivityWaitanySimcall*>(issuer->simcall_.observer_);
153 xbt_assert(observer != nullptr);
154 xbt_assert(timeout <= 0.0, "Timeout not implemented for waitany in the model-checker");
155 int idx = observer->get_value();
156 auto* act = activities[idx];
157 act->simcalls_.push_back(&issuer->simcall_);
158 observer->set_result(idx);
159 act->set_state(State::DONE);
165 issuer->simcall_.timeout_cb_ = nullptr;
167 issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &activities]() {
168 issuer->simcall_.timeout_cb_ = nullptr;
169 for (auto* act : activities)
170 act->unregister_simcall(&issuer->simcall_);
171 // default result (-1) is set in actor::ActivityWaitanySimcall
172 issuer->simcall_answer();
176 for (auto* act : activities) {
177 /* associate this simcall to the the synchro */
178 act->simcalls_.push_back(&issuer->simcall_);
179 /* see if the synchro is already finished */
180 if (act->get_state() != State::WAITING && act->get_state() != State::RUNNING) {
185 XBT_DEBUG("Exit from ActivityImlp::wait_any_for");
188 void ActivityImpl::suspend()
190 if (surf_action_ == nullptr)
192 XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
193 surf_action_->suspend();
197 void ActivityImpl::resume()
199 if (surf_action_ == nullptr)
201 XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
202 surf_action_->resume();
206 void ActivityImpl::cancel()
208 XBT_VERB("Activity %p is canceled", this);
209 if (surf_action_ != nullptr)
210 surf_action_->cancel();
211 state_ = State::CANCELED;
214 void ActivityImpl::handle_activity_waitany(smx_simcall_t simcall)
216 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
217 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
219 if (auto* observer = dynamic_cast<actor::ActivityWaitanySimcall*>(simcall->observer_)) {
220 if (simcall->timeout_cb_) {
221 simcall->timeout_cb_->remove();
222 simcall->timeout_cb_ = nullptr;
225 auto activities = observer->get_activities();
226 for (auto* act : activities)
227 act->unregister_simcall(simcall);
229 if (not MC_is_active() && not MC_record_replay_is_active()) {
230 auto element = std::find(activities.begin(), activities.end(), this);
231 int rank = element != activities.end() ? static_cast<int>(std::distance(activities.begin(), element)) : -1;
232 auto* observer = dynamic_cast<kernel::actor::ActivityWaitanySimcall*>(simcall->observer_);
233 observer->set_result(rank);
238 // boost::intrusive_ptr<Activity> support:
239 void intrusive_ptr_add_ref(ActivityImpl* activity)
241 activity->refcount_.fetch_add(1, std::memory_order_relaxed);
244 void intrusive_ptr_release(ActivityImpl* activity)
246 if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
247 std::atomic_thread_fence(std::memory_order_acquire);
251 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
252 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
255 } // namespace simgrid::kernel::activity::