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;
49 xbt_assert((not simdata->compute) && not task->simdata->is_used,
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) {
58 if (TRACE_actor_is_enabled())
59 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->push_event("execute");
64 if (simdata->host_nb > 0) {
66 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
67 task->name ?: "", simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
68 simdata->bytes_parallel_amount, -1.0, timeout));
69 XBT_DEBUG("Parallel execution action created: %p", simdata->compute.get());
70 if (task->category != nullptr)
71 simgrid::simix::simcall([task] { task->simdata->compute->set_category(task->category); });
73 sg_host_t host = MSG_process_get_host(MSG_process_self());
74 simdata->compute = simgrid::simix::simcall([task, host] {
75 return simgrid::kernel::activity::ExecImplPtr(
76 new simgrid::kernel::activity::ExecImpl(task->name ?: "", task->category ?: "", host));
78 /* checking for infinite values */
79 xbt_assert(std::isfinite(simdata->flops_amount), "flops_amount is not finite!");
80 xbt_assert(std::isfinite(simdata->priority), "priority is not finite!");
82 simdata->compute->start(simdata->flops_amount, simdata->priority, simdata->bound);
85 comp_state = simcall_execution_wait(simdata->compute);
87 simdata->set_not_used();
89 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
90 } catch (simgrid::HostFailureException& e) {
91 status = MSG_HOST_FAILURE;
92 } catch (simgrid::TimeoutError& e) {
94 } catch (simgrid::CancelException& e) {
95 status = MSG_TASK_CANCELED;
98 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
99 simdata->flops_amount = 0.0;
100 simdata->comm = nullptr;
101 simdata->compute = nullptr;
103 if (TRACE_actor_is_enabled())
104 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->pop_event();
110 * @brief Receives a task from a mailbox.
112 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
113 * for receiving tasks asynchronously.
115 * @param task a memory location for storing a #msg_task_t.
116 * @param alias name of the mailbox to receive the task from
119 * #MSG_OK if the task was successfully received,
120 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
122 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
124 return MSG_task_receive_with_timeout(task, alias, -1);
128 * @brief Receives a task from a mailbox at a given rate.
130 * @param task a memory location for storing a #msg_task_t.
131 * @param alias name of the mailbox to receive the task from
132 * @param rate limit the reception to rate bandwidth (byte/sec)
134 * The rate parameter can be used to receive a task with a limited
135 * bandwidth (smaller than the physical available value). Use
136 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
137 * rate value do disable this feature).
140 * #MSG_OK if the task was successfully received,
141 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
143 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
145 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
149 * @brief Receives a task from a mailbox with a given timeout.
151 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
152 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
153 * to obtain an infinite timeout.
155 * @param task a memory location for storing a #msg_task_t.
156 * @param alias name of the mailbox to receive the task from
157 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
160 * #MSG_OK if the task was successfully received,
161 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
163 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
165 return MSG_task_receive_ext(task, alias, timeout, nullptr);
169 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
171 * @param task a memory location for storing a #msg_task_t.
172 * @param alias name of the mailbox to receive the task from
173 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
174 * @param rate limit the reception to rate bandwidth (byte/sec)
176 * The rate parameter can be used to send a task with a limited
177 * bandwidth (smaller than the physical available value). Use
178 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
179 * rate value do disable this feature).
182 * #MSG_OK if the task was successfully received,
183 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
185 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
187 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
191 * @brief Receives a task from a mailbox from a specific host with a given timeout.
193 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
194 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
195 * to obtain an infinite timeout.
197 * @param task a memory location for storing a #msg_task_t.
198 * @param alias name of the mailbox to receive the task from
199 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
200 * @param host a #msg_host_t host from where the task was sent
203 * #MSG_OK if the task was successfully received,
204 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
206 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
208 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
209 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
213 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
215 * @param task a memory location for storing a #msg_task_t.
216 * @param alias name of the mailbox to receive the task from
217 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
218 * @param host a #msg_host_t host from where the task was sent
219 * @param rate limit the reception to rate bandwidth (byte/sec)
221 * The rate parameter can be used to receive a task with a limited
222 * bandwidth (smaller than the physical available value). Use
223 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
224 * rate value do disable this feature).
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_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
233 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
234 msg_error_t ret = MSG_OK;
235 /* We no longer support getting a task from a specific host */
240 xbt_assert(task, "Null pointer for the task storage");
243 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
245 /* Try to receive it by calling SIMIX network layer */
248 simgrid::s4u::Mailbox::by_name(alias)
250 ->set_dst_data(&payload, sizeof(msg_task_t*))
253 *task = static_cast<msg_task_t>(payload);
254 XBT_DEBUG("Got task %s from %s", (*task)->name, alias);
255 (*task)->simdata->set_not_used();
256 } catch (simgrid::HostFailureException& e) {
257 ret = MSG_HOST_FAILURE;
258 } catch (simgrid::TimeoutError& e) {
260 } catch (simgrid::CancelException& e) {
261 ret = MSG_TASK_CANCELED;
262 } catch (xbt_ex& e) {
263 if (e.category == network_error)
264 ret = MSG_TRANSFER_FAILURE;
269 if (TRACE_actor_is_enabled() && ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
270 container_t process_container = simgrid::instr::Container::by_name(instr_pid(MSG_process_self()));
272 std::string key = std::string("p") + std::to_string((*task)->counter);
273 simgrid::instr::Container::get_root()->get_link("ACTOR_TASK_LINK")->end_event(process_container, "SR", key);
278 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
279 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias, void_f_pvoid_t cleanup,
282 simdata_task_t t_simdata = nullptr;
283 msg_process_t myself = MSG_process_self();
284 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
285 TRACE_msg_task_put_start(task);
287 /* Prepare the task to send */
288 t_simdata = task->simdata;
289 t_simdata->sender = myself;
290 t_simdata->source = MSG_host_self();
291 t_simdata->set_used();
292 t_simdata->comm = nullptr;
293 msg_global->sent_msg++;
295 simgrid::s4u::CommPtr comm = mailbox->put_init(task, t_simdata->bytes_amount)->set_rate(t_simdata->rate);
296 t_simdata->comm = comm;
298 comm->detach(cleanup);
302 msg_comm_t msg_comm = nullptr;
304 msg_comm = new simgrid::msg::Comm(task, nullptr, comm);
307 if (TRACE_is_enabled() && task->category != nullptr)
308 simgrid::simix::simcall([comm, task] { comm->get_impl()->set_category(task->category); });
314 * @brief Sends a task on a mailbox.
316 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
318 * @param task a #msg_task_t to send on another location.
319 * @param alias name of the mailbox to sent the task to
320 * @return the msg_comm_t communication created
322 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
324 return MSG_task_isend_internal(task, alias, nullptr, false);
328 * @brief Sends a task on a mailbox with a maximum rate
330 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
331 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
333 * @param task a #msg_task_t to send on another location.
334 * @param alias name of the mailbox to sent the task to
335 * @param maxrate the maximum communication rate for sending this task (byte/sec).
336 * @return the msg_comm_t communication created
338 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
340 task->simdata->rate = maxrate;
341 return MSG_task_isend_internal(task, alias, nullptr, false);
345 * @brief Sends a task on a mailbox.
347 * This is a non blocking detached send function.
348 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
349 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
350 * usual. More details on this can be obtained on
351 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
352 * in the SimGrid-user mailing list archive.
354 * @param task a #msg_task_t to send on another location.
355 * @param alias name of the mailbox to sent the task to
356 * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
357 * (if nullptr, no function will be called)
359 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
361 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, true);
362 xbt_assert(comm == nullptr);
366 * @brief Sends a task on a mailbox with a maximal rate.
368 * This is a non blocking detached send function.
369 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
370 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
371 * usual. More details on this can be obtained on
372 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
373 * in the SimGrid-user mailing list archive.
375 * The rate parameter can be used to send a task with a limited
376 * bandwidth (smaller than the physical available value). Use
377 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
378 * value do disable this feature).
380 * @param task a #msg_task_t to send on another location.
381 * @param alias name of the mailbox to sent the task to
382 * @param cleanup a function to destroy the task if the
383 * communication fails, e.g. MSG_task_destroy
384 * (if nullptr, no function will be called)
385 * @param maxrate the maximum communication rate for sending this task (byte/sec)
388 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
390 task->simdata->rate = maxrate;
391 MSG_task_dsend(task, alias, cleanup);
395 * @brief Starts listening for receiving a task from an asynchronous communication.
397 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
399 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
400 * @param name of the mailbox to receive the task on
401 * @return the msg_comm_t communication created
403 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
405 return MSG_task_irecv_bounded(task, name, -1.0);
409 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
411 * The rate parameter can be used to receive a task with a limited
412 * bandwidth (smaller than the physical available value). Use
413 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
414 * value do disable this feature).
416 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
417 * @param name of the mailbox to receive the task on
418 * @param rate limit the bandwidth to the given rate (byte/sec)
419 * @return the msg_comm_t communication created
421 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
423 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
425 /* FIXME: these functions are not traceable */
427 xbt_assert(task, "Null pointer for the task storage");
430 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
432 /* Try to receive it by calling SIMIX network layer */
433 msg_comm_t comm = new simgrid::msg::Comm(
434 nullptr, task, mbox->get_init()->set_dst_data((void**)task, sizeof(msg_task_t*))->set_rate(rate)->start());
440 * @brief Checks whether a communication is done, and if yes, finalizes it.
441 * @param comm the communication to test
442 * @return 'true' if the communication is finished
443 * (but it may have failed, use MSG_comm_get_status() to know its status)
444 * or 'false' if the communication is not finished yet
445 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
447 int MSG_comm_test(msg_comm_t comm)
449 bool finished = false;
452 finished = comm->s_comm->test();
453 if (finished && comm->task_received != nullptr) {
454 /* I am the receiver */
455 (*comm->task_received)->simdata->set_not_used();
457 } catch (simgrid::TimeoutError& e) {
458 comm->status = MSG_TIMEOUT;
460 } catch (simgrid::CancelException& e) {
461 comm->status = MSG_TASK_CANCELED;
465 if (e.category == network_error) {
466 comm->status = MSG_TRANSFER_FAILURE;
477 * @brief This function checks if a communication is finished.
478 * @param comms a vector of communications
479 * @return the position of the finished communication if any
480 * (but it may have failed, use MSG_comm_get_status() to know its status),
481 * or -1 if none is finished
483 int MSG_comm_testany(xbt_dynar_t comms)
485 int finished_index = -1;
487 /* Create the equivalent array with SIMIX objects: */
488 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
489 s_comms.reserve(xbt_dynar_length(comms));
492 xbt_dynar_foreach(comms, cursor, comm) {
493 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
496 msg_error_t status = MSG_OK;
498 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
499 } catch (simgrid::TimeoutError& e) {
500 finished_index = e.value;
501 status = MSG_TIMEOUT;
502 } catch (simgrid::CancelException& e) {
503 finished_index = e.value;
504 status = MSG_TASK_CANCELED;
507 if (e.category != network_error)
509 finished_index = e.value;
510 status = MSG_TRANSFER_FAILURE;
513 if (finished_index != -1) {
514 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
515 /* the communication is finished */
516 comm->status = status;
518 if (status == MSG_OK && comm->task_received != nullptr) {
519 /* I am the receiver */
520 (*comm->task_received)->simdata->set_not_used();
524 return finished_index;
527 /** @brief Destroys the provided communication. */
528 void MSG_comm_destroy(msg_comm_t comm)
533 /** @brief Wait for the completion of a communication.
535 * It takes two parameters.
536 * @param comm the communication to wait.
537 * @param timeout Wait until the communication terminates or the timeout occurs.
538 * You can provide a -1 timeout to obtain an infinite timeout.
539 * @return msg_error_t
541 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
544 comm->s_comm->wait_for(timeout);
546 if (comm->task_received != nullptr) {
547 /* I am the receiver */
548 (*comm->task_received)->simdata->set_not_used();
551 /* FIXME: these functions are not traceable */
552 } catch (simgrid::TimeoutError& e) {
553 comm->status = MSG_TIMEOUT;
554 } catch (simgrid::CancelException& e) {
555 comm->status = MSG_TASK_CANCELED;
558 if (e.category == network_error)
559 comm->status = MSG_TRANSFER_FAILURE;
567 /** @brief This function is called by a sender and permit to wait for each communication
569 * @param comm a vector of communication
570 * @param nb_elem is the size of the comm vector
571 * @param timeout for each call of MSG_comm_wait
573 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
575 for (int i = 0; i < nb_elem; i++)
576 MSG_comm_wait(comm[i], timeout);
579 /** @brief This function waits for the first communication finished in a list.
580 * @param comms a vector of communications
581 * @return the position of the first finished communication
582 * (but it may have failed, use MSG_comm_get_status() to know its status)
584 int MSG_comm_waitany(xbt_dynar_t comms)
586 int finished_index = -1;
588 /* Create the equivalent array with SIMIX objects: */
589 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
590 s_comms.reserve(xbt_dynar_length(comms));
593 xbt_dynar_foreach(comms, cursor, comm) {
594 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
597 msg_error_t status = MSG_OK;
599 finished_index = simcall_comm_waitany(s_comms.data(), s_comms.size(), -1);
600 } catch (simgrid::TimeoutError& e) {
601 finished_index = e.value;
602 status = MSG_TIMEOUT;
603 } catch (simgrid::CancelException& e) {
604 finished_index = e.value;
605 status = MSG_TASK_CANCELED;
608 if (e.category == network_error) {
609 finished_index = e.value;
610 status = MSG_TRANSFER_FAILURE;
616 xbt_assert(finished_index != -1, "WaitAny returned -1");
618 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
619 /* the communication is finished */
620 comm->status = status;
622 if (comm->task_received != nullptr) {
623 /* I am the receiver */
624 (*comm->task_received)->simdata->set_not_used();
627 return finished_index;
631 * @brief Returns the error (if any) that occurred during a finished communication.
632 * @param comm a finished communication
633 * @return the status of the communication, or #MSG_OK if no error occurred
634 * during the communication
636 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
641 /** @brief Get a task (#msg_task_t) from a communication
643 * @param comm the communication where to get the task
644 * @return the task from the communication
646 msg_task_t MSG_comm_get_task(msg_comm_t comm)
648 xbt_assert(comm, "Invalid parameter");
650 return comm->task_received ? *comm->task_received : comm->task_sent;
654 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
655 * @param comm the comm
656 * @param buff the data copied
657 * @param buff_size size of the buffer
659 void MSG_comm_copy_data_from_SIMIX(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
661 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
663 // notify the user callback if any
664 if (msg_global->task_copy_callback) {
665 msg_task_t task = static_cast<msg_task_t>(buff);
666 msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());
671 * @brief Sends a task to a mailbox
673 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
674 * side if #MSG_task_receive is used).
675 * See #MSG_task_isend for sending tasks asynchronously.
677 * @param task the task to be sent
678 * @param alias the mailbox name to where the task is sent
680 * @return Returns #MSG_OK if the task was successfully sent,
681 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
683 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
685 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
686 return MSG_task_send_with_timeout(task, alias, -1);
690 * @brief Sends a task to a mailbox with a maximum rate
692 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
693 * the application to limit the bandwidth utilization of network links when sending the task.
695 * The maxrate parameter can be used to send a task with a limited
696 * bandwidth (smaller than the physical available value). Use
697 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
698 * value do disable this feature).
700 * @param task the task to be sent
701 * @param alias the mailbox name to where the task is sent
702 * @param maxrate the maximum communication rate for sending this task (byte/sec)
704 * @return Returns #MSG_OK if the task was successfully sent,
705 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
707 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
709 task->simdata->rate = maxrate;
710 return MSG_task_send(task, alias);
714 * @brief Sends a task to a mailbox with a timeout
716 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
718 * @param task the task to be sent
719 * @param alias the mailbox name to where the task is sent
720 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
722 * @return Returns #MSG_OK if the task was successfully sent,
723 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
725 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
727 msg_error_t ret = MSG_OK;
729 TRACE_msg_task_put_start(task);
731 /* Prepare the task to send */
732 simdata_task_t t_simdata = task->simdata;
733 t_simdata->sender = MSG_process_self();
734 t_simdata->source = MSG_host_self();
735 t_simdata->set_used();
737 msg_global->sent_msg++;
741 simgrid::s4u::CommPtr comm =
742 simgrid::s4u::Mailbox::by_name(alias)->put_init(task, t_simdata->bytes_amount)->set_rate(t_simdata->rate);
743 t_simdata->comm = comm;
745 if (TRACE_is_enabled() && task->category != nullptr)
746 simgrid::simix::simcall([comm, task] { comm->get_impl()->set_category(task->category); });
747 comm->wait_for(timeout);
748 } catch (simgrid::TimeoutError& e) {
750 } catch (simgrid::CancelException& e) {
751 ret = MSG_HOST_FAILURE;
752 } catch (xbt_ex& e) {
753 if (e.category == network_error)
754 ret = MSG_TRANSFER_FAILURE;
758 /* If the send failed, it is not used anymore */
759 t_simdata->set_not_used();
766 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
768 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
770 * The maxrate parameter can be used to send a task with a limited
771 * bandwidth (smaller than the physical available value). Use
772 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
773 * value do disable this feature).
775 * @param task the task to be sent
776 * @param alias the mailbox name to where the task is sent
777 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
778 * @param maxrate the maximum communication rate for sending this task (byte/sec)
780 * @return Returns #MSG_OK if the task was successfully sent,
781 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
783 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
785 task->simdata->rate = maxrate;
786 return MSG_task_send_with_timeout(task, alias, timeout);
790 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
792 * @param alias the name of the mailbox to be considered
794 * @return Returns the PID of sender process,
795 * -1 if there is no communication in the mailbox.
797 int MSG_task_listen_from(const char *alias)
799 simgrid::kernel::activity::CommImplPtr comm = simgrid::s4u::Mailbox::by_name(alias)->front();
801 return comm ? MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff_)->simdata->sender) : -1;
805 * @brief Sets the tracing category of a task.
807 * This function should be called after the creation of a MSG task, to define the category of that task. The
808 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
809 * parameter category must contain a category that was previously declared with the function #TRACE_category
810 * (or with #TRACE_category_with_color).
812 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
814 * @param task the task that is going to be categorized
815 * @param category the name of the category to be associated to the task
817 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
819 void MSG_task_set_category (msg_task_t task, const char *category)
821 xbt_assert(task->category == nullptr, "Task %p(%s) already has a category (%s).", task, task->name, task->category);
823 // if user provides a nullptr category, task is no longer traced
824 if (category == nullptr) {
825 xbt_free(task->category);
826 task->category = nullptr;
827 XBT_DEBUG("MSG task %p(%s), category removed", task, task->name);
830 task->category = xbt_strdup(category);
831 XBT_DEBUG("MSG task %p(%s), category %s", task, task->name, task->category);
836 * @brief Gets the current tracing category of a task.
838 * @param task the task to be considered
840 * @see MSG_task_set_category
842 * @return Returns the name of the tracing category of the given task, nullptr otherwise
844 const char *MSG_task_get_category (msg_task_t task)
846 return task->category;