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 #include "src/kernel/activity/CommImpl.hpp"
12 #include "src/kernel/activity/MailboxImpl.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14 #include "src/kernel/context/Context.hpp"
15 #include "src/kernel/resource/CpuImpl.hpp"
16 #include "src/kernel/resource/LinkImpl.hpp"
17 #include "src/kernel/resource/StandardLinkImpl.hpp"
18 #include "src/mc/mc_replay.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_network, kernel, "Kernel network-related synchronization");
22 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox, double task_size,
23 double rate, unsigned char* src_buff, size_t src_buff_size,
24 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
25 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
26 void* data, double timeout)
28 simgrid::kernel::actor::CommIsendSimcall observer(src, mbox, task_size, rate, src_buff, src_buff_size, match_fun,
29 nullptr, copy_data_fun, data, false);
30 simgrid::kernel::activity::ActivityImplPtr comm = simgrid::kernel::activity::CommImpl::isend(&observer);
31 comm->wait_for(simcall->issuer_, timeout);
34 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr simcall_HANDLER_comm_isend(
35 smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate,
36 unsigned char* src_buff, size_t src_buff_size,
37 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
38 void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
39 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), // used to copy data if not default one
40 void* data, bool detached)
42 simgrid::kernel::actor::CommIsendSimcall observer(src_proc, mbox, task_size, rate, src_buff, src_buff_size, match_fun,
43 clean_fun, copy_data_fun, data, detached);
44 return simgrid::kernel::activity::CommImpl::isend(&observer);
47 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
48 unsigned char* dst_buff, size_t* dst_buff_size,
49 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
50 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
51 void* data, double timeout, double rate)
53 simgrid::kernel::actor::CommIrecvSimcall observer(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun,
55 simgrid::kernel::activity::ActivityImplPtr comm = simgrid::kernel::activity::CommImpl::irecv(&observer);
56 comm->wait_for(simcall->issuer_, timeout);
59 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr
60 simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff,
61 size_t* dst_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
62 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
65 simgrid::kernel::actor::CommIrecvSimcall observer(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun,
67 return simgrid::kernel::activity::CommImpl::irecv(&observer);
70 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm, double timeout)
72 comm->wait_for(simcall->issuer_, timeout);
75 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count,
78 std::vector<simgrid::kernel::activity::CommImpl*> comms_vec(comms, comms + count);
79 simgrid::kernel::activity::CommImpl::wait_any_for(simcall->issuer_, comms_vec, timeout);
82 /******************************************************************************/
83 /* SIMIX_comm_copy_data callbacks */
84 /******************************************************************************/
85 // XBT_ATTRIB_DEPRECATED_v333
86 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
88 simgrid::kernel::activity::CommImpl::set_copy_data_callback(callback);
91 // XBT_ATTRIB_DEPRECATED_v333
92 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
94 simgrid::s4u::Comm::copy_buffer_callback(comm, buff, buff_size);
97 // XBT_ATTRIB_DEPRECATED_v333
98 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
100 simgrid::s4u::Comm::copy_pointer_callback(comm, buff, buff_size);
106 xbt::signal<void(CommImpl const&)> CommImpl::on_start;
107 xbt::signal<void(CommImpl const&)> CommImpl::on_completion;
109 void (*CommImpl::copy_data_callback_)(CommImpl*, void*, size_t) = &s4u::Comm::copy_pointer_callback;
111 void CommImpl::set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t))
113 copy_data_callback_ = callback;
116 CommImpl& CommImpl::set_size(double size)
122 CommImpl& CommImpl::set_rate(double rate)
127 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
130 mbox_id_ = mbox->get_id();
135 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
138 src_buff_size_ = size;
142 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
145 dst_buff_size_ = size;
149 CommImpl& CommImpl::detach()
155 CommImpl::CommImpl(s4u::Host* from, s4u::Host* to, double bytes) : size_(bytes), detached_(true), from_(from), to_(to)
157 set_state(State::READY);
160 CommImpl::~CommImpl()
162 XBT_DEBUG("Really free communication %p in state %s (detached = %d)", this, get_state_str(), detached_);
166 if (detached_ && get_state() != State::DONE) {
167 /* the communication has failed and was detached:
168 * we have to free the buffer */
170 clean_fun(src_buff_);
177 /** @brief Starts the simulation of a communication synchro. */
178 CommImpl* CommImpl::start()
180 /* If both the sender and the receiver are already there, start the communication */
181 if (get_state() == State::READY) {
182 from_ = from_ != nullptr ? from_ : src_actor_->get_host();
183 to_ = to_ != nullptr ? to_ : dst_actor_->get_host();
184 /* Getting the network_model from the origin host
185 * Valid while we have a single network model, otherwise we would need to change this function to first get the
186 * routes and later create the respective surf actions */
187 auto net_model = from_->get_netpoint()->get_englobing_zone()->get_network_model();
189 surf_action_ = net_model->communicate(from_, to_, size_, rate_);
190 surf_action_->set_activity(this);
191 surf_action_->set_category(get_tracing_category());
192 set_start_time(surf_action_->get_start_time());
193 set_state(State::RUNNING);
196 XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p; state: %s)", this, from_->get_cname(),
197 to_->get_cname(), surf_action_, get_state_str());
199 /* If a link is failed, detect it immediately */
200 if (surf_action_->get_state() == resource::Action::State::FAILED) {
201 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", from_->get_cname(),
203 set_state(State::LINK_FAILURE);
206 } else if ((src_actor_ != nullptr && src_actor_->is_suspended()) ||
207 (dst_actor_ != nullptr && dst_actor_->is_suspended())) {
208 /* If any of the actor is suspended, create the synchro but stop its execution,
209 it will be restarted when the sender actor resume */
210 if (src_actor_->is_suspended())
211 XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
213 src_actor_->get_cname(), src_actor_->get_host()->get_cname());
215 XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
217 dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
219 surf_action_->suspend();
226 std::vector<s4u::Link*> CommImpl::get_traversed_links() const
228 xbt_assert(get_state() != State::WAITING, "You cannot use %s() if your communication is not ready (%s)", __FUNCTION__,
230 std::vector<s4u::Link*> vlinks;
231 XBT_ATTRIB_UNUSED double res = 0;
232 from_->route_to(to_, vlinks, &res);
236 /** @brief Copy the communication data from the sender's buffer to the receiver's one */
237 void CommImpl::copy_data()
239 size_t buff_size = src_buff_size_;
240 /* If there is no data to copy then return */
241 if (not src_buff_ || not dst_buff_ || copied_)
244 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
245 src_actor_ ? src_actor_->get_host()->get_cname() : "a finished actor", src_buff_,
246 dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished actor", dst_buff_, buff_size);
248 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
249 if (dst_buff_size_) {
250 buff_size = std::min(buff_size, *dst_buff_size_);
252 /* Update the receiver's buffer size to the copied amount */
253 *dst_buff_size_ = buff_size;
258 copy_data_fun(this, src_buff_, buff_size);
260 copy_data_callback_(this, src_buff_, buff_size);
263 /* Set the copied flag so we copy data only once */
264 /* (this function might be called from both communication ends) */
268 ActivityImplPtr CommImpl::isend(actor::CommIsendSimcall* observer)
270 auto* mbox = observer->get_mailbox();
271 XBT_DEBUG("send from mailbox %p", mbox);
273 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
274 CommImplPtr this_comm(new CommImpl(CommImpl::Type::SEND));
276 /* Look for communication synchro matching our needs. We also provide a description of
277 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
279 * If it is not found then push our communication into the rendez-vous point */
280 CommImplPtr other_comm =
281 mbox->find_matching_comm(CommImpl::Type::RECEIVE, observer->get_match_fun(), observer->get_payload(), this_comm,
282 /*done*/ false, /*remove_matching*/ true);
284 if (not other_comm) {
285 other_comm = std::move(this_comm);
287 if (mbox->is_permanent()) {
288 // this mailbox is for small messages, which have to be sent right now
289 other_comm->set_state(State::READY);
290 other_comm->dst_actor_ = mbox->get_permanent_receiver().get();
291 mbox->push_done(other_comm);
292 XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
295 mbox->push(other_comm);
298 XBT_DEBUG("Receive already pushed");
300 other_comm->set_state(State::READY);
302 observer->set_comm(other_comm.get());
304 if (observer->is_detached()) {
305 other_comm->detach();
306 other_comm->clean_fun = observer->get_clean_fun();
308 other_comm->clean_fun = nullptr;
309 observer->get_issuer()->activities_.emplace_back(other_comm);
312 /* Setup the communication synchro */
313 other_comm->src_actor_ = observer->get_issuer();
314 other_comm->src_data_ = observer->get_payload();
316 .set_src_buff(observer->get_src_buff(), observer->get_src_buff_size())
317 .set_size(observer->get_payload_size())
318 .set_rate(observer->get_rate());
320 other_comm->match_fun = observer->get_match_fun();
321 other_comm->copy_data_fun = observer->get_copy_data_fun();
323 if (MC_is_active() || MC_record_replay_is_active())
324 other_comm->set_state(simgrid::kernel::activity::State::RUNNING);
328 return (observer->is_detached() ? nullptr : other_comm);
331 ActivityImplPtr CommImpl::irecv(actor::CommIrecvSimcall* observer)
333 CommImplPtr this_synchro(new CommImpl(CommImpl::Type::RECEIVE));
334 auto* mbox = observer->get_mailbox();
335 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
337 CommImplPtr other_comm;
338 // communication already done, get it inside the list of completed comms
339 if (mbox->is_permanent() && mbox->has_some_done_comm()) {
340 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
341 // find a match in the list of already received comms
342 other_comm = mbox->find_matching_comm(CommImpl::Type::SEND, observer->get_match_fun(), observer->get_payload(),
343 this_synchro, /*done*/ true, /*remove_matching*/ true);
344 // if not found, assume the receiver came first, register it to the mailbox in the classical way
345 if (not other_comm) {
346 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
348 other_comm = std::move(this_synchro);
349 mbox->push(other_comm);
351 if (other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
352 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
353 other_comm->set_state(State::DONE);
354 other_comm->set_mailbox(nullptr);
358 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
360 /* Look for communication activity matching our needs. We also provide a description of
361 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
363 * If it is not found then push our communication into the rendez-vous point */
364 other_comm = mbox->find_matching_comm(CommImpl::Type::SEND, observer->get_match_fun(), observer->get_payload(),
365 this_synchro, /*done*/ false, /*remove_matching*/ true);
367 if (other_comm == nullptr) {
368 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->size());
369 other_comm = std::move(this_synchro);
370 mbox->push(other_comm);
372 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
374 other_comm->set_state(simgrid::kernel::activity::State::READY);
376 observer->get_issuer()->activities_.emplace_back(other_comm);
378 observer->set_comm(other_comm.get());
380 /* Setup communication synchro */
381 other_comm->dst_actor_ = observer->get_issuer();
382 other_comm->dst_data_ = observer->get_payload();
383 other_comm->set_dst_buff(observer->get_dst_buff(), observer->get_dst_buff_size());
385 if (observer->get_rate() > -1.0 && (other_comm->get_rate() < 0.0 || observer->get_rate() < other_comm->get_rate()))
386 other_comm->set_rate(observer->get_rate());
388 other_comm->match_fun = observer->get_match_fun();
389 other_comm->copy_data_fun = observer->get_copy_data_fun();
391 if (MC_is_active() || MC_record_replay_is_active()) {
392 other_comm->set_state(State::RUNNING);
400 bool CommImpl::test(actor::ActorImpl* issuer)
402 if ((MC_is_active() || MC_record_replay_is_active()) && src_actor_ && dst_actor_)
403 set_state(State::DONE);
404 return ActivityImpl::test(issuer);
407 void CommImpl::wait_for(actor::ActorImpl* issuer, double timeout)
409 XBT_DEBUG("CommImpl::wait_for(%g), %p, state %s", timeout, this, get_state_str());
411 /* Associate this simcall to the wait synchro */
412 register_simcall(&issuer->simcall_);
413 if (MC_is_active() || MC_record_replay_is_active()) {
414 // FIXME: what about timeouts?
415 set_state(State::DONE);
419 ActivityImpl::wait_for(issuer, timeout);
422 void CommImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout)
424 std::vector<ActivityImpl*> activities(comms.begin(), comms.end());
425 ActivityImpl::wait_any_for(issuer, activities, timeout);
428 void CommImpl::suspend()
430 /* FIXME: shall we suspend also the timeout synchro? */
432 surf_action_->suspend();
433 /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
436 void CommImpl::resume()
438 /*FIXME: check what happen with the timeouts */
440 surf_action_->resume();
441 /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
444 void CommImpl::cancel()
446 /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
447 if (get_state() == State::WAITING) {
450 set_state(State::CANCELED);
452 } else if (not MC_is_active() /* when running the MC there are no surf actions */
453 && not MC_record_replay_is_active() && (get_state() == State::READY || get_state() == State::RUNNING)) {
454 surf_action_->cancel();
458 /** @brief This is part of the cleanup process, probably an internal command */
459 void CommImpl::cleanup_surf()
464 src_timeout_->unref();
465 src_timeout_ = nullptr;
469 dst_timeout_->unref();
470 dst_timeout_ = nullptr;
474 void CommImpl::post()
476 on_completion(*this);
478 /* Update synchro state */
479 if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
480 set_state(State::SRC_TIMEOUT);
481 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
482 set_state(State::DST_TIMEOUT);
483 else if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED)
484 set_state(State::SRC_HOST_FAILURE);
485 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED)
486 set_state(State::DST_HOST_FAILURE);
487 else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
488 set_state(State::LINK_FAILURE);
490 set_state(State::DONE);
492 XBT_DEBUG("CommImpl::post(): comm %p, state %s, src_proc %p, dst_proc %p, detached: %d", this, get_state_str(),
493 src_actor_.get(), dst_actor_.get(), detached_);
495 /* destroy the surf actions associated with the Simix communication */
498 /* Answer all simcalls associated with the synchro */
501 void CommImpl::set_exception(actor::ActorImpl* issuer)
503 switch (get_state()) {
505 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
507 case State::SRC_TIMEOUT:
509 std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
512 case State::DST_TIMEOUT:
514 std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
517 case State::SRC_HOST_FAILURE:
518 if (issuer == src_actor_)
519 issuer->context_->set_wannadie();
521 set_state(State::FAILED);
522 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
526 case State::DST_HOST_FAILURE:
527 if (issuer == dst_actor_)
528 issuer->context_->set_wannadie();
530 set_state(State::FAILED);
531 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
535 case State::LINK_FAILURE:
536 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
538 this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
539 dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, issuer->get_cname(), issuer, detached_);
540 if (src_actor_ == issuer) {
541 XBT_DEBUG("I'm source");
542 } else if (dst_actor_ == issuer) {
543 XBT_DEBUG("I'm dest");
545 XBT_DEBUG("I'm neither source nor dest");
547 set_state(State::FAILED);
548 issuer->throw_exception(std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Link failure")));
551 case State::CANCELED:
552 if (issuer == dst_actor_)
554 std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
557 std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
561 xbt_assert(get_state() == State::DONE, "Internal error in CommImpl::finish(): unexpected synchro state %s",
566 void CommImpl::finish()
568 XBT_DEBUG("CommImpl::finish() in state %s", get_state_str());
569 /* If the synchro is still in a rendez-vous point then remove from it */
573 if (get_state() == State::DONE)
576 while (not simcalls_.empty()) {
577 smx_simcall_t simcall = simcalls_.front();
578 simcalls_.pop_front();
580 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
581 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
584 if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
585 continue; // if actor handling comm is killed
587 handle_activity_waitany(simcall);
589 /* Check out for errors */
591 if (not simcall->issuer_->get_host()->is_on()) {
592 simcall->issuer_->context_->set_wannadie();
594 // Do not answer to dying actors
595 if (not simcall->issuer_->context_->wannadie()) {
596 set_exception(simcall->issuer_);
597 simcall->issuer_->simcall_answer();
601 simcall->issuer_->waiting_synchro_ = nullptr;
602 simcall->issuer_->activities_.remove(this);
604 if (simcall->issuer_ != dst_actor_ && dst_actor_ != nullptr)
605 dst_actor_->activities_.remove(this);
606 if (simcall->issuer_ != src_actor_ && src_actor_ != nullptr)
607 src_actor_->activities_.remove(this);
612 } // namespace activity
613 } // namespace kernel
614 } // namespace simgrid