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);
36 /** \ingroup msg_task_usage
37 * \brief Executes a parallel task and waits for its termination.
39 * \param task a #msg_task_t to execute on the location on which the process is running.
41 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
42 * or #MSG_HOST_FAILURE otherwise
44 msg_error_t MSG_parallel_task_execute(msg_task_t task)
46 simdata_task_t simdata = task->simdata;
47 simdata_process_t p_simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
48 e_smx_state_t comp_state;
49 msg_error_t status = MSG_OK;
51 TRACE_msg_task_execute_start(task);
53 xbt_assert((!simdata->compute) && !task->simdata->isused, "This task is executed somewhere else. Go fix your code!");
55 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
57 if (simdata->flops_amount <= 0.0 && !simdata->host_nb) {
58 TRACE_msg_task_execute_end(task);
65 if (simdata->host_nb > 0) {
66 simdata->compute = static_cast<simgrid::kernel::activity::Exec*>(
67 simcall_execution_parallel_start(task->name, simdata->host_nb,simdata->host_list,
68 simdata->flops_parallel_amount, simdata->bytes_parallel_amount,
70 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
72 simdata->compute = static_cast<simgrid::kernel::activity::Exec*>(
73 simcall_execution_start(task->name, simdata->flops_amount, simdata->priority, simdata->bound));
75 simcall_set_category(simdata->compute, task->category);
76 p_simdata->waiting_action = simdata->compute;
77 comp_state = simcall_execution_wait(simdata->compute);
79 p_simdata->waiting_action = nullptr;
80 simdata->setNotUsed();
82 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
87 status = MSG_TASK_CANCELED;
90 status = MSG_HOST_FAILURE;
97 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
98 simdata->flops_amount = 0.0;
99 simdata->comm = nullptr;
100 simdata->compute = nullptr;
101 TRACE_msg_task_execute_end(task);
106 /** \ingroup msg_task_usage
107 * \brief Sleep for the specified number of seconds
109 * Makes the current process sleep until \a time seconds have elapsed.
111 * \param nb_sec a number of second
113 msg_error_t MSG_process_sleep(double nb_sec)
115 msg_error_t status = MSG_OK;
117 TRACE_msg_process_sleep_in(MSG_process_self());
120 simcall_process_sleep(nb_sec);
123 switch (e.category) {
125 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, WTF here ?");
126 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
127 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
128 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
129 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
130 // and did not change anythings at the C level.
131 // 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)
132 status = MSG_TASK_CANCELED;
139 TRACE_msg_process_sleep_out(MSG_process_self());
143 /** \ingroup msg_task_usage
144 * \brief Receives a task from a mailbox.
146 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
147 * for receiving tasks asynchronously.
149 * \param task a memory location for storing a #msg_task_t.
150 * \param alias name of the mailbox to receive the task from
153 * #MSG_OK if the task was successfully received,
154 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
156 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
158 return MSG_task_receive_with_timeout(task, alias, -1);
161 /** \ingroup msg_task_usage
162 * \brief Receives a task from a mailbox at a given rate.
164 * \param task a memory location for storing a #msg_task_t.
165 * \param alias name of the mailbox to receive the task from
166 * \param rate limit the reception to rate bandwidth
169 * #MSG_OK if the task was successfully received,
170 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
172 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
174 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
177 /** \ingroup msg_task_usage
178 * \brief Receives a task from a mailbox with a given timeout.
180 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
181 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
182 * to obtain an infinite timeout.
184 * \param task a memory location for storing a #msg_task_t.
185 * \param alias name of the mailbox to receive the task from
186 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
189 * #MSG_OK if the task was successfully received,
190 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
192 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
194 return MSG_task_receive_ext(task, alias, timeout, nullptr);
197 /** \ingroup msg_task_usage
198 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
200 * \param task a memory location for storing a #msg_task_t.
201 * \param alias name of the mailbox to receive the task from
202 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
203 * \param rate limit the reception to rate bandwidth
206 * #MSG_OK if the task was successfully received,
207 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
209 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
211 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
214 /** \ingroup msg_task_usage
215 * \brief Receives a task from a mailbox from a specific host with a given timeout.
217 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
218 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
219 * to obtain an infinite timeout.
221 * \param task a memory location for storing a #msg_task_t.
222 * \param alias name of the mailbox to receive the task from
223 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
224 * \param host a #msg_host_t host from where the task was sent
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(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
232 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
233 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
236 /** \ingroup msg_task_usage
237 * \brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
239 * \param task a memory location for storing a #msg_task_t.
240 * \param alias name of the mailbox to receive the task from
241 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
242 * \param host a #msg_host_t host from where the task was sent
243 * \param rate limit the reception to rate bandwidth
246 * #MSG_OK if the task was successfully received,
247 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
249 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
252 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
253 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
254 msg_error_t ret = MSG_OK;
255 /* We no longer support getting a task from a specific host */
259 TRACE_msg_task_get_start();
260 double start_time = MSG_get_clock();
263 xbt_assert(task, "Null pointer for the task storage");
266 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
268 /* Try to receive it by calling SIMIX network layer */
270 simcall_comm_recv(MSG_process_self(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
271 XBT_DEBUG("Got task %s from %s",(*task)->name,mailbox->getName());
272 (*task)->simdata->setNotUsed();
275 switch (e.category) {
278 ret = MSG_HOST_FAILURE;
281 ret = MSG_TRANSFER_FAILURE;
291 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
292 TRACE_msg_task_get_end(start_time, *task);
297 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
298 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
299 int (*match_fun)(void*,void*, smx_activity_t),
300 void *match_data, void_f_pvoid_t cleanup, int detached)
302 simdata_task_t t_simdata = nullptr;
303 msg_process_t myself = SIMIX_process_self();
304 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
305 int call_end = TRACE_msg_task_put_start(task);
307 /* Prepare the task to send */
308 t_simdata = task->simdata;
309 t_simdata->sender = myself;
310 t_simdata->source = (static_cast<simdata_process_t>(SIMIX_process_self_get_data()))->m_host;
311 t_simdata->setUsed();
312 t_simdata->comm = nullptr;
313 msg_global->sent_msg++;
315 /* Send it by calling SIMIX network layer */
316 smx_activity_t act = simcall_comm_isend(myself, mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate,
317 task, sizeof(void *), match_fun, cleanup, nullptr, match_data,detached);
318 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(act);
320 msg_comm_t comm = nullptr;
322 comm = xbt_new0(s_msg_comm_t, 1);
323 comm->task_sent = task;
324 comm->task_received = nullptr;
325 comm->status = MSG_OK;
329 if (TRACE_is_enabled())
330 simcall_set_category(act, task->category);
332 TRACE_msg_task_put_end();
337 /** \ingroup msg_task_usage
338 * \brief Sends a task on a mailbox.
340 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
342 * \param task a #msg_task_t to send on another location.
343 * \param alias name of the mailbox to sent the task to
344 * \return the msg_comm_t communication created
346 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
348 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
351 /** \ingroup msg_task_usage
352 * \brief Sends a task on a mailbox with a maximum rate
354 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
355 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
357 * \param task a #msg_task_t to send on another location.
358 * \param alias name of the mailbox to sent the task to
359 * \param maxrate the maximum communication rate for sending this task .
360 * \return the msg_comm_t communication created
362 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
364 task->simdata->rate = maxrate;
365 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
368 /** \ingroup msg_task_usage
369 * \brief Sends a task on a mailbox, with support for matching requests
371 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
373 * \param task a #msg_task_t to send on another location.
374 * \param alias name of the mailbox to sent the task to
375 * \param match_fun boolean function which parameters are:
376 * - match_data_provided_here
377 * - match_data_provided_by_other_side_if_any
378 * - the_smx_synchro_describing_the_other_side
379 * \param match_data user provided data passed to match_fun
380 * \return the msg_comm_t communication created
382 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
383 int (*match_fun)(void*, void*, smx_activity_t), void *match_data)
385 return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
388 /** \ingroup msg_task_usage
389 * \brief Sends a task on a mailbox.
391 * This is a non blocking detached send function.
392 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
393 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
394 * usual. More details on this can be obtained on
395 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
396 * in the SimGrid-user mailing list archive.
398 * \param task a #msg_task_t to send on another location.
399 * \param alias name of the mailbox to sent the task to
400 * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
401 * (if nullptr, no function will be called)
403 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
405 MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
408 /** \ingroup msg_task_usage
409 * \brief Sends a task on a mailbox with a maximal rate.
411 * This is a non blocking detached send function.
412 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
413 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
414 * usual. More details on this can be obtained on
415 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
416 * in the SimGrid-user mailing list archive.
418 * \param task a #msg_task_t to send on another location.
419 * \param alias name of the mailbox to sent the task to
420 * \param cleanup a function to destroy the task if the
421 * communication fails, e.g. MSG_task_destroy
422 * (if nullptr, no function will be called)
423 * \param maxrate the maximum communication rate for sending this task
426 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
428 task->simdata->rate = maxrate;
429 MSG_task_dsend(task, alias, cleanup);
432 /** \ingroup msg_task_usage
433 * \brief Starts listening for receiving a task from an asynchronous communication.
435 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
437 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
438 * \param name of the mailbox to receive the task on
439 * \return the msg_comm_t communication created
441 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
443 return MSG_task_irecv_bounded(task, name, -1.0);
446 /** \ingroup msg_task_usage
447 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
449 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
450 * \param name of the mailbox to receive the task on
451 * \param rate limit the bandwidth to the given rate
452 * \return the msg_comm_t communication created
454 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
456 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
458 /* FIXME: these functions are not traceable */
460 xbt_assert(task, "Null pointer for the task storage");
463 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
465 /* Try to receive it by calling SIMIX network layer */
466 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
467 comm->task_sent = nullptr;
468 comm->task_received = task;
469 comm->status = MSG_OK;
470 comm->s_comm = simcall_comm_irecv(MSG_process_self(), mbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, rate);
475 /** \ingroup msg_task_usage
476 * \brief Checks whether a communication is done, and if yes, finalizes it.
477 * \param comm the communication to test
478 * \return TRUE if the communication is finished
479 * (but it may have failed, use MSG_comm_get_status() to know its status)
480 * or FALSE if the communication is not finished yet
481 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
483 int MSG_comm_test(msg_comm_t comm)
488 finished = simcall_comm_test(comm->s_comm);
489 if (finished && comm->task_received != nullptr) {
490 /* I am the receiver */
491 (*comm->task_received)->simdata->setNotUsed();
495 switch (e.category) {
497 comm->status = MSG_TRANSFER_FAILURE;
501 comm->status = MSG_TIMEOUT;
512 /** \ingroup msg_task_usage
513 * \brief This function checks if a communication is finished.
514 * \param comms a vector of communications
515 * \return the position of the finished communication if any
516 * (but it may have failed, use MSG_comm_get_status() to know its status),
517 * or -1 if none is finished
519 int MSG_comm_testany(xbt_dynar_t comms)
521 int finished_index = -1;
523 /* Create the equivalent array with SIMIX objects: */
524 std::vector<simgrid::kernel::activity::ActivityImpl*> s_comms;
525 s_comms.reserve(xbt_dynar_length(comms));
528 xbt_dynar_foreach(comms, cursor, comm) {
529 s_comms.push_back(comm->s_comm);
532 msg_error_t status = MSG_OK;
534 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
537 switch (e.category) {
539 finished_index = e.value;
540 status = MSG_TRANSFER_FAILURE;
543 finished_index = e.value;
544 status = MSG_TIMEOUT;
551 if (finished_index != -1) {
552 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
553 /* the communication is finished */
554 comm->status = status;
556 if (status == MSG_OK && comm->task_received != nullptr) {
557 /* I am the receiver */
558 (*comm->task_received)->simdata->setNotUsed();
562 return finished_index;
565 /** \ingroup msg_task_usage
566 * \brief Destroys a communication.
567 * \param comm the communication to destroy.
569 void MSG_comm_destroy(msg_comm_t comm)
574 /** \ingroup msg_task_usage
575 * \brief Wait for the completion of a communication.
577 * It takes two parameters.
578 * \param comm the communication to wait.
579 * \param timeout Wait until the communication terminates or the timeout occurs.
580 * You can provide a -1 timeout to obtain an infinite timeout.
581 * \return msg_error_t
583 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
586 simcall_comm_wait(comm->s_comm, timeout);
588 if (comm->task_received != nullptr) {
589 /* I am the receiver */
590 (*comm->task_received)->simdata->setNotUsed();
593 /* FIXME: these functions are not traceable */
596 switch (e.category) {
598 comm->status = MSG_TRANSFER_FAILURE;
601 comm->status = MSG_TIMEOUT;
611 /** \ingroup msg_task_usage
612 * \brief This function is called by a sender and permit to wait for each communication
614 * \param comm a vector of communication
615 * \param nb_elem is the size of the comm vector
616 * \param timeout for each call of MSG_comm_wait
618 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
621 for (i = 0; i < nb_elem; i++) {
622 MSG_comm_wait(comm[i], timeout);
626 /** \ingroup msg_task_usage
627 * \brief This function waits for the first communication finished in a list.
628 * \param comms a vector of communications
629 * \return the position of the first finished communication
630 * (but it may have failed, use MSG_comm_get_status() to know its status)
632 int MSG_comm_waitany(xbt_dynar_t comms)
634 int finished_index = -1;
636 /* create the equivalent dynar with SIMIX objects */
637 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), nullptr);
640 xbt_dynar_foreach(comms, cursor, comm) {
641 xbt_dynar_push(s_comms, &comm->s_comm);
644 msg_error_t status = MSG_OK;
646 finished_index = simcall_comm_waitany(s_comms, -1);
649 switch (e.category) {
651 finished_index = e.value;
652 status = MSG_TRANSFER_FAILURE;
655 finished_index = e.value;
656 status = MSG_TIMEOUT;
663 xbt_assert(finished_index != -1, "WaitAny returned -1");
664 xbt_dynar_free(&s_comms);
666 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
667 /* the communication is finished */
668 comm->status = status;
670 if (comm->task_received != nullptr) {
671 /* I am the receiver */
672 (*comm->task_received)->simdata->setNotUsed();
675 return finished_index;
679 * \ingroup msg_task_usage
680 * \brief Returns the error (if any) that occurred during a finished communication.
681 * \param comm a finished communication
682 * \return the status of the communication, or #MSG_OK if no error occurred
683 * during the communication
685 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
690 /** \ingroup msg_task_usage
691 * \brief Get a task (#msg_task_t) from a communication
693 * \param comm the communication where to get the task
694 * \return the task from the communication
696 msg_task_t MSG_comm_get_task(msg_comm_t comm)
698 xbt_assert(comm, "Invalid parameter");
700 return comm->task_received ? *comm->task_received : comm->task_sent;
704 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
705 * \param comm the comm
706 * \param buff the data copied
707 * \param buff_size size of the buffer
709 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
711 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
713 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
715 // notify the user callback if any
716 if (msg_global->task_copy_callback) {
717 msg_task_t task = static_cast<msg_task_t>(buff);
718 msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
722 /** \ingroup msg_task_usage
723 * \brief Sends a task to a mailbox
725 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
726 * side if #MSG_task_receive is used).
727 * See #MSG_task_isend for sending tasks asynchronously.
729 * \param task the task to be sent
730 * \param alias the mailbox name to where the task is sent
732 * \return Returns #MSG_OK if the task was successfully sent,
733 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
735 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
737 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
738 return MSG_task_send_with_timeout(task, alias, -1);
741 /** \ingroup msg_task_usage
742 * \brief Sends a task to a mailbox with a maximum rate
744 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
745 * the application to limit the bandwidth utilization of network links when sending the task.
747 * \param task the task to be sent
748 * \param alias the mailbox name to where the task is sent
749 * \param maxrate the maximum communication rate for sending this task
751 * \return Returns #MSG_OK if the task was successfully sent,
752 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
754 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
756 task->simdata->rate = maxrate;
757 return MSG_task_send(task, alias);
760 /** \ingroup msg_task_usage
761 * \brief Sends a task to a mailbox with a timeout
763 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
765 * \param task the task to be sent
766 * \param alias the mailbox name to where the task is sent
767 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
769 * \return Returns #MSG_OK if the task was successfully sent,
770 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
772 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
774 msg_error_t ret = MSG_OK;
775 simdata_task_t t_simdata = nullptr;
776 msg_process_t process = MSG_process_self();
777 simdata_process_t p_simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
778 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
780 int call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
782 /* Prepare the task to send */
783 t_simdata = task->simdata;
784 t_simdata->sender = process;
785 t_simdata->source = (static_cast<simdata_process_t>(SIMIX_process_self_get_data())) ->m_host;
787 t_simdata->setUsed();
789 t_simdata->comm = nullptr;
790 msg_global->sent_msg++;
792 p_simdata->waiting_task = task;
794 /* Try to send it by calling SIMIX network layer */
796 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
797 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
798 t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
799 if (TRACE_is_enabled())
800 simcall_set_category(comm, task->category);
801 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(comm);
802 simcall_comm_wait(comm, timeout);
805 switch (e.category) {
807 ret = MSG_HOST_FAILURE;
810 ret = MSG_TRANSFER_FAILURE;
819 /* If the send failed, it is not used anymore */
820 t_simdata->setNotUsed();
823 p_simdata->waiting_task = nullptr;
825 TRACE_msg_task_put_end();
829 /** \ingroup msg_task_usage
830 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
832 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
834 * \param task the task to be sent
835 * \param alias the mailbox name to where the task is sent
836 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
837 * \param maxrate the maximum communication rate for sending this task
839 * \return Returns #MSG_OK if the task was successfully sent,
840 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
842 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
844 task->simdata->rate = maxrate;
845 return MSG_task_send_with_timeout(task, alias, timeout);
848 /** \ingroup msg_task_usage
849 * \brief Check if there is a communication going on in a mailbox.
851 * \param alias the name of the mailbox to be considered
853 * \return Returns 1 if there is a communication, 0 otherwise
855 int MSG_task_listen(const char *alias)
857 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
858 return !mbox->empty() ||
859 (mbox->getImpl()->permanent_receiver && !mbox->getImpl()->done_comm_queue.empty());
862 /** \ingroup msg_task_usage
863 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
865 * \param alias the name of the mailbox to be considered
867 * \return Returns the PID of sender process,
868 * -1 if there is no communication in the mailbox.
870 int MSG_task_listen_from(const char *alias)
872 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
873 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mbox->front());
878 return MSG_process_get_PID( static_cast<msg_task_t>(comm->src_data)->simdata->sender );
881 /** \ingroup msg_task_usage
882 * \brief Sets the tracing category of a task.
884 * This function should be called after the creation of a MSG task, to define the category of that task. The
885 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
886 * parameter category must contain a category that was previously declared with the function #TRACE_category
887 * (or with #TRACE_category_with_color).
889 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
891 * \param task the task that is going to be categorized
892 * \param category the name of the category to be associated to the task
894 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
896 void MSG_task_set_category (msg_task_t task, const char *category)
898 TRACE_msg_set_task_category (task, category);
901 /** \ingroup msg_task_usage
903 * \brief Gets the current tracing category of a task.
905 * \param task the task to be considered
907 * \see MSG_task_set_category
909 * \return Returns the name of the tracing category of the given task, nullptr otherwise
911 const char *MSG_task_get_category (msg_task_t task)
913 return task->category;
917 * \brief Returns the value of a given AS or router property
919 * \param asr the name of a router or AS
920 * \param name a property name
921 * \return value of a property (or nullptr if property not set)
923 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
925 return static_cast<char*>(xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name));
929 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
932 * \param asr the name of a router or AS
933 * \return a dict containing the properties
935 xbt_dict_t MSG_as_router_get_properties(const char* asr)
937 return (simcall_asr_get_properties(asr));
941 * \brief Change the value of a given AS or router
943 * \param asr the name of a router or AS
944 * \param name a property name
945 * \param value what to change the property to
946 * \param free_ctn the freeing function to use to kill the value on need
948 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
949 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);