1 /* Copyright (c) 2004-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "src/msg/msg_private.h"
9 #include "src/simix/smx_private.h" /* MSG_task_listen looks inside the rdv directly. Not clean. */
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
13 /** \ingroup msg_task_usage
14 * \brief Executes a task and waits for its termination.
16 * This function is used for describing the behavior of a process. It takes only one parameter.
17 * \param task a #msg_task_t to execute on the location on which the process is running.
18 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
20 msg_error_t MSG_task_execute(msg_task_t task)
22 /* TODO: add this to other locations */
23 msg_host_t host = MSG_process_get_host(MSG_process_self());
24 MSG_host_add_task(host, task);
26 msg_error_t ret = MSG_parallel_task_execute(task);
28 MSG_host_del_task(host, task);
32 /** \ingroup msg_task_usage
33 * \brief Executes a parallel task and waits for its termination.
35 * \param task a #msg_task_t to execute on the location on which the process is running.
37 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
38 * or #MSG_HOST_FAILURE otherwise
40 msg_error_t MSG_parallel_task_execute(msg_task_t task)
42 return MSG_parallel_task_execute_with_timeout(task, -1);
45 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
47 simdata_task_t simdata = task->simdata;
48 simdata_process_t p_simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
49 e_smx_state_t comp_state;
50 msg_error_t status = MSG_OK;
52 TRACE_msg_task_execute_start(task);
54 xbt_assert((!simdata->compute) && !task->simdata->isused, "This task is executed somewhere else. Go fix your code!");
56 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
58 if (simdata->flops_amount <= 0.0 && !simdata->host_nb) {
59 TRACE_msg_task_execute_end(task);
66 if (simdata->host_nb > 0) {
67 simdata->compute = static_cast<simgrid::kernel::activity::Exec*>(simcall_execution_parallel_start(
68 task->name, simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
69 simdata->bytes_parallel_amount, 1.0, -1.0, timeout));
70 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
72 simdata->compute = static_cast<simgrid::kernel::activity::Exec*>(
73 simcall_execution_start(task->name, simdata->flops_amount, simdata->priority, simdata->bound));
75 simcall_set_category(simdata->compute, task->category);
76 p_simdata->waiting_action = simdata->compute;
77 comp_state = simcall_execution_wait(simdata->compute);
79 p_simdata->waiting_action = nullptr;
80 simdata->setNotUsed();
82 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
87 status = MSG_TASK_CANCELED;
90 status = MSG_HOST_FAILURE;
100 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
101 simdata->flops_amount = 0.0;
102 simdata->comm = nullptr;
103 simdata->compute = nullptr;
104 TRACE_msg_task_execute_end(task);
109 /** \ingroup msg_task_usage
110 * \brief Sleep for the specified number of seconds
112 * Makes the current process sleep until \a time seconds have elapsed.
114 * \param nb_sec a number of second
116 msg_error_t MSG_process_sleep(double nb_sec)
118 msg_error_t status = MSG_OK;
120 TRACE_msg_process_sleep_in(MSG_process_self());
123 simcall_process_sleep(nb_sec);
126 switch (e.category) {
128 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
129 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
130 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
131 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
132 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
133 // and did not change anythings at the C level.
134 // 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)
135 status = MSG_TASK_CANCELED;
142 TRACE_msg_process_sleep_out(MSG_process_self());
146 /** \ingroup msg_task_usage
147 * \brief Receives a task from a mailbox.
149 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
150 * for receiving tasks asynchronously.
152 * \param task a memory location for storing a #msg_task_t.
153 * \param alias name of the mailbox to receive the task from
156 * #MSG_OK if the task was successfully received,
157 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
159 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
161 return MSG_task_receive_with_timeout(task, alias, -1);
164 /** \ingroup msg_task_usage
165 * \brief Receives a task from a mailbox at a given rate.
167 * \param task a memory location for storing a #msg_task_t.
168 * \param alias name of the mailbox to receive the task from
169 * \param rate limit the reception to rate bandwidth
172 * #MSG_OK if the task was successfully received,
173 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
175 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
177 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
180 /** \ingroup msg_task_usage
181 * \brief Receives a task from a mailbox with a given timeout.
183 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
184 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
185 * to obtain an infinite timeout.
187 * \param task a memory location for storing a #msg_task_t.
188 * \param alias name of the mailbox to receive the task from
189 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
192 * #MSG_OK if the task was successfully received,
193 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
195 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
197 return MSG_task_receive_ext(task, alias, timeout, nullptr);
200 /** \ingroup msg_task_usage
201 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
203 * \param task a memory location for storing a #msg_task_t.
204 * \param alias name of the mailbox to receive the task from
205 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
206 * \param rate limit the reception to rate bandwidth
209 * #MSG_OK if the task was successfully received,
210 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
212 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
214 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
217 /** \ingroup msg_task_usage
218 * \brief Receives a task from a mailbox from a specific host with a given timeout.
220 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
221 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
222 * to obtain an infinite timeout.
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 timeout is the maximum wait time for completion (provide -1 for no timeout)
227 * \param host a #msg_host_t host from where the task was sent
230 * #MSG_OK if the task was successfully received,
231 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
233 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
235 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
236 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
239 /** \ingroup msg_task_usage
240 * \brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
242 * \param task a memory location for storing a #msg_task_t.
243 * \param alias name of the mailbox to receive the task from
244 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
245 * \param host a #msg_host_t host from where the task was sent
246 * \param rate limit the reception to rate bandwidth
249 * #MSG_OK if the task was successfully received,
250 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
252 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
255 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
256 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
257 msg_error_t ret = MSG_OK;
258 /* We no longer support getting a task from a specific host */
262 TRACE_msg_task_get_start();
263 double start_time = MSG_get_clock();
266 xbt_assert(task, "Null pointer for the task storage");
269 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
271 /* Try to receive it by calling SIMIX network layer */
273 simcall_comm_recv(MSG_process_self(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
274 XBT_DEBUG("Got task %s from %s",(*task)->name,mailbox->name());
275 (*task)->simdata->setNotUsed();
278 switch (e.category) {
281 ret = MSG_HOST_FAILURE;
284 ret = MSG_TRANSFER_FAILURE;
294 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
295 TRACE_msg_task_get_end(start_time, *task);
300 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
301 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
302 int (*match_fun)(void*,void*, smx_activity_t),
303 void *match_data, void_f_pvoid_t cleanup, int detached)
305 simdata_task_t t_simdata = nullptr;
306 msg_process_t myself = SIMIX_process_self();
307 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
308 int call_end = TRACE_msg_task_put_start(task);
310 /* Prepare the task to send */
311 t_simdata = task->simdata;
312 t_simdata->sender = myself;
313 t_simdata->source = (static_cast<simdata_process_t>(SIMIX_process_self_get_data()))->m_host;
314 t_simdata->setUsed();
315 t_simdata->comm = nullptr;
316 msg_global->sent_msg++;
318 /* Send it by calling SIMIX network layer */
319 smx_activity_t act = simcall_comm_isend(myself, mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate,
320 task, sizeof(void *), match_fun, cleanup, nullptr, match_data,detached);
321 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(act);
323 msg_comm_t comm = nullptr;
325 comm = xbt_new0(s_msg_comm_t, 1);
326 comm->task_sent = task;
327 comm->task_received = nullptr;
328 comm->status = MSG_OK;
332 if (TRACE_is_enabled())
333 simcall_set_category(act, task->category);
335 TRACE_msg_task_put_end();
340 /** \ingroup msg_task_usage
341 * \brief Sends a task on a mailbox.
343 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
345 * \param task a #msg_task_t to send on another location.
346 * \param alias name of the mailbox to sent the task to
347 * \return the msg_comm_t communication created
349 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
351 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
354 /** \ingroup msg_task_usage
355 * \brief Sends a task on a mailbox with a maximum rate
357 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
358 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
360 * \param task a #msg_task_t to send on another location.
361 * \param alias name of the mailbox to sent the task to
362 * \param maxrate the maximum communication rate for sending this task .
363 * \return the msg_comm_t communication created
365 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
367 task->simdata->rate = maxrate;
368 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
371 /** \ingroup msg_task_usage
372 * \brief Sends a task on a mailbox, with support for matching requests
374 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
376 * \param task a #msg_task_t to send on another location.
377 * \param alias name of the mailbox to sent the task to
378 * \param match_fun boolean function which parameters are:
379 * - match_data_provided_here
380 * - match_data_provided_by_other_side_if_any
381 * - the_smx_synchro_describing_the_other_side
382 * \param match_data user provided data passed to match_fun
383 * \return the msg_comm_t communication created
385 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
386 int (*match_fun)(void*, void*, smx_activity_t), void *match_data)
388 return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
391 /** \ingroup msg_task_usage
392 * \brief Sends a task on a mailbox.
394 * This is a non blocking detached send function.
395 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
396 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
397 * usual. More details on this can be obtained on
398 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
399 * in the SimGrid-user mailing list archive.
401 * \param task a #msg_task_t to send on another location.
402 * \param alias name of the mailbox to sent the task to
403 * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
404 * (if nullptr, no function will be called)
406 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
408 MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
411 /** \ingroup msg_task_usage
412 * \brief Sends a task on a mailbox with a maximal rate.
414 * This is a non blocking detached send function.
415 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
416 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
417 * usual. More details on this can be obtained on
418 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
419 * in the SimGrid-user mailing list archive.
421 * \param task a #msg_task_t to send on another location.
422 * \param alias name of the mailbox to sent the task to
423 * \param cleanup a function to destroy the task if the
424 * communication fails, e.g. MSG_task_destroy
425 * (if nullptr, no function will be called)
426 * \param maxrate the maximum communication rate for sending this task
429 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
431 task->simdata->rate = maxrate;
432 MSG_task_dsend(task, alias, cleanup);
435 /** \ingroup msg_task_usage
436 * \brief Starts listening for receiving a task from an asynchronous communication.
438 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
440 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
441 * \param name of the mailbox to receive the task on
442 * \return the msg_comm_t communication created
444 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
446 return MSG_task_irecv_bounded(task, name, -1.0);
449 /** \ingroup msg_task_usage
450 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
452 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
453 * \param name of the mailbox to receive the task on
454 * \param rate limit the bandwidth to the given rate
455 * \return the msg_comm_t communication created
457 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
459 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
461 /* FIXME: these functions are not traceable */
463 xbt_assert(task, "Null pointer for the task storage");
466 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
468 /* Try to receive it by calling SIMIX network layer */
469 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
470 comm->task_sent = nullptr;
471 comm->task_received = task;
472 comm->status = MSG_OK;
473 comm->s_comm = simcall_comm_irecv(MSG_process_self(), mbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, rate);
478 /** \ingroup msg_task_usage
479 * \brief Checks whether a communication is done, and if yes, finalizes it.
480 * \param comm the communication to test
481 * \return TRUE if the communication is finished
482 * (but it may have failed, use MSG_comm_get_status() to know its status)
483 * or FALSE if the communication is not finished yet
484 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
486 int MSG_comm_test(msg_comm_t comm)
491 finished = simcall_comm_test(comm->s_comm);
492 if (finished && comm->task_received != nullptr) {
493 /* I am the receiver */
494 (*comm->task_received)->simdata->setNotUsed();
498 switch (e.category) {
500 comm->status = MSG_TRANSFER_FAILURE;
504 comm->status = MSG_TIMEOUT;
515 /** \ingroup msg_task_usage
516 * \brief This function checks if a communication is finished.
517 * \param comms a vector of communications
518 * \return the position of the finished communication if any
519 * (but it may have failed, use MSG_comm_get_status() to know its status),
520 * or -1 if none is finished
522 int MSG_comm_testany(xbt_dynar_t comms)
524 int finished_index = -1;
526 /* Create the equivalent array with SIMIX objects: */
527 std::vector<simgrid::kernel::activity::ActivityImpl*> s_comms;
528 s_comms.reserve(xbt_dynar_length(comms));
531 xbt_dynar_foreach(comms, cursor, comm) {
532 s_comms.push_back(comm->s_comm);
535 msg_error_t status = MSG_OK;
537 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
540 switch (e.category) {
542 finished_index = e.value;
543 status = MSG_TRANSFER_FAILURE;
546 finished_index = e.value;
547 status = MSG_TIMEOUT;
554 if (finished_index != -1) {
555 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
556 /* the communication is finished */
557 comm->status = status;
559 if (status == MSG_OK && comm->task_received != nullptr) {
560 /* I am the receiver */
561 (*comm->task_received)->simdata->setNotUsed();
565 return finished_index;
568 /** \ingroup msg_task_usage
569 * \brief Destroys a communication.
570 * \param comm the communication to destroy.
572 void MSG_comm_destroy(msg_comm_t comm)
577 /** \ingroup msg_task_usage
578 * \brief Wait for the completion of a communication.
580 * It takes two parameters.
581 * \param comm the communication to wait.
582 * \param timeout Wait until the communication terminates or the timeout occurs.
583 * You can provide a -1 timeout to obtain an infinite timeout.
584 * \return msg_error_t
586 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
589 simcall_comm_wait(comm->s_comm, timeout);
591 if (comm->task_received != nullptr) {
592 /* I am the receiver */
593 (*comm->task_received)->simdata->setNotUsed();
596 /* FIXME: these functions are not traceable */
599 switch (e.category) {
601 comm->status = MSG_TRANSFER_FAILURE;
604 comm->status = MSG_TIMEOUT;
614 /** \ingroup msg_task_usage
615 * \brief This function is called by a sender and permit to wait for each communication
617 * \param comm a vector of communication
618 * \param nb_elem is the size of the comm vector
619 * \param timeout for each call of MSG_comm_wait
621 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
623 for (int i = 0; i < nb_elem; i++)
624 MSG_comm_wait(comm[i], timeout);
627 /** \ingroup msg_task_usage
628 * \brief This function waits for the first communication finished in a list.
629 * \param comms a vector of communications
630 * \return the position of the first finished communication
631 * (but it may have failed, use MSG_comm_get_status() to know its status)
633 int MSG_comm_waitany(xbt_dynar_t comms)
635 int finished_index = -1;
637 /* create the equivalent dynar with SIMIX objects */
638 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), nullptr);
641 xbt_dynar_foreach(comms, cursor, comm) {
642 xbt_dynar_push(s_comms, &comm->s_comm);
645 msg_error_t status = MSG_OK;
647 finished_index = simcall_comm_waitany(s_comms, -1);
650 switch (e.category) {
652 finished_index = e.value;
653 status = MSG_TRANSFER_FAILURE;
656 finished_index = e.value;
657 status = MSG_TIMEOUT;
664 xbt_assert(finished_index != -1, "WaitAny returned -1");
665 xbt_dynar_free(&s_comms);
667 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
668 /* the communication is finished */
669 comm->status = status;
671 if (comm->task_received != nullptr) {
672 /* I am the receiver */
673 (*comm->task_received)->simdata->setNotUsed();
676 return finished_index;
680 * \ingroup msg_task_usage
681 * \brief Returns the error (if any) that occurred during a finished communication.
682 * \param comm a finished communication
683 * \return the status of the communication, or #MSG_OK if no error occurred
684 * during the communication
686 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
691 /** \ingroup msg_task_usage
692 * \brief Get a task (#msg_task_t) from a communication
694 * \param comm the communication where to get the task
695 * \return the task from the communication
697 msg_task_t MSG_comm_get_task(msg_comm_t comm)
699 xbt_assert(comm, "Invalid parameter");
701 return comm->task_received ? *comm->task_received : comm->task_sent;
705 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
706 * \param synchro the comm
707 * \param buff the data copied
708 * \param buff_size size of the buffer
710 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
712 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
714 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
716 // notify the user callback if any
717 if (msg_global->task_copy_callback) {
718 msg_task_t task = static_cast<msg_task_t>(buff);
719 msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
723 /** \ingroup msg_task_usage
724 * \brief Sends a task to a mailbox
726 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
727 * side if #MSG_task_receive is used).
728 * See #MSG_task_isend for sending tasks asynchronously.
730 * \param task the task to be sent
731 * \param alias the mailbox name to where the task is sent
733 * \return Returns #MSG_OK if the task was successfully sent,
734 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
736 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
738 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
739 return MSG_task_send_with_timeout(task, alias, -1);
742 /** \ingroup msg_task_usage
743 * \brief Sends a task to a mailbox with a maximum rate
745 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
746 * the application to limit the bandwidth utilization of network links when sending the task.
748 * \param task the task to be sent
749 * \param alias the mailbox name to where the task is sent
750 * \param maxrate the maximum communication rate for sending this task
752 * \return Returns #MSG_OK if the task was successfully sent,
753 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
755 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
757 task->simdata->rate = maxrate;
758 return MSG_task_send(task, alias);
761 /** \ingroup msg_task_usage
762 * \brief Sends a task to a mailbox with a timeout
764 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
766 * \param task the task to be sent
767 * \param alias the mailbox name to where the task is sent
768 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
770 * \return Returns #MSG_OK if the task was successfully sent,
771 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
773 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
775 msg_error_t ret = MSG_OK;
776 simdata_task_t t_simdata = nullptr;
777 msg_process_t process = MSG_process_self();
778 simdata_process_t p_simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
779 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
781 int call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
783 /* Prepare the task to send */
784 t_simdata = task->simdata;
785 t_simdata->sender = process;
786 t_simdata->source = (static_cast<simdata_process_t>(SIMIX_process_self_get_data())) ->m_host;
788 t_simdata->setUsed();
790 t_simdata->comm = nullptr;
791 msg_global->sent_msg++;
793 p_simdata->waiting_task = task;
795 /* Try to send it by calling SIMIX network layer */
797 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
798 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
799 t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
800 if (TRACE_is_enabled())
801 simcall_set_category(comm, task->category);
802 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(comm);
803 simcall_comm_wait(comm, timeout);
806 switch (e.category) {
808 ret = MSG_HOST_FAILURE;
811 ret = MSG_TRANSFER_FAILURE;
820 /* If the send failed, it is not used anymore */
821 t_simdata->setNotUsed();
824 p_simdata->waiting_task = nullptr;
826 TRACE_msg_task_put_end();
830 /** \ingroup msg_task_usage
831 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
833 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
835 * \param task the task to be sent
836 * \param alias the mailbox name to where the task is sent
837 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
838 * \param maxrate the maximum communication rate for sending this task
840 * \return Returns #MSG_OK if the task was successfully sent,
841 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
843 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
845 task->simdata->rate = maxrate;
846 return MSG_task_send_with_timeout(task, alias, timeout);
849 /** \ingroup msg_task_usage
850 * \brief Check if there is a communication going on in a mailbox.
852 * \param alias the name of the mailbox to be considered
854 * \return Returns 1 if there is a communication, 0 otherwise
856 int MSG_task_listen(const char *alias)
858 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
859 return !mbox->empty() ||
860 (mbox->getImpl()->permanent_receiver && !mbox->getImpl()->done_comm_queue.empty());
863 /** \ingroup msg_task_usage
864 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
866 * \param alias the name of the mailbox to be considered
868 * \return Returns the PID of sender process,
869 * -1 if there is no communication in the mailbox.
871 int MSG_task_listen_from(const char *alias)
873 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
874 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mbox->front());
879 return MSG_process_get_PID( static_cast<msg_task_t>(comm->src_data)->simdata->sender );
882 /** \ingroup msg_task_usage
883 * \brief Sets the tracing category of a task.
885 * This function should be called after the creation of a MSG task, to define the category of that task. The
886 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
887 * parameter category must contain a category that was previously declared with the function #TRACE_category
888 * (or with #TRACE_category_with_color).
890 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
892 * \param task the task that is going to be categorized
893 * \param category the name of the category to be associated to the task
895 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
897 void MSG_task_set_category (msg_task_t task, const char *category)
899 TRACE_msg_set_task_category (task, category);
902 /** \ingroup msg_task_usage
904 * \brief Gets the current tracing category of a task.
906 * \param task the task to be considered
908 * \see MSG_task_set_category
910 * \return Returns the name of the tracing category of the given task, nullptr otherwise
912 const char *MSG_task_get_category (msg_task_t task)
914 return task->category;