Logo AND Algorithmique Numérique Distribuée

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