1 /* Copyright (c) 2004-2019. 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)");
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);
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;
83 } catch (simgrid::TimeoutError& e) {
86 if (e.category == cancel_error)
87 status = MSG_TASK_CANCELED;
92 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
93 simdata->flops_amount = 0.0;
94 simdata->comm = nullptr;
95 simdata->compute = nullptr;
96 TRACE_msg_task_execute_end(task);
102 * @brief Sleep for the specified number of seconds
104 * Makes the current process sleep until @a time seconds have elapsed.
106 * @param nb_sec a number of second
108 msg_error_t MSG_process_sleep(double nb_sec)
110 msg_error_t status = MSG_OK;
113 simgrid::s4u::this_actor::sleep_for(nb_sec);
114 } catch (simgrid::HostFailureException& e) {
115 status = MSG_HOST_FAILURE;
116 } catch (xbt_ex& e) {
117 if (e.category == cancel_error) {
118 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
119 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
120 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
121 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
122 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
123 // and did not change anythings at the C level.
124 // 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)
125 status = MSG_TASK_CANCELED;
134 * @brief Receives a task from a mailbox.
136 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
137 * for receiving tasks asynchronously.
139 * @param task a memory location for storing a #msg_task_t.
140 * @param alias name of the mailbox to receive the task from
143 * #MSG_OK if the task was successfully received,
144 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
146 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
148 return MSG_task_receive_with_timeout(task, alias, -1);
152 * @brief Receives a task from a mailbox at a given rate.
154 * @param task a memory location for storing a #msg_task_t.
155 * @param alias name of the mailbox to receive the task from
156 * @param rate limit the reception to rate bandwidth (byte/sec)
158 * The rate parameter can be used to receive a task with a limited
159 * bandwidth (smaller than the physical available value). Use
160 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
161 * rate value do disable this feature).
164 * #MSG_OK if the task was successfully received,
165 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
167 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
169 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
173 * @brief Receives a task from a mailbox with a given timeout.
175 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
176 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
177 * to obtain an infinite timeout.
179 * @param task a memory location for storing a #msg_task_t.
180 * @param alias name of the mailbox to receive the task from
181 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
184 * #MSG_OK if the task was successfully received,
185 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
187 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
189 return MSG_task_receive_ext(task, alias, timeout, nullptr);
193 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
195 * @param task a memory location for storing a #msg_task_t.
196 * @param alias name of the mailbox to receive the task from
197 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
198 * @param rate limit the reception to rate bandwidth (byte/sec)
200 * The rate parameter can be used to send a task with a limited
201 * bandwidth (smaller than the physical available value). Use
202 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
203 * rate value do disable this feature).
206 * #MSG_OK if the task was successfully received,
207 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
209 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
211 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
215 * @brief Receives a task from a mailbox from a specific host with a given timeout.
217 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
218 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
219 * to obtain an infinite timeout.
221 * @param task a memory location for storing a #msg_task_t.
222 * @param alias name of the mailbox to receive the task from
223 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
224 * @param host a #msg_host_t host from where the task was sent
227 * #MSG_OK if the task was successfully received,
228 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
230 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
232 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
233 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
237 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
239 * @param task a memory location for storing a #msg_task_t.
240 * @param alias name of the mailbox to receive the task from
241 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
242 * @param host a #msg_host_t host from where the task was sent
243 * @param rate limit the reception to rate bandwidth (byte/sec)
245 * The rate parameter can be used to receive a task with a limited
246 * bandwidth (smaller than the physical available value). Use
247 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
248 * rate value do disable this feature).
251 * #MSG_OK if the task was successfully received,
252 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
254 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
257 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
258 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
259 msg_error_t ret = MSG_OK;
260 /* We no longer support getting a task from a specific host */
264 TRACE_msg_task_get_start();
267 xbt_assert(task, "Null pointer for the task storage");
270 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
272 /* Try to receive it by calling SIMIX network layer */
274 simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
276 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
277 (*task)->simdata->setNotUsed();
278 } catch (simgrid::HostFailureException& e) {
279 ret = MSG_HOST_FAILURE;
280 } catch (simgrid::TimeoutError& e) {
282 } catch (xbt_ex& e) {
283 switch (e.category) {
285 ret = MSG_HOST_FAILURE;
288 ret = MSG_TRANSFER_FAILURE;
295 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
296 TRACE_msg_task_get_end(*task);
301 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
302 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
303 void_f_pvoid_t cleanup, int detached)
305 simdata_task_t t_simdata = nullptr;
306 msg_process_t myself = MSG_process_self();
307 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
308 TRACE_msg_task_put_start(task);
310 /* Prepare the task to send */
311 t_simdata = task->simdata;
312 t_simdata->sender = myself;
313 t_simdata->source = MSG_host_self();
314 t_simdata->setUsed();
315 t_simdata->comm = nullptr;
316 msg_global->sent_msg++;
318 /* Send it by calling SIMIX network layer */
320 simcall_comm_isend(myself->get_impl(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
321 sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
322 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
324 msg_comm_t comm = nullptr;
326 comm = new simgrid::msg::Comm(task, nullptr, act);
329 if (TRACE_is_enabled())
330 simcall_set_category(act, task->category);
331 TRACE_msg_task_put_end();
337 * @brief Sends a task on a mailbox.
339 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
341 * @param task a #msg_task_t to send on another location.
342 * @param alias name of the mailbox to sent the task to
343 * @return the msg_comm_t communication created
345 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
347 return MSG_task_isend_internal(task, alias, nullptr, 0);
351 * @brief Sends a task on a mailbox with a maximum rate
353 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
354 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
356 * @param task a #msg_task_t to send on another location.
357 * @param alias name of the mailbox to sent the task to
358 * @param maxrate the maximum communication rate for sending this task (byte/sec).
359 * @return the msg_comm_t communication created
361 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
363 task->simdata->rate = maxrate;
364 return MSG_task_isend_internal(task, alias, nullptr, 0);
368 * @brief Sends a task on a mailbox.
370 * This is a non blocking detached send function.
371 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
372 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
373 * usual. More details on this can be obtained on
374 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
375 * in the SimGrid-user mailing list archive.
377 * @param task a #msg_task_t to send on another location.
378 * @param alias name of the mailbox to sent the task to
379 * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
380 * (if nullptr, no function will be called)
382 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
384 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
385 xbt_assert(comm == nullptr);
389 * @brief Sends a task on a mailbox with a maximal rate.
391 * This is a non blocking detached send function.
392 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
393 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
394 * usual. More details on this can be obtained on
395 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
396 * in the SimGrid-user mailing list archive.
398 * The rate parameter can be used to send a task with a limited
399 * bandwidth (smaller than the physical available value). Use
400 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
401 * value do disable this feature).
403 * @param task a #msg_task_t to send on another location.
404 * @param alias name of the mailbox to sent the task to
405 * @param cleanup a function to destroy the task if the
406 * communication fails, e.g. MSG_task_destroy
407 * (if nullptr, no function will be called)
408 * @param maxrate the maximum communication rate for sending this task (byte/sec)
411 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
413 task->simdata->rate = maxrate;
414 MSG_task_dsend(task, alias, cleanup);
418 * @brief Starts listening for receiving a task from an asynchronous communication.
420 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
422 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
423 * @param name of the mailbox to receive the task on
424 * @return the msg_comm_t communication created
426 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
428 return MSG_task_irecv_bounded(task, name, -1.0);
432 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
434 * The rate parameter can be used to receive a task with a limited
435 * bandwidth (smaller than the physical available value). Use
436 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
437 * value do disable this feature).
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 * @param rate limit the bandwidth to the given rate (byte/sec)
442 * @return the msg_comm_t communication created
444 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
446 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
448 /* FIXME: these functions are not traceable */
450 xbt_assert(task, "Null pointer for the task storage");
453 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
455 /* Try to receive it by calling SIMIX network layer */
456 msg_comm_t comm = new simgrid::msg::Comm(
458 simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr, rate));
464 * @brief Checks whether a communication is done, and if yes, finalizes it.
465 * @param comm the communication to test
466 * @return 'true' if the communication is finished
467 * (but it may have failed, use MSG_comm_get_status() to know its status)
468 * or 'false' if the communication is not finished yet
469 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
471 int MSG_comm_test(msg_comm_t comm)
473 bool finished = false;
476 finished = simcall_comm_test(comm->s_comm);
477 if (finished && comm->task_received != nullptr) {
478 /* I am the receiver */
479 (*comm->task_received)->simdata->setNotUsed();
481 } catch (simgrid::TimeoutError& e) {
482 comm->status = MSG_TIMEOUT;
486 if (e.category == network_error) {
487 comm->status = MSG_TRANSFER_FAILURE;
498 * @brief This function checks if a communication is finished.
499 * @param comms a vector of communications
500 * @return the position of the finished communication if any
501 * (but it may have failed, use MSG_comm_get_status() to know its status),
502 * or -1 if none is finished
504 int MSG_comm_testany(xbt_dynar_t comms)
506 int finished_index = -1;
508 /* Create the equivalent array with SIMIX objects: */
509 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
510 s_comms.reserve(xbt_dynar_length(comms));
513 xbt_dynar_foreach(comms, cursor, comm) {
514 s_comms.push_back(comm->s_comm);
517 msg_error_t status = MSG_OK;
519 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
520 } catch (simgrid::TimeoutError& e) {
521 finished_index = e.value;
522 status = MSG_TIMEOUT;
525 if (e.category != network_error)
527 finished_index = e.value;
528 status = MSG_TRANSFER_FAILURE;
531 if (finished_index != -1) {
532 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
533 /* the communication is finished */
534 comm->status = status;
536 if (status == MSG_OK && comm->task_received != nullptr) {
537 /* I am the receiver */
538 (*comm->task_received)->simdata->setNotUsed();
542 return finished_index;
545 /** @brief Destroys the provided communication. */
546 void MSG_comm_destroy(msg_comm_t comm)
551 /** @brief Wait for the completion of a communication.
553 * It takes two parameters.
554 * @param comm the communication to wait.
555 * @param timeout Wait until the communication terminates or the timeout occurs.
556 * You can provide a -1 timeout to obtain an infinite timeout.
557 * @return msg_error_t
559 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
562 simcall_comm_wait(comm->s_comm, timeout);
564 if (comm->task_received != nullptr) {
565 /* I am the receiver */
566 (*comm->task_received)->simdata->setNotUsed();
569 /* FIXME: these functions are not traceable */
570 } catch (simgrid::TimeoutError& e) {
571 comm->status = MSG_TIMEOUT;
574 if (e.category == network_error)
575 comm->status = MSG_TRANSFER_FAILURE;
583 /** @brief This function is called by a sender and permit to wait for each communication
585 * @param comm a vector of communication
586 * @param nb_elem is the size of the comm vector
587 * @param timeout for each call of MSG_comm_wait
589 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
591 for (int i = 0; i < nb_elem; i++)
592 MSG_comm_wait(comm[i], timeout);
595 /** @brief This function waits for the first communication finished in a list.
596 * @param comms a vector of communications
597 * @return the position of the first finished communication
598 * (but it may have failed, use MSG_comm_get_status() to know its status)
600 int MSG_comm_waitany(xbt_dynar_t comms)
602 int finished_index = -1;
604 /* create the equivalent dynar with SIMIX objects */
605 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
606 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
610 xbt_dynar_foreach(comms, cursor, comm) {
611 intrusive_ptr_add_ref(comm->s_comm.get());
612 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
615 msg_error_t status = MSG_OK;
617 finished_index = simcall_comm_waitany(s_comms, -1);
618 } catch (simgrid::TimeoutError& e) {
619 finished_index = e.value;
620 status = MSG_TIMEOUT;
623 if (e.category == network_error) {
624 finished_index = e.value;
625 status = MSG_TRANSFER_FAILURE;
631 xbt_assert(finished_index != -1, "WaitAny returned -1");
632 xbt_dynar_free(&s_comms);
634 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
635 /* the communication is finished */
636 comm->status = status;
638 if (comm->task_received != nullptr) {
639 /* I am the receiver */
640 (*comm->task_received)->simdata->setNotUsed();
643 return finished_index;
647 * @brief Returns the error (if any) that occurred during a finished communication.
648 * @param comm a finished communication
649 * @return the status of the communication, or #MSG_OK if no error occurred
650 * during the communication
652 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
657 /** @brief Get a task (#msg_task_t) from a communication
659 * @param comm the communication where to get the task
660 * @return the task from the communication
662 msg_task_t MSG_comm_get_task(msg_comm_t comm)
664 xbt_assert(comm, "Invalid parameter");
666 return comm->task_received ? *comm->task_received : comm->task_sent;
670 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
671 * @param synchro the comm
672 * @param buff the data copied
673 * @param buff_size size of the buffer
675 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
677 simgrid::kernel::activity::CommImplPtr comm =
678 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
680 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
682 // notify the user callback if any
683 if (msg_global->task_copy_callback) {
684 msg_task_t task = static_cast<msg_task_t>(buff);
685 msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());
690 * @brief Sends a task to a mailbox
692 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
693 * side if #MSG_task_receive is used).
694 * See #MSG_task_isend for sending tasks asynchronously.
696 * @param task the task to be sent
697 * @param alias the mailbox name to where the task is sent
699 * @return Returns #MSG_OK if the task was successfully sent,
700 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
702 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
704 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
705 return MSG_task_send_with_timeout(task, alias, -1);
709 * @brief Sends a task to a mailbox with a maximum rate
711 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
712 * the application to limit the bandwidth utilization of network links when sending the task.
714 * The maxrate parameter can be used to send a task with a limited
715 * bandwidth (smaller than the physical available value). Use
716 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
717 * value do disable this feature).
719 * @param task the task to be sent
720 * @param alias the mailbox name to where the task is sent
721 * @param maxrate the maximum communication rate for sending this task (byte/sec)
723 * @return Returns #MSG_OK if the task was successfully sent,
724 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
726 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
728 task->simdata->rate = maxrate;
729 return MSG_task_send(task, alias);
733 * @brief Sends a task to a mailbox with a timeout
735 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
737 * @param task the task to be sent
738 * @param alias the mailbox name to where the task is sent
739 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
741 * @return Returns #MSG_OK if the task was successfully sent,
742 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
744 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
746 msg_error_t ret = MSG_OK;
747 simdata_task_t t_simdata = nullptr;
748 msg_process_t process = MSG_process_self();
749 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
751 TRACE_msg_task_put_start(task);
753 /* Prepare the task to send */
754 t_simdata = task->simdata;
755 t_simdata->sender = process;
756 t_simdata->source = MSG_host_self();
758 t_simdata->setUsed();
760 t_simdata->comm = nullptr;
761 msg_global->sent_msg++;
763 /* Try to send it by calling SIMIX network layer */
765 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
766 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
767 sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
768 if (TRACE_is_enabled() && task->category != nullptr)
769 simcall_set_category(comm, task->category);
770 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
771 simcall_comm_wait(comm, timeout);
772 } catch (simgrid::TimeoutError& e) {
776 switch (e.category) {
778 ret = MSG_HOST_FAILURE;
781 ret = MSG_TRANSFER_FAILURE;
787 /* If the send failed, it is not used anymore */
788 t_simdata->setNotUsed();
791 TRACE_msg_task_put_end();
796 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
798 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
800 * The maxrate parameter can be used to send a task with a limited
801 * bandwidth (smaller than the physical available value). Use
802 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
803 * value do disable this feature).
805 * @param task the task to be sent
806 * @param alias the mailbox name to where the task is sent
807 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
808 * @param maxrate the maximum communication rate for sending this task (byte/sec)
810 * @return Returns #MSG_OK if the task was successfully sent,
811 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
813 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
815 task->simdata->rate = maxrate;
816 return MSG_task_send_with_timeout(task, alias, timeout);
820 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
822 * @param alias the name of the mailbox to be considered
824 * @return Returns the PID of sender process,
825 * -1 if there is no communication in the mailbox.
827 int MSG_task_listen_from(const char *alias)
829 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
830 simgrid::kernel::activity::CommImplPtr comm =
831 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
836 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff_)->simdata->sender);
840 * @brief Sets the tracing category of a task.
842 * This function should be called after the creation of a MSG task, to define the category of that task. The
843 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
844 * parameter category must contain a category that was previously declared with the function #TRACE_category
845 * (or with #TRACE_category_with_color).
847 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
849 * @param task the task that is going to be categorized
850 * @param category the name of the category to be associated to the task
852 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
854 void MSG_task_set_category (msg_task_t task, const char *category)
856 TRACE_msg_set_task_category (task, category);
860 * @brief Gets the current tracing category of a task.
862 * @param task the task to be considered
864 * @see MSG_task_set_category
866 * @return Returns the name of the tracing category of the given task, nullptr otherwise
868 const char *MSG_task_get_category (msg_task_t task)
870 return task->category;