Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Might be interesting to have start and finish time for Comm, Exec, and Io activities
[simgrid.git] / src / kernel / activity / CommImpl.cpp
1 /* Copyright (c) 2007-2021. 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/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/modelchecker.h>
9 #include <simgrid/s4u/Host.hpp>
10
11 #include "src/kernel/activity/CommImpl.hpp"
12 #include "src/kernel/activity/MailboxImpl.hpp"
13 #include "src/kernel/context/Context.hpp"
14 #include "src/kernel/resource/CpuImpl.hpp"
15 #include "src/mc/mc_replay.hpp"
16 #include "src/surf/network_interface.hpp"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization");
19
20 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox, double task_size,
21                                            double rate, unsigned char* src_buff, size_t src_buff_size,
22                                            bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
23                                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
24                                            void* data, double timeout)
25 {
26   simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_isend(
27       simcall, src, mbox, task_size, rate, src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, false);
28   simcall->mc_value_ = 0;
29   comm->wait_for(simcall->issuer_, timeout);
30 }
31
32 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr simcall_HANDLER_comm_isend(
33     smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate,
34     unsigned char* src_buff, size_t src_buff_size,
35     bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
36     void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
37     void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), // used to copy data if not default one
38     void* data, bool detached)
39 {
40   XBT_DEBUG("send from mailbox %p", mbox);
41
42   /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
43   simgrid::kernel::activity::CommImplPtr this_comm(
44       new simgrid::kernel::activity::CommImpl(simgrid::kernel::activity::CommImpl::Type::SEND));
45
46   /* Look for communication synchro matching our needs. We also provide a description of
47    * ourself so that the other side also gets a chance of choosing if it wants to match with us.
48    *
49    * If it is not found then push our communication into the rendez-vous point */
50   simgrid::kernel::activity::CommImplPtr other_comm =
51       mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::RECEIVE, match_fun, data, this_comm,
52                                /*done*/ false, /*remove_matching*/ true);
53
54   if (not other_comm) {
55     other_comm = std::move(this_comm);
56
57     if (mbox->is_permanent()) {
58       // this mailbox is for small messages, which have to be sent right now
59       other_comm->state_     = simgrid::kernel::activity::State::READY;
60       other_comm->dst_actor_ = mbox->get_permanent_receiver().get();
61       mbox->push_done(other_comm);
62       XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
63
64     } else {
65       mbox->push(other_comm);
66     }
67   } else {
68     XBT_DEBUG("Receive already pushed");
69
70     other_comm->state_ = simgrid::kernel::activity::State::READY;
71   }
72
73   if (detached) {
74     other_comm->detach();
75     other_comm->clean_fun = clean_fun;
76   } else {
77     other_comm->clean_fun = nullptr;
78     src_proc->activities_.emplace_back(other_comm);
79   }
80
81   /* Setup the communication synchro */
82   other_comm->src_actor_ = src_proc;
83   other_comm->src_data_  = data;
84   (*other_comm).set_src_buff(src_buff, src_buff_size).set_size(task_size).set_rate(rate);
85
86   other_comm->match_fun     = match_fun;
87   other_comm->copy_data_fun = copy_data_fun;
88
89   if (MC_is_active() || MC_record_replay_is_active())
90     other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
91   else
92     other_comm->start();
93
94   return (detached ? nullptr : other_comm);
95 }
96
97 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
98                                            unsigned char* dst_buff, size_t* dst_buff_size,
99                                            bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
100                                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
101                                            void* data, double timeout, double rate)
102 {
103   simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_irecv(
104       simcall, receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
105   simcall->mc_value_ = 0;
106   comm->wait_for(simcall->issuer_, timeout);
107 }
108
109 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr
110 simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff,
111                            size_t* dst_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
112                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
113                            double rate)
114 {
115   simgrid::kernel::activity::CommImplPtr this_synchro(
116       new simgrid::kernel::activity::CommImpl(simgrid::kernel::activity::CommImpl::Type::RECEIVE));
117   XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
118
119   simgrid::kernel::activity::CommImplPtr other_comm;
120   // communication already done, get it inside the list of completed comms
121   if (mbox->is_permanent() && mbox->has_some_done_comm()) {
122     XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
123     // find a match in the list of already received comms
124     other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
125                                           this_synchro, /*done*/ true,
126                                           /*remove_matching*/ true);
127     // if not found, assume the receiver came first, register it to the mailbox in the classical way
128     if (not other_comm) {
129       XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
130                 "into list");
131       other_comm = std::move(this_synchro);
132       mbox->push(other_comm);
133     } else {
134       if (other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
135         XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
136         other_comm->state_ = simgrid::kernel::activity::State::DONE;
137         other_comm->set_mailbox(nullptr);
138       }
139     }
140   } else {
141     /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
142
143     /* Look for communication activity matching our needs. We also provide a description of
144      * ourself so that the other side also gets a chance of choosing if it wants to match with us.
145      *
146      * If it is not found then push our communication into the rendez-vous point */
147     other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
148                                           this_synchro, /*done*/ false,
149                                           /*remove_matching*/ true);
150
151     if (other_comm == nullptr) {
152       XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->size());
153       other_comm = std::move(this_synchro);
154       mbox->push(other_comm);
155     } else {
156       XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
157
158       other_comm->state_ = simgrid::kernel::activity::State::READY;
159     }
160     receiver->activities_.emplace_back(other_comm);
161   }
162
163   /* Setup communication synchro */
164   other_comm->dst_actor_ = receiver;
165   other_comm->dst_data_  = data;
166   other_comm->set_dst_buff(dst_buff, dst_buff_size);
167
168   if (rate > -1.0 && (other_comm->get_rate() < 0.0 || rate < other_comm->get_rate()))
169     other_comm->set_rate(rate);
170
171   other_comm->match_fun     = match_fun;
172   other_comm->copy_data_fun = copy_data_fun;
173
174   if (MC_is_active() || MC_record_replay_is_active()) {
175     other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
176     return other_comm;
177   }
178   other_comm->start();
179   return other_comm;
180 }
181
182 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm, double timeout)
183 {
184   comm->wait_for(simcall->issuer_, timeout);
185 }
186
187 bool simcall_HANDLER_comm_test(smx_simcall_t, simgrid::kernel::activity::CommImpl* comm)
188 {
189   return comm->test();
190 }
191
192 ssize_t simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count)
193 {
194   std::vector<simgrid::kernel::activity::CommImpl*> comms_vec(comms, comms + count);
195   return simgrid::kernel::activity::CommImpl::test_any(simcall->issuer_, comms_vec);
196 }
197
198 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count,
199                                   double timeout)
200 {
201   std::vector<simgrid::kernel::activity::CommImpl*> comms_vec(comms, comms + count);
202   simgrid::kernel::activity::CommImpl::wait_any_for(simcall->issuer_, comms_vec, timeout);
203 }
204
205 /******************************************************************************/
206 /*                    SIMIX_comm_copy_data callbacks                       */
207 /******************************************************************************/
208 // XBT_ATTRIB_DEPRECATED_v333
209 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
210 {
211   simgrid::kernel::activity::CommImpl::set_copy_data_callback(callback);
212 }
213
214 // XBT_ATTRIB_DEPRECATED_v333
215 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
216 {
217   simgrid::s4u::Comm::copy_buffer_callback(comm, buff, buff_size);
218 }
219
220 // XBT_ATTRIB_DEPRECATED_v333
221 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
222 {
223   simgrid::s4u::Comm::copy_pointer_callback(comm, buff, buff_size);
224 }
225
226 namespace simgrid {
227 namespace kernel {
228 namespace activity {
229 xbt::signal<void(CommImpl const&)> CommImpl::on_start;
230 xbt::signal<void(CommImpl const&)> CommImpl::on_completion;
231
232 void (*CommImpl::copy_data_callback_)(CommImpl*, void*, size_t) = &s4u::Comm::copy_pointer_callback;
233
234 void CommImpl::set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t))
235 {
236   copy_data_callback_ = callback;
237 }
238
239 CommImpl& CommImpl::set_size(double size)
240 {
241   size_ = size;
242   return *this;
243 }
244
245 CommImpl& CommImpl::set_rate(double rate)
246 {
247   rate_ = rate;
248   return *this;
249 }
250 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
251 {
252   mbox_ = mbox;
253   return *this;
254 }
255
256 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
257 {
258   src_buff_      = buff;
259   src_buff_size_ = size;
260   return *this;
261 }
262
263 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
264 {
265   dst_buff_      = buff;
266   dst_buff_size_ = size;
267   return *this;
268 }
269
270 CommImpl& CommImpl::detach()
271 {
272   detached_ = true;
273   return *this;
274 }
275
276 CommImpl::CommImpl(s4u::Host* from, s4u::Host* to, double bytes) : size_(bytes), detached_(true), from_(from), to_(to)
277 {
278   state_ = State::READY;
279 }
280
281 CommImpl::~CommImpl()
282 {
283   XBT_DEBUG("Really free communication %p in state %s (detached = %d)", this, get_state_str(), detached_);
284
285   cleanup_surf();
286
287   if (detached_ && state_ != State::DONE) {
288     /* the communication has failed and was detached:
289      * we have to free the buffer */
290     if (clean_fun)
291       clean_fun(src_buff_);
292     src_buff_ = nullptr;
293   } else if (mbox_) {
294     mbox_->remove(this);
295   }
296 }
297
298 /**  @brief Starts the simulation of a communication synchro. */
299 CommImpl* CommImpl::start()
300 {
301   /* If both the sender and the receiver are already there, start the communication */
302   if (state_ == State::READY) {
303     from_ = from_ != nullptr ? from_ : src_actor_->get_host();
304     to_   = to_ != nullptr ? to_ : dst_actor_->get_host();
305     /* Getting the network_model from the origin host
306      * Valid while we have a single network model, otherwise we would need to change this function to first get the
307      * routes and later create the respective surf actions */
308     auto net_model = from_->get_netpoint()->get_englobing_zone()->get_network_model();
309
310     surf_action_ = net_model->communicate(from_, to_, size_, rate_);
311     surf_action_->set_activity(this);
312     surf_action_->set_category(get_tracing_category());
313     start_time_ = surf_action_->get_start_time();
314     state_ = State::RUNNING;
315     on_start(*this);
316
317     XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p; state: %s)", this, from_->get_cname(),
318               to_->get_cname(), surf_action_, get_state_str());
319
320     /* If a link is failed, detect it immediately */
321     if (surf_action_->get_state() == resource::Action::State::FAILED) {
322       XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", from_->get_cname(),
323                 to_->get_cname());
324       state_ = State::LINK_FAILURE;
325       post();
326
327     } else if ((src_actor_ != nullptr && src_actor_->is_suspended()) ||
328                (dst_actor_ != nullptr && dst_actor_->is_suspended())) {
329       /* If any of the actor is suspended, create the synchro but stop its execution,
330          it will be restarted when the sender actor resume */
331       if (src_actor_->is_suspended())
332         XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
333                   "communication",
334                   src_actor_->get_cname(), src_actor_->get_host()->get_cname());
335       else
336         XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
337                   "communication",
338                   dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
339
340       surf_action_->suspend();
341     }
342   }
343
344   return this;
345 }
346
347 std::vector<s4u::Link*> CommImpl::get_traversed_links() const
348 {
349   xbt_assert(state_ != State::WAITING, "You cannot use %s() if your communication is not ready (%s)", __FUNCTION__,
350              get_state_str());
351   std::vector<s4u::Link*> vlinks;
352   XBT_ATTRIB_UNUSED double res = 0;
353   from_->route_to(to_, vlinks, &res);
354   return vlinks;
355 }
356
357 /** @brief Copy the communication data from the sender's buffer to the receiver's one  */
358 void CommImpl::copy_data()
359 {
360   size_t buff_size = src_buff_size_;
361   /* If there is no data to copy then return */
362   if (not src_buff_ || not dst_buff_ || copied_)
363     return;
364
365   XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
366             src_actor_ ? src_actor_->get_host()->get_cname() : "a finished actor", src_buff_,
367             dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished actor", dst_buff_, buff_size);
368
369   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
370   if (dst_buff_size_) {
371     buff_size = std::min(buff_size, *dst_buff_size_);
372
373     /* Update the receiver's buffer size to the copied amount */
374     *dst_buff_size_ = buff_size;
375   }
376
377   if (buff_size > 0) {
378     if (copy_data_fun)
379       copy_data_fun(this, src_buff_, buff_size);
380     else
381       copy_data_callback_(this, src_buff_, buff_size);
382   }
383
384   /* Set the copied flag so we copy data only once */
385   /* (this function might be called from both communication ends) */
386   copied_ = true;
387 }
388
389 bool CommImpl::test()
390 {
391   if ((MC_is_active() || MC_record_replay_is_active()) && src_actor_ && dst_actor_)
392     state_ = State::DONE;
393   return ActivityImpl::test();
394 }
395
396 void CommImpl::wait_for(actor::ActorImpl* issuer, double timeout)
397 {
398   XBT_DEBUG("CommImpl::wait_for(%g), %p, state %s", timeout, this, to_c_str(state_));
399
400   /* Associate this simcall to the wait synchro */
401   register_simcall(&issuer->simcall_);
402
403   if (MC_is_active() || MC_record_replay_is_active()) {
404     int idx = issuer->simcall_.mc_value_;
405     if (idx == 0) {
406       state_ = State::DONE;
407     } else {
408       /* If we reached this point, the wait simcall must have a timeout */
409       /* Otherwise it shouldn't be enabled and executed by the MC */
410       if (timeout < 0.0)
411         THROW_IMPOSSIBLE;
412       state_ = (issuer == src_actor_ ? State::SRC_TIMEOUT : State::DST_TIMEOUT);
413     }
414     finish();
415     return;
416   }
417
418   /* If the synchro has already finish perform the error handling, */
419   /* otherwise set up a waiting timeout on the right side          */
420   if (state_ != State::WAITING && state_ != State::RUNNING) {
421     finish();
422   } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
423     resource::Action* sleep = issuer->get_host()->get_cpu()->sleep(timeout);
424     sleep->set_activity(this);
425
426     if (issuer == src_actor_)
427       src_timeout_ = sleep;
428     else
429       dst_timeout_ = sleep;
430   }
431 }
432
433 ssize_t CommImpl::test_any(const actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms)
434 {
435   if (MC_is_active() || MC_record_replay_is_active()) {
436     int idx = issuer->simcall_.mc_value_;
437     xbt_assert(idx == -1 || comms[idx]->test());
438     return idx;
439   }
440
441   for (std::size_t i = 0; i < comms.size(); ++i) {
442     if (comms[i]->test())
443       return i;
444   }
445   return -1;
446 }
447
448 void CommImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout)
449 {
450   if (MC_is_active() || MC_record_replay_is_active()) {
451     xbt_assert(timeout <= 0.0, "Timeout not implemented for waitany in the model-checker");
452     int idx    = issuer->simcall_.mc_value_;
453     auto* comm = comms[idx];
454     comm->simcalls_.push_back(&issuer->simcall_);
455     simcall_comm_waitany__set__result(&issuer->simcall_, idx);
456     comm->state_ = State::DONE;
457     comm->finish();
458     return;
459   }
460
461   if (timeout < 0.0) {
462     issuer->simcall_.timeout_cb_ = nullptr;
463   } else {
464     issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, comms]() {
465       // FIXME: Vector `comms' is copied here. Use a reference once its lifetime is extended (i.e. when the simcall is
466       // modernized).
467       issuer->simcall_.timeout_cb_ = nullptr;
468       for (auto* comm : comms)
469         comm->unregister_simcall(&issuer->simcall_);
470       simcall_comm_waitany__set__result(&issuer->simcall_, -1);
471       issuer->simcall_answer();
472     });
473   }
474
475   for (auto* comm : comms) {
476     /* associate this simcall to the the synchro */
477     comm->simcalls_.push_back(&issuer->simcall_);
478
479     /* see if the synchro is already finished */
480     if (comm->state_ != State::WAITING && comm->state_ != State::RUNNING) {
481       comm->finish();
482       break;
483     }
484   }
485 }
486
487 void CommImpl::suspend()
488 {
489   /* FIXME: shall we suspend also the timeout synchro? */
490   if (surf_action_)
491     surf_action_->suspend();
492   /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
493 }
494
495 void CommImpl::resume()
496 {
497   /*FIXME: check what happen with the timeouts */
498   if (surf_action_)
499     surf_action_->resume();
500   /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
501 }
502
503 void CommImpl::cancel()
504 {
505   /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
506   if (state_ == State::WAITING) {
507     if (not detached_) {
508       mbox_->remove(this);
509       state_ = State::CANCELED;
510     }
511   } else if (not MC_is_active() /* when running the MC there are no surf actions */
512              && not MC_record_replay_is_active() && (state_ == State::READY || state_ == State::RUNNING)) {
513     surf_action_->cancel();
514   }
515 }
516
517 /** @brief This is part of the cleanup process, probably an internal command */
518 void CommImpl::cleanup_surf()
519 {
520   clean_action();
521
522   if (src_timeout_) {
523     src_timeout_->unref();
524     src_timeout_ = nullptr;
525   }
526
527   if (dst_timeout_) {
528     dst_timeout_->unref();
529     dst_timeout_ = nullptr;
530   }
531 }
532
533 void CommImpl::post()
534 {
535   on_completion(*this);
536
537   /* Update synchro state */
538   if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
539     state_ = State::SRC_TIMEOUT;
540   else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
541     state_ = State::DST_TIMEOUT;
542   else if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED)
543     state_ = State::SRC_HOST_FAILURE;
544   else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED)
545     state_ = State::DST_HOST_FAILURE;
546   else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
547     state_ = State::LINK_FAILURE;
548   } else
549     state_ = State::DONE;
550
551   XBT_DEBUG("CommImpl::post(): comm %p, state %s, src_proc %p, dst_proc %p, detached: %d", this, get_state_str(),
552             src_actor_.get(), dst_actor_.get(), detached_);
553
554   /* destroy the surf actions associated with the Simix communication */
555   cleanup_surf();
556
557   /* Answer all simcalls associated with the synchro */
558   finish();
559 }
560 void CommImpl::set_exception(actor::ActorImpl* issuer)
561 {
562   switch (state_) {
563     case State::FAILED:
564       issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
565       break;
566     case State::SRC_TIMEOUT:
567       issuer->exception_ =
568           std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
569       break;
570
571     case State::DST_TIMEOUT:
572       issuer->exception_ =
573           std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
574       break;
575
576     case State::SRC_HOST_FAILURE:
577       if (issuer == src_actor_)
578         issuer->context_->set_wannadie();
579       else {
580         state_             = kernel::activity::State::FAILED;
581         issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
582       }
583       break;
584
585     case State::DST_HOST_FAILURE:
586       if (issuer == dst_actor_)
587         issuer->context_->set_wannadie();
588       else {
589         state_             = kernel::activity::State::FAILED;
590         issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
591       }
592       break;
593
594     case State::LINK_FAILURE:
595       XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
596                 "detached:%d",
597                 this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
598                 dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, issuer->get_cname(), issuer, detached_);
599       if (src_actor_ == issuer) {
600         XBT_DEBUG("I'm source");
601       } else if (dst_actor_ == issuer) {
602         XBT_DEBUG("I'm dest");
603       } else {
604         XBT_DEBUG("I'm neither source nor dest");
605       }
606       state_ = kernel::activity::State::FAILED;
607       issuer->throw_exception(std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Link failure")));
608       break;
609
610     case State::CANCELED:
611       if (issuer == dst_actor_)
612         issuer->exception_ =
613             std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
614       else
615         issuer->exception_ =
616             std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
617       break;
618
619     default:
620       xbt_assert(state_ == State::DONE, "Internal error in CommImpl::finish(): unexpected synchro state %s",
621                  to_c_str(state_));
622   }
623 }
624
625 void CommImpl::finish()
626 {
627   XBT_DEBUG("CommImpl::finish() in state %s", to_c_str(state_));
628   /* If the synchro is still in a rendez-vous point then remove from it */
629   if (mbox_)
630     mbox_->remove(this);
631
632   if (state_ == State::DONE)
633     copy_data();
634
635   while (not simcalls_.empty()) {
636     smx_simcall_t simcall = simcalls_.front();
637     simcalls_.pop_front();
638
639     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
640      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
641      * simcall */
642
643     if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
644       continue;                                 // if actor handling comm is killed
645     if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
646       CommImpl** comms = simcall_comm_waitany__get__comms(simcall);
647       size_t count     = simcall_comm_waitany__get__count(simcall);
648       for (size_t i = 0; i < count; i++)
649         comms[i]->unregister_simcall(simcall);
650       if (simcall->timeout_cb_) {
651         simcall->timeout_cb_->remove();
652         simcall->timeout_cb_ = nullptr;
653       }
654       if (not MC_is_active() && not MC_record_replay_is_active()) {
655         auto element = std::find(comms, comms + count, this);
656         ssize_t rank = (element != comms + count) ? element - comms : -1;
657         simcall_comm_waitany__set__result(simcall, rank);
658       }
659     }
660
661     /* Check out for errors */
662
663     if (not simcall->issuer_->get_host()->is_on()) {
664       simcall->issuer_->context_->set_wannadie();
665     } else {
666       set_exception(simcall->issuer_);
667       simcall->issuer_->simcall_answer();
668     }
669     /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
670     if (simcall->issuer_->exception_ &&
671         (simcall->call_ == simix::Simcall::COMM_WAITANY || simcall->call_ == simix::Simcall::COMM_TESTANY)) {
672       // First retrieve the rank of our failing synchro
673       CommImpl** comms;
674       size_t count;
675       if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
676         comms = simcall_comm_waitany__get__comms(simcall);
677         count = simcall_comm_waitany__get__count(simcall);
678       } else {
679         /* simcall->call_ == simix::Simcall::COMM_TESTANY */
680         comms = simcall_comm_testany__get__comms(simcall);
681         count = simcall_comm_testany__get__count(simcall);
682       }
683       auto element = std::find(comms, comms + count, this);
684       ssize_t rank = (element != comms + count) ? element - comms : -1;
685       // In order to modify the exception we have to rethrow it:
686       try {
687         std::rethrow_exception(simcall->issuer_->exception_);
688       } catch (Exception& e) {
689         e.set_value(rank);
690       }
691     }
692
693     simcall->issuer_->waiting_synchro_ = nullptr;
694     simcall->issuer_->activities_.remove(this);
695     if (detached_) {
696       if (simcall->issuer_ != dst_actor_ && dst_actor_ != nullptr)
697         dst_actor_->activities_.remove(this);
698       if (simcall->issuer_ != src_actor_ && src_actor_ != nullptr)
699         src_actor_->activities_.remove(this);
700     }
701   }
702 }
703
704 } // namespace activity
705 } // namespace kernel
706 } // namespace simgrid