Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
80fac61320bdc2e1a785d6826c9828b90998bfcb
[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 Move a process to the list of process to destroy. *
20  */
21
22 void SIMIX_process_cleanup(void *arg)
23 {
24   xbt_swag_remove(arg, simix_global->process_to_run);
25   xbt_swag_remove(arg, simix_global->process_list);
26   xbt_swag_remove(arg, ((smx_process_t)arg)->smx_host->process_list);
27   xbt_swag_insert(arg, simix_global->process_to_destroy);
28 }
29
30 /** 
31  * Garbage collection
32  *
33  * Should be called some time to time to free the memory allocated for processes
34  * that have finished (or killed).
35  */
36 void SIMIX_process_empty_trash(void)
37
38   smx_process_t process = NULL;
39   int i;  
40
41   while ((process = xbt_swag_extract(simix_global->process_to_destroy))){
42     free(process->name);
43     process->name = NULL;
44   
45     if (process->argv) {
46       for (i = 0; i < process->argc; i++)
47         if (process->argv[i])
48           free(process->argv[i]);
49
50       free(process->argv);
51     }
52   
53     free(process);
54   }
55 }
56
57 /**
58  * \brief Creates and runs the maestro process
59  *
60  */
61 void __SIMIX_create_maestro_process()
62 {
63   smx_process_t process = NULL;
64   process = xbt_new0(s_smx_process_t, 1);
65
66   /* Process data */
67   process->name = (char *)"";
68
69   /* Create the right context type */
70   process->context = SIMIX_context_create_maestro();
71
72   /* Set it as the maestro process */
73   simix_global->maestro_process = process;
74   simix_global->current_process = process;
75
76   return;
77 }
78
79 /**
80  * \brief Creates and runs a new #smx_process_t.
81  *
82  * 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.
83  *
84  * \param name a name for the object. It is for user-level information and can be NULL.
85  * \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.
86  * \param host the location where the new agent is executed.
87  * \param argc first argument passed to \a code
88  * \param argv second argument passed to \a code
89  * \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.
90  * \see smx_process_t
91  * \return The new corresponding object.
92  */
93 smx_process_t SIMIX_process_create(const char *name,
94                                    xbt_main_func_t code, void *data,
95                                    const char *hostname, int argc,
96                                    char **argv, xbt_dict_t properties)
97 {
98   smx_process_t process = NULL;
99   smx_host_t host = SIMIX_host_get_by_name(hostname);
100
101   DEBUG2("Start process %s on host %s", name, hostname);
102
103   if (!SIMIX_host_get_state(host)) {
104     WARN2("Cannot launch process '%s' on failed host '%s'", name, hostname);
105     return NULL;
106   }
107   process = xbt_new0(s_smx_process_t, 1);
108
109   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
110
111   /* Process data */
112   process->name = xbt_strdup(name);
113   process->smx_host = host;
114   process->argc = argc;
115   process->argv = argv;
116   process->mutex = NULL;
117   process->cond = NULL;
118   process->iwannadie = 0;
119
120   VERB1("Create context %s", process->name);
121   process->context = SIMIX_context_new(code);
122
123   process->data = data;
124   process->cleanup_func = simix_global->cleanup_process_function;
125   process->cleanup_arg = process;
126
127   /* Add properties */
128   process->properties = properties;
129
130   /* Add the process to it's host process list */
131   xbt_swag_insert(process, host->process_list);
132
133   DEBUG1("Start context '%s'", process->name);
134   SIMIX_context_start(process->context);
135    
136   /* Now insert it in the global process list and in the process to run list */
137   xbt_swag_insert(process, simix_global->process_list);
138   DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
139   xbt_swag_insert(process, simix_global->process_to_run);
140
141   return process;
142 }
143
144 /**
145  * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
146  *
147  * Warning: this should only be used in libsimgrid4java, since it create
148  * a context with no code, which leads to segfaults in plain libsimgrid
149  */
150 void SIMIX_jprocess_create(const char *name, smx_host_t host,
151                            void *data,
152                            void *jprocess, void *jenv, smx_process_t * res)
153 {
154   smx_process_t process = xbt_new0(s_smx_process_t, 1);
155   smx_process_t self = NULL;
156
157   /* HACK: We need this trick because when we xbt_context_new() do
158      syncronization stuff, the s_process field in the m_process needs
159      to have a valid value, and we call xbt_context_new() before
160      returning, of course, ie, before providing a right value to the
161      caller (Java_simgrid_msg_Msg_processCreate) have time to store it
162      in place. This way, we initialize the m_process->simdata->s_process
163      field ourself ASAP.
164
165      All this would be much simpler if the synchronization stuff would be done
166      in the JAVA world, I think.
167    */
168   *res = process;
169
170   DEBUG5("jprocess_create(name=%s,host=%p,data=%p,jproc=%p,jenv=%p)",
171          name, host, data, jprocess, jenv);
172   xbt_assert0(host, "Invalid parameters");
173
174   /* Process data */
175   process->name = xbt_strdup(name);
176   process->smx_host = host;
177   process->argc = 0;
178   process->argv = NULL;
179   process->mutex = NULL;
180   process->cond = NULL;
181   SIMIX_context_new(jprocess);
182   process->data = data;
183
184   /* Add the process to it's host process list */
185   xbt_swag_insert(&process, host->process_list);
186
187   /* fix current_process, about which xbt_context_start mocks around */
188   self = simix_global->current_process;
189   SIMIX_context_start(process->context);
190   simix_global->current_process = self;
191
192   /* Now insert it in the global process list and in the process to run list */
193   xbt_swag_insert(process, simix_global->process_list);
194   DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
195   xbt_swag_insert(process, simix_global->process_to_run);
196 }
197
198 /** \brief Kill a SIMIX process
199  *
200  * This function simply kills a \a process... scarry isn't it ? :).
201  * \param process poor victim
202  *
203  */
204 void SIMIX_process_kill(smx_process_t process)
205 {
206   DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
207
208   /* Cleanup if we were waiting for something */
209   if (process->mutex)
210     xbt_swag_remove(process, process->mutex->sleeping);
211
212   if (process->cond)
213     xbt_swag_remove(process, process->cond->sleeping);
214
215   DEBUG2("%p here! killing %p", simix_global->current_process, process);
216
217   process->iwannadie = 1;
218
219   /* If I'm killing myself then stop otherwise schedule the process to kill */
220   if (process == SIMIX_process_self())
221     SIMIX_context_stop(1);
222   else
223     __SIMIX_process_schedule(process);
224   
225 }
226
227 /**
228  * \brief Return the user data of a #smx_process_t.
229  *
230  * 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.
231  * \param process SIMIX process
232  * \return A void pointer to the user data
233  */
234 void *SIMIX_process_get_data(smx_process_t process)
235 {
236   xbt_assert0((process != NULL), "Invalid parameters");
237   return (process->data);
238 }
239
240 /**
241  * \brief Set the user data of a #m_process_t.
242  *
243  * 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.
244  * \param process SIMIX process
245  * \param data User data
246  */
247 void SIMIX_process_set_data(smx_process_t process, void *data)
248 {
249   xbt_assert0((process != NULL), "Invalid parameters");
250
251   process->data = data;
252   return;
253 }
254
255 /**
256  * \brief Return the location on which an agent is running.
257  *
258  * 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.
259  * \param process SIMIX process
260  * \return SIMIX host
261  */
262 smx_host_t SIMIX_process_get_host(smx_process_t process)
263 {
264   xbt_assert0((process != NULL), "Invalid parameters");
265   return (process->smx_host);
266 }
267
268 /**
269  * \brief Return the name of an agent.
270  *
271  * This functions checks whether \a process is a valid pointer or not and return its name.
272  * \param process SIMIX process
273  * \return The process name
274  */
275 const char *SIMIX_process_get_name(smx_process_t process)
276 {
277   xbt_assert0((process != NULL), "Invalid parameters");
278   return (process->name);
279 }
280
281 /**
282  * \brief Changes the name of an agent.
283  *
284  * This functions checks whether \a process is a valid pointer or not and return its name.
285  * \param process SIMIX process
286  * \param name The new process name
287  */
288 void SIMIX_process_set_name(smx_process_t process, char *name)
289 {
290   xbt_assert0((process != NULL), "Invalid parameters");
291   process->name = name;
292 }
293
294 /** \ingroup m_process_management
295  * \brief Return the properties
296  *
297  * This functions returns the properties associated with this process
298  */
299 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
300 {
301   return process->properties;
302 }
303
304 /**
305  * \brief Return the current agent.
306  *
307  * This functions returns the currently running #smx_process_t.
308  * \return The SIMIX process
309  */
310 smx_process_t SIMIX_process_self(void)
311 {
312   return simix_global ? simix_global->current_process : NULL;
313 }
314
315 /**
316  * \brief Suspend the process.
317  *
318  * This functions suspend the process by suspending the action on
319  * which it was waiting for the completion.
320  *
321  * \param process SIMIX process
322  */
323 void SIMIX_process_suspend(smx_process_t process)
324 {
325   xbt_assert0(process, "Invalid parameters");
326
327   if (process != SIMIX_process_self()) {
328
329     if (process->mutex) {
330       /* process blocked on a mutex, only set suspend=1 */
331       process->suspended = 1;
332     } else if (process->cond) {
333       /* process blocked cond, suspend all actions */
334
335       /* temporaries variables */
336       smx_cond_t c;
337       xbt_fifo_item_t i;
338       smx_action_t act;
339
340       process->suspended = 1;
341       c = process->cond;
342       xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
343          surf_workstation_model->suspend(act->surf_action);
344       }
345     } else {
346       process->suspended = 1;
347     }
348   } else {
349     /* process executing, I can create an action and suspend it */
350     smx_action_t dummy;
351     smx_cond_t cond;
352     char name[] = "dummy";
353     process->suspended = 1;
354
355     cond = SIMIX_cond_init();
356     dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
357     surf_workstation_model->suspend(dummy->surf_action);
358     SIMIX_register_action_to_condition(dummy, cond);
359     __SIMIX_cond_wait(cond);
360     SIMIX_unregister_action_to_condition(dummy, cond);
361     SIMIX_action_destroy(dummy);
362     SIMIX_cond_destroy(cond);
363   }
364   return;
365 }
366
367 /**
368  * \brief Resume a suspended process.
369  *
370  * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
371  * \param process SIMIX process
372  */
373 void SIMIX_process_resume(smx_process_t process)
374 {
375   xbt_assert0((process != NULL), "Invalid parameters");
376   SIMIX_CHECK_HOST();
377
378   if (process == SIMIX_process_self())
379     return;
380
381   if (process->mutex) {
382     DEBUG0("Resume process blocked on a mutex");
383     process->suspended = 0;     /* It'll wake up by itself when mutex releases */
384     return;
385   } else if (process->cond) {
386     /* temporaries variables */
387     smx_cond_t c;
388     xbt_fifo_item_t i;
389     smx_action_t act;
390     DEBUG0("Resume process blocked on a conditional");
391     process->suspended = 0;
392     c = process->cond;
393     xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
394       surf_workstation_model->resume(act->surf_action);
395     }
396     SIMIX_cond_signal(c);
397     return;
398   } else {
399     process->suspended = 0;
400     xbt_swag_insert(process, simix_global->process_to_run);
401   }
402 }
403
404 /**
405  * \brief Migrates an agent to another location.
406  *
407  * This function changes the value of the host on which \a process is running.
408  */
409 void SIMIX_process_change_host(smx_process_t process, char *source, char *dest)
410 {
411   xbt_assert0((process != NULL), "Invalid parameters");
412   smx_host_t h1 = SIMIX_host_get_by_name(source);
413   smx_host_t h2 = SIMIX_host_get_by_name(dest);
414   process->smx_host = h2;
415   xbt_swag_remove(process, h1->process_list);
416   xbt_swag_insert(process, h2->process_list);
417 }
418
419 /**
420  * \brief Returns true if the process is suspended .
421  *
422  * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
423  * \param process SIMIX process
424  * \return 1, if the process is suspended, else 0.
425  */
426 int SIMIX_process_is_suspended(smx_process_t process)
427 {
428   xbt_assert0((process != NULL), "Invalid parameters");
429
430   return (process->suspended);
431 }
432
433 /**
434  * \brief Returns the amount of SIMIX processes in the system
435  *
436  * Maestro internal process is not counted, only user code processes are
437  */
438 int SIMIX_process_count()
439 {
440   return xbt_swag_size(simix_global->process_list);
441 }
442
443 /** 
444  * Calling this function makes the process process to yield. The process
445  * that scheduled it returns from __SIMIX_process_schedule as if nothing
446  * had happened.
447  * 
448  * Only the processes can call this function, giving back the control
449  * to the maestro
450  */
451 void __SIMIX_process_yield(void)
452 {
453   DEBUG1("Yield process '%s'", simix_global->current_process->name);
454   xbt_assert0((simix_global->current_process != simix_global->maestro_process),
455               "You are not supposed to run this function here!");
456
457   SIMIX_context_suspend(simix_global->current_process->context);
458
459   if (simix_global->current_process->iwannadie)
460     SIMIX_context_stop(1);
461 }
462
463 void __SIMIX_process_schedule(smx_process_t new_process)
464 {
465   DEBUG1("Scheduling context: '%s'", new_process->name);
466
467   /* save the current process */
468   smx_process_t old_process = simix_global->current_process;
469
470   /* update the current process */
471   simix_global->current_process = new_process;
472
473   /* schedule the context */
474   SIMIX_context_resume(old_process->context, new_process->context);
475
476   /* restore the current process to the previously saved process */
477   simix_global->current_process = old_process;
478 }