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
27 return PROCESS_GET_ERRNO();
30 /** \ingroup msg_gos_functions
31 * \brief Executes a task and waits for its termination.
33 * This function is used for describing the behavior of an agent. It
34 * takes only one parameter.
35 * \param task a #m_task_t to execute on the location on which the
37 * \return #MSG_FATAL if \a task is not properly initialized and
41 MSG_task_execute(m_task_t task)
43 simdata_task_t simdata = NULL;
44 m_process_t self = MSG_process_self();
45 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
48 simdata = task->simdata;
49 xbt_assert0((!simdata->compute) && (task->simdata->refcount == 1),"This task is executed somewhere else. Go fix your code!");
51 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
54 SIMIX_mutex_lock(simdata->mutex);
55 simdata->compute = SIMIX_action_execute(SIMIX_host_self(), task->name, simdata->computation_amount);
56 SIMIX_action_set_priority(simdata->compute, simdata->priority);
58 self->simdata->waiting_task = task;
59 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
61 SIMIX_cond_wait(simdata->cond, simdata->mutex);
62 state = SIMIX_action_get_state(simdata->compute);
63 } while(state==SURF_ACTION_READY || state==SURF_ACTION_RUNNING);
64 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
65 self->simdata->waiting_task = NULL;
67 SIMIX_mutex_unlock(simdata->mutex);
70 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE)
72 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
73 SIMIX_action_destroy(task->simdata->compute);
74 simdata->computation_amount = 0.0;
76 simdata->compute = NULL;
79 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);
89 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
90 SIMIX_action_destroy(task->simdata->compute);
92 simdata->compute = NULL;
93 MSG_RETURN(MSG_TASK_CANCELLED);
97 /** \ingroup m_task_management
98 * \brief Creates a new #m_task_t (a parallel one....).
100 * A constructor for #m_task_t taking six arguments and returning the
101 corresponding object.
102 * \param name a name for the object. It is for user-level information
104 * \param host_nb the number of hosts implied in the parallel task.
105 * \param host_list an array of \p host_nb m_host_t.
106 * \param computation_amount an array of \p host_nb
107 doubles. computation_amount[i] is the total number of operations
108 that have to be performed on host_list[i].
109 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
110 * \param data a pointer to any data may want to attach to the new
111 object. It is for user-level information and can be NULL. It can
112 be retrieved with the function \ref MSG_task_get_data.
114 * \return The new corresponding object.
117 MSG_parallel_task_create(const char *name,int host_nb, const m_host_t * host_list, double *computation_amount, double *communication_amount, void *data)
120 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
121 m_task_t task = xbt_new0(s_m_task_t, 1);
122 task->simdata = simdata;
125 task->name = xbt_strdup(name);
129 simdata->computation_amount = 0;
130 simdata->message_size = 0;
131 simdata->cond = SIMIX_cond_init();
132 simdata->mutex = SIMIX_mutex_init();
133 simdata->compute = NULL;
134 simdata->comm = NULL;
135 simdata->rate = -1.0;
136 simdata->refcount = 1;
137 simdata->sender = NULL;
138 simdata->receiver = NULL;
139 simdata->source = NULL;
141 simdata->host_nb = host_nb;
142 simdata->host_list = xbt_new0(smx_host_t, host_nb);
143 simdata->comp_amount = computation_amount;
144 simdata->comm_amount = communication_amount;
146 for (i = 0; i < host_nb; i++)
147 simdata->host_list[i] = host_list[i]->simdata->smx_host;
153 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) && (task->simdata->refcount == 1),"This task is executed somewhere else. Go fix your code!");
164 xbt_assert0(simdata->host_nb,"This is not a parallel task. Go to hell.");
166 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
168 simdata->refcount ++;
170 SIMIX_mutex_lock(simdata->mutex);
172 SIMIX_action_parallel_execute(task->name, simdata->host_nb, simdata->host_list, simdata->comp_amount, simdata->comm_amount, 1.0, -1.0);
174 self->simdata->waiting_task = task;
175 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
177 SIMIX_cond_wait(simdata->cond, simdata->mutex);
178 state = SIMIX_action_get_state(task->simdata->compute);
179 } while(state==SURF_ACTION_READY || state==SURF_ACTION_RUNNING);
181 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
182 self->simdata->waiting_task = NULL;
185 SIMIX_mutex_unlock(simdata->mutex);
186 simdata->refcount --;
188 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE)
190 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
191 SIMIX_action_destroy(task->simdata->compute);
192 simdata->computation_amount = 0.0;
193 simdata->comm = NULL;
194 simdata->compute = NULL;
197 else if(SIMIX_host_get_state(SIMIX_host_self()) == 0)
199 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
200 SIMIX_action_destroy(task->simdata->compute);
201 simdata->comm = NULL;
202 simdata->compute = NULL;
203 MSG_RETURN(MSG_HOST_FAILURE);
207 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
208 SIMIX_action_destroy(task->simdata->compute);
209 simdata->comm = NULL;
210 simdata->compute = NULL;
211 MSG_RETURN(MSG_TASK_CANCELLED);
217 /** \ingroup msg_gos_functions
218 * \brief Sleep for the specified number of seconds
220 * Makes the current process sleep until \a time seconds have elapsed.
222 * \param nb_sec a number of second
225 MSG_process_sleep(double nb_sec)
227 smx_action_t act_sleep;
228 m_process_t proc = MSG_process_self();
229 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
233 /* create action to sleep */
234 act_sleep = SIMIX_action_sleep(SIMIX_process_get_host(proc->simdata->s_process), nb_sec);
236 mutex = SIMIX_mutex_init();
237 SIMIX_mutex_lock(mutex);
239 /* create conditional and register action to it */
240 cond = SIMIX_cond_init();
242 SIMIX_register_action_to_condition(act_sleep, cond);
244 SIMIX_cond_wait(cond, mutex);
245 state = SIMIX_action_get_state(act_sleep);
246 } while(state==SURF_ACTION_READY || state==SURF_ACTION_RUNNING);
247 SIMIX_unregister_action_to_condition(act_sleep, cond);
248 SIMIX_mutex_unlock(mutex);
250 /* remove variables */
251 SIMIX_cond_destroy(cond);
252 SIMIX_mutex_destroy(mutex);
254 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE)
256 if (SIMIX_host_get_state(SIMIX_host_self()) == SURF_CPU_OFF)
258 SIMIX_action_destroy(act_sleep);
259 MSG_RETURN(MSG_HOST_FAILURE);
264 SIMIX_action_destroy(act_sleep);
265 MSG_RETURN(MSG_HOST_FAILURE);
268 SIMIX_action_destroy(act_sleep);
272 /** \ingroup msg_gos_functions
273 * \brief Return the number of MSG tasks currently running on
274 * the host of the current running process.
277 MSG_get_msgload(void)
279 xbt_die("not implemented yet");
285 /** \ingroup msg_gos_functions
286 * \brief Listen on \a channel and waits for receiving a task from \a host.
288 * It takes three parameters.
289 * \param task a memory location for storing a #m_task_t. It will
290 hold a task when this function will return. Thus \a task should not
291 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
292 those two condition does not hold, there will be a warning message.
293 * \param channel the channel on which the agent should be
294 listening. This value has to be >=0 and < than the maximal
295 number of channels fixed with MSG_set_channel_number().
296 * \param host the host that is to be watched.
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.
301 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
303 return MSG_task_get_ext(task, channel, -1, host);
306 /** \ingroup msg_gos_functions
307 * \brief Listen on a channel and wait for receiving a task.
309 * It takes two parameters.
310 * \param task a memory location for storing a #m_task_t. It will
311 hold a task when this function will return. Thus \a task should not
312 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
313 those two condition does not hold, there will be a warning message.
314 * \param channel the channel on which the agent should be
315 listening. This value has to be >=0 and < than the maximal
316 number of channels fixed with MSG_set_channel_number().
317 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
318 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
321 MSG_task_get(m_task_t * task, m_channel_t channel)
323 return MSG_task_get_with_timeout(task, channel, -1);
326 /** \ingroup msg_gos_functions
327 * \brief Listen on a channel and wait for receiving a task with a timeout.
329 * It takes three parameters.
330 * \param task a memory location for storing a #m_task_t. It will
331 hold a task when this function will return. Thus \a task should not
332 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
333 those two condition does not hold, there will be a warning message.
334 * \param channel the channel on which the agent should be
335 listening. This value has to be >=0 and < than the maximal
336 number of channels fixed with MSG_set_channel_number().
337 * \param max_duration the maximum time to wait for a task before giving
338 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
339 will not be modified and will still be
340 equal to \c NULL when returning.
341 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
342 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
345 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel, double max_duration)
347 return MSG_task_get_ext(task, channel, max_duration, NULL);
350 /** \defgroup msg_gos_functions MSG Operating System Functions
351 * \brief This section describes the functions that can be used
352 * by an agent for handling some task.
356 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,m_host_t host)
358 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d",channel);
360 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel(MSG_host_self(), channel), task, host, timeout);
364 MSG_task_receive_from_host(m_task_t * task, const char* alias, m_host_t host)
366 return MSG_task_receive_ext(task, alias, -1, host);
370 MSG_task_receive(m_task_t * task, const char* alias)
372 return MSG_task_receive_with_timeout(task, alias, -1);
376 MSG_task_receive_with_timeout(m_task_t * task, const char* alias, double timeout)
378 return MSG_task_receive_ext(task, alias, timeout, NULL);
382 MSG_task_receive_ext(m_task_t* task, const char* alias, double timeout, m_host_t host)
384 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host, timeout);
388 /** \ingroup msg_gos_functions
389 * \brief Put a task on a channel of an host and waits for the end of the
392 * This function is used for describing the behavior of an agent. It
393 * takes three parameter.
394 * \param task a #m_task_t to send on another location. This task
395 will not be usable anymore when the function will return. There is
396 no automatic task duplication and you have to save your parameters
397 before calling this function. Tasks are unique and once it has been
398 sent to another location, you should not access it anymore. You do
399 not need to call MSG_task_destroy() but to avoid using, as an
400 effect of inattention, this task anymore, you definitely should
401 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
402 can be transfered iff it has been correctly created with
404 * \param dest the destination of the message
405 * \param channel the channel on which the agent should put this
406 task. This value has to be >=0 and < than the maximal number of
407 channels fixed with MSG_set_channel_number().
408 * \return #MSG_FATAL if \a task is not properly initialized and
409 * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
410 * this function was called was shut down. Returns
411 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
412 * (network failure, dest failure)
415 MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
417 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
420 /** \ingroup msg_gos_functions
421 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
427 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel, double maxrate)
429 task->simdata->rate = maxrate;
430 return MSG_task_put(task, dest, channel);
433 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
434 * host (with a timeout on the waiting of the destination host) and
435 * waits for the end of the transmission.
437 * This function is used for describing the behavior of an agent. It
438 * takes four parameter.
439 * \param task a #m_task_t to send on another location. This task
440 will not be usable anymore when the function will return. There is
441 no automatic task duplication and you have to save your parameters
442 before calling this function. Tasks are unique and once it has been
443 sent to another location, you should not access it anymore. You do
444 not need to call MSG_task_destroy() but to avoid using, as an
445 effect of inattention, this task anymore, you definitely should
446 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
447 can be transfered iff it has been correctly created with
449 * \param dest the destination of the message
450 * \param channel the channel on which the agent should put this
451 task. This value has to be >=0 and < than the maximal number of
452 channels fixed with MSG_set_channel_number().
453 * \param timeout the maximum time to wait for a task before giving
454 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
456 * \return #MSG_FATAL if \a task is not properly initialized and
457 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
458 this function was called was shut down. Returns
459 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
460 (network failure, dest failure, timeout...)
463 MSG_task_put_with_timeout(m_task_t task, m_host_t dest, m_channel_t channel, double timeout)
465 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d", channel);
467 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel(dest, channel), task, timeout);
471 MSG_task_send(m_task_t task,const char* alias)
473 return MSG_task_send_with_timeout(task, alias, -1);
478 MSG_task_send_bounded(m_task_t task, const char* alias, double maxrate)
480 task->simdata->rate = maxrate;
481 return MSG_task_send(task, alias);
486 MSG_task_send_with_timeout(m_task_t task, const char* alias, double timeout)
488 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias), task, timeout);
492 MSG_task_listen(const char* alias)
496 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
499 /** \ingroup msg_gos_functions
500 * \brief Test whether there is a pending communication on a channel.
502 * It takes one parameter.
503 * \param channel the channel on which the agent should be
504 listening. This value has to be >=0 and < than the maximal
505 number of channels fixed with MSG_set_channel_number().
506 * \return 1 if there is a pending communication and 0 otherwise
509 MSG_task_Iprobe(m_channel_t channel)
511 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d", channel);
515 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel(MSG_host_self(), channel));
518 /** \ingroup msg_gos_functions
520 * \brief Return the number of tasks waiting to be received on a \a
521 channel and sent by \a host.
523 * It takes two parameters.
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 * \param host the host that is to be watched.
528 * \return the number of tasks waiting to be received on \a channel
532 MSG_task_probe_from_host(int channel, m_host_t host)
534 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d", channel);
538 return MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel(MSG_host_self(), channel),host);
543 MSG_task_listen_from_host(const char* alias, m_host_t host)
547 return MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias(alias),host);
550 /** \ingroup msg_gos_functions
551 * \brief Test whether there is a pending communication on a channel, and who sent it.
553 * It takes one parameter.
554 * \param channel the channel on which the agent should be
555 listening. This value has to be >=0 and < than the maximal
556 number of channels fixed with MSG_set_channel_number().
557 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
560 MSG_task_probe_from(m_channel_t channel)
566 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d", channel);
568 if(NULL == (task = MSG_mailbox_get_head(MSG_mailbox_get_by_channel(MSG_host_self(), channel))))
571 return MSG_process_get_PID(task->simdata->sender);
575 MSG_task_listen_from(const char* alias)
581 if(NULL == (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
584 return MSG_process_get_PID(task->simdata->sender);
587 /** \ingroup msg_gos_functions
588 * \brief Wait for at most \a max_duration second for a task reception
591 * \a PID is updated with the PID of the first process that triggered this event if any.
593 * It takes three parameters:
594 * \param channel the channel on which the agent should be
595 listening. This value has to be >=0 and < than the maximal.
596 number of channels fixed with MSG_set_channel_number().
597 * \param PID a memory location for storing an int.
598 * \param timeout the maximum time to wait for a task before
599 giving up. In the case of a reception, *\a PID will be updated
600 with the PID of the first process to send a task.
601 * \return #MSG_HOST_FAILURE if the host is shut down in the meantime
602 and #MSG_OK otherwise.
605 MSG_channel_select_from(m_channel_t channel,double timeout, int *PID)
608 simdata_host_t h_simdata = NULL;
612 msg_mailbox_t mailbox;
614 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d",channel);
623 *PID = MSG_task_probe_from(channel);
630 h_simdata = h->simdata;
632 mailbox = MSG_mailbox_get_by_channel(MSG_host_self(), channel);
634 while(MSG_mailbox_is_empty(mailbox))
644 SIMIX_mutex_lock(h_simdata->mutex);
646 xbt_assert1(!MSG_mailbox_get_cond(mailbox),"A process is already blocked on this channel %d",channel);
648 cond = SIMIX_cond_init();
650 MSG_mailbox_set_cond(mailbox, cond);
654 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
658 SIMIX_cond_wait(cond, h_simdata->mutex);
661 SIMIX_cond_destroy(cond);
662 SIMIX_mutex_unlock(h_simdata->mutex);
664 if (SIMIX_host_get_state(h_simdata->smx_host) == 0)
666 MSG_RETURN(MSG_HOST_FAILURE);
669 MSG_mailbox_set_cond(mailbox,NULL);
673 if(NULL == (t = MSG_mailbox_get_head(mailbox)))
679 *PID = MSG_process_get_PID(t->simdata->sender);
688 MSG_alias_select_from(const char* alias, double timeout, int* PID)
691 simdata_host_t h_simdata = NULL;
695 msg_mailbox_t mailbox;
704 *PID = MSG_task_listen_from(alias);
711 h_simdata = h->simdata;
713 DEBUG2("Probing on alias %s (%s)", alias, h->name);
715 mailbox = MSG_mailbox_get_by_alias(alias);
717 while(MSG_mailbox_is_empty(mailbox))
727 SIMIX_mutex_lock(h_simdata->mutex);
729 xbt_assert1(!MSG_mailbox_get_cond(mailbox),"A process is already blocked on this alias %s",alias);
731 cond = SIMIX_cond_init();
733 MSG_mailbox_set_cond(mailbox, cond);
737 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)
749 MSG_RETURN(MSG_HOST_FAILURE);
752 MSG_mailbox_set_cond(mailbox,NULL);
756 if(NULL == (t = MSG_mailbox_get_head(mailbox)))
762 *PID = MSG_process_get_PID(t->simdata->sender);