Logo AND Algorithmique Numérique Distribuée

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