1 /* Copyright (c) 2004-2019. 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 "simgrid/Exception.hpp"
9 #include "simgrid/s4u/Actor.hpp"
10 #include "simgrid/s4u/Comm.hpp"
11 #include "simgrid/s4u/Exec.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
13 #include "src/instr/instr_private.hpp"
14 #include "src/msg/msg_private.hpp"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
19 * @brief Receives a task from a mailbox.
21 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
22 * for receiving tasks asynchronously.
24 * @param task a memory location for storing a #msg_task_t.
25 * @param alias name of the mailbox to receive the task from
28 * #MSG_OK if the task was successfully received,
29 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
31 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
33 return MSG_task_receive_with_timeout(task, alias, -1);
37 * @brief Receives a task from a mailbox at a given rate.
39 * @param task a memory location for storing a #msg_task_t.
40 * @param alias name of the mailbox to receive the task from
41 * @param rate limit the reception to rate bandwidth (byte/sec)
43 * The rate parameter can be used to receive a task with a limited bandwidth (smaller than the physical available
44 * value). Use MSG_task_receive() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
47 * #MSG_OK if the task was successfully received,
48 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
50 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
52 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
56 * @brief Receives a task from a mailbox with a given timeout.
58 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
59 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
60 * to obtain an infinite timeout.
62 * @param task a memory location for storing a #msg_task_t.
63 * @param alias name of the mailbox to receive the task from
64 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
67 * #MSG_OK if the task was successfully received,
68 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
70 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
72 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, -1);
76 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
78 * @param task a memory location for storing a #msg_task_t.
79 * @param alias name of the mailbox to receive the task from
80 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
81 * @param rate limit the reception to rate bandwidth (byte/sec)
83 * The rate parameter can be used to send a task with a limited
84 * bandwidth (smaller than the physical available value). Use
85 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
86 * rate value do disable this feature).
89 * #MSG_OK if the task was successfully received,
90 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
92 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t* task, const char* alias, double timeout, double rate)
94 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
98 * @brief Receives a task from a mailbox from a specific host with a given timeout.
100 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
101 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
102 * to obtain an infinite timeout.
104 * @param task a memory location for storing a #msg_task_t.
105 * @param alias name of the mailbox to receive the task from
106 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
107 * @param host a #msg_host_t host from where the task was sent
110 * #MSG_OK if the task was successfully received,
111 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
113 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
115 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
116 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
120 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
122 * @param task a memory location for storing a #msg_task_t.
123 * @param alias name of the mailbox to receive the task from
124 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
125 * @param host a #msg_host_t host from where the task was sent
126 * @param rate limit the reception to rate bandwidth (byte/sec)
128 * The rate parameter can be used to receive a task with a limited bandwidth (smaller than the physical available
129 * value). Use MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
132 * #MSG_OK if the task was successfully received,
133 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
135 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
138 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
139 msg_error_t ret = MSG_OK;
140 /* We no longer support getting a task from a specific host */
145 xbt_assert(task, "Null pointer for the task storage");
148 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
150 /* Try to receive it by calling SIMIX network layer */
153 simgrid::s4u::Mailbox::by_name(alias)
155 ->set_dst_data(&payload, sizeof(msg_task_t*))
158 *task = static_cast<msg_task_t>(payload);
159 XBT_DEBUG("Got task %s from %s", (*task)->get_cname(), alias);
160 (*task)->set_not_used();
161 } catch (simgrid::HostFailureException& e) {
162 ret = MSG_HOST_FAILURE;
163 } catch (simgrid::TimeoutError& e) {
165 } catch (simgrid::CancelException& e) {
166 ret = MSG_TASK_CANCELED;
167 } catch (xbt_ex& e) {
168 if (e.category == network_error)
169 ret = MSG_TRANSFER_FAILURE;
174 if (TRACE_actor_is_enabled() && ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
175 container_t process_container = simgrid::instr::Container::by_name(instr_pid(MSG_process_self()));
177 std::string key = std::string("p") + std::to_string((*task)->get_id());
178 simgrid::instr::Container::get_root()->get_link("ACTOR_TASK_LINK")->end_event(process_container, "SR", key);
185 * @brief Starts listening for receiving a task from an asynchronous communication.
187 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
189 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
190 * @param name of the mailbox to receive the task on
191 * @return the msg_comm_t communication created
193 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
195 return MSG_task_irecv_bounded(task, name, -1.0);
199 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
201 * The rate parameter can be used to receive a task with a limited
202 * bandwidth (smaller than the physical available value). Use
203 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
204 * value do disable this feature).
206 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
207 * @param name of the mailbox to receive the task on
208 * @param rate limit the bandwidth to the given rate (byte/sec)
209 * @return the msg_comm_t communication created
211 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
213 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
215 /* FIXME: these functions are not traceable */
217 xbt_assert(task, "Null pointer for the task storage");
220 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
222 /* Try to receive it by calling SIMIX network layer */
223 simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name(name)
225 ->set_dst_data((void**)task, sizeof(msg_task_t*))
229 return new simgrid::msg::Comm(nullptr, task, comm);
233 * @brief Checks whether a communication is done, and if yes, finalizes it.
234 * @param comm the communication to test
235 * @return 'true' if the communication is finished
236 * (but it may have failed, use MSG_comm_get_status() to know its status)
237 * or 'false' if the communication is not finished yet
238 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
240 int MSG_comm_test(msg_comm_t comm)
242 bool finished = false;
245 finished = comm->s_comm->test();
246 if (finished && comm->task_received != nullptr) {
247 /* I am the receiver */
248 (*comm->task_received)->set_not_used();
250 } catch (simgrid::TimeoutError& e) {
251 comm->status = MSG_TIMEOUT;
253 } catch (simgrid::CancelException& e) {
254 comm->status = MSG_TASK_CANCELED;
258 if (e.category == network_error) {
259 comm->status = MSG_TRANSFER_FAILURE;
270 * @brief This function checks if a communication is finished.
271 * @param comms a vector of communications
272 * @return the position of the finished communication if any
273 * (but it may have failed, use MSG_comm_get_status() to know its status), or -1 if none is finished
275 int MSG_comm_testany(xbt_dynar_t comms)
277 int finished_index = -1;
279 /* Create the equivalent array with SIMIX objects: */
280 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
281 s_comms.reserve(xbt_dynar_length(comms));
284 xbt_dynar_foreach(comms, cursor, comm) {
285 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
288 msg_error_t status = MSG_OK;
290 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
291 } catch (simgrid::TimeoutError& e) {
292 finished_index = e.value;
293 status = MSG_TIMEOUT;
294 } catch (simgrid::CancelException& e) {
295 finished_index = e.value;
296 status = MSG_TASK_CANCELED;
299 if (e.category != network_error)
301 finished_index = e.value;
302 status = MSG_TRANSFER_FAILURE;
305 if (finished_index != -1) {
306 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
307 /* the communication is finished */
308 comm->status = status;
310 if (status == MSG_OK && comm->task_received != nullptr) {
311 /* I am the receiver */
312 (*comm->task_received)->set_not_used();
316 return finished_index;
319 /** @brief Destroys the provided communication. */
320 void MSG_comm_destroy(msg_comm_t comm)
325 /** @brief Wait for the completion of a communication.
327 * It takes two parameters.
328 * @param comm the communication to wait.
329 * @param timeout Wait until the communication terminates or the timeout occurs.
330 * You can provide a -1 timeout to obtain an infinite timeout.
331 * @return msg_error_t
333 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
336 comm->s_comm->wait_for(timeout);
338 if (comm->task_received != nullptr) {
339 /* I am the receiver */
340 (*comm->task_received)->set_not_used();
343 /* FIXME: these functions are not traceable */
344 } catch (simgrid::TimeoutError& e) {
345 comm->status = MSG_TIMEOUT;
346 } catch (simgrid::CancelException& e) {
347 comm->status = MSG_TASK_CANCELED;
350 if (e.category == network_error)
351 comm->status = MSG_TRANSFER_FAILURE;
359 /** @brief This function is called by a sender and permit to wait for each communication
361 * @param comm a vector of communication
362 * @param nb_elem is the size of the comm vector
363 * @param timeout for each call of MSG_comm_wait
365 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
367 for (int i = 0; i < nb_elem; i++)
368 MSG_comm_wait(comm[i], timeout);
371 /** @brief This function waits for the first communication finished in a list.
372 * @param comms a vector of communications
373 * @return the position of the first finished communication
374 * (but it may have failed, use MSG_comm_get_status() to know its status)
376 int MSG_comm_waitany(xbt_dynar_t comms)
378 int finished_index = -1;
380 /* Create the equivalent array with SIMIX objects: */
381 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
382 s_comms.reserve(xbt_dynar_length(comms));
385 xbt_dynar_foreach(comms, cursor, comm) {
386 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
389 msg_error_t status = MSG_OK;
391 finished_index = simcall_comm_waitany(s_comms.data(), s_comms.size(), -1);
392 } catch (simgrid::TimeoutError& e) {
393 finished_index = e.value;
394 status = MSG_TIMEOUT;
395 } catch (simgrid::CancelException& e) {
396 finished_index = e.value;
397 status = MSG_TASK_CANCELED;
400 if (e.category == network_error) {
401 finished_index = e.value;
402 status = MSG_TRANSFER_FAILURE;
408 xbt_assert(finished_index != -1, "WaitAny returned -1");
410 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
411 /* the communication is finished */
412 comm->status = status;
414 if (comm->task_received != nullptr) {
415 /* I am the receiver */
416 (*comm->task_received)->set_not_used();
419 return finished_index;
423 * @brief Returns the error (if any) that occurred during a finished communication.
424 * @param comm a finished communication
425 * @return the status of the communication, or #MSG_OK if no error occurred during the communication
427 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
432 /** @brief Get a task (#msg_task_t) from a communication
434 * @param comm the communication where to get the task
435 * @return the task from the communication
437 msg_task_t MSG_comm_get_task(msg_comm_t comm)
439 xbt_assert(comm, "Invalid parameter");
441 return comm->task_received ? *comm->task_received : comm->task_sent;
445 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
446 * @param comm the comm
447 * @param buff the data copied
448 * @param buff_size size of the buffer
450 // deprecated but used by MSG_set_copy_callback. Should be removed in v325
451 void MSG_comm_copy_data_from_SIMIX(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
453 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
455 // notify the user callback if any
456 if (msg_global->task_copy_callback) {
457 msg_task_t task = static_cast<msg_task_t>(buff);
458 msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());
463 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
465 * @param alias the name of the mailbox to be considered
467 * @return Returns the PID of sender process,
468 * -1 if there is no communication in the mailbox.#include <cmath>
471 int MSG_task_listen_from(const char *alias)
473 /* looks inside the rdv directly. Not clean. */
474 simgrid::kernel::activity::CommImplPtr comm = simgrid::s4u::Mailbox::by_name(alias)->front();
476 if (comm && comm->src_actor_)
477 return comm->src_actor_->get_pid();