Logo AND Algorithmique Numérique Distribuée

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