Logo AND Algorithmique Numérique Distribuée

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