Logo AND Algorithmique Numérique Distribuée

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