Logo AND Algorithmique Numérique Distribuée

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