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 return MSG_parallel_task_execute_with_timeout(task, -1);
49 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
51 simdata_task_t simdata = task->simdata;
52 simdata_process_t p_simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
53 e_smx_state_t comp_state;
54 msg_error_t status = MSG_OK;
56 TRACE_msg_task_execute_start(task);
58 xbt_assert((!simdata->compute) && !task->simdata->isused, "This task is executed somewhere else. Go fix your code!");
60 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
62 if (simdata->flops_amount <= 0.0 && !simdata->host_nb) {
63 TRACE_msg_task_execute_end(task);
70 if (simdata->host_nb > 0) {
71 simdata->compute = static_cast<simgrid::kernel::activity::Exec*>(simcall_execution_parallel_start(
72 task->name, simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
73 simdata->bytes_parallel_amount, 1.0, -1.0, timeout));
74 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
76 simdata->compute = static_cast<simgrid::kernel::activity::Exec*>(
77 simcall_execution_start(task->name, simdata->flops_amount, simdata->priority, simdata->bound));
79 simcall_set_category(simdata->compute, task->category);
80 p_simdata->waiting_action = simdata->compute;
81 comp_state = simcall_execution_wait(simdata->compute);
83 p_simdata->waiting_action = nullptr;
84 simdata->setNotUsed();
86 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
91 status = MSG_TASK_CANCELED;
94 status = MSG_HOST_FAILURE;
104 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
105 simdata->flops_amount = 0.0;
106 simdata->comm = nullptr;
107 simdata->compute = nullptr;
108 TRACE_msg_task_execute_end(task);
113 /** \ingroup msg_task_usage
114 * \brief Sleep for the specified number of seconds
116 * Makes the current process sleep until \a time seconds have elapsed.
118 * \param nb_sec a number of second
120 msg_error_t MSG_process_sleep(double nb_sec)
122 msg_error_t status = MSG_OK;
124 TRACE_msg_process_sleep_in(MSG_process_self());
127 simcall_process_sleep(nb_sec);
130 switch (e.category) {
132 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
133 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
134 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
135 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
136 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
137 // and did not change anythings at the C level.
138 // 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)
139 status = MSG_TASK_CANCELED;
146 TRACE_msg_process_sleep_out(MSG_process_self());
150 /** \ingroup msg_task_usage
151 * \brief Receives a task from a mailbox.
153 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
154 * for receiving tasks asynchronously.
156 * \param task a memory location for storing a #msg_task_t.
157 * \param alias name of the mailbox to receive the task from
160 * #MSG_OK if the task was successfully received,
161 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
163 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
165 return MSG_task_receive_with_timeout(task, alias, -1);
168 /** \ingroup msg_task_usage
169 * \brief Receives a task from a mailbox at a given rate.
171 * \param task a memory location for storing a #msg_task_t.
172 * \param alias name of the mailbox to receive the task from
173 * \param rate limit the reception to rate bandwidth
176 * #MSG_OK if the task was successfully received,
177 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
179 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
181 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
184 /** \ingroup msg_task_usage
185 * \brief Receives a task from a mailbox with a given timeout.
187 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
188 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
189 * to obtain an infinite timeout.
191 * \param task a memory location for storing a #msg_task_t.
192 * \param alias name of the mailbox to receive the task from
193 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
196 * #MSG_OK if the task was successfully received,
197 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
199 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
201 return MSG_task_receive_ext(task, alias, timeout, nullptr);
204 /** \ingroup msg_task_usage
205 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
207 * \param task a memory location for storing a #msg_task_t.
208 * \param alias name of the mailbox to receive the task from
209 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
210 * \param rate limit the reception to rate bandwidth
213 * #MSG_OK if the task was successfully received,
214 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
216 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
218 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
221 /** \ingroup msg_task_usage
222 * \brief Receives a task from a mailbox from a specific host with a given timeout.
224 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
225 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
226 * to obtain an infinite timeout.
228 * \param task a memory location for storing a #msg_task_t.
229 * \param alias name of the mailbox to receive the task from
230 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
231 * \param host a #msg_host_t host from where the task was sent
234 * #MSG_OK if the task was successfully received,
235 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
237 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
239 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
240 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
243 /** \ingroup msg_task_usage
244 * \brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
246 * \param task a memory location for storing a #msg_task_t.
247 * \param alias name of the mailbox to receive the task from
248 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
249 * \param host a #msg_host_t host from where the task was sent
250 * \param rate limit the reception to rate bandwidth
253 * #MSG_OK if the task was successfully received,
254 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
256 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
259 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
260 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
261 msg_error_t ret = MSG_OK;
262 /* We no longer support getting a task from a specific host */
266 TRACE_msg_task_get_start();
267 double start_time = MSG_get_clock();
270 xbt_assert(task, "Null pointer for the task storage");
273 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
275 /* Try to receive it by calling SIMIX network layer */
277 simcall_comm_recv(MSG_process_self(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
278 XBT_DEBUG("Got task %s from %s",(*task)->name,mailbox->name());
279 (*task)->simdata->setNotUsed();
282 switch (e.category) {
285 ret = MSG_HOST_FAILURE;
288 ret = MSG_TRANSFER_FAILURE;
298 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
299 TRACE_msg_task_get_end(start_time, *task);
304 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
305 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
306 int (*match_fun)(void*,void*, smx_activity_t),
307 void *match_data, void_f_pvoid_t cleanup, int detached)
309 simdata_task_t t_simdata = nullptr;
310 msg_process_t myself = SIMIX_process_self();
311 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
312 int call_end = TRACE_msg_task_put_start(task);
314 /* Prepare the task to send */
315 t_simdata = task->simdata;
316 t_simdata->sender = myself;
317 t_simdata->source = (static_cast<simdata_process_t>(SIMIX_process_self_get_data()))->m_host;
318 t_simdata->setUsed();
319 t_simdata->comm = nullptr;
320 msg_global->sent_msg++;
322 /* Send it by calling SIMIX network layer */
323 smx_activity_t act = simcall_comm_isend(myself, mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate,
324 task, sizeof(void *), match_fun, cleanup, nullptr, match_data,detached);
325 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(act);
327 msg_comm_t comm = nullptr;
329 comm = xbt_new0(s_msg_comm_t, 1);
330 comm->task_sent = task;
331 comm->task_received = nullptr;
332 comm->status = MSG_OK;
336 if (TRACE_is_enabled())
337 simcall_set_category(act, task->category);
339 TRACE_msg_task_put_end();
344 /** \ingroup msg_task_usage
345 * \brief Sends a task on a mailbox.
347 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
349 * \param task a #msg_task_t to send on another location.
350 * \param alias name of the mailbox to sent the task to
351 * \return the msg_comm_t communication created
353 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
355 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
358 /** \ingroup msg_task_usage
359 * \brief Sends a task on a mailbox with a maximum rate
361 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
362 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
364 * \param task a #msg_task_t to send on another location.
365 * \param alias name of the mailbox to sent the task to
366 * \param maxrate the maximum communication rate for sending this task .
367 * \return the msg_comm_t communication created
369 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
371 task->simdata->rate = maxrate;
372 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
375 /** \ingroup msg_task_usage
376 * \brief Sends a task on a mailbox, with support for matching requests
378 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
380 * \param task a #msg_task_t to send on another location.
381 * \param alias name of the mailbox to sent the task to
382 * \param match_fun boolean function which parameters are:
383 * - match_data_provided_here
384 * - match_data_provided_by_other_side_if_any
385 * - the_smx_synchro_describing_the_other_side
386 * \param match_data user provided data passed to match_fun
387 * \return the msg_comm_t communication created
389 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
390 int (*match_fun)(void*, void*, smx_activity_t), void *match_data)
392 return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
395 /** \ingroup msg_task_usage
396 * \brief Sends a task on a mailbox.
398 * This is a non blocking detached send function.
399 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
400 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
401 * usual. More details on this can be obtained on
402 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
403 * in the SimGrid-user mailing list archive.
405 * \param task a #msg_task_t to send on another location.
406 * \param alias name of the mailbox to sent the task to
407 * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
408 * (if nullptr, no function will be called)
410 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
412 MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
415 /** \ingroup msg_task_usage
416 * \brief Sends a task on a mailbox with a maximal rate.
418 * This is a non blocking detached send function.
419 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
420 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
421 * usual. More details on this can be obtained on
422 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
423 * in the SimGrid-user mailing list archive.
425 * \param task a #msg_task_t to send on another location.
426 * \param alias name of the mailbox to sent the task to
427 * \param cleanup a function to destroy the task if the
428 * communication fails, e.g. MSG_task_destroy
429 * (if nullptr, no function will be called)
430 * \param maxrate the maximum communication rate for sending this task
433 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
435 task->simdata->rate = maxrate;
436 MSG_task_dsend(task, alias, cleanup);
439 /** \ingroup msg_task_usage
440 * \brief Starts listening for receiving a task from an asynchronous communication.
442 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
444 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
445 * \param name of the mailbox to receive the task on
446 * \return the msg_comm_t communication created
448 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
450 return MSG_task_irecv_bounded(task, name, -1.0);
453 /** \ingroup msg_task_usage
454 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
456 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
457 * \param name of the mailbox to receive the task on
458 * \param rate limit the bandwidth to the given rate
459 * \return the msg_comm_t communication created
461 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
463 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
465 /* FIXME: these functions are not traceable */
467 xbt_assert(task, "Null pointer for the task storage");
470 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
472 /* Try to receive it by calling SIMIX network layer */
473 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
474 comm->task_sent = nullptr;
475 comm->task_received = task;
476 comm->status = MSG_OK;
477 comm->s_comm = simcall_comm_irecv(MSG_process_self(), mbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, rate);
482 /** \ingroup msg_task_usage
483 * \brief Checks whether a communication is done, and if yes, finalizes it.
484 * \param comm the communication to test
485 * \return TRUE if the communication is finished
486 * (but it may have failed, use MSG_comm_get_status() to know its status)
487 * or FALSE if the communication is not finished yet
488 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
490 int MSG_comm_test(msg_comm_t comm)
495 finished = simcall_comm_test(comm->s_comm);
496 if (finished && comm->task_received != nullptr) {
497 /* I am the receiver */
498 (*comm->task_received)->simdata->setNotUsed();
502 switch (e.category) {
504 comm->status = MSG_TRANSFER_FAILURE;
508 comm->status = MSG_TIMEOUT;
519 /** \ingroup msg_task_usage
520 * \brief This function checks if a communication is finished.
521 * \param comms a vector of communications
522 * \return the position of the finished communication if any
523 * (but it may have failed, use MSG_comm_get_status() to know its status),
524 * or -1 if none is finished
526 int MSG_comm_testany(xbt_dynar_t comms)
528 int finished_index = -1;
530 /* Create the equivalent array with SIMIX objects: */
531 std::vector<simgrid::kernel::activity::ActivityImpl*> s_comms;
532 s_comms.reserve(xbt_dynar_length(comms));
535 xbt_dynar_foreach(comms, cursor, comm) {
536 s_comms.push_back(comm->s_comm);
539 msg_error_t status = MSG_OK;
541 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
544 switch (e.category) {
546 finished_index = e.value;
547 status = MSG_TRANSFER_FAILURE;
550 finished_index = e.value;
551 status = MSG_TIMEOUT;
558 if (finished_index != -1) {
559 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
560 /* the communication is finished */
561 comm->status = status;
563 if (status == MSG_OK && comm->task_received != nullptr) {
564 /* I am the receiver */
565 (*comm->task_received)->simdata->setNotUsed();
569 return finished_index;
572 /** \ingroup msg_task_usage
573 * \brief Destroys a communication.
574 * \param comm the communication to destroy.
576 void MSG_comm_destroy(msg_comm_t comm)
581 /** \ingroup msg_task_usage
582 * \brief Wait for the completion of a communication.
584 * It takes two parameters.
585 * \param comm the communication to wait.
586 * \param timeout Wait until the communication terminates or the timeout occurs.
587 * You can provide a -1 timeout to obtain an infinite timeout.
588 * \return msg_error_t
590 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
593 simcall_comm_wait(comm->s_comm, timeout);
595 if (comm->task_received != nullptr) {
596 /* I am the receiver */
597 (*comm->task_received)->simdata->setNotUsed();
600 /* FIXME: these functions are not traceable */
603 switch (e.category) {
605 comm->status = MSG_TRANSFER_FAILURE;
608 comm->status = MSG_TIMEOUT;
618 /** \ingroup msg_task_usage
619 * \brief This function is called by a sender and permit to wait for each communication
621 * \param comm a vector of communication
622 * \param nb_elem is the size of the comm vector
623 * \param timeout for each call of MSG_comm_wait
625 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
628 for (i = 0; i < nb_elem; i++) {
629 MSG_comm_wait(comm[i], timeout);
633 /** \ingroup msg_task_usage
634 * \brief This function waits for the first communication finished in a list.
635 * \param comms a vector of communications
636 * \return the position of the first finished communication
637 * (but it may have failed, use MSG_comm_get_status() to know its status)
639 int MSG_comm_waitany(xbt_dynar_t comms)
641 int finished_index = -1;
643 /* create the equivalent dynar with SIMIX objects */
644 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), nullptr);
647 xbt_dynar_foreach(comms, cursor, comm) {
648 xbt_dynar_push(s_comms, &comm->s_comm);
651 msg_error_t status = MSG_OK;
653 finished_index = simcall_comm_waitany(s_comms, -1);
656 switch (e.category) {
658 finished_index = e.value;
659 status = MSG_TRANSFER_FAILURE;
662 finished_index = e.value;
663 status = MSG_TIMEOUT;
670 xbt_assert(finished_index != -1, "WaitAny returned -1");
671 xbt_dynar_free(&s_comms);
673 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
674 /* the communication is finished */
675 comm->status = status;
677 if (comm->task_received != nullptr) {
678 /* I am the receiver */
679 (*comm->task_received)->simdata->setNotUsed();
682 return finished_index;
686 * \ingroup msg_task_usage
687 * \brief Returns the error (if any) that occurred during a finished communication.
688 * \param comm a finished communication
689 * \return the status of the communication, or #MSG_OK if no error occurred
690 * during the communication
692 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
697 /** \ingroup msg_task_usage
698 * \brief Get a task (#msg_task_t) from a communication
700 * \param comm the communication where to get the task
701 * \return the task from the communication
703 msg_task_t MSG_comm_get_task(msg_comm_t comm)
705 xbt_assert(comm, "Invalid parameter");
707 return comm->task_received ? *comm->task_received : comm->task_sent;
711 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
712 * \param comm the comm
713 * \param buff the data copied
714 * \param buff_size size of the buffer
716 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
718 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
720 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
722 // notify the user callback if any
723 if (msg_global->task_copy_callback) {
724 msg_task_t task = static_cast<msg_task_t>(buff);
725 msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
729 /** \ingroup msg_task_usage
730 * \brief Sends a task to a mailbox
732 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
733 * side if #MSG_task_receive is used).
734 * See #MSG_task_isend for sending tasks asynchronously.
736 * \param task the task to be sent
737 * \param alias the mailbox name to where the task is sent
739 * \return Returns #MSG_OK if the task was successfully sent,
740 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
742 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
744 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
745 return MSG_task_send_with_timeout(task, alias, -1);
748 /** \ingroup msg_task_usage
749 * \brief Sends a task to a mailbox with a maximum rate
751 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
752 * the application to limit the bandwidth utilization of network links when sending the task.
754 * \param task the task to be sent
755 * \param alias the mailbox name to where the task is sent
756 * \param maxrate the maximum communication rate for sending this task
758 * \return Returns #MSG_OK if the task was successfully sent,
759 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
761 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
763 task->simdata->rate = maxrate;
764 return MSG_task_send(task, alias);
767 /** \ingroup msg_task_usage
768 * \brief Sends a task to a mailbox with a timeout
770 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
772 * \param task the task to be sent
773 * \param alias the mailbox name to where the task is sent
774 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
776 * \return Returns #MSG_OK if the task was successfully sent,
777 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
779 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
781 msg_error_t ret = MSG_OK;
782 simdata_task_t t_simdata = nullptr;
783 msg_process_t process = MSG_process_self();
784 simdata_process_t p_simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
785 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
787 int call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
789 /* Prepare the task to send */
790 t_simdata = task->simdata;
791 t_simdata->sender = process;
792 t_simdata->source = (static_cast<simdata_process_t>(SIMIX_process_self_get_data())) ->m_host;
794 t_simdata->setUsed();
796 t_simdata->comm = nullptr;
797 msg_global->sent_msg++;
799 p_simdata->waiting_task = task;
801 /* Try to send it by calling SIMIX network layer */
803 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
804 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
805 t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
806 if (TRACE_is_enabled())
807 simcall_set_category(comm, task->category);
808 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(comm);
809 simcall_comm_wait(comm, timeout);
812 switch (e.category) {
814 ret = MSG_HOST_FAILURE;
817 ret = MSG_TRANSFER_FAILURE;
826 /* If the send failed, it is not used anymore */
827 t_simdata->setNotUsed();
830 p_simdata->waiting_task = nullptr;
832 TRACE_msg_task_put_end();
836 /** \ingroup msg_task_usage
837 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
839 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
841 * \param task the task to be sent
842 * \param alias the mailbox name to where the task is sent
843 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
844 * \param maxrate the maximum communication rate for sending this task
846 * \return Returns #MSG_OK if the task was successfully sent,
847 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
849 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
851 task->simdata->rate = maxrate;
852 return MSG_task_send_with_timeout(task, alias, timeout);
855 /** \ingroup msg_task_usage
856 * \brief Check if there is a communication going on in a mailbox.
858 * \param alias the name of the mailbox to be considered
860 * \return Returns 1 if there is a communication, 0 otherwise
862 int MSG_task_listen(const char *alias)
864 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
865 return !mbox->empty() ||
866 (mbox->getImpl()->permanent_receiver && !mbox->getImpl()->done_comm_queue.empty());
869 /** \ingroup msg_task_usage
870 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
872 * \param alias the name of the mailbox to be considered
874 * \return Returns the PID of sender process,
875 * -1 if there is no communication in the mailbox.
877 int MSG_task_listen_from(const char *alias)
879 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
880 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mbox->front());
885 return MSG_process_get_PID( static_cast<msg_task_t>(comm->src_data)->simdata->sender );
888 /** \ingroup msg_task_usage
889 * \brief Sets the tracing category of a task.
891 * This function should be called after the creation of a MSG task, to define the category of that task. The
892 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
893 * parameter category must contain a category that was previously declared with the function #TRACE_category
894 * (or with #TRACE_category_with_color).
896 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
898 * \param task the task that is going to be categorized
899 * \param category the name of the category to be associated to the task
901 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
903 void MSG_task_set_category (msg_task_t task, const char *category)
905 TRACE_msg_set_task_category (task, category);
908 /** \ingroup msg_task_usage
910 * \brief Gets the current tracing category of a task.
912 * \param task the task to be considered
914 * \see MSG_task_set_category
916 * \return Returns the name of the tracing category of the given task, nullptr otherwise
918 const char *MSG_task_get_category (msg_task_t task)
920 return task->category;
924 * \brief Returns the value of a given AS or router property
926 * \param asr the name of a router or AS
927 * \param name a property name
928 * \return value of a property (or nullptr if property not set)
930 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
932 return static_cast<char*>(xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name));
936 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
939 * \param asr the name of a router or AS
940 * \return a dict containing the properties
942 xbt_dict_t MSG_as_router_get_properties(const char* asr)
944 return (simcall_asr_get_properties(asr));
948 * \brief Change the value of a given AS or router
950 * \param asr the name of a router or AS
951 * \param name a property name
952 * \param value what to change the property to
953 * \param free_ctn the freeing function to use to kill the value on need
955 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
956 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);