1 /* Copyright (c) 2004-2018. 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)");
14 /** \ingroup msg_task_usage
15 * \brief Executes a task and waits for its termination.
17 * This function is used for describing the behavior of a process. It takes only one parameter.
18 * \param task a #msg_task_t to execute on the location on which the process is running.
19 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
21 msg_error_t MSG_task_execute(msg_task_t task)
23 return MSG_parallel_task_execute(task);
26 /** \ingroup msg_task_usage
27 * \brief Executes a parallel task and waits for its termination.
29 * \param task a #msg_task_t to execute on the location on which the process is running.
31 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
32 * or #MSG_HOST_FAILURE otherwise
34 msg_error_t MSG_parallel_task_execute(msg_task_t task)
36 return MSG_parallel_task_execute_with_timeout(task, -1);
39 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
41 simdata_task_t simdata = task->simdata;
42 e_smx_state_t comp_state;
43 msg_error_t status = MSG_OK;
45 TRACE_msg_task_execute_start(task);
47 xbt_assert((not simdata->compute) && not task->simdata->isused,
48 "This task is executed somewhere else. Go fix your code!");
50 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
52 if (simdata->flops_amount <= 0.0 && not simdata->host_nb) {
53 TRACE_msg_task_execute_end(task);
60 if (simdata->host_nb > 0) {
62 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
63 task->name, simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
64 simdata->bytes_parallel_amount, -1.0, timeout));
65 XBT_DEBUG("Parallel execution action created: %p", simdata->compute.get());
67 simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
68 simcall_execution_start(task->name, simdata->flops_amount, simdata->priority, simdata->bound,
69 MSG_process_get_host(MSG_process_self())));
71 simcall_set_category(simdata->compute, task->category);
72 comp_state = simcall_execution_wait(simdata->compute);
74 simdata->setNotUsed();
76 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
81 status = MSG_TASK_CANCELED;
84 status = MSG_HOST_FAILURE;
94 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
95 simdata->flops_amount = 0.0;
96 simdata->comm = nullptr;
97 simdata->compute = nullptr;
98 TRACE_msg_task_execute_end(task);
103 /** \ingroup msg_task_usage
104 * \brief Sleep for the specified number of seconds
106 * Makes the current process sleep until \a time seconds have elapsed.
108 * \param nb_sec a number of second
110 msg_error_t MSG_process_sleep(double nb_sec)
112 msg_error_t status = MSG_OK;
114 if (TRACE_actor_is_enabled())
115 simgrid::instr::Container::byName(instr_pid(MSG_process_self()))->getState("ACTOR_STATE")->pushEvent("sleep");
118 simcall_process_sleep(nb_sec);
121 if (e.category == cancel_error) {
122 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
123 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
124 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
125 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
126 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
127 // and did not change anythings at the C level.
128 // 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)
129 status = MSG_TASK_CANCELED;
134 if (TRACE_actor_is_enabled())
135 simgrid::instr::Container::byName(instr_pid(MSG_process_self()))->getState("ACTOR_STATE")->popEvent();
140 /** \ingroup msg_task_usage
141 * \brief Receives a task from a mailbox.
143 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
144 * for receiving tasks asynchronously.
146 * \param task a memory location for storing a #msg_task_t.
147 * \param alias name of the mailbox to receive the task from
150 * #MSG_OK if the task was successfully received,
151 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
153 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
155 return MSG_task_receive_with_timeout(task, alias, -1);
158 /** \ingroup msg_task_usage
159 * \brief Receives a task from a mailbox at a given rate.
161 * \param task a memory location for storing a #msg_task_t.
162 * \param alias name of the mailbox to receive the task from
163 * \param rate limit the reception to rate bandwidth (byte/sec)
165 * The rate parameter can be used to receive a task with a limited
166 * bandwidth (smaller than the physical available value). Use
167 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
168 * rate value do disable this feature).
171 * #MSG_OK if the task was successfully received,
172 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
174 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
176 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
179 /** \ingroup msg_task_usage
180 * \brief Receives a task from a mailbox with a given timeout.
182 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
183 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
184 * to obtain an infinite timeout.
186 * \param task a memory location for storing a #msg_task_t.
187 * \param alias name of the mailbox to receive the task from
188 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
191 * #MSG_OK if the task was successfully received,
192 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
194 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
196 return MSG_task_receive_ext(task, alias, timeout, nullptr);
199 /** \ingroup msg_task_usage
200 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
202 * \param task a memory location for storing a #msg_task_t.
203 * \param alias name of the mailbox to receive the task from
204 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
205 * \param rate limit the reception to rate bandwidth (byte/sec)
207 * The rate parameter can be used to send a task with a limited
208 * bandwidth (smaller than the physical available value). Use
209 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
210 * rate value do disable this feature).
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 (byte/sec)
252 * The rate parameter can be used to receive a task with a limited
253 * bandwidth (smaller than the physical available value). Use
254 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
255 * rate value do disable this feature).
258 * #MSG_OK if the task was successfully received,
259 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
261 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
264 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
265 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
266 msg_error_t ret = MSG_OK;
267 /* We no longer support getting a task from a specific host */
271 TRACE_msg_task_get_start();
274 xbt_assert(task, "Null pointer for the task storage");
277 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
279 /* Try to receive it by calling SIMIX network layer */
281 simcall_comm_recv(MSG_process_self()->getImpl(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
282 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->getCname());
283 (*task)->simdata->setNotUsed();
286 switch (e.category) {
289 ret = MSG_HOST_FAILURE;
292 ret = MSG_TRANSFER_FAILURE;
302 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
303 TRACE_msg_task_get_end(*task);
308 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
309 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
310 void_f_pvoid_t cleanup, int detached)
312 simdata_task_t t_simdata = nullptr;
313 msg_process_t myself = MSG_process_self();
314 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
315 TRACE_msg_task_put_start(task);
317 /* Prepare the task to send */
318 t_simdata = task->simdata;
319 t_simdata->sender = myself;
320 t_simdata->source = MSG_host_self();
321 t_simdata->setUsed();
322 t_simdata->comm = nullptr;
323 msg_global->sent_msg++;
325 /* Send it by calling SIMIX network layer */
327 simcall_comm_isend(myself->getImpl(), mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate, task,
328 sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
329 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
331 msg_comm_t comm = nullptr;
333 comm = new simgrid::msg::Comm(task, nullptr, act);
336 if (TRACE_is_enabled())
337 simcall_set_category(act, task->category);
338 TRACE_msg_task_put_end();
343 /** \ingroup msg_task_usage
344 * \brief Sends a task on a mailbox.
346 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
348 * \param task a #msg_task_t to send on another location.
349 * \param alias name of the mailbox to sent the task to
350 * \return the msg_comm_t communication created
352 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
354 return MSG_task_isend_internal(task, alias, nullptr, 0);
357 /** \ingroup msg_task_usage
358 * \brief Sends a task on a mailbox with a maximum rate
360 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
361 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
363 * \param task a #msg_task_t to send on another location.
364 * \param alias name of the mailbox to sent the task to
365 * \param maxrate the maximum communication rate for sending this task (byte/sec).
366 * \return the msg_comm_t communication created
368 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
370 task->simdata->rate = maxrate;
371 return MSG_task_isend_internal(task, alias, nullptr, 0);
374 /** \ingroup msg_task_usage
375 * \brief Sends a task on a mailbox.
377 * This is a non blocking detached send function.
378 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
379 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
380 * usual. More details on this can be obtained on
381 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
382 * in the SimGrid-user mailing list archive.
384 * \param task a #msg_task_t to send on another location.
385 * \param alias name of the mailbox to sent the task to
386 * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
387 * (if nullptr, no function will be called)
389 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
391 MSG_task_isend_internal(task, alias, cleanup, 1);
394 /** \ingroup msg_task_usage
395 * \brief Sends a task on a mailbox with a maximal rate.
397 * This is a non blocking detached send function.
398 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
399 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
400 * usual. More details on this can be obtained on
401 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
402 * in the SimGrid-user mailing list archive.
404 * The rate parameter can be used to send a task with a limited
405 * bandwidth (smaller than the physical available value). Use
406 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
407 * value do disable this feature).
409 * \param task a #msg_task_t to send on another location.
410 * \param alias name of the mailbox to sent the task to
411 * \param cleanup a function to destroy the task if the
412 * communication fails, e.g. MSG_task_destroy
413 * (if nullptr, no function will be called)
414 * \param maxrate the maximum communication rate for sending this task (byte/sec)
417 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
419 task->simdata->rate = maxrate;
420 MSG_task_dsend(task, alias, cleanup);
423 /** \ingroup msg_task_usage
424 * \brief Starts listening for receiving a task from an asynchronous communication.
426 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
428 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
429 * \param name of the mailbox to receive the task on
430 * \return the msg_comm_t communication created
432 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
434 return MSG_task_irecv_bounded(task, name, -1.0);
437 /** \ingroup msg_task_usage
438 * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
440 * The rate parameter can be used to receive a task with a limited
441 * bandwidth (smaller than the physical available value). Use
442 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
443 * value do disable this feature).
445 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
446 * \param name of the mailbox to receive the task on
447 * \param rate limit the bandwidth to the given rate (byte/sec)
448 * \return the msg_comm_t communication created
450 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
452 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
454 /* FIXME: these functions are not traceable */
456 xbt_assert(task, "Null pointer for the task storage");
459 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
461 /* Try to receive it by calling SIMIX network layer */
463 new simgrid::msg::Comm(nullptr, task, simcall_comm_irecv(SIMIX_process_self(), mbox->getImpl(), task, nullptr,
464 nullptr, nullptr, nullptr, rate));
469 /** \ingroup msg_task_usage
470 * \brief Checks whether a communication is done, and if yes, finalizes it.
471 * \param comm the communication to test
472 * \return 'true' if the communication is finished
473 * (but it may have failed, use MSG_comm_get_status() to know its status)
474 * or 'false' if the communication is not finished yet
475 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
477 int MSG_comm_test(msg_comm_t comm)
479 bool finished = false;
482 finished = simcall_comm_test(comm->s_comm);
483 if (finished && comm->task_received != nullptr) {
484 /* I am the receiver */
485 (*comm->task_received)->simdata->setNotUsed();
489 switch (e.category) {
491 comm->status = MSG_TRANSFER_FAILURE;
495 comm->status = MSG_TIMEOUT;
506 /** \ingroup msg_task_usage
507 * \brief This function checks if a communication is finished.
508 * \param comms a vector of communications
509 * \return the position of the finished communication if any
510 * (but it may have failed, use MSG_comm_get_status() to know its status),
511 * or -1 if none is finished
513 int MSG_comm_testany(xbt_dynar_t comms)
515 int finished_index = -1;
517 /* Create the equivalent array with SIMIX objects: */
518 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
519 s_comms.reserve(xbt_dynar_length(comms));
522 xbt_dynar_foreach(comms, cursor, comm) {
523 s_comms.push_back(comm->s_comm);
526 msg_error_t status = MSG_OK;
528 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
531 switch (e.category) {
533 finished_index = e.value;
534 status = MSG_TRANSFER_FAILURE;
537 finished_index = e.value;
538 status = MSG_TIMEOUT;
545 if (finished_index != -1) {
546 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
547 /* the communication is finished */
548 comm->status = status;
550 if (status == MSG_OK && comm->task_received != nullptr) {
551 /* I am the receiver */
552 (*comm->task_received)->simdata->setNotUsed();
556 return finished_index;
559 /** \ingroup msg_task_usage
560 * \brief Destroys a communication.
561 * \param comm the communication to destroy.
563 void MSG_comm_destroy(msg_comm_t comm)
568 /** \ingroup msg_task_usage
569 * \brief Wait for the completion of a communication.
571 * It takes two parameters.
572 * \param comm the communication to wait.
573 * \param timeout Wait until the communication terminates or the timeout occurs.
574 * You can provide a -1 timeout to obtain an infinite timeout.
575 * \return msg_error_t
577 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
580 simcall_comm_wait(comm->s_comm, timeout);
582 if (comm->task_received != nullptr) {
583 /* I am the receiver */
584 (*comm->task_received)->simdata->setNotUsed();
587 /* FIXME: these functions are not traceable */
590 switch (e.category) {
592 comm->status = MSG_TRANSFER_FAILURE;
595 comm->status = MSG_TIMEOUT;
605 /** \ingroup msg_task_usage
606 * \brief This function is called by a sender and permit to wait for each communication
608 * \param comm a vector of communication
609 * \param nb_elem is the size of the comm vector
610 * \param timeout for each call of MSG_comm_wait
612 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
614 for (int i = 0; i < nb_elem; i++)
615 MSG_comm_wait(comm[i], timeout);
618 /** \ingroup msg_task_usage
619 * \brief This function waits for the first communication finished in a list.
620 * \param comms a vector of communications
621 * \return the position of the first finished communication
622 * (but it may have failed, use MSG_comm_get_status() to know its status)
624 int MSG_comm_waitany(xbt_dynar_t comms)
626 int finished_index = -1;
628 /* create the equivalent dynar with SIMIX objects */
629 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
630 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
634 xbt_dynar_foreach(comms, cursor, comm) {
635 intrusive_ptr_add_ref(comm->s_comm.get());
636 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
639 msg_error_t status = MSG_OK;
641 finished_index = simcall_comm_waitany(s_comms, -1);
644 switch (e.category) {
646 finished_index = e.value;
647 status = MSG_TRANSFER_FAILURE;
650 finished_index = e.value;
651 status = MSG_TIMEOUT;
658 xbt_assert(finished_index != -1, "WaitAny returned -1");
659 xbt_dynar_free(&s_comms);
661 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
662 /* the communication is finished */
663 comm->status = status;
665 if (comm->task_received != nullptr) {
666 /* I am the receiver */
667 (*comm->task_received)->simdata->setNotUsed();
670 return finished_index;
674 * \ingroup msg_task_usage
675 * \brief Returns the error (if any) that occurred during a finished communication.
676 * \param comm a finished communication
677 * \return the status of the communication, or #MSG_OK if no error occurred
678 * during the communication
680 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
685 /** \ingroup msg_task_usage
686 * \brief Get a task (#msg_task_t) from a communication
688 * \param comm the communication where to get the task
689 * \return the task from the communication
691 msg_task_t MSG_comm_get_task(msg_comm_t comm)
693 xbt_assert(comm, "Invalid parameter");
695 return comm->task_received ? *comm->task_received : comm->task_sent;
699 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
700 * \param synchro the comm
701 * \param buff the data copied
702 * \param buff_size size of the buffer
704 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
706 simgrid::kernel::activity::CommImplPtr comm =
707 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
709 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
711 // notify the user callback if any
712 if (msg_global->task_copy_callback) {
713 msg_task_t task = static_cast<msg_task_t>(buff);
714 msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
718 /** \ingroup msg_task_usage
719 * \brief Sends a task to a mailbox
721 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
722 * side if #MSG_task_receive is used).
723 * See #MSG_task_isend for sending tasks asynchronously.
725 * \param task the task to be sent
726 * \param alias the mailbox name to where the task is sent
728 * \return Returns #MSG_OK if the task was successfully sent,
729 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
731 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
733 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
734 return MSG_task_send_with_timeout(task, alias, -1);
737 /** \ingroup msg_task_usage
738 * \brief Sends a task to a mailbox with a maximum rate
740 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
741 * the application to limit the bandwidth utilization of network links when sending the task.
743 * The maxrate parameter can be used to send a task with a limited
744 * bandwidth (smaller than the physical available value). Use
745 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
746 * value do disable this feature).
748 * \param task the task to be sent
749 * \param alias the mailbox name to where the task is sent
750 * \param maxrate the maximum communication rate for sending this task (byte/sec)
752 * \return Returns #MSG_OK if the task was successfully sent,
753 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
755 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
757 task->simdata->rate = maxrate;
758 return MSG_task_send(task, alias);
761 /** \ingroup msg_task_usage
762 * \brief Sends a task to a mailbox with a timeout
764 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
766 * \param task the task to be sent
767 * \param alias the mailbox name to where the task is sent
768 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
770 * \return Returns #MSG_OK if the task was successfully sent,
771 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
773 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
775 msg_error_t ret = MSG_OK;
776 simdata_task_t t_simdata = nullptr;
777 msg_process_t process = MSG_process_self();
778 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
780 TRACE_msg_task_put_start(task);
782 /* Prepare the task to send */
783 t_simdata = task->simdata;
784 t_simdata->sender = process;
785 t_simdata->source = MSG_host_self();
787 t_simdata->setUsed();
789 t_simdata->comm = nullptr;
790 msg_global->sent_msg++;
792 /* Try to send it by calling SIMIX network layer */
794 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
795 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate, task,
796 sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
797 if (TRACE_is_enabled())
798 simcall_set_category(comm, task->category);
799 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
800 simcall_comm_wait(comm, timeout);
803 switch (e.category) {
805 ret = MSG_HOST_FAILURE;
808 ret = MSG_TRANSFER_FAILURE;
817 /* If the send failed, it is not used anymore */
818 t_simdata->setNotUsed();
821 TRACE_msg_task_put_end();
825 /** \ingroup msg_task_usage
826 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
828 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
830 * The maxrate parameter can be used to send a task with a limited
831 * bandwidth (smaller than the physical available value). Use
832 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
833 * value do disable this feature).
835 * \param task the task to be sent
836 * \param alias the mailbox name to where the task is sent
837 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
838 * \param maxrate the maximum communication rate for sending this task (byte/sec)
840 * \return Returns #MSG_OK if the task was successfully sent,
841 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
843 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
845 task->simdata->rate = maxrate;
846 return MSG_task_send_with_timeout(task, alias, timeout);
849 /** \ingroup msg_task_usage
850 * \brief Check if there is a communication going on in a mailbox.
852 * \param alias the name of the mailbox to be considered
854 * \return Returns 1 if there is a communication, 0 otherwise
856 int MSG_task_listen(const char *alias)
858 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
859 return mbox->listen() ? 1 : 0;
862 /** \ingroup msg_task_usage
863 * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
865 * \param alias the name of the mailbox to be considered
867 * \return Returns the PID of sender process,
868 * -1 if there is no communication in the mailbox.
870 int MSG_task_listen_from(const char *alias)
872 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
873 simgrid::kernel::activity::CommImplPtr comm =
874 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
879 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
882 /** \ingroup msg_task_usage
883 * \brief Sets the tracing category of a task.
885 * This function should be called after the creation of a MSG task, to define the category of that task. The
886 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
887 * parameter category must contain a category that was previously declared with the function #TRACE_category
888 * (or with #TRACE_category_with_color).
890 * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
892 * \param task the task that is going to be categorized
893 * \param category the name of the category to be associated to the task
895 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
897 void MSG_task_set_category (msg_task_t task, const char *category)
899 TRACE_msg_set_task_category (task, category);
902 /** \ingroup msg_task_usage
904 * \brief Gets the current tracing category of a task.
906 * \param task the task to be considered
908 * \see MSG_task_set_category
910 * \return Returns the name of the tracing category of the given task, nullptr otherwise
912 const char *MSG_task_get_category (msg_task_t task)
914 return task->category;