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/context/Context.hpp"
18 #include "src/kernel/resource/CpuImpl.hpp"
19 #include "src/kernel/resource/LinkImpl.hpp"
20 #include "src/kernel/resource/StandardLinkImpl.hpp"
21 #include "src/mc/mc_replay.hpp"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_network, kernel, "Kernel network-related synchronization");
25 /******************************************************************************/
26 /* SIMIX_comm_copy_data callbacks */
27 /******************************************************************************/
28 // XBT_ATTRIB_DEPRECATED_v333
29 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
31 simgrid::kernel::activity::CommImpl::set_copy_data_callback(callback);
34 // XBT_ATTRIB_DEPRECATED_v333
35 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
37 simgrid::s4u::Comm::copy_buffer_callback(comm, buff, buff_size);
40 // XBT_ATTRIB_DEPRECATED_v333
41 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
43 simgrid::s4u::Comm::copy_pointer_callback(comm, buff, buff_size);
49 xbt::signal<void(CommImpl const&)> CommImpl::on_start;
50 xbt::signal<void(CommImpl const&)> CommImpl::on_completion;
52 void (*CommImpl::copy_data_callback_)(CommImpl*, void*, size_t) = &s4u::Comm::copy_pointer_callback;
54 void CommImpl::set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t))
56 copy_data_callback_ = callback;
59 CommImpl& CommImpl::set_type(CommImplType type)
65 CommImpl& CommImpl::set_source(s4u::Host* from)
71 CommImpl& CommImpl::set_destination(s4u::Host* to)
77 CommImpl& CommImpl::set_size(double size)
83 CommImpl& CommImpl::set_rate(double rate)
88 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
91 mbox_id_ = mbox->get_id();
96 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
99 src_buff_size_ = size;
103 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
106 dst_buff_size_ = size;
110 CommImpl& CommImpl::detach()
116 CommImpl::~CommImpl()
118 XBT_DEBUG("Really free communication %p in state %s (detached = %d)", this, get_state_str(), detached_);
122 if (detached_ && get_state() != State::DONE) {
123 /* the communication has failed and was detached:
124 * we have to free the buffer */
126 clean_fun(src_buff_);
133 /** @brief Starts the simulation of a communication synchro. */
134 CommImpl* CommImpl::start()
136 /* If both the sender and the receiver are already there, start the communication */
137 if (get_state() == State::READY) {
138 from_ = from_ != nullptr ? from_ : src_actor_->get_host();
139 xbt_assert(from_->is_on());
140 to_ = to_ != nullptr ? to_ : dst_actor_->get_host();
141 xbt_assert(to_->is_on());
143 /* Getting the network_model from the origin host
144 * Valid while we have a single network model, otherwise we would need to change this function to first get the
145 * routes and later create the respective surf actions */
146 auto net_model = from_->get_netpoint()->get_englobing_zone()->get_network_model();
148 surf_action_ = net_model->communicate(from_, to_, size_, rate_);
149 surf_action_->set_activity(this);
150 surf_action_->set_category(get_tracing_category());
151 set_start_time(surf_action_->get_start_time());
152 set_state(State::RUNNING);
155 XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p; state: %s)", this, from_->get_cname(),
156 to_->get_cname(), surf_action_, get_state_str());
158 /* If a link is failed, detect it immediately */
159 if (surf_action_->get_state() == resource::Action::State::FAILED) {
160 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", from_->get_cname(),
162 set_state(State::LINK_FAILURE);
165 } else if ((src_actor_ != nullptr && src_actor_->is_suspended()) ||
166 (dst_actor_ != nullptr && dst_actor_->is_suspended())) {
167 /* If any of the actor is suspended, create the synchro but stop its execution,
168 it will be restarted when the sender actor resume */
169 if (src_actor_->is_suspended())
170 XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
172 src_actor_->get_cname(), src_actor_->get_host()->get_cname());
174 XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
176 dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
178 surf_action_->suspend();
185 std::vector<s4u::Link*> CommImpl::get_traversed_links() const
187 xbt_assert(get_state() != State::WAITING, "You cannot use %s() if your communication is not ready (%s)", __FUNCTION__,
189 std::vector<s4u::Link*> vlinks;
190 XBT_ATTRIB_UNUSED double res = 0;
191 from_->route_to(to_, vlinks, &res);
195 /** @brief Copy the communication data from the sender's buffer to the receiver's one */
196 void CommImpl::copy_data()
198 size_t buff_size = src_buff_size_;
199 /* If there is no data to copy then return */
200 if (not src_buff_ || not dst_buff_ || copied_)
203 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
204 src_actor_ ? src_actor_->get_host()->get_cname() : "a finished actor", src_buff_,
205 dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished actor", dst_buff_, buff_size);
207 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
208 if (dst_buff_size_) {
209 buff_size = std::min(buff_size, *dst_buff_size_);
211 /* Update the receiver's buffer size to the copied amount */
212 *dst_buff_size_ = buff_size;
217 copy_data_fun(this, src_buff_, buff_size);
219 copy_data_callback_(this, src_buff_, buff_size);
222 /* Set the copied flag so we copy data only once */
223 /* (this function might be called from both communication ends) */
227 ActivityImplPtr CommImpl::isend(actor::CommIsendSimcall* observer)
229 auto* mbox = observer->get_mailbox();
230 XBT_DEBUG("send from mailbox %p", mbox);
232 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
233 CommImplPtr this_comm(new CommImpl());
234 this_comm->set_type(CommImplType::SEND);
236 /* Look for communication synchro matching our needs. We also provide a description of
237 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
239 * If it is not found then push our communication into the rendez-vous point */
240 CommImplPtr other_comm =
241 mbox->find_matching_comm(CommImplType::RECEIVE, observer->get_match_fun(), observer->get_payload(), this_comm,
242 /*done*/ false, /*remove_matching*/ true);
244 if (not other_comm) {
245 other_comm = std::move(this_comm);
247 if (mbox->is_permanent()) {
248 // this mailbox is for small messages, which have to be sent right now
249 other_comm->set_state(State::READY);
250 other_comm->dst_actor_ = mbox->get_permanent_receiver().get();
251 mbox->push_done(other_comm);
252 XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
255 mbox->push(other_comm);
258 XBT_DEBUG("Receive already pushed");
260 other_comm->set_state(State::READY);
262 observer->set_comm(other_comm.get());
264 if (observer->is_detached()) {
265 other_comm->detach();
266 other_comm->clean_fun = observer->get_clean_fun();
268 other_comm->clean_fun = nullptr;
269 observer->get_issuer()->activities_.emplace_back(other_comm);
272 /* Setup the communication synchro */
273 other_comm->src_actor_ = observer->get_issuer();
274 other_comm->src_data_ = observer->get_payload();
276 .set_src_buff(observer->get_src_buff(), observer->get_src_buff_size())
277 .set_size(observer->get_payload_size())
278 .set_rate(observer->get_rate());
280 other_comm->match_fun = observer->get_match_fun();
281 other_comm->copy_data_fun = observer->get_copy_data_fun();
283 if (MC_is_active() || MC_record_replay_is_active())
284 other_comm->set_state(simgrid::kernel::activity::State::RUNNING);
288 return (observer->is_detached() ? nullptr : other_comm);
291 ActivityImplPtr CommImpl::irecv(actor::CommIrecvSimcall* observer)
293 CommImplPtr this_synchro(new CommImpl());
294 this_synchro->set_type(CommImplType::RECEIVE);
296 auto* mbox = observer->get_mailbox();
297 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
299 CommImplPtr other_comm;
300 // communication already done, get it inside the list of completed comms
301 if (mbox->is_permanent() && mbox->has_some_done_comm()) {
302 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
303 // find a match in the list of already received comms
304 other_comm = mbox->find_matching_comm(CommImplType::SEND, observer->get_match_fun(), observer->get_payload(),
305 this_synchro, /*done*/ true, /*remove_matching*/ true);
306 // if not found, assume the receiver came first, register it to the mailbox in the classical way
307 if (not other_comm) {
308 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
310 other_comm = std::move(this_synchro);
311 mbox->push(other_comm);
313 if (other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
314 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
315 other_comm->set_state(State::DONE);
316 other_comm->set_mailbox(nullptr);
320 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
322 /* Look for communication activity matching our needs. We also provide a description of
323 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
325 * If it is not found then push our communication into the rendez-vous point */
326 other_comm = mbox->find_matching_comm(CommImplType::SEND, observer->get_match_fun(), observer->get_payload(),
327 this_synchro, /*done*/ false, /*remove_matching*/ true);
329 if (other_comm == nullptr) {
330 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->size());
331 other_comm = std::move(this_synchro);
332 mbox->push(other_comm);
334 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
336 other_comm->set_state(simgrid::kernel::activity::State::READY);
338 observer->get_issuer()->activities_.emplace_back(other_comm);
340 observer->set_comm(other_comm.get());
342 /* Setup communication synchro */
343 other_comm->dst_actor_ = observer->get_issuer();
344 other_comm->dst_data_ = observer->get_payload();
345 other_comm->set_dst_buff(observer->get_dst_buff(), observer->get_dst_buff_size());
347 if (observer->get_rate() > -1.0 && (other_comm->get_rate() < 0.0 || observer->get_rate() < other_comm->get_rate()))
348 other_comm->set_rate(observer->get_rate());
350 other_comm->match_fun = observer->get_match_fun();
351 other_comm->copy_data_fun = observer->get_copy_data_fun();
353 if (MC_is_active() || MC_record_replay_is_active()) {
354 other_comm->set_state(State::RUNNING);
362 bool CommImpl::test(actor::ActorImpl* issuer)
364 if ((MC_is_active() || MC_record_replay_is_active()) && src_actor_ && dst_actor_)
365 set_state(State::DONE);
366 return ActivityImpl::test(issuer);
369 void CommImpl::wait_for(actor::ActorImpl* issuer, double timeout)
371 XBT_DEBUG("CommImpl::wait_for(%g), %p, state %s", timeout, this, get_state_str());
373 /* Associate this simcall to the wait synchro */
374 register_simcall(&issuer->simcall_);
375 if (MC_is_active() || MC_record_replay_is_active()) {
376 // FIXME: what about timeouts?
377 set_state(State::DONE);
381 ActivityImpl::wait_for(issuer, timeout);
384 void CommImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout)
386 std::vector<ActivityImpl*> activities(comms.begin(), comms.end());
387 ActivityImpl::wait_any_for(issuer, activities, timeout);
390 void CommImpl::suspend()
392 /* FIXME: shall we suspend also the timeout synchro? */
394 surf_action_->suspend();
395 /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
398 void CommImpl::resume()
400 /*FIXME: check what happen with the timeouts */
402 surf_action_->resume();
403 /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
406 void CommImpl::cancel()
408 /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
409 if (get_state() == State::WAITING) {
412 set_state(State::CANCELED);
414 } else if (not MC_is_active() /* when running the MC there are no surf actions */
415 && not MC_record_replay_is_active() && (get_state() == State::READY || get_state() == State::RUNNING)) {
416 surf_action_->cancel();
420 /** @brief This is part of the cleanup process, probably an internal command */
421 void CommImpl::cleanup_surf()
426 src_timeout_->unref();
427 src_timeout_ = nullptr;
431 dst_timeout_->unref();
432 dst_timeout_ = nullptr;
436 void CommImpl::post()
438 on_completion(*this);
440 /* Update synchro state */
441 if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
442 set_state(State::SRC_TIMEOUT);
443 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
444 set_state(State::DST_TIMEOUT);
445 else if ((from_ && not from_->is_on()) || (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED))
446 set_state(State::SRC_HOST_FAILURE);
447 else if ((to_ && not to_->is_on()) || (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED))
448 set_state(State::DST_HOST_FAILURE);
449 else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
450 set_state(State::LINK_FAILURE);
451 } else if (get_state() == State::RUNNING) {
452 xbt_assert(from_ && from_->is_on());
453 xbt_assert(to_ && to_->is_on());
454 set_state(State::DONE);
457 XBT_DEBUG("CommImpl::post(): comm %p, state %s, src_proc %p, dst_proc %p, detached: %d", this, get_state_str(),
458 src_actor_.get(), dst_actor_.get(), detached_);
460 /* destroy the surf actions associated with the Simix communication */
463 /* Answer all simcalls associated with the synchro */
466 void CommImpl::set_exception(actor::ActorImpl* issuer)
468 switch (get_state()) {
470 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
472 case State::SRC_TIMEOUT:
474 std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
477 case State::DST_TIMEOUT:
479 std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
482 case State::SRC_HOST_FAILURE:
483 if (issuer == src_actor_)
484 issuer->context_->set_wannadie();
486 set_state(State::FAILED);
487 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
491 case State::DST_HOST_FAILURE:
492 if (issuer == dst_actor_)
493 issuer->context_->set_wannadie();
495 set_state(State::FAILED);
496 issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
500 case State::LINK_FAILURE:
501 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
503 this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
504 dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, issuer->get_cname(), issuer, detached_);
505 if (src_actor_ == issuer) {
506 XBT_DEBUG("I'm source");
507 } else if (dst_actor_ == issuer) {
508 XBT_DEBUG("I'm dest");
510 XBT_DEBUG("I'm neither source nor dest");
512 set_state(State::FAILED);
513 issuer->throw_exception(std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Link failure")));
516 case State::CANCELED:
517 if (issuer == dst_actor_)
519 std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
522 std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
526 xbt_assert(get_state() == State::DONE, "Internal error in CommImpl::finish(): unexpected synchro state %s",
531 void CommImpl::finish()
533 XBT_DEBUG("CommImpl::finish() in state %s", get_state_str());
534 /* If the synchro is still in a rendez-vous point then remove from it */
538 if (get_state() == State::DONE)
541 while (not simcalls_.empty()) {
542 smx_simcall_t simcall = simcalls_.front();
543 simcalls_.pop_front();
545 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
546 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
549 if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
550 continue; // if actor handling comm is killed
552 handle_activity_waitany(simcall);
554 /* Check out for errors */
556 if (not simcall->issuer_->get_host()->is_on()) {
557 simcall->issuer_->context_->set_wannadie();
559 // Do not answer to dying actors
560 if (not simcall->issuer_->context_->wannadie()) {
561 set_exception(simcall->issuer_);
562 simcall->issuer_->simcall_answer();
566 simcall->issuer_->waiting_synchro_ = nullptr;
567 simcall->issuer_->activities_.remove(this);
569 if (simcall->issuer_ != dst_actor_ && dst_actor_ != nullptr)
570 dst_actor_->activities_.remove(this);
571 if (simcall->issuer_ != src_actor_ && src_actor_ != nullptr)
572 src_actor_->activities_.remove(this);
577 } // namespace activity
578 } // namespace kernel
579 } // namespace simgrid