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/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/modelchecker.h>
9 #include <simgrid/s4u/Host.hpp>
11 #define SIMIX_H_NO_DEPRECATED_WARNING // avoid deprecation warning on include (remove with XBT_ATTRIB_DEPRECATED_v333)
12 #include <simgrid/simix.h>
14 #include "src/kernel/activity/CommImpl.hpp"
15 #include "src/kernel/activity/MailboxImpl.hpp"
16 #include "src/kernel/actor/SimcallObserver.hpp"
17 #include "src/kernel/resource/CpuImpl.hpp"
18 #include "src/kernel/resource/NetworkModel.hpp"
19 #include "src/kernel/resource/StandardLinkImpl.hpp"
20 #include "src/mc/mc_replay.hpp"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_network, kernel, "Kernel network-related synchronization");
24 /******************************************************************************/
25 /* SIMIX_comm_copy_data callbacks */
26 /******************************************************************************/
27 // XBT_ATTRIB_DEPRECATED_v333
28 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
30 simgrid::kernel::activity::CommImpl::set_copy_data_callback(callback);
33 // XBT_ATTRIB_DEPRECATED_v333
34 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
36 simgrid::s4u::Comm::copy_buffer_callback(comm, buff, buff_size);
39 // XBT_ATTRIB_DEPRECATED_v333
40 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
42 simgrid::s4u::Comm::copy_pointer_callback(comm, buff, buff_size);
45 namespace simgrid::kernel::activity {
46 xbt::signal<void(CommImpl const&)> CommImpl::on_start;
47 xbt::signal<void(CommImpl const&)> CommImpl::on_completion;
49 std::function<void(CommImpl*, void*, size_t)> CommImpl::copy_data_callback_ = &s4u::Comm::copy_pointer_callback;
51 void CommImpl::set_copy_data_callback(const std::function<void(CommImpl*, void*, size_t)>& callback)
53 copy_data_callback_ = callback;
56 CommImpl& CommImpl::set_type(CommImplType type)
62 CommImpl& CommImpl::set_source(s4u::Host* from)
68 CommImpl& CommImpl::set_destination(s4u::Host* to)
74 CommImpl& CommImpl::set_size(double size)
80 CommImpl& CommImpl::set_rate(double rate)
85 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
88 mbox_id_ = mbox->get_id();
93 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
96 src_buff_size_ = size;
100 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
103 dst_buff_size_ = size;
107 CommImpl& CommImpl::detach()
113 CommImpl::~CommImpl()
115 XBT_DEBUG("Really free communication %p in state %s (detached = %d)", this, get_state_str(), detached_);
119 if (detached_ && get_state() != State::DONE) {
120 /* the communication has failed and was detached:
121 * we have to free the buffer */
123 clean_fun(src_buff_);
130 /** @brief Starts the simulation of a communication synchro. */
131 CommImpl* CommImpl::start()
133 /* If both the sender and the receiver are already there, start the communication */
134 if (get_state() == State::READY) {
135 from_ = from_ != nullptr ? from_ : src_actor_->get_host();
136 xbt_assert(from_->is_on());
137 to_ = to_ != nullptr ? to_ : dst_actor_->get_host();
138 xbt_assert(to_->is_on());
140 /* Getting the network_model from the origin host
141 * Valid while we have a single network model, otherwise we would need to change this function to first get the
142 * routes and later create the respective surf actions */
143 auto net_model = from_->get_netpoint()->get_englobing_zone()->get_network_model();
145 surf_action_ = net_model->communicate(from_, to_, size_, rate_);
146 surf_action_->set_activity(this);
147 surf_action_->set_category(get_tracing_category());
148 set_start_time(surf_action_->get_start_time());
149 set_state(State::RUNNING);
152 XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p; state: %s)", this, from_->get_cname(),
153 to_->get_cname(), surf_action_, get_state_str());
155 /* If a link is failed, detect it immediately */
156 if (surf_action_->get_state() == resource::Action::State::FAILED) {
157 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", from_->get_cname(),
159 set_state(State::LINK_FAILURE);
162 } else if ((src_actor_ != nullptr && src_actor_->is_suspended()) ||
163 (dst_actor_ != nullptr && dst_actor_->is_suspended())) {
164 /* If any of the actor is suspended, create the synchro but stop its execution,
165 it will be restarted when the sender actor resume */
166 if (src_actor_->is_suspended())
167 XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
169 src_actor_->get_cname(), src_actor_->get_host()->get_cname());
171 XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
173 dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
175 surf_action_->suspend();
182 std::vector<s4u::Link*> CommImpl::get_traversed_links() const
184 xbt_assert(get_state() != State::WAITING, "You cannot use %s() if your communication is not ready (%s)", __FUNCTION__,
186 std::vector<s4u::Link*> vlinks;
187 XBT_ATTRIB_UNUSED double res = 0;
188 from_->route_to(to_, vlinks, &res);
192 /** @brief Copy the communication data from the sender's buffer to the receiver's one */
193 void CommImpl::copy_data()
195 size_t buff_size = src_buff_size_;
196 /* If there is no data to copy then return */
197 if (not src_buff_ || not dst_buff_ || copied_)
200 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
201 src_actor_ ? src_actor_->get_host()->get_cname() : "a finished actor", src_buff_,
202 dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished actor", dst_buff_, buff_size);
204 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
205 if (dst_buff_size_) {
206 buff_size = std::min(buff_size, *dst_buff_size_);
208 /* Update the receiver's buffer size to the copied amount */
209 *dst_buff_size_ = buff_size;
214 copy_data_fun(this, src_buff_, buff_size);
216 copy_data_callback_(this, src_buff_, buff_size);
219 /* Set the copied flag so we copy data only once */
220 /* (this function might be called from both communication ends) */
224 ActivityImplPtr CommImpl::isend(actor::CommIsendSimcall* observer)
226 auto* mbox = observer->get_mailbox();
227 XBT_DEBUG("send from mailbox %p", mbox);
229 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
230 CommImplPtr this_comm(new CommImpl());
231 this_comm->set_type(CommImplType::SEND);
233 /* Look for communication synchro matching our needs. We also provide a description of
234 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
236 * If it is not found then push our communication into the rendez-vous point */
237 CommImplPtr other_comm =
238 mbox->find_matching_comm(CommImplType::RECEIVE, observer->get_match_fun(), observer->get_payload(), this_comm,
239 /*done*/ false, /*remove_matching*/ true);
241 if (not other_comm) {
242 other_comm = std::move(this_comm);
244 if (mbox->is_permanent()) {
245 // this mailbox is for small messages, which have to be sent right now
246 other_comm->set_state(State::READY);
247 other_comm->dst_actor_ = mbox->get_permanent_receiver().get();
248 mbox->push_done(other_comm);
249 XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
252 mbox->push(other_comm);
255 XBT_DEBUG("Receive already pushed");
257 other_comm->set_state(State::READY);
259 observer->set_comm(other_comm.get());
261 if (observer->is_detached()) {
262 other_comm->detach();
263 other_comm->clean_fun = observer->get_clean_fun();
265 other_comm->clean_fun = nullptr;
266 observer->get_issuer()->activities_.emplace_back(other_comm);
269 /* Setup the communication synchro */
270 other_comm->src_actor_ = observer->get_issuer();
271 other_comm->src_data_ = observer->get_payload();
273 .set_src_buff(observer->get_src_buff(), observer->get_src_buff_size())
274 .set_size(observer->get_payload_size())
275 .set_rate(observer->get_rate());
277 other_comm->match_fun = observer->get_match_fun();
278 other_comm->copy_data_fun = observer->get_copy_data_fun();
280 if (MC_is_active() || MC_record_replay_is_active())
281 other_comm->set_state(simgrid::kernel::activity::State::RUNNING);
285 return (observer->is_detached() ? nullptr : other_comm);
288 ActivityImplPtr CommImpl::irecv(actor::CommIrecvSimcall* observer)
290 CommImplPtr this_synchro(new CommImpl());
291 this_synchro->set_type(CommImplType::RECEIVE);
293 auto* mbox = observer->get_mailbox();
294 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
296 CommImplPtr other_comm;
297 // communication already done, get it inside the list of completed comms
298 if (mbox->is_permanent() && mbox->has_some_done_comm()) {
299 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
300 // find a match in the list of already received comms
301 other_comm = mbox->find_matching_comm(CommImplType::SEND, observer->get_match_fun(), observer->get_payload(),
302 this_synchro, /*done*/ true, /*remove_matching*/ true);
303 if (other_comm && other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
304 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
305 other_comm->set_state(State::DONE);
306 other_comm->set_mailbox(nullptr);
308 // if not found, assume the receiver came first, register it to the mailbox in the classical way
309 if (not other_comm) {
310 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
312 other_comm = std::move(this_synchro);
313 mbox->push(other_comm);
315 observer->get_issuer()->activities_.emplace_back(other_comm);
318 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
320 /* Look for communication activity matching our needs. We also provide a description of
321 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
323 * If it is not found then push our communication into the rendez-vous point */
324 other_comm = mbox->find_matching_comm(CommImplType::SEND, observer->get_match_fun(), observer->get_payload(),
325 this_synchro, /*done*/ false, /*remove_matching*/ true);
327 if (other_comm == nullptr) {
328 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->size());
329 other_comm = std::move(this_synchro);
330 mbox->push(other_comm);
332 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
334 other_comm->set_state(simgrid::kernel::activity::State::READY);
336 observer->get_issuer()->activities_.emplace_back(other_comm);
338 observer->set_comm(other_comm.get());
340 /* Setup communication synchro */
341 other_comm->dst_actor_ = observer->get_issuer();
342 other_comm->dst_data_ = observer->get_payload();
343 other_comm->set_dst_buff(observer->get_dst_buff(), observer->get_dst_buff_size());
345 if (observer->get_rate() > -1.0 && (other_comm->get_rate() < 0.0 || observer->get_rate() < other_comm->get_rate()))
346 other_comm->set_rate(observer->get_rate());
348 other_comm->match_fun = observer->get_match_fun();
349 other_comm->copy_data_fun = observer->get_copy_data_fun();
351 if (MC_is_active() || MC_record_replay_is_active()) {
352 other_comm->set_state(State::RUNNING);
360 bool CommImpl::test(actor::ActorImpl* issuer)
362 if ((MC_is_active() || MC_record_replay_is_active()) && src_actor_ && dst_actor_)
363 set_state(State::DONE);
364 return ActivityImpl::test(issuer);
367 void CommImpl::wait_for(actor::ActorImpl* issuer, double timeout)
369 XBT_DEBUG("CommImpl::wait_for(%g), %p, state %s", timeout, this, get_state_str());
371 /* Associate this simcall to the wait synchro */
372 register_simcall(&issuer->simcall_);
373 if (MC_is_active() || MC_record_replay_is_active()) {
374 // FIXME: what about timeouts?
375 set_state(State::DONE);
379 ActivityImpl::wait_for(issuer, timeout);
382 void CommImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout)
384 std::vector<ActivityImpl*> activities(comms.begin(), comms.end());
385 ActivityImpl::wait_any_for(issuer, activities, timeout);
388 void CommImpl::suspend()
390 /* FIXME: shall we suspend also the timeout synchro? */
392 surf_action_->suspend();
393 /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
396 void CommImpl::resume()
398 /*FIXME: check what happen with the timeouts */
400 surf_action_->resume();
401 /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
404 void CommImpl::cancel()
406 /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
407 if (get_state() == State::WAITING) {
410 set_state(State::CANCELED);
412 } else if (not MC_is_active() /* when running the MC there are no surf actions */
413 && not MC_record_replay_is_active() && (get_state() == State::READY || get_state() == State::RUNNING)) {
414 surf_action_->cancel();
418 /** @brief This is part of the cleanup process, probably an internal command */
419 void CommImpl::cleanup_surf()
424 src_timeout_->unref();
425 src_timeout_ = nullptr;
429 dst_timeout_->unref();
430 dst_timeout_ = nullptr;
434 void CommImpl::post()
436 on_completion(*this);
438 /* Update synchro state */
439 if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
440 set_state(State::SRC_TIMEOUT);
441 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
442 set_state(State::DST_TIMEOUT);
443 else if ((from_ && not from_->is_on()) || (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED))
444 set_state(State::SRC_HOST_FAILURE);
445 else if ((to_ && not to_->is_on()) || (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED))
446 set_state(State::DST_HOST_FAILURE);
447 else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
448 set_state(State::LINK_FAILURE);
449 } else if (get_state() == State::RUNNING) {
450 xbt_assert(from_ && from_->is_on());
451 xbt_assert(to_ && to_->is_on());
452 set_state(State::DONE);
455 XBT_DEBUG("CommImpl::post(): comm %p, state %s, src_proc %p, dst_proc %p, detached: %d", this, get_state_str(),
456 src_actor_.get(), dst_actor_.get(), detached_);
458 /* destroy the surf actions associated with the Simix communication */
461 /* Answer all simcalls associated with the synchro */
464 void CommImpl::set_exception(actor::ActorImpl* issuer)
466 switch (get_state()) {
468 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
470 case State::SRC_TIMEOUT:
472 std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
475 case State::DST_TIMEOUT:
477 std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
480 case State::SRC_HOST_FAILURE:
481 if (issuer == src_actor_)
482 issuer->set_wannadie();
484 set_state(State::FAILED);
485 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
489 case State::DST_HOST_FAILURE:
490 if (issuer == dst_actor_)
491 issuer->set_wannadie();
493 set_state(State::FAILED);
494 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
498 case State::LINK_FAILURE:
499 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
501 this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
502 dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, issuer->get_cname(), issuer, detached_);
503 if (src_actor_ == issuer) {
504 XBT_DEBUG("I'm source");
505 } else if (dst_actor_ == issuer) {
506 XBT_DEBUG("I'm dest");
508 XBT_DEBUG("I'm neither source nor dest");
510 set_state(State::FAILED);
511 issuer->throw_exception(std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Link failure")));
514 case State::CANCELED:
515 if (issuer == dst_actor_)
517 std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
520 std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
524 xbt_assert(get_state() == State::DONE, "Internal error in CommImpl::finish(): unexpected synchro state %s",
529 void CommImpl::finish()
531 XBT_DEBUG("CommImpl::finish() in state %s", get_state_str());
532 /* If the synchro is still in a rendez-vous point then remove from it */
536 if (get_state() == State::DONE)
539 while (not simcalls_.empty()) {
540 actor::Simcall* simcall = simcalls_.front();
541 simcalls_.pop_front();
543 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
544 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
547 if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
548 continue; // if actor handling comm is killed
550 handle_activity_waitany(simcall);
552 /* Check out for errors */
554 if (not simcall->issuer_->get_host()->is_on()) {
555 simcall->issuer_->set_wannadie();
557 // Do not answer to dying actors
558 if (not simcall->issuer_->wannadie()) {
559 set_exception(simcall->issuer_);
560 simcall->issuer_->simcall_answer();
564 simcall->issuer_->waiting_synchro_ = nullptr;
565 simcall->issuer_->activities_.remove(this);
567 if (simcall->issuer_ != dst_actor_ && dst_actor_ != nullptr)
568 dst_actor_->activities_.remove(this);
569 if (simcall->issuer_ != src_actor_ && src_actor_ != nullptr)
570 src_actor_->activities_.remove(this);
575 } // namespace simgrid::kernel::activity