Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Check that a host is available before launching a process.
[simgrid.git] / src / simix / smx_process.c
1 //*     $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
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. */
7
8 #include "private.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/dict.h"
12 #include "msg/mailbox.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
15                                 "Logging specific to SIMIX (process)");
16
17 /******************************** Process ************************************/
18 /**
19  * \brief Creates and runs a new #smx_process_t.
20  *
21  * Does exactly the same as #SIMIX_process_create_with_arguments but without 
22    providing standard arguments (\a argc, \a argv).
23  * \see SIMIX_process_create_with_arguments
24  */
25
26
27 void SIMIX_process_cleanup(void *arg)
28 {
29   xbt_swag_remove(arg, simix_global->process_list);
30   xbt_swag_remove(arg, simix_global->process_to_run);
31   xbt_swag_remove(arg,
32                   ((smx_process_t) arg)->simdata->smx_host->
33                   simdata->process_list);
34   free(((smx_process_t) arg)->name);
35   ((smx_process_t) arg)->name = NULL;
36
37   free(((smx_process_t) arg)->simdata);
38   ((smx_process_t) arg)->simdata = NULL;
39   free(arg);
40 }
41
42 /** 
43  * \brief Creates and runs a new #smx_process_t.
44  *
45  * A constructor for #m_process_t taking four arguments and returning the corresponding object. The structure (and the corresponding thread) is created, and put in the list of ready process.
46  *
47  * \param name a name for the object. It is for user-level information and can be NULL.
48 * \param data a pointer to any data one may want to attach to the new object.  It is for user-level information and can be NULL. It can be retrieved with the function \ref MSG_process_get_data.
49  * \param host the location where the new agent is executed.
50  * \param argc first argument passed to \a code
51  * \param argv second argument passed to \a code
52  * \param clean_process_function The cleanup function of user process. It will be called when the process finish. This function have to call the SIMIX_process_cleanup. 
53  * \see smx_process_t
54  * \return The new corresponding object.
55  */
56 smx_process_t SIMIX_process_create(const char *name,
57                                    xbt_main_func_t code, void *data,
58                                    const char *hostname, int argc,
59                                    char **argv, xbt_dict_t properties)
60 {
61   smx_simdata_process_t simdata = NULL;
62   smx_process_t process = NULL;
63   smx_process_t self = NULL;
64   smx_host_t host = SIMIX_host_get_by_name(hostname);
65
66   if (!SIMIX_host_get_state(host)) {
67     WARN2("Cannot launch process '%s' on failed host '%s'", name, hostname);
68     return NULL;
69   }
70   simdata = xbt_new0(s_smx_simdata_process_t, 1);
71   process = xbt_new0(s_smx_process_t, 1);
72   /*char alias[MAX_ALIAS_NAME + 1] = {0};
73      msg_mailbox_t mailbox; */
74
75   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
76   /* Simulator Data */
77
78   simdata->smx_host = host;
79   simdata->mutex = NULL;
80   simdata->cond = NULL;
81   simdata->argc = argc;
82   simdata->argv = argv;
83   simdata->context = xbt_context_new(name, code, NULL, NULL,
84                                      simix_global->cleanup_process_function,
85                                      process, simdata->argc, simdata->argv);
86
87   /* Process structure */
88   process->name = xbt_strdup(name);
89   process->simdata = simdata;
90   process->data = data;
91
92   /* Add properties */
93   simdata->properties = properties;
94
95   xbt_swag_insert(process, host->simdata->process_list);
96
97   /* fix current_process, about which xbt_context_start mocks around */
98   self = simix_global->current_process;
99   xbt_context_start(process->simdata->context);
100   simix_global->current_process = self;
101
102   xbt_swag_insert(process, simix_global->process_list);
103   DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
104   xbt_swag_insert(process, simix_global->process_to_run);
105
106   /*sprintf(alias,"%s:%s",hostname,process->name);
107
108      mailbox = MSG_mailbox_new(alias);
109      MSG_mailbox_set_hostname(mailbox, hostname); */
110
111   return process;
112 }
113
114 /** 
115  * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
116  *
117  * Warning: this should only be used in libsimgrid4java, since it create
118  * a context with no code, which leads to segfaults in plain libsimgrid 
119  */
120 void SIMIX_jprocess_create(const char *name, smx_host_t host,
121                            void *data,
122                            void *jprocess, void *jenv, smx_process_t * res)
123 {
124   smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);
125   smx_process_t process = xbt_new0(s_smx_process_t, 1);
126   smx_process_t self = NULL;
127
128   /* HACK: We need this trick because when we xbt_context_new() do
129      syncronization stuff, the s_process field in the m_process needs 
130      to have a valid value, and we call xbt_context_new() before 
131      returning, of course, ie, before providing a right value to the 
132      caller (Java_simgrid_msg_Msg_processCreate) have time to store it  
133      in place. This way, we initialize the m_process->simdata->s_process 
134      field ourself ASAP.
135
136      All this would be much simpler if the synchronization stuff would be done
137      in the JAVA world, I think.
138    */
139   *res = process;
140
141
142
143   DEBUG5("jprocess_create(name=%s,host=%p,data=%p,jproc=%p,jenv=%p)",
144          name, host, data, jprocess, jenv);
145   xbt_assert0(host, "Invalid parameters");
146   /* Simulator Data */
147   simdata->smx_host = host;
148   simdata->mutex = NULL;
149   simdata->cond = NULL;
150   simdata->argc = 0;
151   simdata->argv = NULL;
152
153
154   simdata->context = xbt_context_new(name, NULL, NULL, jprocess,
155                                      simix_global->cleanup_process_function,
156                                      process,
157                                      /* argc/argv */ 0, NULL);
158
159   /* Process structure */
160   process->name = xbt_strdup(name);
161   process->simdata = simdata;
162   process->data = data;
163
164   xbt_swag_insert(process, host->simdata->process_list);
165
166   /* fix current_process, about which xbt_context_start mocks around */
167   self = simix_global->current_process;
168
169   xbt_context_start(process->simdata->context);
170
171   simix_global->current_process = self;
172
173   xbt_swag_insert(process, simix_global->process_list);
174   DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
175   xbt_swag_insert(process, simix_global->process_to_run);
176
177 }
178
179
180 /** \brief Kill a SIMIX process
181  *
182  * This function simply kills a \a process... scarry isn't it ? :).
183  * \param process poor victim
184  *
185  */
186 void SIMIX_process_kill(smx_process_t process)
187 {
188   smx_simdata_process_t p_simdata = process->simdata;
189
190   DEBUG2("Killing process %s on %s", process->name,
191          p_simdata->smx_host->name);
192
193   /* Cleanup if we were waiting for something */
194   if (p_simdata->mutex)
195     xbt_swag_remove(process, p_simdata->mutex->sleeping);
196
197   if (p_simdata->cond)
198     xbt_swag_remove(process, p_simdata->cond->sleeping);
199
200   xbt_swag_remove(process, simix_global->process_to_run);
201   xbt_swag_remove(process, simix_global->process_list);
202   DEBUG2("%p here! killing %p", simix_global->current_process, process);
203   xbt_context_kill(process->simdata->context);
204
205   if (process == SIMIX_process_self()) {
206     /* I just killed myself */
207     xbt_context_yield();
208   }
209 }
210
211 /**
212  * \brief Return the user data of a #smx_process_t.
213  *
214  * This functions checks whether \a process is a valid pointer or not and return the user data associated to \a process if it is possible.
215  * \param process SIMIX process
216  * \return A void pointer to the user data
217  */
218 void *SIMIX_process_get_data(smx_process_t process)
219 {
220   xbt_assert0((process != NULL), "Invalid parameters");
221
222   return (process->data);
223 }
224
225 /**
226  * \brief Set the user data of a #m_process_t.
227  *
228  * This functions checks whether \a process is a valid pointer or not and set the user data associated to \a process if it is possible.
229  * \param process SIMIX process
230  * \param data User data
231  */
232 void SIMIX_process_set_data(smx_process_t process, void *data)
233 {
234   xbt_assert0((process != NULL), "Invalid parameters");
235   //xbt_assert0((process->data == NULL), "Data already set");
236
237   process->data = data;
238
239   return;
240 }
241
242 /**
243  * \brief Return the location on which an agent is running.
244  *
245  * This functions checks whether \a process is a valid pointer or not and return the m_host_t corresponding to the location on which \a process is running.
246  * \param process SIMIX process
247  * \return SIMIX host
248  */
249 smx_host_t SIMIX_process_get_host(smx_process_t process)
250 {
251   xbt_assert0(((process != NULL)
252                && (process->simdata)), "Invalid parameters");
253
254   return (process->simdata->smx_host);
255 }
256
257 /**
258  * \brief Return the name of an agent.
259  *
260  * This functions checks whether \a process is a valid pointer or not and return its name.
261  * \param process SIMIX process
262  * \return The process name
263  */
264 const char *SIMIX_process_get_name(smx_process_t process)
265 {
266   xbt_assert0(((process != NULL)
267                && (process->simdata)), "Invalid parameters");
268
269   return (process->name);
270 }
271
272 /**
273  * \brief Changes the name of an agent.
274  *
275  * This functions checks whether \a process is a valid pointer or not and return its name.
276  * \param process SIMIX process
277  * \param name The new process name
278  */
279 void SIMIX_process_set_name(smx_process_t process, char *name)
280 {
281   xbt_assert0(((process != NULL)
282                && (process->simdata)), "Invalid parameters");
283
284   process->name = name;
285 }
286
287 /** \ingroup m_process_management
288  * \brief Return the properties
289  *
290  * This functions returns the properties associated with this process
291  */
292 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
293 {
294   return process->simdata->properties;
295 }
296
297 /**
298  * \brief Return the current agent.
299  *
300  * This functions returns the currently running #smx_process_t.
301  * \return The SIMIX process
302  */
303 smx_process_t SIMIX_process_self(void)
304 {
305   return simix_global ? simix_global->current_process : NULL;
306 }
307
308 /**
309  * \brief Suspend the process.
310  *
311  * This functions suspend the process by suspending the action on
312  * which it was waiting for the completion.
313  *
314  * \param process SIMIX process
315  */
316 void SIMIX_process_suspend(smx_process_t process)
317 {
318   smx_simdata_process_t simdata = NULL;
319
320   xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
321
322   if (process != SIMIX_process_self()) {
323     simdata = process->simdata;
324
325     if (simdata->mutex) {
326       /* process blocked on a mutex, only set suspend=1 */
327       simdata->suspended = 1;
328     } else if (simdata->cond) {
329       /* process blocked cond, suspend all actions */
330
331       /* temporaries variables */
332       smx_cond_t c;
333       xbt_fifo_item_t i;
334       smx_action_t act;
335
336       simdata->suspended = 1;
337       c = simdata->cond;
338       xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
339         surf_workstation_model->common_public->suspend(act->
340                                                        simdata->surf_action);
341       }
342     } else {
343       simdata->suspended = 1;
344     }
345   } else {
346     /* process executing, I can create an action and suspend it */
347     smx_action_t dummy;
348     smx_cond_t cond;
349     char name[] = "dummy";
350     process->simdata->suspended = 1;
351
352     cond = SIMIX_cond_init();
353     dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
354     surf_workstation_model->common_public->suspend(dummy->simdata->
355                                                    surf_action);
356     SIMIX_register_action_to_condition(dummy, cond);
357     __SIMIX_cond_wait(cond);
358     SIMIX_unregister_action_to_condition(dummy, cond);
359     SIMIX_action_destroy(dummy);
360     SIMIX_cond_destroy(cond);
361   }
362   return;
363 }
364
365 /**
366  * \brief Resume a suspended process.
367  *
368  * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
369  * \param process SIMIX process
370  */
371 void SIMIX_process_resume(smx_process_t process)
372 {
373   smx_simdata_process_t simdata = NULL;
374
375   xbt_assert0(((process != NULL)
376                && (process->simdata)), "Invalid parameters");
377   SIMIX_CHECK_HOST();
378
379   if (process == SIMIX_process_self()) {
380     return;
381   }
382
383   simdata = process->simdata;
384   if (simdata->mutex) {
385     DEBUG0("Resume process blocked on a mutex");
386     simdata->suspended = 0;     /* He'll wake up by itself */
387     return;
388   } else if (simdata->cond) {
389     /* temporaries variables */
390     smx_cond_t c;
391     xbt_fifo_item_t i;
392     smx_action_t act;
393     DEBUG0("Resume process blocked on a conditional");
394     simdata->suspended = 0;
395     c = simdata->cond;
396     xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
397       surf_workstation_model->common_public->resume(act->simdata->
398                                                     surf_action);
399     }
400     SIMIX_cond_signal(c);
401     return;
402   } else {
403     simdata->suspended = 0;
404     xbt_swag_insert(process, simix_global->process_to_run);
405   }
406
407 }
408
409 /**
410  * \brief Migrates an agent to another location.
411  *
412  * This function changes the value of the host on which \a process is running.
413  */
414 void SIMIX_process_change_host(smx_process_t process, char *source,
415                                char *dest)
416 {
417   smx_simdata_process_t p_simdata = process->simdata;
418   smx_host_t h1 = SIMIX_host_get_by_name(source);
419   smx_host_t h2 = SIMIX_host_get_by_name(dest);
420   p_simdata->smx_host = h2;
421   xbt_swag_remove(process, h1->simdata->process_list);
422   xbt_swag_insert(process, h2->simdata->process_list);
423 }
424
425 /**
426  * \brief Returns true if the process is suspended .
427  *
428  * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
429  * \param process SIMIX process
430  * \return 1, if the process is suspended, else 0.
431  */
432 int SIMIX_process_is_suspended(smx_process_t process)
433 {
434   xbt_assert0(((process != NULL)
435                && (process->simdata)), "Invalid parameters");
436
437   return (process->simdata->suspended);
438 }