Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Check return value of MSG_task_isend_internal (codacy).
[simgrid.git] / src / msg / msg_gos.cpp
1 /* Copyright (c) 2004-2018. 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 <xbt/ex.hpp>
7
8 #include "src/instr/instr_private.hpp"
9 #include "src/kernel/activity/ExecImpl.hpp"
10 #include "src/msg/msg_private.hpp"
11 #include "src/simix/smx_private.hpp" /* MSG_task_listen looks inside the rdv directly. Not clean. */
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
14
15 /** \ingroup msg_task_usage
16  * \brief Executes a task and waits for its termination.
17  *
18  * This function is used for describing the behavior of a process. It takes only one parameter.
19  * \param task a #msg_task_t to execute on the location on which the process is running.
20  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
21  */
22 msg_error_t MSG_task_execute(msg_task_t task)
23 {
24   return MSG_parallel_task_execute(task);
25 }
26
27 /** \ingroup msg_task_usage
28  * \brief Executes a parallel task and waits for its termination.
29  *
30  * \param task a #msg_task_t to execute on the location on which the process is running.
31  *
32  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
33  * or #MSG_HOST_FAILURE otherwise
34  */
35 msg_error_t MSG_parallel_task_execute(msg_task_t task)
36 {
37   return MSG_parallel_task_execute_with_timeout(task, -1);
38 }
39
40 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
41 {
42   simdata_task_t simdata = task->simdata;
43   e_smx_state_t comp_state;
44   msg_error_t status = MSG_OK;
45
46   TRACE_msg_task_execute_start(task);
47
48   xbt_assert((not simdata->compute) && not task->simdata->isused,
49              "This task is executed somewhere else. Go fix your code!");
50
51   XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
52
53   if (simdata->flops_amount <= 0.0 && not simdata->host_nb) {
54     TRACE_msg_task_execute_end(task);
55     return MSG_OK;
56   }
57
58   try {
59     simdata->setUsed();
60
61     if (simdata->host_nb > 0) {
62       simdata->compute =
63           boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
64               task->name, simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
65               simdata->bytes_parallel_amount, -1.0, timeout));
66       XBT_DEBUG("Parallel execution action created: %p", simdata->compute.get());
67     } else {
68       simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
69           simcall_execution_start(task->name, simdata->flops_amount, simdata->priority, simdata->bound,
70                                   MSG_process_get_host(MSG_process_self())));
71     }
72     simcall_set_category(simdata->compute, task->category);
73     comp_state = simcall_execution_wait(simdata->compute);
74
75     simdata->setNotUsed();
76
77     XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
78   }
79   catch (xbt_ex& e) {
80     switch (e.category) {
81     case cancel_error:
82       status = MSG_TASK_CANCELED;
83       break;
84     case host_error:
85       status = MSG_HOST_FAILURE;
86       break;
87     case timeout_error:
88       status = MSG_TIMEOUT;
89       break;
90     default:
91       throw;
92     }
93   }
94
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);
100
101   return status;
102 }
103
104 /** \ingroup msg_task_usage
105  * \brief Sleep for the specified number of seconds
106  *
107  * Makes the current process sleep until \a time seconds have elapsed.
108  *
109  * \param nb_sec a number of second
110  */
111 msg_error_t MSG_process_sleep(double nb_sec)
112 {
113   msg_error_t status = MSG_OK;
114
115   if (TRACE_actor_is_enabled())
116     simgrid::instr::Container::byName(instr_pid(MSG_process_self()))->getState("ACTOR_STATE")->pushEvent("sleep");
117
118   try {
119     simcall_process_sleep(nb_sec);
120   }
121   catch(xbt_ex& e) {
122     if (e.category == cancel_error) {
123       XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
124       // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
125       // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
126       // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
127       // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
128       // and did not change anythings at the C level.
129       // See comment in the jmsg_process.c file, function JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
130       status = MSG_TASK_CANCELED;
131     } else
132       throw;
133   }
134
135   if (TRACE_actor_is_enabled())
136     simgrid::instr::Container::byName(instr_pid(MSG_process_self()))->getState("ACTOR_STATE")->popEvent();
137
138   return status;
139 }
140
141 /** \ingroup msg_task_usage
142  * \brief Receives a task from a mailbox.
143  *
144  * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
145  * for receiving tasks asynchronously.
146  *
147  * \param task a memory location for storing a #msg_task_t.
148  * \param alias name of the mailbox to receive the task from
149  *
150  * \return Returns
151  * #MSG_OK if the task was successfully received,
152  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
153  */
154 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
155 {
156   return MSG_task_receive_with_timeout(task, alias, -1);
157 }
158
159 /** \ingroup msg_task_usage
160  * \brief Receives a task from a mailbox at a given rate.
161  *
162  * \param task a memory location for storing a #msg_task_t.
163  * \param alias name of the mailbox to receive the task from
164  * \param rate limit the reception to rate bandwidth (byte/sec)
165  *
166  * The rate parameter can be used to receive a task with a limited
167  * bandwidth (smaller than the physical available value). Use
168  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
169  * rate value do disable this feature).
170  *
171  * \return Returns
172  * #MSG_OK if the task was successfully received,
173  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
174  */
175 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
176 {
177   return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
178 }
179
180 /** \ingroup msg_task_usage
181  * \brief Receives a task from a mailbox with a given timeout.
182  *
183  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
184  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously.  You can provide a -1 timeout
185  * to obtain an infinite timeout.
186  *
187  * \param task a memory location for storing a #msg_task_t.
188  * \param alias name of the mailbox to receive the task from
189  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
190  *
191  * \return Returns
192  * #MSG_OK if the task was successfully received,
193  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
194  */
195 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
196 {
197   return MSG_task_receive_ext(task, alias, timeout, nullptr);
198 }
199
200 /** \ingroup msg_task_usage
201  * \brief Receives a task from a mailbox with a given timeout and at a given rate.
202  *
203  * \param task a memory location for storing a #msg_task_t.
204  * \param alias name of the mailbox to receive the task from
205  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
206  * \param rate limit the reception to rate bandwidth (byte/sec)
207  *
208  * The rate parameter can be used to send a task with a limited
209  * bandwidth (smaller than the physical available value). Use
210  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
211  * rate value do disable this feature).
212  *
213  * \return Returns
214  * #MSG_OK if the task was successfully received,
215  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
216  */
217 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
218 {
219   return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
220 }
221
222 /** \ingroup msg_task_usage
223  * \brief Receives a task from a mailbox from a specific host with a given timeout.
224  *
225  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
226  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
227  * to obtain an infinite timeout.
228  *
229  * \param task a memory location for storing a #msg_task_t.
230  * \param alias name of the mailbox to receive the task from
231  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
232  * \param host a #msg_host_t host from where the task was sent
233  *
234  * \return Returns
235  * #MSG_OK if the task was successfully received,
236 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
237  */
238 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
239 {
240   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
241   return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
242 }
243
244 /** \ingroup msg_task_usage
245  * \brief Receives a task from a mailbox from a specific host with a given timeout  and at a given rate.
246  *
247  * \param task a memory location for storing a #msg_task_t.
248  * \param alias name of the mailbox to receive the task from
249  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
250  * \param host a #msg_host_t host from where the task was sent
251  * \param rate limit the reception to rate bandwidth (byte/sec)
252  *
253  * The rate parameter can be used to receive a task with a limited
254  * bandwidth (smaller than the physical available value). Use
255  * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
256  * rate value do disable this feature).
257  *
258  * \return Returns
259  * #MSG_OK if the task was successfully received,
260  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
261  */
262 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
263                                          double rate)
264 {
265   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
266   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
267   msg_error_t ret = MSG_OK;
268   /* We no longer support getting a task from a specific host */
269   if (host)
270     THROW_UNIMPLEMENTED;
271
272   TRACE_msg_task_get_start();
273
274   /* Sanity check */
275   xbt_assert(task, "Null pointer for the task storage");
276
277   if (*task)
278     XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
279
280   /* Try to receive it by calling SIMIX network layer */
281   try {
282     simcall_comm_recv(MSG_process_self()->getImpl(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
283     XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
284     (*task)->simdata->setNotUsed();
285   }
286   catch (xbt_ex& e) {
287     switch (e.category) {
288     case host_error:
289     case cancel_error:
290       ret = MSG_HOST_FAILURE;
291       break;
292     case network_error:
293       ret = MSG_TRANSFER_FAILURE;
294       break;
295     case timeout_error:
296       ret = MSG_TIMEOUT;
297       break;
298     default:
299       throw;
300     }
301   }
302
303   if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
304     TRACE_msg_task_get_end(*task);
305   }
306   return ret;
307 }
308
309 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
310 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
311                                                  void_f_pvoid_t cleanup, int detached)
312 {
313   simdata_task_t t_simdata = nullptr;
314   msg_process_t myself = MSG_process_self();
315   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
316   TRACE_msg_task_put_start(task);
317
318   /* Prepare the task to send */
319   t_simdata = task->simdata;
320   t_simdata->sender = myself;
321   t_simdata->source = MSG_host_self();
322   t_simdata->setUsed();
323   t_simdata->comm = nullptr;
324   msg_global->sent_msg++;
325
326   /* Send it by calling SIMIX network layer */
327   smx_activity_t act =
328       simcall_comm_isend(myself->getImpl(), mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate, task,
329                          sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
330   t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
331
332   msg_comm_t comm = nullptr;
333   if (not detached) {
334     comm = new simgrid::msg::Comm(task, nullptr, act);
335   }
336
337   if (TRACE_is_enabled())
338     simcall_set_category(act, task->category);
339   TRACE_msg_task_put_end();
340
341   return comm;
342 }
343
344 /** \ingroup msg_task_usage
345  * \brief Sends a task on a mailbox.
346  *
347  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
348  *
349  * \param task a #msg_task_t to send on another location.
350  * \param alias name of the mailbox to sent the task to
351  * \return the msg_comm_t communication created
352  */
353 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
354 {
355   return MSG_task_isend_internal(task, alias, nullptr, 0);
356 }
357
358 /** \ingroup msg_task_usage
359  * \brief Sends a task on a mailbox with a maximum rate
360  *
361  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
362  * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
363  *
364  * \param task a #msg_task_t to send on another location.
365  * \param alias name of the mailbox to sent the task to
366  * \param maxrate the maximum communication rate for sending this task (byte/sec).
367  * \return the msg_comm_t communication created
368  */
369 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
370 {
371   task->simdata->rate = maxrate;
372   return MSG_task_isend_internal(task, alias, nullptr, 0);
373 }
374
375 /** \ingroup msg_task_usage
376  * \brief Sends a task on a mailbox.
377  *
378  * This is a non blocking detached send function.
379  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
380  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
381  * usual. More details on this can be obtained on
382  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
383  * in the SimGrid-user mailing list archive.
384  *
385  * \param task a #msg_task_t to send on another location.
386  * \param alias name of the mailbox to sent the task to
387  * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
388  * (if nullptr, no function will be called)
389  */
390 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
391 {
392   msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
393   xbt_assert(comm == nullptr);
394 }
395
396 /** \ingroup msg_task_usage
397  * \brief Sends a task on a mailbox with a maximal rate.
398  *
399  * This is a non blocking detached send function.
400  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
401  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
402  * usual. More details on this can be obtained on
403  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
404  * in the SimGrid-user mailing list archive.
405  *
406  * The rate parameter can be used to send a task with a limited
407  * bandwidth (smaller than the physical available value). Use
408  * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
409  * value do disable this feature).
410  *
411  * \param task a #msg_task_t to send on another location.
412  * \param alias name of the mailbox to sent the task to
413  * \param cleanup a function to destroy the task if the
414  * communication fails, e.g. MSG_task_destroy
415  * (if nullptr, no function will be called)
416  * \param maxrate the maximum communication rate for sending this task (byte/sec)
417  *
418  */
419 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
420 {
421   task->simdata->rate = maxrate;
422   MSG_task_dsend(task, alias, cleanup);
423 }
424
425 /** \ingroup msg_task_usage
426  * \brief Starts listening for receiving a task from an asynchronous communication.
427  *
428  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
429  *
430  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
431  * \param name of the mailbox to receive the task on
432  * \return the msg_comm_t communication created
433  */
434 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
435 {
436   return MSG_task_irecv_bounded(task, name, -1.0);
437 }
438
439 /** \ingroup msg_task_usage
440  * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
441  *
442  * The rate parameter can be used to receive a task with a limited
443  * bandwidth (smaller than the physical available value). Use
444  * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
445  * value do disable this feature).
446  *
447  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
448  * \param name of the mailbox to receive the task on
449  * \param rate limit the bandwidth to the given rate (byte/sec)
450  * \return the msg_comm_t communication created
451  */
452 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
453 {
454   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
455
456   /* FIXME: these functions are not traceable */
457   /* Sanity check */
458   xbt_assert(task, "Null pointer for the task storage");
459
460   if (*task)
461     XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
462
463   /* Try to receive it by calling SIMIX network layer */
464   msg_comm_t comm =
465       new simgrid::msg::Comm(nullptr, task, simcall_comm_irecv(SIMIX_process_self(), mbox->getImpl(), task, nullptr,
466                                                                nullptr, nullptr, nullptr, rate));
467
468   return comm;
469 }
470
471 /** \ingroup msg_task_usage
472  * \brief Checks whether a communication is done, and if yes, finalizes it.
473  * \param comm the communication to test
474  * \return 'true' if the communication is finished
475  * (but it may have failed, use MSG_comm_get_status() to know its status)
476  * or 'false' if the communication is not finished yet
477  * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
478  */
479 int MSG_comm_test(msg_comm_t comm)
480 {
481   bool finished = false;
482
483   try {
484     finished = simcall_comm_test(comm->s_comm);
485     if (finished && comm->task_received != nullptr) {
486       /* I am the receiver */
487       (*comm->task_received)->simdata->setNotUsed();
488     }
489   }
490   catch (xbt_ex& e) {
491     switch (e.category) {
492       case network_error:
493         comm->status = MSG_TRANSFER_FAILURE;
494         finished     = true;
495         break;
496       case timeout_error:
497         comm->status = MSG_TIMEOUT;
498         finished     = true;
499         break;
500       default:
501         throw;
502     }
503   }
504
505   return finished;
506 }
507
508 /** \ingroup msg_task_usage
509  * \brief This function checks if a communication is finished.
510  * \param comms a vector of communications
511  * \return the position of the finished communication if any
512  * (but it may have failed, use MSG_comm_get_status() to know its status),
513  * or -1 if none is finished
514  */
515 int MSG_comm_testany(xbt_dynar_t comms)
516 {
517   int finished_index = -1;
518
519   /* Create the equivalent array with SIMIX objects: */
520   std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
521   s_comms.reserve(xbt_dynar_length(comms));
522   msg_comm_t comm;
523   unsigned int cursor;
524   xbt_dynar_foreach(comms, cursor, comm) {
525     s_comms.push_back(comm->s_comm);
526   }
527
528   msg_error_t status = MSG_OK;
529   try {
530     finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
531   }
532   catch (xbt_ex& e) {
533     switch (e.category) {
534       case network_error:
535         finished_index = e.value;
536         status = MSG_TRANSFER_FAILURE;
537         break;
538       case timeout_error:
539         finished_index = e.value;
540         status = MSG_TIMEOUT;
541         break;
542       default:
543         throw;
544     }
545   }
546
547   if (finished_index != -1) {
548     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
549     /* the communication is finished */
550     comm->status = status;
551
552     if (status == MSG_OK && comm->task_received != nullptr) {
553       /* I am the receiver */
554       (*comm->task_received)->simdata->setNotUsed();
555     }
556   }
557
558   return finished_index;
559 }
560
561 /** \ingroup msg_task_usage
562  * \brief Destroys a communication.
563  * \param comm the communication to destroy.
564  */
565 void MSG_comm_destroy(msg_comm_t comm)
566 {
567   delete comm;
568 }
569
570 /** \ingroup msg_task_usage
571  * \brief Wait for the completion of a communication.
572  *
573  * It takes two parameters.
574  * \param comm the communication to wait.
575  * \param timeout Wait until the communication terminates or the timeout occurs.
576  *                You can provide a -1 timeout to obtain an infinite timeout.
577  * \return msg_error_t
578  */
579 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
580 {
581   try {
582     simcall_comm_wait(comm->s_comm, timeout);
583
584     if (comm->task_received != nullptr) {
585       /* I am the receiver */
586       (*comm->task_received)->simdata->setNotUsed();
587     }
588
589     /* FIXME: these functions are not traceable */
590   }
591   catch (xbt_ex& e) {
592     switch (e.category) {
593     case network_error:
594       comm->status = MSG_TRANSFER_FAILURE;
595       break;
596     case timeout_error:
597       comm->status = MSG_TIMEOUT;
598       break;
599     default:
600       throw;
601     }
602   }
603
604   return comm->status;
605 }
606
607 /** \ingroup msg_task_usage
608 * \brief This function is called by a sender and permit to wait for each communication
609 *
610 * \param comm a vector of communication
611 * \param nb_elem is the size of the comm vector
612 * \param timeout for each call of MSG_comm_wait
613 */
614 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
615 {
616   for (int i = 0; i < nb_elem; i++)
617     MSG_comm_wait(comm[i], timeout);
618 }
619
620 /** \ingroup msg_task_usage
621  * \brief This function waits for the first communication finished in a list.
622  * \param comms a vector of communications
623  * \return the position of the first finished communication
624  * (but it may have failed, use MSG_comm_get_status() to know its status)
625  */
626 int MSG_comm_waitany(xbt_dynar_t comms)
627 {
628   int finished_index = -1;
629
630   /* create the equivalent dynar with SIMIX objects */
631   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
632     intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
633   });
634   msg_comm_t comm;
635   unsigned int cursor;
636   xbt_dynar_foreach(comms, cursor, comm) {
637     intrusive_ptr_add_ref(comm->s_comm.get());
638     xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
639   }
640
641   msg_error_t status = MSG_OK;
642   try {
643     finished_index = simcall_comm_waitany(s_comms, -1);
644   }
645   catch(xbt_ex& e) {
646     switch (e.category) {
647       case network_error:
648         finished_index = e.value;
649         status = MSG_TRANSFER_FAILURE;
650         break;
651       case timeout_error:
652         finished_index = e.value;
653         status = MSG_TIMEOUT;
654         break;
655       default:
656         throw;
657     }
658   }
659
660   xbt_assert(finished_index != -1, "WaitAny returned -1");
661   xbt_dynar_free(&s_comms);
662
663   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
664   /* the communication is finished */
665   comm->status = status;
666
667   if (comm->task_received != nullptr) {
668     /* I am the receiver */
669     (*comm->task_received)->simdata->setNotUsed();
670   }
671
672   return finished_index;
673 }
674
675 /**
676  * \ingroup msg_task_usage
677  * \brief Returns the error (if any) that occurred during a finished communication.
678  * \param comm a finished communication
679  * \return the status of the communication, or #MSG_OK if no error occurred
680  * during the communication
681  */
682 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
683
684   return comm->status;
685 }
686
687 /** \ingroup msg_task_usage
688  * \brief Get a task (#msg_task_t) from a communication
689  *
690  * \param comm the communication where to get the task
691  * \return the task from the communication
692  */
693 msg_task_t MSG_comm_get_task(msg_comm_t comm)
694 {
695   xbt_assert(comm, "Invalid parameter");
696
697   return comm->task_received ? *comm->task_received : comm->task_sent;
698 }
699
700 /**
701  * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
702  * \param synchro the comm
703  * \param buff the data copied
704  * \param buff_size size of the buffer
705  */
706 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
707 {
708   simgrid::kernel::activity::CommImplPtr comm =
709       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
710
711   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
712
713   // notify the user callback if any
714   if (msg_global->task_copy_callback) {
715     msg_task_t task = static_cast<msg_task_t>(buff);
716     msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
717   }
718 }
719
720 /** \ingroup msg_task_usage
721  * \brief Sends a task to a mailbox
722  *
723  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
724  * side if #MSG_task_receive is used).
725  * See #MSG_task_isend for sending tasks asynchronously.
726  *
727  * \param task the task to be sent
728  * \param alias the mailbox name to where the task is sent
729  *
730  * \return Returns #MSG_OK if the task was successfully sent,
731  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
732  */
733 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
734 {
735   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
736   return MSG_task_send_with_timeout(task, alias, -1);
737 }
738
739 /** \ingroup msg_task_usage
740  * \brief Sends a task to a mailbox with a maximum rate
741  *
742  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
743  * the application to limit the bandwidth utilization of network links when sending the task.
744  *
745  * The maxrate parameter can be used to send a task with a limited
746  * bandwidth (smaller than the physical available value). Use
747  * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
748  * value do disable this feature).
749  *
750  * \param task the task to be sent
751  * \param alias the mailbox name to where the task is sent
752  * \param maxrate the maximum communication rate for sending this task (byte/sec)
753  *
754  * \return Returns #MSG_OK if the task was successfully sent,
755  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
756  */
757 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
758 {
759   task->simdata->rate = maxrate;
760   return MSG_task_send(task, alias);
761 }
762
763 /** \ingroup msg_task_usage
764  * \brief Sends a task to a mailbox with a timeout
765  *
766  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
767  *
768  * \param task the task to be sent
769  * \param alias the mailbox name to where the task is sent
770  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
771  *
772  * \return Returns #MSG_OK if the task was successfully sent,
773  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
774  */
775 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
776 {
777   msg_error_t ret = MSG_OK;
778   simdata_task_t t_simdata = nullptr;
779   msg_process_t process = MSG_process_self();
780   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
781
782   TRACE_msg_task_put_start(task);
783
784   /* Prepare the task to send */
785   t_simdata = task->simdata;
786   t_simdata->sender = process;
787   t_simdata->source = MSG_host_self();
788
789   t_simdata->setUsed();
790
791   t_simdata->comm = nullptr;
792   msg_global->sent_msg++;
793
794   /* Try to send it by calling SIMIX network layer */
795   try {
796     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call  */
797     comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate, task,
798                               sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
799     if (TRACE_is_enabled())
800       simcall_set_category(comm, task->category);
801     t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
802     simcall_comm_wait(comm, timeout);
803   }
804   catch (xbt_ex& e) {
805     switch (e.category) {
806     case cancel_error:
807       ret = MSG_HOST_FAILURE;
808       break;
809     case network_error:
810       ret = MSG_TRANSFER_FAILURE;
811       break;
812     case timeout_error:
813       ret = MSG_TIMEOUT;
814       break;
815     default:
816       throw;
817     }
818
819     /* If the send failed, it is not used anymore */
820     t_simdata->setNotUsed();
821   }
822
823   TRACE_msg_task_put_end();
824   return ret;
825 }
826
827 /** \ingroup msg_task_usage
828  * \brief Sends a task to a mailbox with a timeout and with a maximum rate
829  *
830  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
831  *
832  * The maxrate parameter can be used to send a task with a limited
833  * bandwidth (smaller than the physical available value). Use
834  * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
835  * value do disable this feature).
836  *
837  * \param task the task to be sent
838  * \param alias the mailbox name to where the task is sent
839  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
840  * \param maxrate the maximum communication rate for sending this task (byte/sec)
841  *
842  * \return Returns #MSG_OK if the task was successfully sent,
843  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
844  */
845 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
846 {
847   task->simdata->rate = maxrate;
848   return MSG_task_send_with_timeout(task, alias, timeout);
849 }
850
851 /** \ingroup msg_task_usage
852  * \brief Check if there is a communication going on in a mailbox.
853  *
854  * \param alias the name of the mailbox to be considered
855  *
856  * \return Returns 1 if there is a communication, 0 otherwise
857  */
858 int MSG_task_listen(const char *alias)
859 {
860   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
861   return mbox->listen() ? 1 : 0;
862 }
863
864 /** \ingroup msg_task_usage
865  * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
866  *
867  * \param alias the name of the mailbox to be considered
868  *
869  * \return Returns the PID of sender process,
870  * -1 if there is no communication in the mailbox.
871  */
872 int MSG_task_listen_from(const char *alias)
873 {
874   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
875   simgrid::kernel::activity::CommImplPtr comm =
876       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
877
878   if (not comm)
879     return -1;
880
881   return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
882 }
883
884 /** \ingroup msg_task_usage
885  * \brief Sets the tracing category of a task.
886  *
887  * This function should be called after the creation of a MSG task, to define the category of that task. The
888  * first parameter task must contain a task that was  created with the function #MSG_task_create. The second
889  * parameter category must contain a category that was previously declared with the function #TRACE_category
890  * (or with #TRACE_category_with_color).
891  *
892  * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
893  *
894  * \param task the task that is going to be categorized
895  * \param category the name of the category to be associated to the task
896  *
897  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
898  */
899 void MSG_task_set_category (msg_task_t task, const char *category)
900 {
901   TRACE_msg_set_task_category (task, category);
902 }
903
904 /** \ingroup msg_task_usage
905  *
906  * \brief Gets the current tracing category of a task.
907  *
908  * \param task the task to be considered
909  *
910  * \see MSG_task_set_category
911  *
912  * \return Returns the name of the tracing category of the given task, nullptr otherwise
913  */
914 const char *MSG_task_get_category (msg_task_t task)
915 {
916   return task->category;
917 }