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"
9 #include "simgrid/s4u/Comm.hpp"
10 #include "simgrid/s4u/Mailbox.hpp"
11 #include "src/instr/instr_private.hpp"
12 #include "src/kernel/activity/ExecImpl.hpp"
13 #include "src/msg/msg_private.hpp"
14 #include "src/simix/smx_private.hpp" /* MSG_task_listen looks inside the rdv directly. Not clean. */
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
19 * @brief Executes a task and waits for its termination.
21 * This function is used for describing the behavior of a process. It takes only one parameter.
22 * @param task a #msg_task_t to execute on the location on which the process is running.
23 * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
25 msg_error_t MSG_task_execute(msg_task_t task)
27 return MSG_parallel_task_execute(task);
31 * @brief Executes a parallel task and waits for its termination.
33 * @param task a #msg_task_t to execute on the location on which the process is running.
35 * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
36 * or #MSG_HOST_FAILURE otherwise
38 msg_error_t MSG_parallel_task_execute(msg_task_t task)
40 return MSG_parallel_task_execute_with_timeout(task, -1);
43 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
45 simdata_task_t simdata = task->simdata;
46 e_smx_state_t comp_state;
47 msg_error_t status = MSG_OK;
50 xbt_assert((not simdata->compute) && not task->simdata->isused,
51 "This task is executed somewhere else. Go fix your code!");
53 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
55 if (simdata->flops_amount <= 0.0 && not simdata->host_nb) {
59 if (TRACE_actor_is_enabled())
60 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->push_event("execute");
65 if (simdata->host_nb > 0) {
67 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
68 task->name ?: "", simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
69 simdata->bytes_parallel_amount, -1.0, timeout));
70 XBT_DEBUG("Parallel execution action created: %p", simdata->compute.get());
71 if (task->category != nullptr)
72 simgrid::simix::simcall([task] { task->simdata->compute->set_category(task->category); });
74 sg_host_t host = MSG_process_get_host(MSG_process_self());
75 simdata->compute = simgrid::simix::simcall([task, host] {
76 return simgrid::kernel::activity::ExecImplPtr(
77 new simgrid::kernel::activity::ExecImpl(task->name ?: "", task->category ?: "",
78 /*timeout_detector*/ nullptr, host));
80 /* checking for infinite values */
81 xbt_assert(std::isfinite(simdata->flops_amount), "flops_amount is not finite!");
82 xbt_assert(std::isfinite(simdata->priority), "priority is not finite!");
84 simdata->compute->start(simdata->flops_amount, simdata->priority, simdata->bound);
87 comp_state = simcall_execution_wait(simdata->compute);
89 simdata->setNotUsed();
91 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
92 } catch (simgrid::HostFailureException& e) {
93 status = MSG_HOST_FAILURE;
94 } catch (simgrid::TimeoutError& e) {
96 } catch (simgrid::CancelException& e) {
97 status = MSG_TASK_CANCELED;
100 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
101 simdata->flops_amount = 0.0;
102 simdata->comm = nullptr;
103 simdata->compute = nullptr;
105 if (TRACE_actor_is_enabled())
106 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->pop_event();
112 * @brief Receives a task from a mailbox.
114 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
115 * for receiving tasks asynchronously.
117 * @param task a memory location for storing a #msg_task_t.
118 * @param alias name of the mailbox to receive the task from
121 * #MSG_OK if the task was successfully received,
122 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
124 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
126 return MSG_task_receive_with_timeout(task, alias, -1);
130 * @brief Receives a task from a mailbox at a given rate.
132 * @param task a memory location for storing a #msg_task_t.
133 * @param alias name of the mailbox to receive the task from
134 * @param rate limit the reception to rate bandwidth (byte/sec)
136 * The rate parameter can be used to receive a task with a limited
137 * bandwidth (smaller than the physical available value). Use
138 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
139 * rate value do disable this feature).
142 * #MSG_OK if the task was successfully received,
143 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
145 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
147 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
151 * @brief Receives a task from a mailbox with a given timeout.
153 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
154 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
155 * to obtain an infinite timeout.
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 timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
162 * #MSG_OK if the task was successfully received,
163 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
165 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
167 return MSG_task_receive_ext(task, alias, timeout, nullptr);
171 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
173 * @param task a memory location for storing a #msg_task_t.
174 * @param alias name of the mailbox to receive the task from
175 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
176 * @param rate limit the reception to rate bandwidth (byte/sec)
178 * The rate parameter can be used to send a task with a limited
179 * bandwidth (smaller than the physical available value). Use
180 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
181 * rate value do disable this feature).
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_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
189 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
193 * @brief Receives a task from a mailbox from a specific host with a given timeout.
195 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
196 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
197 * to obtain an infinite timeout.
199 * @param task a memory location for storing a #msg_task_t.
200 * @param alias name of the mailbox to receive the task from
201 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
202 * @param host a #msg_host_t host from where the task was sent
205 * #MSG_OK if the task was successfully received,
206 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
208 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
210 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
211 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
215 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
217 * @param task a memory location for storing a #msg_task_t.
218 * @param alias name of the mailbox to receive the task from
219 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
220 * @param host a #msg_host_t host from where the task was sent
221 * @param rate limit the reception to rate bandwidth (byte/sec)
223 * The rate parameter can be used to receive a task with a limited
224 * bandwidth (smaller than the physical available value). Use
225 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
226 * rate value do disable this feature).
229 * #MSG_OK if the task was successfully received,
230 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
232 msg_error_t MSG_task_receive_ext_bounded(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 msg_error_t ret = MSG_OK;
237 /* We no longer support getting a task from a specific host */
242 xbt_assert(task, "Null pointer for the task storage");
245 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
247 /* Try to receive it by calling SIMIX network layer */
250 simgrid::s4u::Mailbox::by_name(alias)
252 ->set_dst_data(&payload, sizeof(msg_task_t*))
255 *task = static_cast<msg_task_t>(payload);
256 XBT_DEBUG("Got task %s from %s", (*task)->name, alias);
257 (*task)->simdata->setNotUsed();
258 } catch (simgrid::HostFailureException& e) {
259 ret = MSG_HOST_FAILURE;
260 } catch (simgrid::TimeoutError& e) {
262 } catch (simgrid::CancelException& e) {
263 ret = MSG_HOST_FAILURE;
264 } catch (xbt_ex& e) {
265 if (e.category == network_error)
266 ret = MSG_TRANSFER_FAILURE;
271 if (TRACE_actor_is_enabled() && ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
272 container_t process_container = simgrid::instr::Container::by_name(instr_pid(MSG_process_self()));
274 std::string key = std::string("p") + std::to_string((*task)->counter);
275 simgrid::instr::Container::get_root()->get_link("ACTOR_TASK_LINK")->end_event(process_container, "SR", key);
280 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
281 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias, void_f_pvoid_t cleanup,
284 simdata_task_t t_simdata = nullptr;
285 msg_process_t myself = MSG_process_self();
286 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
287 TRACE_msg_task_put_start(task);
289 /* Prepare the task to send */
290 t_simdata = task->simdata;
291 t_simdata->sender = myself;
292 t_simdata->source = MSG_host_self();
293 t_simdata->setUsed();
294 t_simdata->comm = nullptr;
295 msg_global->sent_msg++;
297 simgrid::s4u::CommPtr comm = mailbox->put_init(task, t_simdata->bytes_amount)->set_rate(t_simdata->rate);
298 t_simdata->comm = comm;
300 comm->detach(cleanup);
304 msg_comm_t msg_comm = nullptr;
306 msg_comm = new simgrid::msg::Comm(task, nullptr, comm);
309 if (TRACE_is_enabled() && task->category != nullptr)
310 simgrid::simix::simcall([comm, task] { comm->get_impl()->set_category(task->category); });
316 * @brief Sends a task on a mailbox.
318 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
320 * @param task a #msg_task_t to send on another location.
321 * @param alias name of the mailbox to sent the task to
322 * @return the msg_comm_t communication created
324 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
326 return MSG_task_isend_internal(task, alias, nullptr, false);
330 * @brief Sends a task on a mailbox with a maximum rate
332 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
333 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
335 * @param task a #msg_task_t to send on another location.
336 * @param alias name of the mailbox to sent the task to
337 * @param maxrate the maximum communication rate for sending this task (byte/sec).
338 * @return the msg_comm_t communication created
340 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
342 task->simdata->rate = maxrate;
343 return MSG_task_isend_internal(task, alias, nullptr, false);
347 * @brief Sends a task on a mailbox.
349 * This is a non blocking detached send function.
350 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
351 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
352 * usual. More details on this can be obtained on
353 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
354 * in the SimGrid-user mailing list archive.
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 cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
359 * (if nullptr, no function will be called)
361 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
363 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, true);
364 xbt_assert(comm == nullptr);
368 * @brief Sends a task on a mailbox with a maximal rate.
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 * The rate parameter can be used to send a task with a limited
378 * bandwidth (smaller than the physical available value). Use
379 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
380 * value do disable this feature).
382 * @param task a #msg_task_t to send on another location.
383 * @param alias name of the mailbox to sent the task to
384 * @param cleanup a function to destroy the task if the
385 * communication fails, e.g. MSG_task_destroy
386 * (if nullptr, no function will be called)
387 * @param maxrate the maximum communication rate for sending this task (byte/sec)
390 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
392 task->simdata->rate = maxrate;
393 MSG_task_dsend(task, alias, cleanup);
397 * @brief Starts listening for receiving a task from an asynchronous communication.
399 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
401 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
402 * @param name of the mailbox to receive the task on
403 * @return the msg_comm_t communication created
405 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
407 return MSG_task_irecv_bounded(task, name, -1.0);
411 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
413 * The rate parameter can be used to receive a task with a limited
414 * bandwidth (smaller than the physical available value). Use
415 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
416 * value do disable this feature).
418 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
419 * @param name of the mailbox to receive the task on
420 * @param rate limit the bandwidth to the given rate (byte/sec)
421 * @return the msg_comm_t communication created
423 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
425 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
427 /* FIXME: these functions are not traceable */
429 xbt_assert(task, "Null pointer for the task storage");
432 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
434 /* Try to receive it by calling SIMIX network layer */
435 msg_comm_t comm = new simgrid::msg::Comm(
436 nullptr, task, mbox->get_init()->set_dst_data((void**)task, sizeof(msg_task_t*))->set_rate(rate)->start());
442 * @brief Checks whether a communication is done, and if yes, finalizes it.
443 * @param comm the communication to test
444 * @return 'true' if the communication is finished
445 * (but it may have failed, use MSG_comm_get_status() to know its status)
446 * or 'false' if the communication is not finished yet
447 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
449 int MSG_comm_test(msg_comm_t comm)
451 bool finished = false;
454 finished = comm->s_comm->test();
455 if (finished && comm->task_received != nullptr) {
456 /* I am the receiver */
457 (*comm->task_received)->simdata->setNotUsed();
459 } catch (simgrid::TimeoutError& e) {
460 comm->status = MSG_TIMEOUT;
462 } catch (simgrid::CancelException& e) {
463 comm->status = MSG_TASK_CANCELED;
467 if (e.category == network_error) {
468 comm->status = MSG_TRANSFER_FAILURE;
479 * @brief This function checks if a communication is finished.
480 * @param comms a vector of communications
481 * @return the position of the finished communication if any
482 * (but it may have failed, use MSG_comm_get_status() to know its status),
483 * or -1 if none is finished
485 int MSG_comm_testany(xbt_dynar_t comms)
487 int finished_index = -1;
489 /* Create the equivalent array with SIMIX objects: */
490 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
491 s_comms.reserve(xbt_dynar_length(comms));
494 xbt_dynar_foreach(comms, cursor, comm) {
495 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
498 msg_error_t status = MSG_OK;
500 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
501 } catch (simgrid::TimeoutError& e) {
502 finished_index = e.value;
503 status = MSG_TIMEOUT;
504 } catch (simgrid::CancelException& e) {
505 finished_index = e.value;
506 status = MSG_TASK_CANCELED;
509 if (e.category != network_error)
511 finished_index = e.value;
512 status = MSG_TRANSFER_FAILURE;
515 if (finished_index != -1) {
516 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
517 /* the communication is finished */
518 comm->status = status;
520 if (status == MSG_OK && comm->task_received != nullptr) {
521 /* I am the receiver */
522 (*comm->task_received)->simdata->setNotUsed();
526 return finished_index;
529 /** @brief Destroys the provided communication. */
530 void MSG_comm_destroy(msg_comm_t comm)
535 /** @brief Wait for the completion of a communication.
537 * It takes two parameters.
538 * @param comm the communication to wait.
539 * @param timeout Wait until the communication terminates or the timeout occurs.
540 * You can provide a -1 timeout to obtain an infinite timeout.
541 * @return msg_error_t
543 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
546 comm->s_comm->wait_for(timeout);
548 if (comm->task_received != nullptr) {
549 /* I am the receiver */
550 (*comm->task_received)->simdata->setNotUsed();
553 /* FIXME: these functions are not traceable */
554 } catch (simgrid::TimeoutError& e) {
555 comm->status = MSG_TIMEOUT;
556 } catch (simgrid::CancelException& e) {
557 comm->status = MSG_TASK_CANCELED;
560 if (e.category == network_error)
561 comm->status = MSG_TRANSFER_FAILURE;
569 /** @brief This function is called by a sender and permit to wait for each communication
571 * @param comm a vector of communication
572 * @param nb_elem is the size of the comm vector
573 * @param timeout for each call of MSG_comm_wait
575 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
577 for (int i = 0; i < nb_elem; i++)
578 MSG_comm_wait(comm[i], timeout);
581 /** @brief This function waits for the first communication finished in a list.
582 * @param comms a vector of communications
583 * @return the position of the first finished communication
584 * (but it may have failed, use MSG_comm_get_status() to know its status)
586 int MSG_comm_waitany(xbt_dynar_t comms)
588 int finished_index = -1;
590 /* Create the equivalent array with SIMIX objects: */
591 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
592 s_comms.reserve(xbt_dynar_length(comms));
595 xbt_dynar_foreach(comms, cursor, comm) {
596 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
599 msg_error_t status = MSG_OK;
601 finished_index = simcall_comm_waitany(s_comms.data(), s_comms.size(), -1);
602 } catch (simgrid::TimeoutError& e) {
603 finished_index = e.value;
604 status = MSG_TIMEOUT;
605 } catch (simgrid::CancelException& e) {
606 finished_index = e.value;
607 status = MSG_TASK_CANCELED;
610 if (e.category == network_error) {
611 finished_index = e.value;
612 status = MSG_TRANSFER_FAILURE;
618 xbt_assert(finished_index != -1, "WaitAny returned -1");
620 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
621 /* the communication is finished */
622 comm->status = status;
624 if (comm->task_received != nullptr) {
625 /* I am the receiver */
626 (*comm->task_received)->simdata->setNotUsed();
629 return finished_index;
633 * @brief Returns the error (if any) that occurred during a finished communication.
634 * @param comm a finished communication
635 * @return the status of the communication, or #MSG_OK if no error occurred
636 * during the communication
638 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
643 /** @brief Get a task (#msg_task_t) from a communication
645 * @param comm the communication where to get the task
646 * @return the task from the communication
648 msg_task_t MSG_comm_get_task(msg_comm_t comm)
650 xbt_assert(comm, "Invalid parameter");
652 return comm->task_received ? *comm->task_received : comm->task_sent;
656 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
657 * @param comm the comm
658 * @param buff the data copied
659 * @param buff_size size of the buffer
661 void MSG_comm_copy_data_from_SIMIX(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
663 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
665 // notify the user callback if any
666 if (msg_global->task_copy_callback) {
667 msg_task_t task = static_cast<msg_task_t>(buff);
668 msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());
673 * @brief Sends a task to a mailbox
675 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
676 * side if #MSG_task_receive is used).
677 * See #MSG_task_isend for sending tasks asynchronously.
679 * @param task the task to be sent
680 * @param alias the mailbox name to where the task is sent
682 * @return Returns #MSG_OK if the task was successfully sent,
683 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
685 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
687 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
688 return MSG_task_send_with_timeout(task, alias, -1);
692 * @brief Sends a task to a mailbox with a maximum rate
694 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
695 * the application to limit the bandwidth utilization of network links when sending the task.
697 * The maxrate parameter can be used to send a task with a limited
698 * bandwidth (smaller than the physical available value). Use
699 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
700 * value do disable this feature).
702 * @param task the task to be sent
703 * @param alias the mailbox name to where the task is sent
704 * @param maxrate the maximum communication rate for sending this task (byte/sec)
706 * @return Returns #MSG_OK if the task was successfully sent,
707 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
709 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
711 task->simdata->rate = maxrate;
712 return MSG_task_send(task, alias);
716 * @brief Sends a task to a mailbox with a timeout
718 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
720 * @param task the task to be sent
721 * @param alias the mailbox name to where the task is sent
722 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
724 * @return Returns #MSG_OK if the task was successfully sent,
725 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
727 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
729 msg_error_t ret = MSG_OK;
731 TRACE_msg_task_put_start(task);
733 /* Prepare the task to send */
734 simdata_task_t t_simdata = task->simdata;
735 t_simdata->sender = MSG_process_self();
736 t_simdata->source = MSG_host_self();
737 t_simdata->setUsed();
739 msg_global->sent_msg++;
743 simgrid::s4u::CommPtr comm =
744 simgrid::s4u::Mailbox::by_name(alias)->put_init(task, t_simdata->bytes_amount)->set_rate(t_simdata->rate);
745 t_simdata->comm = comm;
747 if (TRACE_is_enabled() && task->category != nullptr)
748 simgrid::simix::simcall([comm, task] { comm->get_impl()->set_category(task->category); });
749 comm->wait_for(timeout);
750 } catch (simgrid::TimeoutError& e) {
752 } catch (simgrid::CancelException& e) {
753 ret = MSG_HOST_FAILURE;
754 } catch (xbt_ex& e) {
755 if (e.category == network_error)
756 ret = MSG_TRANSFER_FAILURE;
760 /* If the send failed, it is not used anymore */
761 t_simdata->setNotUsed();
768 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
770 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
772 * The maxrate parameter can be used to send a task with a limited
773 * bandwidth (smaller than the physical available value). Use
774 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
775 * value do disable this feature).
777 * @param task the task to be sent
778 * @param alias the mailbox name to where the task is sent
779 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
780 * @param maxrate the maximum communication rate for sending this task (byte/sec)
782 * @return Returns #MSG_OK if the task was successfully sent,
783 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
785 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
787 task->simdata->rate = maxrate;
788 return MSG_task_send_with_timeout(task, alias, timeout);
792 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
794 * @param alias the name of the mailbox to be considered
796 * @return Returns the PID of sender process,
797 * -1 if there is no communication in the mailbox.
799 int MSG_task_listen_from(const char *alias)
801 simgrid::kernel::activity::CommImplPtr comm = simgrid::s4u::Mailbox::by_name(alias)->front();
803 return comm ? MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff_)->simdata->sender) : -1;
807 * @brief Sets the tracing category of a task.
809 * This function should be called after the creation of a MSG task, to define the category of that task. The
810 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
811 * parameter category must contain a category that was previously declared with the function #TRACE_category
812 * (or with #TRACE_category_with_color).
814 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
816 * @param task the task that is going to be categorized
817 * @param category the name of the category to be associated to the task
819 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
821 void MSG_task_set_category (msg_task_t task, const char *category)
823 xbt_assert(task->category == nullptr, "Task %p(%s) already has a category (%s).", task, task->name, task->category);
825 // if user provides a nullptr category, task is no longer traced
826 if (category == nullptr) {
827 xbt_free(task->category);
828 task->category = nullptr;
829 XBT_DEBUG("MSG task %p(%s), category removed", task, task->name);
832 task->category = xbt_strdup(category);
833 XBT_DEBUG("MSG task %p(%s), category %s", task, task->name, task->category);
838 * @brief Gets the current tracing category of a task.
840 * @param task the task to be considered
842 * @see MSG_task_set_category
844 * @return Returns the name of the tracing category of the given task, nullptr otherwise
846 const char *MSG_task_get_category (msg_task_t task)
848 return task->category;