Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
changing the way the tracing category is passed to ExecImpl
[simgrid.git] / src / msg / msg_gos.cpp
1 /* Copyright (c) 2004-2018. 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 <xbt/ex.hpp>
7
8 #include "simgrid/s4u/Mailbox.hpp"
9 #include "src/instr/instr_private.hpp"
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/msg/msg_private.hpp"
12 #include "src/simix/smx_private.hpp" /* MSG_task_listen looks inside the rdv directly. Not clean. */
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
15
16 /** @ingroup msg_task_usage
17  * @brief Executes a task and waits for its termination.
18  *
19  * This function is used for describing the behavior of a process. It takes only one parameter.
20  * @param task a #msg_task_t to execute on the location on which the process is running.
21  * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
22  */
23 msg_error_t MSG_task_execute(msg_task_t task)
24 {
25   return MSG_parallel_task_execute(task);
26 }
27
28 /** @ingroup msg_task_usage
29  * @brief Executes a parallel task and waits for its termination.
30  *
31  * @param task a #msg_task_t to execute on the location on which the process is running.
32  *
33  * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
34  * or #MSG_HOST_FAILURE otherwise
35  */
36 msg_error_t MSG_parallel_task_execute(msg_task_t task)
37 {
38   return MSG_parallel_task_execute_with_timeout(task, -1);
39 }
40
41 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
42 {
43   simdata_task_t simdata = task->simdata;
44   e_smx_state_t comp_state;
45   msg_error_t status = MSG_OK;
46
47   TRACE_msg_task_execute_start(task);
48
49   xbt_assert((not simdata->compute) && not task->simdata->isused,
50              "This task is executed somewhere else. Go fix your code!");
51
52   XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
53
54   if (simdata->flops_amount <= 0.0 && not simdata->host_nb) {
55     TRACE_msg_task_execute_end(task);
56     return MSG_OK;
57   }
58
59   try {
60     simdata->setUsed();
61
62     if (simdata->host_nb > 0) {
63       simdata->compute =
64           boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
65               task->name ?: "", simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
66               simdata->bytes_parallel_amount, -1.0, timeout));
67       XBT_DEBUG("Parallel execution action created: %p", simdata->compute.get());
68       if (task->category != nullptr)
69         simcall_set_category(simdata->compute, task->category);
70     } else {
71       simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
72           simcall_execution_start(task->name ?: "", task->category ?: "", simdata->flops_amount, simdata->priority,
73                                   simdata->bound, MSG_process_get_host(MSG_process_self())));
74     }
75
76     comp_state = simcall_execution_wait(simdata->compute);
77
78     simdata->setNotUsed();
79
80     XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
81   }
82   catch (xbt_ex& e) {
83     switch (e.category) {
84     case cancel_error:
85       status = MSG_TASK_CANCELED;
86       break;
87     case host_error:
88       status = MSG_HOST_FAILURE;
89       break;
90     case timeout_error:
91       status = MSG_TIMEOUT;
92       break;
93     default:
94       throw;
95     }
96   }
97
98   /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
99   simdata->flops_amount = 0.0;
100   simdata->comm = nullptr;
101   simdata->compute = nullptr;
102   TRACE_msg_task_execute_end(task);
103
104   return status;
105 }
106
107 /** @ingroup msg_task_usage
108  * @brief Sleep for the specified number of seconds
109  *
110  * Makes the current process sleep until @a time seconds have elapsed.
111  *
112  * @param nb_sec a number of second
113  */
114 msg_error_t MSG_process_sleep(double nb_sec)
115 {
116   msg_error_t status = MSG_OK;
117
118   try {
119     simgrid::s4u::this_actor::sleep_for(nb_sec);
120   }
121   catch(xbt_ex& e) {
122     if (e.category == cancel_error) {
123       XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
124       // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
125       // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
126       // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
127       // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
128       // and did not change anythings at the C level.
129       // See comment in the jmsg_process.c file, function JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
130       status = MSG_TASK_CANCELED;
131     } else
132       throw;
133   }
134
135   return status;
136 }
137
138 /** @ingroup msg_task_usage
139  * @brief Receives a task from a mailbox.
140  *
141  * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
142  * for receiving tasks asynchronously.
143  *
144  * @param task a memory location for storing a #msg_task_t.
145  * @param alias name of the mailbox to receive the task from
146  *
147  * @return Returns
148  * #MSG_OK if the task was successfully received,
149  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
150  */
151 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
152 {
153   return MSG_task_receive_with_timeout(task, alias, -1);
154 }
155
156 /** @ingroup msg_task_usage
157  * @brief Receives a task from a mailbox at a given rate.
158  *
159  * @param task a memory location for storing a #msg_task_t.
160  * @param alias name of the mailbox to receive the task from
161  * @param rate limit the reception to rate bandwidth (byte/sec)
162  *
163  * The rate parameter can be used to receive a task with a limited
164  * bandwidth (smaller than the physical available value). Use
165  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
166  * rate value do disable this feature).
167  *
168  * @return Returns
169  * #MSG_OK if the task was successfully received,
170  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
171  */
172 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
173 {
174   return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
175 }
176
177 /** @ingroup msg_task_usage
178  * @brief Receives a task from a mailbox with a given timeout.
179  *
180  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
181  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously.  You can provide a -1 timeout
182  * to obtain an infinite timeout.
183  *
184  * @param task a memory location for storing a #msg_task_t.
185  * @param alias name of the mailbox to receive the task from
186  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
187  *
188  * @return Returns
189  * #MSG_OK if the task was successfully received,
190  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
191  */
192 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
193 {
194   return MSG_task_receive_ext(task, alias, timeout, nullptr);
195 }
196
197 /** @ingroup msg_task_usage
198  * @brief Receives a task from a mailbox with a given timeout and at a given rate.
199  *
200  * @param task a memory location for storing a #msg_task_t.
201  * @param alias name of the mailbox to receive the task from
202  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
203  * @param rate limit the reception to rate bandwidth (byte/sec)
204  *
205  * The rate parameter can be used to send a task with a limited
206  * bandwidth (smaller than the physical available value). Use
207  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
208  * rate value do disable this feature).
209  *
210  * @return Returns
211  * #MSG_OK if the task was successfully received,
212  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
213  */
214 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
215 {
216   return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
217 }
218
219 /** @ingroup msg_task_usage
220  * @brief Receives a task from a mailbox from a specific host with a given timeout.
221  *
222  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
223  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
224  * to obtain an infinite timeout.
225  *
226  * @param task a memory location for storing a #msg_task_t.
227  * @param alias name of the mailbox to receive the task from
228  * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
229  * @param host a #msg_host_t host from where the task was sent
230  *
231  * @return Returns
232  * #MSG_OK if the task was successfully received,
233  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
234  */
235 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
236 {
237   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
238   return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
239 }
240
241 /** @ingroup msg_task_usage
242  * @brief Receives a task from a mailbox from a specific host with a given timeout  and at a given rate.
243  *
244  * @param task a memory location for storing a #msg_task_t.
245  * @param alias name of the mailbox to receive the task from
246  * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
247  * @param host a #msg_host_t host from where the task was sent
248  * @param rate limit the reception to rate bandwidth (byte/sec)
249  *
250  * The rate parameter can be used to receive a task with a limited
251  * bandwidth (smaller than the physical available value). Use
252  * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
253  * rate value do disable this feature).
254  *
255  * @return Returns
256  * #MSG_OK if the task was successfully received,
257  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
258  */
259 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
260                                          double rate)
261 {
262   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
263   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
264   msg_error_t ret = MSG_OK;
265   /* We no longer support getting a task from a specific host */
266   if (host)
267     THROW_UNIMPLEMENTED;
268
269   TRACE_msg_task_get_start();
270
271   /* Sanity check */
272   xbt_assert(task, "Null pointer for the task storage");
273
274   if (*task)
275     XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
276
277   /* Try to receive it by calling SIMIX network layer */
278   try {
279     simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
280                       timeout, rate);
281     XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
282     (*task)->simdata->setNotUsed();
283   }
284   catch (xbt_ex& e) {
285     switch (e.category) {
286     case host_error:
287     case cancel_error:
288       ret = MSG_HOST_FAILURE;
289       break;
290     case network_error:
291       ret = MSG_TRANSFER_FAILURE;
292       break;
293     case timeout_error:
294       ret = MSG_TIMEOUT;
295       break;
296     default:
297       throw;
298     }
299   }
300
301   if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
302     TRACE_msg_task_get_end(*task);
303   }
304   return ret;
305 }
306
307 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
308 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
309                                                  void_f_pvoid_t cleanup, int detached)
310 {
311   simdata_task_t t_simdata = nullptr;
312   msg_process_t myself = MSG_process_self();
313   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
314   TRACE_msg_task_put_start(task);
315
316   /* Prepare the task to send */
317   t_simdata = task->simdata;
318   t_simdata->sender = myself;
319   t_simdata->source = MSG_host_self();
320   t_simdata->setUsed();
321   t_simdata->comm = nullptr;
322   msg_global->sent_msg++;
323
324   /* Send it by calling SIMIX network layer */
325   smx_activity_t act =
326       simcall_comm_isend(myself->get_impl(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
327                          sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
328   t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
329
330   msg_comm_t comm = nullptr;
331   if (not detached) {
332     comm = new simgrid::msg::Comm(task, nullptr, act);
333   }
334
335   if (TRACE_is_enabled())
336     simcall_set_category(act, task->category);
337   TRACE_msg_task_put_end();
338
339   return comm;
340 }
341
342 /** @ingroup msg_task_usage
343  * @brief Sends a task on a mailbox.
344  *
345  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
346  *
347  * @param task a #msg_task_t to send on another location.
348  * @param alias name of the mailbox to sent the task to
349  * @return the msg_comm_t communication created
350  */
351 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
352 {
353   return MSG_task_isend_internal(task, alias, nullptr, 0);
354 }
355
356 /** @ingroup msg_task_usage
357  * @brief Sends a task on a mailbox with a maximum rate
358  *
359  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
360  * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
361  *
362  * @param task a #msg_task_t to send on another location.
363  * @param alias name of the mailbox to sent the task to
364  * @param maxrate the maximum communication rate for sending this task (byte/sec).
365  * @return the msg_comm_t communication created
366  */
367 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
368 {
369   task->simdata->rate = maxrate;
370   return MSG_task_isend_internal(task, alias, nullptr, 0);
371 }
372
373 /** @ingroup msg_task_usage
374  * @brief Sends a task on a mailbox.
375  *
376  * This is a non blocking detached send function.
377  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
378  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
379  * usual. More details on this can be obtained on
380  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
381  * in the SimGrid-user mailing list archive.
382  *
383  * @param task a #msg_task_t to send on another location.
384  * @param alias name of the mailbox to sent the task to
385  * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
386  * (if nullptr, no function will be called)
387  */
388 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
389 {
390   msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
391   xbt_assert(comm == nullptr);
392 }
393
394 /** @ingroup msg_task_usage
395  * @brief Sends a task on a mailbox with a maximal rate.
396  *
397  * This is a non blocking detached send function.
398  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
399  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
400  * usual. More details on this can be obtained on
401  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
402  * in the SimGrid-user mailing list archive.
403  *
404  * The rate parameter can be used to send a task with a limited
405  * bandwidth (smaller than the physical available value). Use
406  * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
407  * value do disable this feature).
408  *
409  * @param task a #msg_task_t to send on another location.
410  * @param alias name of the mailbox to sent the task to
411  * @param cleanup a function to destroy the task if the
412  * communication fails, e.g. MSG_task_destroy
413  * (if nullptr, no function will be called)
414  * @param maxrate the maximum communication rate for sending this task (byte/sec)
415  *
416  */
417 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
418 {
419   task->simdata->rate = maxrate;
420   MSG_task_dsend(task, alias, cleanup);
421 }
422
423 /** @ingroup msg_task_usage
424  * @brief Starts listening for receiving a task from an asynchronous communication.
425  *
426  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
427  *
428  * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
429  * @param name of the mailbox to receive the task on
430  * @return the msg_comm_t communication created
431  */
432 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
433 {
434   return MSG_task_irecv_bounded(task, name, -1.0);
435 }
436
437 /** @ingroup msg_task_usage
438  * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
439  *
440  * The rate parameter can be used to receive a task with a limited
441  * bandwidth (smaller than the physical available value). Use
442  * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
443  * value do disable this feature).
444  *
445  * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
446  * @param name of the mailbox to receive the task on
447  * @param rate limit the bandwidth to the given rate (byte/sec)
448  * @return the msg_comm_t communication created
449  */
450 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
451 {
452   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
453
454   /* FIXME: these functions are not traceable */
455   /* Sanity check */
456   xbt_assert(task, "Null pointer for the task storage");
457
458   if (*task)
459     XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
460
461   /* Try to receive it by calling SIMIX network layer */
462   msg_comm_t comm = new simgrid::msg::Comm(
463       nullptr, task,
464       simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr, rate));
465
466   return comm;
467 }
468
469 /** @ingroup msg_task_usage
470  * @brief Checks whether a communication is done, and if yes, finalizes it.
471  * @param comm the communication to test
472  * @return 'true' if the communication is finished
473  * (but it may have failed, use MSG_comm_get_status() to know its status)
474  * or 'false' if the communication is not finished yet
475  * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
476  */
477 int MSG_comm_test(msg_comm_t comm)
478 {
479   bool finished = false;
480
481   try {
482     finished = simcall_comm_test(comm->s_comm);
483     if (finished && comm->task_received != nullptr) {
484       /* I am the receiver */
485       (*comm->task_received)->simdata->setNotUsed();
486     }
487   }
488   catch (xbt_ex& e) {
489     switch (e.category) {
490       case network_error:
491         comm->status = MSG_TRANSFER_FAILURE;
492         finished     = true;
493         break;
494       case timeout_error:
495         comm->status = MSG_TIMEOUT;
496         finished     = true;
497         break;
498       default:
499         throw;
500     }
501   }
502
503   return finished;
504 }
505
506 /** @ingroup msg_task_usage
507  * @brief This function checks if a communication is finished.
508  * @param comms a vector of communications
509  * @return the position of the finished communication if any
510  * (but it may have failed, use MSG_comm_get_status() to know its status),
511  * or -1 if none is finished
512  */
513 int MSG_comm_testany(xbt_dynar_t comms)
514 {
515   int finished_index = -1;
516
517   /* Create the equivalent array with SIMIX objects: */
518   std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
519   s_comms.reserve(xbt_dynar_length(comms));
520   msg_comm_t comm;
521   unsigned int cursor;
522   xbt_dynar_foreach(comms, cursor, comm) {
523     s_comms.push_back(comm->s_comm);
524   }
525
526   msg_error_t status = MSG_OK;
527   try {
528     finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
529   }
530   catch (xbt_ex& e) {
531     switch (e.category) {
532       case network_error:
533         finished_index = e.value;
534         status = MSG_TRANSFER_FAILURE;
535         break;
536       case timeout_error:
537         finished_index = e.value;
538         status = MSG_TIMEOUT;
539         break;
540       default:
541         throw;
542     }
543   }
544
545   if (finished_index != -1) {
546     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
547     /* the communication is finished */
548     comm->status = status;
549
550     if (status == MSG_OK && comm->task_received != nullptr) {
551       /* I am the receiver */
552       (*comm->task_received)->simdata->setNotUsed();
553     }
554   }
555
556   return finished_index;
557 }
558
559 /** @ingroup msg_task_usage
560  * @brief Destroys a communication.
561  * @param comm the communication to destroy.
562  */
563 void MSG_comm_destroy(msg_comm_t comm)
564 {
565   delete comm;
566 }
567
568 /** @ingroup msg_task_usage
569  * @brief Wait for the completion of a communication.
570  *
571  * It takes two parameters.
572  * @param comm the communication to wait.
573  * @param timeout Wait until the communication terminates or the timeout occurs.
574  *                You can provide a -1 timeout to obtain an infinite timeout.
575  * @return msg_error_t
576  */
577 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
578 {
579   try {
580     simcall_comm_wait(comm->s_comm, timeout);
581
582     if (comm->task_received != nullptr) {
583       /* I am the receiver */
584       (*comm->task_received)->simdata->setNotUsed();
585     }
586
587     /* FIXME: these functions are not traceable */
588   }
589   catch (xbt_ex& e) {
590     switch (e.category) {
591     case network_error:
592       comm->status = MSG_TRANSFER_FAILURE;
593       break;
594     case timeout_error:
595       comm->status = MSG_TIMEOUT;
596       break;
597     default:
598       throw;
599     }
600   }
601
602   return comm->status;
603 }
604
605 /** @ingroup msg_task_usage
606  * @brief This function is called by a sender and permit to wait for each communication
607  *
608  * @param comm a vector of communication
609  * @param nb_elem is the size of the comm vector
610  * @param timeout for each call of MSG_comm_wait
611  */
612 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
613 {
614   for (int i = 0; i < nb_elem; i++)
615     MSG_comm_wait(comm[i], timeout);
616 }
617
618 /** @ingroup msg_task_usage
619  * @brief This function waits for the first communication finished in a list.
620  * @param comms a vector of communications
621  * @return the position of the first finished communication
622  * (but it may have failed, use MSG_comm_get_status() to know its status)
623  */
624 int MSG_comm_waitany(xbt_dynar_t comms)
625 {
626   int finished_index = -1;
627
628   /* create the equivalent dynar with SIMIX objects */
629   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
630     intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
631   });
632   msg_comm_t comm;
633   unsigned int cursor;
634   xbt_dynar_foreach(comms, cursor, comm) {
635     intrusive_ptr_add_ref(comm->s_comm.get());
636     xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
637   }
638
639   msg_error_t status = MSG_OK;
640   try {
641     finished_index = simcall_comm_waitany(s_comms, -1);
642   }
643   catch(xbt_ex& e) {
644     switch (e.category) {
645       case network_error:
646         finished_index = e.value;
647         status = MSG_TRANSFER_FAILURE;
648         break;
649       case timeout_error:
650         finished_index = e.value;
651         status = MSG_TIMEOUT;
652         break;
653       default:
654         throw;
655     }
656   }
657
658   xbt_assert(finished_index != -1, "WaitAny returned -1");
659   xbt_dynar_free(&s_comms);
660
661   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
662   /* the communication is finished */
663   comm->status = status;
664
665   if (comm->task_received != nullptr) {
666     /* I am the receiver */
667     (*comm->task_received)->simdata->setNotUsed();
668   }
669
670   return finished_index;
671 }
672
673 /**
674  * @ingroup msg_task_usage
675  * @brief Returns the error (if any) that occurred during a finished communication.
676  * @param comm a finished communication
677  * @return the status of the communication, or #MSG_OK if no error occurred
678  * during the communication
679  */
680 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
681
682   return comm->status;
683 }
684
685 /** @ingroup msg_task_usage
686  * @brief Get a task (#msg_task_t) from a communication
687  *
688  * @param comm the communication where to get the task
689  * @return the task from the communication
690  */
691 msg_task_t MSG_comm_get_task(msg_comm_t comm)
692 {
693   xbt_assert(comm, "Invalid parameter");
694
695   return comm->task_received ? *comm->task_received : comm->task_sent;
696 }
697
698 /**
699  * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
700  * @param synchro the comm
701  * @param buff the data copied
702  * @param buff_size size of the buffer
703  */
704 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
705 {
706   simgrid::kernel::activity::CommImplPtr comm =
707       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
708
709   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
710
711   // notify the user callback if any
712   if (msg_global->task_copy_callback) {
713     msg_task_t task = static_cast<msg_task_t>(buff);
714     msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
715   }
716 }
717
718 /** @ingroup msg_task_usage
719  * @brief Sends a task to a mailbox
720  *
721  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
722  * side if #MSG_task_receive is used).
723  * See #MSG_task_isend for sending tasks asynchronously.
724  *
725  * @param task the task to be sent
726  * @param alias the mailbox name to where the task is sent
727  *
728  * @return Returns #MSG_OK if the task was successfully sent,
729  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
730  */
731 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
732 {
733   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
734   return MSG_task_send_with_timeout(task, alias, -1);
735 }
736
737 /** @ingroup msg_task_usage
738  * @brief Sends a task to a mailbox with a maximum rate
739  *
740  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
741  * the application to limit the bandwidth utilization of network links when sending the task.
742  *
743  * The maxrate parameter can be used to send a task with a limited
744  * bandwidth (smaller than the physical available value). Use
745  * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
746  * value do disable this feature).
747  *
748  * @param task the task to be sent
749  * @param alias the mailbox name to where the task is sent
750  * @param maxrate the maximum communication rate for sending this task (byte/sec)
751  *
752  * @return Returns #MSG_OK if the task was successfully sent,
753  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
754  */
755 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
756 {
757   task->simdata->rate = maxrate;
758   return MSG_task_send(task, alias);
759 }
760
761 /** @ingroup msg_task_usage
762  * @brief Sends a task to a mailbox with a timeout
763  *
764  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
765  *
766  * @param task the task to be sent
767  * @param alias the mailbox name to where the task is sent
768  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
769  *
770  * @return Returns #MSG_OK if the task was successfully sent,
771  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
772  */
773 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
774 {
775   msg_error_t ret = MSG_OK;
776   simdata_task_t t_simdata = nullptr;
777   msg_process_t process = MSG_process_self();
778   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
779
780   TRACE_msg_task_put_start(task);
781
782   /* Prepare the task to send */
783   t_simdata = task->simdata;
784   t_simdata->sender = process;
785   t_simdata->source = MSG_host_self();
786
787   t_simdata->setUsed();
788
789   t_simdata->comm = nullptr;
790   msg_global->sent_msg++;
791
792   /* Try to send it by calling SIMIX network layer */
793   try {
794     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call  */
795     comm = simcall_comm_isend(SIMIX_process_self(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
796                               sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
797     if (TRACE_is_enabled() && task->category != nullptr)
798       simcall_set_category(comm, task->category);
799     t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
800     simcall_comm_wait(comm, timeout);
801   }
802   catch (xbt_ex& e) {
803     switch (e.category) {
804     case cancel_error:
805       ret = MSG_HOST_FAILURE;
806       break;
807     case network_error:
808       ret = MSG_TRANSFER_FAILURE;
809       break;
810     case timeout_error:
811       ret = MSG_TIMEOUT;
812       break;
813     default:
814       throw;
815     }
816
817     /* If the send failed, it is not used anymore */
818     t_simdata->setNotUsed();
819   }
820
821   TRACE_msg_task_put_end();
822   return ret;
823 }
824
825 /** @ingroup msg_task_usage
826  * @brief Sends a task to a mailbox with a timeout and with a maximum rate
827  *
828  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
829  *
830  * The maxrate parameter can be used to send a task with a limited
831  * bandwidth (smaller than the physical available value). Use
832  * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
833  * value do disable this feature).
834  *
835  * @param task the task to be sent
836  * @param alias the mailbox name to where the task is sent
837  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
838  * @param maxrate the maximum communication rate for sending this task (byte/sec)
839  *
840  * @return Returns #MSG_OK if the task was successfully sent,
841  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
842  */
843 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
844 {
845   task->simdata->rate = maxrate;
846   return MSG_task_send_with_timeout(task, alias, timeout);
847 }
848
849 /** @ingroup msg_task_usage
850  * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
851  *
852  * @param alias the name of the mailbox to be considered
853  *
854  * @return Returns the PID of sender process,
855  * -1 if there is no communication in the mailbox.
856  */
857 int MSG_task_listen_from(const char *alias)
858 {
859   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
860   simgrid::kernel::activity::CommImplPtr comm =
861       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
862
863   if (not comm)
864     return -1;
865
866   return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
867 }
868
869 /** @ingroup msg_task_usage
870  * @brief Sets the tracing category of a task.
871  *
872  * This function should be called after the creation of a MSG task, to define the category of that task. The
873  * first parameter task must contain a task that was  created with the function #MSG_task_create. The second
874  * parameter category must contain a category that was previously declared with the function #TRACE_category
875  * (or with #TRACE_category_with_color).
876  *
877  * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
878  *
879  * @param task the task that is going to be categorized
880  * @param category the name of the category to be associated to the task
881  *
882  * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
883  */
884 void MSG_task_set_category (msg_task_t task, const char *category)
885 {
886   TRACE_msg_set_task_category (task, category);
887 }
888
889 /** @ingroup msg_task_usage
890  *
891  * @brief Gets the current tracing category of a task.
892  *
893  * @param task the task to be considered
894  *
895  * @see MSG_task_set_category
896  *
897  * @return Returns the name of the tracing category of the given task, nullptr otherwise
898  */
899 const char *MSG_task_get_category (msg_task_t task)
900 {
901   return task->category;
902 }