Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
eac963dfcc7bfed6574b0a8fe27b9001a96e0a14
[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   simgrid::s4u::MailboxPtr 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 host_error:
287     case cancel_error:
288       ret = MSG_HOST_FAILURE;
289       break;
290     case network_error:
291       ret = MSG_TRANSFER_FAILURE;
292       break;
293     case timeout_error:
294       ret = MSG_TIMEOUT;
295       break;
296     default:
297       throw;
298     }
299   }
300
301   if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
302     TRACE_msg_task_get_end(start_time, *task);
303   }
304   return ret;
305 }
306
307 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
308 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
309                                                      int (*match_fun)(void*,void*, smx_synchro_t),
310                                                      void *match_data, void_f_pvoid_t cleanup, int detached)
311 {
312   simdata_task_t t_simdata = nullptr;
313   msg_process_t myself = SIMIX_process_self();
314   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
315   int call_end = TRACE_msg_task_put_start(task);
316
317   /* Prepare the task to send */
318   t_simdata = task->simdata;
319   t_simdata->sender = myself;
320   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host;
321   t_simdata->setUsed();
322   t_simdata->comm = nullptr;
323   msg_global->sent_msg++;
324
325   /* Send it by calling SIMIX network layer */
326   smx_synchro_t act = simcall_comm_isend(myself, mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate,
327                                          task, sizeof(void *), match_fun, cleanup, nullptr, match_data,detached);
328   t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(act);
329
330   msg_comm_t comm = nullptr;
331   if (! detached) {
332     comm = xbt_new0(s_msg_comm_t, 1);
333     comm->task_sent = task;
334     comm->task_received = nullptr;
335     comm->status = MSG_OK;
336     comm->s_comm = act;
337   }
338
339   if (TRACE_is_enabled())
340     simcall_set_category(act, task->category);
341   if (call_end)
342     TRACE_msg_task_put_end();
343
344   return comm;
345 }
346
347 /** \ingroup msg_task_usage
348  * \brief Sends a task on a mailbox.
349  *
350  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
351  *
352  * \param task a #msg_task_t to send on another location.
353  * \param alias name of the mailbox to sent the task to
354  * \return the msg_comm_t communication created
355  */
356 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
357 {
358   return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
359 }
360
361 /** \ingroup msg_task_usage
362  * \brief Sends a task on a mailbox with a maximum rate
363  *
364  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
365  * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
366  *
367  * \param task a #msg_task_t to send on another location.
368  * \param alias name of the mailbox to sent the task to
369  * \param maxrate the maximum communication rate for sending this task .
370  * \return the msg_comm_t communication created
371  */
372 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
373 {
374   task->simdata->rate = maxrate;
375   return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
376 }
377
378 /** \ingroup msg_task_usage
379  * \brief Sends a task on a mailbox, with support for matching requests
380  *
381  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
382  *
383  * \param task a #msg_task_t to send on another location.
384  * \param alias name of the mailbox to sent the task to
385  * \param match_fun boolean function which parameters are:
386  *        - match_data_provided_here
387  *        - match_data_provided_by_other_side_if_any
388  *        - the_smx_synchro_describing_the_other_side
389  * \param match_data user provided data passed to match_fun
390  * \return the msg_comm_t communication created
391  */
392 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
393                                         int (*match_fun)(void*, void*, smx_synchro_t), void *match_data)
394 {
395   return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
396 }
397
398 /** \ingroup msg_task_usage
399  * \brief Sends a task on a mailbox.
400  *
401  * This is a non blocking detached send function.
402  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
403  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
404  * usual. More details on this can be obtained on
405  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
406  * in the SimGrid-user mailing list archive.
407  *
408  * \param task a #msg_task_t to send on another location.
409  * \param alias name of the mailbox to sent the task to
410  * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
411  * (if nullptr, no function will be called)
412  */
413 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
414 {
415   MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
416 }
417
418 /** \ingroup msg_task_usage
419  * \brief Sends a task on a mailbox with a maximal rate.
420  *
421  * This is a non blocking detached send function.
422  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
423  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
424  * usual. More details on this can be obtained on
425  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
426  * in the SimGrid-user mailing list archive.
427  *
428  * \param task a #msg_task_t to send on another location.
429  * \param alias name of the mailbox to sent the task to
430  * \param cleanup a function to destroy the task if the
431  * communication fails, e.g. MSG_task_destroy
432  * (if nullptr, no function will be called)
433  * \param maxrate the maximum communication rate for sending this task
434  *
435  */
436 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
437 {
438   task->simdata->rate = maxrate;
439   MSG_task_dsend(task, alias, cleanup);
440 }
441
442 /** \ingroup msg_task_usage
443  * \brief Starts listening for receiving a task from an asynchronous communication.
444  *
445  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
446  *
447  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
448  * \param name of the mailbox to receive the task on
449  * \return the msg_comm_t communication created
450  */
451 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
452 {
453   return MSG_task_irecv_bounded(task, name, -1.0);
454 }
455
456 /** \ingroup msg_task_usage
457  * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
458  *
459  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
460  * \param name of the mailbox to receive the task on
461  * \param rate limit the bandwidth to the given rate
462  * \return the msg_comm_t communication created
463  */
464 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
465 {
466   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
467
468   /* FIXME: these functions are not traceable */
469   /* Sanity check */
470   xbt_assert(task, "Null pointer for the task storage");
471
472   if (*task)
473     XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
474
475   /* Try to receive it by calling SIMIX network layer */
476   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
477   comm->task_sent = nullptr;
478   comm->task_received = task;
479   comm->status = MSG_OK;
480   comm->s_comm = simcall_comm_irecv(MSG_process_self(), mbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, rate);
481
482   return comm;
483 }
484
485 /** \ingroup msg_task_usage
486  * \brief Checks whether a communication is done, and if yes, finalizes it.
487  * \param comm the communication to test
488  * \return TRUE if the communication is finished
489  * (but it may have failed, use MSG_comm_get_status() to know its status)
490  * or FALSE if the communication is not finished yet
491  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
492  */
493 int MSG_comm_test(msg_comm_t comm)
494 {
495   int finished = 0;
496
497   try {
498     finished = simcall_comm_test(comm->s_comm);
499     if (finished && comm->task_received != nullptr) {
500       /* I am the receiver */
501       (*comm->task_received)->simdata->setNotUsed();
502     }
503   }
504   catch (xbt_ex& e) {
505     switch (e.category) {
506       case network_error:
507         comm->status = MSG_TRANSFER_FAILURE;
508         finished = 1;
509         break;
510       case timeout_error:
511         comm->status = MSG_TIMEOUT;
512         finished = 1;
513         break;
514       default:
515         throw;
516     }
517   }
518
519   return finished;
520 }
521
522 /** \ingroup msg_task_usage
523  * \brief This function checks if a communication is finished.
524  * \param comms a vector of communications
525  * \return the position of the finished communication if any
526  * (but it may have failed, use MSG_comm_get_status() to know its status),
527  * or -1 if none is finished
528  */
529 int MSG_comm_testany(xbt_dynar_t comms)
530 {
531   int finished_index = -1;
532
533   /* Create the equivalent array with SIMIX objects: */
534   std::vector<simgrid::kernel::activity::Synchro*> s_comms;
535   s_comms.reserve(xbt_dynar_length(comms));
536   msg_comm_t comm;
537   unsigned int cursor;
538   xbt_dynar_foreach(comms, cursor, comm) {
539     s_comms.push_back(comm->s_comm);
540   }
541
542   msg_error_t status = MSG_OK;
543   try {
544     finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
545   }
546   catch (xbt_ex& e) {
547     switch (e.category) {
548       case network_error:
549         finished_index = e.value;
550         status = MSG_TRANSFER_FAILURE;
551         break;
552       case timeout_error:
553         finished_index = e.value;
554         status = MSG_TIMEOUT;
555         break;
556       default:
557         throw;
558     }
559   }
560
561   if (finished_index != -1) {
562     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
563     /* the communication is finished */
564     comm->status = status;
565
566     if (status == MSG_OK && comm->task_received != nullptr) {
567       /* I am the receiver */
568       (*comm->task_received)->simdata->setNotUsed();
569     }
570   }
571
572   return finished_index;
573 }
574
575 /** \ingroup msg_task_usage
576  * \brief Destroys a communication.
577  * \param comm the communication to destroy.
578  */
579 void MSG_comm_destroy(msg_comm_t comm)
580 {
581   xbt_free(comm);
582 }
583
584 /** \ingroup msg_task_usage
585  * \brief Wait for the completion of a communication.
586  *
587  * It takes two parameters.
588  * \param comm the communication to wait.
589  * \param timeout Wait until the communication terminates or the timeout occurs.
590  *                You can provide a -1 timeout to obtain an infinite timeout.
591  * \return msg_error_t
592  */
593 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
594 {
595   try {
596     simcall_comm_wait(comm->s_comm, timeout);
597
598     if (comm->task_received != nullptr) {
599       /* I am the receiver */
600       (*comm->task_received)->simdata->setNotUsed();
601     }
602
603     /* FIXME: these functions are not traceable */
604   }
605   catch (xbt_ex& e) {
606     switch (e.category) {
607     case network_error:
608       comm->status = MSG_TRANSFER_FAILURE;
609       break;
610     case timeout_error:
611       comm->status = MSG_TIMEOUT;
612       break;
613     default:
614       throw;
615     }
616   }
617
618   return comm->status;
619 }
620
621 /** \ingroup msg_task_usage
622 * \brief This function is called by a sender and permit to wait for each communication
623 *
624 * \param comm a vector of communication
625 * \param nb_elem is the size of the comm vector
626 * \param timeout for each call of MSG_comm_wait
627 */
628 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
629 {
630   int i = 0;
631   for (i = 0; i < nb_elem; i++) {
632     MSG_comm_wait(comm[i], timeout);
633   }
634 }
635
636 /** \ingroup msg_task_usage
637  * \brief This function waits for the first communication finished in a list.
638  * \param comms a vector of communications
639  * \return the position of the first finished communication
640  * (but it may have failed, use MSG_comm_get_status() to know its status)
641  */
642 int MSG_comm_waitany(xbt_dynar_t comms)
643 {
644   int finished_index = -1;
645
646   /* create the equivalent dynar with SIMIX objects */
647   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), nullptr);
648   msg_comm_t comm;
649   unsigned int cursor;
650   xbt_dynar_foreach(comms, cursor, comm) {
651     xbt_dynar_push(s_comms, &comm->s_comm);
652   }
653
654   msg_error_t status = MSG_OK;
655   try {
656     finished_index = simcall_comm_waitany(s_comms, -1);
657   }
658   catch(xbt_ex& e) {
659     switch (e.category) {
660       case network_error:
661         finished_index = e.value;
662         status = MSG_TRANSFER_FAILURE;
663         break;
664       case timeout_error:
665         finished_index = e.value;
666         status = MSG_TIMEOUT;
667         break;
668       default:
669         throw;
670     }
671   }
672
673   xbt_assert(finished_index != -1, "WaitAny returned -1");
674   xbt_dynar_free(&s_comms);
675
676   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
677   /* the communication is finished */
678   comm->status = status;
679
680   if (comm->task_received != nullptr) {
681     /* I am the receiver */
682     (*comm->task_received)->simdata->setNotUsed();
683   }
684
685   return finished_index;
686 }
687
688 /**
689  * \ingroup msg_task_usage
690  * \brief Returns the error (if any) that occurred during a finished communication.
691  * \param comm a finished communication
692  * \return the status of the communication, or #MSG_OK if no error occurred
693  * during the communication
694  */
695 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
696
697   return comm->status;
698 }
699
700 /** \ingroup msg_task_usage
701  * \brief Get a task (#msg_task_t) from a communication
702  *
703  * \param comm the communication where to get the task
704  * \return the task from the communication
705  */
706 msg_task_t MSG_comm_get_task(msg_comm_t comm)
707 {
708   xbt_assert(comm, "Invalid parameter");
709
710   return comm->task_received ? *comm->task_received : comm->task_sent;
711 }
712
713 /**
714  * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
715  * \param comm the comm
716  * \param buff the data copied
717  * \param buff_size size of the buffer
718  */
719 void MSG_comm_copy_data_from_SIMIX(smx_synchro_t synchro, void* buff, size_t buff_size)
720 {
721   simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
722
723   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
724
725   // notify the user callback if any
726   if (msg_global->task_copy_callback) {
727     msg_task_t task = (msg_task_t) buff;
728     msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
729   }
730 }
731
732 /** \ingroup msg_task_usage
733  * \brief Sends a task to a mailbox
734  *
735  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
736  * side if #MSG_task_receive is used).
737  * See #MSG_task_isend for sending tasks asynchronously.
738  *
739  * \param task the task to be sent
740  * \param alias the mailbox name to where the task is sent
741  *
742  * \return Returns #MSG_OK if the task was successfully sent,
743  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
744  */
745 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
746 {
747   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
748   return MSG_task_send_with_timeout(task, alias, -1);
749 }
750
751 /** \ingroup msg_task_usage
752  * \brief Sends a task to a mailbox with a maximum rate
753  *
754  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
755  * the application to limit the bandwidth utilization of network links when sending the task.
756  *
757  * \param task the task to be sent
758  * \param alias the mailbox name to where the task is sent
759  * \param maxrate the maximum communication rate for sending this task
760  *
761  * \return Returns #MSG_OK if the task was successfully sent,
762  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
763  */
764 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
765 {
766   task->simdata->rate = maxrate;
767   return MSG_task_send(task, alias);
768 }
769
770 /** \ingroup msg_task_usage
771  * \brief Sends a task to a mailbox with a timeout
772  *
773  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
774  *
775  * \param task the task to be sent
776  * \param alias the mailbox name to where the task is sent
777  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
778  *
779  * \return Returns #MSG_OK if the task was successfully sent,
780  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
781  */
782 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
783 {
784   msg_error_t ret = MSG_OK;
785   simdata_task_t t_simdata = nullptr;
786   msg_process_t process = MSG_process_self();
787   simdata_process_t p_simdata = (simdata_process_t) SIMIX_process_self_get_data();
788   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
789
790   int call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
791
792   /* Prepare the task to send */
793   t_simdata = task->simdata;
794   t_simdata->sender = process;
795   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host;
796
797   t_simdata->setUsed();
798
799   t_simdata->comm = nullptr;
800   msg_global->sent_msg++;
801
802   p_simdata->waiting_task = task;
803
804   /* Try to send it by calling SIMIX network layer */
805   try {
806     smx_synchro_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call  */
807     comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
808                               t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
809     if (TRACE_is_enabled())
810       simcall_set_category(comm, task->category);
811      t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(comm);
812      simcall_comm_wait(comm, timeout);
813   }
814   catch (xbt_ex& e) {
815     switch (e.category) {
816     case cancel_error:
817       ret = MSG_HOST_FAILURE;
818       break;
819     case network_error:
820       ret = MSG_TRANSFER_FAILURE;
821       break;
822     case timeout_error:
823       ret = MSG_TIMEOUT;
824       break;
825     default:
826       throw;
827     }
828
829     /* If the send failed, it is not used anymore */
830     t_simdata->setNotUsed();
831   }
832
833   p_simdata->waiting_task = nullptr;
834   if (call_end)
835     TRACE_msg_task_put_end();
836   return ret;
837 }
838
839 /** \ingroup msg_task_usage
840  * \brief Sends a task to a mailbox with a timeout and with a maximum rate
841  *
842  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
843  *
844  * \param task the task to be sent
845  * \param alias the mailbox name to where the task is sent
846  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
847  * \param maxrate the maximum communication rate for sending this task
848  *
849  * \return Returns #MSG_OK if the task was successfully sent,
850  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
851  */
852 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
853 {
854   task->simdata->rate = maxrate;
855   return MSG_task_send_with_timeout(task, alias, timeout);
856 }
857
858 /** \ingroup msg_task_usage
859  * \brief Check if there is a communication going on in a mailbox.
860  *
861  * \param alias the name of the mailbox to be considered
862  *
863  * \return Returns 1 if there is a communication, 0 otherwise
864  */
865 int MSG_task_listen(const char *alias)
866 {
867   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
868   return !mbox->empty() ||
869     (mbox->getImpl()->permanent_receiver && !mbox->getImpl()->done_comm_queue.empty());
870 }
871
872 /** \ingroup msg_task_usage
873  * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
874  *
875  * \param alias the name of the mailbox to be considered
876  *
877  * \return Returns the PID of sender process,
878  * -1 if there is no communication in the mailbox.
879  */
880 int MSG_task_listen_from(const char *alias)
881 {
882   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
883   simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mbox->front());
884
885   if (!comm)
886     return -1;
887
888   return MSG_process_get_PID( static_cast<msg_task_t>(comm->src_data)->simdata->sender );
889 }
890
891 /** \ingroup msg_task_usage
892  * \brief Sets the tracing category of a task.
893  *
894  * This function should be called after the creation of a MSG task, to define the category of that task. The
895  * first parameter task must contain a task that was  created with the function #MSG_task_create. The second
896  * parameter category must contain a category that was previously declared with the function #TRACE_category
897  * (or with #TRACE_category_with_color).
898  *
899  * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
900  *
901  * \param task the task that is going to be categorized
902  * \param category the name of the category to be associated to the task
903  *
904  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
905  */
906 void MSG_task_set_category (msg_task_t task, const char *category)
907 {
908   TRACE_msg_set_task_category (task, category);
909 }
910
911 /** \ingroup msg_task_usage
912  *
913  * \brief Gets the current tracing category of a task.
914  *
915  * \param task the task to be considered
916  *
917  * \see MSG_task_set_category
918  *
919  * \return Returns the name of the tracing category of the given task, nullptr otherwise
920  */
921 const char *MSG_task_get_category (msg_task_t task)
922 {
923   return task->category;
924 }
925
926 /**
927  * \brief Returns the value of a given AS or router property
928  *
929  * \param asr the name of a router or AS
930  * \param name a property name
931  * \return value of a property (or nullptr if property not set)
932  */
933 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
934 {
935   return (char*) xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
936 }
937
938 /**
939  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
940  * a the AS or router
941  *
942  * \param asr the name of a router or AS
943  * \return a dict containing the properties
944  */
945 xbt_dict_t MSG_as_router_get_properties(const char* asr)
946 {
947   return (simcall_asr_get_properties(asr));
948 }
949
950 /**
951  * \brief Change the value of a given AS or router
952  *
953  * \param asr the name of a router or AS
954  * \param name a property name
955  * \param value what to change the property to
956  * \param free_ctn the freeing function to use to kill the value on need
957  */
958 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
959   xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
960 }