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. */
6 #include "simgrid/Exception.hpp"
8 #include "simgrid/s4u/Mailbox.hpp"
9 #include "src/instr/instr_private.hpp"
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/msg/msg_private.hpp"
12 #include "src/simix/smx_private.hpp" /* MSG_task_listen looks inside the rdv directly. Not clean. */
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
16 /** @ingroup msg_task_usage
17 * @brief Executes a task and waits for its termination.
19 * This function is used for describing the behavior of a process. It takes only one parameter.
20 * @param task a #msg_task_t to execute on the location on which the process is running.
21 * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
23 msg_error_t MSG_task_execute(msg_task_t task)
25 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());
68 if (task->category != nullptr)
69 simcall_set_category(simdata->compute, task->category);
71 simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
72 simcall_execution_start(task->name ?: "", task->category ?: "", simdata->flops_amount, simdata->priority,
73 simdata->bound, MSG_process_get_host(MSG_process_self())));
76 comp_state = simcall_execution_wait(simdata->compute);
78 simdata->setNotUsed();
80 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
81 } catch (simgrid::HostFailureException& e) {
82 status = MSG_HOST_FAILURE;
83 } catch (simgrid::TimeoutError& e) {
88 status = MSG_TASK_CANCELED;
95 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
96 simdata->flops_amount = 0.0;
97 simdata->comm = nullptr;
98 simdata->compute = nullptr;
99 TRACE_msg_task_execute_end(task);
104 /** @ingroup msg_task_usage
105 * @brief Sleep for the specified number of seconds
107 * Makes the current process sleep until @a time seconds have elapsed.
109 * @param nb_sec a number of second
111 msg_error_t MSG_process_sleep(double nb_sec)
113 msg_error_t status = MSG_OK;
116 simgrid::s4u::this_actor::sleep_for(nb_sec);
119 if (e.category == cancel_error) {
120 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
121 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
122 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
123 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
124 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
125 // and did not change anythings at the C level.
126 // 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)
127 status = MSG_TASK_CANCELED;
135 /** @ingroup msg_task_usage
136 * @brief Receives a task from a mailbox.
138 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
139 * for receiving tasks asynchronously.
141 * @param task a memory location for storing a #msg_task_t.
142 * @param alias name of the mailbox to receive the task from
145 * #MSG_OK if the task was successfully received,
146 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
148 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
150 return MSG_task_receive_with_timeout(task, alias, -1);
153 /** @ingroup msg_task_usage
154 * @brief Receives a task from a mailbox at a given rate.
156 * @param task a memory location for storing a #msg_task_t.
157 * @param alias name of the mailbox to receive the task from
158 * @param rate limit the reception to rate bandwidth (byte/sec)
160 * The rate parameter can be used to receive a task with a limited
161 * bandwidth (smaller than the physical available value). Use
162 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
163 * rate value do disable this feature).
166 * #MSG_OK if the task was successfully received,
167 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
169 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
171 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
174 /** @ingroup msg_task_usage
175 * @brief Receives a task from a mailbox with a given timeout.
177 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
178 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
179 * to obtain an infinite timeout.
181 * @param task a memory location for storing a #msg_task_t.
182 * @param alias name of the mailbox to receive the task from
183 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
186 * #MSG_OK if the task was successfully received,
187 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
189 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
191 return MSG_task_receive_ext(task, alias, timeout, nullptr);
194 /** @ingroup msg_task_usage
195 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
197 * @param task a memory location for storing a #msg_task_t.
198 * @param alias name of the mailbox to receive the task from
199 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
200 * @param rate limit the reception to rate bandwidth (byte/sec)
202 * The rate parameter can be used to send a task with a limited
203 * bandwidth (smaller than the physical available value). Use
204 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
205 * rate value do disable this feature).
208 * #MSG_OK if the task was successfully received,
209 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
211 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
213 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
216 /** @ingroup msg_task_usage
217 * @brief Receives a task from a mailbox from a specific host with a given timeout.
219 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
220 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
221 * to obtain an infinite timeout.
223 * @param task a memory location for storing a #msg_task_t.
224 * @param alias name of the mailbox to receive the task from
225 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
226 * @param host a #msg_host_t host from where the task was sent
229 * #MSG_OK if the task was successfully received,
230 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
232 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
234 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
235 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
238 /** @ingroup msg_task_usage
239 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
241 * @param task a memory location for storing a #msg_task_t.
242 * @param alias name of the mailbox to receive the task from
243 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
244 * @param host a #msg_host_t host from where the task was sent
245 * @param rate limit the reception to rate bandwidth (byte/sec)
247 * The rate parameter can be used to receive a task with a limited
248 * bandwidth (smaller than the physical available value). Use
249 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
250 * rate value do disable this feature).
253 * #MSG_OK if the task was successfully received,
254 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
256 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
259 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
260 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
261 msg_error_t ret = MSG_OK;
262 /* We no longer support getting a task from a specific host */
266 TRACE_msg_task_get_start();
269 xbt_assert(task, "Null pointer for the task storage");
272 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
274 /* Try to receive it by calling SIMIX network layer */
276 simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
278 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
279 (*task)->simdata->setNotUsed();
280 } catch (simgrid::HostFailureException& e) {
281 ret = MSG_HOST_FAILURE;
282 } catch (simgrid::TimeoutError& e) {
284 } catch (xbt_ex& e) {
285 switch (e.category) {
287 ret = MSG_HOST_FAILURE;
290 ret = MSG_TRANSFER_FAILURE;
297 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
298 TRACE_msg_task_get_end(*task);
303 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
304 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
305 void_f_pvoid_t cleanup, int detached)
307 simdata_task_t t_simdata = nullptr;
308 msg_process_t myself = MSG_process_self();
309 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
310 TRACE_msg_task_put_start(task);
312 /* Prepare the task to send */
313 t_simdata = task->simdata;
314 t_simdata->sender = myself;
315 t_simdata->source = MSG_host_self();
316 t_simdata->setUsed();
317 t_simdata->comm = nullptr;
318 msg_global->sent_msg++;
320 /* Send it by calling SIMIX network layer */
322 simcall_comm_isend(myself->get_impl(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
323 sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
324 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
326 msg_comm_t comm = nullptr;
328 comm = new simgrid::msg::Comm(task, nullptr, act);
331 if (TRACE_is_enabled())
332 simcall_set_category(act, task->category);
333 TRACE_msg_task_put_end();
338 /** @ingroup msg_task_usage
339 * @brief Sends a task on a mailbox.
341 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
343 * @param task a #msg_task_t to send on another location.
344 * @param alias name of the mailbox to sent the task to
345 * @return the msg_comm_t communication created
347 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
349 return MSG_task_isend_internal(task, alias, nullptr, 0);
352 /** @ingroup msg_task_usage
353 * @brief Sends a task on a mailbox with a maximum rate
355 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
356 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
358 * @param task a #msg_task_t to send on another location.
359 * @param alias name of the mailbox to sent the task to
360 * @param maxrate the maximum communication rate for sending this task (byte/sec).
361 * @return the msg_comm_t communication created
363 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
365 task->simdata->rate = maxrate;
366 return MSG_task_isend_internal(task, alias, nullptr, 0);
369 /** @ingroup msg_task_usage
370 * @brief Sends a task on a mailbox.
372 * This is a non blocking detached send function.
373 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
374 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
375 * usual. More details on this can be obtained on
376 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
377 * in the SimGrid-user mailing list archive.
379 * @param task a #msg_task_t to send on another location.
380 * @param alias name of the mailbox to sent the task to
381 * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
382 * (if nullptr, no function will be called)
384 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
386 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
387 xbt_assert(comm == nullptr);
390 /** @ingroup msg_task_usage
391 * @brief Sends a task on a mailbox with a maximal rate.
393 * This is a non blocking detached send function.
394 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
395 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
396 * usual. More details on this can be obtained on
397 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
398 * in the SimGrid-user mailing list archive.
400 * The rate parameter can be used to send a task with a limited
401 * bandwidth (smaller than the physical available value). Use
402 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
403 * value do disable this feature).
405 * @param task a #msg_task_t to send on another location.
406 * @param alias name of the mailbox to sent the task to
407 * @param cleanup a function to destroy the task if the
408 * communication fails, e.g. MSG_task_destroy
409 * (if nullptr, no function will be called)
410 * @param maxrate the maximum communication rate for sending this task (byte/sec)
413 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
415 task->simdata->rate = maxrate;
416 MSG_task_dsend(task, alias, cleanup);
419 /** @ingroup msg_task_usage
420 * @brief Starts listening for receiving a task from an asynchronous communication.
422 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
424 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
425 * @param name of the mailbox to receive the task on
426 * @return the msg_comm_t communication created
428 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
430 return MSG_task_irecv_bounded(task, name, -1.0);
433 /** @ingroup msg_task_usage
434 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
436 * The rate parameter can be used to receive a task with a limited
437 * bandwidth (smaller than the physical available value). Use
438 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
439 * value do disable this feature).
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 (byte/sec)
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::by_name(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 */
458 msg_comm_t comm = new simgrid::msg::Comm(
460 simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, 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)
475 bool finished = false;
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();
483 } catch (simgrid::TimeoutError& e) {
484 comm->status = MSG_TIMEOUT;
488 switch (e.category) {
490 comm->status = MSG_TRANSFER_FAILURE;
501 /** @ingroup msg_task_usage
502 * @brief This function checks if a communication is finished.
503 * @param comms a vector of communications
504 * @return the position of the finished communication if any
505 * (but it may have failed, use MSG_comm_get_status() to know its status),
506 * or -1 if none is finished
508 int MSG_comm_testany(xbt_dynar_t comms)
510 int finished_index = -1;
512 /* Create the equivalent array with SIMIX objects: */
513 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
514 s_comms.reserve(xbt_dynar_length(comms));
517 xbt_dynar_foreach(comms, cursor, comm) {
518 s_comms.push_back(comm->s_comm);
521 msg_error_t status = MSG_OK;
523 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
524 } catch (simgrid::TimeoutError& e) {
525 finished_index = e.value;
526 status = MSG_TIMEOUT;
529 switch (e.category) {
531 finished_index = e.value;
532 status = MSG_TRANSFER_FAILURE;
539 if (finished_index != -1) {
540 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
541 /* the communication is finished */
542 comm->status = status;
544 if (status == MSG_OK && comm->task_received != nullptr) {
545 /* I am the receiver */
546 (*comm->task_received)->simdata->setNotUsed();
550 return finished_index;
553 /** @ingroup msg_task_usage
554 * @brief Destroys a communication.
555 * @param comm the communication to destroy.
557 void MSG_comm_destroy(msg_comm_t comm)
562 /** @ingroup msg_task_usage
563 * @brief Wait for the completion of a communication.
565 * It takes two parameters.
566 * @param comm the communication to wait.
567 * @param timeout Wait until the communication terminates or the timeout occurs.
568 * You can provide a -1 timeout to obtain an infinite timeout.
569 * @return msg_error_t
571 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
574 simcall_comm_wait(comm->s_comm, timeout);
576 if (comm->task_received != nullptr) {
577 /* I am the receiver */
578 (*comm->task_received)->simdata->setNotUsed();
581 /* FIXME: these functions are not traceable */
582 } catch (simgrid::TimeoutError& e) {
583 comm->status = MSG_TIMEOUT;
586 switch (e.category) {
588 comm->status = MSG_TRANSFER_FAILURE;
598 /** @ingroup msg_task_usage
599 * @brief This function is called by a sender and permit to wait for each communication
601 * @param comm a vector of communication
602 * @param nb_elem is the size of the comm vector
603 * @param timeout for each call of MSG_comm_wait
605 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
607 for (int i = 0; i < nb_elem; i++)
608 MSG_comm_wait(comm[i], timeout);
611 /** @ingroup msg_task_usage
612 * @brief This function waits for the first communication finished in a list.
613 * @param comms a vector of communications
614 * @return the position of the first finished communication
615 * (but it may have failed, use MSG_comm_get_status() to know its status)
617 int MSG_comm_waitany(xbt_dynar_t comms)
619 int finished_index = -1;
621 /* create the equivalent dynar with SIMIX objects */
622 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
623 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
627 xbt_dynar_foreach(comms, cursor, comm) {
628 intrusive_ptr_add_ref(comm->s_comm.get());
629 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
632 msg_error_t status = MSG_OK;
634 finished_index = simcall_comm_waitany(s_comms, -1);
635 } catch (simgrid::TimeoutError& e) {
636 finished_index = e.value;
637 status = MSG_TIMEOUT;
640 switch (e.category) {
642 finished_index = e.value;
643 status = MSG_TRANSFER_FAILURE;
650 xbt_assert(finished_index != -1, "WaitAny returned -1");
651 xbt_dynar_free(&s_comms);
653 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
654 /* the communication is finished */
655 comm->status = status;
657 if (comm->task_received != nullptr) {
658 /* I am the receiver */
659 (*comm->task_received)->simdata->setNotUsed();
662 return finished_index;
666 * @ingroup msg_task_usage
667 * @brief Returns the error (if any) that occurred during a finished communication.
668 * @param comm a finished communication
669 * @return the status of the communication, or #MSG_OK if no error occurred
670 * during the communication
672 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
677 /** @ingroup msg_task_usage
678 * @brief Get a task (#msg_task_t) from a communication
680 * @param comm the communication where to get the task
681 * @return the task from the communication
683 msg_task_t MSG_comm_get_task(msg_comm_t comm)
685 xbt_assert(comm, "Invalid parameter");
687 return comm->task_received ? *comm->task_received : comm->task_sent;
691 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
692 * @param synchro the comm
693 * @param buff the data copied
694 * @param buff_size size of the buffer
696 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
698 simgrid::kernel::activity::CommImplPtr comm =
699 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
701 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
703 // notify the user callback if any
704 if (msg_global->task_copy_callback) {
705 msg_task_t task = static_cast<msg_task_t>(buff);
706 msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
710 /** @ingroup msg_task_usage
711 * @brief Sends a task to a mailbox
713 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
714 * side if #MSG_task_receive is used).
715 * See #MSG_task_isend for sending tasks asynchronously.
717 * @param task the task to be sent
718 * @param alias the mailbox name to where the task is sent
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(msg_task_t task, const char *alias)
725 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
726 return MSG_task_send_with_timeout(task, alias, -1);
729 /** @ingroup msg_task_usage
730 * @brief Sends a task to a mailbox with a maximum rate
732 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
733 * the application to limit the bandwidth utilization of network links when sending the task.
735 * The maxrate parameter can be used to send a task with a limited
736 * bandwidth (smaller than the physical available value). Use
737 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
738 * value do disable this feature).
740 * @param task the task to be sent
741 * @param alias the mailbox name to where the task is sent
742 * @param maxrate the maximum communication rate for sending this task (byte/sec)
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_bounded(msg_task_t task, const char *alias, double maxrate)
749 task->simdata->rate = maxrate;
750 return MSG_task_send(task, alias);
753 /** @ingroup msg_task_usage
754 * @brief Sends a task to a mailbox with a timeout
756 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
758 * @param task the task to be sent
759 * @param alias the mailbox name to where the task is sent
760 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
762 * @return Returns #MSG_OK if the task was successfully sent,
763 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
765 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
767 msg_error_t ret = MSG_OK;
768 simdata_task_t t_simdata = nullptr;
769 msg_process_t process = MSG_process_self();
770 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
772 TRACE_msg_task_put_start(task);
774 /* Prepare the task to send */
775 t_simdata = task->simdata;
776 t_simdata->sender = process;
777 t_simdata->source = MSG_host_self();
779 t_simdata->setUsed();
781 t_simdata->comm = nullptr;
782 msg_global->sent_msg++;
784 /* Try to send it by calling SIMIX network layer */
786 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
787 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
788 sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
789 if (TRACE_is_enabled() && task->category != nullptr)
790 simcall_set_category(comm, task->category);
791 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
792 simcall_comm_wait(comm, timeout);
793 } catch (simgrid::TimeoutError& e) {
797 switch (e.category) {
799 ret = MSG_HOST_FAILURE;
802 ret = MSG_TRANSFER_FAILURE;
808 /* If the send failed, it is not used anymore */
809 t_simdata->setNotUsed();
812 TRACE_msg_task_put_end();
816 /** @ingroup msg_task_usage
817 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
819 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
821 * The maxrate parameter can be used to send a task with a limited
822 * bandwidth (smaller than the physical available value). Use
823 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
824 * value do disable this feature).
826 * @param task the task to be sent
827 * @param alias the mailbox name to where the task is sent
828 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
829 * @param maxrate the maximum communication rate for sending this task (byte/sec)
831 * @return Returns #MSG_OK if the task was successfully sent,
832 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
834 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
836 task->simdata->rate = maxrate;
837 return MSG_task_send_with_timeout(task, alias, timeout);
840 /** @ingroup msg_task_usage
841 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
843 * @param alias the name of the mailbox to be considered
845 * @return Returns the PID of sender process,
846 * -1 if there is no communication in the mailbox.
848 int MSG_task_listen_from(const char *alias)
850 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
851 simgrid::kernel::activity::CommImplPtr comm =
852 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
857 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
860 /** @ingroup msg_task_usage
861 * @brief Sets the tracing category of a task.
863 * This function should be called after the creation of a MSG task, to define the category of that task. The
864 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
865 * parameter category must contain a category that was previously declared with the function #TRACE_category
866 * (or with #TRACE_category_with_color).
868 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
870 * @param task the task that is going to be categorized
871 * @param category the name of the category to be associated to the task
873 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
875 void MSG_task_set_category (msg_task_t task, const char *category)
877 TRACE_msg_set_task_category (task, category);
880 /** @ingroup msg_task_usage
882 * @brief Gets the current tracing category of a task.
884 * @param task the task to be considered
886 * @see MSG_task_set_category
888 * @return Returns the name of the tracing category of the given task, nullptr otherwise
890 const char *MSG_task_get_category (msg_task_t task)
892 return task->category;