3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/sysdep.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gos, msg,
12 "Logging specific to MSG (gos)");
14 /** \defgroup msg_gos_functions MSG Operating System Functions
15 * \brief This section describes the functions that can be used
16 * by an agent for handling some task.
19 static MSG_error_t __MSG_task_get_with_time_out_from_host(m_task_t * task,
24 m_process_t process = MSG_process_self();
27 simdata_task_t t_simdata = NULL;
28 simdata_host_t h_simdata = NULL;
30 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
31 xbt_fifo_item_t item = NULL;
34 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
36 xbt_assert0(task,"Null pointer for the task\n");
39 CRITICAL0("MSG_task_get() was asked to write in a non empty task struct.");
43 h_simdata = h->simdata;
45 DEBUG2("Waiting for a task on channel %d (%s)", channel,h->name);
48 if(xbt_fifo_size(h_simdata->mbox[channel])>0) {
50 t = xbt_fifo_shift(h_simdata->mbox[channel]);
53 xbt_fifo_foreach(h->simdata->mbox[channel],item,t,m_task_t) {
54 if(t->simdata->source==host) break;
57 xbt_fifo_remove_item(h->simdata->mbox[channel],item);
68 xbt_assert2(!(h_simdata->sleeping[channel]),
69 "A process (%s(%d)) is already blocked on this channel",
70 h_simdata->sleeping[channel]->name,
71 h_simdata->sleeping[channel]->simdata->PID);
72 h_simdata->sleeping[channel] = process; /* I'm waiting. Wake me up when you're ready */
74 __MSG_process_block(max_duration);
76 __MSG_process_block(-1);
78 if(surf_workstation_resource->extension_public->get_state(h_simdata->host)
80 MSG_RETURN(MSG_HOST_FAILURE);
81 h_simdata->sleeping[channel] = NULL;
83 /* OK, we should both be ready now. Are you there ? */
86 DEBUG1("OK, got a task (%s)", t->name);
88 t_simdata = t->simdata;
89 /* *task = __MSG_task_copy(t); */
95 DEBUG0("Calling SURF for communication creation");
96 t_simdata->comm = surf_workstation_resource->extension_public->
97 communicate(MSG_process_get_host(t_simdata->sender)->simdata->host,
98 h->simdata->host, t_simdata->message_size,t_simdata->rate);
100 surf_workstation_resource->common_public->action_set_data(t_simdata->comm,t);
102 if(__MSG_process_isBlocked(t_simdata->sender))
103 __MSG_process_unblock(t_simdata->sender);
105 PAJE_PROCESS_PUSH_STATE(process,"C");
108 DEBUG0("Waiting for action termination");
109 __MSG_task_wait_event(process, t);
110 state=surf_workstation_resource->common_public->action_get_state(t_simdata->comm);
111 } while (state==SURF_ACTION_RUNNING);
112 DEBUG0("Action terminated");
114 if(t->simdata->using>1) {
115 xbt_fifo_unshift(msg_global->process_to_run,process);
119 PAJE_PROCESS_POP_STATE(process);
120 PAJE_COMM_STOP(process,t,channel);
122 if(state == SURF_ACTION_DONE) {
123 if(surf_workstation_resource->common_public->action_free(t_simdata->comm))
124 t_simdata->comm = NULL;
126 } else if(surf_workstation_resource->extension_public->get_state(h_simdata->host)
128 if(surf_workstation_resource->common_public->action_free(t_simdata->comm))
129 t_simdata->comm = NULL;
130 MSG_RETURN(MSG_HOST_FAILURE);
132 if(surf_workstation_resource->common_public->action_free(t_simdata->comm))
133 t_simdata->comm = NULL;
134 MSG_RETURN(MSG_TRANSFER_FAILURE);
138 /** \ingroup msg_gos_functions
139 * \brief Listen on a channel and wait for receiving a task.
141 * It takes two parameters.
142 * \param task a memory location for storing a #m_task_t. It will
143 hold a task when this function will return. Thus \a task should not
144 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
145 those two condition does not hold, there will be a warning message.
146 * \param channel the channel on which the agent should be
147 listening. This value has to be >=0 and < than the maximal
148 number of channels fixed with MSG_set_channel_number().
149 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
150 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
152 MSG_error_t MSG_task_get(m_task_t * task,
155 return MSG_task_get_with_time_out(task, channel, -1);
158 /** \ingroup msg_gos_functions
159 * \brief Listen on a channel and wait for receiving a task with a timeout.
161 * It takes three parameters.
162 * \param task a memory location for storing a #m_task_t. It will
163 hold a task when this function will return. Thus \a task should not
164 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
165 those two condition does not hold, there will be a warning message.
166 * \param channel the channel on which the agent should be
167 listening. This value has to be >=0 and < than the maximal
168 number of channels fixed with MSG_set_channel_number().
169 * \param max_duration the maximum time to wait for a task before giving
170 up. In such a case, \a task will not be modified and will still be
171 equal to \c NULL when returning.
172 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
173 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
175 MSG_error_t MSG_task_get_with_time_out(m_task_t * task,
179 return __MSG_task_get_with_time_out_from_host(task, channel, max_duration, NULL);
182 /** \ingroup msg_gos_functions
183 * \brief Listen on \a channel and waits for receiving a task from \a host.
185 * It takes three parameters.
186 * \param task a memory location for storing a #m_task_t. It will
187 hold a task when this function will return. Thus \a task should not
188 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
189 those two condition does not hold, there will be a warning message.
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 * \param host the host that is to be watched.
194 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
195 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
197 MSG_error_t MSG_task_get_from_host(m_task_t * task, int channel,
200 return __MSG_task_get_with_time_out_from_host(task, channel, -1, host);
203 /** \ingroup msg_gos_functions
204 * \brief Test whether there is a pending communication on a channel.
206 * It takes one parameter.
207 * \param channel the channel on which the agent should be
208 listening. This value has to be >=0 and < than the maximal
209 number of channels fixed with MSG_set_channel_number().
210 * \return 1 if there is a pending communication and 0 otherwise
212 int MSG_task_Iprobe(m_channel_t channel)
215 simdata_host_t h_simdata = NULL;
217 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
218 DEBUG2("Probing on channel %d (%s)", channel,h->name);
221 h_simdata = h->simdata;
222 return(xbt_fifo_get_first_item(h_simdata->mbox[channel])!=NULL);
225 /** \ingroup msg_gos_functions
226 * \brief Test whether there is a pending communication on a channel, and who sent it.
228 * It takes one parameter.
229 * \param channel the channel on which the agent should be
230 listening. This value has to be >=0 and < than the maximal
231 number of channels fixed with MSG_set_channel_number().
232 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
234 int MSG_task_probe_from(m_channel_t channel)
237 simdata_host_t h_simdata = NULL;
238 xbt_fifo_item_t item;
241 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
244 h_simdata = h->simdata;
246 DEBUG2("Probing on channel %d (%s)", channel,h->name);
248 item = xbt_fifo_get_first_item(h->simdata->mbox[channel]);
249 if (!item || !(t = xbt_fifo_get_item_content(item)))
252 return MSG_process_get_PID(t->simdata->sender);
255 /** \ingroup msg_gos_functions
256 * \brief Wait for at most \a max_duration second for a task reception
257 on \a channel. *\a PID is updated with the PID of the first process
258 that triggered this event is any.
260 * It takes three parameters:
261 * \param channel the channel on which the agent should be
262 listening. This value has to be >=0 and < than the maximal.
263 number of channels fixed with MSG_set_channel_number().
264 * \param PID a memory location for storing an int.
265 * \param max_duration the maximum time to wait for a task before
266 giving up. In the case of a reception, *\a PID will be updated
267 with the PID of the first process to send a task.
268 * \return #MSG_HOST_FAILURE if the host is shut down in the meantime
269 and #MSG_OK otherwise.
271 MSG_error_t MSG_channel_select_from(m_channel_t channel, double max_duration,
275 simdata_host_t h_simdata = NULL;
276 xbt_fifo_item_t item;
279 m_process_t process = MSG_process_self();
281 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
286 if(max_duration==0.0) {
287 return MSG_task_probe_from(channel);
291 h_simdata = h->simdata;
293 DEBUG2("Probing on channel %d (%s)", channel,h->name);
294 while(!(item = xbt_fifo_get_first_item(h->simdata->mbox[channel]))) {
300 xbt_assert2(!(h_simdata->sleeping[channel]),
301 "A process (%s(%d)) is already blocked on this channel",
302 h_simdata->sleeping[channel]->name,
303 h_simdata->sleeping[channel]->simdata->PID);
304 h_simdata->sleeping[channel] = process; /* I'm waiting. Wake me up when you're ready */
306 __MSG_process_block(max_duration);
308 __MSG_process_block(-1);
310 if(surf_workstation_resource->extension_public->get_state(h_simdata->host)
312 MSG_RETURN(MSG_HOST_FAILURE);
314 h_simdata->sleeping[channel] = NULL;
317 if (!item || !(t = xbt_fifo_get_item_content(item))) {
321 *PID = MSG_process_get_PID(t->simdata->sender);
328 /** \ingroup msg_gos_functions
330 * \brief Return the number of tasks waiting to be received on a \a
331 channel and sent by \a host.
333 * It takes two parameters.
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 host the host that is to be watched.
338 * \return the number of tasks waiting to be received on \a channel
341 int MSG_task_probe_from_host(int channel, m_host_t host)
343 simdata_host_t h_simdata = NULL;
344 xbt_fifo_item_t item;
349 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
352 h_simdata = h->simdata;
354 DEBUG2("Probing on channel %d (%s)", channel,h->name);
356 xbt_fifo_foreach(h->simdata->mbox[channel],item,t,m_task_t) {
357 if(t->simdata->source==host) count++;
363 /** \ingroup msg_gos_functions
364 * \brief Put a task on a channel of an host and waits for the end of the
367 * This function is used for describing the behavior of an agent. It
368 * takes three parameter.
369 * \param task a #m_task_t to send on another location. This task
370 will not be usable anymore when the function will return. There is
371 no automatic task duplication and you have to save your parameters
372 before calling this function. Tasks are unique and once it has been
373 sent to another location, you should not access it anymore. You do
374 not need to call MSG_task_destroy() but to avoid using, as an
375 effect of inattention, this task anymore, you definitely should
376 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
377 can be transfered iff it has been correctly created with
379 * \param dest the destination of the message
380 * \param channel the channel on which the agent should put this
381 task. This value has to be >=0 and < than the maximal number of
382 channels fixed with MSG_set_channel_number().
383 * \return #MSG_FATAL if \a task is not properly initialized and
386 MSG_error_t MSG_task_put(m_task_t task,
387 m_host_t dest, m_channel_t channel)
389 m_process_t process = MSG_process_self();
390 simdata_task_t task_simdata = NULL;
391 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
392 m_host_t local_host = NULL;
393 m_host_t remote_host = NULL;
397 xbt_assert1((channel>=0) && (channel < msg_global->max_channel),"Invalid channel %d",channel);
399 task_simdata = task->simdata;
400 task_simdata->sender = process;
401 task_simdata->source = MSG_process_get_host(process);
402 xbt_assert0(task_simdata->using==1,"Gargl!");
403 task_simdata->comm = NULL;
405 local_host = ((simdata_process_t) process->simdata)->host;
408 DEBUG4("Trying to send a task (%g Mb) from %s to %s on channel %d",
409 task->simdata->message_size,local_host->name, remote_host->name, channel);
411 xbt_fifo_push(((simdata_host_t) remote_host->simdata)->
412 mbox[channel], task);
414 PAJE_COMM_START(process,task,channel);
416 if(remote_host->simdata->sleeping[channel]) {
417 DEBUG0("Somebody is listening. Let's wake him up!");
418 __MSG_process_unblock(remote_host->simdata->sleeping[channel]);
421 process->simdata->put_host = dest;
422 process->simdata->put_channel = channel;
423 while(!(task_simdata->comm)) {
424 DEBUG0("Communication not initiated yet. Let's block!");
425 __MSG_process_block(-1);
427 DEBUG0("Registering to this communication");
428 surf_workstation_resource->common_public->action_use(task_simdata->comm);
429 process->simdata->put_host = NULL;
430 process->simdata->put_channel = -1;
433 PAJE_PROCESS_PUSH_STATE(process,"C");
435 state=surf_workstation_resource->common_public->action_get_state(task_simdata->comm);
436 while (state==SURF_ACTION_RUNNING) {
437 DEBUG0("Waiting for action termination");
438 __MSG_task_wait_event(process, task);
439 state=surf_workstation_resource->common_public->action_get_state(task_simdata->comm);
441 DEBUG0("Action terminated");
443 PAJE_PROCESS_POP_STATE(process);
445 if(state == SURF_ACTION_DONE) {
446 if(surf_workstation_resource->common_public->action_free(task_simdata->comm))
447 task_simdata->comm = NULL;
448 MSG_task_destroy(task);
450 } else if(surf_workstation_resource->extension_public->get_state(local_host->simdata->host)
452 if(surf_workstation_resource->common_public->action_free(task_simdata->comm))
453 task_simdata->comm = NULL;
454 MSG_task_destroy(task);
455 MSG_RETURN(MSG_HOST_FAILURE);
457 if(surf_workstation_resource->common_public->action_free(task_simdata->comm))
458 task_simdata->comm = NULL;
459 MSG_task_destroy(task);
460 MSG_RETURN(MSG_TRANSFER_FAILURE);
464 /** \ingroup msg_gos_functions
465 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
470 MSG_error_t MSG_task_put_bounded(m_task_t task,
471 m_host_t dest, m_channel_t channel,
474 MSG_error_t res = MSG_OK;
475 task->simdata->rate=max_rate;
476 res = MSG_task_put(task, dest, channel);
477 task->simdata->rate=-1.0;
481 /** \ingroup msg_gos_functions
482 * \brief Executes a task and waits for its termination.
484 * This function is used for describing the behavior of an agent. It
485 * takes only one parameter.
486 * \param task a #m_task_t to execute on the location on which the
488 * \return #MSG_FATAL if \a task is not properly initialized and
491 MSG_error_t MSG_task_execute(m_task_t task)
493 m_process_t process = MSG_process_self();
496 DEBUG1("Computing on %s", process->simdata->host->name);
498 __MSG_task_execute(process, task);
500 PAJE_PROCESS_PUSH_STATE(process,"E");
501 res = __MSG_wait_for_computation(process,task);
502 PAJE_PROCESS_POP_STATE(process);
506 void __MSG_task_execute(m_process_t process, m_task_t task)
508 simdata_task_t simdata = NULL;
512 simdata = task->simdata;
514 simdata->compute = surf_workstation_resource->extension_public->
515 execute(MSG_process_get_host(process)->simdata->host,
516 simdata->computation_amount);
517 surf_workstation_resource->common_public->
518 set_priority(simdata->compute, simdata->priority);
520 surf_workstation_resource->common_public->action_set_data(simdata->compute,task);
523 MSG_error_t __MSG_wait_for_computation(m_process_t process, m_task_t task)
525 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
526 simdata_task_t simdata = task->simdata;
530 __MSG_task_wait_event(process, task);
531 state=surf_workstation_resource->common_public->action_get_state(simdata->compute);
532 } while (state==SURF_ACTION_RUNNING);
536 if(state == SURF_ACTION_DONE) {
537 if(surf_workstation_resource->common_public->action_free(simdata->compute))
538 simdata->compute = NULL;
539 simdata->computation_amount = 0.0;
541 } else if(surf_workstation_resource->extension_public->
542 get_state(MSG_process_get_host(process)->simdata->host)
544 if(surf_workstation_resource->common_public->action_free(simdata->compute))
545 simdata->compute = NULL;
546 MSG_RETURN(MSG_HOST_FAILURE);
548 if(surf_workstation_resource->common_public->action_free(simdata->compute))
549 simdata->compute = NULL;
550 MSG_RETURN(MSG_TASK_CANCELLED);
553 /** \ingroup m_task_management
554 * \brief Creates a new #m_task_t (a parallel one....).
556 * A constructor for #m_task_t taking six arguments and returning the
557 corresponding object.
558 * \param name a name for the object. It is for user-level information
560 * \param host_nb the number of hosts implied in the parallel task.
561 * \param host_list an array of \p host_nb m_host_t.
562 * \param computation_amount an array of \p host_nb
563 doubles. computation_amount[i] is the total number of operations
564 that have to be performed on host_list[i].
565 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
566 * \param data a pointer to any data may want to attach to the new
567 object. It is for user-level information and can be NULL. It can
568 be retrieved with the function \ref MSG_task_get_data.
570 * \return The new corresponding object.
572 m_task_t MSG_parallel_task_create(const char *name,
574 const m_host_t *host_list,
575 double *computation_amount,
576 double *communication_amount,
579 simdata_task_t simdata = xbt_new0(s_simdata_task_t,1);
580 m_task_t task = xbt_new0(s_m_task_t,1);
584 task->name = xbt_strdup(name);
585 task->simdata = simdata;
589 simdata->sleeping = xbt_dynar_new(sizeof(m_process_t),NULL);
590 simdata->rate = -1.0;
592 simdata->sender = NULL;
593 simdata->source = NULL;
594 simdata->host_nb = host_nb;
596 simdata->host_list = xbt_new0(void *, host_nb);
597 simdata->comp_amount = computation_amount;
598 simdata->comm_amount = communication_amount;
600 for(i=0;i<host_nb;i++)
601 simdata->host_list[i] = host_list[i]->simdata->host;
607 static void __MSG_parallel_task_execute(m_process_t process, m_task_t task)
609 simdata_task_t simdata = NULL;
613 simdata = task->simdata;
615 xbt_assert0(simdata->host_nb,"This is not a parallel task. Go to hell.");
617 simdata->compute = surf_workstation_resource->extension_public->
618 execute_parallel_task(task->simdata->host_nb,
619 task->simdata->host_list,
620 task->simdata->comp_amount,
621 task->simdata->comm_amount,
625 surf_workstation_resource->common_public->action_set_data(simdata->compute,task);
628 MSG_error_t MSG_parallel_task_execute(m_task_t task)
630 m_process_t process = MSG_process_self();
633 DEBUG0("Computing on a tons of guys");
635 __MSG_parallel_task_execute(process, task);
637 if(task->simdata->compute)
638 res = __MSG_wait_for_computation(process,task);
646 /** \ingroup msg_gos_functions
647 * \brief Sleep for the specified number of seconds
649 * Makes the current process sleep until \a time seconds have elapsed.
651 * \param nb_sec a number of second
653 MSG_error_t MSG_process_sleep(double nb_sec)
655 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
656 m_process_t process = MSG_process_self();
657 m_task_t dummy = NULL;
658 simdata_task_t simdata = NULL;
661 dummy = MSG_task_create("MSG_sleep", nb_sec, 0.0, NULL);
662 simdata = dummy->simdata;
664 simdata->compute = surf_workstation_resource->extension_public->
665 sleep(MSG_process_get_host(process)->simdata->host,
666 simdata->computation_amount);
667 surf_workstation_resource->common_public->action_set_data(simdata->compute,dummy);
672 __MSG_task_wait_event(process, dummy);
673 state=surf_workstation_resource->common_public->action_get_state(simdata->compute);
674 } while (state==SURF_ACTION_RUNNING);
677 if(state == SURF_ACTION_DONE) {
678 if(surf_workstation_resource->extension_public->
679 get_state(MSG_process_get_host(process)->simdata->host)
681 if(surf_workstation_resource->common_public->action_free(simdata->compute))
682 simdata->compute = NULL;
683 MSG_RETURN(MSG_HOST_FAILURE);
685 if(__MSG_process_isBlocked(process)) {
686 __MSG_process_unblock(MSG_process_self());
688 if(surf_workstation_resource->extension_public->
689 get_state(MSG_process_get_host(process)->simdata->host)
691 if(surf_workstation_resource->common_public->action_free(simdata->compute))
692 simdata->compute = NULL;
693 MSG_RETURN(MSG_HOST_FAILURE);
695 if(surf_workstation_resource->common_public->action_free(simdata->compute))
696 simdata->compute = NULL;
697 MSG_task_destroy(dummy);
699 } else MSG_RETURN(MSG_HOST_FAILURE);
702 /** \ingroup msg_gos_functions
703 * \brief Return the number of MSG tasks currently running on a
704 * the host of the current running process.
706 static int MSG_get_msgload(void)
712 xbt_assert0(0, "This function is still to be specified correctly (what do you mean by 'load', exactly?). In the meantime, please don't use it");
713 process = MSG_process_self();
714 return xbt_fifo_size(process->simdata->host->simdata->process_list);
717 /** \ingroup msg_gos_functions
719 * \brief Return the the last value returned by a MSG function (except
722 MSG_error_t MSG_get_errno(void)
724 return PROCESS_GET_ERRNO();