Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1eba2f67f6754760c100e81e7827532ab9125d56
[simgrid.git] / src / msg / msg_gos.cpp
1 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <cmath>
7
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"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
17
18 /**
19  * @brief Receives a task from a mailbox.
20  *
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.
23  *
24  * @param task a memory location for storing a #msg_task_t.
25  * @param alias name of the mailbox to receive the task from
26  *
27  * @return Returns
28  * #MSG_OK if the task was successfully received,
29  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
30  */
31 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
32 {
33   return MSG_task_receive_with_timeout(task, alias, -1);
34 }
35
36 /**
37  * @brief Receives a task from a mailbox at a given rate.
38  *
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)
42  *
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).
45  *
46  * @return Returns
47  * #MSG_OK if the task was successfully received,
48  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
49  */
50 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
51 {
52   return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
53 }
54
55 /**
56  * @brief Receives a task from a mailbox with a given timeout.
57  *
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.
61  *
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)
65  *
66  * @return Returns
67  * #MSG_OK if the task was successfully received,
68  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
69  */
70 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
71 {
72   return MSG_task_receive_ext(task, alias, timeout, nullptr);
73 }
74
75 /**
76  * @brief Receives a task from a mailbox with a given timeout and at a given rate.
77  *
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)
82  *
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).
87  *
88  * @return Returns
89  * #MSG_OK if the task was successfully received,
90  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
91  */
92 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t* task, const char* alias, double timeout, double rate)
93 {
94   return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
95 }
96
97 /**
98  * @brief Receives a task from a mailbox from a specific host with a given timeout.
99  *
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.
103  *
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
108  *
109  * @return Returns
110  * #MSG_OK if the task was successfully received,
111  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
112  */
113 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
114 {
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);
117 }
118
119 /**
120  * @brief Receives a task from a mailbox from a specific host with a given timeout  and at a given rate.
121  *
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)
127  *
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).
130  *
131  * @return Returns
132  * #MSG_OK if the task was successfully received,
133  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
134  */
135 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
136                                          double rate)
137 {
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 */
141   if (host)
142     THROW_UNIMPLEMENTED;
143
144   /* Sanity check */
145   xbt_assert(task, "Null pointer for the task storage");
146
147   if (*task)
148     XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
149
150   /* Try to receive it by calling SIMIX network layer */
151   try {
152     void* payload;
153     simgrid::s4u::Mailbox::by_name(alias)
154         ->get_init()
155         ->set_dst_data(&payload, sizeof(msg_task_t*))
156         ->set_rate(rate)
157         ->wait_for(timeout);
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) {
164     ret = MSG_TIMEOUT;
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;
170     else
171       throw;
172   }
173
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()));
176
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);
179   }
180   return ret;
181 }
182
183
184 /**
185  * @brief Starts listening for receiving a task from an asynchronous communication.
186  *
187  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
188  *
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
192  */
193 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
194 {
195   return MSG_task_irecv_bounded(task, name, -1.0);
196 }
197
198 /**
199  * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
200  *
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).
205  *
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
210  */
211 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
212 {
213   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
214
215   /* FIXME: these functions are not traceable */
216   /* Sanity check */
217   xbt_assert(task, "Null pointer for the task storage");
218
219   if (*task)
220     XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
221
222   /* Try to receive it by calling SIMIX network layer */
223   simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name(name)
224                                    ->get_init()
225                                    ->set_dst_data((void**)task, sizeof(msg_task_t*))
226                                    ->set_rate(rate)
227                                    ->start();
228
229   return new simgrid::msg::Comm(nullptr, task, comm);
230 }
231
232 /**
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.
239  */
240 int MSG_comm_test(msg_comm_t comm)
241 {
242   bool finished = false;
243
244   try {
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();
249     }
250   } catch (simgrid::TimeoutError& e) {
251     comm->status = MSG_TIMEOUT;
252     finished     = true;
253   } catch (simgrid::CancelException& e) {
254     comm->status = MSG_TASK_CANCELED;
255     finished     = true;
256   }
257   catch (xbt_ex& e) {
258     if (e.category == network_error) {
259       comm->status = MSG_TRANSFER_FAILURE;
260       finished     = true;
261     } else {
262       throw;
263     }
264   }
265
266   return finished;
267 }
268
269 /**
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
274  */
275 int MSG_comm_testany(xbt_dynar_t comms)
276 {
277   int finished_index = -1;
278
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));
282   msg_comm_t comm;
283   unsigned int cursor;
284   xbt_dynar_foreach(comms, cursor, comm) {
285     s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
286   }
287
288   msg_error_t status = MSG_OK;
289   try {
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;
297   }
298   catch (xbt_ex& e) {
299     if (e.category != network_error)
300       throw;
301     finished_index = e.value;
302     status         = MSG_TRANSFER_FAILURE;
303   }
304
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;
309
310     if (status == MSG_OK && comm->task_received != nullptr) {
311       /* I am the receiver */
312       (*comm->task_received)->set_not_used();
313     }
314   }
315
316   return finished_index;
317 }
318
319 /** @brief Destroys the provided communication. */
320 void MSG_comm_destroy(msg_comm_t comm)
321 {
322   delete comm;
323 }
324
325 /** @brief Wait for the completion of a communication.
326  *
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
332  */
333 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
334 {
335   try {
336     comm->s_comm->wait_for(timeout);
337
338     if (comm->task_received != nullptr) {
339       /* I am the receiver */
340       (*comm->task_received)->set_not_used();
341     }
342
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;
348   }
349   catch (xbt_ex& e) {
350     if (e.category == network_error)
351       comm->status = MSG_TRANSFER_FAILURE;
352     else
353       throw;
354   }
355
356   return comm->status;
357 }
358
359 /** @brief This function is called by a sender and permit to wait for each communication
360  *
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
364  */
365 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
366 {
367   for (int i = 0; i < nb_elem; i++)
368     MSG_comm_wait(comm[i], timeout);
369 }
370
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)
375  */
376 int MSG_comm_waitany(xbt_dynar_t comms)
377 {
378   int finished_index = -1;
379
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));
383   msg_comm_t comm;
384   unsigned int cursor;
385   xbt_dynar_foreach(comms, cursor, comm) {
386     s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
387   }
388
389   msg_error_t status = MSG_OK;
390   try {
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;
398   }
399   catch(xbt_ex& e) {
400     if (e.category == network_error) {
401       finished_index = e.value;
402       status         = MSG_TRANSFER_FAILURE;
403     } else {
404       throw;
405     }
406   }
407
408   xbt_assert(finished_index != -1, "WaitAny returned -1");
409
410   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
411   /* the communication is finished */
412   comm->status = status;
413
414   if (comm->task_received != nullptr) {
415     /* I am the receiver */
416     (*comm->task_received)->set_not_used();
417   }
418
419   return finished_index;
420 }
421
422 /**
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
426  */
427 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
428
429   return comm->status;
430 }
431
432 /** @brief Get a task (#msg_task_t) from a communication
433  *
434  * @param comm the communication where to get the task
435  * @return the task from the communication
436  */
437 msg_task_t MSG_comm_get_task(msg_comm_t comm)
438 {
439   xbt_assert(comm, "Invalid parameter");
440
441   return comm->task_received ? *comm->task_received : comm->task_sent;
442 }
443
444 /**
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
449  */
450 void MSG_comm_copy_data_from_SIMIX(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
451 {
452   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
453
454   // notify the user callback if any
455   if (msg_global->task_copy_callback) {
456     msg_task_t task = static_cast<msg_task_t>(buff);
457     msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());
458   }
459 }
460
461 /**
462  * @brief Sends a task to a mailbox
463  *
464  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
465  * side if #MSG_task_receive is used).
466  * See #MSG_task_isend for sending tasks asynchronously.
467  *
468  * @param task the task to be sent
469  * @param alias the mailbox name to where the task is sent
470  *
471  * @return Returns #MSG_OK if the task was successfully sent,
472  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
473  */
474 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
475 {
476   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
477   return MSG_task_send_with_timeout(task, alias, -1);
478 }
479
480 /**
481  * @brief Sends a task to a mailbox with a maximum rate
482  *
483  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
484  * the application to limit the bandwidth utilization of network links when sending the task.
485  *
486  * The maxrate parameter can be used to send a task with a limited bandwidth (smaller than the physical available
487  * value). Use MSG_task_send() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
488  *
489  * @param task the task to be sent
490  * @param alias the mailbox name to where the task is sent
491  * @param maxrate the maximum communication rate for sending this task (byte/sec)
492  *
493  * @return Returns #MSG_OK if the task was successfully sent,
494  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
495  */
496 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
497 {
498   task->set_rate(maxrate);
499   return MSG_task_send(task, alias);
500 }
501
502 /**
503  * @brief Sends a task to a mailbox with a timeout
504  *
505  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
506  *
507  * @param task the task to be sent
508  * @param alias the mailbox name to where the task is sent
509  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
510  *
511  * @return Returns #MSG_OK if the task was successfully sent,
512  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
513  */
514 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
515 {
516   msg_error_t ret = MSG_OK;
517   /* Try to send it */
518   try {
519     simgrid::s4u::CommPtr comm = task->send_async(alias, nullptr, false);
520     task->comm = comm;
521     comm->wait_for(timeout);
522   } catch (simgrid::TimeoutError& e) {
523     ret = MSG_TIMEOUT;
524   } catch (simgrid::CancelException& e) {
525     ret = MSG_HOST_FAILURE;
526   } catch (xbt_ex& e) {
527     if (e.category == network_error)
528       ret = MSG_TRANSFER_FAILURE;
529     else
530       throw;
531
532     /* If the send failed, it is not used anymore */
533     task->set_not_used();
534   }
535
536   return ret;
537 }
538
539 /**
540  * @brief Sends a task to a mailbox with a timeout and with a maximum rate
541  *
542  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
543  *
544  * The maxrate parameter can be used to send a task with a limited bandwidth (smaller than the physical available
545  * value). Use MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate value do disable this
546  * feature).
547  *
548  * @param task the task to be sent
549  * @param alias the mailbox name to where the task is sent
550  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
551  * @param maxrate the maximum communication rate for sending this task (byte/sec)
552  *
553  * @return Returns #MSG_OK if the task was successfully sent,
554  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
555  */
556 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
557 {
558   task->set_rate(maxrate);
559   return MSG_task_send_with_timeout(task, alias, timeout);
560 }
561
562 /**
563  * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
564  *
565  * @param alias the name of the mailbox to be considered
566  *
567  * @return Returns the PID of sender process,
568  * -1 if there is no communication in the mailbox.#include <cmath>
569  *
570  */
571 int MSG_task_listen_from(const char *alias)
572 {
573   /* looks inside the rdv directly. Not clean. */
574   simgrid::kernel::activity::CommImplPtr comm = simgrid::s4u::Mailbox::by_name(alias)->front();
575
576   if (comm && comm->src_actor_)
577     return comm->src_actor_->get_pid();
578   else
579     return -1;
580 }