1 /* Copyright (c) 2004-2016. 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/simix/smx_private.h" /* MSG_task_listen looks inside the rdv directly. Not clean. */
9 #include "msg_private.h"
12 #include "xbt/sysdep.h"
14 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 /* TODO: add this to other locations */
26 msg_host_t host = MSG_process_get_host(MSG_process_self());
27 MSG_host_add_task(host, task);
29 msg_error_t ret = MSG_parallel_task_execute(task);
31 MSG_host_del_task(host, task);
35 /** \ingroup msg_task_usage
36 * \brief Executes a parallel task and waits for its termination.
38 * \param task a #msg_task_t to execute on the location on which the process is running.
40 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
41 * or #MSG_HOST_FAILURE otherwise
43 msg_error_t MSG_parallel_task_execute(msg_task_t task)
45 return MSG_parallel_task_execute_with_timeout(task, -1);
48 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
50 simdata_task_t simdata = task->simdata;
51 simdata_process_t p_simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
52 e_smx_state_t comp_state;
53 msg_error_t status = MSG_OK;
55 TRACE_msg_task_execute_start(task);
57 xbt_assert((!simdata->compute) && !task->simdata->isused, "This task is executed somewhere else. Go fix your code!");
59 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
61 if (simdata->flops_amount <= 0.0 && !simdata->host_nb) {
62 TRACE_msg_task_execute_end(task);
69 if (simdata->host_nb > 0) {
70 simdata->compute = static_cast<simgrid::kernel::activity::Exec*>(simcall_execution_parallel_start(
71 task->name, simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
72 simdata->bytes_parallel_amount, 1.0, -1.0, timeout));
73 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
75 simdata->compute = static_cast<simgrid::kernel::activity::Exec*>(
76 simcall_execution_start(task->name, simdata->flops_amount, simdata->priority, simdata->bound));
78 simcall_set_category(simdata->compute, task->category);
79 p_simdata->waiting_action = simdata->compute;
80 comp_state = simcall_execution_wait(simdata->compute);
82 p_simdata->waiting_action = nullptr;
83 simdata->setNotUsed();
85 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
90 status = MSG_TASK_CANCELED;
93 status = MSG_HOST_FAILURE;
103 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
104 simdata->flops_amount = 0.0;
105 simdata->comm = nullptr;
106 simdata->compute = nullptr;
107 TRACE_msg_task_execute_end(task);
112 /** \ingroup msg_task_usage
113 * \brief Sleep for the specified number of seconds
115 * Makes the current process sleep until \a time seconds have elapsed.
117 * \param nb_sec a number of second
119 msg_error_t MSG_process_sleep(double nb_sec)
121 msg_error_t status = MSG_OK;
123 TRACE_msg_process_sleep_in(MSG_process_self());
126 simcall_process_sleep(nb_sec);
129 switch (e.category) {
131 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
132 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
133 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
134 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
135 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
136 // and did not change anythings at the C level.
137 // 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)
138 status = MSG_TASK_CANCELED;
145 TRACE_msg_process_sleep_out(MSG_process_self());
149 /** \ingroup msg_task_usage
150 * \brief Receives a task from a mailbox.
152 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
153 * for receiving tasks asynchronously.
155 * \param task a memory location for storing a #msg_task_t.
156 * \param alias name of the mailbox to receive the task from
159 * #MSG_OK if the task was successfully received,
160 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
162 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
164 return MSG_task_receive_with_timeout(task, alias, -1);
167 /** \ingroup msg_task_usage
168 * \brief Receives a task from a mailbox at a given rate.
170 * \param task a memory location for storing a #msg_task_t.
171 * \param alias name of the mailbox to receive the task from
172 * \param rate limit the reception to rate bandwidth
175 * #MSG_OK if the task was successfully received,
176 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
178 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
180 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
183 /** \ingroup msg_task_usage
184 * \brief Receives a task from a mailbox with a given timeout.
186 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
187 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
188 * to obtain an infinite timeout.
190 * \param task a memory location for storing a #msg_task_t.
191 * \param alias name of the mailbox to receive the task from
192 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
195 * #MSG_OK if the task was successfully received,
196 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
198 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
200 return MSG_task_receive_ext(task, alias, timeout, nullptr);
203 /** \ingroup msg_task_usage
204 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
206 * \param task a memory location for storing a #msg_task_t.
207 * \param alias name of the mailbox to receive the task from
208 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
209 * \param rate limit the reception to rate bandwidth
212 * #MSG_OK if the task was successfully received,
213 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
215 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
217 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
220 /** \ingroup msg_task_usage
221 * \brief Receives a task from a mailbox from a specific host with a given timeout.
223 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
224 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
225 * to obtain an infinite timeout.
227 * \param task a memory location for storing a #msg_task_t.
228 * \param alias name of the mailbox to receive the task from
229 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
230 * \param host a #msg_host_t host from where the task was sent
233 * #MSG_OK if the task was successfully received,
234 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
236 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
238 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
239 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
242 /** \ingroup msg_task_usage
243 * \brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
245 * \param task a memory location for storing a #msg_task_t.
246 * \param alias name of the mailbox to receive the task from
247 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
248 * \param host a #msg_host_t host from where the task was sent
249 * \param rate limit the reception to rate bandwidth
252 * #MSG_OK if the task was successfully received,
253 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
255 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
258 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
259 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
260 msg_error_t ret = MSG_OK;
261 /* We no longer support getting a task from a specific host */
265 TRACE_msg_task_get_start();
266 double start_time = MSG_get_clock();
269 xbt_assert(task, "Null pointer for the task storage");
272 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
274 /* Try to receive it by calling SIMIX network layer */
276 simcall_comm_recv(MSG_process_self(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
277 XBT_DEBUG("Got task %s from %s",(*task)->name,mailbox->name());
278 (*task)->simdata->setNotUsed();
281 switch (e.category) {
284 ret = MSG_HOST_FAILURE;
287 ret = MSG_TRANSFER_FAILURE;
297 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
298 TRACE_msg_task_get_end(start_time, *task);
303 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
304 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
305 int (*match_fun)(void*,void*, smx_activity_t),
306 void *match_data, void_f_pvoid_t cleanup, int detached)
308 simdata_task_t t_simdata = nullptr;
309 msg_process_t myself = SIMIX_process_self();
310 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
311 int call_end = TRACE_msg_task_put_start(task);
313 /* Prepare the task to send */
314 t_simdata = task->simdata;
315 t_simdata->sender = myself;
316 t_simdata->source = (static_cast<simdata_process_t>(SIMIX_process_self_get_data()))->m_host;
317 t_simdata->setUsed();
318 t_simdata->comm = nullptr;
319 msg_global->sent_msg++;
321 /* Send it by calling SIMIX network layer */
322 smx_activity_t act = simcall_comm_isend(myself, mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate,
323 task, sizeof(void *), match_fun, cleanup, nullptr, match_data,detached);
324 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(act);
326 msg_comm_t comm = nullptr;
328 comm = xbt_new0(s_msg_comm_t, 1);
329 comm->task_sent = task;
330 comm->task_received = nullptr;
331 comm->status = MSG_OK;
335 if (TRACE_is_enabled())
336 simcall_set_category(act, task->category);
338 TRACE_msg_task_put_end();
343 /** \ingroup msg_task_usage
344 * \brief Sends a task on a mailbox.
346 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
348 * \param task a #msg_task_t to send on another location.
349 * \param alias name of the mailbox to sent the task to
350 * \return the msg_comm_t communication created
352 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
354 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
357 /** \ingroup msg_task_usage
358 * \brief Sends a task on a mailbox with a maximum rate
360 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
361 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
363 * \param task a #msg_task_t to send on another location.
364 * \param alias name of the mailbox to sent the task to
365 * \param maxrate the maximum communication rate for sending this task .
366 * \return the msg_comm_t communication created
368 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
370 task->simdata->rate = maxrate;
371 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
374 /** \ingroup msg_task_usage
375 * \brief Sends a task on a mailbox, with support for matching requests
377 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
379 * \param task a #msg_task_t to send on another location.
380 * \param alias name of the mailbox to sent the task to
381 * \param match_fun boolean function which parameters are:
382 * - match_data_provided_here
383 * - match_data_provided_by_other_side_if_any
384 * - the_smx_synchro_describing_the_other_side
385 * \param match_data user provided data passed to match_fun
386 * \return the msg_comm_t communication created
388 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
389 int (*match_fun)(void*, void*, smx_activity_t), void *match_data)
391 return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
394 /** \ingroup msg_task_usage
395 * \brief Sends a task on a mailbox.
397 * This is a non blocking detached send function.
398 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
399 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
400 * usual. More details on this can be obtained on
401 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
402 * in the SimGrid-user mailing list archive.
404 * \param task a #msg_task_t to send on another location.
405 * \param alias name of the mailbox to sent the task to
406 * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
407 * (if nullptr, no function will be called)
409 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
411 MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
414 /** \ingroup msg_task_usage
415 * \brief Sends a task on a mailbox with a maximal rate.
417 * This is a non blocking detached send function.
418 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
419 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
420 * usual. More details on this can be obtained on
421 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
422 * in the SimGrid-user mailing list archive.
424 * \param task a #msg_task_t to send on another location.
425 * \param alias name of the mailbox to sent the task to
426 * \param cleanup a function to destroy the task if the
427 * communication fails, e.g. MSG_task_destroy
428 * (if nullptr, no function will be called)
429 * \param maxrate the maximum communication rate for sending this task
432 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
434 task->simdata->rate = maxrate;
435 MSG_task_dsend(task, alias, cleanup);
438 /** \ingroup msg_task_usage
439 * \brief Starts listening for receiving a task from an asynchronous communication.
441 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
443 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
444 * \param name of the mailbox to receive the task on
445 * \return the msg_comm_t communication created
447 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
449 return MSG_task_irecv_bounded(task, name, -1.0);
452 /** \ingroup msg_task_usage
453 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
455 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
456 * \param name of the mailbox to receive the task on
457 * \param rate limit the bandwidth to the given rate
458 * \return the msg_comm_t communication created
460 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
462 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
464 /* FIXME: these functions are not traceable */
466 xbt_assert(task, "Null pointer for the task storage");
469 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
471 /* Try to receive it by calling SIMIX network layer */
472 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
473 comm->task_sent = nullptr;
474 comm->task_received = task;
475 comm->status = MSG_OK;
476 comm->s_comm = simcall_comm_irecv(MSG_process_self(), mbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, rate);
481 /** \ingroup msg_task_usage
482 * \brief Checks whether a communication is done, and if yes, finalizes it.
483 * \param comm the communication to test
484 * \return TRUE if the communication is finished
485 * (but it may have failed, use MSG_comm_get_status() to know its status)
486 * or FALSE if the communication is not finished yet
487 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
489 int MSG_comm_test(msg_comm_t comm)
494 finished = simcall_comm_test(comm->s_comm);
495 if (finished && comm->task_received != nullptr) {
496 /* I am the receiver */
497 (*comm->task_received)->simdata->setNotUsed();
501 switch (e.category) {
503 comm->status = MSG_TRANSFER_FAILURE;
507 comm->status = MSG_TIMEOUT;
518 /** \ingroup msg_task_usage
519 * \brief This function checks if a communication is finished.
520 * \param comms a vector of communications
521 * \return the position of the finished communication if any
522 * (but it may have failed, use MSG_comm_get_status() to know its status),
523 * or -1 if none is finished
525 int MSG_comm_testany(xbt_dynar_t comms)
527 int finished_index = -1;
529 /* Create the equivalent array with SIMIX objects: */
530 std::vector<simgrid::kernel::activity::ActivityImpl*> s_comms;
531 s_comms.reserve(xbt_dynar_length(comms));
534 xbt_dynar_foreach(comms, cursor, comm) {
535 s_comms.push_back(comm->s_comm);
538 msg_error_t status = MSG_OK;
540 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
543 switch (e.category) {
545 finished_index = e.value;
546 status = MSG_TRANSFER_FAILURE;
549 finished_index = e.value;
550 status = MSG_TIMEOUT;
557 if (finished_index != -1) {
558 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
559 /* the communication is finished */
560 comm->status = status;
562 if (status == MSG_OK && comm->task_received != nullptr) {
563 /* I am the receiver */
564 (*comm->task_received)->simdata->setNotUsed();
568 return finished_index;
571 /** \ingroup msg_task_usage
572 * \brief Destroys a communication.
573 * \param comm the communication to destroy.
575 void MSG_comm_destroy(msg_comm_t comm)
580 /** \ingroup msg_task_usage
581 * \brief Wait for the completion of a communication.
583 * It takes two parameters.
584 * \param comm the communication to wait.
585 * \param timeout Wait until the communication terminates or the timeout occurs.
586 * You can provide a -1 timeout to obtain an infinite timeout.
587 * \return msg_error_t
589 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
592 simcall_comm_wait(comm->s_comm, timeout);
594 if (comm->task_received != nullptr) {
595 /* I am the receiver */
596 (*comm->task_received)->simdata->setNotUsed();
599 /* FIXME: these functions are not traceable */
602 switch (e.category) {
604 comm->status = MSG_TRANSFER_FAILURE;
607 comm->status = MSG_TIMEOUT;
617 /** \ingroup msg_task_usage
618 * \brief This function is called by a sender and permit to wait for each communication
620 * \param comm a vector of communication
621 * \param nb_elem is the size of the comm vector
622 * \param timeout for each call of MSG_comm_wait
624 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
627 for (i = 0; i < nb_elem; i++) {
628 MSG_comm_wait(comm[i], timeout);
632 /** \ingroup msg_task_usage
633 * \brief This function waits for the first communication finished in a list.
634 * \param comms a vector of communications
635 * \return the position of the first finished communication
636 * (but it may have failed, use MSG_comm_get_status() to know its status)
638 int MSG_comm_waitany(xbt_dynar_t comms)
640 int finished_index = -1;
642 /* create the equivalent dynar with SIMIX objects */
643 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), nullptr);
646 xbt_dynar_foreach(comms, cursor, comm) {
647 xbt_dynar_push(s_comms, &comm->s_comm);
650 msg_error_t status = MSG_OK;
652 finished_index = simcall_comm_waitany(s_comms, -1);
655 switch (e.category) {
657 finished_index = e.value;
658 status = MSG_TRANSFER_FAILURE;
661 finished_index = e.value;
662 status = MSG_TIMEOUT;
669 xbt_assert(finished_index != -1, "WaitAny returned -1");
670 xbt_dynar_free(&s_comms);
672 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
673 /* the communication is finished */
674 comm->status = status;
676 if (comm->task_received != nullptr) {
677 /* I am the receiver */
678 (*comm->task_received)->simdata->setNotUsed();
681 return finished_index;
685 * \ingroup msg_task_usage
686 * \brief Returns the error (if any) that occurred during a finished communication.
687 * \param comm a finished communication
688 * \return the status of the communication, or #MSG_OK if no error occurred
689 * during the communication
691 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
696 /** \ingroup msg_task_usage
697 * \brief Get a task (#msg_task_t) from a communication
699 * \param comm the communication where to get the task
700 * \return the task from the communication
702 msg_task_t MSG_comm_get_task(msg_comm_t comm)
704 xbt_assert(comm, "Invalid parameter");
706 return comm->task_received ? *comm->task_received : comm->task_sent;
710 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
711 * \param comm the comm
712 * \param buff the data copied
713 * \param buff_size size of the buffer
715 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
717 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
719 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
721 // notify the user callback if any
722 if (msg_global->task_copy_callback) {
723 msg_task_t task = static_cast<msg_task_t>(buff);
724 msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
728 /** \ingroup msg_task_usage
729 * \brief Sends a task to a mailbox
731 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
732 * side if #MSG_task_receive is used).
733 * See #MSG_task_isend for sending tasks asynchronously.
735 * \param task the task to be sent
736 * \param alias the mailbox name to where the task is sent
738 * \return Returns #MSG_OK if the task was successfully sent,
739 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
741 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
743 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
744 return MSG_task_send_with_timeout(task, alias, -1);
747 /** \ingroup msg_task_usage
748 * \brief Sends a task to a mailbox with a maximum rate
750 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
751 * the application to limit the bandwidth utilization of network links when sending the task.
753 * \param task the task to be sent
754 * \param alias the mailbox name to where the task is sent
755 * \param maxrate the maximum communication rate for sending this task
757 * \return Returns #MSG_OK if the task was successfully sent,
758 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
760 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
762 task->simdata->rate = maxrate;
763 return MSG_task_send(task, alias);
766 /** \ingroup msg_task_usage
767 * \brief Sends a task to a mailbox with a timeout
769 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
771 * \param task the task to be sent
772 * \param alias the mailbox name to where the task is sent
773 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
775 * \return Returns #MSG_OK if the task was successfully sent,
776 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
778 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
780 msg_error_t ret = MSG_OK;
781 simdata_task_t t_simdata = nullptr;
782 msg_process_t process = MSG_process_self();
783 simdata_process_t p_simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
784 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
786 int call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
788 /* Prepare the task to send */
789 t_simdata = task->simdata;
790 t_simdata->sender = process;
791 t_simdata->source = (static_cast<simdata_process_t>(SIMIX_process_self_get_data())) ->m_host;
793 t_simdata->setUsed();
795 t_simdata->comm = nullptr;
796 msg_global->sent_msg++;
798 p_simdata->waiting_task = task;
800 /* Try to send it by calling SIMIX network layer */
802 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
803 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
804 t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
805 if (TRACE_is_enabled())
806 simcall_set_category(comm, task->category);
807 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(comm);
808 simcall_comm_wait(comm, timeout);
811 switch (e.category) {
813 ret = MSG_HOST_FAILURE;
816 ret = MSG_TRANSFER_FAILURE;
825 /* If the send failed, it is not used anymore */
826 t_simdata->setNotUsed();
829 p_simdata->waiting_task = nullptr;
831 TRACE_msg_task_put_end();
835 /** \ingroup msg_task_usage
836 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
838 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
840 * \param task the task to be sent
841 * \param alias the mailbox name to where the task is sent
842 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
843 * \param maxrate the maximum communication rate for sending this task
845 * \return Returns #MSG_OK if the task was successfully sent,
846 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
848 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
850 task->simdata->rate = maxrate;
851 return MSG_task_send_with_timeout(task, alias, timeout);
854 /** \ingroup msg_task_usage
855 * \brief Check if there is a communication going on in a mailbox.
857 * \param alias the name of the mailbox to be considered
859 * \return Returns 1 if there is a communication, 0 otherwise
861 int MSG_task_listen(const char *alias)
863 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
864 return !mbox->empty() ||
865 (mbox->getImpl()->permanent_receiver && !mbox->getImpl()->done_comm_queue.empty());
868 /** \ingroup msg_task_usage
869 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
871 * \param alias the name of the mailbox to be considered
873 * \return Returns the PID of sender process,
874 * -1 if there is no communication in the mailbox.
876 int MSG_task_listen_from(const char *alias)
878 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
879 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mbox->front());
884 return MSG_process_get_PID( static_cast<msg_task_t>(comm->src_data)->simdata->sender );
887 /** \ingroup msg_task_usage
888 * \brief Sets the tracing category of a task.
890 * This function should be called after the creation of a MSG task, to define the category of that task. The
891 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
892 * parameter category must contain a category that was previously declared with the function #TRACE_category
893 * (or with #TRACE_category_with_color).
895 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
897 * \param task the task that is going to be categorized
898 * \param category the name of the category to be associated to the task
900 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
902 void MSG_task_set_category (msg_task_t task, const char *category)
904 TRACE_msg_set_task_category (task, category);
907 /** \ingroup msg_task_usage
909 * \brief Gets the current tracing category of a task.
911 * \param task the task to be considered
913 * \see MSG_task_set_category
915 * \return Returns the name of the tracing category of the given task, nullptr otherwise
917 const char *MSG_task_get_category (msg_task_t task)
919 return task->category;
923 * \brief Returns the value of a given AS or router property
925 * \param asr the name of a router or AS
926 * \param name a property name
927 * \return value of a property (or nullptr if property not set)
929 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
931 return static_cast<char*>(xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name));
935 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
938 * \param asr the name of a router or AS
939 * \return a dict containing the properties
941 xbt_dict_t MSG_as_router_get_properties(const char* asr)
943 return (simcall_asr_get_properties(asr));
947 * \brief Change the value of a given AS or router
949 * \param asr the name of a router or AS
950 * \param name a property name
951 * \param value what to change the property to
953 void MSG_as_router_set_property_value(const char* asr, const char* name, char* value)
955 xbt_dict_set(MSG_as_router_get_properties(asr), name, value, nullptr);