Logo AND Algorithmique Numérique Distribuée

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