Logo AND Algorithmique Numérique Distribuée

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