1 /* Copyright (c) 2004-2014. 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;
56 TRACE_msg_task_execute_start(task);
59 xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
60 "This task is executed somewhere else. Go fix your code! %d",
61 task->simdata->isused);
63 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
65 if (simdata->computation_amount == 0 && !simdata->host_nb) {
67 TRACE_msg_task_execute_end(task);
77 if (simdata->host_nb > 0) {
78 simdata->compute = simcall_host_parallel_execute(task->name,
84 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
86 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));
87 XBT_DEBUG("execute %s@%s with affinity(0x%04lx)", MSG_task_get_name(task), MSG_host_get_name(p_simdata->m_host), affinity_mask);
89 simdata->compute = simcall_host_execute(task->name,
91 simdata->computation_amount,
99 simcall_set_category(simdata->compute, task->category);
101 p_simdata->waiting_action = simdata->compute;
102 comp_state = simcall_host_execution_wait(simdata->compute);
104 p_simdata->waiting_action = NULL;
108 XBT_DEBUG("Execution task '%s' finished in state %d",
109 task->name, (int)comp_state);
112 switch (e.category) {
114 status = MSG_TASK_CANCELED;
117 status = MSG_HOST_FAILURE;
124 /* action ended, set comm and compute = NULL, the actions is already destroyed
125 * in the main function */
126 simdata->computation_amount = 0.0;
127 simdata->comm = NULL;
128 simdata->compute = NULL;
130 TRACE_msg_task_execute_end(task);
137 /** \ingroup msg_task_usage
138 * \brief Sleep for the specified number of seconds
140 * Makes the current process sleep until \a time seconds have elapsed.
142 * \param nb_sec a number of second
144 msg_error_t MSG_process_sleep(double nb_sec)
147 msg_error_t status = MSG_OK;
148 /*msg_process_t proc = MSG_process_self();*/
151 TRACE_msg_process_sleep_in(MSG_process_self());
154 /* create action to sleep */
156 /*proc->simdata->waiting_action = act_sleep;
158 FIXME: check if not setting the waiting_action breaks something on msg
160 proc->simdata->waiting_action = NULL;*/
163 simcall_process_sleep(nb_sec);
166 switch (e.category) {
168 status = MSG_TASK_CANCELED;
177 TRACE_msg_process_sleep_out(MSG_process_self());
182 /** \ingroup msg_task_usage
183 * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
185 * Sorry, this function is not supported anymore. That wouldn't be
186 * impossible to reimplement it, but we are lacking the time to do so ourselves.
187 * If you need this functionality, you can either:
189 * - implement the buffering mechanism on the user-level by queuing all messages
190 * received in the mailbox that do not match your expectation
191 * - change your application logic to leverage the mailboxes features. For example,
192 * if you have A receiving messages from B and C, you could have A waiting on
193 * mailbox "A" most of the time, but on "A#B" when it's waiting for specific
194 * messages from B and "A#C" when waiting for messages from C. You could even get A
195 * sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
196 * an example of use of this function in the @ref MSG_examples section.
197 * - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
198 * very difficult actually. Check the function @ref MSG_mailbox_get_task_ext. During its call to
199 * simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
200 * messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
201 * and that your filtering function will receive as first parameter, and then, the filter could
202 * simply compare the host names, for example. After sufficient testing, provide an example that
203 * we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
205 * \param task a memory location for storing a #msg_task_t.
206 * \param alias name of the mailbox to receive the task from
207 * \param host a #msg_host_t host from where the task was sent
210 * #MSG_OK if the task was successfully received,
211 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
214 MSG_task_receive_from_host(msg_task_t * task, const char *alias,
217 return MSG_task_receive_ext(task, alias, -1, host);
221 *\brief Deprecated function that used to receive a task from a mailbox from a specific host
222 *\brief at a given rate
224 * \param task a memory location for storing a #msg_task_t.
225 * \param alias name of the mailbox to receive the task from
226 * \param host a #msg_host_t host from where the task was sent
227 * \param rate limit the reception to rate bandwidth
230 * #MSG_OK if the task was successfully received,
231 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
234 MSG_task_receive_from_host_bounded(msg_task_t * task, const char *alias,
235 msg_host_t host, double rate)
237 return MSG_task_receive_ext_bounded(task, alias, -1, host, rate);
240 /** \ingroup msg_task_usage
241 * \brief Receives a task from a mailbox.
243 * This is a blocking function, the execution flow will be blocked
244 * until the task is received. See #MSG_task_irecv
245 * for receiving tasks asynchronously.
247 * \param task a memory location for storing a #msg_task_t.
248 * \param alias name of the mailbox to receive the task from
251 * #MSG_OK if the task was successfully received,
252 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
254 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
256 return MSG_task_receive_with_timeout(task, alias, -1);
259 /** \ingroup msg_task_usage
260 * \brief Receives a task from a mailbox at a given rate.
262 * \param task a memory location for storing a #msg_task_t.
263 * \param alias name of the mailbox to receive the task from
264 * \param rate limit the reception to rate bandwidth
267 * #MSG_OK if the task was successfully received,
268 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
270 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias,
273 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
276 /** \ingroup msg_task_usage
277 * \brief Receives a task from a mailbox with a given timeout.
279 * This is a blocking function with a timeout, the execution flow will be blocked
280 * until the task is received or the timeout is achieved. See #MSG_task_irecv
281 * for receiving tasks asynchronously. You can provide a -1 timeout
282 * to obtain an infinite timeout.
284 * \param task a memory location for storing a #msg_task_t.
285 * \param alias name of the mailbox to receive the task from
286 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
289 * #MSG_OK if the task was successfully received,
290 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
293 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
296 return MSG_task_receive_ext(task, alias, timeout, NULL);
299 /** \ingroup msg_task_usage
300 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
302 * \param task a memory location for storing a #msg_task_t.
303 * \param alias name of the mailbox to receive the task from
304 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
305 * \param rate limit the reception to rate bandwidth
308 * #MSG_OK if the task was successfully received,
309 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
312 MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias,
313 double timeout,double rate)
315 return MSG_task_receive_ext_bounded(task, alias, timeout, NULL, rate);
318 /** \ingroup msg_task_usage
319 * \brief Receives a task from a mailbox from a specific host with a given timeout.
321 * This is a blocking function with a timeout, the execution flow will be blocked
322 * until the task is received or the timeout is achieved. See #MSG_task_irecv
323 * for receiving tasks asynchronously. You can provide a -1 timeout
324 * to obtain an infinite timeout.
326 * \param task a memory location for storing a #msg_task_t.
327 * \param alias name of the mailbox to receive the task from
328 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
329 * \param host a #msg_host_t host from where the task was sent
332 * #MSG_OK if the task was successfully received,
333 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
336 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
340 msg_error_t ret = MSG_OK;
342 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
345 ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
349 switch (e.category) {
350 case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */
351 ret = MSG_HOST_FAILURE;
361 /** \ingroup msg_task_usage
362 * \brief Receives a task from a mailbox from a specific host with a given timeout
363 * and at a given rate.
365 * \param task a memory location for storing a #msg_task_t.
366 * \param alias name of the mailbox to receive the task from
367 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
368 * \param host a #msg_host_t host from where the task was sent
369 * \param rate limit the reception to rate bandwidth
372 * #MSG_OK if the task was successfully received,
373 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
376 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
377 msg_host_t host, double rate)
380 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
382 return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
383 host, timeout, rate);
386 /* Internal function used to factorize code between
387 * MSG_task_isend_with_matching() and MSG_task_dsend().
390 msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
391 int (*match_fun)(void*,void*, smx_action_t),
392 void *match_data, void_f_pvoid_t cleanup,
395 simdata_task_t t_simdata = NULL;
396 msg_process_t process = MSG_process_self();
397 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
400 int call_end = TRACE_msg_task_put_start(task);
403 /* Prepare the task to send */
404 t_simdata = task->simdata;
405 t_simdata->sender = process;
406 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
408 xbt_assert(t_simdata->isused == 0,
409 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
411 t_simdata->isused = 1;
412 t_simdata->comm = NULL;
413 msg_global->sent_msg++;
415 /* Send it by calling SIMIX network layer */
416 smx_action_t act = simcall_comm_isend(mailbox, t_simdata->message_size,
417 t_simdata->rate, task, sizeof(void *),
418 match_fun, cleanup, 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;
433 if (TRACE_is_enabled()) {
434 simcall_set_category(act, task->category);
438 TRACE_msg_task_put_end();
445 /** \ingroup msg_task_usage
446 * \brief Sends a task on a mailbox.
448 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
449 * to end the communication.
451 * \param task a #msg_task_t to send on another location.
452 * \param alias name of the mailbox to sent the task to
453 * \return the msg_comm_t communication created
455 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
457 return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
460 /** \ingroup msg_task_usage
461 * \brief Sends a task on a mailbox with a maximum rate
463 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
464 * to end the communication. The maxrate parameter allows the application
465 * to limit the bandwidth utilization of network links when sending the task.
467 * \param task a #msg_task_t to send on another location.
468 * \param alias name of the mailbox to sent the task to
469 * \param maxrate the maximum communication rate for sending this task .
470 * \return the msg_comm_t communication created
472 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias,
475 task->simdata->rate = maxrate;
476 return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
480 /** \ingroup msg_task_usage
481 * \brief Sends a task on a mailbox, with support for matching requests
483 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
484 * to end the communication.
486 * \param task a #msg_task_t to send on another location.
487 * \param alias name of the mailbox to sent the task to
488 * \param match_fun boolean function which parameters are:
489 * - match_data_provided_here
490 * - match_data_provided_by_other_side_if_any
491 * - the_smx_action_describing_the_other_side
492 * \param match_data user provided data passed to match_fun
493 * \return the msg_comm_t communication created
495 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
496 int (*match_fun)(void*, void*,
500 return MSG_task_isend_internal(task, alias, match_fun, match_data, NULL, 0);
503 /** \ingroup msg_task_usage
504 * \brief Sends a task on a mailbox.
506 * This is a non blocking detached send function.
507 * Think of it as a best effort send. Keep in mind that the third parameter
508 * is only called if the communication fails. If the communication does work,
509 * it is responsibility of the receiver code to free anything related to
510 * the task, as usual. More details on this can be obtained on
511 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
512 * in the SimGrid-user mailing list archive.
514 * \param task a #msg_task_t to send on another location.
515 * \param alias name of the mailbox to sent the task to
516 * \param cleanup a function to destroy the task if the
517 * communication fails, e.g. MSG_task_destroy
518 * (if NULL, no function will be called)
520 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
522 MSG_task_isend_internal(task, alias, NULL, NULL, cleanup, 1);
525 /** \ingroup msg_task_usage
526 * \brief Sends a task on a mailbox with a maximal rate.
528 * This is a non blocking detached send function.
529 * Think of it as a best effort send. Keep in mind that the third parameter
530 * is only called if the communication fails. If the communication does work,
531 * it is responsibility of the receiver code to free anything related to
532 * the task, as usual. More details on this can be obtained on
533 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
534 * in the SimGrid-user mailing list archive.
536 * \param task a #msg_task_t to send on another location.
537 * \param alias name of the mailbox to sent the task to
538 * \param cleanup a function to destroy the task if the
539 * communication fails, e.g. MSG_task_destroy
540 * (if NULL, no function will be called)
541 * \param maxrate the maximum communication rate for sending this task
544 void MSG_task_dsend_bounded(msg_task_t task, const char *alias,
545 void_f_pvoid_t cleanup, double maxrate)
547 task->simdata->rate = maxrate;
548 MSG_task_dsend(task, alias, cleanup);
551 /** \ingroup msg_task_usage
552 * \brief Starts listening for receiving a task from an asynchronous communication.
554 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
555 * to end the communication.
557 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
558 * \param name of the mailbox to receive the task on
559 * \return the msg_comm_t communication created
561 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
563 return MSG_task_irecv_bounded(task, name, -1.0);
566 /** \ingroup msg_task_usage
567 * \brief Starts listening for receiving a task from an asynchronous communication
570 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
571 * \param name of the mailbox to receive the task on
572 * \param rate limit the bandwidth to the given rate
573 * \return the msg_comm_t communication created
575 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name,
578 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
580 /* FIXME: these functions are not traceable */
583 xbt_assert(task, "Null pointer for the task storage");
587 ("MSG_task_irecv() was asked to write in a non empty task struct.");
589 /* Try to receive it by calling SIMIX network layer */
590 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
591 comm->task_sent = NULL;
592 comm->task_received = task;
593 comm->status = MSG_OK;
594 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL, rate);
599 /** \ingroup msg_task_usage
600 * \brief Checks whether a communication is done, and if yes, finalizes it.
601 * \param comm the communication to test
602 * \return TRUE if the communication is finished
603 * (but it may have failed, use MSG_comm_get_status() to know its status)
604 * or FALSE if the communication is not finished yet
605 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
607 int MSG_comm_test(msg_comm_t comm)
613 finished = simcall_comm_test(comm->s_comm);
615 if (finished && comm->task_received != NULL) {
616 /* I am the receiver */
617 (*comm->task_received)->simdata->isused = 0;
621 switch (e.category) {
623 comm->status = MSG_TRANSFER_FAILURE;
628 comm->status = MSG_TIMEOUT;
641 /** \ingroup msg_task_usage
642 * \brief This function checks if a communication is finished.
643 * \param comms a vector of communications
644 * \return the position of the finished communication if any
645 * (but it may have failed, use MSG_comm_get_status() to know its status),
646 * or -1 if none is finished
648 int MSG_comm_testany(xbt_dynar_t comms)
651 int finished_index = -1;
653 /* create the equivalent dynar with SIMIX objects */
654 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
657 xbt_dynar_foreach(comms, cursor, comm) {
658 xbt_dynar_push(s_comms, &comm->s_comm);
661 msg_error_t status = MSG_OK;
663 finished_index = simcall_comm_testany(s_comms);
666 switch (e.category) {
668 finished_index = e.value;
669 status = MSG_TRANSFER_FAILURE;
673 finished_index = e.value;
674 status = MSG_TIMEOUT;
682 xbt_dynar_free(&s_comms);
684 if (finished_index != -1) {
685 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
686 /* the communication is finished */
687 comm->status = status;
689 if (status == MSG_OK && comm->task_received != NULL) {
690 /* I am the receiver */
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 (*comm->task_received)->simdata->isused = 0;
727 /* FIXME: these functions are not traceable */
730 switch (e.category) {
732 comm->status = MSG_TRANSFER_FAILURE;
735 comm->status = MSG_TIMEOUT;
746 /** \ingroup msg_task_usage
747 * \brief This function is called by a sender and permit to wait for each communication
749 * \param comm a vector of communication
750 * \param nb_elem is the size of the comm vector
751 * \param timeout for each call of MSG_comm_wait
753 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
756 for (i = 0; i < nb_elem; i++) {
757 MSG_comm_wait(comm[i], timeout);
761 /** \ingroup msg_task_usage
762 * \brief This function waits for the first communication finished in a list.
763 * \param comms a vector of communications
764 * \return the position of the first finished communication
765 * (but it may have failed, use MSG_comm_get_status() to know its status)
767 int MSG_comm_waitany(xbt_dynar_t comms)
770 int finished_index = -1;
772 /* create the equivalent dynar with SIMIX objects */
773 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
776 xbt_dynar_foreach(comms, cursor, comm) {
777 xbt_dynar_push(s_comms, &comm->s_comm);
780 msg_error_t status = MSG_OK;
782 finished_index = simcall_comm_waitany(s_comms);
785 switch (e.category) {
787 finished_index = e.value;
788 status = MSG_TRANSFER_FAILURE;
792 finished_index = e.value;
793 status = MSG_TIMEOUT;
802 xbt_assert(finished_index != -1, "WaitAny returned -1");
803 xbt_dynar_free(&s_comms);
805 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
806 /* the communication is finished */
807 comm->status = status;
809 if (comm->task_received != NULL) {
810 /* I am the receiver */
811 (*comm->task_received)->simdata->isused = 0;
814 return finished_index;
818 * \ingroup msg_task_usage
819 * \brief Returns the error (if any) that occured during a finished communication.
820 * \param comm a finished communication
821 * \return the status of the communication, or #MSG_OK if no error occured
822 * during the communication
824 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
829 /** \ingroup msg_task_usage
830 * \brief Get a task (#msg_task_t) from a communication
832 * \param comm the communication where to get the task
833 * \return the task from the communication
835 msg_task_t MSG_comm_get_task(msg_comm_t comm)
837 xbt_assert(comm, "Invalid parameter");
839 return comm->task_received ? *comm->task_received : comm->task_sent;
843 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
844 * \param comm the comm
845 * \param buff the data copied
846 * \param buff_size size of the buffer
848 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
851 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
853 // notify the user callback if any
854 if (msg_global->task_copy_callback) {
855 msg_task_t task = buff;
856 msg_global->task_copy_callback(task,
857 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
861 /** \ingroup msg_task_usage
862 * \brief Sends a task to a mailbox
864 * This is a blocking function, the execution flow will be blocked
865 * until the task is sent (and received in the other side if #MSG_task_receive is used).
866 * See #MSG_task_isend for sending tasks asynchronously.
868 * \param task the task to be sent
869 * \param alias the mailbox name to where the task is sent
871 * \return Returns #MSG_OK if the task was successfully sent,
872 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
874 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
876 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
877 return MSG_task_send_with_timeout(task, alias, -1);
880 /** \ingroup msg_task_usage
881 * \brief Sends a task to a mailbox with a maximum rate
883 * This is a blocking function, the execution flow will be blocked
884 * until the task is sent. The maxrate parameter allows the application
885 * to limit the bandwidth utilization of network links when sending the task.
887 * \param task the task to be sent
888 * \param alias the mailbox name to where the task is sent
889 * \param maxrate the maximum communication rate for sending this task
891 * \return Returns #MSG_OK if the task was successfully sent,
892 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
895 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
897 task->simdata->rate = maxrate;
898 return MSG_task_send(task, alias);
901 /** \ingroup msg_task_usage
902 * \brief Sends a task to a mailbox with a timeout
904 * This is a blocking function, the execution flow will be blocked
905 * until the task is sent or the timeout is achieved.
907 * \param task the task to be sent
908 * \param alias the mailbox name to where the task is sent
909 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
911 * \return Returns #MSG_OK if the task was successfully sent,
912 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
915 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
918 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
922 /** \ingroup msg_task_usage
923 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
925 * This is a blocking function, the execution flow will be blocked
926 * until the task is sent or the timeout is achieved.
928 * \param task the task to be sent
929 * \param alias the mailbox name to where the task is sent
930 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
931 * \param maxrate the maximum communication rate for sending this task
933 * \return Returns #MSG_OK if the task was successfully sent,
934 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
937 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
938 double timeout, double maxrate)
940 task->simdata->rate = maxrate;
941 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
945 /** \ingroup msg_task_usage
946 * \brief Check if there is a communication going on in a mailbox.
948 * \param alias the name of the mailbox to be considered
950 * \return Returns 1 if there is a communication, 0 otherwise
952 int MSG_task_listen(const char *alias)
954 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
957 /** \ingroup msg_task_usage
958 * \brief Check the number of communication actions of a given host pending in a mailbox.
960 * \param alias the name of the mailbox to be considered
961 * \param host the host to check for communication
963 * \return Returns the number of pending communication actions of the host in the
964 * given mailbox, 0 if there is no pending communication actions.
967 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
970 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
974 /** \ingroup msg_task_usage
975 * \brief Look if there is a communication on a mailbox and return the
976 * PID of the sender process.
978 * \param alias the name of the mailbox to be considered
980 * \return Returns the PID of sender process,
981 * -1 if there is no communication in the mailbox.
983 int MSG_task_listen_from(const char *alias)
988 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
991 return MSG_process_get_PID(task->simdata->sender);
994 /** \ingroup msg_task_usage
995 * \brief Sets the tracing category of a task.
997 * This function should be called after the creation of
998 * a MSG task, to define the category of that task. The
999 * first parameter task must contain a task that was
1000 * created with the function #MSG_task_create. The second
1001 * parameter category must contain a category that was
1002 * previously declared with the function #TRACE_category
1003 * (or with #TRACE_category_with_color).
1005 * See \ref tracing for details on how to trace
1006 * the (categorized) resource utilization.
1008 * \param task the task that is going to be categorized
1009 * \param category the name of the category to be associated to the task
1011 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
1013 void MSG_task_set_category (msg_task_t task, const char *category)
1016 TRACE_msg_set_task_category (task, category);
1020 /** \ingroup msg_task_usage
1022 * \brief Gets the current tracing category of a task.
1024 * \param task the task to be considered
1026 * \see MSG_task_set_category
1028 * \return Returns the name of the tracing category of the given task, NULL otherwise
1030 const char *MSG_task_get_category (msg_task_t task)
1033 return task->category;
1040 * \brief Returns the value of a given AS or router property
1042 * \param asr the name of a router or AS
1043 * \param name a property name
1044 * \return value of a property (or NULL if property not set)
1046 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
1048 return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
1052 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
1053 * a the AS or router
1055 * \param asr the name of a router or AS
1056 * \return a dict containing the properties
1058 xbt_dict_t MSG_as_router_get_properties(const char* asr)
1060 return (simcall_asr_get_properties(asr));
1064 * \brief Change the value of a given AS or router
1066 * \param asr the name of a router or AS
1067 * \param name a property name
1068 * \param value what to change the property to
1069 * \param free_ctn the freeing function to use to kill the value on need
1071 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1072 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1075 #ifdef MSG_USE_DEPRECATED
1076 /** \ingroup msg_deprecated_functions
1078 * \brief Return the last value returned by a MSG function (except
1079 * MSG_get_errno...).
1081 msg_error_t MSG_get_errno(void)
1083 return PROCESS_GET_ERRNO();
1086 /** \ingroup msg_deprecated_functions
1087 * \brief Put a task on a channel of an host and waits for the end of the
1090 * This function is used for describing the behavior of a process. It
1091 * takes three parameter.
1092 * \param task a #msg_task_t to send on another location. This task
1093 will not be usable anymore when the function will return. There is
1094 no automatic task duplication and you have to save your parameters
1095 before calling this function. Tasks are unique and once it has been
1096 sent to another location, you should not access it anymore. You do
1097 not need to call MSG_task_destroy() but to avoid using, as an
1098 effect of inattention, this task anymore, you definitely should
1099 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1100 can be transfered iff it has been correctly created with
1102 * \param dest the destination of the message
1103 * \param channel the channel on which the process should put this
1104 task. This value has to be >=0 and < than the maximal number of
1105 channels fixed with MSG_set_channel_number().
1106 * \return #MSG_HOST_FAILURE if the host on which
1107 * this function was called was shut down,
1108 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1109 * (network failure, dest failure) or #MSG_OK if it succeeded.
1111 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
1113 XBT_WARN("DEPRECATED! Now use MSG_task_send");
1114 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1117 /** \ingroup msg_deprecated_functions
1118 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1124 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
1127 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1128 task->simdata->rate = maxrate;
1129 return MSG_task_put(task, dest, channel);
1132 /** \ingroup msg_deprecated_functions
1134 * \brief Put a task on a channel of an
1135 * host (with a timeout on the waiting of the destination host) and
1136 * waits for the end of the transmission.
1138 * This function is used for describing the behavior of a process. It
1139 * takes four parameter.
1140 * \param task a #msg_task_t to send on another location. This task
1141 will not be usable anymore when the function will return. There is
1142 no automatic task duplication and you have to save your parameters
1143 before calling this function. Tasks are unique and once it has been
1144 sent to another location, you should not access it anymore. You do
1145 not need to call MSG_task_destroy() but to avoid using, as an
1146 effect of inattention, this task anymore, you definitely should
1147 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1148 can be transfered iff it has been correctly created with
1150 * \param dest the destination of the message
1151 * \param channel the channel on which the process should put this
1152 task. This value has to be >=0 and < than the maximal number of
1153 channels fixed with MSG_set_channel_number().
1154 * \param timeout the maximum time to wait for a task before giving
1155 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1156 will not be modified
1157 * \return #MSG_HOST_FAILURE if the host on which
1158 this function was called was shut down,
1159 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1160 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1163 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
1164 m_channel_t channel, double timeout)
1166 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1167 xbt_assert((channel >= 0)
1168 && (channel < msg_global->max_channel), "Invalid channel %d",
1171 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", MSG_host_get_name(dest));
1173 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1174 (dest, channel), task, timeout);
1177 /** \ingroup msg_deprecated_functions
1178 * \brief Test whether there is a pending communication on a channel, and who sent it.
1180 * It takes one parameter.
1181 * \param channel the channel on which the process should be
1182 listening. This value has to be >=0 and < than the maximal
1183 number of channels fixed with MSG_set_channel_number().
1184 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1186 int MSG_task_probe_from(m_channel_t channel)
1188 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1191 xbt_assert((channel >= 0)
1192 && (channel < msg_global->max_channel), "Invalid channel %d",
1197 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1198 (MSG_host_self(), channel))))
1201 return MSG_process_get_PID(task->simdata->sender);
1204 /** \ingroup msg_deprecated_functions
1205 * \brief Test whether there is a pending communication on a channel.
1207 * It takes one parameter.
1208 * \param channel the channel on which the process should be
1209 listening. This value has to be >=0 and < than the maximal
1210 number of channels fixed with MSG_set_channel_number().
1211 * \return 1 if there is a pending communication and 0 otherwise
1213 int MSG_task_Iprobe(m_channel_t channel)
1215 XBT_WARN("DEPRECATED!");
1216 xbt_assert((channel >= 0)
1217 && (channel < msg_global->max_channel), "Invalid channel %d",
1221 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1222 (MSG_host_self(), channel));
1225 /** \ingroup msg_deprecated_functions
1227 * \brief Return the number of tasks waiting to be received on a \a
1228 channel and sent by \a host.
1230 * It takes two parameters.
1231 * \param channel the channel on which the process should be
1232 listening. This value has to be >=0 and < than the maximal
1233 number of channels fixed with MSG_set_channel_number().
1234 * \param host the host that is to be watched.
1235 * \return the number of tasks waiting to be received on \a channel
1236 and sent by \a host.
1238 int MSG_task_probe_from_host(int channel, msg_host_t host)
1240 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1241 xbt_assert((channel >= 0)
1242 && (channel < msg_global->max_channel), "Invalid channel %d",
1246 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1247 (MSG_host_self(), channel),
1252 /** \ingroup msg_deprecated_functions
1253 * \brief Listen on \a channel and waits for receiving a task from \a host.
1255 * It takes three parameters.
1256 * \param task a memory location for storing a #msg_task_t. It will
1257 hold a task when this function will return. Thus \a task should not
1258 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1259 those two condition does not hold, there will be a warning message.
1260 * \param channel the channel on which the process should be
1261 listening. This value has to be >=0 and < than the maximal
1262 number of channels fixed with MSG_set_channel_number().
1263 * \param host the host that is to be watched.
1264 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1267 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1269 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1270 return MSG_task_get_ext(task, channel, -1, host);
1273 /** \ingroup msg_deprecated_functions
1274 * \brief Listen on a channel and wait for receiving a task.
1276 * It takes two parameters.
1277 * \param task a memory location for storing a #msg_task_t. It will
1278 hold a task when this function will return. Thus \a task should not
1279 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1280 those two condition does not hold, there will be a warning message.
1281 * \param channel the channel on which the process should be
1282 listening. This value has to be >=0 and < than the maximal
1283 number of channels fixed with MSG_set_channel_number().
1284 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1286 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1288 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1289 return MSG_task_get_with_timeout(task, channel, -1);
1292 /** \ingroup msg_deprecated_functions
1293 * \brief Listen on a channel and wait for receiving a task with a timeout.
1295 * It takes three parameters.
1296 * \param task a memory location for storing a #msg_task_t. It will
1297 hold a task when this function will return. Thus \a task should not
1298 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1299 those two condition does not hold, there will be a warning message.
1300 * \param channel the channel on which the process should be
1301 listening. This value has to be >=0 and < than the maximal
1302 number of channels fixed with MSG_set_channel_number().
1303 * \param max_duration the maximum time to wait for a task before giving
1304 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1305 will not be modified and will still be
1306 equal to \c NULL when returning.
1307 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1310 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1311 double max_duration)
1313 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1314 return MSG_task_get_ext(task, channel, max_duration, NULL);
1318 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1321 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1322 xbt_assert((channel >= 0)
1323 && (channel < msg_global->max_channel), "Invalid channel %d",
1327 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1328 (MSG_host_self(), channel), task, host,