1 /* Copyright (c) 2004-2017. 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/kernel/activity/ExecImpl.hpp"
9 #include "src/msg/msg_private.hpp"
10 #include "src/simix/smx_private.hpp" /* MSG_task_listen looks inside the rdv directly. Not clean. */
12 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 return MSG_parallel_task_execute(task);
28 /** \ingroup msg_task_usage
29 * \brief Executes a parallel task and waits for its termination.
31 * \param task a #msg_task_t to execute on the location on which the process is running.
33 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
34 * or #MSG_HOST_FAILURE otherwise
36 msg_error_t MSG_parallel_task_execute(msg_task_t task)
38 return MSG_parallel_task_execute_with_timeout(task, -1);
41 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
43 simdata_task_t simdata = task->simdata;
44 e_smx_state_t comp_state;
45 msg_error_t status = MSG_OK;
47 TRACE_msg_task_execute_start(task);
49 xbt_assert((not simdata->compute) && not task->simdata->isused,
50 "This task is executed somewhere else. Go fix your code!");
52 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
54 if (simdata->flops_amount <= 0.0 && not simdata->host_nb) {
55 TRACE_msg_task_execute_end(task);
62 if (simdata->host_nb > 0) {
64 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
65 task->name, simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
66 simdata->bytes_parallel_amount, -1.0, timeout));
67 XBT_DEBUG("Parallel execution action created: %p", simdata->compute.get());
69 simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
70 simcall_execution_start(task->name, simdata->flops_amount, simdata->priority, simdata->bound,
71 MSG_process_get_host(MSG_process_self())));
73 simcall_set_category(simdata->compute, task->category);
74 comp_state = simcall_execution_wait(simdata->compute);
76 simdata->setNotUsed();
78 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
83 status = MSG_TASK_CANCELED;
86 status = MSG_HOST_FAILURE;
96 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
97 simdata->flops_amount = 0.0;
98 simdata->comm = nullptr;
99 simdata->compute = nullptr;
100 TRACE_msg_task_execute_end(task);
105 /** \ingroup msg_task_usage
106 * \brief Sleep for the specified number of seconds
108 * Makes the current process sleep until \a time seconds have elapsed.
110 * \param nb_sec a number of second
112 msg_error_t MSG_process_sleep(double nb_sec)
114 msg_error_t status = MSG_OK;
116 TRACE_msg_process_sleep_in(MSG_process_self());
119 simcall_process_sleep(nb_sec);
122 if (e.category == cancel_error) {
123 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
124 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
125 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
126 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
127 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
128 // and did not change anythings at the C level.
129 // 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)
130 status = MSG_TASK_CANCELED;
135 TRACE_msg_process_sleep_out(MSG_process_self());
139 /** \ingroup msg_task_usage
140 * \brief Receives a task from a mailbox.
142 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
143 * for receiving tasks asynchronously.
145 * \param task a memory location for storing a #msg_task_t.
146 * \param alias name of the mailbox to receive the task from
149 * #MSG_OK if the task was successfully received,
150 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
152 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
154 return MSG_task_receive_with_timeout(task, alias, -1);
157 /** \ingroup msg_task_usage
158 * \brief Receives a task from a mailbox at a given rate.
160 * \param task a memory location for storing a #msg_task_t.
161 * \param alias name of the mailbox to receive the task from
162 * \param rate limit the reception to rate bandwidth
165 * #MSG_OK if the task was successfully received,
166 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
168 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
170 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
173 /** \ingroup msg_task_usage
174 * \brief Receives a task from a mailbox with a given timeout.
176 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
177 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
178 * to obtain an infinite timeout.
180 * \param task a memory location for storing a #msg_task_t.
181 * \param alias name of the mailbox to receive the task from
182 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
185 * #MSG_OK if the task was successfully received,
186 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
188 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
190 return MSG_task_receive_ext(task, alias, timeout, nullptr);
193 /** \ingroup msg_task_usage
194 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
196 * \param task a memory location for storing a #msg_task_t.
197 * \param alias name of the mailbox to receive the task from
198 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
199 * \param rate limit the reception to rate bandwidth
202 * #MSG_OK if the task was successfully received,
203 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
205 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
207 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
210 /** \ingroup msg_task_usage
211 * \brief Receives a task from a mailbox from a specific host with a given timeout.
213 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
214 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
215 * to obtain an infinite timeout.
217 * \param task a memory location for storing a #msg_task_t.
218 * \param alias name of the mailbox to receive the task from
219 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
220 * \param host a #msg_host_t host from where the task was sent
223 * #MSG_OK if the task was successfully received,
224 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
226 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
228 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
229 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
232 /** \ingroup msg_task_usage
233 * \brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
235 * \param task a memory location for storing a #msg_task_t.
236 * \param alias name of the mailbox to receive the task from
237 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
238 * \param host a #msg_host_t host from where the task was sent
239 * \param rate limit the reception to rate bandwidth
242 * #MSG_OK if the task was successfully received,
243 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
245 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
248 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
249 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
250 msg_error_t ret = MSG_OK;
251 /* We no longer support getting a task from a specific host */
255 TRACE_msg_task_get_start();
258 xbt_assert(task, "Null pointer for the task storage");
261 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
263 /* Try to receive it by calling SIMIX network layer */
265 simcall_comm_recv(MSG_process_self()->getImpl(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
266 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->getCname());
267 (*task)->simdata->setNotUsed();
270 switch (e.category) {
273 ret = MSG_HOST_FAILURE;
276 ret = MSG_TRANSFER_FAILURE;
286 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
287 TRACE_msg_task_get_end(*task);
292 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
293 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
294 int (*match_fun)(void*, void*, void*), void* match_data,
295 void_f_pvoid_t cleanup, int detached)
297 simdata_task_t t_simdata = nullptr;
298 msg_process_t myself = MSG_process_self();
299 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
300 int call_end = TRACE_msg_task_put_start(task);
302 /* Prepare the task to send */
303 t_simdata = task->simdata;
304 t_simdata->sender = myself;
305 t_simdata->source = MSG_host_self();
306 t_simdata->setUsed();
307 t_simdata->comm = nullptr;
308 msg_global->sent_msg++;
310 /* Send it by calling SIMIX network layer */
312 simcall_comm_isend(myself->getImpl(), mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate, task,
313 sizeof(void*), (simix_match_func_t)match_fun, cleanup, nullptr, match_data, detached);
314 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
316 msg_comm_t comm = nullptr;
318 comm = new simgrid::msg::Comm(task, nullptr, act);
321 if (TRACE_is_enabled())
322 simcall_set_category(act, task->category);
324 TRACE_msg_task_put_end();
329 /** \ingroup msg_task_usage
330 * \brief Sends a task on a mailbox.
332 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
334 * \param task a #msg_task_t to send on another location.
335 * \param alias name of the mailbox to sent the task to
336 * \return the msg_comm_t communication created
338 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
340 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
343 /** \ingroup msg_task_usage
344 * \brief Sends a task on a mailbox with a maximum rate
346 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
347 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
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 * \param maxrate the maximum communication rate for sending this task .
352 * \return the msg_comm_t communication created
354 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
356 task->simdata->rate = maxrate;
357 return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
360 /** \ingroup msg_task_usage
361 * \brief Sends a task on a mailbox, with support for matching requests
363 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
365 * \param task a #msg_task_t to send on another location.
366 * \param alias name of the mailbox to sent the task to
367 * \param match_fun boolean function which parameters are:
368 * - match_data_provided_here
369 * - match_data_provided_by_other_side_if_any
370 * - the_smx_synchro_describing_the_other_side
371 * \param match_data user provided data passed to match_fun
372 * \return the msg_comm_t communication created
374 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char* alias, int (*match_fun)(void*, void*, void*),
377 return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
380 /** \ingroup msg_task_usage
381 * \brief Sends a task on a mailbox.
383 * This is a non blocking detached send function.
384 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
385 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
386 * usual. More details on this can be obtained on
387 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
388 * in the SimGrid-user mailing list archive.
390 * \param task a #msg_task_t to send on another location.
391 * \param alias name of the mailbox to sent the task to
392 * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
393 * (if nullptr, no function will be called)
395 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
397 MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
400 /** \ingroup msg_task_usage
401 * \brief Sends a task on a mailbox with a maximal rate.
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
413 * communication fails, e.g. MSG_task_destroy
414 * (if nullptr, no function will be called)
415 * \param maxrate the maximum communication rate for sending this task
418 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
420 task->simdata->rate = maxrate;
421 MSG_task_dsend(task, alias, cleanup);
424 /** \ingroup msg_task_usage
425 * \brief Starts listening for receiving a task from an asynchronous communication.
427 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
429 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
430 * \param name of the mailbox to receive the task on
431 * \return the msg_comm_t communication created
433 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
435 return MSG_task_irecv_bounded(task, name, -1.0);
438 /** \ingroup msg_task_usage
439 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
441 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
442 * \param name of the mailbox to receive the task on
443 * \param rate limit the bandwidth to the given rate
444 * \return the msg_comm_t communication created
446 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
448 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
450 /* FIXME: these functions are not traceable */
452 xbt_assert(task, "Null pointer for the task storage");
455 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
457 /* Try to receive it by calling SIMIX network layer */
459 new simgrid::msg::Comm(nullptr, task, simcall_comm_irecv(SIMIX_process_self(), mbox->getImpl(), task, nullptr,
460 nullptr, nullptr, nullptr, rate));
465 /** \ingroup msg_task_usage
466 * \brief Checks whether a communication is done, and if yes, finalizes it.
467 * \param comm the communication to test
468 * \return TRUE if the communication is finished
469 * (but it may have failed, use MSG_comm_get_status() to know its status)
470 * or FALSE if the communication is not finished yet
471 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
473 int MSG_comm_test(msg_comm_t comm)
478 finished = simcall_comm_test(comm->s_comm);
479 if (finished && comm->task_received != nullptr) {
480 /* I am the receiver */
481 (*comm->task_received)->simdata->setNotUsed();
485 switch (e.category) {
487 comm->status = MSG_TRANSFER_FAILURE;
491 comm->status = MSG_TIMEOUT;
502 /** \ingroup msg_task_usage
503 * \brief This function checks if a communication is finished.
504 * \param comms a vector of communications
505 * \return the position of the finished communication if any
506 * (but it may have failed, use MSG_comm_get_status() to know its status),
507 * or -1 if none is finished
509 int MSG_comm_testany(xbt_dynar_t comms)
511 int finished_index = -1;
513 /* Create the equivalent array with SIMIX objects: */
514 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
515 s_comms.reserve(xbt_dynar_length(comms));
518 xbt_dynar_foreach(comms, cursor, comm) {
519 s_comms.push_back(comm->s_comm);
522 msg_error_t status = MSG_OK;
524 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
527 switch (e.category) {
529 finished_index = e.value;
530 status = MSG_TRANSFER_FAILURE;
533 finished_index = e.value;
534 status = MSG_TIMEOUT;
541 if (finished_index != -1) {
542 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
543 /* the communication is finished */
544 comm->status = status;
546 if (status == MSG_OK && comm->task_received != nullptr) {
547 /* I am the receiver */
548 (*comm->task_received)->simdata->setNotUsed();
552 return finished_index;
555 /** \ingroup msg_task_usage
556 * \brief Destroys a communication.
557 * \param comm the communication to destroy.
559 void MSG_comm_destroy(msg_comm_t comm)
564 /** \ingroup msg_task_usage
565 * \brief Wait for the completion of a communication.
567 * It takes two parameters.
568 * \param comm the communication to wait.
569 * \param timeout Wait until the communication terminates or the timeout occurs.
570 * You can provide a -1 timeout to obtain an infinite timeout.
571 * \return msg_error_t
573 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
576 simcall_comm_wait(comm->s_comm, timeout);
578 if (comm->task_received != nullptr) {
579 /* I am the receiver */
580 (*comm->task_received)->simdata->setNotUsed();
583 /* FIXME: these functions are not traceable */
586 switch (e.category) {
588 comm->status = MSG_TRANSFER_FAILURE;
591 comm->status = MSG_TIMEOUT;
601 /** \ingroup msg_task_usage
602 * \brief This function is called by a sender and permit to wait for each communication
604 * \param comm a vector of communication
605 * \param nb_elem is the size of the comm vector
606 * \param timeout for each call of MSG_comm_wait
608 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
610 for (int i = 0; i < nb_elem; i++)
611 MSG_comm_wait(comm[i], timeout);
614 /** \ingroup msg_task_usage
615 * \brief This function waits for the first communication finished in a list.
616 * \param comms a vector of communications
617 * \return the position of the first finished communication
618 * (but it may have failed, use MSG_comm_get_status() to know its status)
620 int MSG_comm_waitany(xbt_dynar_t comms)
622 int finished_index = -1;
624 /* create the equivalent dynar with SIMIX objects */
625 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
626 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
630 xbt_dynar_foreach(comms, cursor, comm) {
631 intrusive_ptr_add_ref(comm->s_comm.get());
632 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
635 msg_error_t status = MSG_OK;
637 finished_index = simcall_comm_waitany(s_comms, -1);
640 switch (e.category) {
642 finished_index = e.value;
643 status = MSG_TRANSFER_FAILURE;
646 finished_index = e.value;
647 status = MSG_TIMEOUT;
654 xbt_assert(finished_index != -1, "WaitAny returned -1");
655 xbt_dynar_free(&s_comms);
657 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
658 /* the communication is finished */
659 comm->status = status;
661 if (comm->task_received != nullptr) {
662 /* I am the receiver */
663 (*comm->task_received)->simdata->setNotUsed();
666 return finished_index;
670 * \ingroup msg_task_usage
671 * \brief Returns the error (if any) that occurred during a finished communication.
672 * \param comm a finished communication
673 * \return the status of the communication, or #MSG_OK if no error occurred
674 * during the communication
676 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
681 /** \ingroup msg_task_usage
682 * \brief Get a task (#msg_task_t) from a communication
684 * \param comm the communication where to get the task
685 * \return the task from the communication
687 msg_task_t MSG_comm_get_task(msg_comm_t comm)
689 xbt_assert(comm, "Invalid parameter");
691 return comm->task_received ? *comm->task_received : comm->task_sent;
695 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
696 * \param synchro the comm
697 * \param buff the data copied
698 * \param buff_size size of the buffer
700 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
702 simgrid::kernel::activity::CommImplPtr comm =
703 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
705 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
707 // notify the user callback if any
708 if (msg_global->task_copy_callback) {
709 msg_task_t task = static_cast<msg_task_t>(buff);
710 msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
714 /** \ingroup msg_task_usage
715 * \brief Sends a task to a mailbox
717 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
718 * side if #MSG_task_receive is used).
719 * See #MSG_task_isend for sending tasks asynchronously.
721 * \param task the task to be sent
722 * \param alias the mailbox name to where the task is sent
724 * \return Returns #MSG_OK if the task was successfully sent,
725 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
727 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
729 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
730 return MSG_task_send_with_timeout(task, alias, -1);
733 /** \ingroup msg_task_usage
734 * \brief Sends a task to a mailbox with a maximum rate
736 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
737 * the application to limit the bandwidth utilization of network links when sending the task.
739 * \param task the task to be sent
740 * \param alias the mailbox name to where the task is sent
741 * \param maxrate the maximum communication rate for sending this task
743 * \return Returns #MSG_OK if the task was successfully sent,
744 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
746 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
748 task->simdata->rate = maxrate;
749 return MSG_task_send(task, alias);
752 /** \ingroup msg_task_usage
753 * \brief Sends a task to a mailbox with a timeout
755 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
757 * \param task the task to be sent
758 * \param alias the mailbox name to where the task is sent
759 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
761 * \return Returns #MSG_OK if the task was successfully sent,
762 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
764 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
766 msg_error_t ret = MSG_OK;
767 simdata_task_t t_simdata = nullptr;
768 msg_process_t process = MSG_process_self();
769 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
771 int call_end = TRACE_msg_task_put_start(task);
773 /* Prepare the task to send */
774 t_simdata = task->simdata;
775 t_simdata->sender = process;
776 t_simdata->source = MSG_host_self();
778 t_simdata->setUsed();
780 t_simdata->comm = nullptr;
781 msg_global->sent_msg++;
783 /* Try to send it by calling SIMIX network layer */
785 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
786 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
787 t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
788 if (TRACE_is_enabled())
789 simcall_set_category(comm, task->category);
790 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
791 simcall_comm_wait(comm, timeout);
794 switch (e.category) {
796 ret = MSG_HOST_FAILURE;
799 ret = MSG_TRANSFER_FAILURE;
808 /* If the send failed, it is not used anymore */
809 t_simdata->setNotUsed();
813 TRACE_msg_task_put_end();
817 /** \ingroup msg_task_usage
818 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
820 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
822 * \param task the task to be sent
823 * \param alias the mailbox name to where the task is sent
824 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
825 * \param maxrate the maximum communication rate for sending this task
827 * \return Returns #MSG_OK if the task was successfully sent,
828 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
830 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
832 task->simdata->rate = maxrate;
833 return MSG_task_send_with_timeout(task, alias, timeout);
836 /** \ingroup msg_task_usage
837 * \brief Check if there is a communication going on in a mailbox.
839 * \param alias the name of the mailbox to be considered
841 * \return Returns 1 if there is a communication, 0 otherwise
843 int MSG_task_listen(const char *alias)
845 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
846 return mbox->listen() ? 1 : 0;
849 /** \ingroup msg_task_usage
850 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
852 * \param alias the name of the mailbox to be considered
854 * \return Returns the PID of sender process,
855 * -1 if there is no communication in the mailbox.
857 int MSG_task_listen_from(const char *alias)
859 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
860 simgrid::kernel::activity::CommImplPtr comm =
861 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
866 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
869 /** \ingroup msg_task_usage
870 * \brief Sets the tracing category of a task.
872 * This function should be called after the creation of a MSG task, to define the category of that task. The
873 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
874 * parameter category must contain a category that was previously declared with the function #TRACE_category
875 * (or with #TRACE_category_with_color).
877 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
879 * \param task the task that is going to be categorized
880 * \param category the name of the category to be associated to the task
882 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
884 void MSG_task_set_category (msg_task_t task, const char *category)
886 TRACE_msg_set_task_category (task, category);
889 /** \ingroup msg_task_usage
891 * \brief Gets the current tracing category of a task.
893 * \param task the task to be considered
895 * \see MSG_task_set_category
897 * \return Returns the name of the tracing category of the given task, nullptr otherwise
899 const char *MSG_task_get_category (msg_task_t task)
901 return task->category;