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 simdata = task->simdata;
47 xbt_assert1((!simdata->compute)
48 && (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);
54 SIMIX_mutex_lock(simdata->mutex);
56 SIMIX_action_execute(SIMIX_host_self(), task->name,
57 simdata->computation_amount);
58 SIMIX_action_set_priority(simdata->compute, simdata->priority);
60 /* changed to waiting action since we are always waiting one action (execute, communicate or sleep) */
61 self->simdata->waiting_action = simdata->compute;
62 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
64 SIMIX_cond_wait(simdata->cond, simdata->mutex);
65 state = SIMIX_action_get_state(simdata->compute);
66 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
67 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
68 self->simdata->waiting_action = NULL;
70 SIMIX_mutex_unlock(simdata->mutex);
73 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE) {
74 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
75 SIMIX_action_destroy(task->simdata->compute);
76 simdata->computation_amount = 0.0;
78 simdata->compute = NULL;
80 } else if (SIMIX_host_get_state(SIMIX_host_self()) == 0) {
81 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
82 SIMIX_action_destroy(task->simdata->compute);
84 simdata->compute = NULL;
85 MSG_RETURN(MSG_HOST_FAILURE);
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;
91 MSG_RETURN(MSG_TASK_CANCELLED);
95 /** \ingroup m_task_management
96 * \brief Creates a new #m_task_t (a parallel one....).
98 * A constructor for #m_task_t taking six arguments and returning the
100 * \param name a name for the object. It is for user-level information
102 * \param host_nb the number of hosts implied in the parallel task.
103 * \param host_list an array of \p host_nb m_host_t.
104 * \param computation_amount an array of \p host_nb
105 doubles. computation_amount[i] is the total number of operations
106 that have to be performed on host_list[i].
107 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
108 * \param data a pointer to any data may want to attach to the new
109 object. It is for user-level information and can be NULL. It can
110 be retrieved with the function \ref MSG_task_get_data.
112 * \return The new corresponding object.
115 MSG_parallel_task_create(const char *name, int host_nb,
116 const m_host_t * host_list,
117 double *computation_amount,
118 double *communication_amount, void *data)
121 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
122 m_task_t task = xbt_new0(s_m_task_t, 1);
123 task->simdata = simdata;
126 task->name = xbt_strdup(name);
130 simdata->computation_amount = 0;
131 simdata->message_size = 0;
132 simdata->cond = SIMIX_cond_init();
133 simdata->mutex = SIMIX_mutex_init();
134 simdata->compute = NULL;
135 simdata->comm = NULL;
136 simdata->rate = -1.0;
137 simdata->refcount = 1;
138 simdata->sender = NULL;
139 simdata->receiver = NULL;
140 simdata->source = NULL;
142 simdata->host_nb = host_nb;
143 simdata->host_list = xbt_new0(smx_host_t, host_nb);
144 simdata->comp_amount = computation_amount;
145 simdata->comm_amount = communication_amount;
147 for (i = 0; i < host_nb; i++)
148 simdata->host_list[i] = host_list[i]->simdata->smx_host;
153 MSG_error_t MSG_parallel_task_execute(m_task_t task)
155 simdata_task_t simdata = NULL;
156 m_process_t self = MSG_process_self();
157 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
160 simdata = task->simdata;
162 xbt_assert0((!simdata->compute)
163 && (task->simdata->refcount == 1),
164 "This task is executed somewhere else. Go fix your code!");
166 xbt_assert0(simdata->host_nb, "This is not a parallel task. Go to hell.");
168 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
172 SIMIX_mutex_lock(simdata->mutex);
174 SIMIX_action_parallel_execute(task->name, simdata->host_nb,
175 simdata->host_list, simdata->comp_amount,
176 simdata->comm_amount, 1.0, -1.0);
178 self->simdata->waiting_action = simdata->compute;
179 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
181 SIMIX_cond_wait(simdata->cond, simdata->mutex);
182 state = SIMIX_action_get_state(task->simdata->compute);
183 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
185 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
186 self->simdata->waiting_action = NULL;
189 SIMIX_mutex_unlock(simdata->mutex);
192 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE) {
193 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
194 SIMIX_action_destroy(task->simdata->compute);
195 simdata->computation_amount = 0.0;
196 simdata->comm = NULL;
197 simdata->compute = NULL;
199 } else if (SIMIX_host_get_state(SIMIX_host_self()) == 0) {
200 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
201 SIMIX_action_destroy(task->simdata->compute);
202 simdata->comm = NULL;
203 simdata->compute = NULL;
204 MSG_RETURN(MSG_HOST_FAILURE);
206 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
207 SIMIX_action_destroy(task->simdata->compute);
208 simdata->comm = NULL;
209 simdata->compute = NULL;
210 MSG_RETURN(MSG_TASK_CANCELLED);
216 /** \ingroup msg_gos_functions
217 * \brief Sleep for the specified number of seconds
219 * Makes the current process sleep until \a time seconds have elapsed.
221 * \param nb_sec a number of second
223 MSG_error_t MSG_process_sleep(double nb_sec)
225 smx_action_t act_sleep;
226 m_process_t proc = MSG_process_self();
227 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
231 /* create action to sleep */
233 SIMIX_action_sleep(SIMIX_process_get_host(proc->simdata->s_process),
236 mutex = SIMIX_mutex_init();
237 SIMIX_mutex_lock(mutex);
239 /* create conditional and register action to it */
240 cond = SIMIX_cond_init();
242 proc->simdata->waiting_action = act_sleep;
243 SIMIX_register_action_to_condition(act_sleep, cond);
245 SIMIX_cond_wait(cond, mutex);
246 state = SIMIX_action_get_state(act_sleep);
247 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
248 proc->simdata->waiting_action = NULL;
249 SIMIX_unregister_action_to_condition(act_sleep, cond);
250 SIMIX_mutex_unlock(mutex);
252 /* remove variables */
253 SIMIX_cond_destroy(cond);
254 SIMIX_mutex_destroy(mutex);
256 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
257 if (SIMIX_host_get_state(SIMIX_host_self()) == SURF_RESOURCE_OFF) {
258 SIMIX_action_destroy(act_sleep);
259 MSG_RETURN(MSG_HOST_FAILURE);
262 SIMIX_action_destroy(act_sleep);
263 MSG_RETURN(MSG_HOST_FAILURE);
266 SIMIX_action_destroy(act_sleep);
270 /** \ingroup msg_gos_functions
271 * \brief Return the number of MSG tasks currently running on
272 * the host of the current running process.
274 static int MSG_get_msgload(void)
276 xbt_die("not implemented yet");
282 /** \ingroup msg_gos_functions
283 * \brief Listen on \a channel and waits for receiving a task from \a host.
285 * It takes three parameters.
286 * \param task a memory location for storing a #m_task_t. It will
287 hold a task when this function will return. Thus \a task should not
288 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
289 those two condition does not hold, there will be a warning message.
290 * \param channel the channel on which the agent should be
291 listening. This value has to be >=0 and < than the maximal
292 number of channels fixed with MSG_set_channel_number().
293 * \param host the host that is to be watched.
294 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
295 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
298 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
300 return MSG_task_get_ext(task, channel, -1, host);
303 /** \ingroup msg_gos_functions
304 * \brief Listen on a channel and wait for receiving a task.
306 * It takes two parameters.
307 * \param task a memory location for storing a #m_task_t. It will
308 hold a task when this function will return. Thus \a task should not
309 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
310 those two condition does not hold, there will be a warning message.
311 * \param channel the channel on which the agent should be
312 listening. This value has to be >=0 and < than the maximal
313 number of channels fixed with MSG_set_channel_number().
314 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
315 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
317 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
319 return MSG_task_get_with_timeout(task, channel, -1);
322 /** \ingroup msg_gos_functions
323 * \brief Listen on a channel and wait for receiving a task with a timeout.
325 * It takes three parameters.
326 * \param task a memory location for storing a #m_task_t. It will
327 hold a task when this function will return. Thus \a task should not
328 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
329 those two condition does not hold, there will be a warning message.
330 * \param channel the channel on which the agent should be
331 listening. This value has to be >=0 and < than the maximal
332 number of channels fixed with MSG_set_channel_number().
333 * \param max_duration the maximum time to wait for a task before giving
334 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
335 will not be modified and will still be
336 equal to \c NULL when returning.
337 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
338 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
341 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
344 return MSG_task_get_ext(task, channel, max_duration, NULL);
347 /** \defgroup msg_gos_functions MSG Operating System Functions
348 * \brief This section describes the functions that can be used
349 * by an agent for handling some task.
353 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
356 xbt_assert1((channel >= 0)
357 && (channel < msg_global->max_channel), "Invalid channel %d",
361 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
362 (MSG_host_self(), channel), task, host, timeout);
366 MSG_task_receive_from_host(m_task_t * task, const char *alias, m_host_t host)
368 return MSG_task_receive_ext(task, alias, -1, host);
371 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
373 return MSG_task_receive_with_timeout(task, alias, -1);
377 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
380 return MSG_task_receive_ext(task, alias, timeout, NULL);
384 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
387 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host,
392 /** \ingroup msg_gos_functions
393 * \brief Put a task on a channel of an host and waits for the end of the
396 * This function is used for describing the behavior of an agent. It
397 * takes three parameter.
398 * \param task a #m_task_t to send on another location. This task
399 will not be usable anymore when the function will return. There is
400 no automatic task duplication and you have to save your parameters
401 before calling this function. Tasks are unique and once it has been
402 sent to another location, you should not access it anymore. You do
403 not need to call MSG_task_destroy() but to avoid using, as an
404 effect of inattention, this task anymore, you definitely should
405 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
406 can be transfered iff it has been correctly created with
408 * \param dest the destination of the message
409 * \param channel the channel on which the agent should put this
410 task. This value has to be >=0 and < than the maximal number of
411 channels fixed with MSG_set_channel_number().
412 * \return #MSG_FATAL if \a task is not properly initialized and
413 * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
414 * this function was called was shut down. Returns
415 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
416 * (network failure, dest failure)
418 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
420 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
423 /** \ingroup msg_gos_functions
424 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
430 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
433 task->simdata->rate = maxrate;
434 return MSG_task_put(task, dest, channel);
437 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
438 * host (with a timeout on the waiting of the destination host) and
439 * waits for the end of the transmission.
441 * This function is used for describing the behavior of an agent. It
442 * takes four parameter.
443 * \param task a #m_task_t to send on another location. This task
444 will not be usable anymore when the function will return. There is
445 no automatic task duplication and you have to save your parameters
446 before calling this function. Tasks are unique and once it has been
447 sent to another location, you should not access it anymore. You do
448 not need to call MSG_task_destroy() but to avoid using, as an
449 effect of inattention, this task anymore, you definitely should
450 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
451 can be transfered iff it has been correctly created with
453 * \param dest the destination of the message
454 * \param channel the channel on which the agent should put this
455 task. This value has to be >=0 and < than the maximal number of
456 channels fixed with MSG_set_channel_number().
457 * \param timeout the maximum time to wait for a task before giving
458 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
460 * \return #MSG_FATAL if \a task is not properly initialized and
461 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
462 this function was called was shut down. Returns
463 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
464 (network failure, dest failure, timeout...)
467 MSG_task_put_with_timeout(m_task_t task, m_host_t dest, m_channel_t channel,
470 xbt_assert1((channel >= 0)
471 && (channel < msg_global->max_channel), "Invalid channel %d",
475 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel(dest, channel),
479 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
481 return MSG_task_send_with_timeout(task, alias, -1);
486 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
488 task->simdata->rate = maxrate;
489 return MSG_task_send(task, alias);
494 MSG_task_send_with_timeout(m_task_t task, const char *alias, double timeout)
496 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias), task,
500 int MSG_task_listen(const char *alias)
504 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
507 /** \ingroup msg_gos_functions
508 * \brief Test whether there is a pending communication on a channel.
510 * It takes one parameter.
511 * \param channel the channel on which the agent should be
512 listening. This value has to be >=0 and < than the maximal
513 number of channels fixed with MSG_set_channel_number().
514 * \return 1 if there is a pending communication and 0 otherwise
516 int MSG_task_Iprobe(m_channel_t channel)
518 xbt_assert1((channel >= 0)
519 && (channel < msg_global->max_channel), "Invalid channel %d",
525 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
526 (MSG_host_self(), channel));
529 /** \ingroup msg_gos_functions
531 * \brief Return the number of tasks waiting to be received on a \a
532 channel and sent by \a host.
534 * It takes two parameters.
535 * \param channel the channel on which the agent should be
536 listening. This value has to be >=0 and < than the maximal
537 number of channels fixed with MSG_set_channel_number().
538 * \param host the host that is to be watched.
539 * \return the number of tasks waiting to be received on \a channel
542 int MSG_task_probe_from_host(int channel, m_host_t host)
544 xbt_assert1((channel >= 0)
545 && (channel < msg_global->max_channel), "Invalid channel %d",
551 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
552 (MSG_host_self(), channel),
557 int MSG_task_listen_from_host(const char *alias, m_host_t host)
562 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias(alias),
566 /** \ingroup msg_gos_functions
567 * \brief Test whether there is a pending communication on a channel, and who sent it.
569 * It takes one parameter.
570 * \param channel the channel on which the agent should be
571 listening. This value has to be >=0 and < than the maximal
572 number of channels fixed with MSG_set_channel_number().
573 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
575 int MSG_task_probe_from(m_channel_t channel)
581 xbt_assert1((channel >= 0)
582 && (channel < msg_global->max_channel), "Invalid channel %d",
587 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
588 (MSG_host_self(), channel))))
591 return MSG_process_get_PID(task->simdata->sender);
594 int MSG_task_listen_from(const char *alias)
600 if (NULL == (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
603 return MSG_process_get_PID(task->simdata->sender);
606 /** \ingroup msg_gos_functions
607 * \brief Wait for at most \a max_duration second for a task reception
610 * \a PID is updated with the PID of the first process that triggered this event if any.
612 * It takes three parameters:
613 * \param channel the channel on which the agent should be
614 listening. This value has to be >=0 and < than the maximal.
615 number of channels fixed with MSG_set_channel_number().
616 * \param PID a memory location for storing an int.
617 * \param timeout the maximum time to wait for a task before
618 giving up. In the case of a reception, *\a PID will be updated
619 with the PID of the first process to send a task.
620 * \return #MSG_HOST_FAILURE if the host is shut down in the meantime
621 and #MSG_OK otherwise.
624 MSG_channel_select_from(m_channel_t channel, double timeout, int *PID)
627 simdata_host_t h_simdata = NULL;
631 msg_mailbox_t mailbox;
633 xbt_assert1((channel >= 0)
634 && (channel < msg_global->max_channel), "Invalid channel %d",
641 if (timeout == 0.0) {
642 *PID = MSG_task_probe_from(channel);
647 h_simdata = h->simdata;
649 mailbox = MSG_mailbox_get_by_channel(MSG_host_self(), channel);
651 while (MSG_mailbox_is_empty(mailbox)) {
658 SIMIX_mutex_lock(h_simdata->mutex);
660 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
661 "A process is already blocked on this channel %d", channel);
663 cond = SIMIX_cond_init();
665 MSG_mailbox_set_cond(mailbox, cond);
668 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
670 SIMIX_cond_wait(cond, h_simdata->mutex);
673 SIMIX_cond_destroy(cond);
674 SIMIX_mutex_unlock(h_simdata->mutex);
676 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
677 MSG_RETURN(MSG_HOST_FAILURE);
680 MSG_mailbox_set_cond(mailbox, NULL);
684 if (NULL == (t = MSG_mailbox_get_head(mailbox)))
689 *PID = MSG_process_get_PID(t->simdata->sender);
697 MSG_error_t MSG_alias_select_from(const char *alias, double timeout, int *PID)
700 simdata_host_t h_simdata = NULL;
704 msg_mailbox_t mailbox;
710 if (timeout == 0.0) {
711 *PID = MSG_task_listen_from(alias);
716 h_simdata = h->simdata;
718 DEBUG2("Probing on alias %s (%s)", alias, h->name);
720 mailbox = MSG_mailbox_get_by_alias(alias);
722 while (MSG_mailbox_is_empty(mailbox)) {
729 SIMIX_mutex_lock(h_simdata->mutex);
731 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
732 "A process is already blocked on this alias %s", alias);
734 cond = SIMIX_cond_init();
736 MSG_mailbox_set_cond(mailbox, cond);
739 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
741 SIMIX_cond_wait(cond, h_simdata->mutex);
744 SIMIX_cond_destroy(cond);
745 SIMIX_mutex_unlock(h_simdata->mutex);
747 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
748 MSG_RETURN(MSG_HOST_FAILURE);
751 MSG_mailbox_set_cond(mailbox, NULL);
755 if (NULL == (t = MSG_mailbox_get_head(mailbox)))
760 *PID = MSG_process_get_PID(t->simdata->sender);