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 = (simdata_process_t) 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_execution_parallel_start(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)(uintptr_t) 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_execution_start(task->name,
88 simdata->flops_amount,
95 simcall_set_category(simdata->compute, task->category);
96 p_simdata->waiting_action = simdata->compute;
97 comp_state = simcall_execution_wait(simdata->compute);
99 p_simdata->waiting_action = NULL;
101 if (msg_global->debug_multiple_use && simdata->isused!=0)
102 xbt_ex_free(*(xbt_ex_t*)simdata->isused);
105 XBT_DEBUG("Execution task '%s' finished in state %d",
106 task->name, (int)comp_state);
109 switch (e.category) {
111 status = MSG_TASK_CANCELED;
114 status = MSG_HOST_FAILURE;
121 /* action ended, set comm and compute = NULL, the actions is already destroyed
122 * in the main function */
123 simdata->flops_amount = 0.0;
124 simdata->comm = NULL;
125 simdata->compute = NULL;
126 TRACE_msg_task_execute_end(task);
132 /** \ingroup msg_task_usage
133 * \brief Sleep for the specified number of seconds
135 * Makes the current process sleep until \a time seconds have elapsed.
137 * \param nb_sec a number of second
139 msg_error_t MSG_process_sleep(double nb_sec)
142 msg_error_t status = MSG_OK;
143 /*msg_process_t proc = MSG_process_self();*/
145 TRACE_msg_process_sleep_in(MSG_process_self());
148 simcall_process_sleep(nb_sec);
151 switch (e.category) {
153 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, WTF here ?");
154 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
155 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
156 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
157 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
158 // and did not change anythings at the C level.
159 // 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)
160 status = MSG_TASK_CANCELED;
168 TRACE_msg_process_sleep_out(MSG_process_self());
172 /** \ingroup msg_task_usage
173 * \brief Receives a task from a mailbox.
175 * This is a blocking function, the execution flow will be blocked
176 * until the task is received. See #MSG_task_irecv
177 * for receiving tasks asynchronously.
179 * \param task a memory location for storing a #msg_task_t.
180 * \param alias name of the mailbox to receive the task from
183 * #MSG_OK if the task was successfully received,
184 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
186 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
188 return MSG_task_receive_with_timeout(task, alias, -1);
191 /** \ingroup msg_task_usage
192 * \brief Receives a task from a mailbox at a given rate.
194 * \param task a memory location for storing a #msg_task_t.
195 * \param alias name of the mailbox to receive the task from
196 * \param rate limit the reception to rate bandwidth
199 * #MSG_OK if the task was successfully received,
200 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
202 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias,
205 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
208 /** \ingroup msg_task_usage
209 * \brief Receives a task from a mailbox with a given timeout.
211 * This is a blocking function with a timeout, the execution flow will be blocked
212 * until the task is received or the timeout is achieved. See #MSG_task_irecv
213 * for receiving tasks asynchronously. You can provide a -1 timeout
214 * to obtain an infinite timeout.
216 * \param task a memory location for storing a #msg_task_t.
217 * \param alias name of the mailbox to receive the task from
218 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
221 * #MSG_OK if the task was successfully received,
222 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
225 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
228 return MSG_task_receive_ext(task, alias, timeout, NULL);
231 /** \ingroup msg_task_usage
232 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
234 * \param task a memory location for storing a #msg_task_t.
235 * \param alias name of the mailbox to receive the task from
236 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
237 * \param rate limit the reception to rate bandwidth
240 * #MSG_OK if the task was successfully received,
241 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
244 MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias,
245 double timeout,double rate)
247 return MSG_task_receive_ext_bounded(task, alias, timeout, NULL, rate);
250 /** \ingroup msg_task_usage
251 * \brief Receives a task from a mailbox from a specific host with a given timeout.
253 * This is a blocking function with a timeout, the execution flow will be blocked
254 * until the task is received or the timeout is achieved. See #MSG_task_irecv
255 * for receiving tasks asynchronously. You can provide a -1 timeout
256 * to obtain an infinite timeout.
258 * \param task a memory location for storing a #msg_task_t.
259 * \param alias name of the mailbox to receive the task from
260 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
261 * \param host a #msg_host_t host from where the task was sent
264 * #MSG_OK if the task was successfully received,
265 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
268 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
272 msg_error_t ret = MSG_OK;
274 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
277 ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
281 switch (e.category) {
282 case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */
283 ret = MSG_HOST_FAILURE;
293 /** \ingroup msg_task_usage
294 * \brief Receives a task from a mailbox from a specific host with a given timeout
295 * and at a given rate.
297 * \param task a memory location for storing a #msg_task_t.
298 * \param alias name of the mailbox to receive the task from
299 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
300 * \param host a #msg_host_t host from where the task was sent
301 * \param rate limit the reception to rate bandwidth
304 * #MSG_OK if the task was successfully received,
305 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
308 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
309 msg_host_t host, double rate)
312 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
314 return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
315 host, timeout, rate);
318 /* Internal function used to factorize code between
319 * MSG_task_isend_with_matching() and MSG_task_dsend().
322 msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
323 int (*match_fun)(void*,void*, smx_synchro_t),
324 void *match_data, void_f_pvoid_t cleanup,
327 simdata_task_t t_simdata = NULL;
328 msg_process_t process = MSG_process_self();
329 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
330 int call_end = TRACE_msg_task_put_start(task);
332 /* Prepare the task to send */
333 t_simdata = task->simdata;
334 t_simdata->sender = process;
335 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
337 if (t_simdata->isused != 0) {
338 if (msg_global->debug_multiple_use){
339 XBT_ERROR("This task is already used in there:");
340 xbt_backtrace_display((xbt_ex_t*) t_simdata->isused);
341 XBT_ERROR("And you try to reuse it from here:");
342 xbt_backtrace_display_current();
344 xbt_assert(t_simdata->isused == 0,
345 "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)");
349 if (msg_global->debug_multiple_use)
350 MSG_BT(t_simdata->isused, "Using Backtrace");
352 t_simdata->isused = (void*)1;
353 t_simdata->comm = NULL;
354 msg_global->sent_msg++;
356 /* Send it by calling SIMIX network layer */
357 smx_synchro_t act = simcall_comm_isend(SIMIX_process_self(), mailbox, t_simdata->bytes_amount,
358 t_simdata->rate, task, sizeof(void *),
359 match_fun, cleanup, NULL, match_data,detached);
360 t_simdata->comm = act; /* FIXME: is the field t_simdata->comm still useful? */
366 comm = xbt_new0(s_msg_comm_t, 1);
367 comm->task_sent = task;
368 comm->task_received = NULL;
369 comm->status = MSG_OK;
373 if (TRACE_is_enabled())
374 simcall_set_category(act, task->category);
376 TRACE_msg_task_put_end();
382 /** \ingroup msg_task_usage
383 * \brief Sends a task on a mailbox.
385 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
386 * to end the communication.
388 * \param task a #msg_task_t to send on another location.
389 * \param alias name of the mailbox to sent the task to
390 * \return the msg_comm_t communication created
392 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
394 return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
397 /** \ingroup msg_task_usage
398 * \brief Sends a task on a mailbox with a maximum rate
400 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
401 * to end the communication. The maxrate parameter allows the application
402 * to limit the bandwidth utilization of network links when sending the task.
404 * \param task a #msg_task_t to send on another location.
405 * \param alias name of the mailbox to sent the task to
406 * \param maxrate the maximum communication rate for sending this task .
407 * \return the msg_comm_t communication created
409 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias,
412 task->simdata->rate = maxrate;
413 return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
417 /** \ingroup msg_task_usage
418 * \brief Sends a task on a mailbox, with support for matching requests
420 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
421 * to end the communication.
423 * \param task a #msg_task_t to send on another location.
424 * \param alias name of the mailbox to sent the task to
425 * \param match_fun boolean function which parameters are:
426 * - match_data_provided_here
427 * - match_data_provided_by_other_side_if_any
428 * - the_smx_synchro_describing_the_other_side
429 * \param match_data user provided data passed to match_fun
430 * \return the msg_comm_t communication created
432 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
433 int (*match_fun)(void*, void*,
437 return MSG_task_isend_internal(task, alias, match_fun, match_data, NULL, 0);
440 /** \ingroup msg_task_usage
441 * \brief Sends a task on a mailbox.
443 * This is a non blocking detached send function.
444 * Think of it as a best effort send. Keep in mind that the third parameter
445 * is only called if the communication fails. If the communication does work,
446 * it is responsibility of the receiver code to free anything related to
447 * the task, as usual. More details on this can be obtained on
448 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
449 * in the SimGrid-user mailing list archive.
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 * \param cleanup a function to destroy the task if the
454 * communication fails, e.g. MSG_task_destroy
455 * (if NULL, no function will be called)
457 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
459 MSG_task_isend_internal(task, alias, NULL, NULL, cleanup, 1);
462 /** \ingroup msg_task_usage
463 * \brief Sends a task on a mailbox with a maximal rate.
465 * This is a non blocking detached send function.
466 * Think of it as a best effort send. Keep in mind that the third parameter
467 * is only called if the communication fails. If the communication does work,
468 * it is responsibility of the receiver code to free anything related to
469 * the task, as usual. More details on this can be obtained on
470 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
471 * in the SimGrid-user mailing list archive.
473 * \param task a #msg_task_t to send on another location.
474 * \param alias name of the mailbox to sent the task to
475 * \param cleanup a function to destroy the task if the
476 * communication fails, e.g. MSG_task_destroy
477 * (if NULL, no function will be called)
478 * \param maxrate the maximum communication rate for sending this task
481 void MSG_task_dsend_bounded(msg_task_t task, const char *alias,
482 void_f_pvoid_t cleanup, double maxrate)
484 task->simdata->rate = maxrate;
485 MSG_task_dsend(task, alias, cleanup);
488 /** \ingroup msg_task_usage
489 * \brief Starts listening for receiving a task from an asynchronous communication.
491 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
492 * to end the communication.
494 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
495 * \param name of the mailbox to receive the task on
496 * \return the msg_comm_t communication created
498 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
500 return MSG_task_irecv_bounded(task, name, -1.0);
503 /** \ingroup msg_task_usage
504 * \brief Starts listening for receiving a task from an asynchronous communication
507 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
508 * \param name of the mailbox to receive the task on
509 * \param rate limit the bandwidth to the given rate
510 * \return the msg_comm_t communication created
512 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name,
515 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
517 /* FIXME: these functions are not traceable */
520 xbt_assert(task, "Null pointer for the task storage");
524 ("MSG_task_irecv() was asked to write in a non empty task struct.");
526 /* Try to receive it by calling SIMIX network layer */
527 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
528 comm->task_sent = NULL;
529 comm->task_received = task;
530 comm->status = MSG_OK;
531 comm->s_comm = simcall_comm_irecv(MSG_process_self(), rdv, task, NULL, NULL, NULL, NULL, rate);
536 /** \ingroup msg_task_usage
537 * \brief Checks whether a communication is done, and if yes, finalizes it.
538 * \param comm the communication to test
539 * \return TRUE if the communication is finished
540 * (but it may have failed, use MSG_comm_get_status() to know its status)
541 * or FALSE if the communication is not finished yet
542 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
544 int MSG_comm_test(msg_comm_t comm)
550 finished = simcall_comm_test(comm->s_comm);
552 if (finished && comm->task_received != NULL) {
553 /* I am the receiver */
554 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
555 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
556 (*comm->task_received)->simdata->isused = 0;
560 switch (e.category) {
562 comm->status = MSG_TRANSFER_FAILURE;
567 comm->status = MSG_TIMEOUT;
580 /** \ingroup msg_task_usage
581 * \brief This function checks if a communication is finished.
582 * \param comms a vector of communications
583 * \return the position of the finished communication if any
584 * (but it may have failed, use MSG_comm_get_status() to know its status),
585 * or -1 if none is finished
587 int MSG_comm_testany(xbt_dynar_t comms)
590 int finished_index = -1;
592 /* create the equivalent dynar with SIMIX objects */
593 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
596 xbt_dynar_foreach(comms, cursor, comm) {
597 xbt_dynar_push(s_comms, &comm->s_comm);
600 msg_error_t status = MSG_OK;
602 finished_index = simcall_comm_testany(s_comms);
605 switch (e.category) {
607 finished_index = e.value;
608 status = MSG_TRANSFER_FAILURE;
612 finished_index = e.value;
613 status = MSG_TIMEOUT;
621 xbt_dynar_free(&s_comms);
623 if (finished_index != -1) {
624 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
625 /* the communication is finished */
626 comm->status = status;
628 if (status == MSG_OK && comm->task_received != NULL) {
629 /* I am the receiver */
630 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
631 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
632 (*comm->task_received)->simdata->isused = 0;
636 return finished_index;
639 /** \ingroup msg_task_usage
640 * \brief Destroys a communication.
641 * \param comm the communication to destroy.
643 void MSG_comm_destroy(msg_comm_t comm)
648 /** \ingroup msg_task_usage
649 * \brief Wait for the completion of a communication.
651 * It takes two parameters.
652 * \param comm the communication to wait.
653 * \param timeout Wait until the communication terminates or the timeout
654 * occurs. You can provide a -1 timeout to obtain an infinite timeout.
655 * \return msg_error_t
657 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
661 simcall_comm_wait(comm->s_comm, timeout);
663 if (comm->task_received != NULL) {
664 /* I am the receiver */
665 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
666 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
667 (*comm->task_received)->simdata->isused = 0;
670 /* FIXME: these functions are not traceable */
673 switch (e.category) {
675 comm->status = MSG_TRANSFER_FAILURE;
678 comm->status = MSG_TIMEOUT;
689 /** \ingroup msg_task_usage
690 * \brief This function is called by a sender and permit to wait for each communication
692 * \param comm a vector of communication
693 * \param nb_elem is the size of the comm vector
694 * \param timeout for each call of MSG_comm_wait
696 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
699 for (i = 0; i < nb_elem; i++) {
700 MSG_comm_wait(comm[i], timeout);
704 /** \ingroup msg_task_usage
705 * \brief This function waits for the first communication finished in a list.
706 * \param comms a vector of communications
707 * \return the position of the first finished communication
708 * (but it may have failed, use MSG_comm_get_status() to know its status)
710 int MSG_comm_waitany(xbt_dynar_t comms)
713 int finished_index = -1;
715 /* create the equivalent dynar with SIMIX objects */
716 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
719 xbt_dynar_foreach(comms, cursor, comm) {
720 xbt_dynar_push(s_comms, &comm->s_comm);
723 msg_error_t status = MSG_OK;
725 finished_index = simcall_comm_waitany(s_comms);
728 switch (e.category) {
730 finished_index = e.value;
731 status = MSG_TRANSFER_FAILURE;
735 finished_index = e.value;
736 status = MSG_TIMEOUT;
745 xbt_assert(finished_index != -1, "WaitAny returned -1");
746 xbt_dynar_free(&s_comms);
748 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
749 /* the communication is finished */
750 comm->status = status;
752 if (comm->task_received != NULL) {
753 /* I am the receiver */
754 if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
755 xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
756 (*comm->task_received)->simdata->isused = 0;
759 return finished_index;
763 * \ingroup msg_task_usage
764 * \brief Returns the error (if any) that occured during a finished communication.
765 * \param comm a finished communication
766 * \return the status of the communication, or #MSG_OK if no error occured
767 * during the communication
769 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
774 /** \ingroup msg_task_usage
775 * \brief Get a task (#msg_task_t) from a communication
777 * \param comm the communication where to get the task
778 * \return the task from the communication
780 msg_task_t MSG_comm_get_task(msg_comm_t comm)
782 xbt_assert(comm, "Invalid parameter");
784 return comm->task_received ? *comm->task_received : comm->task_sent;
788 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
789 * \param comm the comm
790 * \param buff the data copied
791 * \param buff_size size of the buffer
793 void MSG_comm_copy_data_from_SIMIX(smx_synchro_t comm, void* buff, size_t buff_size) {
796 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
798 // notify the user callback if any
799 if (msg_global->task_copy_callback) {
800 msg_task_t task = (msg_task_t) buff;
801 msg_global->task_copy_callback(task,
802 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
806 /** \ingroup msg_task_usage
807 * \brief Sends a task to a mailbox
809 * This is a blocking function, the execution flow will be blocked
810 * until the task is sent (and received in the other side if #MSG_task_receive is used).
811 * See #MSG_task_isend for sending tasks asynchronously.
813 * \param task the task to be sent
814 * \param alias the mailbox name to where the task is sent
816 * \return Returns #MSG_OK if the task was successfully sent,
817 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
819 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
821 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
822 return MSG_task_send_with_timeout(task, alias, -1);
825 /** \ingroup msg_task_usage
826 * \brief Sends a task to a mailbox with a maximum rate
828 * This is a blocking function, the execution flow will be blocked
829 * until the task is sent. The maxrate parameter allows the application
830 * to limit the bandwidth utilization of network links when sending the task.
832 * \param task the task to be sent
833 * \param alias the mailbox name to where the task is sent
834 * \param maxrate the maximum communication rate for sending this task
836 * \return Returns #MSG_OK if the task was successfully sent,
837 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
840 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
842 task->simdata->rate = maxrate;
843 return MSG_task_send(task, alias);
846 /** \ingroup msg_task_usage
847 * \brief Sends a task to a mailbox with a timeout
849 * This is a blocking function, the execution flow will be blocked
850 * until the task is sent or the timeout is achieved.
852 * \param task the task to be sent
853 * \param alias the mailbox name to where the task is sent
854 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
856 * \return Returns #MSG_OK if the task was successfully sent,
857 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
860 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
863 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
867 /** \ingroup msg_task_usage
868 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
870 * This is a blocking function, the execution flow will be blocked
871 * until the task is sent or the timeout is achieved.
873 * \param task the task to be sent
874 * \param alias the mailbox name to where the task is sent
875 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
876 * \param maxrate the maximum communication rate for sending this task
878 * \return Returns #MSG_OK if the task was successfully sent,
879 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
882 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
883 double timeout, double maxrate)
885 task->simdata->rate = maxrate;
886 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
890 /** \ingroup msg_task_usage
891 * \brief Check if there is a communication going on in a mailbox.
893 * \param alias the name of the mailbox to be considered
895 * \return Returns 1 if there is a communication, 0 otherwise
897 int MSG_task_listen(const char *alias)
899 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
902 /** \ingroup msg_task_usage
903 * \brief Check the number of communication actions of a given host pending in a mailbox.
905 * \param alias the name of the mailbox to be considered
906 * \param host the host to check for communication
908 * \return Returns the number of pending communication actions of the host in the
909 * given mailbox, 0 if there is no pending communication actions.
912 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
915 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
919 /** \ingroup msg_task_usage
920 * \brief Look if there is a communication on a mailbox and return the
921 * PID of the sender process.
923 * \param alias the name of the mailbox to be considered
925 * \return Returns the PID of sender process,
926 * -1 if there is no communication in the mailbox.
928 int MSG_task_listen_from(const char *alias)
933 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
936 return MSG_process_get_PID(task->simdata->sender);
939 /** \ingroup msg_task_usage
940 * \brief Sets the tracing category of a task.
942 * This function should be called after the creation of
943 * a MSG task, to define the category of that task. The
944 * first parameter task must contain a task that was
945 * created with the function #MSG_task_create. The second
946 * parameter category must contain a category that was
947 * previously declared with the function #TRACE_category
948 * (or with #TRACE_category_with_color).
950 * See \ref tracing for details on how to trace
951 * the (categorized) resource utilization.
953 * \param task the task that is going to be categorized
954 * \param category the name of the category to be associated to the task
956 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
958 void MSG_task_set_category (msg_task_t task, const char *category)
960 TRACE_msg_set_task_category (task, category);
963 /** \ingroup msg_task_usage
965 * \brief Gets the current tracing category of a task.
967 * \param task the task to be considered
969 * \see MSG_task_set_category
971 * \return Returns the name of the tracing category of the given task, NULL otherwise
973 const char *MSG_task_get_category (msg_task_t task)
975 return task->category;
979 * \brief Returns the value of a given AS or router property
981 * \param asr the name of a router or AS
982 * \param name a property name
983 * \return value of a property (or NULL if property not set)
985 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
987 return (char*) xbt_dict_get_or_null(
988 MSG_as_router_get_properties(asr), name);
992 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
995 * \param asr the name of a router or AS
996 * \return a dict containing the properties
998 xbt_dict_t MSG_as_router_get_properties(const char* asr)
1000 return (simcall_asr_get_properties(asr));
1004 * \brief Change the value of a given AS or router
1006 * \param asr the name of a router or AS
1007 * \param name a property name
1008 * \param value what to change the property to
1009 * \param free_ctn the freeing function to use to kill the value on need
1011 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1012 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);