1 /* Copyright (c) 2007-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "src/surf/surf_interface.hpp"
8 #include "smx_private.h"
9 #include "xbt/sysdep.h"
13 #include "src/mc/mc_replay.h"
14 #include "src/mc/mc_client.h"
15 #include "src/simix/smx_private.hpp"
16 #include "src/msg/msg_private.h"
19 #include "src/smpi/private.h"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
23 "Logging specific to SIMIX (process)");
25 unsigned long simix_process_maxpid = 0;
28 * \brief Returns the current agent.
30 * This functions returns the currently running SIMIX process.
32 * \return The SIMIX process
34 smx_process_t SIMIX_process_self(void)
36 smx_context_t self_context = SIMIX_context_self();
38 return self_context ? SIMIX_context_get_process(self_context) : NULL;
42 * \brief Returns whether a process has pending asynchronous communications.
43 * \return true if there are asynchronous communications in this process
45 int SIMIX_process_has_pending_comms(smx_process_t process) {
47 return xbt_fifo_size(process->comms) > 0;
51 * \brief Moves a process to the list of processes to destroy.
53 void SIMIX_process_cleanup(smx_process_t process)
55 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
56 process->name, process, process->waiting_synchro);
58 SIMIX_process_on_exit_runall(process);
60 /* Unregister from the kill timer if any */
61 if (process->kill_timer != NULL)
62 SIMIX_timer_remove(process->kill_timer);
64 xbt_os_mutex_acquire(simix_global->mutex);
66 /* cancel non-blocking communications */
67 smx_synchro_t synchro;
68 while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
70 /* make sure no one will finish the comm after this process is destroyed,
71 * because src_proc or dst_proc would be an invalid pointer */
72 SIMIX_comm_cancel(synchro);
74 if (synchro->comm.src_proc == process) {
75 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
76 synchro, synchro->comm.detached, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
77 synchro->comm.src_proc = NULL;
79 /* I'm not supposed to destroy a detached comm from the sender side, */
80 if (!synchro->comm.detached)
81 SIMIX_comm_destroy(synchro);
83 XBT_DEBUG("Don't destroy it since it's a detached comm");
86 else if (synchro->comm.dst_proc == process){
87 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
88 synchro, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
89 synchro->comm.dst_proc = NULL;
91 if (synchro->comm.detached && synchro->comm.refcount == 1
92 && synchro->comm.src_proc != NULL) {
93 /* the comm will be freed right now, remove it from the sender */
94 xbt_fifo_remove(synchro->comm.src_proc->comms, synchro);
96 SIMIX_comm_destroy(synchro);
99 xbt_die("Communication synchro %p is in my list but I'm not the sender "
100 "or the receiver", synchro);
104 XBT_DEBUG("%p should not be run anymore",process);
105 xbt_swag_remove(process, simix_global->process_list);
107 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
108 xbt_swag_insert(process, simix_global->process_to_destroy);
109 process->context->iwannadie = 0;
111 xbt_os_mutex_release(simix_global->mutex);
117 * Should be called some time to time to free the memory allocated for processes
118 * that have finished (or killed).
120 void SIMIX_process_empty_trash(void)
122 smx_process_t process = NULL;
124 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
125 XBT_DEBUG("Getting rid of %p",process);
127 SIMIX_context_free(process->context);
129 /* Free the exception allocated at creation time */
130 free(process->running_ctx);
131 xbt_dict_free(&process->properties);
133 xbt_fifo_free(process->comms);
135 xbt_dynar_free(&process->on_exit);
137 xbt_free(process->name);
143 * \brief Creates and runs the maestro process
145 void SIMIX_maestro_create(void (*code)(void*), void* data)
147 smx_process_t maestro = NULL;
148 /* Create maestro process and intilialize it */
149 maestro = xbt_new0(s_smx_process_t, 1);
150 maestro->pid = simix_process_maxpid++;
152 maestro->name = (char*) "";
153 maestro->data = data;
154 maestro->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
155 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
158 maestro->context = SIMIX_context_new(NULL, 0, nullptr, NULL, maestro);
161 xbt_die("simix is not initialized, please call MSG_init first");
163 simix_global->context_factory->create_maestro(
164 std::bind(code, data), maestro);
167 maestro->simcall.issuer = maestro;
168 simix_global->maestro_process = maestro;
172 * \brief Stops a process.
174 * Stops the process, execute all the registered on_exit functions,
175 * register it to the list of the process to restart if needed
176 * and stops its context.
178 void SIMIX_process_stop(smx_process_t arg) {
179 /* execute the on_exit functions */
180 SIMIX_process_on_exit_runall(arg);
181 /* Add the process to the list of process to restart, only if the host is down */
182 if (arg->auto_restart && arg->host->is_off()) {
183 SIMIX_host_add_auto_restart_process(arg->host,arg->name,arg->code, arg->data,
184 sg_host_get_name(arg->host),
185 SIMIX_timer_get_date(arg->kill_timer),
186 arg->argc,arg->argv,arg->properties,
189 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_get_name(arg->host));
190 /* stop the context */
191 SIMIX_context_stop(arg->context);
195 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
196 * This function frees the argument.
197 * \return the process created
199 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
201 smx_process_t process = simix_global->create_process_function(
217 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
219 xbt_main_func_t code,
221 const char *hostname,
223 int argc, char **argv,
224 xbt_dict_t properties,
226 return (void*)SIMIX_process_create(name, code, data, hostname,
227 kill_time, argc, argv, properties, auto_restart,
231 static void kill_process(void* process)
233 simix_global->kill_process_function((smx_process_t) process);
237 * \brief Internal function to create a process.
239 * This function actually creates the process.
240 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
241 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
243 * \return the process created
245 smx_process_t SIMIX_process_create(
247 xbt_main_func_t code,
249 const char *hostname,
251 int argc, char **argv,
252 xbt_dict_t properties,
254 smx_process_t parent_process)
256 smx_process_t process = NULL;
257 sg_host_t host = sg_host_by_name(hostname);
259 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
261 if (host->is_off()) {
263 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
265 for (i = 0; i < argc; i++)
270 process = xbt_new0(s_smx_process_t, 1);
272 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
274 process->pid = simix_process_maxpid++;
275 process->name = xbt_strdup(name);
276 process->host = host;
277 process->data = data;
278 process->comms = xbt_fifo_new();
279 process->simcall.issuer = process;
280 /* Initiliaze data segment to default value */
281 SIMIX_segment_index_set(process, -1);
283 if (parent_process != NULL) {
284 process->ppid = SIMIX_process_get_PID(parent_process);
285 /* SMPI process have their own data segment and
286 each other inherit from their father */
288 if(smpi_privatize_global_variables){
289 if( parent_process->pid != 0){
290 SIMIX_segment_index_set(process, parent_process->segment_index);
292 SIMIX_segment_index_set(process, process->pid - 1);
300 /* Process data for auto-restart */
301 process->auto_restart = auto_restart;
302 process->code = code;
303 process->argc = argc;
304 process->argv = argv;
307 XBT_VERB("Create context %s", process->name);
308 process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
310 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
311 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
314 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
318 process->properties = properties;
320 /* Add the process to it's host process list */
321 xbt_swag_insert(process, sg_host_simix(host)->process_list);
323 XBT_DEBUG("Start context '%s'", process->name);
325 /* Now insert it in the global process list and in the process to run list */
326 xbt_swag_insert(process, simix_global->process_list);
327 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
328 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
330 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
331 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
332 sg_host_get_name(process->host), kill_time);
333 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
336 /* Tracing the process creation */
337 TRACE_msg_process_create(process->name, process->pid, process->host);
342 smx_process_t SIMIX_process_attach(
345 const char* hostname,
346 xbt_dict_t properties,
347 smx_process_t parent_process)
349 // This is mostly a copy/paste from SIMIX_process_new(),
350 // it'd be nice to share some code between those two functions.
352 sg_host_t host = sg_host_by_name(hostname);
353 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
355 if (host->is_off()) {
356 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
361 smx_process_t process = xbt_new0(s_smx_process_t, 1);
363 process->pid = simix_process_maxpid++;
364 process->name = xbt_strdup(name);
365 process->host = host;
366 process->data = data;
367 process->comms = xbt_fifo_new();
368 process->simcall.issuer = process;
370 /* Initiliaze data segment to default value */
371 SIMIX_segment_index_set(process, -1);
372 if (parent_process != NULL) {
373 process->ppid = SIMIX_process_get_PID(parent_process);
374 /* SMPI process have their own data segment and
375 each other inherit from their father */
377 if(smpi_privatize_global_variables){
378 if(parent_process->pid != 0){
379 SIMIX_segment_index_set(process, parent_process->segment_index);
381 SIMIX_segment_index_set(process, process->pid - 1);
387 /* Process data for auto-restart */
388 process->auto_restart = false;
389 process->code = nullptr;
391 process->argv = nullptr;
393 XBT_VERB("Create context %s", process->name);
395 xbt_die("simix is not initialized, please call MSG_init first");
396 process->context = simix_global->context_factory->attach(
397 simix_global->cleanup_process_function, process);
399 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
400 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
403 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
407 process->properties = properties;
409 /* Add the process to it's host process list */
410 xbt_swag_insert(process, sg_host_simix(host)->process_list);
412 /* Now insert it in the global process list and in the process to run list */
413 xbt_swag_insert(process, simix_global->process_list);
414 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
415 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
417 /* Tracing the process creation */
418 TRACE_msg_process_create(process->name, process->pid, process->host);
420 auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
422 xbt_die("Not a suitable context");
424 context->attach_start();
428 void SIMIX_process_detach(void)
430 auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
432 xbt_die("Not a suitable context");
434 simix_global->cleanup_process_function(context->process());
436 // Let maestro ignore we are still alive:
437 // xbt_swag_remove(context->process(), simix_global->process_list);
439 // TODDO, Remove from proces list:
440 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
442 context->attach_stop();
447 * \brief Executes the processes from simix_global->process_to_run.
449 * The processes of simix_global->process_to_run are run (in parallel if
450 * possible). On exit, simix_global->process_to_run is empty, and
451 * simix_global->process_that_ran contains the list of processes that just ran.
452 * The two lists are swapped so, be careful when using them before and after a
453 * call to this function.
455 void SIMIX_process_runall(void)
457 SIMIX_context_runall();
459 xbt_dynar_t tmp = simix_global->process_that_ran;
460 simix_global->process_that_ran = simix_global->process_to_run;
461 simix_global->process_to_run = tmp;
462 xbt_dynar_reset(simix_global->process_to_run);
465 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
466 SIMIX_process_kill(process, simcall->issuer);
469 * \brief Internal function to kill a SIMIX process.
471 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
472 * or directly for SIMIX internal purposes.
474 * \param process poor victim
475 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
477 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
479 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
481 process->context->iwannadie = 1;
482 process->blocked = 0;
483 process->suspended = 0;
484 process->doexception = 0;
486 /* destroy the blocking synchro if any */
487 if (process->waiting_synchro) {
489 switch (process->waiting_synchro->type) {
491 case SIMIX_SYNC_EXECUTE:
492 case SIMIX_SYNC_PARALLEL_EXECUTE:
493 SIMIX_execution_destroy(process->waiting_synchro);
496 case SIMIX_SYNC_COMMUNICATE:
497 xbt_fifo_remove(process->comms, process->waiting_synchro);
498 SIMIX_comm_cancel(process->waiting_synchro);
499 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
500 SIMIX_comm_destroy(process->waiting_synchro);
503 case SIMIX_SYNC_SLEEP:
504 SIMIX_process_sleep_destroy(process->waiting_synchro);
507 case SIMIX_SYNC_JOIN:
508 SIMIX_process_sleep_destroy(process->waiting_synchro);
511 case SIMIX_SYNC_SYNCHRO:
512 SIMIX_synchro_stop_waiting(process, &process->simcall);
513 SIMIX_synchro_destroy(process->waiting_synchro);
517 SIMIX_io_destroy(process->waiting_synchro);
522 process->waiting_synchro = NULL;
524 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
525 XBT_DEBUG("Inserting %s in the to_run list", process->name);
526 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
531 /** @brief Ask another process to raise the given exception
533 * @param cat category of exception
534 * @param value value associated to the exception
535 * @param msg string information associated to the exception
537 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
538 SMX_EXCEPTION(process, cat, value, msg);
540 if (process->suspended)
541 SIMIX_process_resume(process,SIMIX_process_self());
543 /* cancel the blocking synchro if any */
544 if (process->waiting_synchro) {
546 switch (process->waiting_synchro->type) {
548 case SIMIX_SYNC_EXECUTE:
549 case SIMIX_SYNC_PARALLEL_EXECUTE:
550 SIMIX_execution_cancel(process->waiting_synchro);
553 case SIMIX_SYNC_COMMUNICATE:
554 xbt_fifo_remove(process->comms, process->waiting_synchro);
555 SIMIX_comm_cancel(process->waiting_synchro);
558 case SIMIX_SYNC_SLEEP:
559 case SIMIX_SYNC_JOIN:
560 SIMIX_process_sleep_destroy(process->waiting_synchro);
561 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
562 XBT_DEBUG("Inserting %s in the to_run list", process->name);
563 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
567 case SIMIX_SYNC_SYNCHRO:
568 SIMIX_synchro_stop_waiting(process, &process->simcall);
572 SIMIX_io_destroy(process->waiting_synchro);
577 process->waiting_synchro = NULL;
581 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
582 SIMIX_process_killall(simcall->issuer, reset_pid);
585 * \brief Kills all running processes.
586 * \param issuer this one will not be killed
588 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
590 smx_process_t p = NULL;
592 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
594 SIMIX_process_kill(p,issuer);
599 simix_process_maxpid = reset_pid;
601 SIMIX_context_runall();
603 SIMIX_process_empty_trash();
606 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
608 process->new_host = dest;
610 void SIMIX_process_change_host(smx_process_t process,
613 xbt_assert((process != NULL), "Invalid parameters");
614 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
615 process->host = dest;
616 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
620 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
622 smx_synchro_t sync_suspend =
623 SIMIX_process_suspend(process, simcall->issuer);
625 if (process != simcall->issuer) {
626 SIMIX_simcall_answer(simcall);
628 xbt_fifo_push(sync_suspend->simcalls, simcall);
629 process->waiting_synchro = sync_suspend;
630 SIMIX_execution_suspend(process->waiting_synchro);
632 /* If we are suspending ourselves, then just do not finish the simcall now */
635 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
637 xbt_assert((process != NULL), "Invalid parameters");
639 if (process->suspended) {
640 XBT_DEBUG("Process '%s' is already suspended", process->name);
644 process->suspended = 1;
646 /* If we are suspending another process, and it is waiting on a sync,
647 suspend its synchronization. */
648 if (process != issuer) {
650 if (process->waiting_synchro) {
652 switch (process->waiting_synchro->type) {
654 case SIMIX_SYNC_EXECUTE:
655 case SIMIX_SYNC_PARALLEL_EXECUTE:
656 SIMIX_execution_suspend(process->waiting_synchro);
659 case SIMIX_SYNC_COMMUNICATE:
660 SIMIX_comm_suspend(process->waiting_synchro);
663 case SIMIX_SYNC_SLEEP:
664 SIMIX_process_sleep_suspend(process->waiting_synchro);
667 case SIMIX_SYNC_SYNCHRO:
668 /* Suspension is delayed to when the process is rescheduled. */
672 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
673 (int)process->waiting_synchro->type);
677 /* Suspension is delayed to when the process is rescheduled. */
681 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
682 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
686 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
687 SIMIX_process_resume(process, simcall->issuer);
690 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
692 XBT_IN("process = %p, issuer = %p", process, issuer);
694 if(process->context->iwannadie) {
695 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
699 if(!process->suspended) return;
700 process->suspended = 0;
702 /* If we are resuming another process, resume the synchronization it was waiting for
703 if any. Otherwise add it to the list of process to run in the next round. */
704 if (process != issuer) {
706 if (process->waiting_synchro) {
708 switch (process->waiting_synchro->type) {
710 case SIMIX_SYNC_EXECUTE:
711 case SIMIX_SYNC_PARALLEL_EXECUTE:
712 SIMIX_execution_resume(process->waiting_synchro);
715 case SIMIX_SYNC_COMMUNICATE:
716 SIMIX_comm_resume(process->waiting_synchro);
719 case SIMIX_SYNC_SLEEP:
720 SIMIX_process_sleep_resume(process->waiting_synchro);
723 case SIMIX_SYNC_SYNCHRO:
724 /* I cannot resume it now. This is delayed to when the process is rescheduled at
725 * the end of the synchro. */
729 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
730 (int)process->waiting_synchro->type);
733 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
738 int SIMIX_process_get_maxpid(void) {
739 return simix_process_maxpid;
742 int SIMIX_process_count(void)
744 return xbt_swag_size(simix_global->process_list);
747 int SIMIX_process_get_PID(smx_process_t self){
754 int SIMIX_process_get_PPID(smx_process_t self){
761 void* SIMIX_process_self_get_data(smx_process_t self)
763 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
768 return SIMIX_process_get_data(self);
771 void SIMIX_process_self_set_data(smx_process_t self, void *data)
773 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
775 SIMIX_process_set_data(self, data);
778 void* SIMIX_process_get_data(smx_process_t process)
780 return process->data;
783 void SIMIX_process_set_data(smx_process_t process, void *data)
785 process->data = data;
788 sg_host_t SIMIX_process_get_host(smx_process_t process)
790 return process->host;
793 xbt_main_func_t SIMIX_process_get_code(void){
794 return SIMIX_process_self()->code;
797 /* needs to be public and without simcall because it is called
798 by exceptions and logging events */
799 const char* SIMIX_process_self_get_name(void) {
801 smx_process_t process = SIMIX_process_self();
802 if (process == NULL || process == simix_global->maestro_process)
805 return SIMIX_process_get_name(process);
808 const char* SIMIX_process_get_name(smx_process_t process)
810 return process->name;
813 smx_process_t SIMIX_process_get_by_name(const char* name)
817 xbt_swag_foreach(proc, simix_global->process_list)
819 if(!strcmp(name, proc->name))
825 int SIMIX_process_is_suspended(smx_process_t process)
827 return process->suspended;
830 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
832 return process->properties;
835 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
837 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
838 xbt_fifo_push(sync->simcalls, simcall);
839 simcall->issuer->waiting_synchro = sync;
842 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
843 if (sync->sleep.surf_sleep) {
844 sync->sleep.surf_sleep->cancel();
846 smx_simcall_t simcall;
847 while ((simcall = (smx_simcall_t) xbt_fifo_shift(sync->simcalls))) {
848 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
849 simcall->issuer->waiting_synchro = NULL;
850 if (simcall->issuer->suspended) {
851 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
852 simcall->issuer->suspended = 0;
853 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
855 SIMIX_simcall_answer(simcall);
858 sync->sleep.surf_sleep->unref();
859 sync->sleep.surf_sleep = NULL;
861 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
865 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
867 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
868 res->type = SIMIX_SYNC_JOIN;
869 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
873 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
875 if (MC_is_active() || MC_record_replay_is_active()) {
876 MC_process_clock_add(simcall->issuer, duration);
877 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
878 SIMIX_simcall_answer(simcall);
881 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
882 xbt_fifo_push(sync->simcalls, simcall);
883 simcall->issuer->waiting_synchro = sync;
886 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
888 sg_host_t host = process->host;
890 /* check if the host is active */
891 if (host->is_off()) {
892 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
893 sg_host_get_name(host));
896 smx_synchro_t synchro = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
897 synchro->type = SIMIX_SYNC_SLEEP;
898 synchro->name = NULL;
899 synchro->category = NULL;
901 synchro->sleep.host = host;
902 synchro->sleep.surf_sleep = surf_host_sleep(host, duration);
904 synchro->sleep.surf_sleep->setData(synchro);
905 XBT_DEBUG("Create sleep synchronization %p", synchro);
910 void SIMIX_post_process_sleep(smx_synchro_t synchro)
912 smx_simcall_t simcall;
914 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
916 while ((simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls))) {
918 switch (synchro->sleep.surf_sleep->getState()){
919 case SURF_ACTION_FAILED:
920 simcall->issuer->context->iwannadie = 1;
921 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
922 state = SIMIX_SRC_HOST_FAILURE;
925 case SURF_ACTION_DONE:
933 if (simcall->issuer->host->is_off()) {
934 simcall->issuer->context->iwannadie = 1;
936 simcall_process_sleep__set__result(simcall, state);
937 simcall->issuer->waiting_synchro = NULL;
938 if (simcall->issuer->suspended) {
939 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
940 simcall->issuer->suspended = 0;
941 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
943 SIMIX_simcall_answer(simcall);
947 SIMIX_process_sleep_destroy(synchro);
950 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
952 XBT_DEBUG("Destroy synchro %p", synchro);
953 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
955 if (synchro->sleep.surf_sleep) {
956 synchro->sleep.surf_sleep->unref();
957 synchro->sleep.surf_sleep = NULL;
959 if (synchro->type == SIMIX_SYNC_SLEEP)
960 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
963 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
965 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
966 synchro->sleep.surf_sleep->suspend();
969 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
971 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
972 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
973 synchro->sleep.surf_sleep->resume();
977 * \brief Calling this function makes the process to yield.
979 * Only the current process can call this function, giving back the control to
982 * \param self the current process
984 void SIMIX_process_yield(smx_process_t self)
986 XBT_DEBUG("Yield process '%s'", self->name);
988 /* Go into sleep and return control to maestro */
989 SIMIX_context_suspend(self->context);
991 /* Ok, maestro returned control to us */
992 XBT_DEBUG("Control returned to me: '%s'", self->name);
994 if (self->new_host) {
995 SIMIX_process_change_host(self, self->new_host);
996 self->new_host = NULL;
999 if (self->context->iwannadie){
1000 XBT_DEBUG("I wanna die!");
1001 SIMIX_process_stop(self);
1004 if (self->suspended) {
1005 XBT_DEBUG("Hey! I'm suspended.");
1006 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
1007 self->suspended = 0;
1008 SIMIX_process_suspend(self, self);
1011 if (self->doexception) {
1012 XBT_DEBUG("Wait, maestro left me an exception");
1013 self->doexception = 0;
1017 if(SMPI_switch_data_segment && self->segment_index != -1){
1018 SMPI_switch_data_segment(self->segment_index);
1022 /* callback: context fetching */
1023 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
1025 smx_process_t process = SIMIX_process_self();
1027 return process->running_ctx;
1032 /* callback: termination */
1033 void SIMIX_process_exception_terminate(xbt_ex_t * e)
1039 smx_context_t SIMIX_process_get_context(smx_process_t p) {
1043 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
1048 * \brief Returns the list of processes to run.
1050 xbt_dynar_t SIMIX_process_get_runnable(void)
1052 return simix_global->process_to_run;
1056 * \brief Returns the process from PID.
1058 smx_process_t SIMIX_process_from_PID(int PID)
1061 xbt_swag_foreach(proc, simix_global->process_list) {
1062 if (proc->pid == (unsigned long) PID)
1068 /** @brief returns a dynar containg all currently existing processes */
1069 xbt_dynar_t SIMIX_processes_as_dynar(void) {
1071 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
1072 xbt_swag_foreach(proc, simix_global->process_list) {
1073 xbt_dynar_push(res,&proc);
1079 void SIMIX_process_on_exit_runall(smx_process_t process) {
1080 s_smx_process_exit_fun_t exit_fun;
1081 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
1082 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
1083 while (!xbt_dynar_is_empty(process->on_exit)) {
1084 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
1085 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
1089 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
1090 xbt_assert(process, "current process not found: are you in maestro context ?");
1092 if (!process->on_exit) {
1093 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
1096 s_smx_process_exit_fun_t exit_fun = {fun, data};
1098 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
1102 * \brief Sets the auto-restart status of the process.
1103 * If set to 1, the process will be automatically restarted when its host
1106 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
1107 process->auto_restart = auto_restart;
1110 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
1111 return SIMIX_process_restart(process, simcall->issuer);
1113 /** @brief Restart a process, starting it again from the beginning. */
1114 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
1115 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_get_name(process->host));
1116 //retrieve the arguments of the old process
1117 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
1118 s_smx_process_arg_t arg;
1119 arg.code = process->code;
1120 arg.hostname = sg_host_get_name(process->host);
1121 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
1122 arg.argc = process->argc;
1123 arg.data = process->data;
1125 arg.argv = xbt_new(char*,process->argc + 1);
1126 for (i = 0; i < arg.argc; i++) {
1127 arg.argv[i] = xbt_strdup(process->argv[i]);
1129 arg.argv[process->argc] = NULL;
1130 arg.properties = NULL;
1131 arg.auto_restart = process->auto_restart;
1132 //kill the old process
1133 SIMIX_process_kill(process,issuer);
1134 //start the new process
1135 smx_process_t new_process;
1136 if (simix_global->create_process_function) {
1137 new_process = simix_global->create_process_function(
1149 new_process = simcall_process_create(
1164 void SIMIX_segment_index_set(smx_process_t proc, int index){
1165 proc->segment_index = index;