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 "src/instr/instr_private.hpp"
9 #include "src/kernel/activity/ExecImpl.hpp"
10 #include "src/msg/msg_private.hpp"
11 #include "src/simix/smx_private.hpp" /* MSG_task_listen looks inside the rdv directly. Not clean. */
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
15 /** \ingroup msg_task_usage
16 * \brief Executes a task and waits for its termination.
18 * This function is used for describing the behavior of a process. It takes only one parameter.
19 * \param task a #msg_task_t to execute on the location on which the process is running.
20 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
22 msg_error_t MSG_task_execute(msg_task_t task)
24 return MSG_parallel_task_execute(task);
27 /** \ingroup msg_task_usage
28 * \brief Executes a parallel task and waits for its termination.
30 * \param task a #msg_task_t to execute on the location on which the process is running.
32 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
33 * or #MSG_HOST_FAILURE otherwise
35 msg_error_t MSG_parallel_task_execute(msg_task_t task)
37 return MSG_parallel_task_execute_with_timeout(task, -1);
40 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
42 simdata_task_t simdata = task->simdata;
43 e_smx_state_t comp_state;
44 msg_error_t status = MSG_OK;
46 TRACE_msg_task_execute_start(task);
48 xbt_assert((not simdata->compute) && not task->simdata->isused,
49 "This task is executed somewhere else. Go fix your code!");
51 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
53 if (simdata->flops_amount <= 0.0 && not simdata->host_nb) {
54 TRACE_msg_task_execute_end(task);
61 if (simdata->host_nb > 0) {
63 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
64 task->name, simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
65 simdata->bytes_parallel_amount, -1.0, timeout));
66 XBT_DEBUG("Parallel execution action created: %p", simdata->compute.get());
68 simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
69 simcall_execution_start(task->name, simdata->flops_amount, simdata->priority, simdata->bound,
70 MSG_process_get_host(MSG_process_self())));
72 simcall_set_category(simdata->compute, task->category);
73 comp_state = simcall_execution_wait(simdata->compute);
75 simdata->setNotUsed();
77 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
82 status = MSG_TASK_CANCELED;
85 status = MSG_HOST_FAILURE;
95 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
96 simdata->flops_amount = 0.0;
97 simdata->comm = nullptr;
98 simdata->compute = nullptr;
99 TRACE_msg_task_execute_end(task);
104 /** \ingroup msg_task_usage
105 * \brief Sleep for the specified number of seconds
107 * Makes the current process sleep until \a time seconds have elapsed.
109 * \param nb_sec a number of second
111 msg_error_t MSG_process_sleep(double nb_sec)
113 msg_error_t status = MSG_OK;
115 if (TRACE_actor_is_enabled())
116 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->push_event("sleep");
119 simcall_process_sleep(nb_sec);
122 if (e.category == cancel_error) {
123 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
124 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
125 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
126 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
127 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
128 // and did not change anythings at the C level.
129 // 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)
130 status = MSG_TASK_CANCELED;
135 if (TRACE_actor_is_enabled())
136 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->pop_event();
141 /** \ingroup msg_task_usage
142 * \brief Receives a task from a mailbox.
144 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
145 * for receiving tasks asynchronously.
147 * \param task a memory location for storing a #msg_task_t.
148 * \param alias name of the mailbox to receive the task from
151 * #MSG_OK if the task was successfully received,
152 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
154 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
156 return MSG_task_receive_with_timeout(task, alias, -1);
159 /** \ingroup msg_task_usage
160 * \brief Receives a task from a mailbox at a given rate.
162 * \param task a memory location for storing a #msg_task_t.
163 * \param alias name of the mailbox to receive the task from
164 * \param rate limit the reception to rate bandwidth (byte/sec)
166 * The rate parameter can be used to receive a task with a limited
167 * bandwidth (smaller than the physical available value). Use
168 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
169 * rate value do disable this feature).
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 (byte/sec)
208 * The rate parameter can be used to send a task with a limited
209 * bandwidth (smaller than the physical available value). Use
210 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
211 * rate value do disable this feature).
214 * #MSG_OK if the task was successfully received,
215 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
217 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
219 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
222 /** \ingroup msg_task_usage
223 * \brief Receives a task from a mailbox from a specific host with a given timeout.
225 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
226 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
227 * to obtain an infinite timeout.
229 * \param task a memory location for storing a #msg_task_t.
230 * \param alias name of the mailbox to receive the task from
231 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
232 * \param host a #msg_host_t host from where the task was sent
235 * #MSG_OK if the task was successfully received,
236 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
238 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
240 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
241 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
244 /** \ingroup msg_task_usage
245 * \brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
247 * \param task a memory location for storing a #msg_task_t.
248 * \param alias name of the mailbox to receive the task from
249 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
250 * \param host a #msg_host_t host from where the task was sent
251 * \param rate limit the reception to rate bandwidth (byte/sec)
253 * The rate parameter can be used to receive a task with a limited
254 * bandwidth (smaller than the physical available value). Use
255 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
256 * rate value do disable this feature).
259 * #MSG_OK if the task was successfully received,
260 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
262 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
265 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
266 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
267 msg_error_t ret = MSG_OK;
268 /* We no longer support getting a task from a specific host */
272 TRACE_msg_task_get_start();
275 xbt_assert(task, "Null pointer for the task storage");
278 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
280 /* Try to receive it by calling SIMIX network layer */
282 simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
284 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
285 (*task)->simdata->setNotUsed();
288 switch (e.category) {
291 ret = MSG_HOST_FAILURE;
294 ret = MSG_TRANSFER_FAILURE;
304 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
305 TRACE_msg_task_get_end(*task);
310 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
311 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
312 void_f_pvoid_t cleanup, int detached)
314 simdata_task_t t_simdata = nullptr;
315 msg_process_t myself = MSG_process_self();
316 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
317 TRACE_msg_task_put_start(task);
319 /* Prepare the task to send */
320 t_simdata = task->simdata;
321 t_simdata->sender = myself;
322 t_simdata->source = MSG_host_self();
323 t_simdata->setUsed();
324 t_simdata->comm = nullptr;
325 msg_global->sent_msg++;
327 /* Send it by calling SIMIX network layer */
329 simcall_comm_isend(myself->get_impl(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
330 sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
331 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
333 msg_comm_t comm = nullptr;
335 comm = new simgrid::msg::Comm(task, nullptr, act);
338 if (TRACE_is_enabled())
339 simcall_set_category(act, task->category);
340 TRACE_msg_task_put_end();
345 /** \ingroup msg_task_usage
346 * \brief Sends a task on a mailbox.
348 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
350 * \param task a #msg_task_t to send on another location.
351 * \param alias name of the mailbox to sent the task to
352 * \return the msg_comm_t communication created
354 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
356 return MSG_task_isend_internal(task, alias, nullptr, 0);
359 /** \ingroup msg_task_usage
360 * \brief Sends a task on a mailbox with a maximum rate
362 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
363 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
365 * \param task a #msg_task_t to send on another location.
366 * \param alias name of the mailbox to sent the task to
367 * \param maxrate the maximum communication rate for sending this task (byte/sec).
368 * \return the msg_comm_t communication created
370 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
372 task->simdata->rate = maxrate;
373 return MSG_task_isend_internal(task, alias, nullptr, 0);
376 /** \ingroup msg_task_usage
377 * \brief Sends a task on a mailbox.
379 * This is a non blocking detached send function.
380 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
381 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
382 * usual. More details on this can be obtained on
383 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
384 * in the SimGrid-user mailing list archive.
386 * \param task a #msg_task_t to send on another location.
387 * \param alias name of the mailbox to sent the task to
388 * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
389 * (if nullptr, no function will be called)
391 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
393 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
394 xbt_assert(comm == nullptr);
397 /** \ingroup msg_task_usage
398 * \brief Sends a task on a mailbox with a maximal rate.
400 * This is a non blocking detached send function.
401 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
402 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
403 * usual. More details on this can be obtained on
404 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
405 * in the SimGrid-user mailing list archive.
407 * The rate parameter can be used to send a task with a limited
408 * bandwidth (smaller than the physical available value). Use
409 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
410 * value do disable this feature).
412 * \param task a #msg_task_t to send on another location.
413 * \param alias name of the mailbox to sent the task to
414 * \param cleanup a function to destroy the task if the
415 * communication fails, e.g. MSG_task_destroy
416 * (if nullptr, no function will be called)
417 * \param maxrate the maximum communication rate for sending this task (byte/sec)
420 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
422 task->simdata->rate = maxrate;
423 MSG_task_dsend(task, alias, cleanup);
426 /** \ingroup msg_task_usage
427 * \brief Starts listening for receiving a task from an asynchronous communication.
429 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
431 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
432 * \param name of the mailbox to receive the task on
433 * \return the msg_comm_t communication created
435 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
437 return MSG_task_irecv_bounded(task, name, -1.0);
440 /** \ingroup msg_task_usage
441 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
443 * The rate parameter can be used to receive a task with a limited
444 * bandwidth (smaller than the physical available value). Use
445 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
446 * value do disable this feature).
448 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
449 * \param name of the mailbox to receive the task on
450 * \param rate limit the bandwidth to the given rate (byte/sec)
451 * \return the msg_comm_t communication created
453 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
455 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
457 /* FIXME: these functions are not traceable */
459 xbt_assert(task, "Null pointer for the task storage");
462 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
464 /* Try to receive it by calling SIMIX network layer */
465 msg_comm_t comm = new simgrid::msg::Comm(
467 simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr, rate));
472 /** \ingroup msg_task_usage
473 * \brief Checks whether a communication is done, and if yes, finalizes it.
474 * \param comm the communication to test
475 * \return 'true' if the communication is finished
476 * (but it may have failed, use MSG_comm_get_status() to know its status)
477 * or 'false' if the communication is not finished yet
478 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
480 int MSG_comm_test(msg_comm_t comm)
482 bool finished = false;
485 finished = simcall_comm_test(comm->s_comm);
486 if (finished && comm->task_received != nullptr) {
487 /* I am the receiver */
488 (*comm->task_received)->simdata->setNotUsed();
492 switch (e.category) {
494 comm->status = MSG_TRANSFER_FAILURE;
498 comm->status = MSG_TIMEOUT;
509 /** \ingroup msg_task_usage
510 * \brief This function checks if a communication is finished.
511 * \param comms a vector of communications
512 * \return the position of the finished communication if any
513 * (but it may have failed, use MSG_comm_get_status() to know its status),
514 * or -1 if none is finished
516 int MSG_comm_testany(xbt_dynar_t comms)
518 int finished_index = -1;
520 /* Create the equivalent array with SIMIX objects: */
521 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
522 s_comms.reserve(xbt_dynar_length(comms));
525 xbt_dynar_foreach(comms, cursor, comm) {
526 s_comms.push_back(comm->s_comm);
529 msg_error_t status = MSG_OK;
531 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
534 switch (e.category) {
536 finished_index = e.value;
537 status = MSG_TRANSFER_FAILURE;
540 finished_index = e.value;
541 status = MSG_TIMEOUT;
548 if (finished_index != -1) {
549 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
550 /* the communication is finished */
551 comm->status = status;
553 if (status == MSG_OK && comm->task_received != nullptr) {
554 /* I am the receiver */
555 (*comm->task_received)->simdata->setNotUsed();
559 return finished_index;
562 /** \ingroup msg_task_usage
563 * \brief Destroys a communication.
564 * \param comm the communication to destroy.
566 void MSG_comm_destroy(msg_comm_t comm)
571 /** \ingroup msg_task_usage
572 * \brief Wait for the completion of a communication.
574 * It takes two parameters.
575 * \param comm the communication to wait.
576 * \param timeout Wait until the communication terminates or the timeout occurs.
577 * You can provide a -1 timeout to obtain an infinite timeout.
578 * \return msg_error_t
580 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
583 simcall_comm_wait(comm->s_comm, timeout);
585 if (comm->task_received != nullptr) {
586 /* I am the receiver */
587 (*comm->task_received)->simdata->setNotUsed();
590 /* FIXME: these functions are not traceable */
593 switch (e.category) {
595 comm->status = MSG_TRANSFER_FAILURE;
598 comm->status = MSG_TIMEOUT;
608 /** \ingroup msg_task_usage
609 * \brief This function is called by a sender and permit to wait for each communication
611 * \param comm a vector of communication
612 * \param nb_elem is the size of the comm vector
613 * \param timeout for each call of MSG_comm_wait
615 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
617 for (int i = 0; i < nb_elem; i++)
618 MSG_comm_wait(comm[i], timeout);
621 /** \ingroup msg_task_usage
622 * \brief This function waits for the first communication finished in a list.
623 * \param comms a vector of communications
624 * \return the position of the first finished communication
625 * (but it may have failed, use MSG_comm_get_status() to know its status)
627 int MSG_comm_waitany(xbt_dynar_t comms)
629 int finished_index = -1;
631 /* create the equivalent dynar with SIMIX objects */
632 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
633 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
637 xbt_dynar_foreach(comms, cursor, comm) {
638 intrusive_ptr_add_ref(comm->s_comm.get());
639 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
642 msg_error_t status = MSG_OK;
644 finished_index = simcall_comm_waitany(s_comms, -1);
647 switch (e.category) {
649 finished_index = e.value;
650 status = MSG_TRANSFER_FAILURE;
653 finished_index = e.value;
654 status = MSG_TIMEOUT;
661 xbt_assert(finished_index != -1, "WaitAny returned -1");
662 xbt_dynar_free(&s_comms);
664 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
665 /* the communication is finished */
666 comm->status = status;
668 if (comm->task_received != nullptr) {
669 /* I am the receiver */
670 (*comm->task_received)->simdata->setNotUsed();
673 return finished_index;
677 * \ingroup msg_task_usage
678 * \brief Returns the error (if any) that occurred during a finished communication.
679 * \param comm a finished communication
680 * \return the status of the communication, or #MSG_OK if no error occurred
681 * during the communication
683 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
688 /** \ingroup msg_task_usage
689 * \brief Get a task (#msg_task_t) from a communication
691 * \param comm the communication where to get the task
692 * \return the task from the communication
694 msg_task_t MSG_comm_get_task(msg_comm_t comm)
696 xbt_assert(comm, "Invalid parameter");
698 return comm->task_received ? *comm->task_received : comm->task_sent;
702 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
703 * \param synchro the comm
704 * \param buff the data copied
705 * \param buff_size size of the buffer
707 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
709 simgrid::kernel::activity::CommImplPtr comm =
710 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
712 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
714 // notify the user callback if any
715 if (msg_global->task_copy_callback) {
716 msg_task_t task = static_cast<msg_task_t>(buff);
717 msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
721 /** \ingroup msg_task_usage
722 * \brief Sends a task to a mailbox
724 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
725 * side if #MSG_task_receive is used).
726 * See #MSG_task_isend for sending tasks asynchronously.
728 * \param task the task to be sent
729 * \param alias the mailbox name to where the task is sent
731 * \return Returns #MSG_OK if the task was successfully sent,
732 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
734 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
736 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
737 return MSG_task_send_with_timeout(task, alias, -1);
740 /** \ingroup msg_task_usage
741 * \brief Sends a task to a mailbox with a maximum rate
743 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
744 * the application to limit the bandwidth utilization of network links when sending the task.
746 * The maxrate parameter can be used to send a task with a limited
747 * bandwidth (smaller than the physical available value). Use
748 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
749 * value do disable this feature).
751 * \param task the task to be sent
752 * \param alias the mailbox name to where the task is sent
753 * \param maxrate the maximum communication rate for sending this task (byte/sec)
755 * \return Returns #MSG_OK if the task was successfully sent,
756 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
758 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
760 task->simdata->rate = maxrate;
761 return MSG_task_send(task, alias);
764 /** \ingroup msg_task_usage
765 * \brief Sends a task to a mailbox with a timeout
767 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
769 * \param task the task to be sent
770 * \param alias the mailbox name to where the task is sent
771 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
773 * \return Returns #MSG_OK if the task was successfully sent,
774 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
776 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
778 msg_error_t ret = MSG_OK;
779 simdata_task_t t_simdata = nullptr;
780 msg_process_t process = MSG_process_self();
781 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
783 TRACE_msg_task_put_start(task);
785 /* Prepare the task to send */
786 t_simdata = task->simdata;
787 t_simdata->sender = process;
788 t_simdata->source = MSG_host_self();
790 t_simdata->setUsed();
792 t_simdata->comm = nullptr;
793 msg_global->sent_msg++;
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->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
799 sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
800 if (TRACE_is_enabled())
801 simcall_set_category(comm, task->category);
802 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(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 TRACE_msg_task_put_end();
828 /** \ingroup msg_task_usage
829 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
831 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
833 * The maxrate parameter can be used to send a task with a limited
834 * bandwidth (smaller than the physical available value). Use
835 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
836 * value do disable this feature).
838 * \param task the task to be sent
839 * \param alias the mailbox name to where the task is sent
840 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
841 * \param maxrate the maximum communication rate for sending this task (byte/sec)
843 * \return Returns #MSG_OK if the task was successfully sent,
844 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
846 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
848 task->simdata->rate = maxrate;
849 return MSG_task_send_with_timeout(task, alias, timeout);
852 /** \ingroup msg_task_usage
853 * \brief Check if there is a communication going on in a mailbox.
855 * \param alias the name of the mailbox to be considered
857 * \return Returns 1 if there is a communication, 0 otherwise
859 int MSG_task_listen(const char *alias)
861 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
862 return mbox->listen() ? 1 : 0;
865 /** \ingroup msg_task_usage
866 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
868 * \param alias the name of the mailbox to be considered
870 * \return Returns the PID of sender process,
871 * -1 if there is no communication in the mailbox.
873 int MSG_task_listen_from(const char *alias)
875 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
876 simgrid::kernel::activity::CommImplPtr comm =
877 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
882 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
885 /** \ingroup msg_task_usage
886 * \brief Sets the tracing category of a task.
888 * This function should be called after the creation of a MSG task, to define the category of that task. The
889 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
890 * parameter category must contain a category that was previously declared with the function #TRACE_category
891 * (or with #TRACE_category_with_color).
893 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
895 * \param task the task that is going to be categorized
896 * \param category the name of the category to be associated to the task
898 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
900 void MSG_task_set_category (msg_task_t task, const char *category)
902 TRACE_msg_set_task_category (task, category);
905 /** \ingroup msg_task_usage
907 * \brief Gets the current tracing category of a task.
909 * \param task the task to be considered
911 * \see MSG_task_set_category
913 * \return Returns the name of the tracing category of the given task, nullptr otherwise
915 const char *MSG_task_get_category (msg_task_t task)
917 return task->category;