Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fighting more memory leaks in Chord
[simgrid.git] / src / simix / smx_process.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "msg/mailbox.h"
12 #include "mc/mc.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
15                                 "Logging specific to SIMIX (process)");
16
17 unsigned long simix_process_maxpid = 0;
18
19 /**
20  * \brief Returns the current agent.
21  *
22  * This functions returns the currently running SIMIX process.
23  *
24  * \return The SIMIX process
25  */
26 XBT_INLINE smx_process_t SIMIX_process_self(void)
27 {
28   smx_context_t self_context = SIMIX_context_self();
29
30   return self_context ? SIMIX_context_get_data(self_context) : NULL;
31 }
32
33 /**
34  * \brief Move a process to the list of processes to destroy.
35  */
36 void SIMIX_process_cleanup(smx_process_t process)
37 {
38   XBT_DEBUG("Cleanup process %s", process->name);
39   /*xbt_swag_remove(process, simix_global->process_to_run);*/
40   xbt_swag_remove(process, simix_global->process_list);
41   xbt_swag_remove(process, process->smx_host->process_list);
42   xbt_swag_insert(process, simix_global->process_to_destroy);
43 }
44
45 /** 
46  * Garbage collection
47  *
48  * Should be called some time to time to free the memory allocated for processes
49  * that have finished (or killed).
50  */
51 void SIMIX_process_empty_trash(void)
52 {
53   smx_process_t process = NULL;
54
55   while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
56     SIMIX_context_free(process->context);
57
58     /* Free the exception allocated at creation time */
59     if (process->running_ctx)
60       free(process->running_ctx);
61     if (process->properties)
62       xbt_dict_free(&process->properties);
63
64     free(process->name);
65     process->name = NULL;
66     free(process);
67   }
68 }
69
70 /**
71  * \brief Creates and runs the maestro process
72  */
73 void SIMIX_create_maestro_process()
74 {
75   smx_process_t maestro = NULL;
76
77   /* Create maestro process and intilialize it */
78   maestro = xbt_new0(s_smx_process_t, 1);
79   maestro->pid = simix_process_maxpid++;
80   maestro->name = (char *) "";
81   maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
82   XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
83   maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
84   maestro->request.issuer = maestro;
85
86   simix_global->maestro_process = maestro;
87   return;
88 }
89
90 /**
91  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
92  * \return the process created
93  */
94 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
95
96   smx_process_t process;
97   simix_global->create_process_function(
98       &process,
99       args->name,
100       args->code,
101       args->data,
102       args->hostname,
103       args->argc,
104       args->argv,
105       args->properties);
106
107   return process;
108 }
109
110 /**
111  * \brief Internal function to create a process.
112  *
113  * This function actually creates the process.
114  * It may be called when a REQ_PROCESS_CREATE request occurs,
115  * or directly for SIMIX internal purposes.
116  *
117  * \return the process created
118  */
119 void SIMIX_process_create(smx_process_t *process,
120                           const char *name,
121                           xbt_main_func_t code,
122                           void *data,
123                           const char *hostname,
124                           int argc, char **argv,
125                           xbt_dict_t properties) {
126
127   *process = NULL;
128   smx_host_t host = SIMIX_host_get_by_name(hostname);
129
130   XBT_DEBUG("Start process %s on host %s", name, hostname);
131
132   if (!SIMIX_host_get_state(host)) {
133     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
134           hostname);
135   }
136   else {
137     *process = xbt_new0(s_smx_process_t, 1);
138
139     xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
140
141     /* Process data */
142     (*process)->pid = simix_process_maxpid++;
143     (*process)->name = xbt_strdup(name);
144     (*process)->smx_host = host;
145     (*process)->data = data;
146
147     XBT_VERB("Create context %s", (*process)->name);
148     (*process)->context = SIMIX_context_new(code, argc, argv,
149         simix_global->cleanup_process_function, *process);
150
151     (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
152     XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
153
154     /* Add properties */
155     (*process)->properties = properties;
156
157     /* Add the process to it's host process list */
158     xbt_swag_insert(*process, host->process_list);
159
160     XBT_DEBUG("Start context '%s'", (*process)->name);
161
162     /* Now insert it in the global process list and in the process to run list */
163     xbt_swag_insert(*process, simix_global->process_list);
164     XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
165     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
166   }
167 }
168
169 /**
170  * \brief Executes the processes from simix_global->process_to_run.
171  *
172  * The processes of simix_global->process_to_run are run (in parallel if
173  * possible).  On exit, simix_global->process_to_run is empty, and
174  * simix_global->process_that_ran contains the list of processes that just ran.
175  * The two lists are swapped so, be careful when using them before and after a
176  * call to this function.
177  */
178 void SIMIX_process_runall(void)
179 {
180   SIMIX_context_runall(simix_global->process_to_run);
181   xbt_dynar_t tmp = simix_global->process_that_ran;
182   simix_global->process_that_ran = simix_global->process_to_run;
183   simix_global->process_to_run = tmp;
184   xbt_dynar_reset(simix_global->process_to_run);
185 }
186
187 /**
188  * \brief Internal function to kill a SIMIX process.
189  *
190  * This function may be called when a REQ_PROCESS_KILL request occurs,
191  * or directly for SIMIX internal purposes.
192  *
193  * \param process poor victim
194  */
195 void SIMIX_process_kill(smx_process_t process) {
196
197   XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
198
199   process->context->iwannadie = 1;
200   process->blocked = 0;
201   process->suspended = 0;
202   /* FIXME: set doexception to 0 also? */
203   /* FIXME: asynchronous communication actions are not destroyed:
204    * another process may find a matching rdv with me after I'm freed */
205
206   if (process->waiting_action) {
207
208     switch (process->waiting_action->type) {
209
210       case SIMIX_ACTION_EXECUTE:          
211       case SIMIX_ACTION_PARALLEL_EXECUTE:
212         SIMIX_host_execution_destroy(process->waiting_action);
213         break;
214
215       case SIMIX_ACTION_COMMUNICATE:
216         SIMIX_comm_destroy(process->waiting_action);
217         break;
218
219       case SIMIX_ACTION_SLEEP:
220         SIMIX_process_sleep_destroy(process->waiting_action);
221         break;
222
223       case SIMIX_ACTION_SYNCHRO:
224         SIMIX_synchro_stop_waiting(process, &process->request);
225         SIMIX_synchro_destroy(process->waiting_action);
226         break;
227
228       case SIMIX_ACTION_IO:
229         THROW_UNIMPLEMENTED;
230         break;
231     }
232   }
233
234   xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
235 }
236
237 /**
238  * \brief Kills all running processes.
239  * \param issuer this one will not be killed
240  */
241 void SIMIX_process_killall(smx_process_t issuer)
242 {
243   smx_process_t p = NULL;
244
245   while ((p = xbt_swag_extract(simix_global->process_list))) {
246     if (p != issuer) {
247       SIMIX_process_kill(p);
248     }
249   }
250
251   SIMIX_context_runall(simix_global->process_to_run);
252
253   SIMIX_process_empty_trash();
254 }
255
256 void SIMIX_process_change_host(smx_process_t process,
257                                smx_host_t dest)
258 {
259   xbt_assert((process != NULL), "Invalid parameters");
260   xbt_swag_remove(process, process->smx_host->process_list);
261   process->smx_host = dest;
262   xbt_swag_insert(process, dest->process_list);
263 }
264
265 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
266 {
267   process->new_host = dest;
268 }
269
270 void SIMIX_pre_process_suspend(smx_req_t req)
271 {
272   smx_process_t process = req->process_suspend.process;
273   SIMIX_process_suspend(process, req->issuer);
274
275   if (process != req->issuer) {
276     SIMIX_request_answer(req);
277   }
278   /* If we are suspending ourselves, then just do not replay the request. */
279 }
280
281 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
282 {
283   process->suspended = 1;
284
285   /* If we are suspending another process, and it is waiting on an action,
286      suspend it's action. */
287   if (process != issuer) {
288
289     if (process->waiting_action) {
290
291       switch (process->waiting_action->type) {
292
293         case SIMIX_ACTION_EXECUTE:
294         case SIMIX_ACTION_PARALLEL_EXECUTE:
295           SIMIX_host_execution_suspend(process->waiting_action);
296           break;
297
298         case SIMIX_ACTION_COMMUNICATE:
299           SIMIX_comm_suspend(process->waiting_action);
300           break;
301
302         case SIMIX_ACTION_SLEEP:
303           SIMIX_process_sleep_suspend(process->waiting_action);
304           break;
305
306         default:
307           THROW_IMPOSSIBLE;
308       }
309     }
310   }
311 }
312
313 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
314 {
315   xbt_assert((process != NULL), "Invalid parameters");
316
317   process->suspended = 0;
318
319   /* If we are resuming another process, resume the action it was waiting for
320      if any. Otherwise add it to the list of process to run in the next round. */
321   if (process != issuer) {
322
323     if (process->waiting_action) {
324
325       switch (process->waiting_action->type) {
326
327         case SIMIX_ACTION_EXECUTE:          
328         case SIMIX_ACTION_PARALLEL_EXECUTE:
329           SIMIX_host_execution_resume(process->waiting_action);
330           break;
331
332         case SIMIX_ACTION_COMMUNICATE:
333           SIMIX_comm_resume(process->waiting_action);
334           break;
335
336         case SIMIX_ACTION_SLEEP:
337           SIMIX_process_sleep_resume(process->waiting_action);
338           break;
339
340         default:
341           THROW_IMPOSSIBLE;
342       }
343     }
344     else {
345       xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
346     }
347   }
348 }
349
350 int SIMIX_process_get_maxpid(void) {
351   return simix_process_maxpid;
352 }
353
354 int SIMIX_process_count(void)
355 {
356   return xbt_swag_size(simix_global->process_list);
357 }
358
359 void* SIMIX_process_self_get_data(void)
360 {
361   smx_process_t me = SIMIX_process_self();
362   if (!me) {
363     return NULL;
364   }
365   return SIMIX_process_get_data(me);
366 }
367
368 void SIMIX_process_self_set_data(void *data)
369 {
370   SIMIX_process_set_data(SIMIX_process_self(), data);
371 }
372
373 void* SIMIX_process_get_data(smx_process_t process)
374 {
375   return process->data;
376 }
377
378 void SIMIX_process_set_data(smx_process_t process, void *data)
379 {
380   process->data = data;
381 }
382
383 smx_host_t SIMIX_process_get_host(smx_process_t process)
384 {
385   return process->smx_host;
386 }
387
388 /* needs to be public and without request because it is called
389    by exceptions and logging events */
390 const char* SIMIX_process_self_get_name(void) {
391
392   smx_process_t process = SIMIX_process_self();
393   if (process == NULL || process == simix_global->maestro_process)
394     return "";
395
396   return SIMIX_process_get_name(process);
397 }
398
399 const char* SIMIX_process_get_name(smx_process_t process)
400 {
401   return process->name;
402 }
403
404 smx_process_t SIMIX_process_get_by_name(const char* name)
405 {
406   smx_process_t proc;
407
408   xbt_swag_foreach(proc, simix_global->process_list)
409   {
410     if(!strcmp(name, proc->name))
411       return proc;
412   }
413   return NULL;
414 }
415
416 int SIMIX_process_is_suspended(smx_process_t process)
417 {
418   return process->suspended;
419 }
420
421 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
422 {
423   return process->properties;
424 }
425
426 void SIMIX_pre_process_sleep(smx_req_t req)
427 {
428   if (MC_IS_ENABLED) {
429     MC_process_clock_add(req->issuer, req->process_sleep.duration);
430     req->process_sleep.result = SIMIX_DONE;
431     SIMIX_request_answer(req);
432     return;
433   }
434   smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
435   xbt_fifo_push(action->request_list, req);
436   req->issuer->waiting_action = action;
437 }
438
439 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
440 {
441   smx_action_t action;
442   smx_host_t host = process->smx_host;
443
444   /* check if the host is active */
445   if (surf_workstation_model->extension.
446       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
447     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
448            host->name);
449   }
450
451   action = xbt_mallocator_get(simix_global->action_mallocator);
452   action->type = SIMIX_ACTION_SLEEP;
453   action->name = NULL;
454 #ifdef HAVE_TRACING
455   action->category = NULL;
456 #endif
457
458   action->sleep.host = host;
459   action->sleep.surf_sleep =
460       surf_workstation_model->extension.workstation.sleep(host->host, duration);
461
462   surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
463   XBT_DEBUG("Create sleep action %p", action);
464
465   return action;
466 }
467
468 void SIMIX_post_process_sleep(smx_action_t action)
469 {
470   smx_req_t req;
471   e_smx_state_t state;
472
473   while ((req = xbt_fifo_shift(action->request_list))) {
474
475     switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
476       case SURF_ACTION_FAILED:
477         state = SIMIX_SRC_HOST_FAILURE;
478         break;
479
480       case SURF_ACTION_DONE:
481         state = SIMIX_DONE;
482         break;
483
484       default:
485         THROW_IMPOSSIBLE;
486         break;
487     }
488     req->process_sleep.result = state;
489     req->issuer->waiting_action = NULL;
490     SIMIX_request_answer(req);
491   }
492   SIMIX_process_sleep_destroy(action);
493 }
494
495 void SIMIX_process_sleep_destroy(smx_action_t action)
496 {
497   XBT_DEBUG("Destroy action %p", action);
498   if (action->sleep.surf_sleep)
499     action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
500   xbt_mallocator_release(simix_global->action_mallocator, action);
501 }
502
503 void SIMIX_process_sleep_suspend(smx_action_t action)
504 {
505   surf_workstation_model->suspend(action->sleep.surf_sleep);
506 }
507
508 void SIMIX_process_sleep_resume(smx_action_t action)
509 {
510   surf_workstation_model->resume(action->sleep.surf_sleep);
511 }
512
513 /** 
514  * Calling this function makes the process to yield.
515  * Only the processes can call this function, giving back the control to maestro
516  */
517 void SIMIX_process_yield(void)
518 {
519   smx_process_t self = SIMIX_process_self();
520
521   XBT_DEBUG("Yield process '%s'", self->name);
522
523   /* Go into sleep and return control to maestro */
524   SIMIX_context_suspend(self->context);
525
526   /* Ok, maestro returned control to us */
527   XBT_DEBUG("Maestro returned control to me: '%s'", self->name);
528
529   if (self->context->iwannadie){
530     XBT_DEBUG("I wanna die!");
531     SIMIX_context_stop(self->context);
532   }
533
534   if (self->doexception) {
535     XBT_DEBUG("Wait, maestro left me an exception");
536     self->doexception = 0;
537     RETHROW;
538   }
539   
540   if (self->new_host) {
541     SIMIX_process_change_host(self, self->new_host);
542     self->new_host = NULL;
543   }
544 }
545
546 /* callback: context fetching */
547 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
548 {
549   return SIMIX_process_self()->running_ctx;
550 }
551
552 /* callback: termination */
553 void SIMIX_process_exception_terminate(xbt_ex_t * e)
554 {
555   xbt_ex_display(e);
556   abort();
557 }
558
559 smx_context_t SIMIX_process_get_context(smx_process_t p) {
560   return p->context;
561 }
562
563 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
564   p->context = c;
565 }