Logo AND Algorithmique Numérique Distribuée

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