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_host_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_host_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_host_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) {
342 ret = MSG_HOST_FAILURE;
344 case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */
345 ret = MSG_HOST_FAILURE;
355 /** \ingroup msg_task_usage
356 * \brief Receives a task from a mailbox from a specific host with a given timeout
357 * and at a given rate.
359 * \param task a memory location for storing a #msg_task_t.
360 * \param alias name of the mailbox to receive the task from
361 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
362 * \param host a #msg_host_t host from where the task was sent
363 * \param rate limit the reception to rate bandwidth
366 * #MSG_OK if the task was successfully received,
367 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
370 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
371 msg_host_t host, double rate)
374 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
376 return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
377 host, timeout, rate);
380 /* Internal function used to factorize code between
381 * MSG_task_isend_with_matching() and MSG_task_dsend().
384 msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
385 int (*match_fun)(void*,void*, smx_synchro_t),
386 void *match_data, void_f_pvoid_t cleanup,
389 simdata_task_t t_simdata = NULL;
390 msg_process_t process = MSG_process_self();
391 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
392 int call_end = TRACE_msg_task_put_start(task);
394 /* Prepare the task to send */
395 t_simdata = task->simdata;
396 t_simdata->sender = process;
397 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
399 if (t_simdata->isused != 0) {
400 if (msg_global->debug_multiple_use){
401 XBT_ERROR("This task is already used in there:");
402 xbt_backtrace_display(t_simdata->isused);
403 XBT_ERROR("And you try to reuse it from here:");
404 xbt_backtrace_display_current();
406 xbt_assert(t_simdata->isused == 0,
407 "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)");
411 if (msg_global->debug_multiple_use)
412 MSG_BT(t_simdata->isused, "Using Backtrace");
414 t_simdata->isused = (void*)1;
415 t_simdata->comm = NULL;
416 msg_global->sent_msg++;
418 /* Send it by calling SIMIX network layer */
419 smx_synchro_t act = simcall_comm_isend(SIMIX_process_self(), mailbox, t_simdata->bytes_amount,
420 t_simdata->rate, task, sizeof(void *),
421 match_fun, cleanup, NULL, match_data,detached);
422 t_simdata->comm = act; /* FIXME: is the field t_simdata->comm still useful? */
428 comm = xbt_new0(s_msg_comm_t, 1);
429 comm->task_sent = task;
430 comm->task_received = NULL;
431 comm->status = MSG_OK;
435 if (TRACE_is_enabled())
436 simcall_set_category(act, task->category);
438 TRACE_msg_task_put_end();
444 /** \ingroup msg_task_usage
445 * \brief Sends a task on a mailbox.
447 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
448 * to end the communication.
450 * \param task a #msg_task_t to send on another location.
451 * \param alias name of the mailbox to sent the task to
452 * \return the msg_comm_t communication created
454 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
456 return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
459 /** \ingroup msg_task_usage
460 * \brief Sends a task on a mailbox with a maximum rate
462 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
463 * to end the communication. The maxrate parameter allows the application
464 * to limit the bandwidth utilization of network links when sending the task.
466 * \param task a #msg_task_t to send on another location.
467 * \param alias name of the mailbox to sent the task to
468 * \param maxrate the maximum communication rate for sending this task .
469 * \return the msg_comm_t communication created
471 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias,
474 task->simdata->rate = maxrate;
475 return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
479 /** \ingroup msg_task_usage
480 * \brief Sends a task on a mailbox, with support for matching requests
482 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
483 * to end the communication.
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 match_fun boolean function which parameters are:
488 * - match_data_provided_here
489 * - match_data_provided_by_other_side_if_any
490 * - the_smx_synchro_describing_the_other_side
491 * \param match_data user provided data passed to match_fun
492 * \return the msg_comm_t communication created
494 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
495 int (*match_fun)(void*, void*,
499 return MSG_task_isend_internal(task, alias, match_fun, match_data, NULL, 0);
502 /** \ingroup msg_task_usage
503 * \brief Sends a task on a mailbox.
505 * This is a non blocking detached send function.
506 * Think of it as a best effort send. Keep in mind that the third parameter
507 * is only called if the communication fails. If the communication does work,
508 * it is responsibility of the receiver code to free anything related to
509 * the task, as usual. More details on this can be obtained on
510 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
511 * in the SimGrid-user mailing list archive.
513 * \param task a #msg_task_t to send on another location.
514 * \param alias name of the mailbox to sent the task to
515 * \param cleanup a function to destroy the task if the
516 * communication fails, e.g. MSG_task_destroy
517 * (if NULL, no function will be called)
519 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
521 MSG_task_isend_internal(task, alias, NULL, NULL, cleanup, 1);
524 /** \ingroup msg_task_usage
525 * \brief Sends a task on a mailbox with a maximal rate.
527 * This is a non blocking detached send function.
528 * Think of it as a best effort send. Keep in mind that the third parameter
529 * is only called if the communication fails. If the communication does work,
530 * it is responsibility of the receiver code to free anything related to
531 * the task, as usual. More details on this can be obtained on
532 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
533 * in the SimGrid-user mailing list archive.
535 * \param task a #msg_task_t to send on another location.
536 * \param alias name of the mailbox to sent the task to
537 * \param cleanup a function to destroy the task if the
538 * communication fails, e.g. MSG_task_destroy
539 * (if NULL, no function will be called)
540 * \param maxrate the maximum communication rate for sending this task
543 void MSG_task_dsend_bounded(msg_task_t task, const char *alias,
544 void_f_pvoid_t cleanup, double maxrate)
546 task->simdata->rate = maxrate;
547 MSG_task_dsend(task, alias, cleanup);
550 /** \ingroup msg_task_usage
551 * \brief Starts listening for receiving a task from an asynchronous communication.
553 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
554 * to end the communication.
556 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
557 * \param name of the mailbox to receive the task on
558 * \return the msg_comm_t communication created
560 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
562 return MSG_task_irecv_bounded(task, name, -1.0);
565 /** \ingroup msg_task_usage
566 * \brief Starts listening for receiving a task from an asynchronous communication
569 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
570 * \param name of the mailbox to receive the task on
571 * \param rate limit the bandwidth to the given rate
572 * \return the msg_comm_t communication created
574 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name,
577 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
579 /* FIXME: these functions are not traceable */
582 xbt_assert(task, "Null pointer for the task storage");
586 ("MSG_task_irecv() was asked to write in a non empty task struct.");
588 /* Try to receive it by calling SIMIX network layer */
589 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
590 comm->task_sent = NULL;
591 comm->task_received = task;
592 comm->status = MSG_OK;
593 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL, NULL, rate);
598 /** \ingroup msg_task_usage
599 * \brief Checks whether a communication is done, and if yes, finalizes it.
600 * \param comm the communication to test
601 * \return TRUE if the communication is finished
602 * (but it may have failed, use MSG_comm_get_status() to know its status)
603 * or FALSE if the communication is not finished yet
604 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
606 int MSG_comm_test(msg_comm_t comm)
612 finished = simcall_comm_test(comm->s_comm);
614 if (finished && comm->task_received != NULL) {
615 /* I am the receiver */
616 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
617 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
618 (*comm->task_received)->simdata->isused = 0;
622 switch (e.category) {
624 comm->status = MSG_TRANSFER_FAILURE;
629 comm->status = MSG_TIMEOUT;
642 /** \ingroup msg_task_usage
643 * \brief This function checks if a communication is finished.
644 * \param comms a vector of communications
645 * \return the position of the finished communication if any
646 * (but it may have failed, use MSG_comm_get_status() to know its status),
647 * or -1 if none is finished
649 int MSG_comm_testany(xbt_dynar_t comms)
652 int finished_index = -1;
654 /* create the equivalent dynar with SIMIX objects */
655 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
658 xbt_dynar_foreach(comms, cursor, comm) {
659 xbt_dynar_push(s_comms, &comm->s_comm);
662 msg_error_t status = MSG_OK;
664 finished_index = simcall_comm_testany(s_comms);
667 switch (e.category) {
669 finished_index = e.value;
670 status = MSG_TRANSFER_FAILURE;
674 finished_index = e.value;
675 status = MSG_TIMEOUT;
683 xbt_dynar_free(&s_comms);
685 if (finished_index != -1) {
686 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
687 /* the communication is finished */
688 comm->status = status;
690 if (status == MSG_OK && comm->task_received != NULL) {
691 /* I am the receiver */
692 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
693 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
694 (*comm->task_received)->simdata->isused = 0;
698 return finished_index;
701 /** \ingroup msg_task_usage
702 * \brief Destroys a communication.
703 * \param comm the communication to destroy.
705 void MSG_comm_destroy(msg_comm_t comm)
710 /** \ingroup msg_task_usage
711 * \brief Wait for the completion of a communication.
713 * It takes two parameters.
714 * \param comm the communication to wait.
715 * \param timeout Wait until the communication terminates or the timeout
716 * occurs. You can provide a -1 timeout to obtain an infinite timeout.
717 * \return msg_error_t
719 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
723 simcall_comm_wait(comm->s_comm, timeout);
725 if (comm->task_received != NULL) {
726 /* I am the receiver */
727 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
728 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
729 (*comm->task_received)->simdata->isused = 0;
732 /* FIXME: these functions are not traceable */
735 switch (e.category) {
737 comm->status = MSG_TRANSFER_FAILURE;
740 comm->status = MSG_TIMEOUT;
751 /** \ingroup msg_task_usage
752 * \brief This function is called by a sender and permit to wait for each communication
754 * \param comm a vector of communication
755 * \param nb_elem is the size of the comm vector
756 * \param timeout for each call of MSG_comm_wait
758 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
761 for (i = 0; i < nb_elem; i++) {
762 MSG_comm_wait(comm[i], timeout);
766 /** \ingroup msg_task_usage
767 * \brief This function waits for the first communication finished in a list.
768 * \param comms a vector of communications
769 * \return the position of the first finished communication
770 * (but it may have failed, use MSG_comm_get_status() to know its status)
772 int MSG_comm_waitany(xbt_dynar_t comms)
775 int finished_index = -1;
777 /* create the equivalent dynar with SIMIX objects */
778 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
781 xbt_dynar_foreach(comms, cursor, comm) {
782 xbt_dynar_push(s_comms, &comm->s_comm);
785 msg_error_t status = MSG_OK;
787 finished_index = simcall_comm_waitany(s_comms);
790 switch (e.category) {
792 finished_index = e.value;
793 status = MSG_TRANSFER_FAILURE;
797 finished_index = e.value;
798 status = MSG_TIMEOUT;
807 xbt_assert(finished_index != -1, "WaitAny returned -1");
808 xbt_dynar_free(&s_comms);
810 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
811 /* the communication is finished */
812 comm->status = status;
814 if (comm->task_received != NULL) {
815 /* I am the receiver */
816 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
817 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
818 (*comm->task_received)->simdata->isused = 0;
821 return finished_index;
825 * \ingroup msg_task_usage
826 * \brief Returns the error (if any) that occured during a finished communication.
827 * \param comm a finished communication
828 * \return the status of the communication, or #MSG_OK if no error occured
829 * during the communication
831 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
836 /** \ingroup msg_task_usage
837 * \brief Get a task (#msg_task_t) from a communication
839 * \param comm the communication where to get the task
840 * \return the task from the communication
842 msg_task_t MSG_comm_get_task(msg_comm_t comm)
844 xbt_assert(comm, "Invalid parameter");
846 return comm->task_received ? *comm->task_received : comm->task_sent;
850 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
851 * \param comm the comm
852 * \param buff the data copied
853 * \param buff_size size of the buffer
855 void MSG_comm_copy_data_from_SIMIX(smx_synchro_t comm, void* buff, size_t buff_size) {
858 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
860 // notify the user callback if any
861 if (msg_global->task_copy_callback) {
862 msg_task_t task = buff;
863 msg_global->task_copy_callback(task,
864 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
868 /** \ingroup msg_task_usage
869 * \brief Sends a task to a mailbox
871 * This is a blocking function, the execution flow will be blocked
872 * until the task is sent (and received in the other side if #MSG_task_receive is used).
873 * See #MSG_task_isend for sending tasks asynchronously.
875 * \param task the task to be sent
876 * \param alias the mailbox name to where the task is sent
878 * \return Returns #MSG_OK if the task was successfully sent,
879 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
881 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
883 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
884 return MSG_task_send_with_timeout(task, alias, -1);
887 /** \ingroup msg_task_usage
888 * \brief Sends a task to a mailbox with a maximum rate
890 * This is a blocking function, the execution flow will be blocked
891 * until the task is sent. The maxrate parameter allows the application
892 * to limit the bandwidth utilization of network links when sending the task.
894 * \param task the task to be sent
895 * \param alias the mailbox name to where the task is sent
896 * \param maxrate the maximum communication rate for sending this task
898 * \return Returns #MSG_OK if the task was successfully sent,
899 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
902 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
904 task->simdata->rate = maxrate;
905 return MSG_task_send(task, alias);
908 /** \ingroup msg_task_usage
909 * \brief Sends a task to a mailbox with a timeout
911 * This is a blocking function, the execution flow will be blocked
912 * until the task is sent or the timeout is achieved.
914 * \param task the task to be sent
915 * \param alias the mailbox name to where the task is sent
916 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
918 * \return Returns #MSG_OK if the task was successfully sent,
919 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
922 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
925 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
929 /** \ingroup msg_task_usage
930 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
932 * This is a blocking function, the execution flow will be blocked
933 * until the task is sent or the timeout is achieved.
935 * \param task the task to be sent
936 * \param alias the mailbox name to where the task is sent
937 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
938 * \param maxrate the maximum communication rate for sending this task
940 * \return Returns #MSG_OK if the task was successfully sent,
941 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
944 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
945 double timeout, double maxrate)
947 task->simdata->rate = maxrate;
948 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
952 /** \ingroup msg_task_usage
953 * \brief Check if there is a communication going on in a mailbox.
955 * \param alias the name of the mailbox to be considered
957 * \return Returns 1 if there is a communication, 0 otherwise
959 int MSG_task_listen(const char *alias)
961 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
964 /** \ingroup msg_task_usage
965 * \brief Check the number of communication actions of a given host pending in a mailbox.
967 * \param alias the name of the mailbox to be considered
968 * \param host the host to check for communication
970 * \return Returns the number of pending communication actions of the host in the
971 * given mailbox, 0 if there is no pending communication actions.
974 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
977 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
981 /** \ingroup msg_task_usage
982 * \brief Look if there is a communication on a mailbox and return the
983 * PID of the sender process.
985 * \param alias the name of the mailbox to be considered
987 * \return Returns the PID of sender process,
988 * -1 if there is no communication in the mailbox.
990 int MSG_task_listen_from(const char *alias)
995 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
998 return MSG_process_get_PID(task->simdata->sender);
1001 /** \ingroup msg_task_usage
1002 * \brief Sets the tracing category of a task.
1004 * This function should be called after the creation of
1005 * a MSG task, to define the category of that task. The
1006 * first parameter task must contain a task that was
1007 * created with the function #MSG_task_create. The second
1008 * parameter category must contain a category that was
1009 * previously declared with the function #TRACE_category
1010 * (or with #TRACE_category_with_color).
1012 * See \ref tracing for details on how to trace
1013 * the (categorized) resource utilization.
1015 * \param task the task that is going to be categorized
1016 * \param category the name of the category to be associated to the task
1018 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
1020 void MSG_task_set_category (msg_task_t task, const char *category)
1022 TRACE_msg_set_task_category (task, category);
1025 /** \ingroup msg_task_usage
1027 * \brief Gets the current tracing category of a task.
1029 * \param task the task to be considered
1031 * \see MSG_task_set_category
1033 * \return Returns the name of the tracing category of the given task, NULL otherwise
1035 const char *MSG_task_get_category (msg_task_t task)
1037 return task->category;
1041 * \brief Returns the value of a given AS or router property
1043 * \param asr the name of a router or AS
1044 * \param name a property name
1045 * \return value of a property (or NULL if property not set)
1047 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
1049 return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
1053 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
1054 * a the AS or router
1056 * \param asr the name of a router or AS
1057 * \return a dict containing the properties
1059 xbt_dict_t MSG_as_router_get_properties(const char* asr)
1061 return (simcall_asr_get_properties(asr));
1065 * \brief Change the value of a given AS or router
1067 * \param asr the name of a router or AS
1068 * \param name a property name
1069 * \param value what to change the property to
1070 * \param free_ctn the freeing function to use to kill the value on need
1072 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1073 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1076 #ifdef MSG_USE_DEPRECATED
1077 /** \ingroup msg_deprecated_functions
1079 * \brief Return the last value returned by a MSG function (except
1080 * MSG_get_errno...).
1082 msg_error_t MSG_get_errno(void)
1084 return PROCESS_GET_ERRNO();
1087 /** \ingroup msg_deprecated_functions
1088 * \brief Put a task on a channel of an host and waits for the end of the
1091 * This function is used for describing the behavior of a process. It
1092 * takes three parameter.
1093 * \param task a #msg_task_t to send on another location. This task
1094 will not be usable anymore when the function will return. There is
1095 no automatic task duplication and you have to save your parameters
1096 before calling this function. Tasks are unique and once it has been
1097 sent to another location, you should not access it anymore. You do
1098 not need to call MSG_task_destroy() but to avoid using, as an
1099 effect of inattention, this task anymore, you definitely should
1100 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1101 can be transfered iff it has been correctly created with
1103 * \param dest the destination of the message
1104 * \param channel the channel on which the process should put this
1105 task. This value has to be >=0 and < than the maximal number of
1106 channels fixed with MSG_set_channel_number().
1107 * \return #MSG_HOST_FAILURE if the host on which
1108 * this function was called was shut down,
1109 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1110 * (network failure, dest failure) or #MSG_OK if it succeeded.
1112 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
1114 XBT_WARN("DEPRECATED! Now use MSG_task_send");
1115 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1118 /** \ingroup msg_deprecated_functions
1119 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1125 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
1128 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1129 task->simdata->rate = maxrate;
1130 return MSG_task_put(task, dest, channel);
1133 /** \ingroup msg_deprecated_functions
1135 * \brief Put a task on a channel of an
1136 * host (with a timeout on the waiting of the destination host) and
1137 * waits for the end of the transmission.
1139 * This function is used for describing the behavior of a process. It
1140 * takes four parameter.
1141 * \param task a #msg_task_t to send on another location. This task
1142 will not be usable anymore when the function will return. There is
1143 no automatic task duplication and you have to save your parameters
1144 before calling this function. Tasks are unique and once it has been
1145 sent to another location, you should not access it anymore. You do
1146 not need to call MSG_task_destroy() but to avoid using, as an
1147 effect of inattention, this task anymore, you definitely should
1148 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1149 can be transfered iff it has been correctly created with
1151 * \param dest the destination of the message
1152 * \param channel the channel on which the process should put this
1153 task. This value has to be >=0 and < than the maximal number of
1154 channels fixed with MSG_set_channel_number().
1155 * \param timeout the maximum time to wait for a task before giving
1156 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1157 will not be modified
1158 * \return #MSG_HOST_FAILURE if the host on which
1159 this function was called was shut down,
1160 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1161 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1164 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
1165 m_channel_t channel, double timeout)
1167 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1168 xbt_assert((channel >= 0)
1169 && (channel < msg_global->max_channel), "Invalid channel %d",
1172 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", MSG_host_get_name(dest));
1174 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1175 (dest, channel), task, timeout);
1178 /** \ingroup msg_deprecated_functions
1179 * \brief Test whether there is a pending communication on a channel, and who sent it.
1181 * It takes one parameter.
1182 * \param channel the channel on which the process should be
1183 listening. This value has to be >=0 and < than the maximal
1184 number of channels fixed with MSG_set_channel_number().
1185 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1187 int MSG_task_probe_from(m_channel_t channel)
1189 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1192 xbt_assert((channel >= 0)
1193 && (channel < msg_global->max_channel), "Invalid channel %d",
1198 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1199 (MSG_host_self(), channel))))
1202 return MSG_process_get_PID(task->simdata->sender);
1205 /** \ingroup msg_deprecated_functions
1206 * \brief Test whether there is a pending communication on a channel.
1208 * It takes one parameter.
1209 * \param channel the channel on which the process should be
1210 listening. This value has to be >=0 and < than the maximal
1211 number of channels fixed with MSG_set_channel_number().
1212 * \return 1 if there is a pending communication and 0 otherwise
1214 int MSG_task_Iprobe(m_channel_t channel)
1216 XBT_WARN("DEPRECATED!");
1217 xbt_assert((channel >= 0)
1218 && (channel < msg_global->max_channel), "Invalid channel %d",
1222 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1223 (MSG_host_self(), channel));
1226 /** \ingroup msg_deprecated_functions
1228 * \brief Return the number of tasks waiting to be received on a \a
1229 channel and sent by \a host.
1231 * It takes two parameters.
1232 * \param channel the channel on which the process should be
1233 listening. This value has to be >=0 and < than the maximal
1234 number of channels fixed with MSG_set_channel_number().
1235 * \param host the host that is to be watched.
1236 * \return the number of tasks waiting to be received on \a channel
1237 and sent by \a host.
1239 int MSG_task_probe_from_host(int channel, msg_host_t host)
1241 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1242 xbt_assert((channel >= 0)
1243 && (channel < msg_global->max_channel), "Invalid channel %d",
1247 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1248 (MSG_host_self(), channel),
1253 /** \ingroup msg_deprecated_functions
1254 * \brief Listen on \a channel and waits for receiving a task from \a host.
1256 * It takes three parameters.
1257 * \param task a memory location for storing a #msg_task_t. It will
1258 hold a task when this function will return. Thus \a task should not
1259 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1260 those two condition does not hold, there will be a warning message.
1261 * \param channel the channel on which the process should be
1262 listening. This value has to be >=0 and < than the maximal
1263 number of channels fixed with MSG_set_channel_number().
1264 * \param host the host that is to be watched.
1265 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1268 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1270 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1271 return MSG_task_get_ext(task, channel, -1, host);
1274 /** \ingroup msg_deprecated_functions
1275 * \brief Listen on a channel and wait for receiving a task.
1277 * It takes two parameters.
1278 * \param task a memory location for storing a #msg_task_t. It will
1279 hold a task when this function will return. Thus \a task should not
1280 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1281 those two condition does not hold, there will be a warning message.
1282 * \param channel the channel on which the process should be
1283 listening. This value has to be >=0 and < than the maximal
1284 number of channels fixed with MSG_set_channel_number().
1285 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1287 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1289 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1290 return MSG_task_get_with_timeout(task, channel, -1);
1293 /** \ingroup msg_deprecated_functions
1294 * \brief Listen on a channel and wait for receiving a task with a timeout.
1296 * It takes three parameters.
1297 * \param task a memory location for storing a #msg_task_t. It will
1298 hold a task when this function will return. Thus \a task should not
1299 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1300 those two condition does not hold, there will be a warning message.
1301 * \param channel the channel on which the process should be
1302 listening. This value has to be >=0 and < than the maximal
1303 number of channels fixed with MSG_set_channel_number().
1304 * \param max_duration the maximum time to wait for a task before giving
1305 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1306 will not be modified and will still be
1307 equal to \c NULL when returning.
1308 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1311 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1312 double max_duration)
1314 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1315 return MSG_task_get_ext(task, channel, max_duration, NULL);
1319 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1322 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1323 xbt_assert((channel >= 0)
1324 && (channel < msg_global->max_channel), "Invalid channel %d",
1328 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1329 (MSG_host_self(), channel), task, host,