Logo AND Algorithmique Numérique Distribuée

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