Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
this example needs to forcefully destroy the VMs
[simgrid.git] / src / simix / smx_process.c
1 /* Copyright (c) 2007-2014. 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 #include "mc/mc_client.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_process(self_context) : NULL;
31 }
32
33 /**
34  * \brief Returns whether a process has pending asynchronous communications.
35  * \return true if there are asynchronous communications in this process
36  */
37 int SIMIX_process_has_pending_comms(smx_process_t process) {
38
39   return xbt_fifo_size(process->comms) > 0;
40 }
41
42 /**
43  * \brief Moves a process to the list of processes to destroy.
44  */
45 void SIMIX_process_cleanup(smx_process_t process)
46 {
47   XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
48       process->name, process, process->waiting_synchro);
49
50   SIMIX_process_on_exit_runall(process);
51
52   /* Unregister from the kill timer if any */
53   if (process->kill_timer != NULL)
54           SIMIX_timer_remove(process->kill_timer);
55
56   /* cancel non-blocking communications */
57   smx_synchro_t synchro;
58   while ((synchro = xbt_fifo_pop(process->comms))) {
59
60     /* make sure no one will finish the comm after this process is destroyed,
61      * because src_proc or dst_proc would be an invalid pointer */
62     SIMIX_comm_cancel(synchro);
63
64     if (synchro->comm.src_proc == process) {
65       XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
66           synchro, synchro->comm.detached, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
67       synchro->comm.src_proc = NULL;
68
69       /* I'm not supposed to destroy a detached comm from the sender side, */
70       if (!synchro->comm.detached)
71         SIMIX_comm_destroy(synchro);
72       else
73         XBT_DEBUG("Don't destroy it since it's a detached comm");
74
75     }
76     else if (synchro->comm.dst_proc == process){
77       XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
78           synchro, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
79       synchro->comm.dst_proc = NULL;
80
81       if (synchro->comm.detached && synchro->comm.refcount == 1
82           && synchro->comm.src_proc != NULL) {
83         /* the comm will be freed right now, remove it from the sender */
84         xbt_fifo_remove(synchro->comm.src_proc->comms, synchro);
85       }
86       SIMIX_comm_destroy(synchro);
87     }
88     else {
89       xbt_die("Communication synchro %p is in my list but I'm not the sender "
90           "or the receiver", synchro);
91     }
92   }
93
94   XBT_DEBUG("%p should not be run anymore",process);
95   xbt_swag_remove(process, simix_global->process_list);
96   xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
97   xbt_swag_insert(process, simix_global->process_to_destroy);
98   process->context->iwannadie = 0;
99 }
100
101 /**
102  * Garbage collection
103  *
104  * Should be called some time to time to free the memory allocated for processes
105  * that have finished (or killed).
106  */
107 void SIMIX_process_empty_trash(void)
108 {
109   smx_process_t process = NULL;
110
111   while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
112     XBT_DEBUG("Getting rid of %p",process);
113
114     SIMIX_context_free(process->context);
115
116     /* Free the exception allocated at creation time */
117     free(process->running_ctx);
118     xbt_dict_free(&process->properties);
119
120     xbt_fifo_free(process->comms);
121
122     xbt_dynar_free(&process->on_exit);
123
124     xbt_free(process->name);
125     xbt_free(process);
126   }
127 }
128
129 /**
130  * \brief Creates and runs the maestro process
131  */
132 void SIMIX_create_maestro_process()
133 {
134   smx_process_t maestro = NULL;
135
136   /* Create maestro process and intilialize it */
137   maestro = xbt_new0(s_smx_process_t, 1);
138   maestro->pid = simix_process_maxpid++;
139   maestro->ppid = -1;
140   maestro->name = (char *) "";
141   maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
142   XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
143   maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
144   maestro->simcall.issuer = maestro;
145   simix_global->maestro_process = maestro;
146   return;
147 }
148 /**
149  * \brief Stops a process.
150  *
151  * Stops the process, execute all the registered on_exit functions,
152  * register it to the list of the process to restart if needed
153  * and stops its context.
154  */
155 void SIMIX_process_stop(smx_process_t arg) {
156   /* execute the on_exit functions */
157   SIMIX_process_on_exit_runall(arg);
158   /* Add the process to the list of process to restart, only if
159    * the host is down
160    */
161   if (arg->auto_restart && !SIMIX_host_get_state(arg->smx_host)) {
162     SIMIX_host_add_auto_restart_process(arg->smx_host,arg->name,arg->code, arg->data,
163                                         sg_host_name(arg->smx_host),
164                                         SIMIX_timer_get_date(arg->kill_timer),
165                                         arg->argc,arg->argv,arg->properties,
166                                         arg->auto_restart);
167   }
168   XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_name(arg->smx_host));
169   /* stop the context */
170   SIMIX_context_stop(arg->context);
171 }
172
173 /**
174  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
175  * This function frees the argument.
176  * \return the process created
177  */
178 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
179
180   smx_process_t process;
181   simix_global->create_process_function(&process,
182                                         args->name,
183                                         args->code,
184                                         args->data,
185                                         args->hostname,
186                                         args->kill_time,
187                                         args->argc,
188                                         args->argv,
189                                         args->properties,
190                                         args->auto_restart,
191                                         NULL);
192   xbt_free(args);
193   return process;
194 }
195
196
197 void simcall_HANDLER_process_create(smx_simcall_t simcall,
198                           smx_process_t *process,
199                           const char *name,
200                           xbt_main_func_t code,
201                           void *data,
202                           const char *hostname,
203                           double kill_time,
204                           int argc, char **argv,
205                           xbt_dict_t properties,
206                           int auto_restart){
207   SIMIX_process_create(process, name, code, data, hostname,
208                        kill_time, argc, argv, properties, auto_restart,
209                        simcall->issuer);
210 }
211 /**
212  * \brief Internal function to create a process.
213  *
214  * This function actually creates the process.
215  * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
216  * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
217  *
218  * \return the process created
219  */
220 void SIMIX_process_create(smx_process_t *process,
221                           const char *name,
222                           xbt_main_func_t code,
223                           void *data,
224                           const char *hostname,
225                           double kill_time,
226                           int argc, char **argv,
227                           xbt_dict_t properties,
228                           int auto_restart,
229                           smx_process_t parent_process)
230 {
231   *process = NULL;
232   smx_host_t host = SIMIX_host_get_by_name(hostname);
233
234   XBT_DEBUG("Start process %s on host '%s'", name, hostname);
235
236   if (!SIMIX_host_get_state(host)) {
237     int i;
238     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
239           hostname);
240     for (i = 0; i < argc; i++)
241       xbt_free(argv[i]);
242     xbt_free(argv);
243   }
244   else {
245     *process = xbt_new0(s_smx_process_t, 1);
246
247     xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
248     /* Process data */
249     (*process)->pid = simix_process_maxpid++;
250     (*process)->name = xbt_strdup(name);
251     (*process)->smx_host = host;
252     (*process)->data = data;
253     (*process)->comms = xbt_fifo_new();
254     (*process)->simcall.issuer = *process;
255
256      if (parent_process) {
257        (*process)->ppid = SIMIX_process_get_PID(parent_process);
258      } else {
259        (*process)->ppid = -1;
260      }
261
262     /* Process data for auto-restart */
263     (*process)->auto_restart = auto_restart;
264     (*process)->code = code;
265     (*process)->argc = argc;
266     (*process)->argv = argv;
267
268
269     XBT_VERB("Create context %s", (*process)->name);
270     (*process)->context = SIMIX_context_new(code, argc, argv,
271       simix_global->cleanup_process_function, *process);
272
273     (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
274     XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
275
276     if(MC_is_active()){
277       MC_ignore_heap((*process)->running_ctx, sizeof(*(*process)->running_ctx));
278     }
279
280     /* Add properties */
281     (*process)->properties = properties;
282
283     /* Add the process to it's host process list */
284     xbt_swag_insert(*process, SIMIX_host_priv(host)->process_list);
285
286     XBT_DEBUG("Start context '%s'", (*process)->name);
287
288     /* Now insert it in the global process list and in the process to run list */
289     xbt_swag_insert(*process, simix_global->process_list);
290     XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, sg_host_name(host));
291     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
292
293     if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
294       XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
295           sg_host_name((*process)->smx_host), kill_time);
296       (*process)->kill_timer = SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
297     }
298   }
299 }
300
301 /**
302  * \brief Executes the processes from simix_global->process_to_run.
303  *
304  * The processes of simix_global->process_to_run are run (in parallel if
305  * possible).  On exit, simix_global->process_to_run is empty, and
306  * simix_global->process_that_ran contains the list of processes that just ran.
307  * The two lists are swapped so, be careful when using them before and after a
308  * call to this function.
309  */
310 void SIMIX_process_runall(void)
311 {
312   SIMIX_context_runall();
313
314   xbt_dynar_t tmp = simix_global->process_that_ran;
315   simix_global->process_that_ran = simix_global->process_to_run;
316   simix_global->process_to_run = tmp;
317   xbt_dynar_reset(simix_global->process_to_run);
318 }
319
320 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
321   SIMIX_process_kill(process, simcall->issuer);
322 }
323 /**
324  * \brief Internal function to kill a SIMIX process.
325  *
326  * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
327  * or directly for SIMIX internal purposes.
328  *
329  * \param process poor victim
330  * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
331  */
332 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
333
334   XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->smx_host));
335
336   process->context->iwannadie = 1;
337   process->blocked = 0;
338   process->suspended = 0;
339   process->doexception = 0;
340
341   /* destroy the blocking synchro if any */
342   if (process->waiting_synchro) {
343
344     switch (process->waiting_synchro->type) {
345
346     case SIMIX_SYNC_EXECUTE:
347     case SIMIX_SYNC_PARALLEL_EXECUTE:
348       SIMIX_host_execution_destroy(process->waiting_synchro);
349       break;
350
351     case SIMIX_SYNC_COMMUNICATE:
352       xbt_fifo_remove(process->comms, process->waiting_synchro);
353       SIMIX_comm_cancel(process->waiting_synchro);
354       SIMIX_comm_destroy(process->waiting_synchro);
355       break;
356
357     case SIMIX_SYNC_SLEEP:
358       SIMIX_process_sleep_destroy(process->waiting_synchro);
359       break;
360
361     case SIMIX_SYNC_JOIN:
362       SIMIX_process_sleep_destroy(process->waiting_synchro);
363       break;
364
365     case SIMIX_SYNC_SYNCHRO:
366       SIMIX_synchro_stop_waiting(process, &process->simcall);
367       SIMIX_synchro_destroy(process->waiting_synchro);
368       break;
369
370     case SIMIX_SYNC_IO:
371       SIMIX_io_destroy(process->waiting_synchro);
372       break;
373
374     }
375   }
376   if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
377     XBT_DEBUG("Inserting %s in the to_run list", process->name);
378     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
379   }
380
381 }
382
383 /** @brief Ask another process to raise the given exception
384  *
385  * @param cat category of exception
386  * @param value value associated to the exception
387  * @param msg string information associated to the exception
388  */
389 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
390   SMX_EXCEPTION(process, cat, value, msg);
391
392   if (process->suspended)
393     SIMIX_process_resume(process,SIMIX_process_self());
394
395   /* cancel the blocking synchro if any */
396   if (process->waiting_synchro) {
397
398     switch (process->waiting_synchro->type) {
399
400     case SIMIX_SYNC_EXECUTE:
401     case SIMIX_SYNC_PARALLEL_EXECUTE:
402       SIMIX_host_execution_cancel(process->waiting_synchro);
403       break;
404
405     case SIMIX_SYNC_COMMUNICATE:
406       xbt_fifo_remove(process->comms, process->waiting_synchro);
407       SIMIX_comm_cancel(process->waiting_synchro);
408       break;
409
410     case SIMIX_SYNC_SLEEP:
411     case SIMIX_SYNC_JOIN:
412       SIMIX_process_sleep_destroy(process->waiting_synchro);
413       if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
414         XBT_DEBUG("Inserting %s in the to_run list", process->name);
415         xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
416       }
417       break;
418
419     case SIMIX_SYNC_SYNCHRO:
420       SIMIX_synchro_stop_waiting(process, &process->simcall);
421       break;
422
423     case SIMIX_SYNC_IO:
424       SIMIX_io_destroy(process->waiting_synchro);
425       break;
426
427     }
428   }
429   process->waiting_synchro = NULL;
430
431 }
432
433 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
434   SIMIX_process_killall(simcall->issuer, reset_pid);
435 }
436 /**
437  * \brief Kills all running processes.
438  * \param issuer this one will not be killed
439  */
440 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
441 {
442   smx_process_t p = NULL;
443
444   while ((p = xbt_swag_extract(simix_global->process_list))) {
445     if (p != issuer) {
446       SIMIX_process_kill(p,issuer);
447     }
448   }
449
450   if (reset_pid > 0)
451     simix_process_maxpid = reset_pid;
452
453   SIMIX_context_runall();
454
455   SIMIX_process_empty_trash();
456 }
457
458 void simcall_HANDLER_process_change_host(smx_simcall_t simcall, smx_process_t process,
459                                    smx_host_t dest)
460 {
461   process->new_host = dest;
462 }
463 void SIMIX_process_change_host(smx_process_t process,
464              smx_host_t dest)
465 {
466   xbt_assert((process != NULL), "Invalid parameters");
467   xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
468   process->smx_host = dest;
469   xbt_swag_insert(process, SIMIX_host_priv(dest)->process_list);
470 }
471
472
473 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
474 {
475   smx_synchro_t sync_suspend =
476       SIMIX_process_suspend(process, simcall->issuer);
477
478   if (process != simcall->issuer) {
479     SIMIX_simcall_answer(simcall);
480   } else {
481     xbt_fifo_push(sync_suspend->simcalls, simcall);
482     process->waiting_synchro = sync_suspend;
483     SIMIX_host_execution_suspend(process->waiting_synchro);
484   }
485   /* If we are suspending ourselves, then just do not finish the simcall now */
486 }
487
488 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
489 {
490   xbt_assert((process != NULL), "Invalid parameters");
491
492   if (process->suspended) {
493     XBT_DEBUG("Process '%s' is already suspended", process->name);
494     return NULL;
495   }
496
497   process->suspended = 1;
498
499   /* If we are suspending another process, and it is waiting on a sync,
500      suspend its synchronization. */
501   if (process != issuer) {
502
503     if (process->waiting_synchro) {
504
505       switch (process->waiting_synchro->type) {
506
507         case SIMIX_SYNC_EXECUTE:
508         case SIMIX_SYNC_PARALLEL_EXECUTE:
509           SIMIX_host_execution_suspend(process->waiting_synchro);
510           break;
511
512         case SIMIX_SYNC_COMMUNICATE:
513           SIMIX_comm_suspend(process->waiting_synchro);
514           break;
515
516         case SIMIX_SYNC_SLEEP:
517           SIMIX_process_sleep_suspend(process->waiting_synchro);
518           break;
519
520         case SIMIX_SYNC_SYNCHRO:
521           /* Suspension is delayed to when the process is rescheduled. */
522           break;
523
524         default:
525           xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
526               (int)process->waiting_synchro->type);
527       }
528       return NULL;
529     } else {
530       /* Suspension is delayed to when the process is rescheduled. */
531       return NULL;
532     }
533   } else {
534     /* FIXME: computation size is zero. Is it okay that bound is zero ? */
535     return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0, 0.0, 0);
536   }
537 }
538
539 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
540   SIMIX_process_resume(process, simcall->issuer);
541 }
542
543 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
544 {
545   XBT_IN("process = %p, issuer = %p", process, issuer);
546
547   if(process->context->iwannadie) {
548     XBT_VERB("Ignoring request to suspend a process that is currently dying.");
549     return;
550   }
551
552   if(!process->suspended) return;
553   process->suspended = 0;
554
555   /* If we are resuming another process, resume the synchronization it was waiting for
556      if any. Otherwise add it to the list of process to run in the next round. */
557   if (process != issuer) {
558
559     if (process->waiting_synchro) {
560
561       switch (process->waiting_synchro->type) {
562
563         case SIMIX_SYNC_EXECUTE:
564         case SIMIX_SYNC_PARALLEL_EXECUTE:
565           SIMIX_host_execution_resume(process->waiting_synchro);
566           break;
567
568         case SIMIX_SYNC_COMMUNICATE:
569           SIMIX_comm_resume(process->waiting_synchro);
570           break;
571
572         case SIMIX_SYNC_SLEEP:
573           SIMIX_process_sleep_resume(process->waiting_synchro);
574           break;
575
576         case SIMIX_SYNC_SYNCHRO:
577           /* I cannot resume it now. This is delayed to when the process is rescheduled at
578            * the end of the synchro. */
579           break;
580
581         default:
582           xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
583               (int)process->waiting_synchro->type);
584       }
585     }
586   } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
587
588   XBT_OUT();
589 }
590
591 int SIMIX_process_get_maxpid(void) {
592   return simix_process_maxpid;
593 }
594
595 int SIMIX_process_count(void)
596 {
597   return xbt_swag_size(simix_global->process_list);
598 }
599
600 int SIMIX_process_get_PID(smx_process_t self){
601   if (self == NULL)
602     return 0;
603   else
604     return self->pid;
605 }
606
607 int SIMIX_process_get_PPID(smx_process_t self){
608   if (self == NULL)
609     return 0;
610   else
611     return self->ppid;
612 }
613
614 void* SIMIX_process_self_get_data(smx_process_t self)
615 {
616   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
617
618   if (!self) {
619     return NULL;
620   }
621   return SIMIX_process_get_data(self);
622 }
623
624 void SIMIX_process_self_set_data(smx_process_t self, void *data)
625 {
626   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
627
628   SIMIX_process_set_data(self, data);
629 }
630
631 void* SIMIX_process_get_data(smx_process_t process)
632 {
633   return process->data;
634 }
635
636 void SIMIX_process_set_data(smx_process_t process, void *data)
637 {
638   process->data = data;
639 }
640
641 smx_host_t SIMIX_process_get_host(smx_process_t process)
642 {
643   return process->smx_host;
644 }
645
646 /* needs to be public and without simcall because it is called
647    by exceptions and logging events */
648 const char* SIMIX_process_self_get_name(void) {
649
650   smx_process_t process = SIMIX_process_self();
651   if (process == NULL || process == simix_global->maestro_process)
652     return "";
653
654   return SIMIX_process_get_name(process);
655 }
656
657 const char* SIMIX_process_get_name(smx_process_t process)
658 {
659   return process->name;
660 }
661
662 smx_process_t SIMIX_process_get_by_name(const char* name)
663 {
664   smx_process_t proc;
665
666   xbt_swag_foreach(proc, simix_global->process_list)
667   {
668     if(!strcmp(name, proc->name))
669       return proc;
670   }
671   return NULL;
672 }
673
674 int SIMIX_process_is_suspended(smx_process_t process)
675 {
676   return process->suspended;
677 }
678
679 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
680 {
681   return process->properties;
682 }
683
684 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
685 {
686   smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
687   xbt_fifo_push(sync->simcalls, simcall);
688   simcall->issuer->waiting_synchro = sync;
689 }
690
691 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
692   if (sync->sleep.surf_sleep) {
693     surf_action_cancel(sync->sleep.surf_sleep);
694
695     smx_simcall_t simcall;
696     while ((simcall = xbt_fifo_shift(sync->simcalls))) {
697       simcall_process_sleep__set__result(simcall, SIMIX_DONE);
698       simcall->issuer->waiting_synchro = NULL;
699       if (simcall->issuer->suspended) {
700         XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
701         simcall->issuer->suspended = 0;
702         simcall_HANDLER_process_suspend(simcall, simcall->issuer);
703       } else {
704         SIMIX_simcall_answer(simcall);
705       }
706     }
707     surf_action_unref(sync->sleep.surf_sleep);
708     sync->sleep.surf_sleep = NULL;
709   }
710   xbt_mallocator_release(simix_global->synchro_mallocator, sync);
711   return 0;
712 }
713
714 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
715 {
716   smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
717   res->type = SIMIX_SYNC_JOIN;
718   SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
719   return res;
720 }
721
722 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
723 {
724   if (MC_is_active() || MC_record_replay_is_active()) {
725     MC_process_clock_add(simcall->issuer, duration);
726     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
727     SIMIX_simcall_answer(simcall);
728     return;
729   }
730   smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
731   xbt_fifo_push(sync->simcalls, simcall);
732   simcall->issuer->waiting_synchro = sync;
733 }
734
735 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
736 {
737   smx_synchro_t synchro;
738   smx_host_t host = process->smx_host;
739
740   /* check if the host is active */
741   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
742     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
743            sg_host_name(host));
744   }
745
746   synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
747   synchro->type = SIMIX_SYNC_SLEEP;
748   synchro->name = NULL;
749   synchro->category = NULL;
750
751   synchro->sleep.host = host;
752   synchro->sleep.surf_sleep =
753       surf_workstation_sleep(host, duration);
754
755   surf_action_set_data(synchro->sleep.surf_sleep, synchro);
756   XBT_DEBUG("Create sleep synchronization %p", synchro);
757
758   return synchro;
759 }
760
761 void SIMIX_post_process_sleep(smx_synchro_t synchro)
762 {
763   smx_simcall_t simcall;
764   e_smx_state_t state;
765   xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
766
767   while ((simcall = xbt_fifo_shift(synchro->simcalls))) {
768
769     switch(surf_action_get_state(synchro->sleep.surf_sleep)){
770       case SURF_ACTION_FAILED:
771         simcall->issuer->context->iwannadie = 1;
772         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
773         state = SIMIX_SRC_HOST_FAILURE;
774         break;
775
776       case SURF_ACTION_DONE:
777         state = SIMIX_DONE;
778         break;
779
780       default:
781         THROW_IMPOSSIBLE;
782         break;
783     }
784     if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
785       simcall->issuer->context->iwannadie = 1;
786     }
787     simcall_process_sleep__set__result(simcall, state);
788     simcall->issuer->waiting_synchro = NULL;
789     if (simcall->issuer->suspended) {
790       XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
791       simcall->issuer->suspended = 0;
792       simcall_HANDLER_process_suspend(simcall, simcall->issuer);
793     } else {
794       SIMIX_simcall_answer(simcall);
795     }
796   }
797
798   SIMIX_process_sleep_destroy(synchro);
799 }
800
801 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
802 {
803   XBT_DEBUG("Destroy synchro %p", synchro);
804   xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
805
806   if (synchro->sleep.surf_sleep) {
807     surf_action_unref(synchro->sleep.surf_sleep);
808     synchro->sleep.surf_sleep = NULL;
809   }
810   if (synchro->type == SIMIX_SYNC_SLEEP)
811     xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
812 }
813
814 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
815 {
816   xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
817   surf_action_suspend(synchro->sleep.surf_sleep);
818 }
819
820 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
821 {
822   XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
823   xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
824   surf_action_resume(synchro->sleep.surf_sleep);
825 }
826
827 /**
828  * \brief Calling this function makes the process to yield.
829  *
830  * Only the current process can call this function, giving back the control to
831  * maestro.
832  *
833  * \param self the current process
834  */
835 void SIMIX_process_yield(smx_process_t self)
836 {
837   XBT_DEBUG("Yield process '%s'", self->name);
838
839   /* Go into sleep and return control to maestro */
840   SIMIX_context_suspend(self->context);
841
842   /* Ok, maestro returned control to us */
843   XBT_DEBUG("Control returned to me: '%s'", self->name);
844
845   if (self->new_host) {
846     SIMIX_process_change_host(self, self->new_host);
847     self->new_host = NULL;
848   }
849
850   if (self->context->iwannadie){
851     XBT_DEBUG("I wanna die!");
852     SIMIX_process_stop(self);
853   }
854
855   if (self->suspended) {
856     XBT_DEBUG("Hey! I'm suspended.");
857     xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
858     self->suspended = 0;
859     SIMIX_process_suspend(self, self);
860   }
861
862   if (self->doexception) {
863     XBT_DEBUG("Wait, maestro left me an exception");
864     self->doexception = 0;
865     SMX_THROW();
866   }
867
868 }
869
870 /* callback: context fetching */
871 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
872 {
873   return SIMIX_process_self()->running_ctx;
874 }
875
876 /* callback: termination */
877 void SIMIX_process_exception_terminate(xbt_ex_t * e)
878 {
879   xbt_ex_display(e);
880   xbt_abort();
881 }
882
883 smx_context_t SIMIX_process_get_context(smx_process_t p) {
884   return p->context;
885 }
886
887 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
888   p->context = c;
889 }
890
891 /**
892  * \brief Returns the list of processes to run.
893  */
894 xbt_dynar_t SIMIX_process_get_runnable(void)
895 {
896   return simix_global->process_to_run;
897 }
898
899 /**
900  * \brief Returns the process from PID.
901  */
902 smx_process_t SIMIX_process_from_PID(int PID)
903 {
904   smx_process_t proc;
905   xbt_swag_foreach(proc, simix_global->process_list)
906   {
907    if(proc->pid == PID)
908    return proc;
909   }
910   return NULL;
911 }
912
913 /** @brief returns a dynar containg all currently existing processes */
914 xbt_dynar_t SIMIX_processes_as_dynar(void) {
915   smx_process_t proc;
916   xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
917   xbt_swag_foreach(proc, simix_global->process_list) {
918     xbt_dynar_push(res,&proc);
919   }
920   return res;
921 }
922
923
924 void SIMIX_process_on_exit_runall(smx_process_t process) {
925   s_smx_process_exit_fun_t exit_fun;
926   smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
927                                          SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
928   while (!xbt_dynar_is_empty(process->on_exit)) {
929     exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
930     (exit_fun.fun)((void*)exit_status, exit_fun.arg);
931   }
932 }
933
934 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
935   xbt_assert(process, "current process not found: are you in maestro context ?");
936
937   if (!process->on_exit) {
938     process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
939   }
940
941   s_smx_process_exit_fun_t exit_fun = {fun, data};
942
943   xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
944 }
945
946 /**
947  * \brief Sets the auto-restart status of the process.
948  * If set to 1, the process will be automatically restarted when its host
949  * comes back.
950  */
951 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
952   process->auto_restart = auto_restart;
953 }
954
955 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
956   return SIMIX_process_restart(process, simcall->issuer);
957 }
958 /** @brief Restart a process, starting it again from the beginning. */
959 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
960   XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
961   //retrieve the arguments of the old process
962   //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
963   s_smx_process_arg_t arg;
964   arg.code = process->code;
965   arg.hostname = sg_host_name(process->smx_host);
966   arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
967   arg.argc = process->argc;
968   arg.data = process->data;
969   int i;
970   arg.argv = xbt_new(char*,process->argc + 1);
971   for (i = 0; i < arg.argc; i++) {
972     arg.argv[i] = xbt_strdup(process->argv[i]);
973   }
974   arg.argv[process->argc] = NULL;
975   arg.properties = NULL;
976   arg.auto_restart = process->auto_restart;
977   //kill the old process
978   SIMIX_process_kill(process,issuer);
979   //start the new process
980   smx_process_t new_process;
981   if (simix_global->create_process_function) {
982     simix_global->create_process_function(&new_process,
983                                           arg.argv[0],
984                                           arg.code,
985                                           arg.data,
986                                           arg.hostname,
987                                           arg.kill_time,
988                                           arg.argc,
989                                           arg.argv,
990                                           arg.properties,
991                                           arg.auto_restart,
992                                           NULL);
993   } else {
994     simcall_process_create(&new_process,
995                            arg.argv[0],
996                            arg.code,
997                            arg.data,
998                            arg.hostname,
999                            arg.kill_time,
1000                            arg.argc,
1001                            arg.argv,
1002                            arg.properties,
1003                            arg.auto_restart);
1004
1005   }
1006   return new_process;
1007 }