1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/sysdep.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
15 "Logging specific to MSG (gos)");
17 /** \ingroup msg_gos_functions
19 * \brief Return the last value returned by a MSG function (except
22 MSG_error_t MSG_get_errno(void)
24 return PROCESS_GET_ERRNO();
27 /** \ingroup msg_gos_functions
28 * \brief Executes a task and waits for its termination.
30 * This function is used for describing the behavior of an agent. It
31 * takes only one parameter.
32 * \param task a #m_task_t to execute on the location on which the
34 * \return #MSG_FATAL if \a task is not properly initialized and
37 MSG_error_t MSG_task_execute(m_task_t task)
39 simdata_task_t simdata = NULL;
40 m_process_t self = MSG_process_self();
43 simdata = task->simdata;
45 xbt_assert0(simdata->host_nb == 0,
46 "This is a parallel task. Go to hell.");
49 TRACE_msg_task_execute_start(task);
52 xbt_assert1((!simdata->compute) && (task->simdata->refcount == 1),
53 "This task is executed somewhere else. Go fix your code! %d",
54 task->simdata->refcount);
56 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
58 if (simdata->computation_amount == 0) {
60 TRACE_msg_task_execute_end(task);
66 SIMIX_req_host_execute(task->name, SIMIX_host_self(),
67 simdata->computation_amount);
68 SIMIX_req_host_execution_set_priority(simdata->compute, simdata->priority);
70 self->simdata->waiting_action = simdata->compute;
71 SIMIX_req_host_execution_wait(simdata->compute);
72 self->simdata->waiting_action = NULL;
76 DEBUG2("Execution task '%s' finished in state %d", task->name, SIMIX_req_host_execution_get_state(task->simdata->compute));
77 if (SIMIX_req_host_execution_get_state(task->simdata->compute) == SIMIX_DONE) {
78 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
79 SIMIX_req_host_execution_destroy(task->simdata->compute);
80 simdata->computation_amount = 0.0;
82 simdata->compute = NULL;
84 TRACE_msg_task_execute_end(task);
87 } else if (SIMIX_req_host_get_state(SIMIX_host_self()) == 0) {
88 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
89 SIMIX_req_host_execution_destroy(task->simdata->compute);
91 simdata->compute = NULL;
93 TRACE_msg_task_execute_end(task);
95 MSG_RETURN(MSG_HOST_FAILURE);
97 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
98 SIMIX_req_host_execution_destroy(task->simdata->compute);
100 simdata->compute = NULL;
102 TRACE_msg_task_execute_end(task);
104 MSG_RETURN(MSG_TASK_CANCELLED);
108 /** \ingroup m_task_management
109 * \brief Creates a new #m_task_t (a parallel one....).
111 * A constructor for #m_task_t taking six arguments and returning the
112 corresponding object.
113 * \param name a name for the object. It is for user-level information
115 * \param host_nb the number of hosts implied in the parallel task.
116 * \param host_list an array of \p host_nb m_host_t.
117 * \param computation_amount an array of \p host_nb
118 doubles. computation_amount[i] is the total number of operations
119 that have to be performed on host_list[i].
120 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
121 * \param data a pointer to any data may want to attach to the new
122 object. It is for user-level information and can be NULL. It can
123 be retrieved with the function \ref MSG_task_get_data.
125 * \return The new corresponding object.
128 MSG_parallel_task_create(const char *name, int host_nb,
129 const m_host_t * host_list,
130 double *computation_amount,
131 double *communication_amount, void *data)
134 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
135 m_task_t task = xbt_new0(s_m_task_t, 1);
136 task->simdata = simdata;
139 task->name = xbt_strdup(name);
143 simdata->computation_amount = 0;
144 simdata->message_size = 0;
145 simdata->compute = NULL;
146 simdata->comm = NULL;
147 simdata->rate = -1.0;
148 simdata->refcount = 1;
149 simdata->sender = NULL;
150 simdata->receiver = NULL;
151 simdata->source = NULL;
153 simdata->host_nb = host_nb;
154 simdata->host_list = xbt_new0(smx_host_t, host_nb);
155 simdata->comp_amount = computation_amount;
156 simdata->comm_amount = communication_amount;
158 for (i = 0; i < host_nb; i++)
159 simdata->host_list[i] = host_list[i]->simdata->smx_host;
164 MSG_error_t MSG_parallel_task_execute(m_task_t task)
166 simdata_task_t simdata = NULL;
167 m_process_t self = MSG_process_self();
170 simdata = task->simdata;
172 xbt_assert0((!simdata->compute)
173 && (task->simdata->refcount == 1),
174 "This task is executed somewhere else. Go fix your code!");
176 xbt_assert0(simdata->host_nb,
177 "This is not a parallel task. Go to hell.");
179 DEBUG1("Parallel computing on %s", MSG_process_self()->simdata->m_host->name);
184 SIMIX_req_host_parallel_execute(task->name, simdata->host_nb,
186 simdata->comp_amount,
187 simdata->comm_amount, 1.0, -1.0);
188 DEBUG1("Parallel execution action created: %p", simdata->compute);
190 self->simdata->waiting_action = simdata->compute;
191 SIMIX_req_host_execution_wait(simdata->compute);
192 self->simdata->waiting_action = NULL;
194 DEBUG2("Finished waiting for execution of action %p, state = %d", simdata->compute, SIMIX_req_host_execution_get_state(task->simdata->compute));
198 if (SIMIX_req_host_execution_get_state(task->simdata->compute) == SIMIX_DONE) {
199 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
200 SIMIX_req_host_execution_destroy(task->simdata->compute);
201 simdata->computation_amount = 0.0;
202 simdata->comm = NULL;
203 simdata->compute = NULL;
205 } else if (SIMIX_req_host_get_state(SIMIX_host_self()) == 0) {
206 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
207 SIMIX_req_host_execution_destroy(task->simdata->compute);
208 simdata->comm = NULL;
209 simdata->compute = NULL;
210 MSG_RETURN(MSG_HOST_FAILURE);
212 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
213 SIMIX_req_host_execution_destroy(task->simdata->compute);
214 simdata->comm = NULL;
215 simdata->compute = NULL;
216 MSG_RETURN(MSG_TASK_CANCELLED);
221 /** \ingroup msg_gos_functions
222 * \brief Sleep for the specified number of seconds
224 * Makes the current process sleep until \a time seconds have elapsed.
226 * \param nb_sec a number of second
228 MSG_error_t MSG_process_sleep(double nb_sec)
231 /*m_process_t proc = MSG_process_self();*/
234 TRACE_msg_process_sleep_in(MSG_process_self());
237 /* create action to sleep */
238 state = SIMIX_req_process_sleep(nb_sec);
240 /*proc->simdata->waiting_action = act_sleep;
242 FIXME: check if not setting the waiting_action breaks something on msg
244 proc->simdata->waiting_action = NULL;*/
246 if (state == SIMIX_DONE) {
247 if (SIMIX_req_host_get_state(SIMIX_host_self()) == SURF_RESOURCE_OFF) {
249 TRACE_msg_process_sleep_out(MSG_process_self());
251 MSG_RETURN(MSG_HOST_FAILURE);
255 TRACE_msg_process_sleep_out(MSG_process_self());
257 MSG_RETURN(MSG_HOST_FAILURE);
260 TRACE_msg_process_sleep_out(MSG_process_self());
265 /** \ingroup msg_gos_functions
266 * \brief Listen on \a channel and waits for receiving a task from \a host.
268 * It takes three parameters.
269 * \param task a memory location for storing a #m_task_t. It will
270 hold a task when this function will return. Thus \a task should not
271 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
272 those two condition does not hold, there will be a warning message.
273 * \param channel the channel on which the agent should be
274 listening. This value has to be >=0 and < than the maximal
275 number of channels fixed with MSG_set_channel_number().
276 * \param host the host that is to be watched.
277 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
278 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
281 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
283 return MSG_task_get_ext(task, channel, -1, host);
286 /** \ingroup msg_gos_functions
287 * \brief Listen on a channel and wait for receiving a task.
289 * It takes two parameters.
290 * \param task a memory location for storing a #m_task_t. It will
291 hold a task when this function will return. Thus \a task should not
292 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
293 those two condition does not hold, there will be a warning message.
294 * \param channel the channel on which the agent should be
295 listening. This value has to be >=0 and < than the maximal
296 number of channels fixed with MSG_set_channel_number().
297 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
298 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
300 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
302 return MSG_task_get_with_timeout(task, channel, -1);
305 /** \ingroup msg_gos_functions
306 * \brief Listen on a channel and wait for receiving a task with a timeout.
308 * It takes three parameters.
309 * \param task a memory location for storing a #m_task_t. It will
310 hold a task when this function will return. Thus \a task should not
311 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
312 those two condition does not hold, there will be a warning message.
313 * \param channel the channel on which the agent should be
314 listening. This value has to be >=0 and < than the maximal
315 number of channels fixed with MSG_set_channel_number().
316 * \param max_duration the maximum time to wait for a task before giving
317 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
318 will not be modified and will still be
319 equal to \c NULL when returning.
320 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
321 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
324 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
327 return MSG_task_get_ext(task, channel, max_duration, NULL);
330 /** \defgroup msg_gos_functions MSG Operating System Functions
331 * \brief This section describes the functions that can be used
332 * by an agent for handling some task.
336 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
339 xbt_assert1((channel >= 0)
340 && (channel < msg_global->max_channel), "Invalid channel %d",
344 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
345 (MSG_host_self(), channel), task, host,
350 MSG_task_receive_from_host(m_task_t * task, const char *alias,
353 return MSG_task_receive_ext(task, alias, -1, host);
356 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
358 return MSG_task_receive_with_timeout(task, alias, -1);
362 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
365 return MSG_task_receive_ext(task, alias, timeout, NULL);
369 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
373 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
375 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
379 /** \ingroup msg_gos_functions
380 * \brief Send a task on a channel.
382 * This function takes two parameter.
383 * \param task a #m_task_t to send on another location.
384 * \param alias the channel on which the agent should put this
385 task. This value has to be >=0 and < than the maximal number of
386 channels fixed with MSG_set_channel_number().
387 * \return the msg_comm_t communication.
389 msg_comm_t MSG_task_isend(m_task_t task, const char *alias)
391 simdata_task_t t_simdata = NULL;
392 m_process_t process = MSG_process_self();
393 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
397 /* FIXME: these functions are not tracable */
399 /* Prepare the task to send */
400 t_simdata = task->simdata;
401 t_simdata->sender = process;
402 t_simdata->source = MSG_host_self();
404 xbt_assert0(t_simdata->refcount == 1,
405 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
407 t_simdata->refcount++;
408 msg_global->sent_msg++;
409 process->simdata->waiting_task = task;
411 /* Send it by calling SIMIX network layer */
413 return SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
414 t_simdata->rate, task, sizeof(void *),
418 /** \ingroup msg_gos_functions
419 * \brief Listen on a channel for receiving a task from an asynchronous communication.
421 * It takes two parameters.
422 * \param task a memory location for storing a #m_task_t.
423 * \param alias the channel on which the agent should be
424 listening. This value has to be >=0 and < than the maximal
425 number of channels fixed with MSG_set_channel_number().
426 * \return the msg_comm_t communication.
428 msg_comm_t MSG_task_irecv(m_task_t * task, const char *alias)
430 smx_rdv_t rdv = MSG_mailbox_get_by_alias(alias);
434 /* FIXME: these functions are not tracable */
437 xbt_assert0(task, "Null pointer for the task storage");
441 ("MSG_task_get() was asked to write in a non empty task struct.");
443 /* Try to receive it by calling SIMIX network layer */
444 return SIMIX_req_comm_irecv(rdv, task, NULL);
447 /** \ingroup msg_gos_functions
448 * \brief Test the status of a communication.
450 * It takes one parameter.
451 * \param comm the communication to test.
452 * \return the status of the communication:
453 * TRUE : the communication is completed
454 * FALSE: the communication is incompleted
455 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
457 int MSG_comm_test(msg_comm_t comm)
459 return SIMIX_req_comm_test(comm);
462 /** \ingroup msg_gos_functions
463 * \brief After received TRUE to MSG_comm_test(), the communication must be destroyed.
465 * It takes one parameter.
466 * \param comm the communication to destroy.
468 void MSG_comm_destroy(msg_comm_t comm)
470 if (SIMIX_req_comm_get_src_proc(comm) != SIMIX_process_self()) {
472 task = (m_task_t) SIMIX_req_comm_get_src_buff(comm);
473 task->simdata->refcount--;
475 SIMIX_req_comm_destroy(comm);
478 /** \ingroup msg_gos_functions
479 * \brief Wait for the completion of a communication.
481 * It takes two parameters.
482 * \param comm the communication to wait.
483 * \param timeout Wait until the communication terminates or the timeout occurs
484 * \return MSG_error_t
486 MSG_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
489 MSG_error_t res = MSG_OK;
491 SIMIX_req_comm_wait(comm, timeout);
493 if (SIMIX_req_comm_get_src_proc(comm) != SIMIX_process_self()) {
495 task = (m_task_t) SIMIX_req_comm_get_src_buff(comm);
496 task->simdata->refcount--;
498 SIMIX_req_comm_destroy(comm);
500 /* FIXME: these functions are not tracable */
503 switch (e.category) {
505 res = MSG_HOST_FAILURE;
508 res = MSG_TRANSFER_FAILURE;
514 xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
521 /** \ingroup msg_gos_functions
522 * \brief This function is called by a sender and permit to wait for each communication
524 * It takes three parameters.
525 * \param comm a vector of communication
526 * \param nb_elem is the size of the comm vector
527 * \param timeout for each call of MSG_comm_wait
529 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
532 for (i = 0; i < nb_elem; i++) {
533 MSG_comm_wait(comm[i], timeout);
537 /** \ingroup msg_gos_functions
538 * \brief This function wait for the first completed communication
540 * It takes on parameter.
541 * \param comms a vector of communication
542 * \return the position of the completed communication from the xbt_dynar_t.
544 int MSG_comm_waitany(xbt_dynar_t comms)
546 return SIMIX_req_comm_waitany(comms);
549 m_task_t MSG_comm_get_task(msg_comm_t comm)
551 xbt_assert0(comm, "Invalid parameters");
552 return (m_task_t) SIMIX_req_comm_get_src_buff(comm);
555 /** \ingroup msg_gos_functions
556 * \brief Put a task on a channel of an host and waits for the end of the
559 * This function is used for describing the behavior of an agent. It
560 * takes three parameter.
561 * \param task a #m_task_t to send on another location. This task
562 will not be usable anymore when the function will return. There is
563 no automatic task duplication and you have to save your parameters
564 before calling this function. Tasks are unique and once it has been
565 sent to another location, you should not access it anymore. You do
566 not need to call MSG_task_destroy() but to avoid using, as an
567 effect of inattention, this task anymore, you definitely should
568 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
569 can be transfered iff it has been correctly created with
571 * \param dest the destination of the message
572 * \param channel the channel on which the agent should put this
573 task. This value has to be >=0 and < than the maximal number of
574 channels fixed with MSG_set_channel_number().
575 * \return #MSG_FATAL if \a task is not properly initialized and
576 * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
577 * this function was called was shut down. Returns
578 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
579 * (network failure, dest failure)
581 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
583 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
586 /** \ingroup msg_gos_functions
587 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
593 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
596 task->simdata->rate = maxrate;
597 return MSG_task_put(task, dest, channel);
600 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
601 * host (with a timeout on the waiting of the destination host) and
602 * waits for the end of the transmission.
604 * This function is used for describing the behavior of an agent. It
605 * takes four parameter.
606 * \param task a #m_task_t to send on another location. This task
607 will not be usable anymore when the function will return. There is
608 no automatic task duplication and you have to save your parameters
609 before calling this function. Tasks are unique and once it has been
610 sent to another location, you should not access it anymore. You do
611 not need to call MSG_task_destroy() but to avoid using, as an
612 effect of inattention, this task anymore, you definitely should
613 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
614 can be transfered iff it has been correctly created with
616 * \param dest the destination of the message
617 * \param channel the channel on which the agent should put this
618 task. This value has to be >=0 and < than the maximal number of
619 channels fixed with MSG_set_channel_number().
620 * \param timeout the maximum time to wait for a task before giving
621 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
623 * \return #MSG_FATAL if \a task is not properly initialized and
624 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
625 this function was called was shut down. Returns
626 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
627 (network failure, dest failure, timeout...)
630 MSG_task_put_with_timeout(m_task_t task, m_host_t dest,
631 m_channel_t channel, double timeout)
633 xbt_assert1((channel >= 0)
634 && (channel < msg_global->max_channel), "Invalid channel %d",
637 DEBUG1("MSG_task_put_with_timout: Trying to send a task to '%s'", dest->name);
639 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
640 (dest, channel), task, timeout);
643 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
645 DEBUG1("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
646 return MSG_task_send_with_timeout(task, alias, -1);
651 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
653 task->simdata->rate = maxrate;
654 return MSG_task_send(task, alias);
659 MSG_task_send_with_timeout(m_task_t task, const char *alias,
662 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
666 int MSG_task_listen(const char *alias)
670 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
673 /** \ingroup msg_gos_functions
674 * \brief Test whether there is a pending communication on a channel.
676 * It takes one parameter.
677 * \param channel the channel on which the agent should be
678 listening. This value has to be >=0 and < than the maximal
679 number of channels fixed with MSG_set_channel_number().
680 * \return 1 if there is a pending communication and 0 otherwise
682 int MSG_task_Iprobe(m_channel_t channel)
684 xbt_assert1((channel >= 0)
685 && (channel < msg_global->max_channel), "Invalid channel %d",
691 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
692 (MSG_host_self(), channel));
695 /** \ingroup msg_gos_functions
697 * \brief Return the number of tasks waiting to be received on a \a
698 channel and sent by \a host.
700 * It takes two parameters.
701 * \param channel the channel on which the agent should be
702 listening. This value has to be >=0 and < than the maximal
703 number of channels fixed with MSG_set_channel_number().
704 * \param host the host that is to be watched.
705 * \return the number of tasks waiting to be received on \a channel
708 int MSG_task_probe_from_host(int channel, m_host_t host)
710 xbt_assert1((channel >= 0)
711 && (channel < msg_global->max_channel), "Invalid channel %d",
717 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
718 (MSG_host_self(), channel),
723 int MSG_task_listen_from_host(const char *alias, m_host_t host)
728 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
732 /** \ingroup msg_gos_functions
733 * \brief Test whether there is a pending communication on a channel, and who sent it.
735 * It takes one parameter.
736 * \param channel the channel on which the agent should be
737 listening. This value has to be >=0 and < than the maximal
738 number of channels fixed with MSG_set_channel_number().
739 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
741 int MSG_task_probe_from(m_channel_t channel)
747 xbt_assert1((channel >= 0)
748 && (channel < msg_global->max_channel), "Invalid channel %d",
753 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
754 (MSG_host_self(), channel))))
757 return MSG_process_get_PID(task->simdata->sender);
760 int MSG_task_listen_from(const char *alias)
767 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
770 return MSG_process_get_PID(task->simdata->sender);