Logo AND Algorithmique Numérique Distribuée

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