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::activity::ActivityImplPtr comm = simgrid::kernel::activity::CommImpl::isend(
29 src, mbox, task_size, rate, src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, false);
30 simcall->mc_value_ = 0;
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 return simgrid::kernel::activity::CommImpl::isend(src_proc, mbox, task_size, rate, src_buff, src_buff_size, match_fun,
43 clean_fun, copy_data_fun, data, detached);
46 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
47 unsigned char* dst_buff, size_t* dst_buff_size,
48 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
49 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
50 void* data, double timeout, double rate)
52 simgrid::kernel::activity::ActivityImplPtr comm = simgrid::kernel::activity::CommImpl::irecv(
53 receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
54 simcall->mc_value_ = 0;
55 comm->wait_for(simcall->issuer_, timeout);
58 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr
59 simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff,
60 size_t* dst_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
61 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
64 return simgrid::kernel::activity::CommImpl::irecv(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun,
68 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm, double timeout)
70 comm->wait_for(simcall->issuer_, timeout);
73 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count,
76 std::vector<simgrid::kernel::activity::CommImpl*> comms_vec(comms, comms + count);
77 simgrid::kernel::activity::CommImpl::wait_any_for(simcall->issuer_, comms_vec, timeout);
80 /******************************************************************************/
81 /* SIMIX_comm_copy_data callbacks */
82 /******************************************************************************/
83 // XBT_ATTRIB_DEPRECATED_v333
84 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
86 simgrid::kernel::activity::CommImpl::set_copy_data_callback(callback);
89 // XBT_ATTRIB_DEPRECATED_v333
90 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
92 simgrid::s4u::Comm::copy_buffer_callback(comm, buff, buff_size);
95 // XBT_ATTRIB_DEPRECATED_v333
96 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
98 simgrid::s4u::Comm::copy_pointer_callback(comm, buff, buff_size);
104 xbt::signal<void(CommImpl const&)> CommImpl::on_start;
105 xbt::signal<void(CommImpl const&)> CommImpl::on_completion;
107 void (*CommImpl::copy_data_callback_)(CommImpl*, void*, size_t) = &s4u::Comm::copy_pointer_callback;
109 void CommImpl::set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t))
111 copy_data_callback_ = callback;
114 CommImpl& CommImpl::set_size(double size)
120 CommImpl& CommImpl::set_rate(double rate)
125 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
131 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
134 src_buff_size_ = size;
138 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
141 dst_buff_size_ = size;
145 CommImpl& CommImpl::detach()
151 CommImpl::CommImpl(s4u::Host* from, s4u::Host* to, double bytes) : size_(bytes), detached_(true), from_(from), to_(to)
153 set_state(State::READY);
156 CommImpl::~CommImpl()
158 XBT_DEBUG("Really free communication %p in state %s (detached = %d)", this, get_state_str(), detached_);
162 if (detached_ && get_state() != State::DONE) {
163 /* the communication has failed and was detached:
164 * we have to free the buffer */
166 clean_fun(src_buff_);
173 /** @brief Starts the simulation of a communication synchro. */
174 CommImpl* CommImpl::start()
176 /* If both the sender and the receiver are already there, start the communication */
177 if (get_state() == State::READY) {
178 from_ = from_ != nullptr ? from_ : src_actor_->get_host();
179 to_ = to_ != nullptr ? to_ : dst_actor_->get_host();
180 /* Getting the network_model from the origin host
181 * Valid while we have a single network model, otherwise we would need to change this function to first get the
182 * routes and later create the respective surf actions */
183 auto net_model = from_->get_netpoint()->get_englobing_zone()->get_network_model();
185 surf_action_ = net_model->communicate(from_, to_, size_, rate_);
186 surf_action_->set_activity(this);
187 surf_action_->set_category(get_tracing_category());
188 set_start_time(surf_action_->get_start_time());
189 set_state(State::RUNNING);
192 XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p; state: %s)", this, from_->get_cname(),
193 to_->get_cname(), surf_action_, get_state_str());
195 /* If a link is failed, detect it immediately */
196 if (surf_action_->get_state() == resource::Action::State::FAILED) {
197 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", from_->get_cname(),
199 set_state(State::LINK_FAILURE);
202 } else if ((src_actor_ != nullptr && src_actor_->is_suspended()) ||
203 (dst_actor_ != nullptr && dst_actor_->is_suspended())) {
204 /* If any of the actor is suspended, create the synchro but stop its execution,
205 it will be restarted when the sender actor resume */
206 if (src_actor_->is_suspended())
207 XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
209 src_actor_->get_cname(), src_actor_->get_host()->get_cname());
211 XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
213 dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
215 surf_action_->suspend();
222 std::vector<s4u::Link*> CommImpl::get_traversed_links() const
224 xbt_assert(get_state() != State::WAITING, "You cannot use %s() if your communication is not ready (%s)", __FUNCTION__,
226 std::vector<s4u::Link*> vlinks;
227 XBT_ATTRIB_UNUSED double res = 0;
228 from_->route_to(to_, vlinks, &res);
232 /** @brief Copy the communication data from the sender's buffer to the receiver's one */
233 void CommImpl::copy_data()
235 size_t buff_size = src_buff_size_;
236 /* If there is no data to copy then return */
237 if (not src_buff_ || not dst_buff_ || copied_)
240 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
241 src_actor_ ? src_actor_->get_host()->get_cname() : "a finished actor", src_buff_,
242 dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished actor", dst_buff_, buff_size);
244 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
245 if (dst_buff_size_) {
246 buff_size = std::min(buff_size, *dst_buff_size_);
248 /* Update the receiver's buffer size to the copied amount */
249 *dst_buff_size_ = buff_size;
254 copy_data_fun(this, src_buff_, buff_size);
256 copy_data_callback_(this, src_buff_, buff_size);
259 /* Set the copied flag so we copy data only once */
260 /* (this function might be called from both communication ends) */
265 CommImpl::isend(actor::ActorImpl* sender, MailboxImpl* mbox, double task_size, double rate, unsigned char* src_buff,
266 size_t src_buff_size, bool (*match_fun)(void*, void*, CommImpl*),
267 void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
268 void (*copy_data_fun)(CommImpl*, void*, size_t), // used to copy data if not default one
269 void* data, bool detached)
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 = mbox->find_matching_comm(CommImpl::Type::RECEIVE, match_fun, data, this_comm,
281 /*done*/ false, /*remove_matching*/ true);
283 if (not other_comm) {
284 other_comm = std::move(this_comm);
286 if (mbox->is_permanent()) {
287 // this mailbox is for small messages, which have to be sent right now
288 other_comm->set_state(State::READY);
289 other_comm->dst_actor_ = mbox->get_permanent_receiver().get();
290 mbox->push_done(other_comm);
291 XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
294 mbox->push(other_comm);
297 XBT_DEBUG("Receive already pushed");
299 other_comm->set_state(State::READY);
303 other_comm->detach();
304 other_comm->clean_fun = clean_fun;
306 other_comm->clean_fun = nullptr;
307 sender->activities_.emplace_back(other_comm);
310 /* Setup the communication synchro */
311 other_comm->src_actor_ = sender;
312 other_comm->src_data_ = data;
313 (*other_comm).set_src_buff(src_buff, src_buff_size).set_size(task_size).set_rate(rate);
315 other_comm->match_fun = match_fun;
316 other_comm->copy_data_fun = copy_data_fun;
318 if (MC_is_active() || MC_record_replay_is_active())
319 other_comm->set_state(simgrid::kernel::activity::State::RUNNING);
323 return (detached ? nullptr : other_comm);
326 ActivityImplPtr CommImpl::irecv(actor::ActorImpl* receiver, MailboxImpl* mbox, unsigned char* dst_buff,
327 size_t* dst_buff_size, bool (*match_fun)(void*, void*, CommImpl*),
328 void (*copy_data_fun)(CommImpl*, void*, size_t), void* data, double rate)
330 CommImplPtr this_synchro(new CommImpl(CommImpl::Type::RECEIVE));
331 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
333 CommImplPtr other_comm;
334 // communication already done, get it inside the list of completed comms
335 if (mbox->is_permanent() && mbox->has_some_done_comm()) {
336 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
337 // find a match in the list of already received comms
338 other_comm = mbox->find_matching_comm(CommImpl::Type::SEND, match_fun, data, this_synchro, /*done*/ true,
339 /*remove_matching*/ true);
340 // if not found, assume the receiver came first, register it to the mailbox in the classical way
341 if (not other_comm) {
342 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
344 other_comm = std::move(this_synchro);
345 mbox->push(other_comm);
347 if (other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
348 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
349 other_comm->set_state(State::DONE);
350 other_comm->set_mailbox(nullptr);
354 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
356 /* Look for communication activity matching our needs. We also provide a description of
357 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
359 * If it is not found then push our communication into the rendez-vous point */
360 other_comm = mbox->find_matching_comm(CommImpl::Type::SEND, match_fun, data, this_synchro, /*done*/ false,
361 /*remove_matching*/ true);
363 if (other_comm == nullptr) {
364 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->size());
365 other_comm = std::move(this_synchro);
366 mbox->push(other_comm);
368 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
370 other_comm->set_state(simgrid::kernel::activity::State::READY);
372 receiver->activities_.emplace_back(other_comm);
375 /* Setup communication synchro */
376 other_comm->dst_actor_ = receiver;
377 other_comm->dst_data_ = data;
378 other_comm->set_dst_buff(dst_buff, dst_buff_size);
380 if (rate > -1.0 && (other_comm->get_rate() < 0.0 || rate < other_comm->get_rate()))
381 other_comm->set_rate(rate);
383 other_comm->match_fun = match_fun;
384 other_comm->copy_data_fun = copy_data_fun;
386 if (MC_is_active() || MC_record_replay_is_active()) {
387 other_comm->set_state(State::RUNNING);
395 bool CommImpl::test(actor::ActorImpl* issuer)
397 if ((MC_is_active() || MC_record_replay_is_active()) && src_actor_ && dst_actor_)
398 set_state(State::DONE);
399 return ActivityImpl::test(issuer);
402 void CommImpl::wait_for(actor::ActorImpl* issuer, double timeout)
404 XBT_DEBUG("CommImpl::wait_for(%g), %p, state %s", timeout, this, get_state_str());
406 /* Associate this simcall to the wait synchro */
407 register_simcall(&issuer->simcall_);
408 if (MC_is_active() || MC_record_replay_is_active()) {
409 int idx = issuer->simcall_.mc_value_;
411 set_state(State::DONE);
413 /* If we reached this point, the wait simcall must have a timeout */
414 /* Otherwise it shouldn't be enabled and executed by the MC */
417 set_state(issuer == src_actor_ ? State::SRC_TIMEOUT : State::DST_TIMEOUT);
422 ActivityImpl::wait_for(issuer, timeout);
425 void CommImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout)
427 std::vector<ActivityImpl*> activities(comms.begin(), comms.end());
428 ActivityImpl::wait_any_for(issuer, activities, timeout);
431 void CommImpl::suspend()
433 /* FIXME: shall we suspend also the timeout synchro? */
435 surf_action_->suspend();
436 /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
439 void CommImpl::resume()
441 /*FIXME: check what happen with the timeouts */
443 surf_action_->resume();
444 /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
447 void CommImpl::cancel()
449 /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
450 if (get_state() == State::WAITING) {
453 set_state(State::CANCELED);
455 } else if (not MC_is_active() /* when running the MC there are no surf actions */
456 && not MC_record_replay_is_active() && (get_state() == State::READY || get_state() == State::RUNNING)) {
457 surf_action_->cancel();
461 /** @brief This is part of the cleanup process, probably an internal command */
462 void CommImpl::cleanup_surf()
467 src_timeout_->unref();
468 src_timeout_ = nullptr;
472 dst_timeout_->unref();
473 dst_timeout_ = nullptr;
477 void CommImpl::post()
479 on_completion(*this);
481 /* Update synchro state */
482 if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
483 set_state(State::SRC_TIMEOUT);
484 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
485 set_state(State::DST_TIMEOUT);
486 else if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED)
487 set_state(State::SRC_HOST_FAILURE);
488 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED)
489 set_state(State::DST_HOST_FAILURE);
490 else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
491 set_state(State::LINK_FAILURE);
493 set_state(State::DONE);
495 XBT_DEBUG("CommImpl::post(): comm %p, state %s, src_proc %p, dst_proc %p, detached: %d", this, get_state_str(),
496 src_actor_.get(), dst_actor_.get(), detached_);
498 /* destroy the surf actions associated with the Simix communication */
501 /* Answer all simcalls associated with the synchro */
504 void CommImpl::set_exception(actor::ActorImpl* issuer)
506 switch (get_state()) {
508 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
510 case State::SRC_TIMEOUT:
512 std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
515 case State::DST_TIMEOUT:
517 std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
520 case State::SRC_HOST_FAILURE:
521 if (issuer == src_actor_)
522 issuer->context_->set_wannadie();
524 set_state(State::FAILED);
525 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
529 case State::DST_HOST_FAILURE:
530 if (issuer == dst_actor_)
531 issuer->context_->set_wannadie();
533 set_state(State::FAILED);
534 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
538 case State::LINK_FAILURE:
539 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
541 this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
542 dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, issuer->get_cname(), issuer, detached_);
543 if (src_actor_ == issuer) {
544 XBT_DEBUG("I'm source");
545 } else if (dst_actor_ == issuer) {
546 XBT_DEBUG("I'm dest");
548 XBT_DEBUG("I'm neither source nor dest");
550 set_state(State::FAILED);
551 issuer->throw_exception(std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Link failure")));
554 case State::CANCELED:
555 if (issuer == dst_actor_)
557 std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
560 std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
564 xbt_assert(get_state() == State::DONE, "Internal error in CommImpl::finish(): unexpected synchro state %s",
569 void CommImpl::finish()
571 XBT_DEBUG("CommImpl::finish() in state %s", get_state_str());
572 /* If the synchro is still in a rendez-vous point then remove from it */
576 if (get_state() == State::DONE)
579 while (not simcalls_.empty()) {
580 smx_simcall_t simcall = simcalls_.front();
581 simcalls_.pop_front();
583 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
584 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
587 if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
588 continue; // if actor handling comm is killed
590 handle_activity_waitany(simcall);
592 /* Check out for errors */
594 if (not simcall->issuer_->get_host()->is_on()) {
595 simcall->issuer_->context_->set_wannadie();
597 // Do not answer to dying actors
598 if (not simcall->issuer_->context_->wannadie()) {
599 set_exception(simcall->issuer_);
600 simcall->issuer_->simcall_answer();
604 simcall->issuer_->waiting_synchro_ = nullptr;
605 simcall->issuer_->activities_.remove(this);
607 if (simcall->issuer_ != dst_actor_ && dst_actor_ != nullptr)
608 dst_actor_->activities_.remove(this);
609 if (simcall->issuer_ != src_actor_ && src_actor_ != nullptr)
610 src_actor_->activities_.remove(this);
615 } // namespace activity
616 } // namespace kernel
617 } // namespace simgrid