Logo AND Algorithmique Numérique Distribuée

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