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. */
6 #include "simgrid/Exception.hpp"
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());
68 if (task->category != nullptr)
69 simcall_set_category(simdata->compute, task->category);
71 simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
72 simcall_execution_start(task->name ?: "", task->category ?: "", simdata->flops_amount, simdata->priority,
73 simdata->bound, MSG_process_get_host(MSG_process_self())));
76 comp_state = simcall_execution_wait(simdata->compute);
78 simdata->setNotUsed();
80 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
81 } catch (simgrid::HostFailureException& e) {
82 status = MSG_HOST_FAILURE;
86 status = MSG_TASK_CANCELED;
96 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
97 simdata->flops_amount = 0.0;
98 simdata->comm = nullptr;
99 simdata->compute = nullptr;
100 TRACE_msg_task_execute_end(task);
105 /** @ingroup msg_task_usage
106 * @brief Sleep for the specified number of seconds
108 * Makes the current process sleep until @a time seconds have elapsed.
110 * @param nb_sec a number of second
112 msg_error_t MSG_process_sleep(double nb_sec)
114 msg_error_t status = MSG_OK;
117 simgrid::s4u::this_actor::sleep_for(nb_sec);
120 if (e.category == cancel_error) {
121 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
122 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
123 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
124 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
125 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
126 // and did not change anythings at the C level.
127 // 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)
128 status = MSG_TASK_CANCELED;
136 /** @ingroup msg_task_usage
137 * @brief Receives a task from a mailbox.
139 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
140 * for receiving tasks asynchronously.
142 * @param task a memory location for storing a #msg_task_t.
143 * @param alias name of the mailbox to receive the task from
146 * #MSG_OK if the task was successfully received,
147 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
149 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
151 return MSG_task_receive_with_timeout(task, alias, -1);
154 /** @ingroup msg_task_usage
155 * @brief Receives a task from a mailbox at a given rate.
157 * @param task a memory location for storing a #msg_task_t.
158 * @param alias name of the mailbox to receive the task from
159 * @param rate limit the reception to rate bandwidth (byte/sec)
161 * The rate parameter can be used to receive a task with a limited
162 * bandwidth (smaller than the physical available value). Use
163 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
164 * rate value do disable this feature).
167 * #MSG_OK if the task was successfully received,
168 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
170 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
172 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
175 /** @ingroup msg_task_usage
176 * @brief Receives a task from a mailbox with a given timeout.
178 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
179 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
180 * to obtain an infinite timeout.
182 * @param task a memory location for storing a #msg_task_t.
183 * @param alias name of the mailbox to receive the task from
184 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
187 * #MSG_OK if the task was successfully received,
188 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
190 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
192 return MSG_task_receive_ext(task, alias, timeout, nullptr);
195 /** @ingroup msg_task_usage
196 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
198 * @param task a memory location for storing a #msg_task_t.
199 * @param alias name of the mailbox to receive the task from
200 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
201 * @param rate limit the reception to rate bandwidth (byte/sec)
203 * The rate parameter can be used to send a task with a limited
204 * bandwidth (smaller than the physical available value). Use
205 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
206 * rate value do disable this feature).
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 (byte/sec)
248 * The rate parameter can be used to receive a task with a limited
249 * bandwidth (smaller than the physical available value). Use
250 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
251 * rate value do disable this feature).
254 * #MSG_OK if the task was successfully received,
255 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
257 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
260 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
261 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
262 msg_error_t ret = MSG_OK;
263 /* We no longer support getting a task from a specific host */
267 TRACE_msg_task_get_start();
270 xbt_assert(task, "Null pointer for the task storage");
273 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
275 /* Try to receive it by calling SIMIX network layer */
277 simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
279 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
280 (*task)->simdata->setNotUsed();
281 } catch (simgrid::HostFailureException& e) {
282 ret = MSG_HOST_FAILURE;
283 } catch (xbt_ex& e) {
284 switch (e.category) {
286 ret = MSG_HOST_FAILURE;
289 ret = MSG_TRANSFER_FAILURE;
299 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
300 TRACE_msg_task_get_end(*task);
305 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
306 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
307 void_f_pvoid_t cleanup, int detached)
309 simdata_task_t t_simdata = nullptr;
310 msg_process_t myself = MSG_process_self();
311 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
312 TRACE_msg_task_put_start(task);
314 /* Prepare the task to send */
315 t_simdata = task->simdata;
316 t_simdata->sender = myself;
317 t_simdata->source = MSG_host_self();
318 t_simdata->setUsed();
319 t_simdata->comm = nullptr;
320 msg_global->sent_msg++;
322 /* Send it by calling SIMIX network layer */
324 simcall_comm_isend(myself->get_impl(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
325 sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
326 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
328 msg_comm_t comm = nullptr;
330 comm = new simgrid::msg::Comm(task, nullptr, act);
333 if (TRACE_is_enabled())
334 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, 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 (byte/sec).
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, 0);
371 /** @ingroup msg_task_usage
372 * @brief Sends a task on a mailbox.
374 * This is a non blocking detached send function.
375 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
376 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
377 * usual. More details on this can be obtained on
378 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
379 * in the SimGrid-user mailing list archive.
381 * @param task a #msg_task_t to send on another location.
382 * @param alias name of the mailbox to sent the task to
383 * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
384 * (if nullptr, no function will be called)
386 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
388 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
389 xbt_assert(comm == nullptr);
392 /** @ingroup msg_task_usage
393 * @brief Sends a task on a mailbox with a maximal rate.
395 * This is a non blocking detached send function.
396 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
397 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
398 * usual. More details on this can be obtained on
399 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
400 * in the SimGrid-user mailing list archive.
402 * The rate parameter can be used to send a task with a limited
403 * bandwidth (smaller than the physical available value). Use
404 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
405 * value do disable this feature).
407 * @param task a #msg_task_t to send on another location.
408 * @param alias name of the mailbox to sent the task to
409 * @param cleanup a function to destroy the task if the
410 * communication fails, e.g. MSG_task_destroy
411 * (if nullptr, no function will be called)
412 * @param maxrate the maximum communication rate for sending this task (byte/sec)
415 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
417 task->simdata->rate = maxrate;
418 MSG_task_dsend(task, alias, cleanup);
421 /** @ingroup msg_task_usage
422 * @brief Starts listening for receiving a task from an asynchronous communication.
424 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
426 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
427 * @param name of the mailbox to receive the task on
428 * @return the msg_comm_t communication created
430 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
432 return MSG_task_irecv_bounded(task, name, -1.0);
435 /** @ingroup msg_task_usage
436 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
438 * The rate parameter can be used to receive a task with a limited
439 * bandwidth (smaller than the physical available value). Use
440 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
441 * value do disable this feature).
443 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
444 * @param name of the mailbox to receive the task on
445 * @param rate limit the bandwidth to the given rate (byte/sec)
446 * @return the msg_comm_t communication created
448 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
450 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
452 /* FIXME: these functions are not traceable */
454 xbt_assert(task, "Null pointer for the task storage");
457 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
459 /* Try to receive it by calling SIMIX network layer */
460 msg_comm_t comm = new simgrid::msg::Comm(
462 simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr, rate));
467 /** @ingroup msg_task_usage
468 * @brief Checks whether a communication is done, and if yes, finalizes it.
469 * @param comm the communication to test
470 * @return 'true' if the communication is finished
471 * (but it may have failed, use MSG_comm_get_status() to know its status)
472 * or 'false' if the communication is not finished yet
473 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
475 int MSG_comm_test(msg_comm_t comm)
477 bool finished = false;
480 finished = simcall_comm_test(comm->s_comm);
481 if (finished && comm->task_received != nullptr) {
482 /* I am the receiver */
483 (*comm->task_received)->simdata->setNotUsed();
487 switch (e.category) {
489 comm->status = MSG_TRANSFER_FAILURE;
493 comm->status = MSG_TIMEOUT;
504 /** @ingroup msg_task_usage
505 * @brief This function checks if a communication is finished.
506 * @param comms a vector of communications
507 * @return the position of the finished communication if any
508 * (but it may have failed, use MSG_comm_get_status() to know its status),
509 * or -1 if none is finished
511 int MSG_comm_testany(xbt_dynar_t comms)
513 int finished_index = -1;
515 /* Create the equivalent array with SIMIX objects: */
516 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
517 s_comms.reserve(xbt_dynar_length(comms));
520 xbt_dynar_foreach(comms, cursor, comm) {
521 s_comms.push_back(comm->s_comm);
524 msg_error_t status = MSG_OK;
526 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
529 switch (e.category) {
531 finished_index = e.value;
532 status = MSG_TRANSFER_FAILURE;
535 finished_index = e.value;
536 status = MSG_TIMEOUT;
543 if (finished_index != -1) {
544 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
545 /* the communication is finished */
546 comm->status = status;
548 if (status == MSG_OK && comm->task_received != nullptr) {
549 /* I am the receiver */
550 (*comm->task_received)->simdata->setNotUsed();
554 return finished_index;
557 /** @ingroup msg_task_usage
558 * @brief Destroys a communication.
559 * @param comm the communication to destroy.
561 void MSG_comm_destroy(msg_comm_t comm)
566 /** @ingroup msg_task_usage
567 * @brief Wait for the completion of a communication.
569 * It takes two parameters.
570 * @param comm the communication to wait.
571 * @param timeout Wait until the communication terminates or the timeout occurs.
572 * You can provide a -1 timeout to obtain an infinite timeout.
573 * @return msg_error_t
575 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
578 simcall_comm_wait(comm->s_comm, timeout);
580 if (comm->task_received != nullptr) {
581 /* I am the receiver */
582 (*comm->task_received)->simdata->setNotUsed();
585 /* FIXME: these functions are not traceable */
588 switch (e.category) {
590 comm->status = MSG_TRANSFER_FAILURE;
593 comm->status = MSG_TIMEOUT;
603 /** @ingroup msg_task_usage
604 * @brief This function is called by a sender and permit to wait for each communication
606 * @param comm a vector of communication
607 * @param nb_elem is the size of the comm vector
608 * @param timeout for each call of MSG_comm_wait
610 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
612 for (int i = 0; i < nb_elem; i++)
613 MSG_comm_wait(comm[i], timeout);
616 /** @ingroup msg_task_usage
617 * @brief This function waits for the first communication finished in a list.
618 * @param comms a vector of communications
619 * @return the position of the first finished communication
620 * (but it may have failed, use MSG_comm_get_status() to know its status)
622 int MSG_comm_waitany(xbt_dynar_t comms)
624 int finished_index = -1;
626 /* create the equivalent dynar with SIMIX objects */
627 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
628 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
632 xbt_dynar_foreach(comms, cursor, comm) {
633 intrusive_ptr_add_ref(comm->s_comm.get());
634 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
637 msg_error_t status = MSG_OK;
639 finished_index = simcall_comm_waitany(s_comms, -1);
642 switch (e.category) {
644 finished_index = e.value;
645 status = MSG_TRANSFER_FAILURE;
648 finished_index = e.value;
649 status = MSG_TIMEOUT;
656 xbt_assert(finished_index != -1, "WaitAny returned -1");
657 xbt_dynar_free(&s_comms);
659 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
660 /* the communication is finished */
661 comm->status = status;
663 if (comm->task_received != nullptr) {
664 /* I am the receiver */
665 (*comm->task_received)->simdata->setNotUsed();
668 return finished_index;
672 * @ingroup msg_task_usage
673 * @brief Returns the error (if any) that occurred during a finished communication.
674 * @param comm a finished communication
675 * @return the status of the communication, or #MSG_OK if no error occurred
676 * during the communication
678 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
683 /** @ingroup msg_task_usage
684 * @brief Get a task (#msg_task_t) from a communication
686 * @param comm the communication where to get the task
687 * @return the task from the communication
689 msg_task_t MSG_comm_get_task(msg_comm_t comm)
691 xbt_assert(comm, "Invalid parameter");
693 return comm->task_received ? *comm->task_received : comm->task_sent;
697 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
698 * @param synchro the comm
699 * @param buff the data copied
700 * @param buff_size size of the buffer
702 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
704 simgrid::kernel::activity::CommImplPtr comm =
705 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
707 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
709 // notify the user callback if any
710 if (msg_global->task_copy_callback) {
711 msg_task_t task = static_cast<msg_task_t>(buff);
712 msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
716 /** @ingroup msg_task_usage
717 * @brief Sends a task to a mailbox
719 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
720 * side if #MSG_task_receive is used).
721 * See #MSG_task_isend for sending tasks asynchronously.
723 * @param task the task to be sent
724 * @param alias the mailbox name to where the task is sent
726 * @return Returns #MSG_OK if the task was successfully sent,
727 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
729 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
731 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
732 return MSG_task_send_with_timeout(task, alias, -1);
735 /** @ingroup msg_task_usage
736 * @brief Sends a task to a mailbox with a maximum rate
738 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
739 * the application to limit the bandwidth utilization of network links when sending the task.
741 * The maxrate parameter can be used to send a task with a limited
742 * bandwidth (smaller than the physical available value). Use
743 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
744 * value do disable this feature).
746 * @param task the task to be sent
747 * @param alias the mailbox name to where the task is sent
748 * @param maxrate the maximum communication rate for sending this task (byte/sec)
750 * @return Returns #MSG_OK if the task was successfully sent,
751 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
753 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
755 task->simdata->rate = maxrate;
756 return MSG_task_send(task, alias);
759 /** @ingroup msg_task_usage
760 * @brief Sends a task to a mailbox with a timeout
762 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
764 * @param task the task to be sent
765 * @param alias the mailbox name to where the task is sent
766 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
768 * @return Returns #MSG_OK if the task was successfully sent,
769 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
771 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
773 msg_error_t ret = MSG_OK;
774 simdata_task_t t_simdata = nullptr;
775 msg_process_t process = MSG_process_self();
776 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
778 TRACE_msg_task_put_start(task);
780 /* Prepare the task to send */
781 t_simdata = task->simdata;
782 t_simdata->sender = process;
783 t_simdata->source = MSG_host_self();
785 t_simdata->setUsed();
787 t_simdata->comm = nullptr;
788 msg_global->sent_msg++;
790 /* Try to send it by calling SIMIX network layer */
792 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
793 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
794 sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
795 if (TRACE_is_enabled() && task->category != nullptr)
796 simcall_set_category(comm, task->category);
797 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
798 simcall_comm_wait(comm, timeout);
801 switch (e.category) {
803 ret = MSG_HOST_FAILURE;
806 ret = MSG_TRANSFER_FAILURE;
815 /* If the send failed, it is not used anymore */
816 t_simdata->setNotUsed();
819 TRACE_msg_task_put_end();
823 /** @ingroup msg_task_usage
824 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
826 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
828 * The maxrate parameter can be used to send a task with a limited
829 * bandwidth (smaller than the physical available value). Use
830 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
831 * value do disable this feature).
833 * @param task the task to be sent
834 * @param alias the mailbox name to where the task is sent
835 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
836 * @param maxrate the maximum communication rate for sending this task (byte/sec)
838 * @return Returns #MSG_OK if the task was successfully sent,
839 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
841 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
843 task->simdata->rate = maxrate;
844 return MSG_task_send_with_timeout(task, alias, timeout);
847 /** @ingroup msg_task_usage
848 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
850 * @param alias the name of the mailbox to be considered
852 * @return Returns the PID of sender process,
853 * -1 if there is no communication in the mailbox.
855 int MSG_task_listen_from(const char *alias)
857 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
858 simgrid::kernel::activity::CommImplPtr comm =
859 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
864 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
867 /** @ingroup msg_task_usage
868 * @brief Sets the tracing category of a task.
870 * This function should be called after the creation of a MSG task, to define the category of that task. The
871 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
872 * parameter category must contain a category that was previously declared with the function #TRACE_category
873 * (or with #TRACE_category_with_color).
875 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
877 * @param task the task that is going to be categorized
878 * @param category the name of the category to be associated to the task
880 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
882 void MSG_task_set_category (msg_task_t task, const char *category)
884 TRACE_msg_set_task_category (task, category);
887 /** @ingroup msg_task_usage
889 * @brief Gets the current tracing category of a task.
891 * @param task the task to be considered
893 * @see MSG_task_set_category
895 * @return Returns the name of the tracing category of the given task, nullptr otherwise
897 const char *MSG_task_get_category (msg_task_t task)
899 return task->category;