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. */
7 #include "msg/private.h"
8 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
14 "Logging specific to MSG (gos)");
16 /** \ingroup msg_gos_functions
18 * \brief Return the last value returned by a MSG function (except
21 MSG_error_t MSG_get_errno(void)
23 return PROCESS_GET_ERRNO();
26 /** \ingroup msg_gos_functions
27 * \brief Executes a task and waits for its termination.
29 * This function is used for describing the behavior of an agent. It
30 * takes only one parameter.
31 * \param task a #m_task_t to execute on the location on which the
33 * \return #MSG_FATAL if \a task is not properly initialized and
36 MSG_error_t MSG_task_execute(m_task_t task)
38 simdata_task_t simdata = NULL;
39 m_process_t self = MSG_process_self();
40 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
43 TRACE_msg_task_execute_start (task);
46 simdata = task->simdata;
48 xbt_assert1((!simdata->compute) && (task->simdata->refcount == 1),
49 "This task is executed somewhere else. Go fix your code! %d", task->simdata->refcount);
51 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
53 if (simdata->computation_amount == 0) {
55 TRACE_msg_task_execute_end (task);
60 SIMIX_mutex_lock(simdata->mutex);
62 SIMIX_action_execute(SIMIX_host_self(), task->name,
63 simdata->computation_amount);
64 SIMIX_action_set_priority(simdata->compute, simdata->priority);
66 /* changed to waiting action since we are always waiting one action (execute, communicate or sleep) */
67 self->simdata->waiting_action = simdata->compute;
68 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
70 SIMIX_cond_wait(simdata->cond, simdata->mutex);
71 state = SIMIX_action_get_state(simdata->compute);
72 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
73 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
74 self->simdata->waiting_action = NULL;
76 SIMIX_mutex_unlock(simdata->mutex);
79 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE) {
80 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
81 SIMIX_action_destroy(task->simdata->compute);
82 simdata->computation_amount = 0.0;
84 simdata->compute = NULL;
86 TRACE_msg_task_execute_end (task);
89 } else if (SIMIX_host_get_state(SIMIX_host_self()) == 0) {
90 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
91 SIMIX_action_destroy(task->simdata->compute);
93 simdata->compute = NULL;
95 TRACE_msg_task_execute_end (task);
97 MSG_RETURN(MSG_HOST_FAILURE);
99 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
100 SIMIX_action_destroy(task->simdata->compute);
101 simdata->comm = NULL;
102 simdata->compute = NULL;
104 TRACE_msg_task_execute_end (task);
106 MSG_RETURN(MSG_TASK_CANCELLED);
110 /** \ingroup m_task_management
111 * \brief Creates a new #m_task_t (a parallel one....).
113 * A constructor for #m_task_t taking six arguments and returning the
114 corresponding object.
115 * \param name a name for the object. It is for user-level information
117 * \param host_nb the number of hosts implied in the parallel task.
118 * \param host_list an array of \p host_nb m_host_t.
119 * \param computation_amount an array of \p host_nb
120 doubles. computation_amount[i] is the total number of operations
121 that have to be performed on host_list[i].
122 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
123 * \param data a pointer to any data may want to attach to the new
124 object. It is for user-level information and can be NULL. It can
125 be retrieved with the function \ref MSG_task_get_data.
127 * \return The new corresponding object.
130 MSG_parallel_task_create(const char *name, int host_nb,
131 const m_host_t * host_list,
132 double *computation_amount,
133 double *communication_amount, void *data)
136 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
137 m_task_t task = xbt_new0(s_m_task_t, 1);
138 task->simdata = simdata;
141 task->name = xbt_strdup(name);
145 simdata->computation_amount = 0;
146 simdata->message_size = 0;
147 simdata->cond = SIMIX_cond_init();
148 simdata->mutex = SIMIX_mutex_init();
149 simdata->compute = NULL;
150 simdata->comm = NULL;
151 simdata->rate = -1.0;
152 simdata->refcount = 1;
153 simdata->sender = NULL;
154 simdata->receiver = NULL;
155 simdata->source = NULL;
157 simdata->host_nb = host_nb;
158 simdata->host_list = xbt_new0(smx_host_t, host_nb);
159 simdata->comp_amount = computation_amount;
160 simdata->comm_amount = communication_amount;
162 for (i = 0; i < host_nb; i++)
163 simdata->host_list[i] = host_list[i]->simdata->smx_host;
168 MSG_error_t MSG_parallel_task_execute(m_task_t task)
170 simdata_task_t simdata = NULL;
171 m_process_t self = MSG_process_self();
172 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
175 simdata = task->simdata;
177 xbt_assert0((!simdata->compute)
178 && (task->simdata->refcount == 1),
179 "This task is executed somewhere else. Go fix your code!");
181 xbt_assert0(simdata->host_nb, "This is not a parallel task. Go to hell.");
183 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
187 SIMIX_mutex_lock(simdata->mutex);
189 SIMIX_action_parallel_execute(task->name, simdata->host_nb,
190 simdata->host_list, simdata->comp_amount,
191 simdata->comm_amount, 1.0, -1.0);
193 self->simdata->waiting_action = simdata->compute;
194 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
196 SIMIX_cond_wait(simdata->cond, simdata->mutex);
197 state = SIMIX_action_get_state(task->simdata->compute);
198 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
200 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
201 self->simdata->waiting_action = NULL;
204 SIMIX_mutex_unlock(simdata->mutex);
207 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE) {
208 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
209 SIMIX_action_destroy(task->simdata->compute);
210 simdata->computation_amount = 0.0;
211 simdata->comm = NULL;
212 simdata->compute = NULL;
214 } else if (SIMIX_host_get_state(SIMIX_host_self()) == 0) {
215 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
216 SIMIX_action_destroy(task->simdata->compute);
217 simdata->comm = NULL;
218 simdata->compute = NULL;
219 MSG_RETURN(MSG_HOST_FAILURE);
221 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
222 SIMIX_action_destroy(task->simdata->compute);
223 simdata->comm = NULL;
224 simdata->compute = NULL;
225 MSG_RETURN(MSG_TASK_CANCELLED);
231 /** \ingroup msg_gos_functions
232 * \brief Sleep for the specified number of seconds
234 * Makes the current process sleep until \a time seconds have elapsed.
236 * \param nb_sec a number of second
238 MSG_error_t MSG_process_sleep(double nb_sec)
240 smx_action_t act_sleep;
241 m_process_t proc = MSG_process_self();
242 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
247 TRACE_msg_process_sleep_in (MSG_process_self());
250 /* create action to sleep */
252 SIMIX_action_sleep(SIMIX_process_get_host(proc->simdata->s_process),
255 mutex = SIMIX_mutex_init();
256 SIMIX_mutex_lock(mutex);
258 /* create conditional and register action to it */
259 cond = SIMIX_cond_init();
261 proc->simdata->waiting_action = act_sleep;
262 SIMIX_register_action_to_condition(act_sleep, cond);
264 SIMIX_cond_wait(cond, mutex);
265 state = SIMIX_action_get_state(act_sleep);
266 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
267 proc->simdata->waiting_action = NULL;
268 SIMIX_unregister_action_to_condition(act_sleep, cond);
269 SIMIX_mutex_unlock(mutex);
271 /* remove variables */
272 SIMIX_cond_destroy(cond);
273 SIMIX_mutex_destroy(mutex);
275 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
276 if (SIMIX_host_get_state(SIMIX_host_self()) == SURF_RESOURCE_OFF) {
277 SIMIX_action_destroy(act_sleep);
279 TRACE_msg_process_sleep_out (MSG_process_self());
281 MSG_RETURN(MSG_HOST_FAILURE);
284 SIMIX_action_destroy(act_sleep);
286 TRACE_msg_process_sleep_out (MSG_process_self());
288 MSG_RETURN(MSG_HOST_FAILURE);
291 SIMIX_action_destroy(act_sleep);
293 TRACE_msg_process_sleep_out (MSG_process_self());
298 /** \ingroup msg_gos_functions
299 * \brief Listen on \a channel and waits for receiving a task from \a host.
301 * It takes three parameters.
302 * \param task a memory location for storing a #m_task_t. It will
303 hold a task when this function will return. Thus \a task should not
304 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
305 those two condition does not hold, there will be a warning message.
306 * \param channel the channel on which the agent should be
307 listening. This value has to be >=0 and < than the maximal
308 number of channels fixed with MSG_set_channel_number().
309 * \param host the host that is to be watched.
310 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
311 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
314 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
316 return MSG_task_get_ext(task, channel, -1, host);
319 /** \ingroup msg_gos_functions
320 * \brief Listen on a channel and wait for receiving a task.
322 * It takes two parameters.
323 * \param task a memory location for storing a #m_task_t. It will
324 hold a task when this function will return. Thus \a task should not
325 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
326 those two condition does not hold, there will be a warning message.
327 * \param channel the channel on which the agent should be
328 listening. This value has to be >=0 and < than the maximal
329 number of channels fixed with MSG_set_channel_number().
330 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
331 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
333 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
335 return MSG_task_get_with_timeout(task, channel, -1);
338 /** \ingroup msg_gos_functions
339 * \brief Listen on a channel and wait for receiving a task with a timeout.
341 * It takes three parameters.
342 * \param task a memory location for storing a #m_task_t. It will
343 hold a task when this function will return. Thus \a task should not
344 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
345 those two condition does not hold, there will be a warning message.
346 * \param channel the channel on which the agent should be
347 listening. This value has to be >=0 and < than the maximal
348 number of channels fixed with MSG_set_channel_number().
349 * \param max_duration the maximum time to wait for a task before giving
350 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
351 will not be modified and will still be
352 equal to \c NULL when returning.
353 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
354 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
357 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
360 return MSG_task_get_ext(task, channel, max_duration, NULL);
363 /** \defgroup msg_gos_functions MSG Operating System Functions
364 * \brief This section describes the functions that can be used
365 * by an agent for handling some task.
369 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
372 xbt_assert1((channel >= 0)
373 && (channel < msg_global->max_channel), "Invalid channel %d",
377 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
378 (MSG_host_self(), channel), task, host, timeout);
382 MSG_task_receive_from_host(m_task_t * task, const char *alias, m_host_t host)
384 return MSG_task_receive_ext(task, alias, -1, host);
387 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
389 return MSG_task_receive_with_timeout(task, alias, -1);
393 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
396 return MSG_task_receive_ext(task, alias, timeout, NULL);
400 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
403 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host,
408 /** \ingroup msg_gos_functions
409 * \brief Put a task on a channel of an host and waits for the end of the
412 * This function is used for describing the behavior of an agent. It
413 * takes three parameter.
414 * \param task a #m_task_t to send on another location. This task
415 will not be usable anymore when the function will return. There is
416 no automatic task duplication and you have to save your parameters
417 before calling this function. Tasks are unique and once it has been
418 sent to another location, you should not access it anymore. You do
419 not need to call MSG_task_destroy() but to avoid using, as an
420 effect of inattention, this task anymore, you definitely should
421 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
422 can be transfered iff it has been correctly created with
424 * \param dest the destination of the message
425 * \param channel the channel on which the agent should put this
426 task. This value has to be >=0 and < than the maximal number of
427 channels fixed with MSG_set_channel_number().
428 * \return #MSG_FATAL if \a task is not properly initialized and
429 * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
430 * this function was called was shut down. Returns
431 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
432 * (network failure, dest failure)
434 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
436 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
439 /** \ingroup msg_gos_functions
440 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
446 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
449 task->simdata->rate = maxrate;
450 return MSG_task_put(task, dest, channel);
453 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
454 * host (with a timeout on the waiting of the destination host) and
455 * waits for the end of the transmission.
457 * This function is used for describing the behavior of an agent. It
458 * takes four parameter.
459 * \param task a #m_task_t to send on another location. This task
460 will not be usable anymore when the function will return. There is
461 no automatic task duplication and you have to save your parameters
462 before calling this function. Tasks are unique and once it has been
463 sent to another location, you should not access it anymore. You do
464 not need to call MSG_task_destroy() but to avoid using, as an
465 effect of inattention, this task anymore, you definitely should
466 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
467 can be transfered iff it has been correctly created with
469 * \param dest the destination of the message
470 * \param channel the channel on which the agent should put this
471 task. This value has to be >=0 and < than the maximal number of
472 channels fixed with MSG_set_channel_number().
473 * \param timeout the maximum time to wait for a task before giving
474 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
476 * \return #MSG_FATAL if \a task is not properly initialized and
477 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
478 this function was called was shut down. Returns
479 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
480 (network failure, dest failure, timeout...)
483 MSG_task_put_with_timeout(m_task_t task, m_host_t dest, m_channel_t channel,
486 xbt_assert1((channel >= 0)
487 && (channel < msg_global->max_channel), "Invalid channel %d",
491 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel(dest, channel),
495 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
497 return MSG_task_send_with_timeout(task, alias, -1);
502 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
504 task->simdata->rate = maxrate;
505 return MSG_task_send(task, alias);
510 MSG_task_send_with_timeout(m_task_t task, const char *alias, double timeout)
512 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias), task,
516 int MSG_task_listen(const char *alias)
520 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
523 /** \ingroup msg_gos_functions
524 * \brief Test whether there is a pending communication on a channel.
526 * It takes one parameter.
527 * \param channel the channel on which the agent should be
528 listening. This value has to be >=0 and < than the maximal
529 number of channels fixed with MSG_set_channel_number().
530 * \return 1 if there is a pending communication and 0 otherwise
532 int MSG_task_Iprobe(m_channel_t channel)
534 xbt_assert1((channel >= 0)
535 && (channel < msg_global->max_channel), "Invalid channel %d",
541 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
542 (MSG_host_self(), channel));
545 /** \ingroup msg_gos_functions
547 * \brief Return the number of tasks waiting to be received on a \a
548 channel and sent by \a host.
550 * It takes two parameters.
551 * \param channel the channel on which the agent should be
552 listening. This value has to be >=0 and < than the maximal
553 number of channels fixed with MSG_set_channel_number().
554 * \param host the host that is to be watched.
555 * \return the number of tasks waiting to be received on \a channel
558 int MSG_task_probe_from_host(int channel, m_host_t host)
560 xbt_assert1((channel >= 0)
561 && (channel < msg_global->max_channel), "Invalid channel %d",
567 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
568 (MSG_host_self(), channel),
573 int MSG_task_listen_from_host(const char *alias, m_host_t host)
578 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias(alias),
582 /** \ingroup msg_gos_functions
583 * \brief Test whether there is a pending communication on a channel, and who sent it.
585 * It takes one parameter.
586 * \param channel the channel on which the agent should be
587 listening. This value has to be >=0 and < than the maximal
588 number of channels fixed with MSG_set_channel_number().
589 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
591 int MSG_task_probe_from(m_channel_t channel)
597 xbt_assert1((channel >= 0)
598 && (channel < msg_global->max_channel), "Invalid channel %d",
603 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
604 (MSG_host_self(), channel))))
607 return MSG_process_get_PID(task->simdata->sender);
610 int MSG_task_listen_from(const char *alias)
616 if (NULL == (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
619 return MSG_process_get_PID(task->simdata->sender);
622 /** \ingroup msg_gos_functions
623 * \brief Wait for at most \a max_duration second for a task reception
626 * \a PID is updated with the PID of the first process that triggered this event if any.
628 * It takes three parameters:
629 * \param channel the channel on which the agent should be
630 listening. This value has to be >=0 and < than the maximal.
631 number of channels fixed with MSG_set_channel_number().
632 * \param PID a memory location for storing an int.
633 * \param timeout the maximum time to wait for a task before
634 giving up. In the case of a reception, *\a PID will be updated
635 with the PID of the first process to send a task.
636 * \return #MSG_HOST_FAILURE if the host is shut down in the meantime
637 and #MSG_OK otherwise.
640 MSG_channel_select_from(m_channel_t channel, double timeout, int *PID)
643 simdata_host_t h_simdata = NULL;
647 msg_mailbox_t mailbox;
649 xbt_assert1((channel >= 0)
650 && (channel < msg_global->max_channel), "Invalid channel %d",
657 if (timeout == 0.0) {
658 *PID = MSG_task_probe_from(channel);
663 h_simdata = h->simdata;
665 mailbox = MSG_mailbox_get_by_channel(MSG_host_self(), channel);
667 while (MSG_mailbox_is_empty(mailbox)) {
674 SIMIX_mutex_lock(h_simdata->mutex);
676 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
677 "A process is already blocked on this channel %d", channel);
679 cond = SIMIX_cond_init();
681 MSG_mailbox_set_cond(mailbox, cond);
684 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
686 SIMIX_cond_wait(cond, h_simdata->mutex);
689 SIMIX_cond_destroy(cond);
690 SIMIX_mutex_unlock(h_simdata->mutex);
692 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
693 MSG_RETURN(MSG_HOST_FAILURE);
696 MSG_mailbox_set_cond(mailbox, NULL);
700 if (NULL == (t = MSG_mailbox_get_head(mailbox)))
705 *PID = MSG_process_get_PID(t->simdata->sender);
713 MSG_error_t MSG_alias_select_from(const char *alias, double timeout, int *PID)
716 simdata_host_t h_simdata = NULL;
720 msg_mailbox_t mailbox;
726 if (timeout == 0.0) {
727 *PID = MSG_task_listen_from(alias);
732 h_simdata = h->simdata;
734 DEBUG2("Probing on alias %s (%s)", alias, h->name);
736 mailbox = MSG_mailbox_get_by_alias(alias);
738 while (MSG_mailbox_is_empty(mailbox)) {
745 SIMIX_mutex_lock(h_simdata->mutex);
747 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
748 "A process is already blocked on this alias %s", alias);
750 cond = SIMIX_cond_init();
752 MSG_mailbox_set_cond(mailbox, cond);
755 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
757 SIMIX_cond_wait(cond, h_simdata->mutex);
760 SIMIX_cond_destroy(cond);
761 SIMIX_mutex_unlock(h_simdata->mutex);
763 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
764 MSG_RETURN(MSG_HOST_FAILURE);
767 MSG_mailbox_set_cond(mailbox, NULL);
771 if (NULL == (t = MSG_mailbox_get_head(mailbox)))
776 *PID = MSG_process_get_PID(t->simdata->sender);