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,
93 simcall_set_category(simdata->compute, task->category);
95 p_simdata->waiting_action = simdata->compute;
96 comp_state = simcall_host_execution_wait(simdata->compute);
98 p_simdata->waiting_action = NULL;
102 XBT_DEBUG("Execution task '%s' finished in state %d",
103 task->name, (int)comp_state);
106 switch (e.category) {
108 status = MSG_TASK_CANCELED;
115 /* action ended, set comm and compute = NULL, the actions is already destroyed
116 * in the main function */
117 simdata->computation_amount = 0.0;
118 simdata->comm = NULL;
119 simdata->compute = NULL;
121 TRACE_msg_task_execute_end(task);
128 /** \ingroup msg_task_usage
129 * \brief Sleep for the specified number of seconds
131 * Makes the current process sleep until \a time seconds have elapsed.
133 * \param nb_sec a number of second
135 msg_error_t MSG_process_sleep(double nb_sec)
138 msg_error_t status = MSG_OK;
139 /*msg_process_t proc = MSG_process_self();*/
142 TRACE_msg_process_sleep_in(MSG_process_self());
145 /* create action to sleep */
147 /*proc->simdata->waiting_action = act_sleep;
149 FIXME: check if not setting the waiting_action breaks something on msg
151 proc->simdata->waiting_action = NULL;*/
154 simcall_process_sleep(nb_sec);
157 switch (e.category) {
159 status = MSG_TASK_CANCELED;
168 TRACE_msg_process_sleep_out(MSG_process_self());
173 /** \ingroup msg_task_usage
174 * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
176 * Sorry, this function is not supported anymore. That wouldn't be
177 * impossible to reimplement it, but we are lacking the time to do so ourselves.
178 * If you need this functionality, you can either:
180 * - implement the buffering mechanism on the user-level by queuing all messages
181 * received in the mailbox that do not match your expectation
182 * - change your application logic to leverage the mailboxes features. For example,
183 * if you have A receiving messages from B and C, you could have A waiting on
184 * mailbox "A" most of the time, but on "A#B" when it's waiting for specific
185 * messages from B and "A#C" when waiting for messages from C. You could even get A
186 * sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
187 * an example of use of this function in the @ref MSG_examples section.
188 * - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
189 * very difficult actually. Check the function @ref MSG_mailbox_get_task_ext. During its call to
190 * simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
191 * messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
192 * and that your filtering function will receive as first parameter, and then, the filter could
193 * simply compare the host names, for example. After sufficient testing, provide an example that
194 * we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
196 * \param task a memory location for storing a #msg_task_t.
197 * \param alias name of the mailbox to receive the task from
198 * \param host a #msg_host_t host from where the task was sent
201 * #MSG_OK if the task was successfully received,
202 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
205 MSG_task_receive_from_host(msg_task_t * task, const char *alias,
208 return MSG_task_receive_ext(task, alias, -1, host);
212 *\brief Deprecated function that used to receive a task from a mailbox from a specific host
213 *\brief at a given rate
215 * \param task a memory location for storing a #msg_task_t.
216 * \param alias name of the mailbox to receive the task from
217 * \param host a #msg_host_t host from where the task was sent
218 * \param rate limit the reception to rate bandwidth
221 * #MSG_OK if the task was successfully received,
222 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
225 MSG_task_receive_from_host_bounded(msg_task_t * task, const char *alias,
226 msg_host_t host, double rate)
228 return MSG_task_receive_ext_bounded(task, alias, -1, host, rate);
231 /** \ingroup msg_task_usage
232 * \brief Receives a task from a mailbox.
234 * This is a blocking function, the execution flow will be blocked
235 * until the task is received. See #MSG_task_irecv
236 * for receiving tasks asynchronously.
238 * \param task a memory location for storing a #msg_task_t.
239 * \param alias name of the mailbox to receive the task from
242 * #MSG_OK if the task was successfully received,
243 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
245 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
247 return MSG_task_receive_with_timeout(task, alias, -1);
250 /** \ingroup msg_task_usage
251 * \brief Receives a task from a mailbox at a given rate.
253 * \param task a memory location for storing a #msg_task_t.
254 * \param alias name of the mailbox to receive the task from
255 * \param rate limit the reception to rate bandwidth
258 * #MSG_OK if the task was successfully received,
259 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
261 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
263 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
266 /** \ingroup msg_task_usage
267 * \brief Receives a task from a mailbox with a given timeout.
269 * This is a blocking function with a timeout, the execution flow will be blocked
270 * until the task is received or the timeout is achieved. See #MSG_task_irecv
271 * for receiving tasks asynchronously. You can provide a -1 timeout
272 * to obtain an infinite timeout.
274 * \param task a memory location for storing a #msg_task_t.
275 * \param alias name of the mailbox to receive the task from
276 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
279 * #MSG_OK if the task was successfully received,
280 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
283 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
286 return MSG_task_receive_ext(task, alias, timeout, NULL);
289 /** \ingroup msg_task_usage
290 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
292 * \param task a memory location for storing a #msg_task_t.
293 * \param alias name of the mailbox to receive the task from
294 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
295 * \param rate limit the reception to rate bandwidth
298 * #MSG_OK if the task was successfully received,
299 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
302 MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias,
303 double timeout,double rate)
305 return MSG_task_receive_ext_bounded(task, alias, timeout, NULL,rate);
308 /** \ingroup msg_task_usage
309 * \brief Receives a task from a mailbox from a specific host with a given timeout.
311 * This is a blocking function with a timeout, the execution flow will be blocked
312 * until the task is received or the timeout is achieved. See #MSG_task_irecv
313 * for receiving tasks asynchronously. You can provide a -1 timeout
314 * to obtain an infinite timeout.
316 * \param task a memory location for storing a #msg_task_t.
317 * \param alias name of the mailbox to receive the task from
318 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
319 * \param host a #msg_host_t host from where the task was sent
322 * #MSG_OK if the task was successfully received,
323 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
326 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
330 msg_error_t ret = MSG_OK;
332 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
335 ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
339 switch (e.category) {
340 case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */
341 ret = MSG_HOST_FAILURE;
351 /** \ingroup msg_task_usage
352 * \brief Receives a task from a mailbox from a specific host with a given timeout
353 * and at a given rate.
355 * \param task a memory location for storing a #msg_task_t.
356 * \param alias name of the mailbox to receive the task from
357 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
358 * \param host a #msg_host_t host from where the task was sent
359 * \param rate limit the reception to rate bandwidth
362 * #MSG_OK if the task was successfully received,
363 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
366 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
367 msg_host_t host, double rate)
370 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
372 return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
373 host, timeout, rate);
376 /** \ingroup msg_task_usage
377 * \brief Sends a task on a mailbox.
379 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
380 * to end the communication.
382 * \param task a #msg_task_t to send on another location.
383 * \param alias name of the mailbox to sent the task to
384 * \return the msg_comm_t communication created
386 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
388 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
391 /** \ingroup msg_task_usage
392 * \brief Sends a task on a mailbox with a maximum rate
394 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
395 * to end the communication. The maxrate parameter allows the application
396 * to limit the bandwidth utilization of network links when sending the task.
398 * \param task a #msg_task_t to send on another location.
399 * \param alias name of the mailbox to sent the task to
400 * \param maxrate the maximum communication rate for sending this task .
401 * \return the msg_comm_t communication created
403 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
405 task->simdata->rate = maxrate;
406 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
410 /** \ingroup msg_task_usage
411 * \brief Sends a task on a mailbox, with support for matching requests
413 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
414 * to end the communication.
416 * \param task a #msg_task_t to send on another location.
417 * \param alias name of the mailbox to sent the task to
418 * \param match_fun boolean function which parameters are:
419 * - match_data_provided_here
420 * - match_data_provided_by_other_side_if_any
421 * - the_smx_action_describing_the_other_side
422 * \param match_data user provided data passed to match_fun
423 * \return the msg_comm_t communication created
425 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
426 int (*match_fun)(void*,void*, smx_action_t),
429 simdata_task_t t_simdata = NULL;
430 msg_process_t process = MSG_process_self();
431 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
434 int call_end = TRACE_msg_task_put_start(task);
437 /* Prepare the task to send */
438 t_simdata = task->simdata;
439 t_simdata->sender = process;
440 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
442 xbt_assert(t_simdata->isused == 0,
443 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
445 t_simdata->isused = 1;
446 t_simdata->comm = NULL;
447 msg_global->sent_msg++;
449 /* Send it by calling SIMIX network layer */
450 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
451 comm->task_sent = task;
452 comm->task_received = NULL;
453 comm->status = MSG_OK;
455 simcall_comm_isend(mailbox, t_simdata->message_size,
456 t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
457 t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
459 if (TRACE_is_enabled()) {
460 simcall_set_category(comm->s_comm, task->category);
466 TRACE_msg_task_put_end();
472 /** \ingroup msg_task_usage
473 * \brief Sends a task on a mailbox.
475 * This is a non blocking detached send function.
476 * Think of it as a best effort send. Keep in mind that the third parameter
477 * is only called if the communication fails. If the communication does work,
478 * it is responsibility of the receiver code to free anything related to
479 * the task, as usual. More details on this can be obtained on
480 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
481 * in the SimGrid-user mailing list archive.
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 cleanup a function to destroy the task if the
486 * communication fails, e.g. MSG_task_destroy
487 * (if NULL, no function will be called)
489 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
491 simdata_task_t t_simdata = NULL;
492 msg_process_t process = MSG_process_self();
493 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
495 /* Prepare the task to send */
496 t_simdata = task->simdata;
497 t_simdata->sender = process;
498 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
500 xbt_assert(t_simdata->isused == 0,
501 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
503 t_simdata->isused = 1;
504 t_simdata->comm = NULL;
505 msg_global->sent_msg++;
508 int call_end = TRACE_msg_task_put_start(task);
511 /* Send it by calling SIMIX network layer */
512 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
513 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
514 t_simdata->comm = comm;
516 if (TRACE_is_enabled()) {
517 simcall_set_category(comm, task->category);
523 TRACE_msg_task_put_end();
528 /** \ingroup msg_task_usage
529 * \brief Sends a task on a mailbox with a maximal rate.
531 * This is a non blocking detached send function.
532 * Think of it as a best effort send. Keep in mind that the third parameter
533 * is only called if the communication fails. If the communication does work,
534 * it is responsibility of the receiver code to free anything related to
535 * the task, as usual. More details on this can be obtained on
536 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
537 * in the SimGrid-user mailing list archive.
539 * \param task a #msg_task_t to send on another location.
540 * \param alias name of the mailbox to sent the task to
541 * \param cleanup a function to destroy the task if the
542 * communication fails, e.g. MSG_task_destroy
543 * (if NULL, no function will be called)
544 * \param maxrate the maximum communication rate for sending this task
547 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
549 task->simdata->rate = maxrate;
551 simdata_task_t t_simdata = NULL;
552 msg_process_t process = MSG_process_self();
553 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
555 /* Prepare the task to send */
556 t_simdata = task->simdata;
557 t_simdata->sender = process;
558 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
560 xbt_assert(t_simdata->isused == 0,
561 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
563 t_simdata->isused = 1;
564 t_simdata->comm = NULL;
565 msg_global->sent_msg++;
568 int call_end = TRACE_msg_task_put_start(task);
571 /* Send it by calling SIMIX network layer */
572 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
573 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
574 t_simdata->comm = comm;
576 if (TRACE_is_enabled()) {
577 simcall_set_category(comm, task->category);
583 TRACE_msg_task_put_end();
587 /** \ingroup msg_task_usage
588 * \brief Starts listening for receiving a task from an asynchronous communication.
590 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
591 * to end the communication.
593 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
594 * \param name of the mailbox to receive the task on
595 * \return the msg_comm_t communication created
597 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
599 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
601 /* FIXME: these functions are not traceable */
604 xbt_assert(task, "Null pointer for the task storage");
608 ("MSG_task_irecv() was asked to write in a non empty task struct.");
610 /* Try to receive it by calling SIMIX network layer */
611 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
612 comm->task_sent = NULL;
613 comm->task_received = task;
614 comm->status = MSG_OK;
615 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
620 /** \ingroup msg_task_usage
621 * \brief Starts listening for receiving a task from an asynchronous communication
624 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
625 * \param name of the mailbox to receive the task on
626 * \param rate limit the bandwidth to the given rate
627 * \return the msg_comm_t communication created
629 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
633 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
635 /* FIXME: these functions are not traceable */
638 xbt_assert(task, "Null pointer for the task storage");
642 ("MSG_task_irecv() was asked to write in a non empty task struct.");
644 /* Try to receive it by calling SIMIX network layer */
645 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
646 comm->task_sent = NULL;
647 comm->task_received = task;
648 comm->status = MSG_OK;
649 comm->s_comm = simcall_comm_irecv_bounded(rdv, task, NULL, NULL, NULL, rate);
654 /** \ingroup msg_task_usage
655 * \brief Checks whether a communication is done, and if yes, finalizes it.
656 * \param comm the communication to test
657 * \return TRUE if the communication is finished
658 * (but it may have failed, use MSG_comm_get_status() to know its status)
659 * or FALSE if the communication is not finished yet
660 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
662 int MSG_comm_test(msg_comm_t comm)
668 finished = simcall_comm_test(comm->s_comm);
670 if (finished && comm->task_received != NULL) {
671 /* I am the receiver */
672 (*comm->task_received)->simdata->isused = 0;
676 switch (e.category) {
678 comm->status = MSG_TRANSFER_FAILURE;
683 comm->status = MSG_TIMEOUT;
696 /** \ingroup msg_task_usage
697 * \brief This function checks if a communication is finished.
698 * \param comms a vector of communications
699 * \return the position of the finished communication if any
700 * (but it may have failed, use MSG_comm_get_status() to know its status),
701 * or -1 if none is finished
703 int MSG_comm_testany(xbt_dynar_t comms)
706 int finished_index = -1;
708 /* create the equivalent dynar with SIMIX objects */
709 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
712 xbt_dynar_foreach(comms, cursor, comm) {
713 xbt_dynar_push(s_comms, &comm->s_comm);
716 msg_error_t status = MSG_OK;
718 finished_index = simcall_comm_testany(s_comms);
721 switch (e.category) {
723 finished_index = e.value;
724 status = MSG_TRANSFER_FAILURE;
728 finished_index = e.value;
729 status = MSG_TIMEOUT;
737 xbt_dynar_free(&s_comms);
739 if (finished_index != -1) {
740 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
741 /* the communication is finished */
742 comm->status = status;
744 if (status == MSG_OK && comm->task_received != NULL) {
745 /* I am the receiver */
746 (*comm->task_received)->simdata->isused = 0;
750 return finished_index;
753 /** \ingroup msg_task_usage
754 * \brief Destroys a communication.
755 * \param comm the communication to destroy.
757 void MSG_comm_destroy(msg_comm_t comm)
762 /** \ingroup msg_task_usage
763 * \brief Wait for the completion of a communication.
765 * It takes two parameters.
766 * \param comm the communication to wait.
767 * \param timeout Wait until the communication terminates or the timeout
768 * occurs. You can provide a -1 timeout to obtain an infinite timeout.
769 * \return msg_error_t
771 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
775 simcall_comm_wait(comm->s_comm, timeout);
777 if (comm->task_received != NULL) {
778 /* I am the receiver */
779 (*comm->task_received)->simdata->isused = 0;
782 /* FIXME: these functions are not traceable */
785 switch (e.category) {
787 comm->status = MSG_TRANSFER_FAILURE;
790 comm->status = MSG_TIMEOUT;
801 /** \ingroup msg_task_usage
802 * \brief This function is called by a sender and permit to wait for each communication
804 * \param comm a vector of communication
805 * \param nb_elem is the size of the comm vector
806 * \param timeout for each call of MSG_comm_wait
808 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
811 for (i = 0; i < nb_elem; i++) {
812 MSG_comm_wait(comm[i], timeout);
816 /** \ingroup msg_task_usage
817 * \brief This function waits for the first communication finished in a list.
818 * \param comms a vector of communications
819 * \return the position of the first finished communication
820 * (but it may have failed, use MSG_comm_get_status() to know its status)
822 int MSG_comm_waitany(xbt_dynar_t comms)
825 int finished_index = -1;
827 /* create the equivalent dynar with SIMIX objects */
828 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
831 xbt_dynar_foreach(comms, cursor, comm) {
832 xbt_dynar_push(s_comms, &comm->s_comm);
835 msg_error_t status = MSG_OK;
837 finished_index = simcall_comm_waitany(s_comms);
840 switch (e.category) {
842 finished_index = e.value;
843 status = MSG_TRANSFER_FAILURE;
847 finished_index = e.value;
848 status = MSG_TIMEOUT;
857 xbt_assert(finished_index != -1, "WaitAny returned -1");
858 xbt_dynar_free(&s_comms);
860 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
861 /* the communication is finished */
862 comm->status = status;
864 if (comm->task_received != NULL) {
865 /* I am the receiver */
866 (*comm->task_received)->simdata->isused = 0;
869 return finished_index;
873 * \ingroup msg_task_usage
874 * \brief Returns the error (if any) that occured during a finished communication.
875 * \param comm a finished communication
876 * \return the status of the communication, or #MSG_OK if no error occured
877 * during the communication
879 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
884 /** \ingroup msg_task_usage
885 * \brief Get a task (#msg_task_t) from a communication
887 * \param comm the communication where to get the task
888 * \return the task from the communication
890 msg_task_t MSG_comm_get_task(msg_comm_t comm)
892 xbt_assert(comm, "Invalid parameter");
894 return comm->task_received ? *comm->task_received : comm->task_sent;
898 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
899 * \param comm the comm
900 * \param buff the data copied
901 * \param buff_size size of the buffer
903 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
906 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
908 // notify the user callback if any
909 if (msg_global->task_copy_callback) {
910 msg_task_t task = buff;
911 msg_global->task_copy_callback(task,
912 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
916 /** \ingroup msg_task_usage
917 * \brief Sends a task to a mailbox
919 * This is a blocking function, the execution flow will be blocked
920 * until the task is sent (and received in the other side if #MSG_task_receive is used).
921 * See #MSG_task_isend for sending tasks asynchronously.
923 * \param task the task to be sent
924 * \param alias the mailbox name to where the task is sent
926 * \return Returns #MSG_OK if the task was successfully sent,
927 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
929 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
931 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
932 return MSG_task_send_with_timeout(task, alias, -1);
935 /** \ingroup msg_task_usage
936 * \brief Sends a task to a mailbox with a maximum rate
938 * This is a blocking function, the execution flow will be blocked
939 * until the task is sent. The maxrate parameter allows the application
940 * to limit the bandwidth utilization of network links when sending the task.
942 * \param task the task to be sent
943 * \param alias the mailbox name to where the task is sent
944 * \param maxrate the maximum communication rate for sending this task
946 * \return Returns #MSG_OK if the task was successfully sent,
947 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
950 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
952 task->simdata->rate = maxrate;
953 return MSG_task_send(task, alias);
956 /** \ingroup msg_task_usage
957 * \brief Sends a task to a mailbox with a timeout
959 * This is a blocking function, the execution flow will be blocked
960 * until the task is sent or the timeout is achieved.
962 * \param task the task to be sent
963 * \param alias the mailbox name to where the task is sent
964 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
966 * \return Returns #MSG_OK if the task was successfully sent,
967 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
970 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
973 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
977 /** \ingroup msg_task_usage
978 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
980 * This is a blocking function, the execution flow will be blocked
981 * until the task is sent or the timeout is achieved.
983 * \param task the task to be sent
984 * \param alias the mailbox name to where the task is sent
985 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
986 * \param maxrate the maximum communication rate for sending this task
988 * \return Returns #MSG_OK if the task was successfully sent,
989 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
992 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
993 double timeout, double maxrate)
995 task->simdata->rate = maxrate;
996 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
1000 /** \ingroup msg_task_usage
1001 * \brief Check if there is a communication going on in a mailbox.
1003 * \param alias the name of the mailbox to be considered
1005 * \return Returns 1 if there is a communication, 0 otherwise
1007 int MSG_task_listen(const char *alias)
1009 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
1012 /** \ingroup msg_task_usage
1013 * \brief Check the number of communication actions of a given host pending in a mailbox.
1015 * \param alias the name of the mailbox to be considered
1016 * \param host the host to check for communication
1018 * \return Returns the number of pending communication actions of the host in the
1019 * given mailbox, 0 if there is no pending communication actions.
1022 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
1025 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
1029 /** \ingroup msg_task_usage
1030 * \brief Look if there is a communication on a mailbox and return the
1031 * PID of the sender process.
1033 * \param alias the name of the mailbox to be considered
1035 * \return Returns the PID of sender process,
1036 * -1 if there is no communication in the mailbox.
1038 int MSG_task_listen_from(const char *alias)
1043 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
1046 return MSG_process_get_PID(task->simdata->sender);
1049 /** \ingroup msg_task_usage
1050 * \brief Sets the tracing category of a task.
1052 * This function should be called after the creation of
1053 * a MSG task, to define the category of that task. The
1054 * first parameter task must contain a task that was
1055 * created with the function #MSG_task_create. The second
1056 * parameter category must contain a category that was
1057 * previously declared with the function #TRACE_category
1058 * (or with #TRACE_category_with_color).
1060 * See \ref tracing for details on how to trace
1061 * the (categorized) resource utilization.
1063 * \param task the task that is going to be categorized
1064 * \param category the name of the category to be associated to the task
1066 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
1068 void MSG_task_set_category (msg_task_t task, const char *category)
1071 TRACE_msg_set_task_category (task, category);
1075 /** \ingroup msg_task_usage
1077 * \brief Gets the current tracing category of a task.
1079 * \param task the task to be considered
1081 * \see MSG_task_set_category
1083 * \return Returns the name of the tracing category of the given task, NULL otherwise
1085 const char *MSG_task_get_category (msg_task_t task)
1088 return task->category;
1095 * \brief Returns the value of a given AS or router property
1097 * \param asr the name of a router or AS
1098 * \param name a property name
1099 * \return value of a property (or NULL if property not set)
1101 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
1103 return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
1107 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
1108 * a the AS or router
1110 * \param asr the name of a router or AS
1111 * \return a dict containing the properties
1113 xbt_dict_t MSG_as_router_get_properties(const char* asr)
1115 return (simcall_asr_get_properties(asr));
1119 * \brief Change the value of a given AS or router
1121 * \param asr the name of a router or AS
1122 * \param name a property name
1123 * \param value what to change the property to
1124 * \param free_ctn the freeing function to use to kill the value on need
1126 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1127 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1130 #ifdef MSG_USE_DEPRECATED
1131 /** \ingroup msg_deprecated_functions
1133 * \brief Return the last value returned by a MSG function (except
1134 * MSG_get_errno...).
1136 msg_error_t MSG_get_errno(void)
1138 return PROCESS_GET_ERRNO();
1141 /** \ingroup msg_deprecated_functions
1142 * \brief Put a task on a channel of an host and waits for the end of the
1145 * This function is used for describing the behavior of a process. It
1146 * takes three parameter.
1147 * \param task a #msg_task_t to send on another location. This task
1148 will not be usable anymore when the function will return. There is
1149 no automatic task duplication and you have to save your parameters
1150 before calling this function. Tasks are unique and once it has been
1151 sent to another location, you should not access it anymore. You do
1152 not need to call MSG_task_destroy() but to avoid using, as an
1153 effect of inattention, this task anymore, you definitely should
1154 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1155 can be transfered iff it has been correctly created with
1157 * \param dest the destination of the message
1158 * \param channel the channel on which the process should put this
1159 task. This value has to be >=0 and < than the maximal number of
1160 channels fixed with MSG_set_channel_number().
1161 * \return #MSG_HOST_FAILURE if the host on which
1162 * this function was called was shut down,
1163 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1164 * (network failure, dest failure) or #MSG_OK if it succeeded.
1166 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
1168 XBT_WARN("DEPRECATED! Now use MSG_task_send");
1169 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1172 /** \ingroup msg_deprecated_functions
1173 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1179 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
1182 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1183 task->simdata->rate = maxrate;
1184 return MSG_task_put(task, dest, channel);
1187 /** \ingroup msg_deprecated_functions
1189 * \brief Put a task on a channel of an
1190 * host (with a timeout on the waiting of the destination host) and
1191 * waits for the end of the transmission.
1193 * This function is used for describing the behavior of a process. It
1194 * takes four parameter.
1195 * \param task a #msg_task_t to send on another location. This task
1196 will not be usable anymore when the function will return. There is
1197 no automatic task duplication and you have to save your parameters
1198 before calling this function. Tasks are unique and once it has been
1199 sent to another location, you should not access it anymore. You do
1200 not need to call MSG_task_destroy() but to avoid using, as an
1201 effect of inattention, this task anymore, you definitely should
1202 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1203 can be transfered iff it has been correctly created with
1205 * \param dest the destination of the message
1206 * \param channel the channel on which the process should put this
1207 task. This value has to be >=0 and < than the maximal number of
1208 channels fixed with MSG_set_channel_number().
1209 * \param timeout the maximum time to wait for a task before giving
1210 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1211 will not be modified
1212 * \return #MSG_HOST_FAILURE if the host on which
1213 this function was called was shut down,
1214 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1215 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1218 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
1219 m_channel_t channel, double timeout)
1221 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1222 xbt_assert((channel >= 0)
1223 && (channel < msg_global->max_channel), "Invalid channel %d",
1226 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
1228 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1229 (dest, channel), task, timeout);
1232 /** \ingroup msg_deprecated_functions
1233 * \brief Test whether there is a pending communication on a channel, and who sent it.
1235 * It takes one parameter.
1236 * \param channel the channel on which the process should be
1237 listening. This value has to be >=0 and < than the maximal
1238 number of channels fixed with MSG_set_channel_number().
1239 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1241 int MSG_task_probe_from(m_channel_t channel)
1243 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1246 xbt_assert((channel >= 0)
1247 && (channel < msg_global->max_channel), "Invalid channel %d",
1252 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1253 (MSG_host_self(), channel))))
1256 return MSG_process_get_PID(task->simdata->sender);
1259 /** \ingroup msg_deprecated_functions
1260 * \brief Test whether there is a pending communication on a channel.
1262 * It takes one parameter.
1263 * \param channel the channel on which the process should be
1264 listening. This value has to be >=0 and < than the maximal
1265 number of channels fixed with MSG_set_channel_number().
1266 * \return 1 if there is a pending communication and 0 otherwise
1268 int MSG_task_Iprobe(m_channel_t channel)
1270 XBT_WARN("DEPRECATED!");
1271 xbt_assert((channel >= 0)
1272 && (channel < msg_global->max_channel), "Invalid channel %d",
1276 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1277 (MSG_host_self(), channel));
1280 /** \ingroup msg_deprecated_functions
1282 * \brief Return the number of tasks waiting to be received on a \a
1283 channel and sent by \a host.
1285 * It takes two parameters.
1286 * \param channel the channel on which the process should be
1287 listening. This value has to be >=0 and < than the maximal
1288 number of channels fixed with MSG_set_channel_number().
1289 * \param host the host that is to be watched.
1290 * \return the number of tasks waiting to be received on \a channel
1291 and sent by \a host.
1293 int MSG_task_probe_from_host(int channel, msg_host_t host)
1295 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1296 xbt_assert((channel >= 0)
1297 && (channel < msg_global->max_channel), "Invalid channel %d",
1301 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1302 (MSG_host_self(), channel),
1307 /** \ingroup msg_deprecated_functions
1308 * \brief Listen on \a channel and waits for receiving a task from \a host.
1310 * It takes three parameters.
1311 * \param task a memory location for storing a #msg_task_t. It will
1312 hold a task when this function will return. Thus \a task should not
1313 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1314 those two condition does not hold, there will be a warning message.
1315 * \param channel the channel on which the process should be
1316 listening. This value has to be >=0 and < than the maximal
1317 number of channels fixed with MSG_set_channel_number().
1318 * \param host the host that is to be watched.
1319 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1322 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1324 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1325 return MSG_task_get_ext(task, channel, -1, host);
1328 /** \ingroup msg_deprecated_functions
1329 * \brief Listen on a channel and wait for receiving a task.
1331 * It takes two parameters.
1332 * \param task a memory location for storing a #msg_task_t. It will
1333 hold a task when this function will return. Thus \a task should not
1334 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1335 those two condition does not hold, there will be a warning message.
1336 * \param channel the channel on which the process should be
1337 listening. This value has to be >=0 and < than the maximal
1338 number of channels fixed with MSG_set_channel_number().
1339 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1341 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1343 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1344 return MSG_task_get_with_timeout(task, channel, -1);
1347 /** \ingroup msg_deprecated_functions
1348 * \brief Listen on a channel and wait for receiving a task with a timeout.
1350 * It takes three parameters.
1351 * \param task a memory location for storing a #msg_task_t. It will
1352 hold a task when this function will return. Thus \a task should not
1353 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1354 those two condition does not hold, there will be a warning message.
1355 * \param channel the channel on which the process should be
1356 listening. This value has to be >=0 and < than the maximal
1357 number of channels fixed with MSG_set_channel_number().
1358 * \param max_duration the maximum time to wait for a task before giving
1359 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1360 will not be modified and will still be
1361 equal to \c NULL when returning.
1362 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1365 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1366 double max_duration)
1368 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1369 return MSG_task_get_ext(task, channel, max_duration, NULL);
1373 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1376 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1377 xbt_assert((channel >= 0)
1378 && (channel < msg_global->max_channel), "Invalid channel %d",
1382 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1383 (MSG_host_self(), channel), task, host,