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)
577 /* Diagnose a possible user error: destroying a comm that is not done yet, and not detached either */
578 if ((comm->s_comm->state == SIMIX_READY || comm->s_comm->state == SIMIX_RUNNING)
579 && (!static_cast<simgrid::kernel::activity::Comm*>(comm->s_comm)->detached))
580 XBT_WARN("You are destroying a communication that is not done yet but %s. "
581 "This will lead to issues when the other side continues this communication. "
582 "Are you looking for MSG_task_dsend()?",
583 (comm->s_comm->state == SIMIX_READY ? "ready" : "running"));
588 /** \ingroup msg_task_usage
589 * \brief Wait for the completion of a communication.
591 * It takes two parameters.
592 * \param comm the communication to wait.
593 * \param timeout Wait until the communication terminates or the timeout occurs.
594 * You can provide a -1 timeout to obtain an infinite timeout.
595 * \return msg_error_t
597 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
600 simcall_comm_wait(comm->s_comm, timeout);
602 if (comm->task_received != nullptr) {
603 /* I am the receiver */
604 (*comm->task_received)->simdata->setNotUsed();
607 /* FIXME: these functions are not traceable */
610 switch (e.category) {
612 comm->status = MSG_TRANSFER_FAILURE;
615 comm->status = MSG_TIMEOUT;
625 /** \ingroup msg_task_usage
626 * \brief This function is called by a sender and permit to wait for each communication
628 * \param comm a vector of communication
629 * \param nb_elem is the size of the comm vector
630 * \param timeout for each call of MSG_comm_wait
632 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
635 for (i = 0; i < nb_elem; i++) {
636 MSG_comm_wait(comm[i], timeout);
640 /** \ingroup msg_task_usage
641 * \brief This function waits for the first communication finished in a list.
642 * \param comms a vector of communications
643 * \return the position of the first finished communication
644 * (but it may have failed, use MSG_comm_get_status() to know its status)
646 int MSG_comm_waitany(xbt_dynar_t comms)
648 int finished_index = -1;
650 /* create the equivalent dynar with SIMIX objects */
651 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), nullptr);
654 xbt_dynar_foreach(comms, cursor, comm) {
655 xbt_dynar_push(s_comms, &comm->s_comm);
658 msg_error_t status = MSG_OK;
660 finished_index = simcall_comm_waitany(s_comms, -1);
663 switch (e.category) {
665 finished_index = e.value;
666 status = MSG_TRANSFER_FAILURE;
669 finished_index = e.value;
670 status = MSG_TIMEOUT;
677 xbt_assert(finished_index != -1, "WaitAny returned -1");
678 xbt_dynar_free(&s_comms);
680 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
681 /* the communication is finished */
682 comm->status = status;
684 if (comm->task_received != nullptr) {
685 /* I am the receiver */
686 (*comm->task_received)->simdata->setNotUsed();
689 return finished_index;
693 * \ingroup msg_task_usage
694 * \brief Returns the error (if any) that occurred during a finished communication.
695 * \param comm a finished communication
696 * \return the status of the communication, or #MSG_OK if no error occurred
697 * during the communication
699 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
704 /** \ingroup msg_task_usage
705 * \brief Get a task (#msg_task_t) from a communication
707 * \param comm the communication where to get the task
708 * \return the task from the communication
710 msg_task_t MSG_comm_get_task(msg_comm_t comm)
712 xbt_assert(comm, "Invalid parameter");
714 return comm->task_received ? *comm->task_received : comm->task_sent;
718 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
719 * \param comm the comm
720 * \param buff the data copied
721 * \param buff_size size of the buffer
723 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
725 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
727 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
729 // notify the user callback if any
730 if (msg_global->task_copy_callback) {
731 msg_task_t task = static_cast<msg_task_t>(buff);
732 msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
736 /** \ingroup msg_task_usage
737 * \brief Sends a task to a mailbox
739 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
740 * side if #MSG_task_receive is used).
741 * See #MSG_task_isend for sending tasks asynchronously.
743 * \param task the task to be sent
744 * \param alias the mailbox name to where the task is sent
746 * \return Returns #MSG_OK if the task was successfully sent,
747 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
749 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
751 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
752 return MSG_task_send_with_timeout(task, alias, -1);
755 /** \ingroup msg_task_usage
756 * \brief Sends a task to a mailbox with a maximum rate
758 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
759 * the application to limit the bandwidth utilization of network links when sending the task.
761 * \param task the task to be sent
762 * \param alias the mailbox name to where the task is sent
763 * \param maxrate the maximum communication rate for sending this task
765 * \return Returns #MSG_OK if the task was successfully sent,
766 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
768 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
770 task->simdata->rate = maxrate;
771 return MSG_task_send(task, alias);
774 /** \ingroup msg_task_usage
775 * \brief Sends a task to a mailbox with a timeout
777 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
779 * \param task the task to be sent
780 * \param alias the mailbox name to where the task is sent
781 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
783 * \return Returns #MSG_OK if the task was successfully sent,
784 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
786 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
788 msg_error_t ret = MSG_OK;
789 simdata_task_t t_simdata = nullptr;
790 msg_process_t process = MSG_process_self();
791 simdata_process_t p_simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
792 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
794 int call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
796 /* Prepare the task to send */
797 t_simdata = task->simdata;
798 t_simdata->sender = process;
799 t_simdata->source = (static_cast<simdata_process_t>(SIMIX_process_self_get_data())) ->m_host;
801 t_simdata->setUsed();
803 t_simdata->comm = nullptr;
804 msg_global->sent_msg++;
806 p_simdata->waiting_task = task;
808 /* Try to send it by calling SIMIX network layer */
810 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
811 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
812 t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
813 if (TRACE_is_enabled())
814 simcall_set_category(comm, task->category);
815 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(comm);
816 simcall_comm_wait(comm, timeout);
819 switch (e.category) {
821 ret = MSG_HOST_FAILURE;
824 ret = MSG_TRANSFER_FAILURE;
833 /* If the send failed, it is not used anymore */
834 t_simdata->setNotUsed();
837 p_simdata->waiting_task = nullptr;
839 TRACE_msg_task_put_end();
843 /** \ingroup msg_task_usage
844 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
846 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
848 * \param task the task to be sent
849 * \param alias the mailbox name to where the task is sent
850 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
851 * \param maxrate the maximum communication rate for sending this task
853 * \return Returns #MSG_OK if the task was successfully sent,
854 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
856 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
858 task->simdata->rate = maxrate;
859 return MSG_task_send_with_timeout(task, alias, timeout);
862 /** \ingroup msg_task_usage
863 * \brief Check if there is a communication going on in a mailbox.
865 * \param alias the name of the mailbox to be considered
867 * \return Returns 1 if there is a communication, 0 otherwise
869 int MSG_task_listen(const char *alias)
871 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
872 return !mbox->empty() ||
873 (mbox->getImpl()->permanent_receiver && !mbox->getImpl()->done_comm_queue.empty());
876 /** \ingroup msg_task_usage
877 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
879 * \param alias the name of the mailbox to be considered
881 * \return Returns the PID of sender process,
882 * -1 if there is no communication in the mailbox.
884 int MSG_task_listen_from(const char *alias)
886 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
887 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mbox->front());
892 return MSG_process_get_PID( static_cast<msg_task_t>(comm->src_data)->simdata->sender );
895 /** \ingroup msg_task_usage
896 * \brief Sets the tracing category of a task.
898 * This function should be called after the creation of a MSG task, to define the category of that task. The
899 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
900 * parameter category must contain a category that was previously declared with the function #TRACE_category
901 * (or with #TRACE_category_with_color).
903 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
905 * \param task the task that is going to be categorized
906 * \param category the name of the category to be associated to the task
908 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
910 void MSG_task_set_category (msg_task_t task, const char *category)
912 TRACE_msg_set_task_category (task, category);
915 /** \ingroup msg_task_usage
917 * \brief Gets the current tracing category of a task.
919 * \param task the task to be considered
921 * \see MSG_task_set_category
923 * \return Returns the name of the tracing category of the given task, nullptr otherwise
925 const char *MSG_task_get_category (msg_task_t task)
927 return task->category;
931 * \brief Returns the value of a given AS or router property
933 * \param asr the name of a router or AS
934 * \param name a property name
935 * \return value of a property (or nullptr if property not set)
937 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
939 return static_cast<char*>(xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name));
943 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
946 * \param asr the name of a router or AS
947 * \return a dict containing the properties
949 xbt_dict_t MSG_as_router_get_properties(const char* asr)
951 return (simcall_asr_get_properties(asr));
955 * \brief Change the value of a given AS or router
957 * \param asr the name of a router or AS
958 * \param name a property name
959 * \param value what to change the property to
961 void MSG_as_router_set_property_value(const char* asr, const char* name, char* value)
963 xbt_dict_set(MSG_as_router_get_properties(asr), name, value, nullptr);