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,
15 "Logging specific to MSG (gos)");
17 /** \ingroup msg_task_usage
18 * \brief Executes a task and waits for its termination.
20 * This function is used for describing the behavior of a process. It takes only one parameter.
21 * \param task a #msg_task_t to execute on the location on which the process is running.
22 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
24 msg_error_t MSG_task_execute(msg_task_t task)
26 /* TODO: add this to other locations */
27 msg_host_t host = MSG_process_get_host(MSG_process_self());
28 MSG_host_add_task(host, task);
30 msg_error_t ret = MSG_parallel_task_execute(task);
32 MSG_host_del_task(host, task);
37 /** \ingroup msg_task_usage
38 * \brief Executes a parallel task and waits for its termination.
40 * \param task a #msg_task_t to execute on the location on which the process is running.
42 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
43 * or #MSG_HOST_FAILURE otherwise
45 msg_error_t MSG_parallel_task_execute(msg_task_t task)
47 simdata_task_t simdata = task->simdata;
48 simdata_process_t p_simdata = (simdata_process_t) SIMIX_process_self_get_data();
49 e_smx_state_t comp_state;
50 msg_error_t status = MSG_OK;
52 TRACE_msg_task_execute_start(task);
54 xbt_assert((!simdata->compute) && !task->simdata->isused,
55 "This task is executed somewhere else. Go fix your code!");
57 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
59 if (simdata->flops_amount == 0 && !simdata->host_nb) {
60 TRACE_msg_task_execute_end(task);
67 if (simdata->host_nb > 0) {
68 simdata->compute = static_cast<simgrid::kernel::activity::Exec*>(
69 simcall_execution_parallel_start(task->name, simdata->host_nb,simdata->host_list,
70 simdata->flops_parallel_amount, simdata->bytes_parallel_amount,
72 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
74 unsigned long affinity_mask =
75 (unsigned long)(uintptr_t) xbt_dict_get_or_null_ext(simdata->affinity_mask_db, (char *) p_simdata->m_host,
77 XBT_DEBUG("execute %s@%s with affinity(0x%04lx)",
78 MSG_task_get_name(task), MSG_host_get_name(p_simdata->m_host), affinity_mask);
80 simdata->compute = static_cast<simgrid::kernel::activity::Exec*>(
81 simcall_execution_start(task->name, simdata->flops_amount, simdata->priority,
82 simdata->bound, affinity_mask));
84 simcall_set_category(simdata->compute, task->category);
85 p_simdata->waiting_action = simdata->compute;
86 comp_state = simcall_execution_wait(simdata->compute);
88 p_simdata->waiting_action = nullptr;
89 simdata->setNotUsed();
91 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
96 status = MSG_TASK_CANCELED;
99 status = MSG_HOST_FAILURE;
106 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
107 simdata->flops_amount = 0.0;
108 simdata->comm = nullptr;
109 simdata->compute = nullptr;
110 TRACE_msg_task_execute_end(task);
115 /** \ingroup msg_task_usage
116 * \brief Sleep for the specified number of seconds
118 * Makes the current process sleep until \a time seconds have elapsed.
120 * \param nb_sec a number of second
122 msg_error_t MSG_process_sleep(double nb_sec)
124 msg_error_t status = MSG_OK;
125 /*msg_process_t proc = MSG_process_self();*/
127 TRACE_msg_process_sleep_in(MSG_process_self());
130 simcall_process_sleep(nb_sec);
133 switch (e.category) {
135 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, WTF here ?");
136 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
137 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
138 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
139 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
140 // and did not change anythings at the C level.
141 // 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)
142 status = MSG_TASK_CANCELED;
149 TRACE_msg_process_sleep_out(MSG_process_self());
153 /** \ingroup msg_task_usage
154 * \brief Receives a task from a mailbox.
156 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
157 * for receiving tasks asynchronously.
159 * \param task a memory location for storing a #msg_task_t.
160 * \param alias name of the mailbox to receive the task from
163 * #MSG_OK if the task was successfully received,
164 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
166 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
168 return MSG_task_receive_with_timeout(task, alias, -1);
171 /** \ingroup msg_task_usage
172 * \brief Receives a task from a mailbox at a given rate.
174 * \param task a memory location for storing a #msg_task_t.
175 * \param alias name of the mailbox to receive the task from
176 * \param rate limit the reception to rate bandwidth
179 * #MSG_OK if the task was successfully received,
180 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
182 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
184 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
187 /** \ingroup msg_task_usage
188 * \brief Receives a task from a mailbox with a given timeout.
190 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
191 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
192 * to obtain an infinite timeout.
194 * \param task a memory location for storing a #msg_task_t.
195 * \param alias name of the mailbox to receive the task from
196 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
199 * #MSG_OK if the task was successfully received,
200 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
202 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
204 return MSG_task_receive_ext(task, alias, timeout, nullptr);
207 /** \ingroup msg_task_usage
208 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
210 * \param task a memory location for storing a #msg_task_t.
211 * \param alias name of the mailbox to receive the task from
212 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
213 * \param rate limit the reception to rate bandwidth
216 * #MSG_OK if the task was successfully received,
217 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
219 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
221 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
224 /** \ingroup msg_task_usage
225 * \brief Receives a task from a mailbox from a specific host with a given timeout.
227 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
228 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
229 * to obtain an infinite timeout.
231 * \param task a memory location for storing a #msg_task_t.
232 * \param alias name of the mailbox to receive the task from
233 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
234 * \param host a #msg_host_t host from where the task was sent
237 * #MSG_OK if the task was successfully received,
238 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
240 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
242 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
243 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
246 /** \ingroup msg_task_usage
247 * \brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
249 * \param task a memory location for storing a #msg_task_t.
250 * \param alias name of the mailbox to receive the task from
251 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
252 * \param host a #msg_host_t host from where the task was sent
253 * \param rate limit the reception to rate bandwidth
256 * #MSG_OK if the task was successfully received,
257 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
259 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
262 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
263 msg_mailbox_t mailbox = simgrid::s4u::Mailbox::byName(alias);
264 msg_error_t ret = MSG_OK;
265 /* We no longer support getting a task from a specific host */
269 TRACE_msg_task_get_start();
270 double start_time = MSG_get_clock();
273 xbt_assert(task, "Null pointer for the task storage");
276 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
278 /* Try to receive it by calling SIMIX network layer */
280 simcall_comm_recv(MSG_process_self(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
281 XBT_DEBUG("Got task %s from %s",(*task)->name,mailbox->getName());
282 (*task)->simdata->setNotUsed();
285 switch (e.category) {
287 ret = MSG_HOST_FAILURE;
290 ret = MSG_TRANSFER_FAILURE;
296 ret = MSG_HOST_FAILURE;
303 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
304 TRACE_msg_task_get_end(start_time, *task);
309 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
310 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
311 int (*match_fun)(void*,void*, smx_synchro_t),
312 void *match_data, void_f_pvoid_t cleanup, int detached)
314 simdata_task_t t_simdata = nullptr;
315 msg_process_t myself = SIMIX_process_self();
316 msg_mailbox_t mailbox = simgrid::s4u::Mailbox::byName(alias);
317 int call_end = TRACE_msg_task_put_start(task);
319 /* Prepare the task to send */
320 t_simdata = task->simdata;
321 t_simdata->sender = myself;
322 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host;
323 t_simdata->setUsed();
324 t_simdata->comm = nullptr;
325 msg_global->sent_msg++;
327 /* Send it by calling SIMIX network layer */
328 smx_synchro_t act = simcall_comm_isend(myself, mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate,
329 task, sizeof(void *), match_fun, cleanup, nullptr, match_data,detached);
330 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(act);
332 msg_comm_t comm = nullptr;
334 comm = xbt_new0(s_msg_comm_t, 1);
335 comm->task_sent = task;
336 comm->task_received = nullptr;
337 comm->status = MSG_OK;
341 if (TRACE_is_enabled())
342 simcall_set_category(act, task->category);
344 TRACE_msg_task_put_end();
349 /** \ingroup msg_task_usage
350 * \brief Sends a task on a mailbox.
352 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
354 * \param task a #msg_task_t to send on another location.
355 * \param alias name of the mailbox to sent the task to
356 * \return the msg_comm_t communication created
358 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
360 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
363 /** \ingroup msg_task_usage
364 * \brief Sends a task on a mailbox with a maximum rate
366 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
367 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
369 * \param task a #msg_task_t to send on another location.
370 * \param alias name of the mailbox to sent the task to
371 * \param maxrate the maximum communication rate for sending this task .
372 * \return the msg_comm_t communication created
374 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
376 task->simdata->rate = maxrate;
377 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
380 /** \ingroup msg_task_usage
381 * \brief Sends a task on a mailbox, with support for matching requests
383 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
385 * \param task a #msg_task_t to send on another location.
386 * \param alias name of the mailbox to sent the task to
387 * \param match_fun boolean function which parameters are:
388 * - match_data_provided_here
389 * - match_data_provided_by_other_side_if_any
390 * - the_smx_synchro_describing_the_other_side
391 * \param match_data user provided data passed to match_fun
392 * \return the msg_comm_t communication created
394 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
395 int (*match_fun)(void*, void*, smx_synchro_t), void *match_data)
397 return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
400 /** \ingroup msg_task_usage
401 * \brief Sends a task on a mailbox.
403 * This is a non blocking detached send function.
404 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
405 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
406 * usual. More details on this can be obtained on
407 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
408 * in the SimGrid-user mailing list archive.
410 * \param task a #msg_task_t to send on another location.
411 * \param alias name of the mailbox to sent the task to
412 * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
413 * (if nullptr, no function will be called)
415 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
417 MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
420 /** \ingroup msg_task_usage
421 * \brief Sends a task on a mailbox with a maximal rate.
423 * This is a non blocking detached send function.
424 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
425 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
426 * usual. More details on this can be obtained on
427 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
428 * in the SimGrid-user mailing list archive.
430 * \param task a #msg_task_t to send on another location.
431 * \param alias name of the mailbox to sent the task to
432 * \param cleanup a function to destroy the task if the
433 * communication fails, e.g. MSG_task_destroy
434 * (if nullptr, no function will be called)
435 * \param maxrate the maximum communication rate for sending this task
438 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
440 task->simdata->rate = maxrate;
441 MSG_task_dsend(task, alias, cleanup);
444 /** \ingroup msg_task_usage
445 * \brief Starts listening for receiving a task from an asynchronous communication.
447 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
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 * \return the msg_comm_t communication created
453 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
455 return MSG_task_irecv_bounded(task, name, -1.0);
458 /** \ingroup msg_task_usage
459 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
461 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
462 * \param name of the mailbox to receive the task on
463 * \param rate limit the bandwidth to the given rate
464 * \return the msg_comm_t communication created
466 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
468 msg_mailbox_t mbox = simgrid::s4u::Mailbox::byName(name);
470 /* FIXME: these functions are not traceable */
472 xbt_assert(task, "Null pointer for the task storage");
475 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
477 /* Try to receive it by calling SIMIX network layer */
478 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
479 comm->task_sent = nullptr;
480 comm->task_received = task;
481 comm->status = MSG_OK;
482 comm->s_comm = simcall_comm_irecv(MSG_process_self(), mbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, rate);
487 /** \ingroup msg_task_usage
488 * \brief Checks whether a communication is done, and if yes, finalizes it.
489 * \param comm the communication to test
490 * \return TRUE if the communication is finished
491 * (but it may have failed, use MSG_comm_get_status() to know its status)
492 * or FALSE if the communication is not finished yet
493 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
495 int MSG_comm_test(msg_comm_t comm)
500 finished = simcall_comm_test(comm->s_comm);
501 if (finished && comm->task_received != nullptr) {
502 /* I am the receiver */
503 (*comm->task_received)->simdata->setNotUsed();
507 switch (e.category) {
509 comm->status = MSG_TRANSFER_FAILURE;
513 comm->status = MSG_TIMEOUT;
524 /** \ingroup msg_task_usage
525 * \brief This function checks if a communication is finished.
526 * \param comms a vector of communications
527 * \return the position of the finished communication if any
528 * (but it may have failed, use MSG_comm_get_status() to know its status),
529 * or -1 if none is finished
531 int MSG_comm_testany(xbt_dynar_t comms)
533 int finished_index = -1;
535 /* Create the equivalent array with SIMIX objects: */
536 std::vector<simgrid::kernel::activity::Synchro*> s_comms;
537 s_comms.reserve(xbt_dynar_length(comms));
540 xbt_dynar_foreach(comms, cursor, comm) {
541 s_comms.push_back(comm->s_comm);
544 msg_error_t status = MSG_OK;
546 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
549 switch (e.category) {
551 finished_index = e.value;
552 status = MSG_TRANSFER_FAILURE;
555 finished_index = e.value;
556 status = MSG_TIMEOUT;
563 if (finished_index != -1) {
564 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
565 /* the communication is finished */
566 comm->status = status;
568 if (status == MSG_OK && comm->task_received != nullptr) {
569 /* I am the receiver */
570 (*comm->task_received)->simdata->setNotUsed();
574 return finished_index;
577 /** \ingroup msg_task_usage
578 * \brief Destroys a communication.
579 * \param comm the communication to destroy.
581 void MSG_comm_destroy(msg_comm_t comm)
586 /** \ingroup msg_task_usage
587 * \brief Wait for the completion of a communication.
589 * It takes two parameters.
590 * \param comm the communication to wait.
591 * \param timeout Wait until the communication terminates or the timeout occurs.
592 * You can provide a -1 timeout to obtain an infinite timeout.
593 * \return msg_error_t
595 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
598 simcall_comm_wait(comm->s_comm, timeout);
600 if (comm->task_received != nullptr) {
601 /* I am the receiver */
602 (*comm->task_received)->simdata->setNotUsed();
605 /* FIXME: these functions are not traceable */
608 switch (e.category) {
610 comm->status = MSG_TRANSFER_FAILURE;
613 comm->status = MSG_TIMEOUT;
623 /** \ingroup msg_task_usage
624 * \brief This function is called by a sender and permit to wait for each communication
626 * \param comm a vector of communication
627 * \param nb_elem is the size of the comm vector
628 * \param timeout for each call of MSG_comm_wait
630 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
633 for (i = 0; i < nb_elem; i++) {
634 MSG_comm_wait(comm[i], timeout);
638 /** \ingroup msg_task_usage
639 * \brief This function waits for the first communication finished in a list.
640 * \param comms a vector of communications
641 * \return the position of the first finished communication
642 * (but it may have failed, use MSG_comm_get_status() to know its status)
644 int MSG_comm_waitany(xbt_dynar_t comms)
646 int finished_index = -1;
648 /* create the equivalent dynar with SIMIX objects */
649 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), nullptr);
652 xbt_dynar_foreach(comms, cursor, comm) {
653 xbt_dynar_push(s_comms, &comm->s_comm);
656 msg_error_t status = MSG_OK;
658 finished_index = simcall_comm_waitany(s_comms, -1);
661 switch (e.category) {
663 finished_index = e.value;
664 status = MSG_TRANSFER_FAILURE;
667 finished_index = e.value;
668 status = MSG_TIMEOUT;
675 xbt_assert(finished_index != -1, "WaitAny returned -1");
676 xbt_dynar_free(&s_comms);
678 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
679 /* the communication is finished */
680 comm->status = status;
682 if (comm->task_received != nullptr) {
683 /* I am the receiver */
684 (*comm->task_received)->simdata->setNotUsed();
687 return finished_index;
691 * \ingroup msg_task_usage
692 * \brief Returns the error (if any) that occurred during a finished communication.
693 * \param comm a finished communication
694 * \return the status of the communication, or #MSG_OK if no error occurred
695 * during the communication
697 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
702 /** \ingroup msg_task_usage
703 * \brief Get a task (#msg_task_t) from a communication
705 * \param comm the communication where to get the task
706 * \return the task from the communication
708 msg_task_t MSG_comm_get_task(msg_comm_t comm)
710 xbt_assert(comm, "Invalid parameter");
712 return comm->task_received ? *comm->task_received : comm->task_sent;
716 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
717 * \param comm the comm
718 * \param buff the data copied
719 * \param buff_size size of the buffer
721 void MSG_comm_copy_data_from_SIMIX(smx_synchro_t synchro, void* buff, size_t buff_size)
723 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
725 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
727 // notify the user callback if any
728 if (msg_global->task_copy_callback) {
729 msg_task_t task = (msg_task_t) buff;
730 msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
734 /** \ingroup msg_task_usage
735 * \brief Sends a task to a mailbox
737 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
738 * side if #MSG_task_receive is used).
739 * See #MSG_task_isend for sending tasks asynchronously.
741 * \param task the task to be sent
742 * \param alias the mailbox name to where the task is sent
744 * \return Returns #MSG_OK if the task was successfully sent,
745 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
747 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
749 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
750 return MSG_task_send_with_timeout(task, alias, -1);
753 /** \ingroup msg_task_usage
754 * \brief Sends a task to a mailbox with a maximum rate
756 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
757 * the application to limit the bandwidth utilization of network links when sending the task.
759 * \param task the task to be sent
760 * \param alias the mailbox name to where the task is sent
761 * \param maxrate the maximum communication rate for sending this task
763 * \return Returns #MSG_OK if the task was successfully sent,
764 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
766 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
768 task->simdata->rate = maxrate;
769 return MSG_task_send(task, alias);
772 /** \ingroup msg_task_usage
773 * \brief Sends a task to a mailbox with a timeout
775 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
777 * \param task the task to be sent
778 * \param alias the mailbox name to where the task is sent
779 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
781 * \return Returns #MSG_OK if the task was successfully sent,
782 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
784 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
786 msg_error_t ret = MSG_OK;
787 simdata_task_t t_simdata = nullptr;
788 msg_process_t process = MSG_process_self();
789 simdata_process_t p_simdata = (simdata_process_t) SIMIX_process_self_get_data();
790 msg_mailbox_t mailbox = simgrid::s4u::Mailbox::byName(alias);
792 int call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
794 /* Prepare the task to send */
795 t_simdata = task->simdata;
796 t_simdata->sender = process;
797 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host;
799 t_simdata->setUsed();
801 t_simdata->comm = nullptr;
802 msg_global->sent_msg++;
804 p_simdata->waiting_task = task;
806 /* Try to send it by calling SIMIX network layer */
808 smx_synchro_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
809 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
810 t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
811 if (TRACE_is_enabled())
812 simcall_set_category(comm, task->category);
813 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(comm);
814 simcall_comm_wait(comm, timeout);
817 switch (e.category) {
819 ret = MSG_HOST_FAILURE;
822 ret = MSG_TRANSFER_FAILURE;
831 /* If the send failed, it is not used anymore */
832 t_simdata->setNotUsed();
835 p_simdata->waiting_task = nullptr;
837 TRACE_msg_task_put_end();
841 /** \ingroup msg_task_usage
842 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
844 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
846 * \param task the task to be sent
847 * \param alias the mailbox name to where the task is sent
848 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
849 * \param maxrate the maximum communication rate for sending this task
851 * \return Returns #MSG_OK if the task was successfully sent,
852 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
854 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
856 task->simdata->rate = maxrate;
857 return MSG_task_send_with_timeout(task, alias, timeout);
860 /** \ingroup msg_task_usage
861 * \brief Check if there is a communication going on in a mailbox.
863 * \param alias the name of the mailbox to be considered
865 * \return Returns 1 if there is a communication, 0 otherwise
867 int MSG_task_listen(const char *alias)
869 msg_mailbox_t mbox = simgrid::s4u::Mailbox::byName(alias);
870 return !mbox->empty() ||
871 (mbox->getImpl()->permanent_receiver && !mbox->getImpl()->done_comm_queue.empty());
874 /** \ingroup msg_task_usage
875 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
877 * \param alias the name of the mailbox to be considered
879 * \return Returns the PID of sender process,
880 * -1 if there is no communication in the mailbox.
882 int MSG_task_listen_from(const char *alias)
884 msg_mailbox_t mbox = simgrid::s4u::Mailbox::byName(alias);
885 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mbox->front());
890 return MSG_process_get_PID( static_cast<msg_task_t>(comm->src_data)->simdata->sender );
893 /** \ingroup msg_task_usage
894 * \brief Sets the tracing category of a task.
896 * This function should be called after the creation of a MSG task, to define the category of that task. The
897 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
898 * parameter category must contain a category that was previously declared with the function #TRACE_category
899 * (or with #TRACE_category_with_color).
901 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
903 * \param task the task that is going to be categorized
904 * \param category the name of the category to be associated to the task
906 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
908 void MSG_task_set_category (msg_task_t task, const char *category)
910 TRACE_msg_set_task_category (task, category);
913 /** \ingroup msg_task_usage
915 * \brief Gets the current tracing category of a task.
917 * \param task the task to be considered
919 * \see MSG_task_set_category
921 * \return Returns the name of the tracing category of the given task, nullptr otherwise
923 const char *MSG_task_get_category (msg_task_t task)
925 return task->category;
929 * \brief Returns the value of a given AS or router property
931 * \param asr the name of a router or AS
932 * \param name a property name
933 * \return value of a property (or nullptr if property not set)
935 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
937 return (char*) xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
941 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
944 * \param asr the name of a router or AS
945 * \return a dict containing the properties
947 xbt_dict_t MSG_as_router_get_properties(const char* asr)
949 return (simcall_asr_get_properties(asr));
953 * \brief Change the value of a given AS or router
955 * \param asr the name of a router or AS
956 * \param name a property name
957 * \param value what to change the property to
958 * \param free_ctn the freeing function to use to kill the value on need
960 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
961 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);