Logo AND Algorithmique Numérique Distribuée

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