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 return MSG_mailbox_get_task_ext_bounded(simgrid::s4u::Mailbox::byName(alias), task, host, timeout, rate);
266 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
267 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
268 int (*match_fun)(void*,void*, smx_synchro_t),
269 void *match_data, void_f_pvoid_t cleanup, int detached)
271 simdata_task_t t_simdata = nullptr;
272 msg_process_t myself = SIMIX_process_self();
273 msg_mailbox_t mailbox = simgrid::s4u::Mailbox::byName(alias);
274 int call_end = TRACE_msg_task_put_start(task);
276 /* Prepare the task to send */
277 t_simdata = task->simdata;
278 t_simdata->sender = myself;
279 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host;
280 t_simdata->setUsed();
281 t_simdata->comm = nullptr;
282 msg_global->sent_msg++;
284 /* Send it by calling SIMIX network layer */
285 smx_synchro_t act = simcall_comm_isend(myself, mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate,
286 task, sizeof(void *), match_fun, cleanup, nullptr, match_data,detached);
287 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(act);
289 msg_comm_t comm = nullptr;
291 comm = xbt_new0(s_msg_comm_t, 1);
292 comm->task_sent = task;
293 comm->task_received = nullptr;
294 comm->status = MSG_OK;
298 if (TRACE_is_enabled())
299 simcall_set_category(act, task->category);
301 TRACE_msg_task_put_end();
306 /** \ingroup msg_task_usage
307 * \brief Sends a task on a mailbox.
309 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
311 * \param task a #msg_task_t to send on another location.
312 * \param alias name of the mailbox to sent the task to
313 * \return the msg_comm_t communication created
315 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
317 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
320 /** \ingroup msg_task_usage
321 * \brief Sends a task on a mailbox with a maximum rate
323 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
324 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
326 * \param task a #msg_task_t to send on another location.
327 * \param alias name of the mailbox to sent the task to
328 * \param maxrate the maximum communication rate for sending this task .
329 * \return the msg_comm_t communication created
331 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
333 task->simdata->rate = maxrate;
334 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
337 /** \ingroup msg_task_usage
338 * \brief Sends a task on a mailbox, with support for matching requests
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 * \param match_fun boolean function which parameters are:
345 * - match_data_provided_here
346 * - match_data_provided_by_other_side_if_any
347 * - the_smx_synchro_describing_the_other_side
348 * \param match_data user provided data passed to match_fun
349 * \return the msg_comm_t communication created
351 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
352 int (*match_fun)(void*, void*, smx_synchro_t), void *match_data)
354 return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
357 /** \ingroup msg_task_usage
358 * \brief Sends a task on a mailbox.
360 * This is a non blocking detached send function.
361 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
362 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
363 * usual. More details on this can be obtained on
364 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
365 * in the SimGrid-user mailing list archive.
367 * \param task a #msg_task_t to send on another location.
368 * \param alias name of the mailbox to sent the task to
369 * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
370 * (if nullptr, no function will be called)
372 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
374 MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
377 /** \ingroup msg_task_usage
378 * \brief Sends a task on a mailbox with a maximal rate.
380 * This is a non blocking detached send function.
381 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
382 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
383 * usual. More details on this can be obtained on
384 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
385 * in the SimGrid-user mailing list archive.
387 * \param task a #msg_task_t to send on another location.
388 * \param alias name of the mailbox to sent the task to
389 * \param cleanup a function to destroy the task if the
390 * communication fails, e.g. MSG_task_destroy
391 * (if nullptr, no function will be called)
392 * \param maxrate the maximum communication rate for sending this task
395 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
397 task->simdata->rate = maxrate;
398 MSG_task_dsend(task, alias, cleanup);
401 /** \ingroup msg_task_usage
402 * \brief Starts listening for receiving a task from an asynchronous communication.
404 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
406 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
407 * \param name of the mailbox to receive the task on
408 * \return the msg_comm_t communication created
410 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
412 return MSG_task_irecv_bounded(task, name, -1.0);
415 /** \ingroup msg_task_usage
416 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
418 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
419 * \param name of the mailbox to receive the task on
420 * \param rate limit the bandwidth to the given rate
421 * \return the msg_comm_t communication created
423 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
425 msg_mailbox_t mbox = simgrid::s4u::Mailbox::byName(name);
427 /* FIXME: these functions are not traceable */
429 xbt_assert(task, "Null pointer for the task storage");
432 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
434 /* Try to receive it by calling SIMIX network layer */
435 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
436 comm->task_sent = nullptr;
437 comm->task_received = task;
438 comm->status = MSG_OK;
439 comm->s_comm = simcall_comm_irecv(MSG_process_self(), mbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, rate);
444 /** \ingroup msg_task_usage
445 * \brief Checks whether a communication is done, and if yes, finalizes it.
446 * \param comm the communication to test
447 * \return TRUE if the communication is finished
448 * (but it may have failed, use MSG_comm_get_status() to know its status)
449 * or FALSE if the communication is not finished yet
450 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
452 int MSG_comm_test(msg_comm_t comm)
457 finished = simcall_comm_test(comm->s_comm);
458 if (finished && comm->task_received != nullptr) {
459 /* I am the receiver */
460 (*comm->task_received)->simdata->setNotUsed();
464 switch (e.category) {
466 comm->status = MSG_TRANSFER_FAILURE;
470 comm->status = MSG_TIMEOUT;
481 /** \ingroup msg_task_usage
482 * \brief This function checks if a communication is finished.
483 * \param comms a vector of communications
484 * \return the position of the finished communication if any
485 * (but it may have failed, use MSG_comm_get_status() to know its status),
486 * or -1 if none is finished
488 int MSG_comm_testany(xbt_dynar_t comms)
490 int finished_index = -1;
492 /* Create the equivalent array with SIMIX objects: */
493 std::vector<simgrid::kernel::activity::Synchro*> s_comms;
494 s_comms.reserve(xbt_dynar_length(comms));
497 xbt_dynar_foreach(comms, cursor, comm) {
498 s_comms.push_back(comm->s_comm);
501 msg_error_t status = MSG_OK;
503 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
506 switch (e.category) {
508 finished_index = e.value;
509 status = MSG_TRANSFER_FAILURE;
512 finished_index = e.value;
513 status = MSG_TIMEOUT;
520 if (finished_index != -1) {
521 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
522 /* the communication is finished */
523 comm->status = status;
525 if (status == MSG_OK && comm->task_received != nullptr) {
526 /* I am the receiver */
527 (*comm->task_received)->simdata->setNotUsed();
531 return finished_index;
534 /** \ingroup msg_task_usage
535 * \brief Destroys a communication.
536 * \param comm the communication to destroy.
538 void MSG_comm_destroy(msg_comm_t comm)
543 /** \ingroup msg_task_usage
544 * \brief Wait for the completion of a communication.
546 * It takes two parameters.
547 * \param comm the communication to wait.
548 * \param timeout Wait until the communication terminates or the timeout occurs.
549 * You can provide a -1 timeout to obtain an infinite timeout.
550 * \return msg_error_t
552 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
555 simcall_comm_wait(comm->s_comm, timeout);
557 if (comm->task_received != nullptr) {
558 /* I am the receiver */
559 (*comm->task_received)->simdata->setNotUsed();
562 /* FIXME: these functions are not traceable */
565 switch (e.category) {
567 comm->status = MSG_TRANSFER_FAILURE;
570 comm->status = MSG_TIMEOUT;
580 /** \ingroup msg_task_usage
581 * \brief This function is called by a sender and permit to wait for each communication
583 * \param comm a vector of communication
584 * \param nb_elem is the size of the comm vector
585 * \param timeout for each call of MSG_comm_wait
587 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
590 for (i = 0; i < nb_elem; i++) {
591 MSG_comm_wait(comm[i], timeout);
595 /** \ingroup msg_task_usage
596 * \brief This function waits for the first communication finished in a list.
597 * \param comms a vector of communications
598 * \return the position of the first finished communication
599 * (but it may have failed, use MSG_comm_get_status() to know its status)
601 int MSG_comm_waitany(xbt_dynar_t comms)
603 int finished_index = -1;
605 /* create the equivalent dynar with SIMIX objects */
606 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), nullptr);
609 xbt_dynar_foreach(comms, cursor, comm) {
610 xbt_dynar_push(s_comms, &comm->s_comm);
613 msg_error_t status = MSG_OK;
615 finished_index = simcall_comm_waitany(s_comms, -1);
618 switch (e.category) {
620 finished_index = e.value;
621 status = MSG_TRANSFER_FAILURE;
624 finished_index = e.value;
625 status = MSG_TIMEOUT;
632 xbt_assert(finished_index != -1, "WaitAny returned -1");
633 xbt_dynar_free(&s_comms);
635 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
636 /* the communication is finished */
637 comm->status = status;
639 if (comm->task_received != nullptr) {
640 /* I am the receiver */
641 (*comm->task_received)->simdata->setNotUsed();
644 return finished_index;
648 * \ingroup msg_task_usage
649 * \brief Returns the error (if any) that occurred during a finished communication.
650 * \param comm a finished communication
651 * \return the status of the communication, or #MSG_OK if no error occurred
652 * during the communication
654 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
659 /** \ingroup msg_task_usage
660 * \brief Get a task (#msg_task_t) from a communication
662 * \param comm the communication where to get the task
663 * \return the task from the communication
665 msg_task_t MSG_comm_get_task(msg_comm_t comm)
667 xbt_assert(comm, "Invalid parameter");
669 return comm->task_received ? *comm->task_received : comm->task_sent;
673 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
674 * \param comm the comm
675 * \param buff the data copied
676 * \param buff_size size of the buffer
678 void MSG_comm_copy_data_from_SIMIX(smx_synchro_t synchro, void* buff, size_t buff_size)
680 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
682 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
684 // notify the user callback if any
685 if (msg_global->task_copy_callback) {
686 msg_task_t task = (msg_task_t) buff;
687 msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
691 /** \ingroup msg_task_usage
692 * \brief Sends a task to a mailbox
694 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
695 * side if #MSG_task_receive is used).
696 * See #MSG_task_isend for sending tasks asynchronously.
698 * \param task the task to be sent
699 * \param alias the mailbox name to where the task is sent
701 * \return Returns #MSG_OK if the task was successfully sent,
702 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
704 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
706 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
707 return MSG_task_send_with_timeout(task, alias, -1);
710 /** \ingroup msg_task_usage
711 * \brief Sends a task to a mailbox with a maximum rate
713 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
714 * the application to limit the bandwidth utilization of network links when sending the task.
716 * \param task the task to be sent
717 * \param alias the mailbox name to where the task is sent
718 * \param maxrate the maximum communication rate for sending this task
720 * \return Returns #MSG_OK if the task was successfully sent,
721 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
723 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
725 task->simdata->rate = maxrate;
726 return MSG_task_send(task, alias);
729 /** \ingroup msg_task_usage
730 * \brief Sends a task to a mailbox with a timeout
732 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
734 * \param task the task to be sent
735 * \param alias the mailbox name to where the task is sent
736 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
738 * \return Returns #MSG_OK if the task was successfully sent,
739 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
741 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
743 msg_error_t ret = MSG_OK;
744 simdata_task_t t_simdata = nullptr;
745 msg_process_t process = MSG_process_self();
746 simdata_process_t p_simdata = (simdata_process_t) SIMIX_process_self_get_data();
747 msg_mailbox_t mailbox = simgrid::s4u::Mailbox::byName(alias);
749 int call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
751 /* Prepare the task to send */
752 t_simdata = task->simdata;
753 t_simdata->sender = process;
754 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host;
756 t_simdata->setUsed();
758 t_simdata->comm = nullptr;
759 msg_global->sent_msg++;
761 p_simdata->waiting_task = task;
763 /* Try to send it by calling SIMIX network layer */
765 smx_synchro_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
766 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
767 t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
768 if (TRACE_is_enabled())
769 simcall_set_category(comm, task->category);
770 t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(comm);
771 simcall_comm_wait(comm, timeout);
774 switch (e.category) {
776 ret = MSG_HOST_FAILURE;
779 ret = MSG_TRANSFER_FAILURE;
788 /* If the send failed, it is not used anymore */
789 t_simdata->setNotUsed();
792 p_simdata->waiting_task = nullptr;
794 TRACE_msg_task_put_end();
798 /** \ingroup msg_task_usage
799 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
801 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
803 * \param task the task to be sent
804 * \param alias the mailbox name to where the task is sent
805 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
806 * \param maxrate the maximum communication rate for sending this task
808 * \return Returns #MSG_OK if the task was successfully sent,
809 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
811 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
813 task->simdata->rate = maxrate;
814 return MSG_task_send_with_timeout(task, alias, timeout);
817 /** \ingroup msg_task_usage
818 * \brief Check if there is a communication going on in a mailbox.
820 * \param alias the name of the mailbox to be considered
822 * \return Returns 1 if there is a communication, 0 otherwise
824 int MSG_task_listen(const char *alias)
826 msg_mailbox_t mbox = simgrid::s4u::Mailbox::byName(alias);
827 return !mbox->empty() ||
828 (mbox->getImpl()->permanent_receiver && !mbox->getImpl()->done_comm_queue.empty());
831 /** \ingroup msg_task_usage
832 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
834 * \param alias the name of the mailbox to be considered
836 * \return Returns the PID of sender process,
837 * -1 if there is no communication in the mailbox.
839 int MSG_task_listen_from(const char *alias)
841 msg_mailbox_t mbox = simgrid::s4u::Mailbox::byName(alias);
842 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mbox->front());
847 return MSG_process_get_PID( static_cast<msg_task_t>(comm->src_data)->simdata->sender );
850 /** \ingroup msg_task_usage
851 * \brief Sets the tracing category of a task.
853 * This function should be called after the creation of a MSG task, to define the category of that task. The
854 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
855 * parameter category must contain a category that was previously declared with the function #TRACE_category
856 * (or with #TRACE_category_with_color).
858 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
860 * \param task the task that is going to be categorized
861 * \param category the name of the category to be associated to the task
863 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
865 void MSG_task_set_category (msg_task_t task, const char *category)
867 TRACE_msg_set_task_category (task, category);
870 /** \ingroup msg_task_usage
872 * \brief Gets the current tracing category of a task.
874 * \param task the task to be considered
876 * \see MSG_task_set_category
878 * \return Returns the name of the tracing category of the given task, nullptr otherwise
880 const char *MSG_task_get_category (msg_task_t task)
882 return task->category;
886 * \brief Returns the value of a given AS or router property
888 * \param asr the name of a router or AS
889 * \param name a property name
890 * \return value of a property (or nullptr if property not set)
892 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
894 return (char*) xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
898 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
901 * \param asr the name of a router or AS
902 * \return a dict containing the properties
904 xbt_dict_t MSG_as_router_get_properties(const char* asr)
906 return (simcall_asr_get_properties(asr));
910 * \brief Change the value of a given AS or router
912 * \param asr the name of a router or AS
913 * \param name a property name
914 * \param value what to change the property to
915 * \param free_ctn the freeing function to use to kill the value on need
917 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
918 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);