3 /* Copyright (c) 2002-2007 Arnaud Legrand. */
4 /* Copyright (c) 2007 Bruno Donassolo. */
5 /* All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "msg/private.h"
11 #include "xbt/sysdep.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
17 "Logging specific to MSG (gos)");
19 /** \ingroup msg_gos_functions
21 * \brief Return the last value returned by a MSG function (except
24 MSG_error_t MSG_get_errno(void)
26 return PROCESS_GET_ERRNO();
29 /** \ingroup msg_gos_functions
30 * \brief Executes a task and waits for its termination.
32 * This function is used for describing the behavior of an agent. It
33 * takes only one parameter.
34 * \param task a #m_task_t to execute on the location on which the
36 * \return #MSG_FATAL if \a task is not properly initialized and
39 MSG_error_t MSG_task_execute(m_task_t task)
41 simdata_task_t simdata = NULL;
42 m_process_t self = MSG_process_self();
43 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
46 TRACE_msg_task_execute_start (task);
49 simdata = task->simdata;
51 xbt_assert1((!simdata->compute) && (task->simdata->refcount == 1),
52 "This task is executed somewhere else. Go fix your code! %d", task->simdata->refcount);
54 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
57 SIMIX_mutex_lock(simdata->mutex);
59 SIMIX_action_execute(SIMIX_host_self(), task->name,
60 simdata->computation_amount);
61 SIMIX_action_set_priority(simdata->compute, simdata->priority);
63 /* changed to waiting action since we are always waiting one action (execute, communicate or sleep) */
64 self->simdata->waiting_action = simdata->compute;
65 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
67 SIMIX_cond_wait(simdata->cond, simdata->mutex);
68 state = SIMIX_action_get_state(simdata->compute);
69 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
70 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
71 self->simdata->waiting_action = NULL;
73 SIMIX_mutex_unlock(simdata->mutex);
76 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE) {
77 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
78 SIMIX_action_destroy(task->simdata->compute);
79 simdata->computation_amount = 0.0;
81 simdata->compute = NULL;
83 TRACE_msg_task_execute_end (task);
86 } else if (SIMIX_host_get_state(SIMIX_host_self()) == 0) {
87 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
88 SIMIX_action_destroy(task->simdata->compute);
90 simdata->compute = NULL;
92 TRACE_msg_task_execute_end (task);
94 MSG_RETURN(MSG_HOST_FAILURE);
96 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
97 SIMIX_action_destroy(task->simdata->compute);
99 simdata->compute = NULL;
101 TRACE_msg_task_execute_end (task);
103 MSG_RETURN(MSG_TASK_CANCELLED);
107 /** \ingroup m_task_management
108 * \brief Creates a new #m_task_t (a parallel one....).
110 * A constructor for #m_task_t taking six arguments and returning the
111 corresponding object.
112 * \param name a name for the object. It is for user-level information
114 * \param host_nb the number of hosts implied in the parallel task.
115 * \param host_list an array of \p host_nb m_host_t.
116 * \param computation_amount an array of \p host_nb
117 doubles. computation_amount[i] is the total number of operations
118 that have to be performed on host_list[i].
119 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
120 * \param data a pointer to any data may want to attach to the new
121 object. It is for user-level information and can be NULL. It can
122 be retrieved with the function \ref MSG_task_get_data.
124 * \return The new corresponding object.
127 MSG_parallel_task_create(const char *name, int host_nb,
128 const m_host_t * host_list,
129 double *computation_amount,
130 double *communication_amount, void *data)
133 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
134 m_task_t task = xbt_new0(s_m_task_t, 1);
135 task->simdata = simdata;
138 task->name = xbt_strdup(name);
142 simdata->computation_amount = 0;
143 simdata->message_size = 0;
144 simdata->cond = SIMIX_cond_init();
145 simdata->mutex = SIMIX_mutex_init();
146 simdata->compute = NULL;
147 simdata->comm = NULL;
148 simdata->rate = -1.0;
149 simdata->refcount = 1;
150 simdata->sender = NULL;
151 simdata->receiver = NULL;
152 simdata->source = NULL;
154 simdata->host_nb = host_nb;
155 simdata->host_list = xbt_new0(smx_host_t, host_nb);
156 simdata->comp_amount = computation_amount;
157 simdata->comm_amount = communication_amount;
159 for (i = 0; i < host_nb; i++)
160 simdata->host_list[i] = host_list[i]->simdata->smx_host;
165 MSG_error_t MSG_parallel_task_execute(m_task_t task)
167 simdata_task_t simdata = NULL;
168 m_process_t self = MSG_process_self();
169 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
172 simdata = task->simdata;
174 xbt_assert0((!simdata->compute)
175 && (task->simdata->refcount == 1),
176 "This task is executed somewhere else. Go fix your code!");
178 xbt_assert0(simdata->host_nb, "This is not a parallel task. Go to hell.");
180 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
184 SIMIX_mutex_lock(simdata->mutex);
186 SIMIX_action_parallel_execute(task->name, simdata->host_nb,
187 simdata->host_list, simdata->comp_amount,
188 simdata->comm_amount, 1.0, -1.0);
190 self->simdata->waiting_action = simdata->compute;
191 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
193 SIMIX_cond_wait(simdata->cond, simdata->mutex);
194 state = SIMIX_action_get_state(task->simdata->compute);
195 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
197 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
198 self->simdata->waiting_action = NULL;
201 SIMIX_mutex_unlock(simdata->mutex);
204 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE) {
205 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
206 SIMIX_action_destroy(task->simdata->compute);
207 simdata->computation_amount = 0.0;
208 simdata->comm = NULL;
209 simdata->compute = NULL;
211 } else if (SIMIX_host_get_state(SIMIX_host_self()) == 0) {
212 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
213 SIMIX_action_destroy(task->simdata->compute);
214 simdata->comm = NULL;
215 simdata->compute = NULL;
216 MSG_RETURN(MSG_HOST_FAILURE);
218 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
219 SIMIX_action_destroy(task->simdata->compute);
220 simdata->comm = NULL;
221 simdata->compute = NULL;
222 MSG_RETURN(MSG_TASK_CANCELLED);
228 /** \ingroup msg_gos_functions
229 * \brief Sleep for the specified number of seconds
231 * Makes the current process sleep until \a time seconds have elapsed.
233 * \param nb_sec a number of second
235 MSG_error_t MSG_process_sleep(double nb_sec)
237 smx_action_t act_sleep;
238 m_process_t proc = MSG_process_self();
239 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
244 TRACE_msg_process_sleep_in (MSG_process_self());
247 /* create action to sleep */
249 SIMIX_action_sleep(SIMIX_process_get_host(proc->simdata->s_process),
252 mutex = SIMIX_mutex_init();
253 SIMIX_mutex_lock(mutex);
255 /* create conditional and register action to it */
256 cond = SIMIX_cond_init();
258 proc->simdata->waiting_action = act_sleep;
259 SIMIX_register_action_to_condition(act_sleep, cond);
261 SIMIX_cond_wait(cond, mutex);
262 state = SIMIX_action_get_state(act_sleep);
263 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
264 proc->simdata->waiting_action = NULL;
265 SIMIX_unregister_action_to_condition(act_sleep, cond);
266 SIMIX_mutex_unlock(mutex);
268 /* remove variables */
269 SIMIX_cond_destroy(cond);
270 SIMIX_mutex_destroy(mutex);
272 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
273 if (SIMIX_host_get_state(SIMIX_host_self()) == SURF_RESOURCE_OFF) {
274 SIMIX_action_destroy(act_sleep);
276 TRACE_msg_process_sleep_out (MSG_process_self());
278 MSG_RETURN(MSG_HOST_FAILURE);
281 SIMIX_action_destroy(act_sleep);
283 TRACE_msg_process_sleep_out (MSG_process_self());
285 MSG_RETURN(MSG_HOST_FAILURE);
288 SIMIX_action_destroy(act_sleep);
290 TRACE_msg_process_sleep_out (MSG_process_self());
295 /** \ingroup msg_gos_functions
296 * \brief Listen on \a channel and waits for receiving a task from \a host.
298 * It takes three parameters.
299 * \param task a memory location for storing a #m_task_t. It will
300 hold a task when this function will return. Thus \a task should not
301 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
302 those two condition does not hold, there will be a warning message.
303 * \param channel the channel on which the agent should be
304 listening. This value has to be >=0 and < than the maximal
305 number of channels fixed with MSG_set_channel_number().
306 * \param host the host that is to be watched.
307 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
308 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
311 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
313 return MSG_task_get_ext(task, channel, -1, host);
316 /** \ingroup msg_gos_functions
317 * \brief Listen on a channel and wait for receiving a task.
319 * It takes two parameters.
320 * \param task a memory location for storing a #m_task_t. It will
321 hold a task when this function will return. Thus \a task should not
322 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
323 those two condition does not hold, there will be a warning message.
324 * \param channel the channel on which the agent should be
325 listening. This value has to be >=0 and < than the maximal
326 number of channels fixed with MSG_set_channel_number().
327 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
328 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
330 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
332 return MSG_task_get_with_timeout(task, channel, -1);
335 /** \ingroup msg_gos_functions
336 * \brief Listen on a channel and wait for receiving a task with a timeout.
338 * It takes three parameters.
339 * \param task a memory location for storing a #m_task_t. It will
340 hold a task when this function will return. Thus \a task should not
341 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
342 those two condition does not hold, there will be a warning message.
343 * \param channel the channel on which the agent should be
344 listening. This value has to be >=0 and < than the maximal
345 number of channels fixed with MSG_set_channel_number().
346 * \param max_duration the maximum time to wait for a task before giving
347 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
348 will not be modified and will still be
349 equal to \c NULL when returning.
350 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
351 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
354 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
357 return MSG_task_get_ext(task, channel, max_duration, NULL);
360 /** \defgroup msg_gos_functions MSG Operating System Functions
361 * \brief This section describes the functions that can be used
362 * by an agent for handling some task.
366 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
369 xbt_assert1((channel >= 0)
370 && (channel < msg_global->max_channel), "Invalid channel %d",
374 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
375 (MSG_host_self(), channel), task, host, timeout);
379 MSG_task_receive_from_host(m_task_t * task, const char *alias, m_host_t host)
381 return MSG_task_receive_ext(task, alias, -1, host);
384 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
386 return MSG_task_receive_with_timeout(task, alias, -1);
390 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
393 return MSG_task_receive_ext(task, alias, timeout, NULL);
397 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
400 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host,
405 /** \ingroup msg_gos_functions
406 * \brief Put a task on a channel of an host and waits for the end of the
409 * This function is used for describing the behavior of an agent. It
410 * takes three parameter.
411 * \param task a #m_task_t to send on another location. This task
412 will not be usable anymore when the function will return. There is
413 no automatic task duplication and you have to save your parameters
414 before calling this function. Tasks are unique and once it has been
415 sent to another location, you should not access it anymore. You do
416 not need to call MSG_task_destroy() but to avoid using, as an
417 effect of inattention, this task anymore, you definitely should
418 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
419 can be transfered iff it has been correctly created with
421 * \param dest the destination of the message
422 * \param channel the channel on which the agent should put this
423 task. This value has to be >=0 and < than the maximal number of
424 channels fixed with MSG_set_channel_number().
425 * \return #MSG_FATAL if \a task is not properly initialized and
426 * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
427 * this function was called was shut down. Returns
428 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
429 * (network failure, dest failure)
431 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
433 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
436 /** \ingroup msg_gos_functions
437 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
443 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
446 task->simdata->rate = maxrate;
447 return MSG_task_put(task, dest, channel);
450 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
451 * host (with a timeout on the waiting of the destination host) and
452 * waits for the end of the transmission.
454 * This function is used for describing the behavior of an agent. It
455 * takes four parameter.
456 * \param task a #m_task_t to send on another location. This task
457 will not be usable anymore when the function will return. There is
458 no automatic task duplication and you have to save your parameters
459 before calling this function. Tasks are unique and once it has been
460 sent to another location, you should not access it anymore. You do
461 not need to call MSG_task_destroy() but to avoid using, as an
462 effect of inattention, this task anymore, you definitely should
463 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
464 can be transfered iff it has been correctly created with
466 * \param dest the destination of the message
467 * \param channel the channel on which the agent should put this
468 task. This value has to be >=0 and < than the maximal number of
469 channels fixed with MSG_set_channel_number().
470 * \param timeout the maximum time to wait for a task before giving
471 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
473 * \return #MSG_FATAL if \a task is not properly initialized and
474 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
475 this function was called was shut down. Returns
476 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
477 (network failure, dest failure, timeout...)
480 MSG_task_put_with_timeout(m_task_t task, m_host_t dest, m_channel_t channel,
483 xbt_assert1((channel >= 0)
484 && (channel < msg_global->max_channel), "Invalid channel %d",
488 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel(dest, channel),
492 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
494 return MSG_task_send_with_timeout(task, alias, -1);
499 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
501 task->simdata->rate = maxrate;
502 return MSG_task_send(task, alias);
507 MSG_task_send_with_timeout(m_task_t task, const char *alias, double timeout)
509 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias), task,
513 int MSG_task_listen(const char *alias)
517 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
520 /** \ingroup msg_gos_functions
521 * \brief Test whether there is a pending communication on a channel.
523 * It takes one parameter.
524 * \param channel the channel on which the agent should be
525 listening. This value has to be >=0 and < than the maximal
526 number of channels fixed with MSG_set_channel_number().
527 * \return 1 if there is a pending communication and 0 otherwise
529 int MSG_task_Iprobe(m_channel_t channel)
531 xbt_assert1((channel >= 0)
532 && (channel < msg_global->max_channel), "Invalid channel %d",
538 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
539 (MSG_host_self(), channel));
542 /** \ingroup msg_gos_functions
544 * \brief Return the number of tasks waiting to be received on a \a
545 channel and sent by \a host.
547 * It takes two parameters.
548 * \param channel the channel on which the agent should be
549 listening. This value has to be >=0 and < than the maximal
550 number of channels fixed with MSG_set_channel_number().
551 * \param host the host that is to be watched.
552 * \return the number of tasks waiting to be received on \a channel
555 int MSG_task_probe_from_host(int channel, m_host_t host)
557 xbt_assert1((channel >= 0)
558 && (channel < msg_global->max_channel), "Invalid channel %d",
564 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
565 (MSG_host_self(), channel),
570 int MSG_task_listen_from_host(const char *alias, m_host_t host)
575 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias(alias),
579 /** \ingroup msg_gos_functions
580 * \brief Test whether there is a pending communication on a channel, and who sent it.
582 * It takes one parameter.
583 * \param channel the channel on which the agent should be
584 listening. This value has to be >=0 and < than the maximal
585 number of channels fixed with MSG_set_channel_number().
586 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
588 int MSG_task_probe_from(m_channel_t channel)
594 xbt_assert1((channel >= 0)
595 && (channel < msg_global->max_channel), "Invalid channel %d",
600 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
601 (MSG_host_self(), channel))))
604 return MSG_process_get_PID(task->simdata->sender);
607 int MSG_task_listen_from(const char *alias)
613 if (NULL == (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
616 return MSG_process_get_PID(task->simdata->sender);
619 /** \ingroup msg_gos_functions
620 * \brief Wait for at most \a max_duration second for a task reception
623 * \a PID is updated with the PID of the first process that triggered this event if any.
625 * It takes three parameters:
626 * \param channel the channel on which the agent should be
627 listening. This value has to be >=0 and < than the maximal.
628 number of channels fixed with MSG_set_channel_number().
629 * \param PID a memory location for storing an int.
630 * \param timeout the maximum time to wait for a task before
631 giving up. In the case of a reception, *\a PID will be updated
632 with the PID of the first process to send a task.
633 * \return #MSG_HOST_FAILURE if the host is shut down in the meantime
634 and #MSG_OK otherwise.
637 MSG_channel_select_from(m_channel_t channel, double timeout, int *PID)
640 simdata_host_t h_simdata = NULL;
644 msg_mailbox_t mailbox;
646 xbt_assert1((channel >= 0)
647 && (channel < msg_global->max_channel), "Invalid channel %d",
654 if (timeout == 0.0) {
655 *PID = MSG_task_probe_from(channel);
660 h_simdata = h->simdata;
662 mailbox = MSG_mailbox_get_by_channel(MSG_host_self(), channel);
664 while (MSG_mailbox_is_empty(mailbox)) {
671 SIMIX_mutex_lock(h_simdata->mutex);
673 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
674 "A process is already blocked on this channel %d", channel);
676 cond = SIMIX_cond_init();
678 MSG_mailbox_set_cond(mailbox, cond);
681 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
683 SIMIX_cond_wait(cond, h_simdata->mutex);
686 SIMIX_cond_destroy(cond);
687 SIMIX_mutex_unlock(h_simdata->mutex);
689 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
690 MSG_RETURN(MSG_HOST_FAILURE);
693 MSG_mailbox_set_cond(mailbox, NULL);
697 if (NULL == (t = MSG_mailbox_get_head(mailbox)))
702 *PID = MSG_process_get_PID(t->simdata->sender);
710 MSG_error_t MSG_alias_select_from(const char *alias, double timeout, int *PID)
713 simdata_host_t h_simdata = NULL;
717 msg_mailbox_t mailbox;
723 if (timeout == 0.0) {
724 *PID = MSG_task_listen_from(alias);
729 h_simdata = h->simdata;
731 DEBUG2("Probing on alias %s (%s)", alias, h->name);
733 mailbox = MSG_mailbox_get_by_alias(alias);
735 while (MSG_mailbox_is_empty(mailbox)) {
742 SIMIX_mutex_lock(h_simdata->mutex);
744 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
745 "A process is already blocked on this alias %s", alias);
747 cond = SIMIX_cond_init();
749 MSG_mailbox_set_cond(mailbox, cond);
752 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
754 SIMIX_cond_wait(cond, h_simdata->mutex);
757 SIMIX_cond_destroy(cond);
758 SIMIX_mutex_unlock(h_simdata->mutex);
760 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
761 MSG_RETURN(MSG_HOST_FAILURE);
764 MSG_mailbox_set_cond(mailbox, NULL);
768 if (NULL == (t = MSG_mailbox_get_head(mailbox)))
773 *PID = MSG_process_get_PID(t->simdata->sender);