Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
better handling of ActivityImpl::wait_for with dying actors
[simgrid.git] / src / kernel / activity / ActivityImpl.cpp
1 /* Copyright (c) 2007-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 <simgrid/modelchecker.h>
7 #include <simgrid/s4u/Engine.hpp>
8
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"
16
17 #include <boost/range/algorithm.hpp>
18 #include <cmath> // isfinite()
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_activity, kernel, "Kernel activity-related synchronization");
21
22 namespace simgrid {
23 namespace kernel {
24 namespace activity {
25
26 ActivityImpl::~ActivityImpl()
27 {
28   clean_action();
29   XBT_DEBUG("Destroy activity %p", this);
30 }
31
32 void ActivityImpl::register_simcall(smx_simcall_t simcall)
33 {
34   simcalls_.push_back(simcall);
35   simcall->issuer_->waiting_synchro_ = this;
36 }
37
38 void ActivityImpl::unregister_simcall(smx_simcall_t simcall)
39 {
40   // Remove the first occurrence of simcall:
41   auto j = boost::range::find(simcalls_, simcall);
42   if (j != simcalls_.end())
43     simcalls_.erase(j);
44 }
45
46 void ActivityImpl::clean_action()
47 {
48   if (surf_action_) {
49     surf_action_->unref();
50     surf_action_ = nullptr;
51   }
52 }
53
54 double ActivityImpl::get_remaining() const
55 {
56   return surf_action_ ? surf_action_->get_remains() : 0;
57 }
58
59 const char* ActivityImpl::get_state_str() const
60 {
61   return to_c_str(state_);
62 }
63
64 bool ActivityImpl::test(actor::ActorImpl* issuer)
65 {
66   // Associate this simcall to the synchro
67   auto* observer = dynamic_cast<kernel::actor::ActivityTestSimcall*>(issuer->simcall_.observer_);
68   if (observer)
69     register_simcall(&issuer->simcall_);
70
71   if (state_ != State::WAITING && state_ != State::RUNNING) {
72     finish();
73     issuer->exception_ = nullptr; // Do not propagate exception in that case
74     return true;
75   }
76
77   if (observer) {
78     observer->set_result(false);
79     issuer->waiting_synchro_ = nullptr;
80     unregister_simcall(&issuer->simcall_);
81     issuer->simcall_answer();
82   }
83   return false;
84 }
85
86 ssize_t ActivityImpl::test_any(actor::ActorImpl* issuer, const std::vector<ActivityImpl*>& activities)
87 {
88   if (MC_is_active() || MC_record_replay_is_active()) {
89     int idx = issuer->simcall_.mc_value_;
90     xbt_assert(idx == -1 || activities[idx]->test(issuer));
91     return idx;
92   }
93
94   for (std::size_t i = 0; i < activities.size(); ++i) {
95     if (activities[i]->test(issuer)) {
96       auto* observer = dynamic_cast<kernel::actor::ActivityTestanySimcall*>(issuer->simcall_.observer_);
97       xbt_assert(observer != nullptr);
98       observer->set_result(i);
99       issuer->simcall_answer();
100       return i;
101     }
102   }
103   issuer->simcall_answer();
104   return -1;
105 }
106
107 void ActivityImpl::wait_for(actor::ActorImpl* issuer, double timeout)
108 {
109   XBT_DEBUG("Wait for execution of synchro %p, state %s", this, get_state_str());
110   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
111
112   /* Associate this simcall to the synchro */
113   register_simcall(&issuer->simcall_);
114
115   xbt_assert(not MC_is_active() && not MC_record_replay_is_active(), "MC is currently not supported here.");
116
117   /* If the synchro is already finished then perform the error handling */
118   if (state_ != State::WAITING && state_ != State::RUNNING) {
119     finish();
120   } else {
121     /* we need a sleep action (even when the timeout is infinite) to be notified of host failures */
122     /* Comms handle that a bit differently of the other activities */
123     auto* comm = dynamic_cast<CommImpl*>(this);
124     if (comm != nullptr) {
125       resource::Action* sleep = issuer->get_host()->get_cpu()->sleep(timeout);
126       sleep->set_activity(comm);
127
128       if (issuer == comm->src_actor_)
129         comm->src_timeout_ = sleep;
130       else
131         comm->dst_timeout_ = sleep;
132     } else {
133       RawImplPtr synchro(new RawImpl([this, issuer]() {
134         this->unregister_simcall(&issuer->simcall_);
135         issuer->waiting_synchro_ = nullptr;
136         issuer->exception_       = nullptr;
137         auto* observer           = dynamic_cast<kernel::actor::ActivityWaitSimcall*>(issuer->simcall_.observer_);
138         xbt_assert(observer != nullptr);
139         observer->set_result(true);
140       }));
141       synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
142       synchro->register_simcall(&issuer->simcall_);
143     }
144   }
145 }
146
147 void ActivityImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<ActivityImpl*>& activities, double timeout)
148 {
149   XBT_DEBUG("Wait for execution of any synchro");
150   if (timeout < 0.0) {
151     issuer->simcall_.timeout_cb_ = nullptr;
152   } else {
153     issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &activities]() {
154       issuer->simcall_.timeout_cb_ = nullptr;
155       for (auto* act : activities)
156         act->unregister_simcall(&issuer->simcall_);
157       // default result (-1) is set in actor::ActivityWaitanySimcall
158       issuer->simcall_answer();
159     });
160   }
161
162   for (auto* act : activities) {
163     /* associate this simcall to the the synchro */
164     act->simcalls_.push_back(&issuer->simcall_);
165     /* see if the synchro is already finished */
166     if (act->get_state() != State::WAITING && act->get_state() != State::RUNNING) {
167       act->finish();
168       break;
169     }
170   }
171   XBT_DEBUG("Exit from ActivityImlp::wait_any_for");
172 }
173
174 void ActivityImpl::suspend()
175 {
176   if (surf_action_ == nullptr)
177     return;
178   XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
179   surf_action_->suspend();
180   on_suspended(*this);
181 }
182
183 void ActivityImpl::resume()
184 {
185   if (surf_action_ == nullptr)
186     return;
187   XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
188   surf_action_->resume();
189   on_resumed(*this);
190 }
191
192 void ActivityImpl::cancel()
193 {
194   XBT_VERB("Activity %p is canceled", this);
195   if (surf_action_ != nullptr)
196     surf_action_->cancel();
197   state_ = State::CANCELED;
198 }
199
200 void ActivityImpl::handle_activity_waitany(smx_simcall_t simcall)
201 {
202   /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
203    * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
204    * simcall */
205   if (auto* observer = dynamic_cast<actor::ActivityWaitanySimcall*>(simcall->observer_)) {
206     if (simcall->timeout_cb_) {
207       simcall->timeout_cb_->remove();
208       simcall->timeout_cb_ = nullptr;
209     }
210
211     auto activities = observer->get_activities();
212     for (auto* act : activities)
213       act->unregister_simcall(simcall);
214
215     if (not MC_is_active() && not MC_record_replay_is_active()) {
216       auto element   = std::find(activities.begin(), activities.end(), this);
217       int rank       = element != activities.end() ? static_cast<int>(std::distance(activities.begin(), element)) : -1;
218       auto* observer = dynamic_cast<kernel::actor::ActivityWaitanySimcall*>(simcall->observer_);
219       observer->set_result(rank);
220     }
221   }
222 }
223
224 // boost::intrusive_ptr<Activity> support:
225 void intrusive_ptr_add_ref(ActivityImpl* activity)
226 {
227   activity->refcount_.fetch_add(1, std::memory_order_relaxed);
228 }
229
230 void intrusive_ptr_release(ActivityImpl* activity)
231 {
232   if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
233     std::atomic_thread_fence(std::memory_order_acquire);
234     delete activity;
235   }
236 }
237 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
238 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
239 }
240 }
241 } // namespace simgrid::kernel::activity::