1 #include "msg_simix_private.h"
2 #include "xbt/sysdep.h"
5 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
7 /** \defgroup msg_gos_functions MSG Operating System Functions
8 * \brief This section describes the functions that can be used
9 * by an agent for handling some task.
12 static MSG_error_t __MSG_task_get_with_time_out_from_host(m_task_t * task,
18 m_process_t process = MSG_process_self();
21 simdata_task_t t_simdata = NULL;
22 simdata_host_t h_simdata = NULL;
24 xbt_fifo_item_t item = NULL;
26 smx_cond_t cond = NULL; //conditional wait if the task isn't on the channel yet
29 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
31 xbt_assert0(task,"Null pointer for the task\n");
34 CRITICAL0("MSG_task_get() was asked to write in a non empty task struct.");
38 h_simdata = h->simdata;
40 DEBUG2("Waiting for a task on channel %d (%s)", channel,h->name);
42 SIMIX_mutex_lock(h->simdata->mutex);
44 if(xbt_fifo_size(h_simdata->mbox[channel])>0) {
46 t = xbt_fifo_shift(h_simdata->mbox[channel]);
49 xbt_fifo_foreach(h->simdata->mbox[channel],item,t,m_task_t) {
50 if(t->simdata->source==host) break;
53 xbt_fifo_remove_item(h->simdata->mbox[channel],item);
61 MSG_RETURN(MSG_TRANSFER_FAILURE);
64 xbt_assert1(!(h_simdata->sleeping[channel]),"A process is already blocked on channel %d", channel);
66 cond = SIMIX_cond_init();
67 h_simdata->sleeping[channel] = cond;
68 if (max_duration > 0) {
69 SIMIX_cond_wait_timeout(cond, h->simdata->mutex, max_duration);
71 else SIMIX_cond_wait(h_simdata->sleeping[channel],h->simdata->mutex);
75 SIMIX_mutex_unlock(h->simdata->mutex);
77 DEBUG1("OK, got a task (%s)", t->name);
78 /* clean conditional */
80 SIMIX_cond_destroy(cond);
81 h_simdata->sleeping[channel] = NULL;
84 t_simdata = t->simdata;
85 t_simdata->receiver = process;
88 SIMIX_mutex_lock(t_simdata->mutex);
92 /* create SIMIX action to the communication */
93 t_simdata->comm = SIMIX_action_communicate(t_simdata->sender->simdata->host->simdata->host,
94 process->simdata->host->simdata->host,t->name, t_simdata->message_size,
96 /* if the process is suspend, create the action but stop its execution, it will be restart when the sender process resume */
97 if (MSG_process_is_suspended(t_simdata->sender)) {
98 DEBUG1("Process sender (%s) suspended", t_simdata->sender->name);
99 SIMIX_action_set_priority(t_simdata->comm,0);
101 process->simdata->waiting_task = t;
102 SIMIX_register_action_to_condition(t_simdata->comm, t_simdata->cond);
103 SIMIX_register_condition_to_action(t_simdata->comm, t_simdata->cond);
104 SIMIX_cond_wait(t_simdata->cond,t_simdata->mutex);
105 process->simdata->waiting_task = NULL;
107 /* the task has already finished and the pointer must be null*/
108 if (t->simdata->sender) {
109 t->simdata->sender->simdata->waiting_task = NULL;
110 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
111 t->simdata->comm = NULL;
112 t->simdata->compute = NULL;
114 /* for this process, don't need to change in get function*/
115 t->simdata->receiver = NULL;
116 SIMIX_mutex_unlock(t_simdata->mutex);
120 /** \ingroup msg_gos_functions
121 * \brief Listen on a channel and wait for receiving a task.
123 * It takes two parameters.
124 * \param task a memory location for storing a #m_task_t. It will
125 hold a task when this function will return. Thus \a task should not
126 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
127 those two condition does not hold, there will be a warning message.
128 * \param channel the channel on which the agent should be
129 listening. This value has to be >=0 and < than the maximal
130 number of channels fixed with MSG_set_channel_number().
131 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
132 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
134 MSG_error_t MSG_task_get(m_task_t * task,
137 return MSG_task_get_with_time_out(task, channel, -1);
140 /** \ingroup msg_gos_functions
141 * \brief Listen on a channel and wait for receiving a task with a timeout.
143 * It takes three parameters.
144 * \param task a memory location for storing a #m_task_t. It will
145 hold a task when this function will return. Thus \a task should not
146 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
147 those two condition does not hold, there will be a warning message.
148 * \param channel the channel on which the agent should be
149 listening. This value has to be >=0 and < than the maximal
150 number of channels fixed with MSG_set_channel_number().
151 * \param max_duration the maximum time to wait for a task before giving
152 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
153 will not be modified and will still be
154 equal to \c NULL when returning.
155 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
156 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
158 MSG_error_t MSG_task_get_with_time_out(m_task_t * task,
162 return __MSG_task_get_with_time_out_from_host(task, channel, max_duration, NULL);
165 /** \ingroup msg_gos_functions
166 * \brief Listen on \a channel and waits for receiving a task from \a host.
168 * It takes three parameters.
169 * \param task a memory location for storing a #m_task_t. It will
170 hold a task when this function will return. Thus \a task should not
171 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
172 those two condition does not hold, there will be a warning message.
173 * \param channel the channel on which the agent should be
174 listening. This value has to be >=0 and < than the maximal
175 number of channels fixed with MSG_set_channel_number().
176 * \param host the host that is to be watched.
177 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
178 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
180 MSG_error_t MSG_task_get_from_host(m_task_t * task, int channel,
183 return __MSG_task_get_with_time_out_from_host(task, channel, -1, host);
186 /** \ingroup msg_gos_functions
187 * \brief Test whether there is a pending communication on a channel.
189 * It takes one parameter.
190 * \param channel the channel on which the agent should be
191 listening. This value has to be >=0 and < than the maximal
192 number of channels fixed with MSG_set_channel_number().
193 * \return 1 if there is a pending communication and 0 otherwise
195 int MSG_task_Iprobe(m_channel_t channel)
199 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
202 DEBUG2("Probing on channel %d (%s)", channel,h->name);
205 return(xbt_fifo_get_first_item(h->simdata->mbox[channel])!=NULL);
208 /** \ingroup msg_gos_functions
209 * \brief Test whether there is a pending communication on a channel, and who sent it.
211 * It takes one parameter.
212 * \param channel the channel on which the agent should be
213 listening. This value has to be >=0 and < than the maximal
214 number of channels fixed with MSG_set_channel_number().
215 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
217 int MSG_task_probe_from(m_channel_t channel)
220 xbt_fifo_item_t item;
223 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
228 DEBUG2("Probing on channel %d (%s)", channel,h->name);
230 item = xbt_fifo_get_first_item(h->simdata->mbox[channel]);
231 if ( (!item) || (!(t = xbt_fifo_get_item_content(item))) )
234 return MSG_process_get_PID(t->simdata->sender);
237 /** \ingroup msg_gos_functions
238 * \brief Wait for at most \a max_duration second for a task reception
239 on \a channel. *\a PID is updated with the PID of the first process
240 that triggered this event if any.
242 * It takes three parameters:
243 * \param channel the channel on which the agent should be
244 listening. This value has to be >=0 and < than the maximal.
245 number of channels fixed with MSG_set_channel_number().
246 * \param PID a memory location for storing an int.
247 * \param max_duration the maximum time to wait for a task before
248 giving up. In the case of a reception, *\a PID will be updated
249 with the PID of the first process to send a task.
250 * \return #MSG_HOST_FAILURE if the host is shut down in the meantime
251 and #MSG_OK otherwise.
253 MSG_error_t MSG_channel_select_from(m_channel_t channel, double max_duration,
257 simdata_host_t h_simdata = NULL;
258 xbt_fifo_item_t item;
263 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
268 if(max_duration==0.0) {
269 *PID = MSG_task_probe_from(channel);
274 h_simdata = h->simdata;
276 DEBUG2("Probing on channel %d (%s)", channel,h->name);
277 while(!(item = xbt_fifo_get_first_item(h->simdata->mbox[channel]))) {
283 SIMIX_mutex_lock(h_simdata->mutex);
284 xbt_assert1(!(h_simdata->sleeping[channel]),
285 "A process is already blocked on this channel %d", channel);
286 cond = SIMIX_cond_init();
287 h_simdata->sleeping[channel] = cond; /* I'm waiting. Wake me up when you're ready */
289 SIMIX_cond_wait_timeout(cond,h_simdata->mutex, max_duration);
291 SIMIX_cond_wait(cond,h_simdata->mutex);
293 SIMIX_cond_destroy(cond);
294 SIMIX_mutex_unlock(h_simdata->mutex);
295 if(SIMIX_host_get_state(h_simdata->host)==0) {
296 MSG_RETURN(MSG_HOST_FAILURE);
298 h_simdata->sleeping[channel] = NULL;
301 if (!item || !(t = xbt_fifo_get_item_content(item))) {
305 *PID = MSG_process_get_PID(t->simdata->sender);
312 /** \ingroup msg_gos_functions
314 * \brief Return the number of tasks waiting to be received on a \a
315 channel and sent by \a host.
317 * It takes two parameters.
318 * \param channel the channel on which the agent should be
319 listening. This value has to be >=0 and < than the maximal
320 number of channels fixed with MSG_set_channel_number().
321 * \param host the host that is to be watched.
322 * \return the number of tasks waiting to be received on \a channel
325 int MSG_task_probe_from_host(int channel, m_host_t host)
327 xbt_fifo_item_t item;
332 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
336 DEBUG2("Probing on channel %d (%s)", channel,h->name);
338 xbt_fifo_foreach(h->simdata->mbox[channel],item,t,m_task_t) {
339 if(t->simdata->source==host) count++;
345 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
346 * host (with a timeout on the waiting of the destination host) and
347 * waits for the end of the transmission.
349 * This function is used for describing the behavior of an agent. It
350 * takes four parameter.
351 * \param task a #m_task_t to send on another location. This task
352 will not be usable anymore when the function will return. There is
353 no automatic task duplication and you have to save your parameters
354 before calling this function. Tasks are unique and once it has been
355 sent to another location, you should not access it anymore. You do
356 not need to call MSG_task_destroy() but to avoid using, as an
357 effect of inattention, this task anymore, you definitely should
358 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
359 can be transfered iff it has been correctly created with
361 * \param dest the destination of the message
362 * \param channel the channel on which the agent should put this
363 task. This value has to be >=0 and < than the maximal number of
364 channels fixed with MSG_set_channel_number().
365 * \param max_duration the maximum time to wait for a task before giving
366 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
368 * \return #MSG_FATAL if \a task is not properly initialized and
369 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
370 this function was called was shut down. Returns
371 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
372 (network failure, dest failure, timeout...)
374 MSG_error_t MSG_task_put_with_timeout(m_task_t task, m_host_t dest,
375 m_channel_t channel, double max_duration)
379 m_process_t process = MSG_process_self();
380 simdata_task_t task_simdata = NULL;
381 m_host_t local_host = NULL;
382 m_host_t remote_host = NULL;
385 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
387 task_simdata = task->simdata;
388 task_simdata->sender = process;
389 task_simdata->source = MSG_process_get_host(process);
390 xbt_assert0(task_simdata->using==1,
391 "This taks is still being used somewhere else. You cannot send it now. Go fix your code!");
392 task_simdata->comm = NULL;
394 local_host = ((simdata_process_t) process->simdata)->host;
397 DEBUG4("Trying to send a task (%g kB) from %s to %s on channel %d",
398 task->simdata->message_size/1000,local_host->name, remote_host->name, channel);
400 SIMIX_mutex_lock(remote_host->simdata->mutex);
401 xbt_fifo_push(((simdata_host_t) remote_host->simdata)->
402 mbox[channel], task);
405 if(remote_host->simdata->sleeping[channel]) {
406 DEBUG0("Somebody is listening. Let's wake him up!");
407 SIMIX_cond_signal(remote_host->simdata->sleeping[channel]);
409 SIMIX_mutex_unlock(remote_host->simdata->mutex);
411 process->simdata->put_host = dest;
412 process->simdata->put_channel = channel;
413 SIMIX_mutex_lock(task->simdata->mutex);
414 // DEBUG4("Task sent (%g kB) from %s to %s on channel %d, waiting...", task->simdata->message_size/1000,local_host->name, remote_host->name, channel);
416 process->simdata->waiting_task = task;
417 if (max_duration >0) {
418 SIMIX_cond_wait_timeout(task->simdata->cond,task->simdata->mutex,max_duration);
421 SIMIX_cond_wait(task->simdata->cond,task->simdata->mutex);
423 DEBUG1("Action terminated %s",task->name);
424 task->simdata->using--;
425 process->simdata->waiting_task = NULL;
426 /* the task has already finished and the pointer must be null*/
427 if (task->simdata->receiver) {
428 task->simdata->receiver->simdata->waiting_task = NULL;
429 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
430 task->simdata->comm = NULL;
431 task->simdata->compute = NULL;
433 /* for this process, don't need to change in get function*/
434 task->simdata->sender = NULL;
435 SIMIX_mutex_unlock(task->simdata->mutex);
441 /** \ingroup msg_gos_functions
442 * \brief Put a task on a channel of an host and waits for the end of the
445 * This function is used for describing the behavior of an agent. It
446 * takes three parameter.
447 * \param task a #m_task_t to send on another location. This task
448 will not be usable anymore when the function will return. There is
449 no automatic task duplication and you have to save your parameters
450 before calling this function. Tasks are unique and once it has been
451 sent to another location, you should not access it anymore. You do
452 not need to call MSG_task_destroy() but to avoid using, as an
453 effect of inattention, this task anymore, you definitely should
454 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
455 can be transfered iff it has been correctly created with
457 * \param dest the destination of the message
458 * \param channel the channel on which the agent should put this
459 task. This value has to be >=0 and < than the maximal number of
460 channels fixed with MSG_set_channel_number().
461 * \return #MSG_FATAL if \a task is not properly initialized and
462 * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
463 * this function was called was shut down. Returns
464 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
465 * (network failure, dest failure)
467 MSG_error_t MSG_task_put(m_task_t task,
468 m_host_t dest, m_channel_t channel)
470 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
473 /** \ingroup msg_gos_functions
474 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
479 MSG_error_t MSG_task_put_bounded(m_task_t task,
480 m_host_t dest, m_channel_t channel,
483 MSG_error_t res = MSG_OK;
484 task->simdata->rate=max_rate;
485 res = MSG_task_put(task, dest, channel);
489 /** \ingroup msg_gos_functions
490 * \brief Executes a task and waits for its termination.
492 * This function is used for describing the behavior of an agent. It
493 * takes only one parameter.
494 * \param task a #m_task_t to execute on the location on which the
496 * \return #MSG_FATAL if \a task is not properly initialized and
499 MSG_error_t MSG_task_execute(m_task_t task)
501 simdata_task_t simdata = NULL;
502 m_process_t self = MSG_process_self();
505 simdata = task->simdata;
506 xbt_assert0((!simdata->compute)&&(task->simdata->using==1),
507 "This taks is executed somewhere else. Go fix your code!");
509 DEBUG1("Computing on %s", MSG_process_self()->simdata->host->name);
511 SIMIX_mutex_lock(simdata->mutex);
512 simdata->compute = SIMIX_action_execute(SIMIX_host_self(), task->name, simdata->computation_amount);
513 SIMIX_action_set_priority(simdata->compute, simdata->priority);
515 self->simdata->waiting_task = task;
516 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
517 SIMIX_register_condition_to_action(simdata->compute, simdata->cond);
518 SIMIX_cond_wait(simdata->cond, simdata->mutex);
519 self->simdata->waiting_task = NULL;
521 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
522 simdata->comm = NULL;
523 simdata->compute = NULL;
525 SIMIX_mutex_unlock(simdata->mutex);
531 /** \ingroup m_task_management
532 * \brief Creates a new #m_task_t (a parallel one....).
534 * A constructor for #m_task_t taking six arguments and returning the
535 corresponding object.
536 * \param name a name for the object. It is for user-level information
538 * \param host_nb the number of hosts implied in the parallel task.
539 * \param host_list an array of \p host_nb m_host_t.
540 * \param computation_amount an array of \p host_nb
541 doubles. computation_amount[i] is the total number of operations
542 that have to be performed on host_list[i].
543 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
544 * \param data a pointer to any data may want to attach to the new
545 object. It is for user-level information and can be NULL. It can
546 be retrieved with the function \ref MSG_task_get_data.
548 * \return The new corresponding object.
550 m_task_t MSG_parallel_task_create(const char *name,
552 const m_host_t *host_list,
553 double *computation_amount,
554 double *communication_amount,
557 m_task_t task = xbt_new0(s_m_task_t,1);
558 xbt_die("not implemented yet");
563 static void __MSG_parallel_task_execute(m_process_t process, m_task_t task)
568 MSG_error_t MSG_parallel_task_execute(m_task_t task)
571 xbt_die("not implemented yet");
576 /** \ingroup msg_gos_functions
577 * \brief Sleep for the specified number of seconds
579 * Makes the current process sleep until \a time seconds have elapsed.
581 * \param nb_sec a number of second
583 MSG_error_t MSG_process_sleep(double nb_sec)
585 smx_action_t act_sleep;
586 m_process_t proc = MSG_process_self();
589 /* create action to sleep */
590 act_sleep = SIMIX_action_sleep(SIMIX_process_get_host(proc->simdata->smx_process),nb_sec);
592 mutex = SIMIX_mutex_init();
593 SIMIX_mutex_lock(mutex);
594 /* create conditional and register action to it */
595 cond = SIMIX_cond_init();
597 SIMIX_register_condition_to_action(act_sleep, cond);
598 SIMIX_register_action_to_condition(act_sleep, cond);
599 SIMIX_cond_wait(cond,mutex);
600 SIMIX_mutex_unlock(mutex);
602 /* remove variables */
603 SIMIX_cond_destroy(cond);
604 SIMIX_mutex_destroy(mutex);
609 /** \ingroup msg_gos_functions
610 * \brief Return the number of MSG tasks currently running on
611 * the host of the current running process.
613 static int MSG_get_msgload(void)
615 xbt_die("not implemented yet");
619 /** \ingroup msg_gos_functions
621 * \brief Return the last value returned by a MSG function (except
624 MSG_error_t MSG_get_errno(void)
626 return PROCESS_GET_ERRNO();