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 simdata->compute = simcall_host_execute(task->name,
87 simdata->computation_amount,
90 simdata->affinity_mask
95 simcall_set_category(simdata->compute, task->category);
97 p_simdata->waiting_action = simdata->compute;
98 comp_state = simcall_host_execution_wait(simdata->compute);
100 p_simdata->waiting_action = NULL;
104 XBT_DEBUG("Execution task '%s' finished in state %d",
105 task->name, (int)comp_state);
108 switch (e.category) {
110 status = MSG_TASK_CANCELED;
117 /* action ended, set comm and compute = NULL, the actions is already destroyed
118 * in the main function */
119 simdata->computation_amount = 0.0;
120 simdata->comm = NULL;
121 simdata->compute = NULL;
123 TRACE_msg_task_execute_end(task);
130 /** \ingroup msg_task_usage
131 * \brief Sleep for the specified number of seconds
133 * Makes the current process sleep until \a time seconds have elapsed.
135 * \param nb_sec a number of second
137 msg_error_t MSG_process_sleep(double nb_sec)
140 msg_error_t status = MSG_OK;
141 /*msg_process_t proc = MSG_process_self();*/
144 TRACE_msg_process_sleep_in(MSG_process_self());
147 /* create action to sleep */
149 /*proc->simdata->waiting_action = act_sleep;
151 FIXME: check if not setting the waiting_action breaks something on msg
153 proc->simdata->waiting_action = NULL;*/
156 simcall_process_sleep(nb_sec);
159 switch (e.category) {
161 status = MSG_TASK_CANCELED;
170 TRACE_msg_process_sleep_out(MSG_process_self());
175 /** \ingroup msg_task_usage
176 * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
178 * Sorry, this function is not supported anymore. That wouldn't be
179 * impossible to reimplement it, but we are lacking the time to do so ourselves.
180 * If you need this functionality, you can either:
182 * - implement the buffering mechanism on the user-level by queuing all messages
183 * received in the mailbox that do not match your expectation
184 * - change your application logic to leverage the mailboxes features. For example,
185 * if you have A receiving messages from B and C, you could have A waiting on
186 * mailbox "A" most of the time, but on "A#B" when it's waiting for specific
187 * messages from B and "A#C" when waiting for messages from C. You could even get A
188 * sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
189 * an example of use of this function in the @ref MSG_examples section.
190 * - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
191 * very difficult actually. Check the function @ref MSG_mailbox_get_task_ext. During its call to
192 * simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
193 * messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
194 * and that your filtering function will receive as first parameter, and then, the filter could
195 * simply compare the host names, for example. After sufficient testing, provide an example that
196 * we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
198 * \param task a memory location for storing a #msg_task_t.
199 * \param alias name of the mailbox to receive the task from
200 * \param host a #msg_host_t host from where the task was sent
203 * #MSG_OK if the task was successfully received,
204 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
207 MSG_task_receive_from_host(msg_task_t * task, const char *alias,
210 return MSG_task_receive_ext(task, alias, -1, host);
214 *\brief Deprecated function that used to receive a task from a mailbox from a specific host
215 *\brief at a given rate
217 * \param task a memory location for storing a #msg_task_t.
218 * \param alias name of the mailbox to receive the task from
219 * \param host a #msg_host_t host from where the task was sent
220 * \param rate limit the reception to rate bandwidth
223 * #MSG_OK if the task was successfully received,
224 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
227 MSG_task_receive_from_host_bounded(msg_task_t * task, const char *alias,
228 msg_host_t host, double rate)
230 return MSG_task_receive_ext_bounded(task, alias, -1, host, rate);
233 /** \ingroup msg_task_usage
234 * \brief Receives a task from a mailbox.
236 * This is a blocking function, the execution flow will be blocked
237 * until the task is received. See #MSG_task_irecv
238 * for receiving tasks asynchronously.
240 * \param task a memory location for storing a #msg_task_t.
241 * \param alias name of the mailbox to receive the task from
244 * #MSG_OK if the task was successfully received,
245 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
247 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
249 return MSG_task_receive_with_timeout(task, alias, -1);
252 /** \ingroup msg_task_usage
253 * \brief Receives a task from a mailbox at a given rate.
255 * \param task a memory location for storing a #msg_task_t.
256 * \param alias name of the mailbox to receive the task from
257 * \param rate limit the reception to rate bandwidth
260 * #MSG_OK if the task was successfully received,
261 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
263 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
265 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
268 /** \ingroup msg_task_usage
269 * \brief Receives a task from a mailbox with a given timeout.
271 * This is a blocking function with a timeout, the execution flow will be blocked
272 * until the task is received or the timeout is achieved. See #MSG_task_irecv
273 * for receiving tasks asynchronously. You can provide a -1 timeout
274 * to obtain an infinite timeout.
276 * \param task a memory location for storing a #msg_task_t.
277 * \param alias name of the mailbox to receive the task from
278 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
281 * #MSG_OK if the task was successfully received,
282 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
285 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
288 return MSG_task_receive_ext(task, alias, timeout, NULL);
291 /** \ingroup msg_task_usage
292 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
294 * \param task a memory location for storing a #msg_task_t.
295 * \param alias name of the mailbox to receive the task from
296 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
297 * \param rate limit the reception to rate bandwidth
300 * #MSG_OK if the task was successfully received,
301 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
304 MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias,
305 double timeout,double rate)
307 return MSG_task_receive_ext_bounded(task, alias, timeout, NULL,rate);
310 /** \ingroup msg_task_usage
311 * \brief Receives a task from a mailbox from a specific host with a given timeout.
313 * This is a blocking function with a timeout, the execution flow will be blocked
314 * until the task is received or the timeout is achieved. See #MSG_task_irecv
315 * for receiving tasks asynchronously. You can provide a -1 timeout
316 * to obtain an infinite timeout.
318 * \param task a memory location for storing a #msg_task_t.
319 * \param alias name of the mailbox to receive the task from
320 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
321 * \param host a #msg_host_t host from where the task was sent
324 * #MSG_OK if the task was successfully received,
325 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
328 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
332 msg_error_t ret = MSG_OK;
334 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
337 ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
341 switch (e.category) {
342 case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */
343 ret = MSG_HOST_FAILURE;
353 /** \ingroup msg_task_usage
354 * \brief Receives a task from a mailbox from a specific host with a given timeout
355 * and at a given rate.
357 * \param task a memory location for storing a #msg_task_t.
358 * \param alias name of the mailbox to receive the task from
359 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
360 * \param host a #msg_host_t host from where the task was sent
361 * \param rate limit the reception to rate bandwidth
364 * #MSG_OK if the task was successfully received,
365 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
368 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
369 msg_host_t host, double rate)
372 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
374 return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
375 host, timeout, rate);
378 /** \ingroup msg_task_usage
379 * \brief Sends a task on a mailbox.
381 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
382 * to end the communication.
384 * \param task a #msg_task_t to send on another location.
385 * \param alias name of the mailbox to sent the task to
386 * \return the msg_comm_t communication created
388 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
390 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
393 /** \ingroup msg_task_usage
394 * \brief Sends a task on a mailbox with a maximum rate
396 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
397 * to end the communication. The maxrate parameter allows the application
398 * to limit the bandwidth utilization of network links when sending the task.
400 * \param task a #msg_task_t to send on another location.
401 * \param alias name of the mailbox to sent the task to
402 * \param maxrate the maximum communication rate for sending this task .
403 * \return the msg_comm_t communication created
405 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
407 task->simdata->rate = maxrate;
408 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
412 /** \ingroup msg_task_usage
413 * \brief Sends a task on a mailbox, with support for matching requests
415 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
416 * to end the communication.
418 * \param task a #msg_task_t to send on another location.
419 * \param alias name of the mailbox to sent the task to
420 * \param match_fun boolean function which parameters are:
421 * - match_data_provided_here
422 * - match_data_provided_by_other_side_if_any
423 * - the_smx_action_describing_the_other_side
424 * \param match_data user provided data passed to match_fun
425 * \return the msg_comm_t communication created
427 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
428 int (*match_fun)(void*,void*, smx_action_t),
431 simdata_task_t t_simdata = NULL;
432 msg_process_t process = MSG_process_self();
433 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
436 int call_end = TRACE_msg_task_put_start(task);
439 /* Prepare the task to send */
440 t_simdata = task->simdata;
441 t_simdata->sender = process;
442 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
444 xbt_assert(t_simdata->isused == 0,
445 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
447 t_simdata->isused = 1;
448 t_simdata->comm = NULL;
449 msg_global->sent_msg++;
451 /* Send it by calling SIMIX network layer */
452 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
453 comm->task_sent = task;
454 comm->task_received = NULL;
455 comm->status = MSG_OK;
457 simcall_comm_isend(mailbox, t_simdata->message_size,
458 t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
459 t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
461 if (TRACE_is_enabled()) {
462 simcall_set_category(comm->s_comm, task->category);
468 TRACE_msg_task_put_end();
474 /** \ingroup msg_task_usage
475 * \brief Sends a task on a mailbox.
477 * This is a non blocking detached send function.
478 * Think of it as a best effort send. Keep in mind that the third parameter
479 * is only called if the communication fails. If the communication does work,
480 * it is responsibility of the receiver code to free anything related to
481 * the task, as usual. More details on this can be obtained on
482 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
483 * in the SimGrid-user mailing list archive.
485 * \param task a #msg_task_t to send on another location.
486 * \param alias name of the mailbox to sent the task to
487 * \param cleanup a function to destroy the task if the
488 * communication fails, e.g. MSG_task_destroy
489 * (if NULL, no function will be called)
491 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
493 simdata_task_t t_simdata = NULL;
494 msg_process_t process = MSG_process_self();
495 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
497 /* Prepare the task to send */
498 t_simdata = task->simdata;
499 t_simdata->sender = process;
500 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
502 xbt_assert(t_simdata->isused == 0,
503 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
505 t_simdata->isused = 1;
506 t_simdata->comm = NULL;
507 msg_global->sent_msg++;
510 int call_end = TRACE_msg_task_put_start(task);
513 /* Send it by calling SIMIX network layer */
514 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
515 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
516 t_simdata->comm = comm;
518 if (TRACE_is_enabled()) {
519 simcall_set_category(comm, task->category);
525 TRACE_msg_task_put_end();
530 /** \ingroup msg_task_usage
531 * \brief Sends a task on a mailbox with a maximal rate.
533 * This is a non blocking detached send function.
534 * Think of it as a best effort send. Keep in mind that the third parameter
535 * is only called if the communication fails. If the communication does work,
536 * it is responsibility of the receiver code to free anything related to
537 * the task, as usual. More details on this can be obtained on
538 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
539 * in the SimGrid-user mailing list archive.
541 * \param task a #msg_task_t to send on another location.
542 * \param alias name of the mailbox to sent the task to
543 * \param cleanup a function to destroy the task if the
544 * communication fails, e.g. MSG_task_destroy
545 * (if NULL, no function will be called)
546 * \param maxrate the maximum communication rate for sending this task
549 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
551 task->simdata->rate = maxrate;
553 simdata_task_t t_simdata = NULL;
554 msg_process_t process = MSG_process_self();
555 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
557 /* Prepare the task to send */
558 t_simdata = task->simdata;
559 t_simdata->sender = process;
560 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
562 xbt_assert(t_simdata->isused == 0,
563 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
565 t_simdata->isused = 1;
566 t_simdata->comm = NULL;
567 msg_global->sent_msg++;
570 int call_end = TRACE_msg_task_put_start(task);
573 /* Send it by calling SIMIX network layer */
574 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
575 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
576 t_simdata->comm = comm;
578 if (TRACE_is_enabled()) {
579 simcall_set_category(comm, task->category);
585 TRACE_msg_task_put_end();
589 /** \ingroup msg_task_usage
590 * \brief Starts listening for receiving a task from an asynchronous communication.
592 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
593 * to end the communication.
595 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
596 * \param name of the mailbox to receive the task on
597 * \return the msg_comm_t communication created
599 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
601 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
603 /* FIXME: these functions are not traceable */
606 xbt_assert(task, "Null pointer for the task storage");
610 ("MSG_task_irecv() was asked to write in a non empty task struct.");
612 /* Try to receive it by calling SIMIX network layer */
613 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
614 comm->task_sent = NULL;
615 comm->task_received = task;
616 comm->status = MSG_OK;
617 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
622 /** \ingroup msg_task_usage
623 * \brief Starts listening for receiving a task from an asynchronous communication
626 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
627 * \param name of the mailbox to receive the task on
628 * \param rate limit the bandwidth to the given rate
629 * \return the msg_comm_t communication created
631 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
635 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
637 /* FIXME: these functions are not traceable */
640 xbt_assert(task, "Null pointer for the task storage");
644 ("MSG_task_irecv() was asked to write in a non empty task struct.");
646 /* Try to receive it by calling SIMIX network layer */
647 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
648 comm->task_sent = NULL;
649 comm->task_received = task;
650 comm->status = MSG_OK;
651 comm->s_comm = simcall_comm_irecv_bounded(rdv, task, NULL, NULL, NULL, rate);
656 /** \ingroup msg_task_usage
657 * \brief Checks whether a communication is done, and if yes, finalizes it.
658 * \param comm the communication to test
659 * \return TRUE if the communication is finished
660 * (but it may have failed, use MSG_comm_get_status() to know its status)
661 * or FALSE if the communication is not finished yet
662 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
664 int MSG_comm_test(msg_comm_t comm)
670 finished = simcall_comm_test(comm->s_comm);
672 if (finished && comm->task_received != NULL) {
673 /* I am the receiver */
674 (*comm->task_received)->simdata->isused = 0;
678 switch (e.category) {
680 comm->status = MSG_TRANSFER_FAILURE;
685 comm->status = MSG_TIMEOUT;
698 /** \ingroup msg_task_usage
699 * \brief This function checks if a communication is finished.
700 * \param comms a vector of communications
701 * \return the position of the finished communication if any
702 * (but it may have failed, use MSG_comm_get_status() to know its status),
703 * or -1 if none is finished
705 int MSG_comm_testany(xbt_dynar_t comms)
708 int finished_index = -1;
710 /* create the equivalent dynar with SIMIX objects */
711 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
714 xbt_dynar_foreach(comms, cursor, comm) {
715 xbt_dynar_push(s_comms, &comm->s_comm);
718 msg_error_t status = MSG_OK;
720 finished_index = simcall_comm_testany(s_comms);
723 switch (e.category) {
725 finished_index = e.value;
726 status = MSG_TRANSFER_FAILURE;
730 finished_index = e.value;
731 status = MSG_TIMEOUT;
739 xbt_dynar_free(&s_comms);
741 if (finished_index != -1) {
742 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
743 /* the communication is finished */
744 comm->status = status;
746 if (status == MSG_OK && comm->task_received != NULL) {
747 /* I am the receiver */
748 (*comm->task_received)->simdata->isused = 0;
752 return finished_index;
755 /** \ingroup msg_task_usage
756 * \brief Destroys a communication.
757 * \param comm the communication to destroy.
759 void MSG_comm_destroy(msg_comm_t comm)
764 /** \ingroup msg_task_usage
765 * \brief Wait for the completion of a communication.
767 * It takes two parameters.
768 * \param comm the communication to wait.
769 * \param timeout Wait until the communication terminates or the timeout
770 * occurs. You can provide a -1 timeout to obtain an infinite timeout.
771 * \return msg_error_t
773 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
777 simcall_comm_wait(comm->s_comm, timeout);
779 if (comm->task_received != NULL) {
780 /* I am the receiver */
781 (*comm->task_received)->simdata->isused = 0;
784 /* FIXME: these functions are not traceable */
787 switch (e.category) {
789 comm->status = MSG_TRANSFER_FAILURE;
792 comm->status = MSG_TIMEOUT;
803 /** \ingroup msg_task_usage
804 * \brief This function is called by a sender and permit to wait for each communication
806 * \param comm a vector of communication
807 * \param nb_elem is the size of the comm vector
808 * \param timeout for each call of MSG_comm_wait
810 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
813 for (i = 0; i < nb_elem; i++) {
814 MSG_comm_wait(comm[i], timeout);
818 /** \ingroup msg_task_usage
819 * \brief This function waits for the first communication finished in a list.
820 * \param comms a vector of communications
821 * \return the position of the first finished communication
822 * (but it may have failed, use MSG_comm_get_status() to know its status)
824 int MSG_comm_waitany(xbt_dynar_t comms)
827 int finished_index = -1;
829 /* create the equivalent dynar with SIMIX objects */
830 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
833 xbt_dynar_foreach(comms, cursor, comm) {
834 xbt_dynar_push(s_comms, &comm->s_comm);
837 msg_error_t status = MSG_OK;
839 finished_index = simcall_comm_waitany(s_comms);
842 switch (e.category) {
844 finished_index = e.value;
845 status = MSG_TRANSFER_FAILURE;
849 finished_index = e.value;
850 status = MSG_TIMEOUT;
859 xbt_assert(finished_index != -1, "WaitAny returned -1");
860 xbt_dynar_free(&s_comms);
862 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
863 /* the communication is finished */
864 comm->status = status;
866 if (comm->task_received != NULL) {
867 /* I am the receiver */
868 (*comm->task_received)->simdata->isused = 0;
871 return finished_index;
875 * \ingroup msg_task_usage
876 * \brief Returns the error (if any) that occured during a finished communication.
877 * \param comm a finished communication
878 * \return the status of the communication, or #MSG_OK if no error occured
879 * during the communication
881 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
886 /** \ingroup msg_task_usage
887 * \brief Get a task (#msg_task_t) from a communication
889 * \param comm the communication where to get the task
890 * \return the task from the communication
892 msg_task_t MSG_comm_get_task(msg_comm_t comm)
894 xbt_assert(comm, "Invalid parameter");
896 return comm->task_received ? *comm->task_received : comm->task_sent;
900 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
901 * \param comm the comm
902 * \param buff the data copied
903 * \param buff_size size of the buffer
905 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
908 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
910 // notify the user callback if any
911 if (msg_global->task_copy_callback) {
912 msg_task_t task = buff;
913 msg_global->task_copy_callback(task,
914 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
918 /** \ingroup msg_task_usage
919 * \brief Sends a task to a mailbox
921 * This is a blocking function, the execution flow will be blocked
922 * until the task is sent (and received in the other side if #MSG_task_receive is used).
923 * See #MSG_task_isend for sending tasks asynchronously.
925 * \param task the task to be sent
926 * \param alias the mailbox name to where the task is sent
928 * \return Returns #MSG_OK if the task was successfully sent,
929 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
931 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
933 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
934 return MSG_task_send_with_timeout(task, alias, -1);
937 /** \ingroup msg_task_usage
938 * \brief Sends a task to a mailbox with a maximum rate
940 * This is a blocking function, the execution flow will be blocked
941 * until the task is sent. The maxrate parameter allows the application
942 * to limit the bandwidth utilization of network links when sending the task.
944 * \param task the task to be sent
945 * \param alias the mailbox name to where the task is sent
946 * \param maxrate the maximum communication rate for sending this task
948 * \return Returns #MSG_OK if the task was successfully sent,
949 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
952 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
954 task->simdata->rate = maxrate;
955 return MSG_task_send(task, alias);
958 /** \ingroup msg_task_usage
959 * \brief Sends a task to a mailbox with a timeout
961 * This is a blocking function, the execution flow will be blocked
962 * until the task is sent or the timeout is achieved.
964 * \param task the task to be sent
965 * \param alias the mailbox name to where the task is sent
966 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
968 * \return Returns #MSG_OK if the task was successfully sent,
969 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
972 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
975 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
979 /** \ingroup msg_task_usage
980 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
982 * This is a blocking function, the execution flow will be blocked
983 * until the task is sent or the timeout is achieved.
985 * \param task the task to be sent
986 * \param alias the mailbox name to where the task is sent
987 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
988 * \param maxrate the maximum communication rate for sending this task
990 * \return Returns #MSG_OK if the task was successfully sent,
991 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
994 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
995 double timeout, double maxrate)
997 task->simdata->rate = maxrate;
998 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
1002 /** \ingroup msg_task_usage
1003 * \brief Check if there is a communication going on in a mailbox.
1005 * \param alias the name of the mailbox to be considered
1007 * \return Returns 1 if there is a communication, 0 otherwise
1009 int MSG_task_listen(const char *alias)
1011 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
1014 /** \ingroup msg_task_usage
1015 * \brief Check the number of communication actions of a given host pending in a mailbox.
1017 * \param alias the name of the mailbox to be considered
1018 * \param host the host to check for communication
1020 * \return Returns the number of pending communication actions of the host in the
1021 * given mailbox, 0 if there is no pending communication actions.
1024 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
1027 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
1031 /** \ingroup msg_task_usage
1032 * \brief Look if there is a communication on a mailbox and return the
1033 * PID of the sender process.
1035 * \param alias the name of the mailbox to be considered
1037 * \return Returns the PID of sender process,
1038 * -1 if there is no communication in the mailbox.
1040 int MSG_task_listen_from(const char *alias)
1045 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
1048 return MSG_process_get_PID(task->simdata->sender);
1051 /** \ingroup msg_task_usage
1052 * \brief Sets the tracing category of a task.
1054 * This function should be called after the creation of
1055 * a MSG task, to define the category of that task. The
1056 * first parameter task must contain a task that was
1057 * created with the function #MSG_task_create. The second
1058 * parameter category must contain a category that was
1059 * previously declared with the function #TRACE_category
1060 * (or with #TRACE_category_with_color).
1062 * See \ref tracing for details on how to trace
1063 * the (categorized) resource utilization.
1065 * \param task the task that is going to be categorized
1066 * \param category the name of the category to be associated to the task
1068 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
1070 void MSG_task_set_category (msg_task_t task, const char *category)
1073 TRACE_msg_set_task_category (task, category);
1077 /** \ingroup msg_task_usage
1079 * \brief Gets the current tracing category of a task.
1081 * \param task the task to be considered
1083 * \see MSG_task_set_category
1085 * \return Returns the name of the tracing category of the given task, NULL otherwise
1087 const char *MSG_task_get_category (msg_task_t task)
1090 return task->category;
1097 * \brief Returns the value of a given AS or router property
1099 * \param asr the name of a router or AS
1100 * \param name a property name
1101 * \return value of a property (or NULL if property not set)
1103 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
1105 return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
1109 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
1110 * a the AS or router
1112 * \param asr the name of a router or AS
1113 * \return a dict containing the properties
1115 xbt_dict_t MSG_as_router_get_properties(const char* asr)
1117 return (simcall_asr_get_properties(asr));
1121 * \brief Change the value of a given AS or router
1123 * \param asr the name of a router or AS
1124 * \param name a property name
1125 * \param value what to change the property to
1126 * \param free_ctn the freeing function to use to kill the value on need
1128 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1129 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1132 #ifdef MSG_USE_DEPRECATED
1133 /** \ingroup msg_deprecated_functions
1135 * \brief Return the last value returned by a MSG function (except
1136 * MSG_get_errno...).
1138 msg_error_t MSG_get_errno(void)
1140 return PROCESS_GET_ERRNO();
1143 /** \ingroup msg_deprecated_functions
1144 * \brief Put a task on a channel of an host and waits for the end of the
1147 * This function is used for describing the behavior of a process. It
1148 * takes three parameter.
1149 * \param task a #msg_task_t to send on another location. This task
1150 will not be usable anymore when the function will return. There is
1151 no automatic task duplication and you have to save your parameters
1152 before calling this function. Tasks are unique and once it has been
1153 sent to another location, you should not access it anymore. You do
1154 not need to call MSG_task_destroy() but to avoid using, as an
1155 effect of inattention, this task anymore, you definitely should
1156 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1157 can be transfered iff it has been correctly created with
1159 * \param dest the destination of the message
1160 * \param channel the channel on which the process should put this
1161 task. This value has to be >=0 and < than the maximal number of
1162 channels fixed with MSG_set_channel_number().
1163 * \return #MSG_HOST_FAILURE if the host on which
1164 * this function was called was shut down,
1165 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1166 * (network failure, dest failure) or #MSG_OK if it succeeded.
1168 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
1170 XBT_WARN("DEPRECATED! Now use MSG_task_send");
1171 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1174 /** \ingroup msg_deprecated_functions
1175 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1181 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
1184 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1185 task->simdata->rate = maxrate;
1186 return MSG_task_put(task, dest, channel);
1189 /** \ingroup msg_deprecated_functions
1191 * \brief Put a task on a channel of an
1192 * host (with a timeout on the waiting of the destination host) and
1193 * waits for the end of the transmission.
1195 * This function is used for describing the behavior of a process. It
1196 * takes four parameter.
1197 * \param task a #msg_task_t to send on another location. This task
1198 will not be usable anymore when the function will return. There is
1199 no automatic task duplication and you have to save your parameters
1200 before calling this function. Tasks are unique and once it has been
1201 sent to another location, you should not access it anymore. You do
1202 not need to call MSG_task_destroy() but to avoid using, as an
1203 effect of inattention, this task anymore, you definitely should
1204 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1205 can be transfered iff it has been correctly created with
1207 * \param dest the destination of the message
1208 * \param channel the channel on which the process should put this
1209 task. This value has to be >=0 and < than the maximal number of
1210 channels fixed with MSG_set_channel_number().
1211 * \param timeout the maximum time to wait for a task before giving
1212 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1213 will not be modified
1214 * \return #MSG_HOST_FAILURE if the host on which
1215 this function was called was shut down,
1216 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1217 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1220 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
1221 m_channel_t channel, double timeout)
1223 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1224 xbt_assert((channel >= 0)
1225 && (channel < msg_global->max_channel), "Invalid channel %d",
1228 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
1230 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1231 (dest, channel), task, timeout);
1234 /** \ingroup msg_deprecated_functions
1235 * \brief Test whether there is a pending communication on a channel, and who sent it.
1237 * It takes one parameter.
1238 * \param channel the channel on which the process should be
1239 listening. This value has to be >=0 and < than the maximal
1240 number of channels fixed with MSG_set_channel_number().
1241 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1243 int MSG_task_probe_from(m_channel_t channel)
1245 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1248 xbt_assert((channel >= 0)
1249 && (channel < msg_global->max_channel), "Invalid channel %d",
1254 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1255 (MSG_host_self(), channel))))
1258 return MSG_process_get_PID(task->simdata->sender);
1261 /** \ingroup msg_deprecated_functions
1262 * \brief Test whether there is a pending communication on a channel.
1264 * It takes one parameter.
1265 * \param channel the channel on which the process should be
1266 listening. This value has to be >=0 and < than the maximal
1267 number of channels fixed with MSG_set_channel_number().
1268 * \return 1 if there is a pending communication and 0 otherwise
1270 int MSG_task_Iprobe(m_channel_t channel)
1272 XBT_WARN("DEPRECATED!");
1273 xbt_assert((channel >= 0)
1274 && (channel < msg_global->max_channel), "Invalid channel %d",
1278 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1279 (MSG_host_self(), channel));
1282 /** \ingroup msg_deprecated_functions
1284 * \brief Return the number of tasks waiting to be received on a \a
1285 channel and sent by \a host.
1287 * It takes two parameters.
1288 * \param channel the channel on which the process should be
1289 listening. This value has to be >=0 and < than the maximal
1290 number of channels fixed with MSG_set_channel_number().
1291 * \param host the host that is to be watched.
1292 * \return the number of tasks waiting to be received on \a channel
1293 and sent by \a host.
1295 int MSG_task_probe_from_host(int channel, msg_host_t host)
1297 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1298 xbt_assert((channel >= 0)
1299 && (channel < msg_global->max_channel), "Invalid channel %d",
1303 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1304 (MSG_host_self(), channel),
1309 /** \ingroup msg_deprecated_functions
1310 * \brief Listen on \a channel and waits for receiving a task from \a host.
1312 * It takes three parameters.
1313 * \param task a memory location for storing a #msg_task_t. It will
1314 hold a task when this function will return. Thus \a task should not
1315 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1316 those two condition does not hold, there will be a warning message.
1317 * \param channel the channel on which the process should be
1318 listening. This value has to be >=0 and < than the maximal
1319 number of channels fixed with MSG_set_channel_number().
1320 * \param host the host that is to be watched.
1321 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1324 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1326 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1327 return MSG_task_get_ext(task, channel, -1, host);
1330 /** \ingroup msg_deprecated_functions
1331 * \brief Listen on a channel and wait for receiving a task.
1333 * It takes two parameters.
1334 * \param task a memory location for storing a #msg_task_t. It will
1335 hold a task when this function will return. Thus \a task should not
1336 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1337 those two condition does not hold, there will be a warning message.
1338 * \param channel the channel on which the process should be
1339 listening. This value has to be >=0 and < than the maximal
1340 number of channels fixed with MSG_set_channel_number().
1341 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1343 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1345 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1346 return MSG_task_get_with_timeout(task, channel, -1);
1349 /** \ingroup msg_deprecated_functions
1350 * \brief Listen on a channel and wait for receiving a task with a timeout.
1352 * It takes three parameters.
1353 * \param task a memory location for storing a #msg_task_t. It will
1354 hold a task when this function will return. Thus \a task should not
1355 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1356 those two condition does not hold, there will be a warning message.
1357 * \param channel the channel on which the process should be
1358 listening. This value has to be >=0 and < than the maximal
1359 number of channels fixed with MSG_set_channel_number().
1360 * \param max_duration the maximum time to wait for a task before giving
1361 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1362 will not be modified and will still be
1363 equal to \c NULL when returning.
1364 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1367 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1368 double max_duration)
1370 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1371 return MSG_task_get_ext(task, channel, max_duration, NULL);
1375 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1378 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1379 xbt_assert((channel >= 0)
1380 && (channel < msg_global->max_channel), "Invalid channel %d",
1384 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1385 (MSG_host_self(), channel), task, host,