1 /* Copyright (c) 2004-2017. 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/kernel/activity/ExecImpl.hpp"
9 #include "src/msg/msg_private.h"
10 #include "src/simix/smx_private.h" /* MSG_task_listen looks inside the rdv directly. Not clean. */
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "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 takes only one parameter.
20 * \param task a #msg_task_t to execute on the location on which the process is running.
21 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
23 msg_error_t MSG_task_execute(msg_task_t task)
25 /* TODO: add this to other locations */
26 msg_host_t host = MSG_process_get_host(MSG_process_self());
27 MSG_host_add_task(host, task);
29 msg_error_t ret = MSG_parallel_task_execute(task);
31 MSG_host_del_task(host, task);
35 /** \ingroup msg_task_usage
36 * \brief Executes a parallel task and waits for its termination.
38 * \param task a #msg_task_t to execute on the location on which the process is running.
40 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
41 * or #MSG_HOST_FAILURE otherwise
43 msg_error_t MSG_parallel_task_execute(msg_task_t task)
45 return MSG_parallel_task_execute_with_timeout(task, -1);
48 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
50 simdata_task_t simdata = task->simdata;
51 e_smx_state_t comp_state;
52 msg_error_t status = MSG_OK;
54 TRACE_msg_task_execute_start(task);
56 xbt_assert((not simdata->compute) && not task->simdata->isused,
57 "This task is executed somewhere else. Go fix your code!");
59 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
61 if (simdata->flops_amount <= 0.0 && not simdata->host_nb) {
62 TRACE_msg_task_execute_end(task);
69 if (simdata->host_nb > 0) {
71 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
72 task->name, simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
73 simdata->bytes_parallel_amount, 1.0, -1.0, timeout));
74 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
76 simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
77 simcall_execution_start(task->name, simdata->flops_amount, simdata->priority, simdata->bound));
79 simcall_set_category(simdata->compute, task->category);
80 comp_state = simcall_execution_wait(simdata->compute);
82 simdata->setNotUsed();
84 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
89 status = MSG_TASK_CANCELED;
92 status = MSG_HOST_FAILURE;
102 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
103 simdata->flops_amount = 0.0;
104 simdata->comm = nullptr;
105 simdata->compute = nullptr;
106 TRACE_msg_task_execute_end(task);
111 /** \ingroup msg_task_usage
112 * \brief Sleep for the specified number of seconds
114 * Makes the current process sleep until \a time seconds have elapsed.
116 * \param nb_sec a number of second
118 msg_error_t MSG_process_sleep(double nb_sec)
120 msg_error_t status = MSG_OK;
122 TRACE_msg_process_sleep_in(MSG_process_self());
125 simcall_process_sleep(nb_sec);
128 if (e.category == cancel_error) {
129 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
130 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
131 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
132 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
133 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
134 // and did not change anythings at the C level.
135 // 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)
136 status = MSG_TASK_CANCELED;
141 TRACE_msg_process_sleep_out(MSG_process_self());
145 /** \ingroup msg_task_usage
146 * \brief Receives a task from a mailbox.
148 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
149 * for receiving tasks asynchronously.
151 * \param task a memory location for storing a #msg_task_t.
152 * \param alias name of the mailbox to receive the task from
155 * #MSG_OK if the task was successfully received,
156 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
158 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
160 return MSG_task_receive_with_timeout(task, alias, -1);
163 /** \ingroup msg_task_usage
164 * \brief Receives a task from a mailbox at a given rate.
166 * \param task a memory location for storing a #msg_task_t.
167 * \param alias name of the mailbox to receive the task from
168 * \param rate limit the reception to rate bandwidth
171 * #MSG_OK if the task was successfully received,
172 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
174 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
176 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
179 /** \ingroup msg_task_usage
180 * \brief Receives a task from a mailbox with a given timeout.
182 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
183 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
184 * to obtain an infinite timeout.
186 * \param task a memory location for storing a #msg_task_t.
187 * \param alias name of the mailbox to receive the task from
188 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
191 * #MSG_OK if the task was successfully received,
192 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
194 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
196 return MSG_task_receive_ext(task, alias, timeout, nullptr);
199 /** \ingroup msg_task_usage
200 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
202 * \param task a memory location for storing a #msg_task_t.
203 * \param alias name of the mailbox to receive the task from
204 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
205 * \param rate limit the reception to rate bandwidth
208 * #MSG_OK if the task was successfully received,
209 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
211 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
213 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
216 /** \ingroup msg_task_usage
217 * \brief Receives a task from a mailbox from a specific host with a given timeout.
219 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
220 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
221 * to obtain an infinite timeout.
223 * \param task a memory location for storing a #msg_task_t.
224 * \param alias name of the mailbox to receive the task from
225 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
226 * \param host a #msg_host_t host from where the task was sent
229 * #MSG_OK if the task was successfully received,
230 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
232 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
234 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
235 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
238 /** \ingroup msg_task_usage
239 * \brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
241 * \param task a memory location for storing a #msg_task_t.
242 * \param alias name of the mailbox to receive the task from
243 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
244 * \param host a #msg_host_t host from where the task was sent
245 * \param rate limit the reception to rate bandwidth
248 * #MSG_OK if the task was successfully received,
249 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
251 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
254 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
255 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
256 msg_error_t ret = MSG_OK;
257 /* We no longer support getting a task from a specific host */
261 TRACE_msg_task_get_start();
262 double start_time = MSG_get_clock();
265 xbt_assert(task, "Null pointer for the task storage");
268 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
270 /* Try to receive it by calling SIMIX network layer */
272 simcall_comm_recv(MSG_process_self()->getImpl(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
273 XBT_DEBUG("Got task %s from %s",(*task)->name,mailbox->name());
274 (*task)->simdata->setNotUsed();
277 switch (e.category) {
280 ret = MSG_HOST_FAILURE;
283 ret = MSG_TRANSFER_FAILURE;
293 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
294 TRACE_msg_task_get_end(start_time, *task);
299 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
300 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
301 int (*match_fun)(void*,void*, smx_activity_t),
302 void *match_data, void_f_pvoid_t cleanup, int detached)
304 simdata_task_t t_simdata = nullptr;
305 msg_process_t myself = MSG_process_self();
306 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
307 int call_end = TRACE_msg_task_put_start(task);
309 /* Prepare the task to send */
310 t_simdata = task->simdata;
311 t_simdata->sender = myself;
312 t_simdata->source = MSG_host_self();
313 t_simdata->setUsed();
314 t_simdata->comm = nullptr;
315 msg_global->sent_msg++;
317 /* Send it by calling SIMIX network layer */
318 smx_activity_t act = simcall_comm_isend(myself->getImpl(), mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate,
319 task, sizeof(void *), match_fun, cleanup, nullptr, match_data,detached);
320 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
322 msg_comm_t comm = nullptr;
324 comm = xbt_new0(s_msg_comm_t, 1);
325 comm->task_sent = task;
326 comm->task_received = nullptr;
327 comm->status = MSG_OK;
331 if (TRACE_is_enabled())
332 simcall_set_category(act, task->category);
334 TRACE_msg_task_put_end();
339 /** \ingroup msg_task_usage
340 * \brief Sends a task on a mailbox.
342 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
344 * \param task a #msg_task_t to send on another location.
345 * \param alias name of the mailbox to sent the task to
346 * \return the msg_comm_t communication created
348 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
350 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
353 /** \ingroup msg_task_usage
354 * \brief Sends a task on a mailbox with a maximum rate
356 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
357 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
359 * \param task a #msg_task_t to send on another location.
360 * \param alias name of the mailbox to sent the task to
361 * \param maxrate the maximum communication rate for sending this task .
362 * \return the msg_comm_t communication created
364 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
366 task->simdata->rate = maxrate;
367 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
370 /** \ingroup msg_task_usage
371 * \brief Sends a task on a mailbox, with support for matching requests
373 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
375 * \param task a #msg_task_t to send on another location.
376 * \param alias name of the mailbox to sent the task to
377 * \param match_fun boolean function which parameters are:
378 * - match_data_provided_here
379 * - match_data_provided_by_other_side_if_any
380 * - the_smx_synchro_describing_the_other_side
381 * \param match_data user provided data passed to match_fun
382 * \return the msg_comm_t communication created
384 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
385 int (*match_fun)(void*, void*, smx_activity_t), void *match_data)
387 return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
390 /** \ingroup msg_task_usage
391 * \brief Sends a task on a mailbox.
393 * This is a non blocking detached send function.
394 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
395 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
396 * usual. More details on this can be obtained on
397 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
398 * in the SimGrid-user mailing list archive.
400 * \param task a #msg_task_t to send on another location.
401 * \param alias name of the mailbox to sent the task to
402 * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
403 * (if nullptr, no function will be called)
405 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
407 MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
410 /** \ingroup msg_task_usage
411 * \brief Sends a task on a mailbox with a maximal rate.
413 * This is a non blocking detached send function.
414 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
415 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
416 * usual. More details on this can be obtained on
417 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
418 * in the SimGrid-user mailing list archive.
420 * \param task a #msg_task_t to send on another location.
421 * \param alias name of the mailbox to sent the task to
422 * \param cleanup a function to destroy the task if the
423 * communication fails, e.g. MSG_task_destroy
424 * (if nullptr, no function will be called)
425 * \param maxrate the maximum communication rate for sending this task
428 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
430 task->simdata->rate = maxrate;
431 MSG_task_dsend(task, alias, cleanup);
434 /** \ingroup msg_task_usage
435 * \brief Starts listening for receiving a task from an asynchronous communication.
437 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
439 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
440 * \param name of the mailbox to receive the task on
441 * \return the msg_comm_t communication created
443 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
445 return MSG_task_irecv_bounded(task, name, -1.0);
448 /** \ingroup msg_task_usage
449 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
451 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
452 * \param name of the mailbox to receive the task on
453 * \param rate limit the bandwidth to the given rate
454 * \return the msg_comm_t communication created
456 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
458 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
460 /* FIXME: these functions are not traceable */
462 xbt_assert(task, "Null pointer for the task storage");
465 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
467 /* Try to receive it by calling SIMIX network layer */
468 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
469 comm->task_sent = nullptr;
470 comm->task_received = task;
471 comm->status = MSG_OK;
472 comm->s_comm = simcall_comm_irecv(SIMIX_process_self(), mbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, rate);
477 /** \ingroup msg_task_usage
478 * \brief Checks whether a communication is done, and if yes, finalizes it.
479 * \param comm the communication to test
480 * \return TRUE if the communication is finished
481 * (but it may have failed, use MSG_comm_get_status() to know its status)
482 * or FALSE if the communication is not finished yet
483 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
485 int MSG_comm_test(msg_comm_t comm)
490 finished = simcall_comm_test(comm->s_comm);
491 if (finished && comm->task_received != nullptr) {
492 /* I am the receiver */
493 (*comm->task_received)->simdata->setNotUsed();
494 comm->s_comm->unref();
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::ActivityImplPtr> 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();
562 comm->s_comm->unref();
566 return finished_index;
569 /** \ingroup msg_task_usage
570 * \brief Destroys a communication.
571 * \param comm the communication to destroy.
573 void MSG_comm_destroy(msg_comm_t comm)
578 /** \ingroup msg_task_usage
579 * \brief Wait for the completion of a communication.
581 * It takes two parameters.
582 * \param comm the communication to wait.
583 * \param timeout Wait until the communication terminates or the timeout occurs.
584 * You can provide a -1 timeout to obtain an infinite timeout.
585 * \return msg_error_t
587 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
590 simcall_comm_wait(comm->s_comm, timeout);
591 comm->s_comm->unref();
593 if (comm->task_received != nullptr) {
594 /* I am the receiver */
595 (*comm->task_received)->simdata->setNotUsed();
598 /* FIXME: these functions are not traceable */
601 switch (e.category) {
603 comm->status = MSG_TRANSFER_FAILURE;
606 comm->status = MSG_TIMEOUT;
616 /** \ingroup msg_task_usage
617 * \brief This function is called by a sender and permit to wait for each communication
619 * \param comm a vector of communication
620 * \param nb_elem is the size of the comm vector
621 * \param timeout for each call of MSG_comm_wait
623 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
625 for (int i = 0; i < nb_elem; i++)
626 MSG_comm_wait(comm[i], timeout);
629 /** \ingroup msg_task_usage
630 * \brief This function waits for the first communication finished in a list.
631 * \param comms a vector of communications
632 * \return the position of the first finished communication
633 * (but it may have failed, use MSG_comm_get_status() to know its status)
635 int MSG_comm_waitany(xbt_dynar_t comms)
637 int finished_index = -1;
639 /* create the equivalent dynar with SIMIX objects */
640 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), nullptr);
643 xbt_dynar_foreach(comms, cursor, comm) {
644 xbt_dynar_push(s_comms, &comm->s_comm);
647 msg_error_t status = MSG_OK;
649 finished_index = simcall_comm_waitany(s_comms, -1);
652 switch (e.category) {
654 finished_index = e.value;
655 status = MSG_TRANSFER_FAILURE;
658 finished_index = e.value;
659 status = MSG_TIMEOUT;
666 xbt_assert(finished_index != -1, "WaitAny returned -1");
667 xbt_dynar_free(&s_comms);
669 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
670 /* the communication is finished */
671 comm->status = status;
673 if (comm->task_received != nullptr) {
674 /* I am the receiver */
675 (*comm->task_received)->simdata->setNotUsed();
676 comm->s_comm->unref();
679 return finished_index;
683 * \ingroup msg_task_usage
684 * \brief Returns the error (if any) that occurred during a finished communication.
685 * \param comm a finished communication
686 * \return the status of the communication, or #MSG_OK if no error occurred
687 * during the communication
689 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
694 /** \ingroup msg_task_usage
695 * \brief Get a task (#msg_task_t) from a communication
697 * \param comm the communication where to get the task
698 * \return the task from the communication
700 msg_task_t MSG_comm_get_task(msg_comm_t comm)
702 xbt_assert(comm, "Invalid parameter");
704 return comm->task_received ? *comm->task_received : comm->task_sent;
708 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
709 * \param synchro the comm
710 * \param buff the data copied
711 * \param buff_size size of the buffer
713 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
715 simgrid::kernel::activity::CommImplPtr comm =
716 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
718 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
720 // notify the user callback if any
721 if (msg_global->task_copy_callback) {
722 msg_task_t task = static_cast<msg_task_t>(buff);
723 msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
727 /** \ingroup msg_task_usage
728 * \brief Sends a task to a mailbox
730 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
731 * side if #MSG_task_receive is used).
732 * See #MSG_task_isend for sending tasks asynchronously.
734 * \param task the task to be sent
735 * \param alias the mailbox name to where the task is sent
737 * \return Returns #MSG_OK if the task was successfully sent,
738 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
740 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
742 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
743 return MSG_task_send_with_timeout(task, alias, -1);
746 /** \ingroup msg_task_usage
747 * \brief Sends a task to a mailbox with a maximum rate
749 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
750 * the application to limit the bandwidth utilization of network links when sending the task.
752 * \param task the task to be sent
753 * \param alias the mailbox name to where the task is sent
754 * \param maxrate the maximum communication rate for sending this task
756 * \return Returns #MSG_OK if the task was successfully sent,
757 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
759 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
761 task->simdata->rate = maxrate;
762 return MSG_task_send(task, alias);
765 /** \ingroup msg_task_usage
766 * \brief Sends a task to a mailbox with a timeout
768 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
770 * \param task the task to be sent
771 * \param alias the mailbox name to where the task is sent
772 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
774 * \return Returns #MSG_OK if the task was successfully sent,
775 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
777 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
779 msg_error_t ret = MSG_OK;
780 simdata_task_t t_simdata = nullptr;
781 msg_process_t process = MSG_process_self();
782 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
784 int call_end = TRACE_msg_task_put_start(task);
786 /* Prepare the task to send */
787 t_simdata = task->simdata;
788 t_simdata->sender = process;
789 t_simdata->source = MSG_host_self();
791 t_simdata->setUsed();
793 t_simdata->comm = nullptr;
794 msg_global->sent_msg++;
796 /* Try to send it by calling SIMIX network layer */
798 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
799 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
800 t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
801 if (TRACE_is_enabled())
802 simcall_set_category(comm, task->category);
803 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
804 simcall_comm_wait(comm, timeout);
808 switch (e.category) {
810 ret = MSG_HOST_FAILURE;
813 ret = MSG_TRANSFER_FAILURE;
822 /* If the send failed, it is not used anymore */
823 t_simdata->setNotUsed();
827 TRACE_msg_task_put_end();
831 /** \ingroup msg_task_usage
832 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
834 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
836 * \param task the task to be sent
837 * \param alias the mailbox name to where the task is sent
838 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
839 * \param maxrate the maximum communication rate for sending this task
841 * \return Returns #MSG_OK if the task was successfully sent,
842 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
844 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
846 task->simdata->rate = maxrate;
847 return MSG_task_send_with_timeout(task, alias, timeout);
850 /** \ingroup msg_task_usage
851 * \brief Check if there is a communication going on in a mailbox.
853 * \param alias the name of the mailbox to be considered
855 * \return Returns 1 if there is a communication, 0 otherwise
857 int MSG_task_listen(const char *alias)
859 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
860 return mbox->listen() ? 1 : 0;
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::CommImplPtr comm =
875 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
880 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
883 /** \ingroup msg_task_usage
884 * \brief Sets the tracing category of a task.
886 * This function should be called after the creation of a MSG task, to define the category of that task. The
887 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
888 * parameter category must contain a category that was previously declared with the function #TRACE_category
889 * (or with #TRACE_category_with_color).
891 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
893 * \param task the task that is going to be categorized
894 * \param category the name of the category to be associated to the task
896 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
898 void MSG_task_set_category (msg_task_t task, const char *category)
900 TRACE_msg_set_task_category (task, category);
903 /** \ingroup msg_task_usage
905 * \brief Gets the current tracing category of a task.
907 * \param task the task to be considered
909 * \see MSG_task_set_category
911 * \return Returns the name of the tracing category of the given task, nullptr otherwise
913 const char *MSG_task_get_category (msg_task_t task)
915 return task->category;