1 /* Copyright (c) 2004-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "msg_private.h"
8 #include "msg_mailbox.h"
11 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
14 "Logging specific to MSG (gos)");
16 /** \ingroup msg_task_usage
17 * \brief Executes a task and waits for its termination.
19 * This function is used for describing the behavior of a process. It
20 * takes only one parameter.
21 * \param task a #msg_task_t to execute on the location on which the process is running.
22 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
23 * or #MSG_HOST_FAILURE otherwise
25 msg_error_t MSG_task_execute(msg_task_t task)
27 /* TODO: add this to other locations */
28 msg_host_t host = MSG_process_get_host(MSG_process_self());
29 MSG_host_add_task(host, task);
31 msg_error_t ret = MSG_parallel_task_execute(task);
33 MSG_host_del_task(host, task);
38 /** \ingroup msg_task_usage
39 * \brief Executes a parallel task and waits for its termination.
41 * \param task a #msg_task_t to execute on the location on which the process is running.
43 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
44 * or #MSG_HOST_FAILURE otherwise
46 msg_error_t MSG_parallel_task_execute(msg_task_t task)
49 simdata_task_t simdata = task->simdata;
50 msg_process_t self = SIMIX_process_self();
51 simdata_process_t p_simdata = SIMIX_process_self_get_data(self);
52 e_smx_state_t comp_state;
53 msg_error_t status = MSG_OK;
55 TRACE_msg_task_execute_start(task);
57 xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
58 "This task is executed somewhere else. Go fix your code! %d",
59 task->simdata->isused!=NULL);
61 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
63 if (simdata->flops_amount == 0 && !simdata->host_nb) {
64 TRACE_msg_task_execute_end(task);
70 if (msg_global->debug_multiple_use)
71 MSG_BT(simdata->isused, "Using Backtrace");
73 simdata->isused = (void*)1;
75 if (simdata->host_nb > 0) {
76 simdata->compute = simcall_process_parallel_execute(task->name,
79 simdata->flops_parallel_amount,
80 simdata->bytes_parallel_amount,
82 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
84 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));
85 XBT_DEBUG("execute %s@%s with affinity(0x%04lx)", MSG_task_get_name(task), MSG_host_get_name(p_simdata->m_host), affinity_mask);
87 simdata->compute = simcall_process_execute(task->name,
89 simdata->flops_amount,
96 simcall_set_category(simdata->compute, task->category);
97 p_simdata->waiting_action = simdata->compute;
98 comp_state = simcall_process_execution_wait(simdata->compute);
100 p_simdata->waiting_action = NULL;
102 if (msg_global->debug_multiple_use && simdata->isused!=0)
103 xbt_ex_free(*(xbt_ex_t*)simdata->isused);
106 XBT_DEBUG("Execution task '%s' finished in state %d",
107 task->name, (int)comp_state);
110 switch (e.category) {
112 status = MSG_TASK_CANCELED;
115 status = MSG_HOST_FAILURE;
122 /* action ended, set comm and compute = NULL, the actions is already destroyed
123 * in the main function */
124 simdata->flops_amount = 0.0;
125 simdata->comm = NULL;
126 simdata->compute = NULL;
127 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();*/
146 TRACE_msg_process_sleep_in(MSG_process_self());
149 simcall_process_sleep(nb_sec);
152 switch (e.category) {
154 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, WTF here ?");
155 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
156 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
157 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
158 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
159 // and did not change anythings at the C level.
160 // See comment in the jmsg_process.c file, function JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
161 status = MSG_TASK_CANCELED;
169 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,
264 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
267 /** \ingroup msg_task_usage
268 * \brief Receives a task from a mailbox with a given timeout.
270 * This is a blocking function with a timeout, the execution flow will be blocked
271 * until the task is received or the timeout is achieved. See #MSG_task_irecv
272 * for receiving tasks asynchronously. You can provide a -1 timeout
273 * to obtain an infinite timeout.
275 * \param task a memory location for storing a #msg_task_t.
276 * \param alias name of the mailbox to receive the task from
277 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
280 * #MSG_OK if the task was successfully received,
281 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
284 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
287 return MSG_task_receive_ext(task, alias, timeout, NULL);
290 /** \ingroup msg_task_usage
291 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
293 * \param task a memory location for storing a #msg_task_t.
294 * \param alias name of the mailbox to receive the task from
295 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
296 * \param rate limit the reception to rate bandwidth
299 * #MSG_OK if the task was successfully received,
300 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
303 MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias,
304 double timeout,double rate)
306 return MSG_task_receive_ext_bounded(task, alias, timeout, NULL, rate);
309 /** \ingroup msg_task_usage
310 * \brief Receives a task from a mailbox from a specific host with a given timeout.
312 * This is a blocking function with a timeout, the execution flow will be blocked
313 * until the task is received or the timeout is achieved. See #MSG_task_irecv
314 * for receiving tasks asynchronously. You can provide a -1 timeout
315 * to obtain an infinite timeout.
317 * \param task a memory location for storing a #msg_task_t.
318 * \param alias name of the mailbox to receive the task from
319 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
320 * \param host a #msg_host_t host from where the task was sent
323 * #MSG_OK if the task was successfully received,
324 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
327 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
331 msg_error_t ret = MSG_OK;
333 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
336 ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
340 switch (e.category) {
341 case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */
342 ret = MSG_HOST_FAILURE;
352 /** \ingroup msg_task_usage
353 * \brief Receives a task from a mailbox from a specific host with a given timeout
354 * and at a given rate.
356 * \param task a memory location for storing a #msg_task_t.
357 * \param alias name of the mailbox to receive the task from
358 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
359 * \param host a #msg_host_t host from where the task was sent
360 * \param rate limit the reception to rate bandwidth
363 * #MSG_OK if the task was successfully received,
364 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
367 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
368 msg_host_t host, double rate)
371 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
373 return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
374 host, timeout, rate);
377 /* Internal function used to factorize code between
378 * MSG_task_isend_with_matching() and MSG_task_dsend().
381 msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
382 int (*match_fun)(void*,void*, smx_synchro_t),
383 void *match_data, void_f_pvoid_t cleanup,
386 simdata_task_t t_simdata = NULL;
387 msg_process_t process = MSG_process_self();
388 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
389 int call_end = TRACE_msg_task_put_start(task);
391 /* Prepare the task to send */
392 t_simdata = task->simdata;
393 t_simdata->sender = process;
394 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
396 if (t_simdata->isused != 0) {
397 if (msg_global->debug_multiple_use){
398 XBT_ERROR("This task is already used in there:");
399 xbt_backtrace_display(t_simdata->isused);
400 XBT_ERROR("And you try to reuse it from here:");
401 xbt_backtrace_display_current();
403 xbt_assert(t_simdata->isused == 0,
404 "This task is still being used somewhere else. You cannot send it now. Go fix your code! (use --cfg=msg/debug_multiple_use:on to get the backtrace of the other process)");
408 if (msg_global->debug_multiple_use)
409 MSG_BT(t_simdata->isused, "Using Backtrace");
411 t_simdata->isused = (void*)1;
412 t_simdata->comm = NULL;
413 msg_global->sent_msg++;
415 /* Send it by calling SIMIX network layer */
416 smx_synchro_t act = simcall_comm_isend(SIMIX_process_self(), mailbox, t_simdata->bytes_amount,
417 t_simdata->rate, task, sizeof(void *),
418 match_fun, cleanup, NULL, match_data,detached);
419 t_simdata->comm = act; /* FIXME: is the field t_simdata->comm still useful? */
425 comm = xbt_new0(s_msg_comm_t, 1);
426 comm->task_sent = task;
427 comm->task_received = NULL;
428 comm->status = MSG_OK;
432 if (TRACE_is_enabled())
433 simcall_set_category(act, task->category);
435 TRACE_msg_task_put_end();
441 /** \ingroup msg_task_usage
442 * \brief Sends a task on a mailbox.
444 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
445 * to end the communication.
447 * \param task a #msg_task_t to send on another location.
448 * \param alias name of the mailbox to sent the task to
449 * \return the msg_comm_t communication created
451 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
453 return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
456 /** \ingroup msg_task_usage
457 * \brief Sends a task on a mailbox with a maximum rate
459 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
460 * to end the communication. The maxrate parameter allows the application
461 * to limit the bandwidth utilization of network links when sending the task.
463 * \param task a #msg_task_t to send on another location.
464 * \param alias name of the mailbox to sent the task to
465 * \param maxrate the maximum communication rate for sending this task .
466 * \return the msg_comm_t communication created
468 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias,
471 task->simdata->rate = maxrate;
472 return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
476 /** \ingroup msg_task_usage
477 * \brief Sends a task on a mailbox, with support for matching requests
479 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
480 * to end the communication.
482 * \param task a #msg_task_t to send on another location.
483 * \param alias name of the mailbox to sent the task to
484 * \param match_fun boolean function which parameters are:
485 * - match_data_provided_here
486 * - match_data_provided_by_other_side_if_any
487 * - the_smx_synchro_describing_the_other_side
488 * \param match_data user provided data passed to match_fun
489 * \return the msg_comm_t communication created
491 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
492 int (*match_fun)(void*, void*,
496 return MSG_task_isend_internal(task, alias, match_fun, match_data, NULL, 0);
499 /** \ingroup msg_task_usage
500 * \brief Sends a task on a mailbox.
502 * This is a non blocking detached send function.
503 * Think of it as a best effort send. Keep in mind that the third parameter
504 * is only called if the communication fails. If the communication does work,
505 * it is responsibility of the receiver code to free anything related to
506 * the task, as usual. More details on this can be obtained on
507 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
508 * in the SimGrid-user mailing list archive.
510 * \param task a #msg_task_t to send on another location.
511 * \param alias name of the mailbox to sent the task to
512 * \param cleanup a function to destroy the task if the
513 * communication fails, e.g. MSG_task_destroy
514 * (if NULL, no function will be called)
516 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
518 MSG_task_isend_internal(task, alias, NULL, NULL, cleanup, 1);
521 /** \ingroup msg_task_usage
522 * \brief Sends a task on a mailbox with a maximal rate.
524 * This is a non blocking detached send function.
525 * Think of it as a best effort send. Keep in mind that the third parameter
526 * is only called if the communication fails. If the communication does work,
527 * it is responsibility of the receiver code to free anything related to
528 * the task, as usual. More details on this can be obtained on
529 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
530 * in the SimGrid-user mailing list archive.
532 * \param task a #msg_task_t to send on another location.
533 * \param alias name of the mailbox to sent the task to
534 * \param cleanup a function to destroy the task if the
535 * communication fails, e.g. MSG_task_destroy
536 * (if NULL, no function will be called)
537 * \param maxrate the maximum communication rate for sending this task
540 void MSG_task_dsend_bounded(msg_task_t task, const char *alias,
541 void_f_pvoid_t cleanup, double maxrate)
543 task->simdata->rate = maxrate;
544 MSG_task_dsend(task, alias, cleanup);
547 /** \ingroup msg_task_usage
548 * \brief Starts listening for receiving a task from an asynchronous communication.
550 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
551 * to end the communication.
553 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
554 * \param name of the mailbox to receive the task on
555 * \return the msg_comm_t communication created
557 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
559 return MSG_task_irecv_bounded(task, name, -1.0);
562 /** \ingroup msg_task_usage
563 * \brief Starts listening for receiving a task from an asynchronous communication
566 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
567 * \param name of the mailbox to receive the task on
568 * \param rate limit the bandwidth to the given rate
569 * \return the msg_comm_t communication created
571 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name,
574 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
576 /* FIXME: these functions are not traceable */
579 xbt_assert(task, "Null pointer for the task storage");
583 ("MSG_task_irecv() was asked to write in a non empty task struct.");
585 /* Try to receive it by calling SIMIX network layer */
586 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
587 comm->task_sent = NULL;
588 comm->task_received = task;
589 comm->status = MSG_OK;
590 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL, NULL, rate);
595 /** \ingroup msg_task_usage
596 * \brief Checks whether a communication is done, and if yes, finalizes it.
597 * \param comm the communication to test
598 * \return TRUE if the communication is finished
599 * (but it may have failed, use MSG_comm_get_status() to know its status)
600 * or FALSE if the communication is not finished yet
601 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
603 int MSG_comm_test(msg_comm_t comm)
609 finished = simcall_comm_test(comm->s_comm);
611 if (finished && comm->task_received != NULL) {
612 /* I am the receiver */
613 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
614 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
615 (*comm->task_received)->simdata->isused = 0;
619 switch (e.category) {
621 comm->status = MSG_TRANSFER_FAILURE;
626 comm->status = MSG_TIMEOUT;
639 /** \ingroup msg_task_usage
640 * \brief This function checks if a communication is finished.
641 * \param comms a vector of communications
642 * \return the position of the finished communication if any
643 * (but it may have failed, use MSG_comm_get_status() to know its status),
644 * or -1 if none is finished
646 int MSG_comm_testany(xbt_dynar_t comms)
649 int finished_index = -1;
651 /* create the equivalent dynar with SIMIX objects */
652 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
655 xbt_dynar_foreach(comms, cursor, comm) {
656 xbt_dynar_push(s_comms, &comm->s_comm);
659 msg_error_t status = MSG_OK;
661 finished_index = simcall_comm_testany(s_comms);
664 switch (e.category) {
666 finished_index = e.value;
667 status = MSG_TRANSFER_FAILURE;
671 finished_index = e.value;
672 status = MSG_TIMEOUT;
680 xbt_dynar_free(&s_comms);
682 if (finished_index != -1) {
683 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
684 /* the communication is finished */
685 comm->status = status;
687 if (status == MSG_OK && comm->task_received != NULL) {
688 /* I am the receiver */
689 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
690 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
691 (*comm->task_received)->simdata->isused = 0;
695 return finished_index;
698 /** \ingroup msg_task_usage
699 * \brief Destroys a communication.
700 * \param comm the communication to destroy.
702 void MSG_comm_destroy(msg_comm_t comm)
707 /** \ingroup msg_task_usage
708 * \brief Wait for the completion of a communication.
710 * It takes two parameters.
711 * \param comm the communication to wait.
712 * \param timeout Wait until the communication terminates or the timeout
713 * occurs. You can provide a -1 timeout to obtain an infinite timeout.
714 * \return msg_error_t
716 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
720 simcall_comm_wait(comm->s_comm, timeout);
722 if (comm->task_received != NULL) {
723 /* I am the receiver */
724 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
725 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
726 (*comm->task_received)->simdata->isused = 0;
729 /* FIXME: these functions are not traceable */
732 switch (e.category) {
734 comm->status = MSG_TRANSFER_FAILURE;
737 comm->status = MSG_TIMEOUT;
748 /** \ingroup msg_task_usage
749 * \brief This function is called by a sender and permit to wait for each communication
751 * \param comm a vector of communication
752 * \param nb_elem is the size of the comm vector
753 * \param timeout for each call of MSG_comm_wait
755 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
758 for (i = 0; i < nb_elem; i++) {
759 MSG_comm_wait(comm[i], timeout);
763 /** \ingroup msg_task_usage
764 * \brief This function waits for the first communication finished in a list.
765 * \param comms a vector of communications
766 * \return the position of the first finished communication
767 * (but it may have failed, use MSG_comm_get_status() to know its status)
769 int MSG_comm_waitany(xbt_dynar_t comms)
772 int finished_index = -1;
774 /* create the equivalent dynar with SIMIX objects */
775 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
778 xbt_dynar_foreach(comms, cursor, comm) {
779 xbt_dynar_push(s_comms, &comm->s_comm);
782 msg_error_t status = MSG_OK;
784 finished_index = simcall_comm_waitany(s_comms);
787 switch (e.category) {
789 finished_index = e.value;
790 status = MSG_TRANSFER_FAILURE;
794 finished_index = e.value;
795 status = MSG_TIMEOUT;
804 xbt_assert(finished_index != -1, "WaitAny returned -1");
805 xbt_dynar_free(&s_comms);
807 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
808 /* the communication is finished */
809 comm->status = status;
811 if (comm->task_received != NULL) {
812 /* I am the receiver */
813 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
814 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
815 (*comm->task_received)->simdata->isused = 0;
818 return finished_index;
822 * \ingroup msg_task_usage
823 * \brief Returns the error (if any) that occured during a finished communication.
824 * \param comm a finished communication
825 * \return the status of the communication, or #MSG_OK if no error occured
826 * during the communication
828 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
833 /** \ingroup msg_task_usage
834 * \brief Get a task (#msg_task_t) from a communication
836 * \param comm the communication where to get the task
837 * \return the task from the communication
839 msg_task_t MSG_comm_get_task(msg_comm_t comm)
841 xbt_assert(comm, "Invalid parameter");
843 return comm->task_received ? *comm->task_received : comm->task_sent;
847 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
848 * \param comm the comm
849 * \param buff the data copied
850 * \param buff_size size of the buffer
852 void MSG_comm_copy_data_from_SIMIX(smx_synchro_t comm, void* buff, size_t buff_size) {
855 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
857 // notify the user callback if any
858 if (msg_global->task_copy_callback) {
859 msg_task_t task = buff;
860 msg_global->task_copy_callback(task,
861 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
865 /** \ingroup msg_task_usage
866 * \brief Sends a task to a mailbox
868 * This is a blocking function, the execution flow will be blocked
869 * until the task is sent (and received in the other side if #MSG_task_receive is used).
870 * See #MSG_task_isend for sending tasks asynchronously.
872 * \param task the task to be sent
873 * \param alias the mailbox name to where the task is sent
875 * \return Returns #MSG_OK if the task was successfully sent,
876 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
878 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
880 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
881 return MSG_task_send_with_timeout(task, alias, -1);
884 /** \ingroup msg_task_usage
885 * \brief Sends a task to a mailbox with a maximum rate
887 * This is a blocking function, the execution flow will be blocked
888 * until the task is sent. The maxrate parameter allows the application
889 * to limit the bandwidth utilization of network links when sending the task.
891 * \param task the task to be sent
892 * \param alias the mailbox name to where the task is sent
893 * \param maxrate the maximum communication rate for sending this task
895 * \return Returns #MSG_OK if the task was successfully sent,
896 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
899 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
901 task->simdata->rate = maxrate;
902 return MSG_task_send(task, alias);
905 /** \ingroup msg_task_usage
906 * \brief Sends a task to a mailbox with a timeout
908 * This is a blocking function, the execution flow will be blocked
909 * until the task is sent or the timeout is achieved.
911 * \param task the task to be sent
912 * \param alias the mailbox name to where the task is sent
913 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
915 * \return Returns #MSG_OK if the task was successfully sent,
916 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
919 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
922 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
926 /** \ingroup msg_task_usage
927 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
929 * This is a blocking function, the execution flow will be blocked
930 * until the task is sent or the timeout is achieved.
932 * \param task the task to be sent
933 * \param alias the mailbox name to where the task is sent
934 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
935 * \param maxrate the maximum communication rate for sending this task
937 * \return Returns #MSG_OK if the task was successfully sent,
938 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
941 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
942 double timeout, double maxrate)
944 task->simdata->rate = maxrate;
945 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
949 /** \ingroup msg_task_usage
950 * \brief Check if there is a communication going on in a mailbox.
952 * \param alias the name of the mailbox to be considered
954 * \return Returns 1 if there is a communication, 0 otherwise
956 int MSG_task_listen(const char *alias)
958 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
961 /** \ingroup msg_task_usage
962 * \brief Check the number of communication actions of a given host pending in a mailbox.
964 * \param alias the name of the mailbox to be considered
965 * \param host the host to check for communication
967 * \return Returns the number of pending communication actions of the host in the
968 * given mailbox, 0 if there is no pending communication actions.
971 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
974 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
978 /** \ingroup msg_task_usage
979 * \brief Look if there is a communication on a mailbox and return the
980 * PID of the sender process.
982 * \param alias the name of the mailbox to be considered
984 * \return Returns the PID of sender process,
985 * -1 if there is no communication in the mailbox.
987 int MSG_task_listen_from(const char *alias)
992 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
995 return MSG_process_get_PID(task->simdata->sender);
998 /** \ingroup msg_task_usage
999 * \brief Sets the tracing category of a task.
1001 * This function should be called after the creation of
1002 * a MSG task, to define the category of that task. The
1003 * first parameter task must contain a task that was
1004 * created with the function #MSG_task_create. The second
1005 * parameter category must contain a category that was
1006 * previously declared with the function #TRACE_category
1007 * (or with #TRACE_category_with_color).
1009 * See \ref tracing for details on how to trace
1010 * the (categorized) resource utilization.
1012 * \param task the task that is going to be categorized
1013 * \param category the name of the category to be associated to the task
1015 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
1017 void MSG_task_set_category (msg_task_t task, const char *category)
1019 TRACE_msg_set_task_category (task, category);
1022 /** \ingroup msg_task_usage
1024 * \brief Gets the current tracing category of a task.
1026 * \param task the task to be considered
1028 * \see MSG_task_set_category
1030 * \return Returns the name of the tracing category of the given task, NULL otherwise
1032 const char *MSG_task_get_category (msg_task_t task)
1034 return task->category;
1038 * \brief Returns the value of a given AS or router property
1040 * \param asr the name of a router or AS
1041 * \param name a property name
1042 * \return value of a property (or NULL if property not set)
1044 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
1046 return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
1050 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
1051 * a the AS or router
1053 * \param asr the name of a router or AS
1054 * \return a dict containing the properties
1056 xbt_dict_t MSG_as_router_get_properties(const char* asr)
1058 return (simcall_asr_get_properties(asr));
1062 * \brief Change the value of a given AS or router
1064 * \param asr the name of a router or AS
1065 * \param name a property name
1066 * \param value what to change the property to
1067 * \param free_ctn the freeing function to use to kill the value on need
1069 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1070 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1073 #ifdef MSG_USE_DEPRECATED
1074 /** \ingroup msg_deprecated_functions
1076 * \brief Return the last value returned by a MSG function (except
1077 * MSG_get_errno...).
1079 msg_error_t MSG_get_errno(void)
1081 return PROCESS_GET_ERRNO();
1084 /** \ingroup msg_deprecated_functions
1085 * \brief Put a task on a channel of an host and waits for the end of the
1088 * This function is used for describing the behavior of a process. It
1089 * takes three parameter.
1090 * \param task a #msg_task_t to send on another location. This task
1091 will not be usable anymore when the function will return. There is
1092 no automatic task duplication and you have to save your parameters
1093 before calling this function. Tasks are unique and once it has been
1094 sent to another location, you should not access it anymore. You do
1095 not need to call MSG_task_destroy() but to avoid using, as an
1096 effect of inattention, this task anymore, you definitely should
1097 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1098 can be transfered iff it has been correctly created with
1100 * \param dest the destination of the message
1101 * \param channel the channel on which the process should put this
1102 task. This value has to be >=0 and < than the maximal number of
1103 channels fixed with MSG_set_channel_number().
1104 * \return #MSG_HOST_FAILURE if the host on which
1105 * this function was called was shut down,
1106 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1107 * (network failure, dest failure) or #MSG_OK if it succeeded.
1109 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
1111 XBT_WARN("DEPRECATED! Now use MSG_task_send");
1112 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1115 /** \ingroup msg_deprecated_functions
1116 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1122 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
1125 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1126 task->simdata->rate = maxrate;
1127 return MSG_task_put(task, dest, channel);
1130 /** \ingroup msg_deprecated_functions
1132 * \brief Put a task on a channel of an
1133 * host (with a timeout on the waiting of the destination host) and
1134 * waits for the end of the transmission.
1136 * This function is used for describing the behavior of a process. It
1137 * takes four parameter.
1138 * \param task a #msg_task_t to send on another location. This task
1139 will not be usable anymore when the function will return. There is
1140 no automatic task duplication and you have to save your parameters
1141 before calling this function. Tasks are unique and once it has been
1142 sent to another location, you should not access it anymore. You do
1143 not need to call MSG_task_destroy() but to avoid using, as an
1144 effect of inattention, this task anymore, you definitely should
1145 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1146 can be transfered iff it has been correctly created with
1148 * \param dest the destination of the message
1149 * \param channel the channel on which the process should put this
1150 task. This value has to be >=0 and < than the maximal number of
1151 channels fixed with MSG_set_channel_number().
1152 * \param timeout the maximum time to wait for a task before giving
1153 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1154 will not be modified
1155 * \return #MSG_HOST_FAILURE if the host on which
1156 this function was called was shut down,
1157 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1158 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1161 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
1162 m_channel_t channel, double timeout)
1164 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1165 xbt_assert((channel >= 0)
1166 && (channel < msg_global->max_channel), "Invalid channel %d",
1169 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", MSG_host_get_name(dest));
1171 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1172 (dest, channel), task, timeout);
1175 /** \ingroup msg_deprecated_functions
1176 * \brief Test whether there is a pending communication on a channel, and who sent it.
1178 * It takes one parameter.
1179 * \param channel the channel on which the process should be
1180 listening. This value has to be >=0 and < than the maximal
1181 number of channels fixed with MSG_set_channel_number().
1182 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1184 int MSG_task_probe_from(m_channel_t channel)
1186 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1189 xbt_assert((channel >= 0)
1190 && (channel < msg_global->max_channel), "Invalid channel %d",
1195 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1196 (MSG_host_self(), channel))))
1199 return MSG_process_get_PID(task->simdata->sender);
1202 /** \ingroup msg_deprecated_functions
1203 * \brief Test whether there is a pending communication on a channel.
1205 * It takes one parameter.
1206 * \param channel the channel on which the process should be
1207 listening. This value has to be >=0 and < than the maximal
1208 number of channels fixed with MSG_set_channel_number().
1209 * \return 1 if there is a pending communication and 0 otherwise
1211 int MSG_task_Iprobe(m_channel_t channel)
1213 XBT_WARN("DEPRECATED!");
1214 xbt_assert((channel >= 0)
1215 && (channel < msg_global->max_channel), "Invalid channel %d",
1219 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1220 (MSG_host_self(), channel));
1223 /** \ingroup msg_deprecated_functions
1225 * \brief Return the number of tasks waiting to be received on a \a
1226 channel and sent by \a host.
1228 * It takes two parameters.
1229 * \param channel the channel on which the process should be
1230 listening. This value has to be >=0 and < than the maximal
1231 number of channels fixed with MSG_set_channel_number().
1232 * \param host the host that is to be watched.
1233 * \return the number of tasks waiting to be received on \a channel
1234 and sent by \a host.
1236 int MSG_task_probe_from_host(int channel, msg_host_t host)
1238 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1239 xbt_assert((channel >= 0)
1240 && (channel < msg_global->max_channel), "Invalid channel %d",
1244 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1245 (MSG_host_self(), channel),
1250 /** \ingroup msg_deprecated_functions
1251 * \brief Listen on \a channel and waits for receiving a task from \a host.
1253 * It takes three parameters.
1254 * \param task a memory location for storing a #msg_task_t. It will
1255 hold a task when this function will return. Thus \a task should not
1256 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1257 those two condition does not hold, there will be a warning message.
1258 * \param channel the channel on which the process should be
1259 listening. This value has to be >=0 and < than the maximal
1260 number of channels fixed with MSG_set_channel_number().
1261 * \param host the host that is to be watched.
1262 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1265 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1267 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1268 return MSG_task_get_ext(task, channel, -1, host);
1271 /** \ingroup msg_deprecated_functions
1272 * \brief Listen on a channel and wait for receiving a task.
1274 * It takes two parameters.
1275 * \param task a memory location for storing a #msg_task_t. It will
1276 hold a task when this function will return. Thus \a task should not
1277 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1278 those two condition does not hold, there will be a warning message.
1279 * \param channel the channel on which the process should be
1280 listening. This value has to be >=0 and < than the maximal
1281 number of channels fixed with MSG_set_channel_number().
1282 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1284 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1286 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1287 return MSG_task_get_with_timeout(task, channel, -1);
1290 /** \ingroup msg_deprecated_functions
1291 * \brief Listen on a channel and wait for receiving a task with a timeout.
1293 * It takes three parameters.
1294 * \param task a memory location for storing a #msg_task_t. It will
1295 hold a task when this function will return. Thus \a task should not
1296 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1297 those two condition does not hold, there will be a warning message.
1298 * \param channel the channel on which the process should be
1299 listening. This value has to be >=0 and < than the maximal
1300 number of channels fixed with MSG_set_channel_number().
1301 * \param max_duration the maximum time to wait for a task before giving
1302 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1303 will not be modified and will still be
1304 equal to \c NULL when returning.
1305 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1308 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1309 double max_duration)
1311 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1312 return MSG_task_get_ext(task, channel, max_duration, NULL);
1316 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1319 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1320 xbt_assert((channel >= 0)
1321 && (channel < msg_global->max_channel), "Invalid channel %d",
1325 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1326 (MSG_host_self(), channel), task, host,