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. */
10 #include "xbt/error.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 /* \ingroup msg_gos_functions
20 * \brief This function is now deprecated and useless. Please stop using it.
22 MSG_error_t MSG_process_start(m_process_t process)
24 xbt_assert0(0,"This function is now deprecated and useless. Please stop using it.");
29 /** \ingroup msg_gos_functions
30 * \brief Listen on a channel and wait for receiving a task.
32 * It takes two parameters.
33 * \param task a memory location for storing a #m_task_t. It will
34 hold a task when this function will return. Thus \a task should not
35 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
36 those two condition does not hold, there will be a warning message.
37 * \param channel the channel on which the agent should be
38 listening. This value has to be >=0 and < than the maximal
39 number of channels fixed with MSG_set_channel_number().
40 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
41 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
43 MSG_error_t MSG_task_get(m_task_t * task,
46 return MSG_task_get_with_time_out(task, channel, -1);
49 /** \ingroup msg_gos_functions
50 * \brief Listen on a channel and wait for receiving a task with a timeout.
52 * It takes three parameters.
53 * \param task a memory location for storing a #m_task_t. It will
54 hold a task when this function will return. Thus \a task should not
55 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
56 those two condition does not hold, there will be a warning message.
57 * \param channel the channel on which the agent should be
58 listening. This value has to be >=0 and < than the maximal
59 number of channels fixed with MSG_set_channel_number().
60 * \param max_duration the maximum time to wait for a task before giving
61 up. In such a case, \a task will not be modified and will still be
62 equal to \c NULL when returning.
63 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
64 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
67 MSG_error_t MSG_task_get_with_time_out(m_task_t * task,
71 m_process_t process = MSG_process_self();
74 simdata_task_t t_simdata = NULL;
75 simdata_host_t h_simdata = NULL;
78 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
82 xbt_assert0(task,"Null pointer for the task\n");
85 CRITICAL0("MSG_task_get() was asked to write in a non empty task struct.");
89 h_simdata = h->simdata;
91 DEBUG2("Waiting for a task on channel %d (%s)", channel,h->name);
93 while ((t = xbt_fifo_shift(h_simdata->mbox[channel])) == NULL) {
99 xbt_assert2(!(h_simdata->sleeping[channel]),
100 "A process (%s(%d)) is already blocked on this channel",
101 h_simdata->sleeping[channel]->name,
102 h_simdata->sleeping[channel]->simdata->PID);
103 h_simdata->sleeping[channel] = process; /* I'm waiting. Wake me up when you're ready */
105 __MSG_process_block(max_duration);
107 __MSG_process_block(-1);
109 if(surf_workstation_resource->extension_public->get_state(h_simdata->host)
111 MSG_RETURN(MSG_HOST_FAILURE);
112 h_simdata->sleeping[channel] = NULL;
114 /* OK, we should both be ready now. Are you there ? */
117 t_simdata = t->simdata;
118 /* *task = __MSG_task_copy(t); */
124 t_simdata->comm = surf_workstation_resource->extension_public->
125 communicate(MSG_process_get_host(t_simdata->sender)->simdata->host,
126 h->simdata->host, t_simdata->message_size,t_simdata->rate);
128 surf_workstation_resource->common_public->action_set_data(t_simdata->comm,t);
130 if(__MSG_process_isBlocked(t_simdata->sender))
131 __MSG_process_unblock(t_simdata->sender);
133 PAJE_PROCESS_PUSH_STATE(process,"C");
136 __MSG_task_wait_event(process, t);
137 state=surf_workstation_resource->common_public->action_get_state(t_simdata->comm);
138 } while (state==SURF_ACTION_RUNNING);
140 if(t->simdata->using>1) {
141 xbt_fifo_unshift(msg_global->process_to_run,process);
145 PAJE_PROCESS_POP_STATE(process);
146 PAJE_COMM_STOP(process,t,channel);
148 if(state == SURF_ACTION_DONE) {
149 if(surf_workstation_resource->common_public->action_free(t_simdata->comm))
150 t_simdata->comm = NULL;
152 } else if(surf_workstation_resource->extension_public->get_state(h_simdata->host)
154 if(surf_workstation_resource->common_public->action_free(t_simdata->comm))
155 t_simdata->comm = NULL;
156 MSG_RETURN(MSG_HOST_FAILURE);
158 if(surf_workstation_resource->common_public->action_free(t_simdata->comm))
159 t_simdata->comm = NULL;
160 MSG_RETURN(MSG_TRANSFER_FAILURE);
164 /** \ingroup msg_gos_functions
165 * \brief Test whether there is a pending communication on a channel.
167 * It takes one parameter.
168 * \param channel the channel on which the agent should be
169 listening. This value has to be >=0 and < than the maximal
170 number of channels fixed with MSG_set_channel_number().
171 * \return 1 if there is a pending communication and 0 otherwise
173 int MSG_task_Iprobe(m_channel_t channel)
176 simdata_host_t h_simdata = NULL;
178 DEBUG2("Probing on channel %d (%s)", channel,h->name);
181 h_simdata = h->simdata;
182 return(xbt_fifo_getFirstItem(h_simdata->mbox[channel])!=NULL);
185 /** \ingroup msg_gos_functions
186 * \brief Test whether there is a pending communication on a channel, and who sent it.
188 * It takes one parameter.
189 * \param channel the channel on which the agent should be
190 listening. This value has to be >=0 and < than the maximal
191 number of channels fixed with MSG_set_channel_number().
192 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
194 int MSG_task_probe_from(m_channel_t channel)
197 simdata_host_t h_simdata = NULL;
198 xbt_fifo_item_t item;
203 h_simdata = h->simdata;
205 DEBUG2("Probing on channel %d (%s)", channel,h->name);
207 item = xbt_fifo_getFirstItem(h->simdata->mbox[channel]);
208 if (!item || !(t = xbt_fifo_get_item_content(item)))
211 return MSG_process_get_PID(t->simdata->sender);
214 MSG_error_t MSG_channel_select_from(m_channel_t channel, double max_duration,
218 simdata_host_t h_simdata = NULL;
219 xbt_fifo_item_t item;
222 m_process_t process = MSG_process_self();
228 if(max_duration==0.0) {
229 return MSG_task_probe_from(channel);
233 h_simdata = h->simdata;
235 DEBUG2("Probing on channel %d (%s)", channel,h->name);
236 while(!(item = xbt_fifo_getFirstItem(h->simdata->mbox[channel]))) {
242 xbt_assert2(!(h_simdata->sleeping[channel]),
243 "A process (%s(%d)) is already blocked on this channel",
244 h_simdata->sleeping[channel]->name,
245 h_simdata->sleeping[channel]->simdata->PID);
246 h_simdata->sleeping[channel] = process; /* I'm waiting. Wake me up when you're ready */
248 __MSG_process_block(max_duration);
250 __MSG_process_block(-1);
252 if(surf_workstation_resource->extension_public->get_state(h_simdata->host)
254 MSG_RETURN(MSG_HOST_FAILURE);
256 h_simdata->sleeping[channel] = NULL;
259 if (!item || !(t = xbt_fifo_get_item_content(item))) {
263 *PID = MSG_process_get_PID(t->simdata->sender);
268 /** \ingroup msg_gos_functions
269 * \brief Put a task on a channel of an host and waits for the end of the
272 * This function is used for describing the behavior of an agent. It
273 * takes three parameter.
274 * \param task a #m_task_t to send on another location. This task
275 will not be usable anymore when the function will return. There is
276 no automatic task duplication and you have to save your parameters
277 before calling this function. Tasks are unique and once it has been
278 sent to another location, you should not access it anymore. You do
279 not need to call MSG_task_destroy() but to avoid using, as an
280 effect of inattention, this task anymore, you definitely should
281 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
282 can be transfered iff it has been correctly created with
284 * \param dest the destination of the message
285 * \param channel the channel on which the agent should put this
286 task. This value has to be >=0 and < than the maximal number of
287 channels fixed with MSG_set_channel_number().
288 * \return #MSG_FATAL if \a task is not properly initialized and
291 MSG_error_t MSG_task_put(m_task_t task,
292 m_host_t dest, m_channel_t channel)
294 m_process_t process = MSG_process_self();
295 simdata_task_t task_simdata = NULL;
296 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
297 m_host_t local_host = NULL;
298 m_host_t remote_host = NULL;
302 task_simdata = task->simdata;
303 task_simdata->sender = process;
304 xbt_assert0(task_simdata->using==1,"Gargl!");
305 task_simdata->comm = NULL;
307 local_host = ((simdata_process_t) process->simdata)->host;
310 DEBUG4("Trying to send a task (%g Mb) from %s to %s on channel %d",
311 task->simdata->message_size,local_host->name, remote_host->name, channel);
313 xbt_fifo_push(((simdata_host_t) remote_host->simdata)->
314 mbox[channel], task);
316 PAJE_COMM_START(process,task,channel);
318 if(remote_host->simdata->sleeping[channel])
319 __MSG_process_unblock(remote_host->simdata->sleeping[channel]);
321 process->simdata->put_host = dest;
322 process->simdata->put_channel = channel;
323 while(!(task_simdata->comm))
324 __MSG_process_block(-1);
325 surf_workstation_resource->common_public->action_use(task_simdata->comm);
326 process->simdata->put_host = NULL;
327 process->simdata->put_channel = -1;
330 PAJE_PROCESS_PUSH_STATE(process,"C");
332 state=surf_workstation_resource->common_public->action_get_state(task_simdata->comm);
333 while (state==SURF_ACTION_RUNNING) {
334 __MSG_task_wait_event(process, task);
335 state=surf_workstation_resource->common_public->action_get_state(task_simdata->comm);
339 PAJE_PROCESS_POP_STATE(process);
341 if(state == SURF_ACTION_DONE) {
342 if(surf_workstation_resource->common_public->action_free(task_simdata->comm))
343 task_simdata->comm = NULL;
344 MSG_task_destroy(task);
346 } else if(surf_workstation_resource->extension_public->get_state(local_host->simdata->host)
348 if(surf_workstation_resource->common_public->action_free(task_simdata->comm))
349 task_simdata->comm = NULL;
350 MSG_task_destroy(task);
351 MSG_RETURN(MSG_HOST_FAILURE);
353 if(surf_workstation_resource->common_public->action_free(task_simdata->comm))
354 task_simdata->comm = NULL;
355 MSG_task_destroy(task);
356 MSG_RETURN(MSG_TRANSFER_FAILURE);
360 /** \ingroup msg_gos_functions
361 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
366 MSG_error_t MSG_task_put_bounded(m_task_t task,
367 m_host_t dest, m_channel_t channel,
370 task->simdata->rate=max_rate;
371 return(MSG_task_put(task, dest, channel));
372 task->simdata->rate=-1.0;
375 /** \ingroup msg_gos_functions
376 * \brief Executes a task and waits for its termination.
378 * This function is used for describing the behavior of an agent. It
379 * takes only one parameter.
380 * \param task a #m_task_t to execute on the location on which the
382 * \return #MSG_FATAL if \a task is not properly initialized and
385 MSG_error_t MSG_task_execute(m_task_t task)
387 m_process_t process = MSG_process_self();
390 DEBUG1("Computing on %s", process->simdata->host->name);
392 __MSG_task_execute(process, task);
394 PAJE_PROCESS_PUSH_STATE(process,"E");
395 res = __MSG_wait_for_computation(process,task);
396 PAJE_PROCESS_POP_STATE(process);
400 void __MSG_task_execute(m_process_t process, m_task_t task)
402 simdata_task_t simdata = NULL;
406 simdata = task->simdata;
408 simdata->compute = surf_workstation_resource->extension_public->
409 execute(MSG_process_get_host(process)->simdata->host,
410 simdata->computation_amount);
411 surf_workstation_resource->common_public->action_set_data(simdata->compute,task);
414 MSG_error_t __MSG_wait_for_computation(m_process_t process, m_task_t task)
416 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
417 simdata_task_t simdata = task->simdata;
421 __MSG_task_wait_event(process, task);
422 state=surf_workstation_resource->common_public->action_get_state(simdata->compute);
423 } while (state==SURF_ACTION_RUNNING);
427 if(state == SURF_ACTION_DONE) {
428 if(surf_workstation_resource->common_public->action_free(simdata->compute))
429 simdata->compute = NULL;
431 } else if(surf_workstation_resource->extension_public->
432 get_state(MSG_process_get_host(process)->simdata->host)
434 if(surf_workstation_resource->common_public->action_free(simdata->compute))
435 simdata->compute = NULL;
436 MSG_RETURN(MSG_HOST_FAILURE);
438 if(surf_workstation_resource->common_public->action_free(simdata->compute))
439 simdata->compute = NULL;
440 MSG_RETURN(MSG_TRANSFER_FAILURE);
444 m_task_t MSG_parallel_task_create(const char *name,
446 const m_host_t *host_list,
447 double *computation_amount,
448 double *communication_amount,
451 simdata_task_t simdata = xbt_new0(s_simdata_task_t,1);
452 m_task_t task = xbt_new0(s_m_task_t,1);
456 task->name = xbt_strdup(name);
457 task->simdata = simdata;
461 simdata->sleeping = xbt_dynar_new(sizeof(m_process_t),NULL);
462 simdata->rate = -1.0;
464 simdata->sender = NULL;
465 simdata->host_nb = host_nb;
467 simdata->host_list = xbt_new0(void *, host_nb);
468 simdata->comp_amount = computation_amount;
469 simdata->comm_amount = communication_amount;
471 for(i=0;i<host_nb;i++)
472 simdata->host_list[i] = host_list[i]->simdata->host;
478 static void __MSG_parallel_task_execute(m_process_t process, m_task_t task)
480 simdata_task_t simdata = NULL;
484 simdata = task->simdata;
486 xbt_assert0(simdata->host_nb,"This is not a parallel task. Go to hell.");
488 simdata->compute = surf_workstation_resource->extension_public->
489 execute_parallel_task(task->simdata->host_nb,
490 task->simdata->host_list,
491 task->simdata->comp_amount,
492 task->simdata->comm_amount,
496 surf_workstation_resource->common_public->action_set_data(simdata->compute,task);
499 MSG_error_t MSG_parallel_task_execute(m_task_t task)
501 m_process_t process = MSG_process_self();
504 DEBUG0("Computing on a tons of guys");
506 __MSG_parallel_task_execute(process, task);
508 if(task->simdata->compute)
509 res = __MSG_wait_for_computation(process,task);
517 /** \ingroup msg_gos_functions
518 * \brief Sleep for the specified number of seconds
520 * Makes the current process sleep until \a time seconds have elapsed.
522 * \param nb_sec a number of second
524 MSG_error_t MSG_process_sleep(double nb_sec)
526 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
527 m_process_t process = MSG_process_self();
528 m_task_t dummy = NULL;
529 simdata_task_t simdata = NULL;
532 dummy = MSG_task_create("MSG_sleep", nb_sec, 0.0, NULL);
533 simdata = dummy->simdata;
535 simdata->compute = surf_workstation_resource->extension_public->
536 sleep(MSG_process_get_host(process)->simdata->host,
537 simdata->computation_amount);
538 surf_workstation_resource->common_public->action_set_data(simdata->compute,dummy);
543 __MSG_task_wait_event(process, dummy);
544 state=surf_workstation_resource->common_public->action_get_state(simdata->compute);
545 } while (state==SURF_ACTION_RUNNING);
548 if(state == SURF_ACTION_DONE) {
549 if(surf_workstation_resource->extension_public->
550 get_state(MSG_process_get_host(process)->simdata->host)
552 if(surf_workstation_resource->common_public->action_free(simdata->compute))
553 simdata->compute = NULL;
554 MSG_RETURN(MSG_HOST_FAILURE);
556 if(__MSG_process_isBlocked(process)) {
557 __MSG_process_unblock(MSG_process_self());
559 if(surf_workstation_resource->extension_public->
560 get_state(MSG_process_get_host(process)->simdata->host)
562 if(surf_workstation_resource->common_public->action_free(simdata->compute))
563 simdata->compute = NULL;
564 MSG_RETURN(MSG_HOST_FAILURE);
566 if(surf_workstation_resource->common_public->action_free(simdata->compute))
567 simdata->compute = NULL;
568 MSG_task_destroy(dummy);
570 } else MSG_RETURN(MSG_HOST_FAILURE);
573 /** \ingroup msg_gos_functions
574 * \brief Return the number of MSG tasks currently running on a
575 * the host of the current running process.
577 static int MSG_get_msgload(void)
583 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");
584 process = MSG_process_self();
585 return xbt_fifo_size(process->simdata->host->simdata->process_list);
588 /** \ingroup msg_gos_functions
590 * \brief Return the the last value returned by a MSG function (except
593 MSG_error_t MSG_get_errno(void)
595 return PROCESS_GET_ERRNO();