1 /* Copyright (c) 2004-2018. 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 "simgrid/s4u/Mailbox.hpp"
9 #include "src/instr/instr_private.hpp"
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/msg/msg_private.hpp"
12 #include "src/simix/smx_private.hpp" /* MSG_task_listen looks inside the rdv directly. Not clean. */
14 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 return MSG_parallel_task_execute(task);
28 /** @ingroup msg_task_usage
29 * @brief Executes a parallel task and waits for its termination.
31 * @param task a #msg_task_t to execute on the location on which the process is running.
33 * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
34 * or #MSG_HOST_FAILURE otherwise
36 msg_error_t MSG_parallel_task_execute(msg_task_t task)
38 return MSG_parallel_task_execute_with_timeout(task, -1);
41 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
43 simdata_task_t simdata = task->simdata;
44 e_smx_state_t comp_state;
45 msg_error_t status = MSG_OK;
47 TRACE_msg_task_execute_start(task);
49 xbt_assert((not simdata->compute) && not task->simdata->isused,
50 "This task is executed somewhere else. Go fix your code!");
52 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
54 if (simdata->flops_amount <= 0.0 && not simdata->host_nb) {
55 TRACE_msg_task_execute_end(task);
62 if (simdata->host_nb > 0) {
64 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
65 task->name ?: "", simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
66 simdata->bytes_parallel_amount, -1.0, timeout));
67 XBT_DEBUG("Parallel execution action created: %p", simdata->compute.get());
69 simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
70 simcall_execution_start(task->name ?: "", simdata->flops_amount, simdata->priority, simdata->bound,
71 MSG_process_get_host(MSG_process_self())));
73 if (task->category != nullptr)
74 simcall_set_category(simdata->compute, task->category);
75 comp_state = simcall_execution_wait(simdata->compute);
77 simdata->setNotUsed();
79 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
84 status = MSG_TASK_CANCELED;
87 status = MSG_HOST_FAILURE;
97 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
98 simdata->flops_amount = 0.0;
99 simdata->comm = nullptr;
100 simdata->compute = nullptr;
101 TRACE_msg_task_execute_end(task);
106 /** @ingroup msg_task_usage
107 * @brief Sleep for the specified number of seconds
109 * Makes the current process sleep until @a time seconds have elapsed.
111 * @param nb_sec a number of second
113 msg_error_t MSG_process_sleep(double nb_sec)
115 msg_error_t status = MSG_OK;
118 simgrid::s4u::this_actor::sleep_for(nb_sec);
121 if (e.category == cancel_error) {
122 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
123 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
124 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
125 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
126 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
127 // and did not change anythings at the C level.
128 // 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)
129 status = MSG_TASK_CANCELED;
137 /** @ingroup msg_task_usage
138 * @brief Receives a task from a mailbox.
140 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
141 * for receiving tasks asynchronously.
143 * @param task a memory location for storing a #msg_task_t.
144 * @param alias name of the mailbox to receive the task from
147 * #MSG_OK if the task was successfully received,
148 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
150 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
152 return MSG_task_receive_with_timeout(task, alias, -1);
155 /** @ingroup msg_task_usage
156 * @brief Receives a task from a mailbox at a given rate.
158 * @param task a memory location for storing a #msg_task_t.
159 * @param alias name of the mailbox to receive the task from
160 * @param rate limit the reception to rate bandwidth (byte/sec)
162 * The rate parameter can be used to receive a task with a limited
163 * bandwidth (smaller than the physical available value). Use
164 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
165 * rate value do disable this feature).
168 * #MSG_OK if the task was successfully received,
169 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
171 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
173 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
176 /** @ingroup msg_task_usage
177 * @brief Receives a task from a mailbox with a given timeout.
179 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
180 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
181 * to obtain an infinite timeout.
183 * @param task a memory location for storing a #msg_task_t.
184 * @param alias name of the mailbox to receive the task from
185 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
188 * #MSG_OK if the task was successfully received,
189 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
191 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
193 return MSG_task_receive_ext(task, alias, timeout, nullptr);
196 /** @ingroup msg_task_usage
197 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
199 * @param task a memory location for storing a #msg_task_t.
200 * @param alias name of the mailbox to receive the task from
201 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
202 * @param rate limit the reception to rate bandwidth (byte/sec)
204 * The rate parameter can be used to send a task with a limited
205 * bandwidth (smaller than the physical available value). Use
206 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
207 * rate value do disable this feature).
210 * #MSG_OK if the task was successfully received,
211 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
213 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
215 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
218 /** @ingroup msg_task_usage
219 * @brief Receives a task from a mailbox from a specific host with a given timeout.
221 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
222 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
223 * to obtain an infinite timeout.
225 * @param task a memory location for storing a #msg_task_t.
226 * @param alias name of the mailbox to receive the task from
227 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
228 * @param host a #msg_host_t host from where the task was sent
231 * #MSG_OK if the task was successfully received,
232 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
234 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
236 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
237 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
240 /** @ingroup msg_task_usage
241 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
243 * @param task a memory location for storing a #msg_task_t.
244 * @param alias name of the mailbox to receive the task from
245 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
246 * @param host a #msg_host_t host from where the task was sent
247 * @param rate limit the reception to rate bandwidth (byte/sec)
249 * The rate parameter can be used to receive a task with a limited
250 * bandwidth (smaller than the physical available value). Use
251 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
252 * rate value do disable this feature).
255 * #MSG_OK if the task was successfully received,
256 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
258 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
261 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
262 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
263 msg_error_t ret = MSG_OK;
264 /* We no longer support getting a task from a specific host */
268 TRACE_msg_task_get_start();
271 xbt_assert(task, "Null pointer for the task storage");
274 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
276 /* Try to receive it by calling SIMIX network layer */
278 simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
280 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
281 (*task)->simdata->setNotUsed();
284 switch (e.category) {
287 ret = MSG_HOST_FAILURE;
290 ret = MSG_TRANSFER_FAILURE;
300 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
301 TRACE_msg_task_get_end(*task);
306 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
307 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
308 void_f_pvoid_t cleanup, int detached)
310 simdata_task_t t_simdata = nullptr;
311 msg_process_t myself = MSG_process_self();
312 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
313 TRACE_msg_task_put_start(task);
315 /* Prepare the task to send */
316 t_simdata = task->simdata;
317 t_simdata->sender = myself;
318 t_simdata->source = MSG_host_self();
319 t_simdata->setUsed();
320 t_simdata->comm = nullptr;
321 msg_global->sent_msg++;
323 /* Send it by calling SIMIX network layer */
325 simcall_comm_isend(myself->get_impl(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
326 sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
327 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
329 msg_comm_t comm = nullptr;
331 comm = new simgrid::msg::Comm(task, nullptr, act);
334 if (TRACE_is_enabled())
335 simcall_set_category(act, task->category);
336 TRACE_msg_task_put_end();
341 /** @ingroup msg_task_usage
342 * @brief Sends a task on a mailbox.
344 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
346 * @param task a #msg_task_t to send on another location.
347 * @param alias name of the mailbox to sent the task to
348 * @return the msg_comm_t communication created
350 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
352 return MSG_task_isend_internal(task, alias, nullptr, 0);
355 /** @ingroup msg_task_usage
356 * @brief Sends a task on a mailbox with a maximum rate
358 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
359 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
361 * @param task a #msg_task_t to send on another location.
362 * @param alias name of the mailbox to sent the task to
363 * @param maxrate the maximum communication rate for sending this task (byte/sec).
364 * @return the msg_comm_t communication created
366 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
368 task->simdata->rate = maxrate;
369 return MSG_task_isend_internal(task, alias, nullptr, 0);
372 /** @ingroup msg_task_usage
373 * @brief Sends a task on a mailbox.
375 * This is a non blocking detached send function.
376 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
377 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
378 * usual. More details on this can be obtained on
379 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
380 * in the SimGrid-user mailing list archive.
382 * @param task a #msg_task_t to send on another location.
383 * @param alias name of the mailbox to sent the task to
384 * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
385 * (if nullptr, no function will be called)
387 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
389 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
390 xbt_assert(comm == nullptr);
393 /** @ingroup msg_task_usage
394 * @brief Sends a task on a mailbox with a maximal rate.
396 * This is a non blocking detached send function.
397 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
398 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
399 * usual. More details on this can be obtained on
400 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
401 * in the SimGrid-user mailing list archive.
403 * The rate parameter can be used to send a task with a limited
404 * bandwidth (smaller than the physical available value). Use
405 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
406 * value do disable this feature).
408 * @param task a #msg_task_t to send on another location.
409 * @param alias name of the mailbox to sent the task to
410 * @param cleanup a function to destroy the task if the
411 * communication fails, e.g. MSG_task_destroy
412 * (if nullptr, no function will be called)
413 * @param maxrate the maximum communication rate for sending this task (byte/sec)
416 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
418 task->simdata->rate = maxrate;
419 MSG_task_dsend(task, alias, cleanup);
422 /** @ingroup msg_task_usage
423 * @brief Starts listening for receiving a task from an asynchronous communication.
425 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
427 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
428 * @param name of the mailbox to receive the task on
429 * @return the msg_comm_t communication created
431 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
433 return MSG_task_irecv_bounded(task, name, -1.0);
436 /** @ingroup msg_task_usage
437 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
439 * The rate parameter can be used to receive a task with a limited
440 * bandwidth (smaller than the physical available value). Use
441 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
442 * value do disable this feature).
444 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
445 * @param name of the mailbox to receive the task on
446 * @param rate limit the bandwidth to the given rate (byte/sec)
447 * @return the msg_comm_t communication created
449 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
451 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
453 /* FIXME: these functions are not traceable */
455 xbt_assert(task, "Null pointer for the task storage");
458 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
460 /* Try to receive it by calling SIMIX network layer */
461 msg_comm_t comm = new simgrid::msg::Comm(
463 simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr, rate));
468 /** @ingroup msg_task_usage
469 * @brief Checks whether a communication is done, and if yes, finalizes it.
470 * @param comm the communication to test
471 * @return 'true' if the communication is finished
472 * (but it may have failed, use MSG_comm_get_status() to know its status)
473 * or 'false' if the communication is not finished yet
474 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
476 int MSG_comm_test(msg_comm_t comm)
478 bool finished = false;
481 finished = simcall_comm_test(comm->s_comm);
482 if (finished && comm->task_received != nullptr) {
483 /* I am the receiver */
484 (*comm->task_received)->simdata->setNotUsed();
488 switch (e.category) {
490 comm->status = MSG_TRANSFER_FAILURE;
494 comm->status = MSG_TIMEOUT;
505 /** @ingroup msg_task_usage
506 * @brief This function checks if a communication is finished.
507 * @param comms a vector of communications
508 * @return the position of the finished communication if any
509 * (but it may have failed, use MSG_comm_get_status() to know its status),
510 * or -1 if none is finished
512 int MSG_comm_testany(xbt_dynar_t comms)
514 int finished_index = -1;
516 /* Create the equivalent array with SIMIX objects: */
517 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
518 s_comms.reserve(xbt_dynar_length(comms));
521 xbt_dynar_foreach(comms, cursor, comm) {
522 s_comms.push_back(comm->s_comm);
525 msg_error_t status = MSG_OK;
527 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
530 switch (e.category) {
532 finished_index = e.value;
533 status = MSG_TRANSFER_FAILURE;
536 finished_index = e.value;
537 status = MSG_TIMEOUT;
544 if (finished_index != -1) {
545 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
546 /* the communication is finished */
547 comm->status = status;
549 if (status == MSG_OK && comm->task_received != nullptr) {
550 /* I am the receiver */
551 (*comm->task_received)->simdata->setNotUsed();
555 return finished_index;
558 /** @ingroup msg_task_usage
559 * @brief Destroys a communication.
560 * @param comm the communication to destroy.
562 void MSG_comm_destroy(msg_comm_t comm)
567 /** @ingroup msg_task_usage
568 * @brief Wait for the completion of a communication.
570 * It takes two parameters.
571 * @param comm the communication to wait.
572 * @param timeout Wait until the communication terminates or the timeout occurs.
573 * You can provide a -1 timeout to obtain an infinite timeout.
574 * @return msg_error_t
576 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
579 simcall_comm_wait(comm->s_comm, timeout);
581 if (comm->task_received != nullptr) {
582 /* I am the receiver */
583 (*comm->task_received)->simdata->setNotUsed();
586 /* FIXME: these functions are not traceable */
589 switch (e.category) {
591 comm->status = MSG_TRANSFER_FAILURE;
594 comm->status = MSG_TIMEOUT;
604 /** @ingroup msg_task_usage
605 * @brief This function is called by a sender and permit to wait for each communication
607 * @param comm a vector of communication
608 * @param nb_elem is the size of the comm vector
609 * @param timeout for each call of MSG_comm_wait
611 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
613 for (int i = 0; i < nb_elem; i++)
614 MSG_comm_wait(comm[i], timeout);
617 /** @ingroup msg_task_usage
618 * @brief This function waits for the first communication finished in a list.
619 * @param comms a vector of communications
620 * @return the position of the first finished communication
621 * (but it may have failed, use MSG_comm_get_status() to know its status)
623 int MSG_comm_waitany(xbt_dynar_t comms)
625 int finished_index = -1;
627 /* create the equivalent dynar with SIMIX objects */
628 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
629 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
633 xbt_dynar_foreach(comms, cursor, comm) {
634 intrusive_ptr_add_ref(comm->s_comm.get());
635 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
638 msg_error_t status = MSG_OK;
640 finished_index = simcall_comm_waitany(s_comms, -1);
643 switch (e.category) {
645 finished_index = e.value;
646 status = MSG_TRANSFER_FAILURE;
649 finished_index = e.value;
650 status = MSG_TIMEOUT;
657 xbt_assert(finished_index != -1, "WaitAny returned -1");
658 xbt_dynar_free(&s_comms);
660 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
661 /* the communication is finished */
662 comm->status = status;
664 if (comm->task_received != nullptr) {
665 /* I am the receiver */
666 (*comm->task_received)->simdata->setNotUsed();
669 return finished_index;
673 * @ingroup msg_task_usage
674 * @brief Returns the error (if any) that occurred during a finished communication.
675 * @param comm a finished communication
676 * @return the status of the communication, or #MSG_OK if no error occurred
677 * during the communication
679 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
684 /** @ingroup msg_task_usage
685 * @brief Get a task (#msg_task_t) from a communication
687 * @param comm the communication where to get the task
688 * @return the task from the communication
690 msg_task_t MSG_comm_get_task(msg_comm_t comm)
692 xbt_assert(comm, "Invalid parameter");
694 return comm->task_received ? *comm->task_received : comm->task_sent;
698 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
699 * @param synchro the comm
700 * @param buff the data copied
701 * @param buff_size size of the buffer
703 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
705 simgrid::kernel::activity::CommImplPtr comm =
706 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
708 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
710 // notify the user callback if any
711 if (msg_global->task_copy_callback) {
712 msg_task_t task = static_cast<msg_task_t>(buff);
713 msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
717 /** @ingroup msg_task_usage
718 * @brief Sends a task to a mailbox
720 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
721 * side if #MSG_task_receive is used).
722 * See #MSG_task_isend for sending tasks asynchronously.
724 * @param task the task to be sent
725 * @param alias the mailbox name to where the task is sent
727 * @return Returns #MSG_OK if the task was successfully sent,
728 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
730 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
732 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
733 return MSG_task_send_with_timeout(task, alias, -1);
736 /** @ingroup msg_task_usage
737 * @brief Sends a task to a mailbox with a maximum rate
739 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
740 * the application to limit the bandwidth utilization of network links when sending the task.
742 * The maxrate parameter can be used to send a task with a limited
743 * bandwidth (smaller than the physical available value). Use
744 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
745 * value do disable this feature).
747 * @param task the task to be sent
748 * @param alias the mailbox name to where the task is sent
749 * @param maxrate the maximum communication rate for sending this task (byte/sec)
751 * @return Returns #MSG_OK if the task was successfully sent,
752 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
754 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
756 task->simdata->rate = maxrate;
757 return MSG_task_send(task, alias);
760 /** @ingroup msg_task_usage
761 * @brief Sends a task to a mailbox with a timeout
763 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
765 * @param task the task to be sent
766 * @param alias the mailbox name to where the task is sent
767 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
769 * @return Returns #MSG_OK if the task was successfully sent,
770 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
772 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
774 msg_error_t ret = MSG_OK;
775 simdata_task_t t_simdata = nullptr;
776 msg_process_t process = MSG_process_self();
777 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
779 TRACE_msg_task_put_start(task);
781 /* Prepare the task to send */
782 t_simdata = task->simdata;
783 t_simdata->sender = process;
784 t_simdata->source = MSG_host_self();
786 t_simdata->setUsed();
788 t_simdata->comm = nullptr;
789 msg_global->sent_msg++;
791 /* Try to send it by calling SIMIX network layer */
793 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
794 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
795 sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
796 if (TRACE_is_enabled() && task->category != nullptr)
797 simcall_set_category(comm, task->category);
798 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
799 simcall_comm_wait(comm, timeout);
802 switch (e.category) {
804 ret = MSG_HOST_FAILURE;
807 ret = MSG_TRANSFER_FAILURE;
816 /* If the send failed, it is not used anymore */
817 t_simdata->setNotUsed();
820 TRACE_msg_task_put_end();
824 /** @ingroup msg_task_usage
825 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
827 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
829 * The maxrate parameter can be used to send a task with a limited
830 * bandwidth (smaller than the physical available value). Use
831 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
832 * value do disable this feature).
834 * @param task the task to be sent
835 * @param alias the mailbox name to where the task is sent
836 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
837 * @param maxrate the maximum communication rate for sending this task (byte/sec)
839 * @return Returns #MSG_OK if the task was successfully sent,
840 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
842 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
844 task->simdata->rate = maxrate;
845 return MSG_task_send_with_timeout(task, alias, timeout);
848 /** @ingroup msg_task_usage
849 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
851 * @param alias the name of the mailbox to be considered
853 * @return Returns the PID of sender process,
854 * -1 if there is no communication in the mailbox.
856 int MSG_task_listen_from(const char *alias)
858 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
859 simgrid::kernel::activity::CommImplPtr comm =
860 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
865 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
868 /** @ingroup msg_task_usage
869 * @brief Sets the tracing category of a task.
871 * This function should be called after the creation of a MSG task, to define the category of that task. The
872 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
873 * parameter category must contain a category that was previously declared with the function #TRACE_category
874 * (or with #TRACE_category_with_color).
876 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
878 * @param task the task that is going to be categorized
879 * @param category the name of the category to be associated to the task
881 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
883 void MSG_task_set_category (msg_task_t task, const char *category)
885 TRACE_msg_set_task_category (task, category);
888 /** @ingroup msg_task_usage
890 * @brief Gets the current tracing category of a task.
892 * @param task the task to be considered
894 * @see MSG_task_set_category
896 * @return Returns the name of the tracing category of the given task, nullptr otherwise
898 const char *MSG_task_get_category (msg_task_t task)
900 return task->category;