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);
145 void create_maestro(std::function<void()> code)
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 = nullptr;
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(code, maestro);
166 maestro->simcall.issuer = maestro;
167 simix_global->maestro_process = maestro;
174 * \brief Creates and runs the maestro process
176 void SIMIX_maestro_create(void (*code)(void*), void* data)
178 simgrid::simix::create_maestro(std::bind(code, data));
182 * \brief Stops a process.
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.
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,arg->code, arg->data,
194 sg_host_get_name(arg->host),
195 SIMIX_timer_get_date(arg->kill_timer),
196 arg->argc,arg->argv,arg->properties,
199 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_get_name(arg->host));
200 /* stop the context */
201 SIMIX_context_stop(arg->context);
205 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
206 * This function frees the argument.
207 * \return the process created
209 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
211 smx_process_t process = simix_global->create_process_function(
227 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
229 xbt_main_func_t code,
231 const char *hostname,
233 int argc, char **argv,
234 xbt_dict_t properties,
236 return (void*)SIMIX_process_create(name, code, data, hostname,
237 kill_time, argc, argv, properties, auto_restart,
241 static void kill_process(void* process)
243 simix_global->kill_process_function((smx_process_t) process);
247 * \brief Internal function to create a process.
249 * This function actually creates the process.
250 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
251 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
253 * \return the process created
255 smx_process_t SIMIX_process_create(
257 xbt_main_func_t code,
259 const char *hostname,
261 int argc, char **argv,
262 xbt_dict_t properties,
264 smx_process_t parent_process)
266 smx_process_t process = NULL;
267 sg_host_t host = sg_host_by_name(hostname);
269 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
273 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
275 for (i = 0; i < argc; i++)
280 process = xbt_new0(s_smx_process_t, 1);
282 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
284 process->pid = simix_process_maxpid++;
285 process->name = xbt_strdup(name);
286 process->host = host;
287 process->data = data;
288 process->comms = xbt_fifo_new();
289 process->simcall.issuer = process;
290 /* Initiliaze data segment to default value */
291 SIMIX_segment_index_set(process, -1);
293 if (parent_process != NULL) {
294 process->ppid = SIMIX_process_get_PID(parent_process);
295 /* SMPI process have their own data segment and
296 each other inherit from their father */
298 if(smpi_privatize_global_variables){
299 if( parent_process->pid != 0){
300 SIMIX_segment_index_set(process, parent_process->segment_index);
302 SIMIX_segment_index_set(process, process->pid - 1);
310 /* Process data for auto-restart */
311 process->auto_restart = auto_restart;
312 process->code = code;
313 process->argc = argc;
314 process->argv = argv;
317 XBT_VERB("Create context %s", process->name);
318 process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
320 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
321 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
324 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
328 process->properties = properties;
330 /* Add the process to it's host process list */
331 xbt_swag_insert(process, sg_host_simix(host)->process_list);
333 XBT_DEBUG("Start context '%s'", process->name);
335 /* Now insert it in the global process list and in the process to run list */
336 xbt_swag_insert(process, simix_global->process_list);
337 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
338 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
340 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
341 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
342 sg_host_get_name(process->host), kill_time);
343 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
346 /* Tracing the process creation */
347 TRACE_msg_process_create(process->name, process->pid, process->host);
352 smx_process_t SIMIX_process_attach(
355 const char* hostname,
356 xbt_dict_t properties,
357 smx_process_t parent_process)
359 // This is mostly a copy/paste from SIMIX_process_new(),
360 // it'd be nice to share some code between those two functions.
362 sg_host_t host = sg_host_by_name(hostname);
363 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
366 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
371 smx_process_t process = xbt_new0(s_smx_process_t, 1);
373 process->pid = simix_process_maxpid++;
374 process->name = xbt_strdup(name);
375 process->host = host;
376 process->data = data;
377 process->comms = xbt_fifo_new();
378 process->simcall.issuer = process;
380 /* Initiliaze data segment to default value */
381 SIMIX_segment_index_set(process, -1);
382 if (parent_process != NULL) {
383 process->ppid = SIMIX_process_get_PID(parent_process);
384 /* SMPI process have their own data segment and
385 each other inherit from their father */
387 if(smpi_privatize_global_variables){
388 if(parent_process->pid != 0){
389 SIMIX_segment_index_set(process, parent_process->segment_index);
391 SIMIX_segment_index_set(process, process->pid - 1);
397 /* Process data for auto-restart */
398 process->auto_restart = false;
399 process->code = nullptr;
401 process->argv = nullptr;
403 XBT_VERB("Create context %s", process->name);
405 xbt_die("simix is not initialized, please call MSG_init first");
406 process->context = simix_global->context_factory->attach(
407 simix_global->cleanup_process_function, process);
409 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
410 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
413 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
417 process->properties = properties;
419 /* Add the process to it's host process list */
420 xbt_swag_insert(process, sg_host_simix(host)->process_list);
422 /* Now insert it in the global process list and in the process to run list */
423 xbt_swag_insert(process, simix_global->process_list);
424 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
425 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
427 /* Tracing the process creation */
428 TRACE_msg_process_create(process->name, process->pid, process->host);
430 auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
432 xbt_die("Not a suitable context");
434 context->attach_start();
438 void SIMIX_process_detach(void)
440 auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
442 xbt_die("Not a suitable context");
444 simix_global->cleanup_process_function(context->process());
446 // Let maestro ignore we are still alive:
447 // xbt_swag_remove(context->process(), simix_global->process_list);
449 // TODDO, Remove from proces list:
450 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
452 context->attach_stop();
457 * \brief Executes the processes from simix_global->process_to_run.
459 * The processes of simix_global->process_to_run are run (in parallel if
460 * possible). On exit, simix_global->process_to_run is empty, and
461 * simix_global->process_that_ran contains the list of processes that just ran.
462 * The two lists are swapped so, be careful when using them before and after a
463 * call to this function.
465 void SIMIX_process_runall(void)
467 SIMIX_context_runall();
469 xbt_dynar_t tmp = simix_global->process_that_ran;
470 simix_global->process_that_ran = simix_global->process_to_run;
471 simix_global->process_to_run = tmp;
472 xbt_dynar_reset(simix_global->process_to_run);
475 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
476 SIMIX_process_kill(process, simcall->issuer);
479 * \brief Internal function to kill a SIMIX process.
481 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
482 * or directly for SIMIX internal purposes.
484 * \param process poor victim
485 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
487 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
489 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
491 process->context->iwannadie = 1;
492 process->blocked = 0;
493 process->suspended = 0;
494 process->doexception = 0;
496 /* destroy the blocking synchro if any */
497 if (process->waiting_synchro) {
499 switch (process->waiting_synchro->type) {
501 case SIMIX_SYNC_EXECUTE:
502 case SIMIX_SYNC_PARALLEL_EXECUTE:
503 SIMIX_execution_destroy(process->waiting_synchro);
506 case SIMIX_SYNC_COMMUNICATE:
507 xbt_fifo_remove(process->comms, process->waiting_synchro);
508 SIMIX_comm_cancel(process->waiting_synchro);
509 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
510 SIMIX_comm_destroy(process->waiting_synchro);
513 case SIMIX_SYNC_SLEEP:
514 SIMIX_process_sleep_destroy(process->waiting_synchro);
517 case SIMIX_SYNC_JOIN:
518 SIMIX_process_sleep_destroy(process->waiting_synchro);
521 case SIMIX_SYNC_SYNCHRO:
522 SIMIX_synchro_stop_waiting(process, &process->simcall);
523 SIMIX_synchro_destroy(process->waiting_synchro);
527 SIMIX_io_destroy(process->waiting_synchro);
532 process->waiting_synchro = NULL;
534 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
535 XBT_DEBUG("Inserting %s in the to_run list", process->name);
536 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
541 /** @brief Ask another process to raise the given exception
543 * @param cat category of exception
544 * @param value value associated to the exception
545 * @param msg string information associated to the exception
547 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
548 SMX_EXCEPTION(process, cat, value, msg);
550 if (process->suspended)
551 SIMIX_process_resume(process,SIMIX_process_self());
553 /* cancel the blocking synchro if any */
554 if (process->waiting_synchro) {
556 switch (process->waiting_synchro->type) {
558 case SIMIX_SYNC_EXECUTE:
559 case SIMIX_SYNC_PARALLEL_EXECUTE:
560 SIMIX_execution_cancel(process->waiting_synchro);
563 case SIMIX_SYNC_COMMUNICATE:
564 xbt_fifo_remove(process->comms, process->waiting_synchro);
565 SIMIX_comm_cancel(process->waiting_synchro);
568 case SIMIX_SYNC_SLEEP:
569 case SIMIX_SYNC_JOIN:
570 SIMIX_process_sleep_destroy(process->waiting_synchro);
571 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
572 XBT_DEBUG("Inserting %s in the to_run list", process->name);
573 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
577 case SIMIX_SYNC_SYNCHRO:
578 SIMIX_synchro_stop_waiting(process, &process->simcall);
582 SIMIX_io_destroy(process->waiting_synchro);
587 process->waiting_synchro = NULL;
591 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
592 SIMIX_process_killall(simcall->issuer, reset_pid);
595 * \brief Kills all running processes.
596 * \param issuer this one will not be killed
598 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
600 smx_process_t p = NULL;
602 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
604 SIMIX_process_kill(p,issuer);
609 simix_process_maxpid = reset_pid;
611 SIMIX_context_runall();
613 SIMIX_process_empty_trash();
616 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
618 process->new_host = dest;
620 void SIMIX_process_change_host(smx_process_t process,
623 xbt_assert((process != NULL), "Invalid parameters");
624 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
625 process->host = dest;
626 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
630 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
632 smx_synchro_t sync_suspend =
633 SIMIX_process_suspend(process, simcall->issuer);
635 if (process != simcall->issuer) {
636 SIMIX_simcall_answer(simcall);
638 xbt_fifo_push(sync_suspend->simcalls, simcall);
639 process->waiting_synchro = sync_suspend;
640 SIMIX_execution_suspend(process->waiting_synchro);
642 /* If we are suspending ourselves, then just do not finish the simcall now */
645 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
647 xbt_assert((process != NULL), "Invalid parameters");
649 if (process->suspended) {
650 XBT_DEBUG("Process '%s' is already suspended", process->name);
654 process->suspended = 1;
656 /* If we are suspending another process, and it is waiting on a sync,
657 suspend its synchronization. */
658 if (process != issuer) {
660 if (process->waiting_synchro) {
662 switch (process->waiting_synchro->type) {
664 case SIMIX_SYNC_EXECUTE:
665 case SIMIX_SYNC_PARALLEL_EXECUTE:
666 SIMIX_execution_suspend(process->waiting_synchro);
669 case SIMIX_SYNC_COMMUNICATE:
670 SIMIX_comm_suspend(process->waiting_synchro);
673 case SIMIX_SYNC_SLEEP:
674 SIMIX_process_sleep_suspend(process->waiting_synchro);
677 case SIMIX_SYNC_SYNCHRO:
678 /* Suspension is delayed to when the process is rescheduled. */
682 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
683 (int)process->waiting_synchro->type);
687 /* Suspension is delayed to when the process is rescheduled. */
691 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
692 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
696 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
697 SIMIX_process_resume(process, simcall->issuer);
700 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
702 XBT_IN("process = %p, issuer = %p", process, issuer);
704 if(process->context->iwannadie) {
705 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
709 if(!process->suspended) return;
710 process->suspended = 0;
712 /* If we are resuming another process, resume the synchronization it was waiting for
713 if any. Otherwise add it to the list of process to run in the next round. */
714 if (process != issuer) {
716 if (process->waiting_synchro) {
718 switch (process->waiting_synchro->type) {
720 case SIMIX_SYNC_EXECUTE:
721 case SIMIX_SYNC_PARALLEL_EXECUTE:
722 SIMIX_execution_resume(process->waiting_synchro);
725 case SIMIX_SYNC_COMMUNICATE:
726 SIMIX_comm_resume(process->waiting_synchro);
729 case SIMIX_SYNC_SLEEP:
730 SIMIX_process_sleep_resume(process->waiting_synchro);
733 case SIMIX_SYNC_SYNCHRO:
734 /* I cannot resume it now. This is delayed to when the process is rescheduled at
735 * the end of the synchro. */
739 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
740 (int)process->waiting_synchro->type);
743 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
748 int SIMIX_process_get_maxpid(void) {
749 return simix_process_maxpid;
752 int SIMIX_process_count(void)
754 return xbt_swag_size(simix_global->process_list);
757 int SIMIX_process_get_PID(smx_process_t self){
764 int SIMIX_process_get_PPID(smx_process_t self){
771 void* SIMIX_process_self_get_data()
773 smx_process_t self = SIMIX_process_self();
778 return SIMIX_process_get_data(self);
781 void SIMIX_process_self_set_data(void *data)
783 smx_process_t self = SIMIX_process_self();
785 SIMIX_process_set_data(self, data);
788 void* SIMIX_process_get_data(smx_process_t process)
790 return process->data;
793 void SIMIX_process_set_data(smx_process_t process, void *data)
795 process->data = data;
798 sg_host_t SIMIX_process_get_host(smx_process_t process)
800 return process->host;
803 xbt_main_func_t SIMIX_process_get_code(void){
804 return SIMIX_process_self()->code;
807 /* needs to be public and without simcall because it is called
808 by exceptions and logging events */
809 const char* SIMIX_process_self_get_name(void) {
811 smx_process_t process = SIMIX_process_self();
812 if (process == NULL || process == simix_global->maestro_process)
815 return SIMIX_process_get_name(process);
818 const char* SIMIX_process_get_name(smx_process_t process)
820 return process->name;
823 smx_process_t SIMIX_process_get_by_name(const char* name)
827 xbt_swag_foreach(proc, simix_global->process_list)
829 if(!strcmp(name, proc->name))
835 int SIMIX_process_is_suspended(smx_process_t process)
837 return process->suspended;
840 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
842 return process->properties;
845 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
847 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
848 xbt_fifo_push(sync->simcalls, simcall);
849 simcall->issuer->waiting_synchro = sync;
852 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
853 if (sync->sleep.surf_sleep) {
854 sync->sleep.surf_sleep->cancel();
856 smx_simcall_t simcall;
857 while ((simcall = (smx_simcall_t) xbt_fifo_shift(sync->simcalls))) {
858 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
859 simcall->issuer->waiting_synchro = NULL;
860 if (simcall->issuer->suspended) {
861 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
862 simcall->issuer->suspended = 0;
863 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
865 SIMIX_simcall_answer(simcall);
868 sync->sleep.surf_sleep->unref();
869 sync->sleep.surf_sleep = NULL;
871 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
875 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
877 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
878 res->type = SIMIX_SYNC_JOIN;
879 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
883 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
885 if (MC_is_active() || MC_record_replay_is_active()) {
886 MC_process_clock_add(simcall->issuer, duration);
887 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
888 SIMIX_simcall_answer(simcall);
891 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
892 xbt_fifo_push(sync->simcalls, simcall);
893 simcall->issuer->waiting_synchro = sync;
896 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
898 sg_host_t host = process->host;
900 /* check if the host is active */
902 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
903 sg_host_get_name(host));
906 smx_synchro_t synchro = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
907 synchro->type = SIMIX_SYNC_SLEEP;
908 synchro->name = NULL;
909 synchro->category = NULL;
911 synchro->sleep.host = host;
912 synchro->sleep.surf_sleep = surf_host_sleep(host, duration);
914 synchro->sleep.surf_sleep->setData(synchro);
915 XBT_DEBUG("Create sleep synchronization %p", synchro);
920 void SIMIX_post_process_sleep(smx_synchro_t synchro)
922 smx_simcall_t simcall;
924 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
926 while ((simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls))) {
928 switch (synchro->sleep.surf_sleep->getState()){
929 case SURF_ACTION_FAILED:
930 simcall->issuer->context->iwannadie = 1;
931 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
932 state = SIMIX_SRC_HOST_FAILURE;
935 case SURF_ACTION_DONE:
943 if (simcall->issuer->host->isOff()) {
944 simcall->issuer->context->iwannadie = 1;
946 simcall_process_sleep__set__result(simcall, state);
947 simcall->issuer->waiting_synchro = NULL;
948 if (simcall->issuer->suspended) {
949 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
950 simcall->issuer->suspended = 0;
951 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
953 SIMIX_simcall_answer(simcall);
957 SIMIX_process_sleep_destroy(synchro);
960 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
962 XBT_DEBUG("Destroy synchro %p", synchro);
963 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
965 if (synchro->sleep.surf_sleep) {
966 synchro->sleep.surf_sleep->unref();
967 synchro->sleep.surf_sleep = NULL;
969 if (synchro->type == SIMIX_SYNC_SLEEP)
970 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
973 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
975 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
976 synchro->sleep.surf_sleep->suspend();
979 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
981 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
982 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
983 synchro->sleep.surf_sleep->resume();
987 * \brief Calling this function makes the process to yield.
989 * Only the current process can call this function, giving back the control to
992 * \param self the current process
994 void SIMIX_process_yield(smx_process_t self)
996 XBT_DEBUG("Yield process '%s'", self->name);
998 /* Go into sleep and return control to maestro */
999 SIMIX_context_suspend(self->context);
1001 /* Ok, maestro returned control to us */
1002 XBT_DEBUG("Control returned to me: '%s'", self->name);
1004 if (self->new_host) {
1005 SIMIX_process_change_host(self, self->new_host);
1006 self->new_host = NULL;
1009 if (self->context->iwannadie){
1010 XBT_DEBUG("I wanna die!");
1011 SIMIX_process_stop(self);
1014 if (self->suspended) {
1015 XBT_DEBUG("Hey! I'm suspended.");
1016 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
1017 self->suspended = 0;
1018 SIMIX_process_suspend(self, self);
1021 if (self->doexception) {
1022 XBT_DEBUG("Wait, maestro left me an exception");
1023 self->doexception = 0;
1027 if(SMPI_switch_data_segment && self->segment_index != -1){
1028 SMPI_switch_data_segment(self->segment_index);
1032 /* callback: context fetching */
1033 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
1035 smx_process_t process = SIMIX_process_self();
1037 return process->running_ctx;
1042 /* callback: termination */
1043 void SIMIX_process_exception_terminate(xbt_ex_t * e)
1049 smx_context_t SIMIX_process_get_context(smx_process_t p) {
1053 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
1058 * \brief Returns the list of processes to run.
1060 xbt_dynar_t SIMIX_process_get_runnable(void)
1062 return simix_global->process_to_run;
1066 * \brief Returns the process from PID.
1068 smx_process_t SIMIX_process_from_PID(int PID)
1071 xbt_swag_foreach(proc, simix_global->process_list) {
1072 if (proc->pid == (unsigned long) PID)
1078 /** @brief returns a dynar containg all currently existing processes */
1079 xbt_dynar_t SIMIX_processes_as_dynar(void) {
1081 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
1082 xbt_swag_foreach(proc, simix_global->process_list) {
1083 xbt_dynar_push(res,&proc);
1089 void SIMIX_process_on_exit_runall(smx_process_t process) {
1090 s_smx_process_exit_fun_t exit_fun;
1091 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
1092 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
1093 while (!xbt_dynar_is_empty(process->on_exit)) {
1094 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
1095 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
1099 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
1100 xbt_assert(process, "current process not found: are you in maestro context ?");
1102 if (!process->on_exit) {
1103 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
1106 s_smx_process_exit_fun_t exit_fun = {fun, data};
1108 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
1112 * \brief Sets the auto-restart status of the process.
1113 * If set to 1, the process will be automatically restarted when its host
1116 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
1117 process->auto_restart = auto_restart;
1120 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
1121 return SIMIX_process_restart(process, simcall->issuer);
1123 /** @brief Restart a process, starting it again from the beginning. */
1124 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
1125 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_get_name(process->host));
1126 //retrieve the arguments of the old process
1127 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
1128 s_smx_process_arg_t arg;
1129 arg.code = process->code;
1130 arg.hostname = sg_host_get_name(process->host);
1131 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
1132 arg.argc = process->argc;
1133 arg.data = process->data;
1135 arg.argv = xbt_new(char*,process->argc + 1);
1136 for (i = 0; i < arg.argc; i++) {
1137 arg.argv[i] = xbt_strdup(process->argv[i]);
1139 arg.argv[process->argc] = NULL;
1140 arg.properties = NULL;
1141 arg.auto_restart = process->auto_restart;
1142 //kill the old process
1143 SIMIX_process_kill(process,issuer);
1144 //start the new process
1145 smx_process_t new_process;
1146 if (simix_global->create_process_function) {
1147 new_process = simix_global->create_process_function(
1159 new_process = simcall_process_create(
1174 void SIMIX_segment_index_set(smx_process_t proc, int index){
1175 proc->segment_index = index;