Logo AND Algorithmique Numérique Distribuée

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