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/Mailbox.hpp"
10 #include "src/instr/instr_private.hpp"
11 #include "src/kernel/activity/ExecImpl.hpp"
12 #include "src/msg/msg_private.hpp"
13 #include "src/simix/smx_private.hpp" /* MSG_task_listen looks inside the rdv directly. Not clean. */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
18 * @brief Executes a task and waits for its termination.
20 * This function is used for describing the behavior of a process. It takes only one parameter.
21 * @param task a #msg_task_t to execute on the location on which the process is running.
22 * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
24 msg_error_t MSG_task_execute(msg_task_t task)
26 return MSG_parallel_task_execute(task);
30 * @brief Executes a parallel task and waits for its termination.
32 * @param task a #msg_task_t to execute on the location on which the process is running.
34 * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
35 * or #MSG_HOST_FAILURE otherwise
37 msg_error_t MSG_parallel_task_execute(msg_task_t task)
39 return MSG_parallel_task_execute_with_timeout(task, -1);
42 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
44 simdata_task_t simdata = task->simdata;
45 e_smx_state_t comp_state;
46 msg_error_t status = MSG_OK;
48 TRACE_msg_task_execute_start(task);
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) {
56 TRACE_msg_task_execute_end(task);
63 if (simdata->host_nb > 0) {
65 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
66 task->name ?: "", simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
67 simdata->bytes_parallel_amount, -1.0, timeout));
68 XBT_DEBUG("Parallel execution action created: %p", simdata->compute.get());
69 if (task->category != nullptr)
70 simgrid::simix::simcall([task] { task->simdata->compute->set_category(task->category); });
72 sg_host_t host = MSG_process_get_host(MSG_process_self());
73 simdata->compute = simgrid::simix::simcall([task, host] {
74 return simgrid::kernel::activity::ExecImplPtr(
75 new simgrid::kernel::activity::ExecImpl(task->name ?: "", task->category ?: "",
76 /*timeout_detector*/ nullptr, 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->setNotUsed();
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) {
95 if (e.category == cancel_error)
96 status = MSG_TASK_CANCELED;
101 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
102 simdata->flops_amount = 0.0;
103 simdata->comm = nullptr;
104 simdata->compute = nullptr;
105 TRACE_msg_task_execute_end(task);
111 * @brief Receives a task from a mailbox.
113 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
114 * for receiving tasks asynchronously.
116 * @param task a memory location for storing a #msg_task_t.
117 * @param alias name of the mailbox to receive the task from
120 * #MSG_OK if the task was successfully received,
121 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
123 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
125 return MSG_task_receive_with_timeout(task, alias, -1);
129 * @brief Receives a task from a mailbox at a given rate.
131 * @param task a memory location for storing a #msg_task_t.
132 * @param alias name of the mailbox to receive the task from
133 * @param rate limit the reception to rate bandwidth (byte/sec)
135 * The rate parameter can be used to receive a task with a limited
136 * bandwidth (smaller than the physical available value). Use
137 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
138 * rate value do disable this feature).
141 * #MSG_OK if the task was successfully received,
142 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
144 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
146 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
150 * @brief Receives a task from a mailbox with a given timeout.
152 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
153 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
154 * to obtain an infinite timeout.
156 * @param task a memory location for storing a #msg_task_t.
157 * @param alias name of the mailbox to receive the task from
158 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
161 * #MSG_OK if the task was successfully received,
162 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
164 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
166 return MSG_task_receive_ext(task, alias, timeout, nullptr);
170 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
172 * @param task a memory location for storing a #msg_task_t.
173 * @param alias name of the mailbox to receive the task from
174 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
175 * @param rate limit the reception to rate bandwidth (byte/sec)
177 * The rate parameter can be used to send a task with a limited
178 * bandwidth (smaller than the physical available value). Use
179 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
180 * rate value do disable this feature).
183 * #MSG_OK if the task was successfully received,
184 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
186 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
188 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
192 * @brief Receives a task from a mailbox from a specific host with a given timeout.
194 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
195 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
196 * to obtain an infinite timeout.
198 * @param task a memory location for storing a #msg_task_t.
199 * @param alias name of the mailbox to receive the task from
200 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
201 * @param host a #msg_host_t host from where the task was sent
204 * #MSG_OK if the task was successfully received,
205 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
207 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
209 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
210 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
214 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
216 * @param task a memory location for storing a #msg_task_t.
217 * @param alias name of the mailbox to receive the task from
218 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
219 * @param host a #msg_host_t host from where the task was sent
220 * @param rate limit the reception to rate bandwidth (byte/sec)
222 * The rate parameter can be used to receive a task with a limited
223 * bandwidth (smaller than the physical available value). Use
224 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
225 * rate value do disable this feature).
228 * #MSG_OK if the task was successfully received,
229 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
231 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
234 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
235 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
236 msg_error_t ret = MSG_OK;
237 /* We no longer support getting a task from a specific host */
241 TRACE_msg_task_get_start();
244 xbt_assert(task, "Null pointer for the task storage");
247 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
249 /* Try to receive it by calling SIMIX network layer */
251 simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
253 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
254 (*task)->simdata->setNotUsed();
255 } catch (simgrid::HostFailureException& e) {
256 ret = MSG_HOST_FAILURE;
257 } catch (simgrid::TimeoutError& e) {
259 } catch (xbt_ex& e) {
260 switch (e.category) {
262 ret = MSG_HOST_FAILURE;
265 ret = MSG_TRANSFER_FAILURE;
272 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
273 TRACE_msg_task_get_end(*task);
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,
280 void_f_pvoid_t cleanup, int detached)
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->setUsed();
292 t_simdata->comm = nullptr;
293 msg_global->sent_msg++;
295 /* Send it by calling SIMIX network layer */
297 simcall_comm_isend(myself->get_impl(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
298 sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
299 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
301 msg_comm_t comm = nullptr;
303 comm = new simgrid::msg::Comm(task, nullptr, act);
306 if (TRACE_is_enabled() && task->category != nullptr)
307 simgrid::simix::simcall([act, task] { act->set_category(task->category); });
309 TRACE_msg_task_put_end();
315 * @brief Sends a task on a mailbox.
317 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
319 * @param task a #msg_task_t to send on another location.
320 * @param alias name of the mailbox to sent the task to
321 * @return the msg_comm_t communication created
323 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
325 return MSG_task_isend_internal(task, alias, nullptr, 0);
329 * @brief Sends a task on a mailbox with a maximum rate
331 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
332 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
334 * @param task a #msg_task_t to send on another location.
335 * @param alias name of the mailbox to sent the task to
336 * @param maxrate the maximum communication rate for sending this task (byte/sec).
337 * @return the msg_comm_t communication created
339 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
341 task->simdata->rate = maxrate;
342 return MSG_task_isend_internal(task, alias, nullptr, 0);
346 * @brief Sends a task on a mailbox.
348 * This is a non blocking detached send function.
349 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
350 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
351 * usual. More details on this can be obtained on
352 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
353 * in the SimGrid-user mailing list archive.
355 * @param task a #msg_task_t to send on another location.
356 * @param alias name of the mailbox to sent the task to
357 * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
358 * (if nullptr, no function will be called)
360 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
362 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
363 xbt_assert(comm == nullptr);
367 * @brief Sends a task on a mailbox with a maximal rate.
369 * This is a non blocking detached send function.
370 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
371 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
372 * usual. More details on this can be obtained on
373 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
374 * in the SimGrid-user mailing list archive.
376 * The rate parameter can be used to send a task with a limited
377 * bandwidth (smaller than the physical available value). Use
378 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
379 * value do disable this feature).
381 * @param task a #msg_task_t to send on another location.
382 * @param alias name of the mailbox to sent the task to
383 * @param cleanup a function to destroy the task if the
384 * communication fails, e.g. MSG_task_destroy
385 * (if nullptr, no function will be called)
386 * @param maxrate the maximum communication rate for sending this task (byte/sec)
389 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
391 task->simdata->rate = maxrate;
392 MSG_task_dsend(task, alias, cleanup);
396 * @brief Starts listening for receiving a task from an asynchronous communication.
398 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
400 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
401 * @param name of the mailbox to receive the task on
402 * @return the msg_comm_t communication created
404 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
406 return MSG_task_irecv_bounded(task, name, -1.0);
410 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
412 * The rate parameter can be used to receive a task with a limited
413 * bandwidth (smaller than the physical available value). Use
414 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
415 * value do disable this feature).
417 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
418 * @param name of the mailbox to receive the task on
419 * @param rate limit the bandwidth to the given rate (byte/sec)
420 * @return the msg_comm_t communication created
422 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
424 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
426 /* FIXME: these functions are not traceable */
428 xbt_assert(task, "Null pointer for the task storage");
431 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
433 /* Try to receive it by calling SIMIX network layer */
434 msg_comm_t comm = new simgrid::msg::Comm(
436 simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr, rate));
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 = simcall_comm_test(comm->s_comm);
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;
464 if (e.category == network_error) {
465 comm->status = MSG_TRANSFER_FAILURE;
476 * @brief This function checks if a communication is finished.
477 * @param comms a vector of communications
478 * @return the position of the finished communication if any
479 * (but it may have failed, use MSG_comm_get_status() to know its status),
480 * or -1 if none is finished
482 int MSG_comm_testany(xbt_dynar_t comms)
484 int finished_index = -1;
486 /* Create the equivalent array with SIMIX objects: */
487 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
488 s_comms.reserve(xbt_dynar_length(comms));
491 xbt_dynar_foreach(comms, cursor, comm) {
492 s_comms.push_back(comm->s_comm);
495 msg_error_t status = MSG_OK;
497 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
498 } catch (simgrid::TimeoutError& e) {
499 finished_index = e.value;
500 status = MSG_TIMEOUT;
503 if (e.category != network_error)
505 finished_index = e.value;
506 status = MSG_TRANSFER_FAILURE;
509 if (finished_index != -1) {
510 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
511 /* the communication is finished */
512 comm->status = status;
514 if (status == MSG_OK && comm->task_received != nullptr) {
515 /* I am the receiver */
516 (*comm->task_received)->simdata->setNotUsed();
520 return finished_index;
523 /** @brief Destroys the provided communication. */
524 void MSG_comm_destroy(msg_comm_t comm)
529 /** @brief Wait for the completion of a communication.
531 * It takes two parameters.
532 * @param comm the communication to wait.
533 * @param timeout Wait until the communication terminates or the timeout occurs.
534 * You can provide a -1 timeout to obtain an infinite timeout.
535 * @return msg_error_t
537 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
540 simcall_comm_wait(comm->s_comm, timeout);
542 if (comm->task_received != nullptr) {
543 /* I am the receiver */
544 (*comm->task_received)->simdata->setNotUsed();
547 /* FIXME: these functions are not traceable */
548 } catch (simgrid::TimeoutError& e) {
549 comm->status = MSG_TIMEOUT;
552 if (e.category == network_error)
553 comm->status = MSG_TRANSFER_FAILURE;
561 /** @brief This function is called by a sender and permit to wait for each communication
563 * @param comm a vector of communication
564 * @param nb_elem is the size of the comm vector
565 * @param timeout for each call of MSG_comm_wait
567 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
569 for (int i = 0; i < nb_elem; i++)
570 MSG_comm_wait(comm[i], timeout);
573 /** @brief This function waits for the first communication finished in a list.
574 * @param comms a vector of communications
575 * @return the position of the first finished communication
576 * (but it may have failed, use MSG_comm_get_status() to know its status)
578 int MSG_comm_waitany(xbt_dynar_t comms)
580 int finished_index = -1;
582 /* create the equivalent dynar with SIMIX objects */
583 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
584 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
588 xbt_dynar_foreach(comms, cursor, comm) {
589 intrusive_ptr_add_ref(comm->s_comm.get());
590 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
593 msg_error_t status = MSG_OK;
595 finished_index = simcall_comm_waitany(s_comms, -1);
596 } catch (simgrid::TimeoutError& e) {
597 finished_index = e.value;
598 status = MSG_TIMEOUT;
601 if (e.category == network_error) {
602 finished_index = e.value;
603 status = MSG_TRANSFER_FAILURE;
609 xbt_assert(finished_index != -1, "WaitAny returned -1");
610 xbt_dynar_free(&s_comms);
612 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
613 /* the communication is finished */
614 comm->status = status;
616 if (comm->task_received != nullptr) {
617 /* I am the receiver */
618 (*comm->task_received)->simdata->setNotUsed();
621 return finished_index;
625 * @brief Returns the error (if any) that occurred during a finished communication.
626 * @param comm a finished communication
627 * @return the status of the communication, or #MSG_OK if no error occurred
628 * during the communication
630 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
635 /** @brief Get a task (#msg_task_t) from a communication
637 * @param comm the communication where to get the task
638 * @return the task from the communication
640 msg_task_t MSG_comm_get_task(msg_comm_t comm)
642 xbt_assert(comm, "Invalid parameter");
644 return comm->task_received ? *comm->task_received : comm->task_sent;
648 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
649 * @param synchro the comm
650 * @param buff the data copied
651 * @param buff_size size of the buffer
653 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
655 simgrid::kernel::activity::CommImplPtr comm =
656 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
658 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
660 // notify the user callback if any
661 if (msg_global->task_copy_callback) {
662 msg_task_t task = static_cast<msg_task_t>(buff);
663 msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());
668 * @brief Sends a task to a mailbox
670 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
671 * side if #MSG_task_receive is used).
672 * See #MSG_task_isend for sending tasks asynchronously.
674 * @param task the task to be sent
675 * @param alias the mailbox name to where the task is sent
677 * @return Returns #MSG_OK if the task was successfully sent,
678 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
680 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
682 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
683 return MSG_task_send_with_timeout(task, alias, -1);
687 * @brief Sends a task to a mailbox with a maximum rate
689 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
690 * the application to limit the bandwidth utilization of network links when sending the task.
692 * The maxrate parameter can be used to send a task with a limited
693 * bandwidth (smaller than the physical available value). Use
694 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
695 * value do disable this feature).
697 * @param task the task to be sent
698 * @param alias the mailbox name to where the task is sent
699 * @param maxrate the maximum communication rate for sending this task (byte/sec)
701 * @return Returns #MSG_OK if the task was successfully sent,
702 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
704 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
706 task->simdata->rate = maxrate;
707 return MSG_task_send(task, alias);
711 * @brief Sends a task to a mailbox with a timeout
713 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
715 * @param task the task to be sent
716 * @param alias the mailbox name to where the task is sent
717 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
719 * @return Returns #MSG_OK if the task was successfully sent,
720 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
722 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
724 msg_error_t ret = MSG_OK;
725 simdata_task_t t_simdata = nullptr;
726 msg_process_t process = MSG_process_self();
727 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
729 TRACE_msg_task_put_start(task);
731 /* Prepare the task to send */
732 t_simdata = task->simdata;
733 t_simdata->sender = process;
734 t_simdata->source = MSG_host_self();
736 t_simdata->setUsed();
738 t_simdata->comm = nullptr;
739 msg_global->sent_msg++;
741 /* Try to send it by calling SIMIX network layer */
743 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
744 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
745 sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
746 if (TRACE_is_enabled() && task->category != nullptr)
747 simgrid::simix::simcall([comm, task] { comm->set_category(task->category); });
748 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
749 simcall_comm_wait(comm, timeout);
750 } catch (simgrid::TimeoutError& e) {
754 switch (e.category) {
756 ret = MSG_HOST_FAILURE;
759 ret = MSG_TRANSFER_FAILURE;
765 /* If the send failed, it is not used anymore */
766 t_simdata->setNotUsed();
769 TRACE_msg_task_put_end();
774 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
776 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
778 * The maxrate parameter can be used to send a task with a limited
779 * bandwidth (smaller than the physical available value). Use
780 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
781 * value do disable this feature).
783 * @param task the task to be sent
784 * @param alias the mailbox name to where the task is sent
785 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
786 * @param maxrate the maximum communication rate for sending this task (byte/sec)
788 * @return Returns #MSG_OK if the task was successfully sent,
789 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
791 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
793 task->simdata->rate = maxrate;
794 return MSG_task_send_with_timeout(task, alias, timeout);
798 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
800 * @param alias the name of the mailbox to be considered
802 * @return Returns the PID of sender process,
803 * -1 if there is no communication in the mailbox.
805 int MSG_task_listen_from(const char *alias)
807 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
808 simgrid::kernel::activity::CommImplPtr comm =
809 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
814 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff_)->simdata->sender);
818 * @brief Sets the tracing category of a task.
820 * This function should be called after the creation of a MSG task, to define the category of that task. The
821 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
822 * parameter category must contain a category that was previously declared with the function #TRACE_category
823 * (or with #TRACE_category_with_color).
825 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
827 * @param task the task that is going to be categorized
828 * @param category the name of the category to be associated to the task
830 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
832 void MSG_task_set_category (msg_task_t task, const char *category)
834 TRACE_msg_set_task_category (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;