1 /* Copyright (c) 2004-2012. The SimGrid Team. All rights reserved. */
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. */
6 #include "msg_private.h"
7 #include "msg_mailbox.h"
10 #include "xbt/sysdep.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
13 "Logging specific to MSG (gos)");
15 /** \ingroup msg_task_usage
16 * \brief Executes a task and waits for its termination.
18 * This function is used for describing the behavior of a process. It
19 * 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
22 * or #MSG_HOST_FAILURE otherwise
24 msg_error_t MSG_task_execute(msg_task_t task)
26 /* TODO: add this to other locations */
27 msg_host_t host = MSG_process_get_host(MSG_process_self());
28 MSG_host_add_task(host, task);
30 msg_error_t ret = MSG_parallel_task_execute(task);
32 MSG_host_del_task(host, task);
37 /** \ingroup msg_task_usage
38 * \brief Executes a parallel task and waits for its termination.
40 * \param task a #msg_task_t to execute on the location on which the process is running.
42 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
43 * or #MSG_HOST_FAILURE otherwise
45 msg_error_t MSG_parallel_task_execute(msg_task_t task)
48 simdata_task_t simdata = task->simdata;
49 msg_process_t self = SIMIX_process_self();
50 simdata_process_t p_simdata = SIMIX_process_self_get_data(self);
51 e_smx_state_t comp_state;
52 msg_error_t status = MSG_OK;
55 TRACE_msg_task_execute_start(task);
58 xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
59 "This task is executed somewhere else. Go fix your code! %d",
60 task->simdata->isused);
62 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
64 if (simdata->computation_amount == 0 && !simdata->host_nb) {
66 TRACE_msg_task_execute_end(task);
76 if (simdata->host_nb > 0) {
77 simdata->compute = simcall_host_parallel_execute(task->name,
83 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
85 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));
86 XBT_INFO("execute %s@%s with affinity(0x%04lx)", MSG_task_get_name(task), MSG_host_get_name(p_simdata->m_host), affinity_mask);
88 simdata->compute = simcall_host_execute(task->name,
90 simdata->computation_amount,
98 simcall_set_category(simdata->compute, task->category);
100 p_simdata->waiting_action = simdata->compute;
101 comp_state = simcall_host_execution_wait(simdata->compute);
103 p_simdata->waiting_action = NULL;
107 XBT_DEBUG("Execution task '%s' finished in state %d",
108 task->name, (int)comp_state);
111 switch (e.category) {
113 status = MSG_TASK_CANCELED;
120 /* action ended, set comm and compute = NULL, the actions is already destroyed
121 * in the main function */
122 simdata->computation_amount = 0.0;
123 simdata->comm = NULL;
124 simdata->compute = NULL;
126 TRACE_msg_task_execute_end(task);
133 /** \ingroup msg_task_usage
134 * \brief Sleep for the specified number of seconds
136 * Makes the current process sleep until \a time seconds have elapsed.
138 * \param nb_sec a number of second
140 msg_error_t MSG_process_sleep(double nb_sec)
143 msg_error_t status = MSG_OK;
144 /*msg_process_t proc = MSG_process_self();*/
147 TRACE_msg_process_sleep_in(MSG_process_self());
150 /* create action to sleep */
152 /*proc->simdata->waiting_action = act_sleep;
154 FIXME: check if not setting the waiting_action breaks something on msg
156 proc->simdata->waiting_action = NULL;*/
159 simcall_process_sleep(nb_sec);
162 switch (e.category) {
164 status = MSG_TASK_CANCELED;
173 TRACE_msg_process_sleep_out(MSG_process_self());
178 /** \ingroup msg_task_usage
179 * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
181 * Sorry, this function is not supported anymore. That wouldn't be
182 * impossible to reimplement it, but we are lacking the time to do so ourselves.
183 * If you need this functionality, you can either:
185 * - implement the buffering mechanism on the user-level by queuing all messages
186 * received in the mailbox that do not match your expectation
187 * - change your application logic to leverage the mailboxes features. For example,
188 * if you have A receiving messages from B and C, you could have A waiting on
189 * mailbox "A" most of the time, but on "A#B" when it's waiting for specific
190 * messages from B and "A#C" when waiting for messages from C. You could even get A
191 * sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
192 * an example of use of this function in the @ref MSG_examples section.
193 * - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
194 * very difficult actually. Check the function @ref MSG_mailbox_get_task_ext. During its call to
195 * simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
196 * messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
197 * and that your filtering function will receive as first parameter, and then, the filter could
198 * simply compare the host names, for example. After sufficient testing, provide an example that
199 * we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
201 * \param task a memory location for storing a #msg_task_t.
202 * \param alias name of the mailbox to receive the task from
203 * \param host a #msg_host_t host from where the task was sent
206 * #MSG_OK if the task was successfully received,
207 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
210 MSG_task_receive_from_host(msg_task_t * task, const char *alias,
213 return MSG_task_receive_ext(task, alias, -1, host);
217 *\brief Deprecated function that used to receive a task from a mailbox from a specific host
218 *\brief at a given rate
220 * \param task a memory location for storing a #msg_task_t.
221 * \param alias name of the mailbox to receive the task from
222 * \param host a #msg_host_t host from where the task was sent
223 * \param rate limit the reception to rate bandwidth
226 * #MSG_OK if the task was successfully received,
227 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
230 MSG_task_receive_from_host_bounded(msg_task_t * task, const char *alias,
231 msg_host_t host, double rate)
233 return MSG_task_receive_ext_bounded(task, alias, -1, host, rate);
236 /** \ingroup msg_task_usage
237 * \brief Receives a task from a mailbox.
239 * This is a blocking function, the execution flow will be blocked
240 * until the task is received. See #MSG_task_irecv
241 * for receiving tasks asynchronously.
243 * \param task a memory location for storing a #msg_task_t.
244 * \param alias name of the mailbox to receive the task from
247 * #MSG_OK if the task was successfully received,
248 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
250 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
252 return MSG_task_receive_with_timeout(task, alias, -1);
255 /** \ingroup msg_task_usage
256 * \brief Receives a task from a mailbox at a given rate.
258 * \param task a memory location for storing a #msg_task_t.
259 * \param alias name of the mailbox to receive the task from
260 * \param rate limit the reception to rate bandwidth
263 * #MSG_OK if the task was successfully received,
264 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
266 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
268 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
271 /** \ingroup msg_task_usage
272 * \brief Receives a task from a mailbox with a given timeout.
274 * This is a blocking function with a timeout, the execution flow will be blocked
275 * until the task is received or the timeout is achieved. See #MSG_task_irecv
276 * for receiving tasks asynchronously. You can provide a -1 timeout
277 * to obtain an infinite timeout.
279 * \param task a memory location for storing a #msg_task_t.
280 * \param alias name of the mailbox to receive the task from
281 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
284 * #MSG_OK if the task was successfully received,
285 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
288 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
291 return MSG_task_receive_ext(task, alias, timeout, NULL);
294 /** \ingroup msg_task_usage
295 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
297 * \param task a memory location for storing a #msg_task_t.
298 * \param alias name of the mailbox to receive the task from
299 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
300 * \param rate limit the reception to rate bandwidth
303 * #MSG_OK if the task was successfully received,
304 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
307 MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias,
308 double timeout,double rate)
310 return MSG_task_receive_ext_bounded(task, alias, timeout, NULL,rate);
313 /** \ingroup msg_task_usage
314 * \brief Receives a task from a mailbox from a specific host with a given timeout.
316 * This is a blocking function with a timeout, the execution flow will be blocked
317 * until the task is received or the timeout is achieved. See #MSG_task_irecv
318 * for receiving tasks asynchronously. You can provide a -1 timeout
319 * to obtain an infinite timeout.
321 * \param task a memory location for storing a #msg_task_t.
322 * \param alias name of the mailbox to receive the task from
323 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
324 * \param host a #msg_host_t host from where the task was sent
327 * #MSG_OK if the task was successfully received,
328 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
331 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
335 msg_error_t ret = MSG_OK;
337 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
340 ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
344 switch (e.category) {
345 case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */
346 ret = MSG_HOST_FAILURE;
356 /** \ingroup msg_task_usage
357 * \brief Receives a task from a mailbox from a specific host with a given timeout
358 * and at a given rate.
360 * \param task a memory location for storing a #msg_task_t.
361 * \param alias name of the mailbox to receive the task from
362 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
363 * \param host a #msg_host_t host from where the task was sent
364 * \param rate limit the reception to rate bandwidth
367 * #MSG_OK if the task was successfully received,
368 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
371 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
372 msg_host_t host, double rate)
375 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
377 return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
378 host, timeout, rate);
381 /** \ingroup msg_task_usage
382 * \brief Sends a task on a mailbox.
384 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
385 * to end the communication.
387 * \param task a #msg_task_t to send on another location.
388 * \param alias name of the mailbox to sent the task to
389 * \return the msg_comm_t communication created
391 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
393 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
396 /** \ingroup msg_task_usage
397 * \brief Sends a task on a mailbox with a maximum rate
399 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
400 * to end the communication. The maxrate parameter allows the application
401 * to limit the bandwidth utilization of network links when sending the task.
403 * \param task a #msg_task_t to send on another location.
404 * \param alias name of the mailbox to sent the task to
405 * \param maxrate the maximum communication rate for sending this task .
406 * \return the msg_comm_t communication created
408 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
410 task->simdata->rate = maxrate;
411 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
415 /** \ingroup msg_task_usage
416 * \brief Sends a task on a mailbox, with support for matching requests
418 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
419 * to end the communication.
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 match_fun boolean function which parameters are:
424 * - match_data_provided_here
425 * - match_data_provided_by_other_side_if_any
426 * - the_smx_action_describing_the_other_side
427 * \param match_data user provided data passed to match_fun
428 * \return the msg_comm_t communication created
430 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
431 int (*match_fun)(void*,void*, smx_action_t),
434 simdata_task_t t_simdata = NULL;
435 msg_process_t process = MSG_process_self();
436 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
439 int call_end = TRACE_msg_task_put_start(task);
442 /* Prepare the task to send */
443 t_simdata = task->simdata;
444 t_simdata->sender = process;
445 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
447 xbt_assert(t_simdata->isused == 0,
448 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
450 t_simdata->isused = 1;
451 t_simdata->comm = NULL;
452 msg_global->sent_msg++;
454 /* Send it by calling SIMIX network layer */
455 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
456 comm->task_sent = task;
457 comm->task_received = NULL;
458 comm->status = MSG_OK;
460 simcall_comm_isend(mailbox, t_simdata->message_size,
461 t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
462 t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
464 if (TRACE_is_enabled()) {
465 simcall_set_category(comm->s_comm, task->category);
471 TRACE_msg_task_put_end();
477 /** \ingroup msg_task_usage
478 * \brief Sends a task on a mailbox.
480 * This is a non blocking detached send function.
481 * Think of it as a best effort send. Keep in mind that the third parameter
482 * is only called if the communication fails. If the communication does work,
483 * it is responsibility of the receiver code to free anything related to
484 * the task, as usual. More details on this can be obtained on
485 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
486 * in the SimGrid-user mailing list archive.
488 * \param task a #msg_task_t to send on another location.
489 * \param alias name of the mailbox to sent the task to
490 * \param cleanup a function to destroy the task if the
491 * communication fails, e.g. MSG_task_destroy
492 * (if NULL, no function will be called)
494 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
496 simdata_task_t t_simdata = NULL;
497 msg_process_t process = MSG_process_self();
498 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
500 /* Prepare the task to send */
501 t_simdata = task->simdata;
502 t_simdata->sender = process;
503 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
505 xbt_assert(t_simdata->isused == 0,
506 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
508 t_simdata->isused = 1;
509 t_simdata->comm = NULL;
510 msg_global->sent_msg++;
513 int call_end = TRACE_msg_task_put_start(task);
516 /* Send it by calling SIMIX network layer */
517 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
518 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
519 t_simdata->comm = comm;
521 if (TRACE_is_enabled()) {
522 simcall_set_category(comm, task->category);
528 TRACE_msg_task_put_end();
533 /** \ingroup msg_task_usage
534 * \brief Sends a task on a mailbox with a maximal rate.
536 * This is a non blocking detached send function.
537 * Think of it as a best effort send. Keep in mind that the third parameter
538 * is only called if the communication fails. If the communication does work,
539 * it is responsibility of the receiver code to free anything related to
540 * the task, as usual. More details on this can be obtained on
541 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
542 * in the SimGrid-user mailing list archive.
544 * \param task a #msg_task_t to send on another location.
545 * \param alias name of the mailbox to sent the task to
546 * \param cleanup a function to destroy the task if the
547 * communication fails, e.g. MSG_task_destroy
548 * (if NULL, no function will be called)
549 * \param maxrate the maximum communication rate for sending this task
552 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
554 task->simdata->rate = maxrate;
556 simdata_task_t t_simdata = NULL;
557 msg_process_t process = MSG_process_self();
558 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
560 /* Prepare the task to send */
561 t_simdata = task->simdata;
562 t_simdata->sender = process;
563 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
565 xbt_assert(t_simdata->isused == 0,
566 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
568 t_simdata->isused = 1;
569 t_simdata->comm = NULL;
570 msg_global->sent_msg++;
573 int call_end = TRACE_msg_task_put_start(task);
576 /* Send it by calling SIMIX network layer */
577 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
578 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
579 t_simdata->comm = comm;
581 if (TRACE_is_enabled()) {
582 simcall_set_category(comm, task->category);
588 TRACE_msg_task_put_end();
592 /** \ingroup msg_task_usage
593 * \brief Starts listening for receiving a task from an asynchronous communication.
595 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
596 * to end the communication.
598 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
599 * \param name of the mailbox to receive the task on
600 * \return the msg_comm_t communication created
602 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
604 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
606 /* FIXME: these functions are not traceable */
609 xbt_assert(task, "Null pointer for the task storage");
613 ("MSG_task_irecv() was asked to write in a non empty task struct.");
615 /* Try to receive it by calling SIMIX network layer */
616 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
617 comm->task_sent = NULL;
618 comm->task_received = task;
619 comm->status = MSG_OK;
620 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
625 /** \ingroup msg_task_usage
626 * \brief Starts listening for receiving a task from an asynchronous communication
629 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
630 * \param name of the mailbox to receive the task on
631 * \param rate limit the bandwidth to the given rate
632 * \return the msg_comm_t communication created
634 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
638 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
640 /* FIXME: these functions are not traceable */
643 xbt_assert(task, "Null pointer for the task storage");
647 ("MSG_task_irecv() was asked to write in a non empty task struct.");
649 /* Try to receive it by calling SIMIX network layer */
650 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
651 comm->task_sent = NULL;
652 comm->task_received = task;
653 comm->status = MSG_OK;
654 comm->s_comm = simcall_comm_irecv_bounded(rdv, task, NULL, NULL, NULL, rate);
659 /** \ingroup msg_task_usage
660 * \brief Checks whether a communication is done, and if yes, finalizes it.
661 * \param comm the communication to test
662 * \return TRUE if the communication is finished
663 * (but it may have failed, use MSG_comm_get_status() to know its status)
664 * or FALSE if the communication is not finished yet
665 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
667 int MSG_comm_test(msg_comm_t comm)
673 finished = simcall_comm_test(comm->s_comm);
675 if (finished && comm->task_received != NULL) {
676 /* I am the receiver */
677 (*comm->task_received)->simdata->isused = 0;
681 switch (e.category) {
683 comm->status = MSG_TRANSFER_FAILURE;
688 comm->status = MSG_TIMEOUT;
701 /** \ingroup msg_task_usage
702 * \brief This function checks if a communication is finished.
703 * \param comms a vector of communications
704 * \return the position of the finished communication if any
705 * (but it may have failed, use MSG_comm_get_status() to know its status),
706 * or -1 if none is finished
708 int MSG_comm_testany(xbt_dynar_t comms)
711 int finished_index = -1;
713 /* create the equivalent dynar with SIMIX objects */
714 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
717 xbt_dynar_foreach(comms, cursor, comm) {
718 xbt_dynar_push(s_comms, &comm->s_comm);
721 msg_error_t status = MSG_OK;
723 finished_index = simcall_comm_testany(s_comms);
726 switch (e.category) {
728 finished_index = e.value;
729 status = MSG_TRANSFER_FAILURE;
733 finished_index = e.value;
734 status = MSG_TIMEOUT;
742 xbt_dynar_free(&s_comms);
744 if (finished_index != -1) {
745 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
746 /* the communication is finished */
747 comm->status = status;
749 if (status == MSG_OK && comm->task_received != NULL) {
750 /* I am the receiver */
751 (*comm->task_received)->simdata->isused = 0;
755 return finished_index;
758 /** \ingroup msg_task_usage
759 * \brief Destroys a communication.
760 * \param comm the communication to destroy.
762 void MSG_comm_destroy(msg_comm_t comm)
767 /** \ingroup msg_task_usage
768 * \brief Wait for the completion of a communication.
770 * It takes two parameters.
771 * \param comm the communication to wait.
772 * \param timeout Wait until the communication terminates or the timeout
773 * occurs. You can provide a -1 timeout to obtain an infinite timeout.
774 * \return msg_error_t
776 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
780 simcall_comm_wait(comm->s_comm, timeout);
782 if (comm->task_received != NULL) {
783 /* I am the receiver */
784 (*comm->task_received)->simdata->isused = 0;
787 /* FIXME: these functions are not traceable */
790 switch (e.category) {
792 comm->status = MSG_TRANSFER_FAILURE;
795 comm->status = MSG_TIMEOUT;
806 /** \ingroup msg_task_usage
807 * \brief This function is called by a sender and permit to wait for each communication
809 * \param comm a vector of communication
810 * \param nb_elem is the size of the comm vector
811 * \param timeout for each call of MSG_comm_wait
813 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
816 for (i = 0; i < nb_elem; i++) {
817 MSG_comm_wait(comm[i], timeout);
821 /** \ingroup msg_task_usage
822 * \brief This function waits for the first communication finished in a list.
823 * \param comms a vector of communications
824 * \return the position of the first finished communication
825 * (but it may have failed, use MSG_comm_get_status() to know its status)
827 int MSG_comm_waitany(xbt_dynar_t comms)
830 int finished_index = -1;
832 /* create the equivalent dynar with SIMIX objects */
833 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
836 xbt_dynar_foreach(comms, cursor, comm) {
837 xbt_dynar_push(s_comms, &comm->s_comm);
840 msg_error_t status = MSG_OK;
842 finished_index = simcall_comm_waitany(s_comms);
845 switch (e.category) {
847 finished_index = e.value;
848 status = MSG_TRANSFER_FAILURE;
852 finished_index = e.value;
853 status = MSG_TIMEOUT;
862 xbt_assert(finished_index != -1, "WaitAny returned -1");
863 xbt_dynar_free(&s_comms);
865 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
866 /* the communication is finished */
867 comm->status = status;
869 if (comm->task_received != NULL) {
870 /* I am the receiver */
871 (*comm->task_received)->simdata->isused = 0;
874 return finished_index;
878 * \ingroup msg_task_usage
879 * \brief Returns the error (if any) that occured during a finished communication.
880 * \param comm a finished communication
881 * \return the status of the communication, or #MSG_OK if no error occured
882 * during the communication
884 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
889 /** \ingroup msg_task_usage
890 * \brief Get a task (#msg_task_t) from a communication
892 * \param comm the communication where to get the task
893 * \return the task from the communication
895 msg_task_t MSG_comm_get_task(msg_comm_t comm)
897 xbt_assert(comm, "Invalid parameter");
899 return comm->task_received ? *comm->task_received : comm->task_sent;
903 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
904 * \param comm the comm
905 * \param buff the data copied
906 * \param buff_size size of the buffer
908 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
911 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
913 // notify the user callback if any
914 if (msg_global->task_copy_callback) {
915 msg_task_t task = buff;
916 msg_global->task_copy_callback(task,
917 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
921 /** \ingroup msg_task_usage
922 * \brief Sends a task to a mailbox
924 * This is a blocking function, the execution flow will be blocked
925 * until the task is sent (and received in the other side if #MSG_task_receive is used).
926 * See #MSG_task_isend for sending tasks asynchronously.
928 * \param task the task to be sent
929 * \param alias the mailbox name to where the task is sent
931 * \return Returns #MSG_OK if the task was successfully sent,
932 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
934 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
936 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
937 return MSG_task_send_with_timeout(task, alias, -1);
940 /** \ingroup msg_task_usage
941 * \brief Sends a task to a mailbox with a maximum rate
943 * This is a blocking function, the execution flow will be blocked
944 * until the task is sent. The maxrate parameter allows the application
945 * to limit the bandwidth utilization of network links when sending the task.
947 * \param task the task to be sent
948 * \param alias the mailbox name to where the task is sent
949 * \param maxrate the maximum communication rate for sending this task
951 * \return Returns #MSG_OK if the task was successfully sent,
952 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
955 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
957 task->simdata->rate = maxrate;
958 return MSG_task_send(task, alias);
961 /** \ingroup msg_task_usage
962 * \brief Sends a task to a mailbox with a timeout
964 * This is a blocking function, the execution flow will be blocked
965 * until the task is sent or the timeout is achieved.
967 * \param task the task to be sent
968 * \param alias the mailbox name to where the task is sent
969 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
971 * \return Returns #MSG_OK if the task was successfully sent,
972 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
975 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
978 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
982 /** \ingroup msg_task_usage
983 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
985 * This is a blocking function, the execution flow will be blocked
986 * until the task is sent or the timeout is achieved.
988 * \param task the task to be sent
989 * \param alias the mailbox name to where the task is sent
990 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
991 * \param maxrate the maximum communication rate for sending this task
993 * \return Returns #MSG_OK if the task was successfully sent,
994 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
997 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
998 double timeout, double maxrate)
1000 task->simdata->rate = maxrate;
1001 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
1005 /** \ingroup msg_task_usage
1006 * \brief Check if there is a communication going on in a mailbox.
1008 * \param alias the name of the mailbox to be considered
1010 * \return Returns 1 if there is a communication, 0 otherwise
1012 int MSG_task_listen(const char *alias)
1014 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
1017 /** \ingroup msg_task_usage
1018 * \brief Check the number of communication actions of a given host pending in a mailbox.
1020 * \param alias the name of the mailbox to be considered
1021 * \param host the host to check for communication
1023 * \return Returns the number of pending communication actions of the host in the
1024 * given mailbox, 0 if there is no pending communication actions.
1027 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
1030 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
1034 /** \ingroup msg_task_usage
1035 * \brief Look if there is a communication on a mailbox and return the
1036 * PID of the sender process.
1038 * \param alias the name of the mailbox to be considered
1040 * \return Returns the PID of sender process,
1041 * -1 if there is no communication in the mailbox.
1043 int MSG_task_listen_from(const char *alias)
1048 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
1051 return MSG_process_get_PID(task->simdata->sender);
1054 /** \ingroup msg_task_usage
1055 * \brief Sets the tracing category of a task.
1057 * This function should be called after the creation of
1058 * a MSG task, to define the category of that task. The
1059 * first parameter task must contain a task that was
1060 * created with the function #MSG_task_create. The second
1061 * parameter category must contain a category that was
1062 * previously declared with the function #TRACE_category
1063 * (or with #TRACE_category_with_color).
1065 * See \ref tracing for details on how to trace
1066 * the (categorized) resource utilization.
1068 * \param task the task that is going to be categorized
1069 * \param category the name of the category to be associated to the task
1071 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
1073 void MSG_task_set_category (msg_task_t task, const char *category)
1076 TRACE_msg_set_task_category (task, category);
1080 /** \ingroup msg_task_usage
1082 * \brief Gets the current tracing category of a task.
1084 * \param task the task to be considered
1086 * \see MSG_task_set_category
1088 * \return Returns the name of the tracing category of the given task, NULL otherwise
1090 const char *MSG_task_get_category (msg_task_t task)
1093 return task->category;
1100 * \brief Returns the value of a given AS or router property
1102 * \param asr the name of a router or AS
1103 * \param name a property name
1104 * \return value of a property (or NULL if property not set)
1106 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
1108 return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
1112 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
1113 * a the AS or router
1115 * \param asr the name of a router or AS
1116 * \return a dict containing the properties
1118 xbt_dict_t MSG_as_router_get_properties(const char* asr)
1120 return (simcall_asr_get_properties(asr));
1124 * \brief Change the value of a given AS or router
1126 * \param asr the name of a router or AS
1127 * \param name a property name
1128 * \param value what to change the property to
1129 * \param free_ctn the freeing function to use to kill the value on need
1131 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1132 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1135 #ifdef MSG_USE_DEPRECATED
1136 /** \ingroup msg_deprecated_functions
1138 * \brief Return the last value returned by a MSG function (except
1139 * MSG_get_errno...).
1141 msg_error_t MSG_get_errno(void)
1143 return PROCESS_GET_ERRNO();
1146 /** \ingroup msg_deprecated_functions
1147 * \brief Put a task on a channel of an host and waits for the end of the
1150 * This function is used for describing the behavior of a process. It
1151 * takes three parameter.
1152 * \param task a #msg_task_t to send on another location. This task
1153 will not be usable anymore when the function will return. There is
1154 no automatic task duplication and you have to save your parameters
1155 before calling this function. Tasks are unique and once it has been
1156 sent to another location, you should not access it anymore. You do
1157 not need to call MSG_task_destroy() but to avoid using, as an
1158 effect of inattention, this task anymore, you definitely should
1159 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1160 can be transfered iff it has been correctly created with
1162 * \param dest the destination of the message
1163 * \param channel the channel on which the process should put this
1164 task. This value has to be >=0 and < than the maximal number of
1165 channels fixed with MSG_set_channel_number().
1166 * \return #MSG_HOST_FAILURE if the host on which
1167 * this function was called was shut down,
1168 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1169 * (network failure, dest failure) or #MSG_OK if it succeeded.
1171 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
1173 XBT_WARN("DEPRECATED! Now use MSG_task_send");
1174 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1177 /** \ingroup msg_deprecated_functions
1178 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1184 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
1187 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1188 task->simdata->rate = maxrate;
1189 return MSG_task_put(task, dest, channel);
1192 /** \ingroup msg_deprecated_functions
1194 * \brief Put a task on a channel of an
1195 * host (with a timeout on the waiting of the destination host) and
1196 * waits for the end of the transmission.
1198 * This function is used for describing the behavior of a process. It
1199 * takes four parameter.
1200 * \param task a #msg_task_t to send on another location. This task
1201 will not be usable anymore when the function will return. There is
1202 no automatic task duplication and you have to save your parameters
1203 before calling this function. Tasks are unique and once it has been
1204 sent to another location, you should not access it anymore. You do
1205 not need to call MSG_task_destroy() but to avoid using, as an
1206 effect of inattention, this task anymore, you definitely should
1207 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1208 can be transfered iff it has been correctly created with
1210 * \param dest the destination of the message
1211 * \param channel the channel on which the process should put this
1212 task. This value has to be >=0 and < than the maximal number of
1213 channels fixed with MSG_set_channel_number().
1214 * \param timeout the maximum time to wait for a task before giving
1215 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1216 will not be modified
1217 * \return #MSG_HOST_FAILURE if the host on which
1218 this function was called was shut down,
1219 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1220 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1223 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
1224 m_channel_t channel, double timeout)
1226 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1227 xbt_assert((channel >= 0)
1228 && (channel < msg_global->max_channel), "Invalid channel %d",
1231 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
1233 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1234 (dest, channel), task, timeout);
1237 /** \ingroup msg_deprecated_functions
1238 * \brief Test whether there is a pending communication on a channel, and who sent it.
1240 * It takes one parameter.
1241 * \param channel the channel on which the process should be
1242 listening. This value has to be >=0 and < than the maximal
1243 number of channels fixed with MSG_set_channel_number().
1244 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1246 int MSG_task_probe_from(m_channel_t channel)
1248 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1251 xbt_assert((channel >= 0)
1252 && (channel < msg_global->max_channel), "Invalid channel %d",
1257 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1258 (MSG_host_self(), channel))))
1261 return MSG_process_get_PID(task->simdata->sender);
1264 /** \ingroup msg_deprecated_functions
1265 * \brief Test whether there is a pending communication on a channel.
1267 * It takes one parameter.
1268 * \param channel the channel on which the process should be
1269 listening. This value has to be >=0 and < than the maximal
1270 number of channels fixed with MSG_set_channel_number().
1271 * \return 1 if there is a pending communication and 0 otherwise
1273 int MSG_task_Iprobe(m_channel_t channel)
1275 XBT_WARN("DEPRECATED!");
1276 xbt_assert((channel >= 0)
1277 && (channel < msg_global->max_channel), "Invalid channel %d",
1281 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1282 (MSG_host_self(), channel));
1285 /** \ingroup msg_deprecated_functions
1287 * \brief Return the number of tasks waiting to be received on a \a
1288 channel and sent by \a host.
1290 * It takes two parameters.
1291 * \param channel the channel on which the process should be
1292 listening. This value has to be >=0 and < than the maximal
1293 number of channels fixed with MSG_set_channel_number().
1294 * \param host the host that is to be watched.
1295 * \return the number of tasks waiting to be received on \a channel
1296 and sent by \a host.
1298 int MSG_task_probe_from_host(int channel, msg_host_t host)
1300 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1301 xbt_assert((channel >= 0)
1302 && (channel < msg_global->max_channel), "Invalid channel %d",
1306 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1307 (MSG_host_self(), channel),
1312 /** \ingroup msg_deprecated_functions
1313 * \brief Listen on \a channel and waits for receiving a task from \a host.
1315 * It takes three parameters.
1316 * \param task a memory location for storing a #msg_task_t. It will
1317 hold a task when this function will return. Thus \a task should not
1318 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1319 those two condition does not hold, there will be a warning message.
1320 * \param channel the channel on which the process should be
1321 listening. This value has to be >=0 and < than the maximal
1322 number of channels fixed with MSG_set_channel_number().
1323 * \param host the host that is to be watched.
1324 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1327 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1329 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1330 return MSG_task_get_ext(task, channel, -1, host);
1333 /** \ingroup msg_deprecated_functions
1334 * \brief Listen on a channel and wait for receiving a task.
1336 * It takes two parameters.
1337 * \param task a memory location for storing a #msg_task_t. It will
1338 hold a task when this function will return. Thus \a task should not
1339 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1340 those two condition does not hold, there will be a warning message.
1341 * \param channel the channel on which the process should be
1342 listening. This value has to be >=0 and < than the maximal
1343 number of channels fixed with MSG_set_channel_number().
1344 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1346 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1348 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1349 return MSG_task_get_with_timeout(task, channel, -1);
1352 /** \ingroup msg_deprecated_functions
1353 * \brief Listen on a channel and wait for receiving a task with a timeout.
1355 * It takes three parameters.
1356 * \param task a memory location for storing a #msg_task_t. It will
1357 hold a task when this function will return. Thus \a task should not
1358 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1359 those two condition does not hold, there will be a warning message.
1360 * \param channel the channel on which the process should be
1361 listening. This value has to be >=0 and < than the maximal
1362 number of channels fixed with MSG_set_channel_number().
1363 * \param max_duration the maximum time to wait for a task before giving
1364 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1365 will not be modified and will still be
1366 equal to \c NULL when returning.
1367 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1370 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1371 double max_duration)
1373 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1374 return MSG_task_get_ext(task, channel, max_duration, NULL);
1378 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1381 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1382 xbt_assert((channel >= 0)
1383 && (channel < msg_global->max_channel), "Invalid channel %d",
1387 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1388 (MSG_host_self(), channel), task, host,