Logo AND Algorithmique Numérique Distribuée

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