1 /* Copyright (c) 2004-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "msg_private.h"
8 #include "msg_mailbox.h"
11 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
14 "Logging specific to MSG (gos)");
16 /** \ingroup msg_task_usage
17 * \brief Executes a task and waits for its termination.
19 * This function is used for describing the behavior of a process. It
20 * 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
23 * or #MSG_HOST_FAILURE otherwise
25 msg_error_t MSG_task_execute(msg_task_t task)
27 /* TODO: add this to other locations */
28 msg_host_t host = MSG_process_get_host(MSG_process_self());
29 MSG_host_add_task(host, task);
31 msg_error_t ret = MSG_parallel_task_execute(task);
33 MSG_host_del_task(host, task);
38 /** \ingroup msg_task_usage
39 * \brief Executes a parallel task and waits for its termination.
41 * \param task a #msg_task_t to execute on the location on which the process is running.
43 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
44 * or #MSG_HOST_FAILURE otherwise
46 msg_error_t MSG_parallel_task_execute(msg_task_t task)
49 simdata_task_t simdata = task->simdata;
50 msg_process_t self = SIMIX_process_self();
51 simdata_process_t p_simdata = SIMIX_process_self_get_data(self);
52 e_smx_state_t comp_state;
53 msg_error_t status = MSG_OK;
56 TRACE_msg_task_execute_start(task);
59 xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
60 "This task is executed somewhere else. Go fix your code! %d",
61 task->simdata->isused!=NULL);
63 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
65 if (simdata->computation_amount == 0 && !simdata->host_nb) {
67 TRACE_msg_task_execute_end(task);
74 if (msg_global->debug_multiple_use)
75 MSG_BT(simdata->isused, "Using Backtrace");
77 simdata->isused = (void*)1;
79 if (simdata->host_nb > 0) {
80 simdata->compute = simcall_host_parallel_execute(task->name,
86 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
88 unsigned long affinity_mask = (unsigned long) xbt_dict_get_or_null_ext(simdata->affinity_mask_db, (char *) p_simdata->m_host, sizeof(msg_host_t));
89 XBT_DEBUG("execute %s@%s with affinity(0x%04lx)", MSG_task_get_name(task), MSG_host_get_name(p_simdata->m_host), affinity_mask);
91 simdata->compute = simcall_host_execute(task->name,
93 simdata->computation_amount,
101 simcall_set_category(simdata->compute, task->category);
103 p_simdata->waiting_action = simdata->compute;
104 comp_state = simcall_host_execution_wait(simdata->compute);
106 p_simdata->waiting_action = NULL;
108 if (msg_global->debug_multiple_use && simdata->isused!=0)
109 xbt_ex_free(*(xbt_ex_t*)simdata->isused);
112 XBT_DEBUG("Execution task '%s' finished in state %d",
113 task->name, (int)comp_state);
116 switch (e.category) {
118 status = MSG_TASK_CANCELED;
121 status = MSG_HOST_FAILURE;
128 /* action ended, set comm and compute = NULL, the actions is already destroyed
129 * in the main function */
130 simdata->computation_amount = 0.0;
131 simdata->comm = NULL;
132 simdata->compute = NULL;
134 TRACE_msg_task_execute_end(task);
141 /** \ingroup msg_task_usage
142 * \brief Sleep for the specified number of seconds
144 * Makes the current process sleep until \a time seconds have elapsed.
146 * \param nb_sec a number of second
148 msg_error_t MSG_process_sleep(double nb_sec)
151 msg_error_t status = MSG_OK;
152 /*msg_process_t proc = MSG_process_self();*/
155 TRACE_msg_process_sleep_in(MSG_process_self());
158 /* create action to sleep */
160 /*proc->simdata->waiting_action = act_sleep;
162 FIXME: check if not setting the waiting_action breaks something on msg
164 proc->simdata->waiting_action = NULL;*/
167 simcall_process_sleep(nb_sec);
170 switch (e.category) {
172 status = MSG_TASK_CANCELED;
181 TRACE_msg_process_sleep_out(MSG_process_self());
186 /** \ingroup msg_task_usage
187 * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
189 * Sorry, this function is not supported anymore. That wouldn't be
190 * impossible to reimplement it, but we are lacking the time to do so ourselves.
191 * If you need this functionality, you can either:
193 * - implement the buffering mechanism on the user-level by queuing all messages
194 * received in the mailbox that do not match your expectation
195 * - change your application logic to leverage the mailboxes features. For example,
196 * if you have A receiving messages from B and C, you could have A waiting on
197 * mailbox "A" most of the time, but on "A#B" when it's waiting for specific
198 * messages from B and "A#C" when waiting for messages from C. You could even get A
199 * sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
200 * an example of use of this function in the @ref MSG_examples section.
201 * - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
202 * very difficult actually. Check the function @ref MSG_mailbox_get_task_ext. During its call to
203 * simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
204 * messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
205 * and that your filtering function will receive as first parameter, and then, the filter could
206 * simply compare the host names, for example. After sufficient testing, provide an example that
207 * we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
209 * \param task a memory location for storing a #msg_task_t.
210 * \param alias name of the mailbox to receive the task from
211 * \param host a #msg_host_t host from where the task was sent
214 * #MSG_OK if the task was successfully received,
215 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
218 MSG_task_receive_from_host(msg_task_t * task, const char *alias,
221 return MSG_task_receive_ext(task, alias, -1, host);
225 *\brief Deprecated function that used to receive a task from a mailbox from a specific host
226 *\brief at a given rate
228 * \param task a memory location for storing a #msg_task_t.
229 * \param alias name of the mailbox to receive the task from
230 * \param host a #msg_host_t host from where the task was sent
231 * \param rate limit the reception to rate bandwidth
234 * #MSG_OK if the task was successfully received,
235 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
238 MSG_task_receive_from_host_bounded(msg_task_t * task, const char *alias,
239 msg_host_t host, double rate)
241 return MSG_task_receive_ext_bounded(task, alias, -1, host, rate);
244 /** \ingroup msg_task_usage
245 * \brief Receives a task from a mailbox.
247 * This is a blocking function, the execution flow will be blocked
248 * until the task is received. See #MSG_task_irecv
249 * for receiving tasks asynchronously.
251 * \param task a memory location for storing a #msg_task_t.
252 * \param alias name of the mailbox to receive the task from
255 * #MSG_OK if the task was successfully received,
256 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
258 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
260 return MSG_task_receive_with_timeout(task, alias, -1);
263 /** \ingroup msg_task_usage
264 * \brief Receives a task from a mailbox at a given rate.
266 * \param task a memory location for storing a #msg_task_t.
267 * \param alias name of the mailbox to receive the task from
268 * \param rate limit the reception to rate bandwidth
271 * #MSG_OK if the task was successfully received,
272 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
274 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias,
277 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
280 /** \ingroup msg_task_usage
281 * \brief Receives a task from a mailbox with a given timeout.
283 * This is a blocking function with a timeout, the execution flow will be blocked
284 * until the task is received or the timeout is achieved. See #MSG_task_irecv
285 * for receiving tasks asynchronously. You can provide a -1 timeout
286 * to obtain an infinite timeout.
288 * \param task a memory location for storing a #msg_task_t.
289 * \param alias name of the mailbox to receive the task from
290 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
293 * #MSG_OK if the task was successfully received,
294 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
297 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
300 return MSG_task_receive_ext(task, alias, timeout, NULL);
303 /** \ingroup msg_task_usage
304 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
306 * \param task a memory location for storing a #msg_task_t.
307 * \param alias name of the mailbox to receive the task from
308 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
309 * \param rate limit the reception to rate bandwidth
312 * #MSG_OK if the task was successfully received,
313 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
316 MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias,
317 double timeout,double rate)
319 return MSG_task_receive_ext_bounded(task, alias, timeout, NULL, rate);
322 /** \ingroup msg_task_usage
323 * \brief Receives a task from a mailbox from a specific host with a given timeout.
325 * This is a blocking function with a timeout, the execution flow will be blocked
326 * until the task is received or the timeout is achieved. See #MSG_task_irecv
327 * for receiving tasks asynchronously. You can provide a -1 timeout
328 * to obtain an infinite timeout.
330 * \param task a memory location for storing a #msg_task_t.
331 * \param alias name of the mailbox to receive the task from
332 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
333 * \param host a #msg_host_t host from where the task was sent
336 * #MSG_OK if the task was successfully received,
337 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
340 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
344 msg_error_t ret = MSG_OK;
346 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
349 ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
353 switch (e.category) {
354 case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */
355 ret = MSG_HOST_FAILURE;
365 /** \ingroup msg_task_usage
366 * \brief Receives a task from a mailbox from a specific host with a given timeout
367 * and at a given rate.
369 * \param task a memory location for storing a #msg_task_t.
370 * \param alias name of the mailbox to receive the task from
371 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
372 * \param host a #msg_host_t host from where the task was sent
373 * \param rate limit the reception to rate bandwidth
376 * #MSG_OK if the task was successfully received,
377 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
380 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
381 msg_host_t host, double rate)
384 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
386 return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
387 host, timeout, rate);
390 /* Internal function used to factorize code between
391 * MSG_task_isend_with_matching() and MSG_task_dsend().
394 msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
395 int (*match_fun)(void*,void*, smx_action_t),
396 void *match_data, void_f_pvoid_t cleanup,
399 simdata_task_t t_simdata = NULL;
400 msg_process_t process = MSG_process_self();
401 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
404 int call_end = TRACE_msg_task_put_start(task);
407 /* Prepare the task to send */
408 t_simdata = task->simdata;
409 t_simdata->sender = process;
410 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
412 if (t_simdata->isused != 0) {
413 if (msg_global->debug_multiple_use){
414 XBT_ERROR("This task is already used in there:");
415 xbt_backtrace_display(t_simdata->isused);
416 XBT_ERROR("And you try to reuse it from here:");
417 xbt_backtrace_display_current();
419 xbt_assert(t_simdata->isused == 0,
420 "This task is still being used somewhere else. You cannot send it now. Go fix your code! (use --cfg=msg/debug_multiple_use:on to get the backtrace of the other process)");
424 if (msg_global->debug_multiple_use)
425 MSG_BT(t_simdata->isused, "Using Backtrace");
427 t_simdata->isused = (void*)1;
428 t_simdata->comm = NULL;
429 msg_global->sent_msg++;
431 /* Send it by calling SIMIX network layer */
432 smx_action_t act = simcall_comm_isend(mailbox, t_simdata->message_size,
433 t_simdata->rate, task, sizeof(void *),
434 match_fun, cleanup, match_data,detached);
435 t_simdata->comm = act; /* FIXME: is the field t_simdata->comm still useful? */
441 comm = xbt_new0(s_msg_comm_t, 1);
442 comm->task_sent = task;
443 comm->task_received = NULL;
444 comm->status = MSG_OK;
449 if (TRACE_is_enabled()) {
450 simcall_set_category(act, task->category);
454 TRACE_msg_task_put_end();
461 /** \ingroup msg_task_usage
462 * \brief Sends a task on a mailbox.
464 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
465 * to end the communication.
467 * \param task a #msg_task_t to send on another location.
468 * \param alias name of the mailbox to sent the task to
469 * \return the msg_comm_t communication created
471 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
473 return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
476 /** \ingroup msg_task_usage
477 * \brief Sends a task on a mailbox with a maximum rate
479 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
480 * to end the communication. The maxrate parameter allows the application
481 * to limit the bandwidth utilization of network links when sending the task.
483 * \param task a #msg_task_t to send on another location.
484 * \param alias name of the mailbox to sent the task to
485 * \param maxrate the maximum communication rate for sending this task .
486 * \return the msg_comm_t communication created
488 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias,
491 task->simdata->rate = maxrate;
492 return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
496 /** \ingroup msg_task_usage
497 * \brief Sends a task on a mailbox, with support for matching requests
499 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
500 * to end the communication.
502 * \param task a #msg_task_t to send on another location.
503 * \param alias name of the mailbox to sent the task to
504 * \param match_fun boolean function which parameters are:
505 * - match_data_provided_here
506 * - match_data_provided_by_other_side_if_any
507 * - the_smx_action_describing_the_other_side
508 * \param match_data user provided data passed to match_fun
509 * \return the msg_comm_t communication created
511 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
512 int (*match_fun)(void*, void*,
516 return MSG_task_isend_internal(task, alias, match_fun, match_data, NULL, 0);
519 /** \ingroup msg_task_usage
520 * \brief Sends a task on a mailbox.
522 * This is a non blocking detached send function.
523 * Think of it as a best effort send. Keep in mind that the third parameter
524 * is only called if the communication fails. If the communication does work,
525 * it is responsibility of the receiver code to free anything related to
526 * the task, as usual. More details on this can be obtained on
527 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
528 * in the SimGrid-user mailing list archive.
530 * \param task a #msg_task_t to send on another location.
531 * \param alias name of the mailbox to sent the task to
532 * \param cleanup a function to destroy the task if the
533 * communication fails, e.g. MSG_task_destroy
534 * (if NULL, no function will be called)
536 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
538 MSG_task_isend_internal(task, alias, NULL, NULL, cleanup, 1);
541 /** \ingroup msg_task_usage
542 * \brief Sends a task on a mailbox with a maximal rate.
544 * This is a non blocking detached send function.
545 * Think of it as a best effort send. Keep in mind that the third parameter
546 * is only called if the communication fails. If the communication does work,
547 * it is responsibility of the receiver code to free anything related to
548 * the task, as usual. More details on this can be obtained on
549 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
550 * in the SimGrid-user mailing list archive.
552 * \param task a #msg_task_t to send on another location.
553 * \param alias name of the mailbox to sent the task to
554 * \param cleanup a function to destroy the task if the
555 * communication fails, e.g. MSG_task_destroy
556 * (if NULL, no function will be called)
557 * \param maxrate the maximum communication rate for sending this task
560 void MSG_task_dsend_bounded(msg_task_t task, const char *alias,
561 void_f_pvoid_t cleanup, double maxrate)
563 task->simdata->rate = maxrate;
564 MSG_task_dsend(task, alias, cleanup);
567 /** \ingroup msg_task_usage
568 * \brief Starts listening for receiving a task from an asynchronous communication.
570 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
571 * to end the communication.
573 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
574 * \param name of the mailbox to receive the task on
575 * \return the msg_comm_t communication created
577 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
579 return MSG_task_irecv_bounded(task, name, -1.0);
582 /** \ingroup msg_task_usage
583 * \brief Starts listening for receiving a task from an asynchronous communication
586 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
587 * \param name of the mailbox to receive the task on
588 * \param rate limit the bandwidth to the given rate
589 * \return the msg_comm_t communication created
591 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name,
594 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
596 /* FIXME: these functions are not traceable */
599 xbt_assert(task, "Null pointer for the task storage");
603 ("MSG_task_irecv() was asked to write in a non empty task struct.");
605 /* Try to receive it by calling SIMIX network layer */
606 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
607 comm->task_sent = NULL;
608 comm->task_received = task;
609 comm->status = MSG_OK;
610 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL, rate);
615 /** \ingroup msg_task_usage
616 * \brief Checks whether a communication is done, and if yes, finalizes it.
617 * \param comm the communication to test
618 * \return TRUE if the communication is finished
619 * (but it may have failed, use MSG_comm_get_status() to know its status)
620 * or FALSE if the communication is not finished yet
621 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
623 int MSG_comm_test(msg_comm_t comm)
629 finished = simcall_comm_test(comm->s_comm);
631 if (finished && comm->task_received != NULL) {
632 /* I am the receiver */
633 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
634 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
635 (*comm->task_received)->simdata->isused = 0;
639 switch (e.category) {
641 comm->status = MSG_TRANSFER_FAILURE;
646 comm->status = MSG_TIMEOUT;
659 /** \ingroup msg_task_usage
660 * \brief This function checks if a communication is finished.
661 * \param comms a vector of communications
662 * \return the position of the finished communication if any
663 * (but it may have failed, use MSG_comm_get_status() to know its status),
664 * or -1 if none is finished
666 int MSG_comm_testany(xbt_dynar_t comms)
669 int finished_index = -1;
671 /* create the equivalent dynar with SIMIX objects */
672 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
675 xbt_dynar_foreach(comms, cursor, comm) {
676 xbt_dynar_push(s_comms, &comm->s_comm);
679 msg_error_t status = MSG_OK;
681 finished_index = simcall_comm_testany(s_comms);
684 switch (e.category) {
686 finished_index = e.value;
687 status = MSG_TRANSFER_FAILURE;
691 finished_index = e.value;
692 status = MSG_TIMEOUT;
700 xbt_dynar_free(&s_comms);
702 if (finished_index != -1) {
703 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
704 /* the communication is finished */
705 comm->status = status;
707 if (status == MSG_OK && comm->task_received != NULL) {
708 /* I am the receiver */
709 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
710 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
711 (*comm->task_received)->simdata->isused = 0;
715 return finished_index;
718 /** \ingroup msg_task_usage
719 * \brief Destroys a communication.
720 * \param comm the communication to destroy.
722 void MSG_comm_destroy(msg_comm_t comm)
727 /** \ingroup msg_task_usage
728 * \brief Wait for the completion of a communication.
730 * It takes two parameters.
731 * \param comm the communication to wait.
732 * \param timeout Wait until the communication terminates or the timeout
733 * occurs. You can provide a -1 timeout to obtain an infinite timeout.
734 * \return msg_error_t
736 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
740 simcall_comm_wait(comm->s_comm, timeout);
742 if (comm->task_received != NULL) {
743 /* I am the receiver */
744 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
745 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
746 (*comm->task_received)->simdata->isused = 0;
749 /* FIXME: these functions are not traceable */
752 switch (e.category) {
754 comm->status = MSG_TRANSFER_FAILURE;
757 comm->status = MSG_TIMEOUT;
768 /** \ingroup msg_task_usage
769 * \brief This function is called by a sender and permit to wait for each communication
771 * \param comm a vector of communication
772 * \param nb_elem is the size of the comm vector
773 * \param timeout for each call of MSG_comm_wait
775 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
778 for (i = 0; i < nb_elem; i++) {
779 MSG_comm_wait(comm[i], timeout);
783 /** \ingroup msg_task_usage
784 * \brief This function waits for the first communication finished in a list.
785 * \param comms a vector of communications
786 * \return the position of the first finished communication
787 * (but it may have failed, use MSG_comm_get_status() to know its status)
789 int MSG_comm_waitany(xbt_dynar_t comms)
792 int finished_index = -1;
794 /* create the equivalent dynar with SIMIX objects */
795 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
798 xbt_dynar_foreach(comms, cursor, comm) {
799 xbt_dynar_push(s_comms, &comm->s_comm);
802 msg_error_t status = MSG_OK;
804 finished_index = simcall_comm_waitany(s_comms);
807 switch (e.category) {
809 finished_index = e.value;
810 status = MSG_TRANSFER_FAILURE;
814 finished_index = e.value;
815 status = MSG_TIMEOUT;
824 xbt_assert(finished_index != -1, "WaitAny returned -1");
825 xbt_dynar_free(&s_comms);
827 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
828 /* the communication is finished */
829 comm->status = status;
831 if (comm->task_received != NULL) {
832 /* I am the receiver */
833 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
834 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
835 (*comm->task_received)->simdata->isused = 0;
838 return finished_index;
842 * \ingroup msg_task_usage
843 * \brief Returns the error (if any) that occured during a finished communication.
844 * \param comm a finished communication
845 * \return the status of the communication, or #MSG_OK if no error occured
846 * during the communication
848 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
853 /** \ingroup msg_task_usage
854 * \brief Get a task (#msg_task_t) from a communication
856 * \param comm the communication where to get the task
857 * \return the task from the communication
859 msg_task_t MSG_comm_get_task(msg_comm_t comm)
861 xbt_assert(comm, "Invalid parameter");
863 return comm->task_received ? *comm->task_received : comm->task_sent;
867 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
868 * \param comm the comm
869 * \param buff the data copied
870 * \param buff_size size of the buffer
872 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
875 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
877 // notify the user callback if any
878 if (msg_global->task_copy_callback) {
879 msg_task_t task = buff;
880 msg_global->task_copy_callback(task,
881 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
885 /** \ingroup msg_task_usage
886 * \brief Sends a task to a mailbox
888 * This is a blocking function, the execution flow will be blocked
889 * until the task is sent (and received in the other side if #MSG_task_receive is used).
890 * See #MSG_task_isend for sending tasks asynchronously.
892 * \param task the task to be sent
893 * \param alias the mailbox name to where the task is sent
895 * \return Returns #MSG_OK if the task was successfully sent,
896 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
898 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
900 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
901 return MSG_task_send_with_timeout(task, alias, -1);
904 /** \ingroup msg_task_usage
905 * \brief Sends a task to a mailbox with a maximum rate
907 * This is a blocking function, the execution flow will be blocked
908 * until the task is sent. The maxrate parameter allows the application
909 * to limit the bandwidth utilization of network links when sending the task.
911 * \param task the task to be sent
912 * \param alias the mailbox name to where the task is sent
913 * \param maxrate the maximum communication rate for sending this task
915 * \return Returns #MSG_OK if the task was successfully sent,
916 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
919 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
921 task->simdata->rate = maxrate;
922 return MSG_task_send(task, alias);
925 /** \ingroup msg_task_usage
926 * \brief Sends a task to a mailbox with a timeout
928 * This is a blocking function, the execution flow will be blocked
929 * until the task is sent or the timeout is achieved.
931 * \param task the task to be sent
932 * \param alias the mailbox name to where the task is sent
933 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
935 * \return Returns #MSG_OK if the task was successfully sent,
936 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
939 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
942 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
946 /** \ingroup msg_task_usage
947 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
949 * This is a blocking function, the execution flow will be blocked
950 * until the task is sent or the timeout is achieved.
952 * \param task the task to be sent
953 * \param alias the mailbox name to where the task is sent
954 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
955 * \param maxrate the maximum communication rate for sending this task
957 * \return Returns #MSG_OK if the task was successfully sent,
958 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
961 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
962 double timeout, double maxrate)
964 task->simdata->rate = maxrate;
965 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
969 /** \ingroup msg_task_usage
970 * \brief Check if there is a communication going on in a mailbox.
972 * \param alias the name of the mailbox to be considered
974 * \return Returns 1 if there is a communication, 0 otherwise
976 int MSG_task_listen(const char *alias)
978 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
981 /** \ingroup msg_task_usage
982 * \brief Check the number of communication actions of a given host pending in a mailbox.
984 * \param alias the name of the mailbox to be considered
985 * \param host the host to check for communication
987 * \return Returns the number of pending communication actions of the host in the
988 * given mailbox, 0 if there is no pending communication actions.
991 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
994 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
998 /** \ingroup msg_task_usage
999 * \brief Look if there is a communication on a mailbox and return the
1000 * PID of the sender process.
1002 * \param alias the name of the mailbox to be considered
1004 * \return Returns the PID of sender process,
1005 * -1 if there is no communication in the mailbox.
1007 int MSG_task_listen_from(const char *alias)
1012 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
1015 return MSG_process_get_PID(task->simdata->sender);
1018 /** \ingroup msg_task_usage
1019 * \brief Sets the tracing category of a task.
1021 * This function should be called after the creation of
1022 * a MSG task, to define the category of that task. The
1023 * first parameter task must contain a task that was
1024 * created with the function #MSG_task_create. The second
1025 * parameter category must contain a category that was
1026 * previously declared with the function #TRACE_category
1027 * (or with #TRACE_category_with_color).
1029 * See \ref tracing for details on how to trace
1030 * the (categorized) resource utilization.
1032 * \param task the task that is going to be categorized
1033 * \param category the name of the category to be associated to the task
1035 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
1037 void MSG_task_set_category (msg_task_t task, const char *category)
1040 TRACE_msg_set_task_category (task, category);
1044 /** \ingroup msg_task_usage
1046 * \brief Gets the current tracing category of a task.
1048 * \param task the task to be considered
1050 * \see MSG_task_set_category
1052 * \return Returns the name of the tracing category of the given task, NULL otherwise
1054 const char *MSG_task_get_category (msg_task_t task)
1057 return task->category;
1064 * \brief Returns the value of a given AS or router property
1066 * \param asr the name of a router or AS
1067 * \param name a property name
1068 * \return value of a property (or NULL if property not set)
1070 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
1072 return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
1076 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
1077 * a the AS or router
1079 * \param asr the name of a router or AS
1080 * \return a dict containing the properties
1082 xbt_dict_t MSG_as_router_get_properties(const char* asr)
1084 return (simcall_asr_get_properties(asr));
1088 * \brief Change the value of a given AS or router
1090 * \param asr the name of a router or AS
1091 * \param name a property name
1092 * \param value what to change the property to
1093 * \param free_ctn the freeing function to use to kill the value on need
1095 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1096 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1099 #ifdef MSG_USE_DEPRECATED
1100 /** \ingroup msg_deprecated_functions
1102 * \brief Return the last value returned by a MSG function (except
1103 * MSG_get_errno...).
1105 msg_error_t MSG_get_errno(void)
1107 return PROCESS_GET_ERRNO();
1110 /** \ingroup msg_deprecated_functions
1111 * \brief Put a task on a channel of an host and waits for the end of the
1114 * This function is used for describing the behavior of a process. It
1115 * takes three parameter.
1116 * \param task a #msg_task_t to send on another location. This task
1117 will not be usable anymore when the function will return. There is
1118 no automatic task duplication and you have to save your parameters
1119 before calling this function. Tasks are unique and once it has been
1120 sent to another location, you should not access it anymore. You do
1121 not need to call MSG_task_destroy() but to avoid using, as an
1122 effect of inattention, this task anymore, you definitely should
1123 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1124 can be transfered iff it has been correctly created with
1126 * \param dest the destination of the message
1127 * \param channel the channel on which the process should put this
1128 task. This value has to be >=0 and < than the maximal number of
1129 channels fixed with MSG_set_channel_number().
1130 * \return #MSG_HOST_FAILURE if the host on which
1131 * this function was called was shut down,
1132 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1133 * (network failure, dest failure) or #MSG_OK if it succeeded.
1135 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
1137 XBT_WARN("DEPRECATED! Now use MSG_task_send");
1138 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1141 /** \ingroup msg_deprecated_functions
1142 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1148 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
1151 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1152 task->simdata->rate = maxrate;
1153 return MSG_task_put(task, dest, channel);
1156 /** \ingroup msg_deprecated_functions
1158 * \brief Put a task on a channel of an
1159 * host (with a timeout on the waiting of the destination host) and
1160 * waits for the end of the transmission.
1162 * This function is used for describing the behavior of a process. It
1163 * takes four parameter.
1164 * \param task a #msg_task_t to send on another location. This task
1165 will not be usable anymore when the function will return. There is
1166 no automatic task duplication and you have to save your parameters
1167 before calling this function. Tasks are unique and once it has been
1168 sent to another location, you should not access it anymore. You do
1169 not need to call MSG_task_destroy() but to avoid using, as an
1170 effect of inattention, this task anymore, you definitely should
1171 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1172 can be transfered iff it has been correctly created with
1174 * \param dest the destination of the message
1175 * \param channel the channel on which the process should put this
1176 task. This value has to be >=0 and < than the maximal number of
1177 channels fixed with MSG_set_channel_number().
1178 * \param timeout the maximum time to wait for a task before giving
1179 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1180 will not be modified
1181 * \return #MSG_HOST_FAILURE if the host on which
1182 this function was called was shut down,
1183 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1184 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1187 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
1188 m_channel_t channel, double timeout)
1190 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1191 xbt_assert((channel >= 0)
1192 && (channel < msg_global->max_channel), "Invalid channel %d",
1195 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", MSG_host_get_name(dest));
1197 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1198 (dest, channel), task, timeout);
1201 /** \ingroup msg_deprecated_functions
1202 * \brief Test whether there is a pending communication on a channel, and who sent it.
1204 * It takes one parameter.
1205 * \param channel the channel on which the process should be
1206 listening. This value has to be >=0 and < than the maximal
1207 number of channels fixed with MSG_set_channel_number().
1208 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1210 int MSG_task_probe_from(m_channel_t channel)
1212 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1215 xbt_assert((channel >= 0)
1216 && (channel < msg_global->max_channel), "Invalid channel %d",
1221 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1222 (MSG_host_self(), channel))))
1225 return MSG_process_get_PID(task->simdata->sender);
1228 /** \ingroup msg_deprecated_functions
1229 * \brief Test whether there is a pending communication on a channel.
1231 * It takes one parameter.
1232 * \param channel the channel on which the process should be
1233 listening. This value has to be >=0 and < than the maximal
1234 number of channels fixed with MSG_set_channel_number().
1235 * \return 1 if there is a pending communication and 0 otherwise
1237 int MSG_task_Iprobe(m_channel_t channel)
1239 XBT_WARN("DEPRECATED!");
1240 xbt_assert((channel >= 0)
1241 && (channel < msg_global->max_channel), "Invalid channel %d",
1245 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1246 (MSG_host_self(), channel));
1249 /** \ingroup msg_deprecated_functions
1251 * \brief Return the number of tasks waiting to be received on a \a
1252 channel and sent by \a host.
1254 * It takes two parameters.
1255 * \param channel the channel on which the process should be
1256 listening. This value has to be >=0 and < than the maximal
1257 number of channels fixed with MSG_set_channel_number().
1258 * \param host the host that is to be watched.
1259 * \return the number of tasks waiting to be received on \a channel
1260 and sent by \a host.
1262 int MSG_task_probe_from_host(int channel, msg_host_t host)
1264 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1265 xbt_assert((channel >= 0)
1266 && (channel < msg_global->max_channel), "Invalid channel %d",
1270 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1271 (MSG_host_self(), channel),
1276 /** \ingroup msg_deprecated_functions
1277 * \brief Listen on \a channel and waits for receiving a task from \a host.
1279 * It takes three parameters.
1280 * \param task a memory location for storing a #msg_task_t. It will
1281 hold a task when this function will return. Thus \a task should not
1282 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1283 those two condition does not hold, there will be a warning message.
1284 * \param channel the channel on which the process should be
1285 listening. This value has to be >=0 and < than the maximal
1286 number of channels fixed with MSG_set_channel_number().
1287 * \param host the host that is to be watched.
1288 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1291 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1293 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1294 return MSG_task_get_ext(task, channel, -1, host);
1297 /** \ingroup msg_deprecated_functions
1298 * \brief Listen on a channel and wait for receiving a task.
1300 * It takes two parameters.
1301 * \param task a memory location for storing a #msg_task_t. It will
1302 hold a task when this function will return. Thus \a task should not
1303 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1304 those two condition does not hold, there will be a warning message.
1305 * \param channel the channel on which the process should be
1306 listening. This value has to be >=0 and < than the maximal
1307 number of channels fixed with MSG_set_channel_number().
1308 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1310 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1312 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1313 return MSG_task_get_with_timeout(task, channel, -1);
1316 /** \ingroup msg_deprecated_functions
1317 * \brief Listen on a channel and wait for receiving a task with a timeout.
1319 * It takes three parameters.
1320 * \param task a memory location for storing a #msg_task_t. It will
1321 hold a task when this function will return. Thus \a task should not
1322 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1323 those two condition does not hold, there will be a warning message.
1324 * \param channel the channel on which the process should be
1325 listening. This value has to be >=0 and < than the maximal
1326 number of channels fixed with MSG_set_channel_number().
1327 * \param max_duration the maximum time to wait for a task before giving
1328 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1329 will not be modified and will still be
1330 equal to \c NULL when returning.
1331 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1334 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1335 double max_duration)
1337 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1338 return MSG_task_get_ext(task, channel, max_duration, NULL);
1342 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1345 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1346 xbt_assert((channel >= 0)
1347 && (channel < msg_global->max_channel), "Invalid channel %d",
1351 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1352 (MSG_host_self(), channel), task, host,