Logo AND Algorithmique Numérique Distribuée

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