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/kernel/activity/ExecImpl.hpp"
9 #include "src/msg/msg_private.hpp"
10 #include "src/simix/smx_private.hpp" /* MSG_task_listen looks inside the rdv directly. Not clean. */
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
16 /** \ingroup msg_task_usage
17 * \brief Executes a task and waits for its termination.
19 * This function is used for describing the behavior of a process. It takes only one parameter.
20 * \param task a #msg_task_t to execute on the location on which the process is running.
21 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
23 msg_error_t MSG_task_execute(msg_task_t task)
25 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 simcall_set_category(simdata->compute, task->category);
74 comp_state = simcall_execution_wait(simdata->compute);
76 simdata->setNotUsed();
78 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
83 status = MSG_TASK_CANCELED;
86 status = MSG_HOST_FAILURE;
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;
116 if (TRACE_actor_is_enabled())
117 simgrid::instr::Container::byName(instr_pid(MSG_process_self()))->getState("ACTOR_STATE")->pushEvent("sleep");
120 simcall_process_sleep(nb_sec);
123 if (e.category == cancel_error) {
124 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
125 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
126 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
127 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
128 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
129 // and did not change anythings at the C level.
130 // 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)
131 status = MSG_TASK_CANCELED;
136 if (TRACE_actor_is_enabled())
137 simgrid::instr::Container::byName(instr_pid(MSG_process_self()))->getState("ACTOR_STATE")->popEvent();
142 /** \ingroup msg_task_usage
143 * \brief Receives a task from a mailbox.
145 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
146 * for receiving tasks asynchronously.
148 * \param task a memory location for storing a #msg_task_t.
149 * \param alias name of the mailbox to receive the task from
152 * #MSG_OK if the task was successfully received,
153 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
155 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
157 return MSG_task_receive_with_timeout(task, alias, -1);
160 /** \ingroup msg_task_usage
161 * \brief Receives a task from a mailbox at a given rate.
163 * \param task a memory location for storing a #msg_task_t.
164 * \param alias name of the mailbox to receive the task from
165 * \param rate limit the reception to rate bandwidth (byte/sec)
167 * The rate parameter can be used to receive a task with a limited
168 * bandwidth (smaller than the physical available value). Use
169 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
170 * rate value do disable this feature).
173 * #MSG_OK if the task was successfully received,
174 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
176 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
178 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
181 /** \ingroup msg_task_usage
182 * \brief Receives a task from a mailbox with a given timeout.
184 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
185 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
186 * to obtain an infinite timeout.
188 * \param task a memory location for storing a #msg_task_t.
189 * \param alias name of the mailbox to receive the task from
190 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
193 * #MSG_OK if the task was successfully received,
194 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
196 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
198 return MSG_task_receive_ext(task, alias, timeout, nullptr);
201 /** \ingroup msg_task_usage
202 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
204 * \param task a memory location for storing a #msg_task_t.
205 * \param alias name of the mailbox to receive the task from
206 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
207 * \param rate limit the reception to rate bandwidth (byte/sec)
209 * The rate parameter can be used to send a task with a limited
210 * bandwidth (smaller than the physical available value). Use
211 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
212 * rate value do disable this feature).
215 * #MSG_OK if the task was successfully received,
216 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
218 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
220 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
223 /** \ingroup msg_task_usage
224 * \brief Receives a task from a mailbox from a specific host with a given timeout.
226 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
227 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
228 * to obtain an infinite timeout.
230 * \param task a memory location for storing a #msg_task_t.
231 * \param alias name of the mailbox to receive the task from
232 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
233 * \param host a #msg_host_t host from where the task was sent
236 * #MSG_OK if the task was successfully received,
237 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
239 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
241 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
242 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
245 /** \ingroup msg_task_usage
246 * \brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
248 * \param task a memory location for storing a #msg_task_t.
249 * \param alias name of the mailbox to receive the task from
250 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
251 * \param host a #msg_host_t host from where the task was sent
252 * \param rate limit the reception to rate bandwidth (byte/sec)
254 * The rate parameter can be used to receive a task with a limited
255 * bandwidth (smaller than the physical available value). Use
256 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
257 * rate value do disable this feature).
260 * #MSG_OK if the task was successfully received,
261 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
263 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
266 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
267 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
268 msg_error_t ret = MSG_OK;
269 /* We no longer support getting a task from a specific host */
273 TRACE_msg_task_get_start();
276 xbt_assert(task, "Null pointer for the task storage");
279 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
281 /* Try to receive it by calling SIMIX network layer */
283 simcall_comm_recv(MSG_process_self()->getImpl(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
284 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->getCname());
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::byName(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->getImpl(), mailbox->getImpl(), 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_task_isend_internal(task, alias, cleanup, 1);
396 /** \ingroup msg_task_usage
397 * \brief Sends a task on a mailbox with a maximal rate.
399 * This is a non blocking detached send function.
400 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
401 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
402 * usual. More details on this can be obtained on
403 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
404 * in the SimGrid-user mailing list archive.
406 * The rate parameter can be used to send a task with a limited
407 * bandwidth (smaller than the physical available value). Use
408 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
409 * value do disable this feature).
411 * \param task a #msg_task_t to send on another location.
412 * \param alias name of the mailbox to sent the task to
413 * \param cleanup a function to destroy the task if the
414 * communication fails, e.g. MSG_task_destroy
415 * (if nullptr, no function will be called)
416 * \param maxrate the maximum communication rate for sending this task (byte/sec)
419 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
421 task->simdata->rate = maxrate;
422 MSG_task_dsend(task, alias, cleanup);
425 /** \ingroup msg_task_usage
426 * \brief Starts listening for receiving a task from an asynchronous communication.
428 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
430 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
431 * \param name of the mailbox to receive the task on
432 * \return the msg_comm_t communication created
434 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
436 return MSG_task_irecv_bounded(task, name, -1.0);
439 /** \ingroup msg_task_usage
440 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
442 * The rate parameter can be used to receive a task with a limited
443 * bandwidth (smaller than the physical available value). Use
444 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
445 * value do disable this feature).
447 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
448 * \param name of the mailbox to receive the task on
449 * \param rate limit the bandwidth to the given rate (byte/sec)
450 * \return the msg_comm_t communication created
452 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
454 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
456 /* FIXME: these functions are not traceable */
458 xbt_assert(task, "Null pointer for the task storage");
461 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
463 /* Try to receive it by calling SIMIX network layer */
465 new simgrid::msg::Comm(nullptr, task, simcall_comm_irecv(SIMIX_process_self(), mbox->getImpl(), task, nullptr,
466 nullptr, nullptr, nullptr, rate));
471 /** \ingroup msg_task_usage
472 * \brief Checks whether a communication is done, and if yes, finalizes it.
473 * \param comm the communication to test
474 * \return 'true' if the communication is finished
475 * (but it may have failed, use MSG_comm_get_status() to know its status)
476 * or 'false' if the communication is not finished yet
477 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
479 int MSG_comm_test(msg_comm_t comm)
481 bool finished = false;
484 finished = simcall_comm_test(comm->s_comm);
485 if (finished && comm->task_received != nullptr) {
486 /* I am the receiver */
487 (*comm->task_received)->simdata->setNotUsed();
491 switch (e.category) {
493 comm->status = MSG_TRANSFER_FAILURE;
497 comm->status = MSG_TIMEOUT;
508 /** \ingroup msg_task_usage
509 * \brief This function checks if a communication is finished.
510 * \param comms a vector of communications
511 * \return the position of the finished communication if any
512 * (but it may have failed, use MSG_comm_get_status() to know its status),
513 * or -1 if none is finished
515 int MSG_comm_testany(xbt_dynar_t comms)
517 int finished_index = -1;
519 /* Create the equivalent array with SIMIX objects: */
520 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
521 s_comms.reserve(xbt_dynar_length(comms));
524 xbt_dynar_foreach(comms, cursor, comm) {
525 s_comms.push_back(comm->s_comm);
528 msg_error_t status = MSG_OK;
530 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
533 switch (e.category) {
535 finished_index = e.value;
536 status = MSG_TRANSFER_FAILURE;
539 finished_index = e.value;
540 status = MSG_TIMEOUT;
547 if (finished_index != -1) {
548 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
549 /* the communication is finished */
550 comm->status = status;
552 if (status == MSG_OK && comm->task_received != nullptr) {
553 /* I am the receiver */
554 (*comm->task_received)->simdata->setNotUsed();
558 return finished_index;
561 /** \ingroup msg_task_usage
562 * \brief Destroys a communication.
563 * \param comm the communication to destroy.
565 void MSG_comm_destroy(msg_comm_t comm)
570 /** \ingroup msg_task_usage
571 * \brief Wait for the completion of a communication.
573 * It takes two parameters.
574 * \param comm the communication to wait.
575 * \param timeout Wait until the communication terminates or the timeout occurs.
576 * You can provide a -1 timeout to obtain an infinite timeout.
577 * \return msg_error_t
579 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
582 simcall_comm_wait(comm->s_comm, timeout);
584 if (comm->task_received != nullptr) {
585 /* I am the receiver */
586 (*comm->task_received)->simdata->setNotUsed();
589 /* FIXME: these functions are not traceable */
592 switch (e.category) {
594 comm->status = MSG_TRANSFER_FAILURE;
597 comm->status = MSG_TIMEOUT;
607 /** \ingroup msg_task_usage
608 * \brief This function is called by a sender and permit to wait for each communication
610 * \param comm a vector of communication
611 * \param nb_elem is the size of the comm vector
612 * \param timeout for each call of MSG_comm_wait
614 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
616 for (int i = 0; i < nb_elem; i++)
617 MSG_comm_wait(comm[i], timeout);
620 /** \ingroup msg_task_usage
621 * \brief This function waits for the first communication finished in a list.
622 * \param comms a vector of communications
623 * \return the position of the first finished communication
624 * (but it may have failed, use MSG_comm_get_status() to know its status)
626 int MSG_comm_waitany(xbt_dynar_t comms)
628 int finished_index = -1;
630 /* create the equivalent dynar with SIMIX objects */
631 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
632 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
636 xbt_dynar_foreach(comms, cursor, comm) {
637 intrusive_ptr_add_ref(comm->s_comm.get());
638 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
641 msg_error_t status = MSG_OK;
643 finished_index = simcall_comm_waitany(s_comms, -1);
646 switch (e.category) {
648 finished_index = e.value;
649 status = MSG_TRANSFER_FAILURE;
652 finished_index = e.value;
653 status = MSG_TIMEOUT;
660 xbt_assert(finished_index != -1, "WaitAny returned -1");
661 xbt_dynar_free(&s_comms);
663 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
664 /* the communication is finished */
665 comm->status = status;
667 if (comm->task_received != nullptr) {
668 /* I am the receiver */
669 (*comm->task_received)->simdata->setNotUsed();
672 return finished_index;
676 * \ingroup msg_task_usage
677 * \brief Returns the error (if any) that occurred during a finished communication.
678 * \param comm a finished communication
679 * \return the status of the communication, or #MSG_OK if no error occurred
680 * during the communication
682 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
687 /** \ingroup msg_task_usage
688 * \brief Get a task (#msg_task_t) from a communication
690 * \param comm the communication where to get the task
691 * \return the task from the communication
693 msg_task_t MSG_comm_get_task(msg_comm_t comm)
695 xbt_assert(comm, "Invalid parameter");
697 return comm->task_received ? *comm->task_received : comm->task_sent;
701 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
702 * \param synchro the comm
703 * \param buff the data copied
704 * \param buff_size size of the buffer
706 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
708 simgrid::kernel::activity::CommImplPtr comm =
709 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
711 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
713 // notify the user callback if any
714 if (msg_global->task_copy_callback) {
715 msg_task_t task = static_cast<msg_task_t>(buff);
716 msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
720 /** \ingroup msg_task_usage
721 * \brief Sends a task to a mailbox
723 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
724 * side if #MSG_task_receive is used).
725 * See #MSG_task_isend for sending tasks asynchronously.
727 * \param task the task to be sent
728 * \param alias the mailbox name to where the task is sent
730 * \return Returns #MSG_OK if the task was successfully sent,
731 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
733 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
735 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
736 return MSG_task_send_with_timeout(task, alias, -1);
739 /** \ingroup msg_task_usage
740 * \brief Sends a task to a mailbox with a maximum rate
742 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
743 * the application to limit the bandwidth utilization of network links when sending the task.
745 * The maxrate parameter can be used to send a task with a limited
746 * bandwidth (smaller than the physical available value). Use
747 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
748 * value do disable this feature).
750 * \param task the task to be sent
751 * \param alias the mailbox name to where the task is sent
752 * \param maxrate the maximum communication rate for sending this task (byte/sec)
754 * \return Returns #MSG_OK if the task was successfully sent,
755 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
757 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
759 task->simdata->rate = maxrate;
760 return MSG_task_send(task, alias);
763 /** \ingroup msg_task_usage
764 * \brief Sends a task to a mailbox with a timeout
766 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
768 * \param task the task to be sent
769 * \param alias the mailbox name to where the task is sent
770 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
772 * \return Returns #MSG_OK if the task was successfully sent,
773 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
775 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
777 msg_error_t ret = MSG_OK;
778 simdata_task_t t_simdata = nullptr;
779 msg_process_t process = MSG_process_self();
780 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
782 TRACE_msg_task_put_start(task);
784 /* Prepare the task to send */
785 t_simdata = task->simdata;
786 t_simdata->sender = process;
787 t_simdata->source = MSG_host_self();
789 t_simdata->setUsed();
791 t_simdata->comm = nullptr;
792 msg_global->sent_msg++;
794 /* Try to send it by calling SIMIX network layer */
796 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
797 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate, task,
798 sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
799 if (TRACE_is_enabled())
800 simcall_set_category(comm, task->category);
801 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
802 simcall_comm_wait(comm, timeout);
805 switch (e.category) {
807 ret = MSG_HOST_FAILURE;
810 ret = MSG_TRANSFER_FAILURE;
819 /* If the send failed, it is not used anymore */
820 t_simdata->setNotUsed();
823 TRACE_msg_task_put_end();
827 /** \ingroup msg_task_usage
828 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
830 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
832 * The maxrate parameter can be used to send a task with a limited
833 * bandwidth (smaller than the physical available value). Use
834 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
835 * value do disable this feature).
837 * \param task the task to be sent
838 * \param alias the mailbox name to where the task is sent
839 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
840 * \param maxrate the maximum communication rate for sending this task (byte/sec)
842 * \return Returns #MSG_OK if the task was successfully sent,
843 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
845 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
847 task->simdata->rate = maxrate;
848 return MSG_task_send_with_timeout(task, alias, timeout);
851 /** \ingroup msg_task_usage
852 * \brief Check if there is a communication going on in a mailbox.
854 * \param alias the name of the mailbox to be considered
856 * \return Returns 1 if there is a communication, 0 otherwise
858 int MSG_task_listen(const char *alias)
860 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
861 return mbox->listen() ? 1 : 0;
864 /** \ingroup msg_task_usage
865 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
867 * \param alias the name of the mailbox to be considered
869 * \return Returns the PID of sender process,
870 * -1 if there is no communication in the mailbox.
872 int MSG_task_listen_from(const char *alias)
874 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
875 simgrid::kernel::activity::CommImplPtr comm =
876 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
881 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
884 /** \ingroup msg_task_usage
885 * \brief Sets the tracing category of a task.
887 * This function should be called after the creation of a MSG task, to define the category of that task. The
888 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
889 * parameter category must contain a category that was previously declared with the function #TRACE_category
890 * (or with #TRACE_category_with_color).
892 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
894 * \param task the task that is going to be categorized
895 * \param category the name of the category to be associated to the task
897 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
899 void MSG_task_set_category (msg_task_t task, const char *category)
901 TRACE_msg_set_task_category (task, category);
904 /** \ingroup msg_task_usage
906 * \brief Gets the current tracing category of a task.
908 * \param task the task to be considered
910 * \see MSG_task_set_category
912 * \return Returns the name of the tracing category of the given task, nullptr otherwise
914 const char *MSG_task_get_category (msg_task_t task)
916 return task->category;