Logo AND Algorithmique Numérique Distribuée

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