Logo AND Algorithmique Numérique Distribuée

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