Logo AND Algorithmique Numérique Distribuée

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