Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0abc0ce7b4bce184df2fa12e396baa51f5b4babc
[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   /* Diagnose a possible user error: destroying a comm that is not done yet, and not detached either  */
578   if ((comm->s_comm->state == SIMIX_READY || comm->s_comm->state == SIMIX_RUNNING)
579       && (!static_cast<simgrid::kernel::activity::Comm*>(comm->s_comm)->detached))
580     XBT_WARN("You are destroying a communication that is not done yet but %s. "
581              "This will lead to issues when the other side continues this communication. "
582              "Are you looking for MSG_task_dsend()?",
583              (comm->s_comm->state == SIMIX_READY ? "ready" : "running"));
584
585   xbt_free(comm);
586 }
587
588 /** \ingroup msg_task_usage
589  * \brief Wait for the completion of a communication.
590  *
591  * It takes two parameters.
592  * \param comm the communication to wait.
593  * \param timeout Wait until the communication terminates or the timeout occurs.
594  *                You can provide a -1 timeout to obtain an infinite timeout.
595  * \return msg_error_t
596  */
597 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
598 {
599   try {
600     simcall_comm_wait(comm->s_comm, timeout);
601
602     if (comm->task_received != nullptr) {
603       /* I am the receiver */
604       (*comm->task_received)->simdata->setNotUsed();
605     }
606
607     /* FIXME: these functions are not traceable */
608   }
609   catch (xbt_ex& e) {
610     switch (e.category) {
611     case network_error:
612       comm->status = MSG_TRANSFER_FAILURE;
613       break;
614     case timeout_error:
615       comm->status = MSG_TIMEOUT;
616       break;
617     default:
618       throw;
619     }
620   }
621
622   return comm->status;
623 }
624
625 /** \ingroup msg_task_usage
626 * \brief This function is called by a sender and permit to wait for each communication
627 *
628 * \param comm a vector of communication
629 * \param nb_elem is the size of the comm vector
630 * \param timeout for each call of MSG_comm_wait
631 */
632 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
633 {
634   int i = 0;
635   for (i = 0; i < nb_elem; i++) {
636     MSG_comm_wait(comm[i], timeout);
637   }
638 }
639
640 /** \ingroup msg_task_usage
641  * \brief This function waits for the first communication finished in a list.
642  * \param comms a vector of communications
643  * \return the position of the first finished communication
644  * (but it may have failed, use MSG_comm_get_status() to know its status)
645  */
646 int MSG_comm_waitany(xbt_dynar_t comms)
647 {
648   int finished_index = -1;
649
650   /* create the equivalent dynar with SIMIX objects */
651   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), nullptr);
652   msg_comm_t comm;
653   unsigned int cursor;
654   xbt_dynar_foreach(comms, cursor, comm) {
655     xbt_dynar_push(s_comms, &comm->s_comm);
656   }
657
658   msg_error_t status = MSG_OK;
659   try {
660     finished_index = simcall_comm_waitany(s_comms, -1);
661   }
662   catch(xbt_ex& e) {
663     switch (e.category) {
664       case network_error:
665         finished_index = e.value;
666         status = MSG_TRANSFER_FAILURE;
667         break;
668       case timeout_error:
669         finished_index = e.value;
670         status = MSG_TIMEOUT;
671         break;
672       default:
673         throw;
674     }
675   }
676
677   xbt_assert(finished_index != -1, "WaitAny returned -1");
678   xbt_dynar_free(&s_comms);
679
680   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
681   /* the communication is finished */
682   comm->status = status;
683
684   if (comm->task_received != nullptr) {
685     /* I am the receiver */
686     (*comm->task_received)->simdata->setNotUsed();
687   }
688
689   return finished_index;
690 }
691
692 /**
693  * \ingroup msg_task_usage
694  * \brief Returns the error (if any) that occurred during a finished communication.
695  * \param comm a finished communication
696  * \return the status of the communication, or #MSG_OK if no error occurred
697  * during the communication
698  */
699 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
700
701   return comm->status;
702 }
703
704 /** \ingroup msg_task_usage
705  * \brief Get a task (#msg_task_t) from a communication
706  *
707  * \param comm the communication where to get the task
708  * \return the task from the communication
709  */
710 msg_task_t MSG_comm_get_task(msg_comm_t comm)
711 {
712   xbt_assert(comm, "Invalid parameter");
713
714   return comm->task_received ? *comm->task_received : comm->task_sent;
715 }
716
717 /**
718  * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
719  * \param comm the comm
720  * \param buff the data copied
721  * \param buff_size size of the buffer
722  */
723 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
724 {
725   simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
726
727   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
728
729   // notify the user callback if any
730   if (msg_global->task_copy_callback) {
731     msg_task_t task = static_cast<msg_task_t>(buff);
732     msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
733   }
734 }
735
736 /** \ingroup msg_task_usage
737  * \brief Sends a task to a mailbox
738  *
739  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
740  * side if #MSG_task_receive is used).
741  * See #MSG_task_isend for sending tasks asynchronously.
742  *
743  * \param task the task to be sent
744  * \param alias the mailbox name to where the task is sent
745  *
746  * \return Returns #MSG_OK if the task was successfully sent,
747  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
748  */
749 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
750 {
751   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
752   return MSG_task_send_with_timeout(task, alias, -1);
753 }
754
755 /** \ingroup msg_task_usage
756  * \brief Sends a task to a mailbox with a maximum rate
757  *
758  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
759  * the application to limit the bandwidth utilization of network links when sending the task.
760  *
761  * \param task the task to be sent
762  * \param alias the mailbox name to where the task is sent
763  * \param maxrate the maximum communication rate for sending this task
764  *
765  * \return Returns #MSG_OK if the task was successfully sent,
766  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
767  */
768 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
769 {
770   task->simdata->rate = maxrate;
771   return MSG_task_send(task, alias);
772 }
773
774 /** \ingroup msg_task_usage
775  * \brief Sends a task to a mailbox with a timeout
776  *
777  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
778  *
779  * \param task the task to be sent
780  * \param alias the mailbox name to where the task is sent
781  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
782  *
783  * \return Returns #MSG_OK if the task was successfully sent,
784  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
785  */
786 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
787 {
788   msg_error_t ret = MSG_OK;
789   simdata_task_t t_simdata = nullptr;
790   msg_process_t process = MSG_process_self();
791   simdata_process_t p_simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
792   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
793
794   int call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
795
796   /* Prepare the task to send */
797   t_simdata = task->simdata;
798   t_simdata->sender = process;
799   t_simdata->source = (static_cast<simdata_process_t>(SIMIX_process_self_get_data()))   ->m_host;
800
801   t_simdata->setUsed();
802
803   t_simdata->comm = nullptr;
804   msg_global->sent_msg++;
805
806   p_simdata->waiting_task = task;
807
808   /* Try to send it by calling SIMIX network layer */
809   try {
810     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call  */
811     comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
812                               t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
813     if (TRACE_is_enabled())
814       simcall_set_category(comm, task->category);
815      t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(comm);
816      simcall_comm_wait(comm, timeout);
817   }
818   catch (xbt_ex& e) {
819     switch (e.category) {
820     case cancel_error:
821       ret = MSG_HOST_FAILURE;
822       break;
823     case network_error:
824       ret = MSG_TRANSFER_FAILURE;
825       break;
826     case timeout_error:
827       ret = MSG_TIMEOUT;
828       break;
829     default:
830       throw;
831     }
832
833     /* If the send failed, it is not used anymore */
834     t_simdata->setNotUsed();
835   }
836
837   p_simdata->waiting_task = nullptr;
838   if (call_end)
839     TRACE_msg_task_put_end();
840   return ret;
841 }
842
843 /** \ingroup msg_task_usage
844  * \brief Sends a task to a mailbox with a timeout and with a maximum rate
845  *
846  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
847  *
848  * \param task the task to be sent
849  * \param alias the mailbox name to where the task is sent
850  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
851  * \param maxrate the maximum communication rate for sending this task
852  *
853  * \return Returns #MSG_OK if the task was successfully sent,
854  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
855  */
856 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
857 {
858   task->simdata->rate = maxrate;
859   return MSG_task_send_with_timeout(task, alias, timeout);
860 }
861
862 /** \ingroup msg_task_usage
863  * \brief Check if there is a communication going on in a mailbox.
864  *
865  * \param alias the name of the mailbox to be considered
866  *
867  * \return Returns 1 if there is a communication, 0 otherwise
868  */
869 int MSG_task_listen(const char *alias)
870 {
871   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
872   return !mbox->empty() ||
873     (mbox->getImpl()->permanent_receiver && !mbox->getImpl()->done_comm_queue.empty());
874 }
875
876 /** \ingroup msg_task_usage
877  * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
878  *
879  * \param alias the name of the mailbox to be considered
880  *
881  * \return Returns the PID of sender process,
882  * -1 if there is no communication in the mailbox.
883  */
884 int MSG_task_listen_from(const char *alias)
885 {
886   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
887   simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mbox->front());
888
889   if (!comm)
890     return -1;
891
892   return MSG_process_get_PID( static_cast<msg_task_t>(comm->src_data)->simdata->sender );
893 }
894
895 /** \ingroup msg_task_usage
896  * \brief Sets the tracing category of a task.
897  *
898  * This function should be called after the creation of a MSG task, to define the category of that task. The
899  * first parameter task must contain a task that was  created with the function #MSG_task_create. The second
900  * parameter category must contain a category that was previously declared with the function #TRACE_category
901  * (or with #TRACE_category_with_color).
902  *
903  * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
904  *
905  * \param task the task that is going to be categorized
906  * \param category the name of the category to be associated to the task
907  *
908  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
909  */
910 void MSG_task_set_category (msg_task_t task, const char *category)
911 {
912   TRACE_msg_set_task_category (task, category);
913 }
914
915 /** \ingroup msg_task_usage
916  *
917  * \brief Gets the current tracing category of a task.
918  *
919  * \param task the task to be considered
920  *
921  * \see MSG_task_set_category
922  *
923  * \return Returns the name of the tracing category of the given task, nullptr otherwise
924  */
925 const char *MSG_task_get_category (msg_task_t task)
926 {
927   return task->category;
928 }
929
930 /**
931  * \brief Returns the value of a given AS or router property
932  *
933  * \param asr the name of a router or AS
934  * \param name a property name
935  * \return value of a property (or nullptr if property not set)
936  */
937 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
938 {
939   return static_cast<char*>(xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name));
940 }
941
942 /**
943  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
944  * a the AS or router
945  *
946  * \param asr the name of a router or AS
947  * \return a dict containing the properties
948  */
949 xbt_dict_t MSG_as_router_get_properties(const char* asr)
950 {
951   return (simcall_asr_get_properties(asr));
952 }
953
954 /**
955  * \brief Change the value of a given AS or router
956  *
957  * \param asr the name of a router or AS
958  * \param name a property name
959  * \param value what to change the property to
960  */
961 void MSG_as_router_set_property_value(const char* asr, const char* name, char* value)
962 {
963   xbt_dict_set(MSG_as_router_get_properties(asr), name, value, nullptr);
964 }