1 /* Copyright (c) 2007-2012. 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 "smx_private.h"
8 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14 "Logging specific to SIMIX (process)");
16 unsigned long simix_process_maxpid = 0;
19 * \brief Returns the current agent.
21 * This functions returns the currently running SIMIX process.
23 * \return The SIMIX process
25 XBT_INLINE smx_process_t SIMIX_process_self(void)
27 smx_context_t self_context = SIMIX_context_self();
29 return self_context ? SIMIX_context_get_data(self_context) : NULL;
33 * \brief Returns whether a process has pending asynchronous communications.
34 * \return true if there are asynchronous communications in this process
36 int SIMIX_process_has_pending_comms(smx_process_t process) {
38 return xbt_fifo_size(process->comms) > 0;
42 * \brief Moves a process to the list of processes to destroy.
44 void SIMIX_process_cleanup(smx_process_t process)
46 XBT_DEBUG("Cleanup process %s (%p), waiting action %p",
47 process->name, process, process->waiting_action);
49 /* cancel non-blocking communications */
51 while ((action = xbt_fifo_pop(process->comms))) {
53 /* make sure no one will finish the comm after this process is destroyed,
54 * because src_proc or dst_proc would be an invalid pointer */
55 SIMIX_comm_cancel(action);
57 if (action->comm.src_proc == process) {
58 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
59 action, action->comm.detached, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
60 action->comm.src_proc = NULL;
62 if (action->comm.detached) {
63 if (action->comm.refcount == 0) {
64 XBT_DEBUG("Increase the refcount before destroying it since it's detached");
65 /* I'm not supposed to destroy a detached comm from the sender side,
66 * unless there is no receiver matching the rdv */
67 action->comm.refcount++;
68 SIMIX_comm_destroy(action);
71 XBT_DEBUG("Don't destroy it since its refcount is %d", action->comm.refcount);
74 SIMIX_comm_destroy(action);
77 else if (action->comm.dst_proc == process){
78 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
79 action, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
80 action->comm.dst_proc = NULL;
82 if (action->comm.detached && action->comm.refcount == 1
83 && action->comm.src_proc != NULL) {
84 /* the comm will be freed right now, remove it from the sender */
85 xbt_fifo_remove(action->comm.src_proc->comms, action);
87 SIMIX_comm_destroy(action);
90 xbt_die("Communication action %p is in my list but I'm not the sender "
91 "or the receiver", action);
95 /*xbt_swag_remove(process, simix_global->process_to_run);*/
96 xbt_swag_remove(process, simix_global->process_list);
97 xbt_swag_remove(process, process->smx_host->process_list);
98 xbt_swag_insert(process, simix_global->process_to_destroy);
99 process->context->iwannadie = 0;
105 * Should be called some time to time to free the memory allocated for processes
106 * that have finished (or killed).
108 void SIMIX_process_empty_trash(void)
110 smx_process_t process = NULL;
112 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
113 SIMIX_context_free(process->context);
115 /* Free the exception allocated at creation time */
116 free(process->running_ctx);
117 xbt_dict_free(&process->properties);
119 xbt_fifo_free(process->comms);
121 xbt_dynar_free(&process->on_exit);
129 * \brief Creates and runs the maestro process
131 void SIMIX_create_maestro_process()
133 smx_process_t maestro = NULL;
135 /* Create maestro process and intilialize it */
136 maestro = xbt_new0(s_smx_process_t, 1);
137 maestro->pid = simix_process_maxpid++;
138 maestro->name = (char *) "";
139 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
140 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
141 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
142 maestro->simcall.issuer = maestro;
144 simix_global->maestro_process = maestro;
148 * \brief Stops a process.
150 * Stops the process, execute all the registered on_exit functions,
151 * register it to the list of the process to restart if needed
152 * and stops its context.
154 void SIMIX_process_stop(smx_process_t arg) {
155 /* execute the on_exit functions */
156 SIMIX_process_on_exit_runall(arg);
157 /* Add the process to the list of process to restart, only if
160 if (arg->auto_restart && !SIMIX_host_get_state(arg->smx_host)) {
161 SIMIX_host_add_auto_restart_process(arg->smx_host,arg->name,arg->code, arg->data,
164 arg->argc,arg->argv,arg->properties,
167 XBT_DEBUG("Process %s (%s) is dead",arg->name,arg->smx_host->name);
168 /* stop the context */
169 SIMIX_context_stop(arg->context);
173 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
174 * This function frees the argument.
175 * \return the process created
177 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
179 smx_process_t process;
180 simix_global->create_process_function(
196 * \brief Internal function to create a process.
198 * This function actually creates the process.
199 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
200 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
202 * \return the process created
204 void SIMIX_process_create(smx_process_t *process,
206 xbt_main_func_t code,
208 const char *hostname,
210 int argc, char **argv,
211 xbt_dict_t properties,
215 smx_host_t host = SIMIX_host_get_by_name(hostname);
217 XBT_DEBUG("Start process %s on host %s", name, hostname);
219 if (!SIMIX_host_get_state(host)) {
220 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
224 *process = xbt_new0(s_smx_process_t, 1);
226 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
229 (*process)->pid = simix_process_maxpid++;
230 (*process)->name = xbt_strdup(name);
231 (*process)->smx_host = host;
232 (*process)->data = data;
233 (*process)->comms = xbt_fifo_new();
234 (*process)->simcall.issuer = *process;
235 /* Process data for auto-restart */
236 (*process)->auto_restart = auto_restart;
237 (*process)->code = code;
238 (*process)->argc = argc;
239 (*process)->argv = argv;
240 (*process)->kill_time = kill_time;
243 XBT_VERB("Create context %s", (*process)->name);
244 (*process)->context = SIMIX_context_new(code, argc, argv,
245 simix_global->cleanup_process_function, *process);
247 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
248 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
251 (*process)->properties = properties;
253 /* Add the process to it's host process list */
254 xbt_swag_insert(*process, host->process_list);
256 XBT_DEBUG("Start context '%s'", (*process)->name);
258 /* Now insert it in the global process list and in the process to run list */
259 xbt_swag_insert(*process, simix_global->process_list);
260 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
261 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
264 if (kill_time > SIMIX_get_clock()) {
265 if (simix_global->kill_process_function) {
266 XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
267 (*process)->smx_host->name, kill_time);
268 SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
274 * \brief Executes the processes from simix_global->process_to_run.
276 * The processes of simix_global->process_to_run are run (in parallel if
277 * possible). On exit, simix_global->process_to_run is empty, and
278 * simix_global->process_that_ran contains the list of processes that just ran.
279 * The two lists are swapped so, be careful when using them before and after a
280 * call to this function.
282 void SIMIX_process_runall(void)
284 SIMIX_context_runall();
286 xbt_dynar_t tmp = simix_global->process_that_ran;
287 simix_global->process_that_ran = simix_global->process_to_run;
288 simix_global->process_to_run = tmp;
289 xbt_dynar_reset(simix_global->process_to_run);
293 * \brief Internal function to kill a SIMIX process.
295 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
296 * or directly for SIMIX internal purposes.
298 * \param process poor victim
300 void SIMIX_process_kill(smx_process_t process) {
302 XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
304 process->context->iwannadie = 1;
305 process->blocked = 0;
306 process->suspended = 0;
307 /* FIXME: set doexception to 0 also? */
309 /* destroy the blocking action if any */
310 if (process->waiting_action) {
312 switch (process->waiting_action->type) {
314 case SIMIX_ACTION_EXECUTE:
315 case SIMIX_ACTION_PARALLEL_EXECUTE:
316 SIMIX_host_execution_destroy(process->waiting_action);
319 case SIMIX_ACTION_COMMUNICATE:
320 xbt_fifo_remove(process->comms, process->waiting_action);
321 SIMIX_comm_destroy(process->waiting_action);
324 case SIMIX_ACTION_SLEEP:
325 SIMIX_process_sleep_destroy(process->waiting_action);
328 case SIMIX_ACTION_SYNCHRO:
329 SIMIX_synchro_stop_waiting(process, &process->simcall);
330 SIMIX_synchro_destroy(process->waiting_action);
333 case SIMIX_ACTION_IO:
334 SIMIX_io_destroy(process->waiting_action);
338 if(!xbt_dynar_member(simix_global->process_to_run, &(process)))
339 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
343 * \brief Kills all running processes.
344 * \param issuer this one will not be killed
346 void SIMIX_process_killall(smx_process_t issuer)
348 smx_process_t p = NULL;
350 while ((p = xbt_swag_extract(simix_global->process_list))) {
352 SIMIX_process_kill(p);
356 SIMIX_context_runall();
358 SIMIX_process_empty_trash();
361 void SIMIX_process_change_host(smx_process_t process,
364 xbt_assert((process != NULL), "Invalid parameters");
365 xbt_swag_remove(process, process->smx_host->process_list);
366 process->smx_host = dest;
367 xbt_swag_insert(process, dest->process_list);
370 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
372 process->new_host = dest;
375 void SIMIX_pre_process_suspend(smx_simcall_t simcall)
377 smx_process_t process = simcall->process_suspend.process;
378 smx_action_t action_suspend =
379 SIMIX_process_suspend(process, simcall->issuer);
381 if (process != simcall->issuer) {
382 SIMIX_simcall_answer(simcall);
384 xbt_fifo_push(action_suspend->simcalls, simcall);
385 process->waiting_action = action_suspend;
386 SIMIX_host_execution_suspend(process->waiting_action);
388 /* If we are suspending ourselves, then just do not finish the simcall now */
391 smx_action_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
393 xbt_assert((process != NULL), "Invalid parameters");
395 if (process->suspended) {
396 XBT_DEBUG("Process '%s' is already suspended", process->name);
400 process->suspended = 1;
402 /* If we are suspending another process, and it is waiting on an action,
403 suspend its action. */
404 if (process != issuer) {
406 if (process->waiting_action) {
408 switch (process->waiting_action->type) {
410 case SIMIX_ACTION_EXECUTE:
411 case SIMIX_ACTION_PARALLEL_EXECUTE:
412 SIMIX_host_execution_suspend(process->waiting_action);
415 case SIMIX_ACTION_COMMUNICATE:
416 SIMIX_comm_suspend(process->waiting_action);
419 case SIMIX_ACTION_SLEEP:
420 SIMIX_process_sleep_suspend(process->waiting_action);
423 case SIMIX_ACTION_SYNCHRO:
424 /* Suspension is delayed to when the process is rescheduled. */
428 xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
429 (int)process->waiting_action->type);
433 /* Suspension is delayed to when the process is rescheduled. */
437 return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0);
441 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
443 xbt_assert((process != NULL), "Invalid parameters");
445 XBT_IN("process = %p, issuer = %p", process, issuer);
447 if(process->context->iwannadie) {
448 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
452 if(!process->suspended) return;
453 process->suspended = 0;
455 /* If we are resuming another process, resume the action it was waiting for
456 if any. Otherwise add it to the list of process to run in the next round. */
457 if (process != issuer) {
459 if (process->waiting_action) {
461 switch (process->waiting_action->type) {
463 case SIMIX_ACTION_EXECUTE:
464 case SIMIX_ACTION_PARALLEL_EXECUTE:
465 SIMIX_host_execution_resume(process->waiting_action);
468 case SIMIX_ACTION_COMMUNICATE:
469 SIMIX_comm_resume(process->waiting_action);
472 case SIMIX_ACTION_SLEEP:
473 SIMIX_process_sleep_resume(process->waiting_action);
476 case SIMIX_ACTION_SYNCHRO:
477 /* I cannot resume it now. This is delayed to when the process is rescheduled at
478 * the end of the synchro. */
482 xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
483 (int)process->waiting_action->type);
486 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
491 int SIMIX_process_get_maxpid(void) {
492 return simix_process_maxpid;
495 int SIMIX_process_count(void)
497 return xbt_swag_size(simix_global->process_list);
500 void* SIMIX_process_self_get_data(smx_process_t self)
502 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
507 return SIMIX_process_get_data(self);
510 void SIMIX_process_self_set_data(smx_process_t self, void *data)
512 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
514 SIMIX_process_set_data(self, data);
517 void* SIMIX_process_get_data(smx_process_t process)
519 return process->data;
522 void SIMIX_process_set_data(smx_process_t process, void *data)
524 process->data = data;
527 smx_host_t SIMIX_process_get_host(smx_process_t process)
529 return process->smx_host;
532 /* needs to be public and without simcall because it is called
533 by exceptions and logging events */
534 const char* SIMIX_process_self_get_name(void) {
536 smx_process_t process = SIMIX_process_self();
537 if (process == NULL || process == simix_global->maestro_process)
540 return SIMIX_process_get_name(process);
543 const char* SIMIX_process_get_name(smx_process_t process)
545 return process->name;
548 smx_process_t SIMIX_process_get_by_name(const char* name)
552 xbt_swag_foreach(proc, simix_global->process_list)
554 if(!strcmp(name, proc->name))
560 int SIMIX_process_is_suspended(smx_process_t process)
562 return process->suspended;
565 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
567 return process->properties;
570 void SIMIX_pre_process_sleep(smx_simcall_t simcall)
573 MC_process_clock_add(simcall->issuer, simcall->process_sleep.duration);
574 simcall->process_sleep.result = SIMIX_DONE;
575 SIMIX_simcall_answer(simcall);
578 smx_action_t action = SIMIX_process_sleep(simcall->issuer, simcall->process_sleep.duration);
579 xbt_fifo_push(action->simcalls, simcall);
580 simcall->issuer->waiting_action = action;
583 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
586 smx_host_t host = process->smx_host;
588 /* check if the host is active */
589 if (surf_workstation_model->extension.
590 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
591 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
595 action = xbt_mallocator_get(simix_global->action_mallocator);
596 action->type = SIMIX_ACTION_SLEEP;
599 action->category = NULL;
602 action->sleep.host = host;
603 action->sleep.surf_sleep =
604 surf_workstation_model->extension.workstation.sleep(host->host, duration);
606 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
607 XBT_DEBUG("Create sleep action %p", action);
612 void SIMIX_post_process_sleep(smx_action_t action)
614 smx_simcall_t simcall;
617 while ((simcall = xbt_fifo_shift(action->simcalls))) {
619 switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
620 case SURF_ACTION_FAILED:
621 simcall->issuer->context->iwannadie = 1;
622 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
625 case SURF_ACTION_DONE:
633 if (surf_workstation_model->extension.
634 workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
635 simcall->issuer->context->iwannadie = 1;
637 simcall->process_sleep.result = state;
638 simcall->issuer->waiting_action = NULL;
639 SIMIX_simcall_answer(simcall);
642 SIMIX_process_sleep_destroy(action);
645 void SIMIX_process_sleep_destroy(smx_action_t action)
647 XBT_DEBUG("Destroy action %p", action);
648 if (action->sleep.surf_sleep)
649 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
650 xbt_mallocator_release(simix_global->action_mallocator, action);
653 void SIMIX_process_sleep_suspend(smx_action_t action)
655 surf_workstation_model->suspend(action->sleep.surf_sleep);
658 void SIMIX_process_sleep_resume(smx_action_t action)
660 surf_workstation_model->resume(action->sleep.surf_sleep);
664 * \brief Calling this function makes the process to yield.
666 * Only the current process can call this function, giving back the control to
669 * \param self the current process
671 void SIMIX_process_yield(smx_process_t self)
673 XBT_DEBUG("Yield process '%s'", self->name);
675 /* Go into sleep and return control to maestro */
676 SIMIX_context_suspend(self->context);
678 /* Ok, maestro returned control to us */
679 XBT_DEBUG("Control returned to me: '%s'", self->name);
681 if (self->new_host) {
682 SIMIX_process_change_host(self, self->new_host);
683 self->new_host = NULL;
686 if (self->context->iwannadie){
687 XBT_DEBUG("I wanna die!");
688 SIMIX_process_stop(self);
691 if(self->suspended) {
692 xbt_assert(!self->doexception, "Gloups! This exception may be lost by subsequent calls.");
694 SIMIX_process_suspend(self,self);
697 if (self->doexception) {
698 XBT_DEBUG("Wait, maestro left me an exception");
699 self->doexception = 0;
704 /* callback: context fetching */
705 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
707 return SIMIX_process_self()->running_ctx;
710 /* callback: termination */
711 void SIMIX_process_exception_terminate(xbt_ex_t * e)
717 smx_context_t SIMIX_process_get_context(smx_process_t p) {
721 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
726 * \brief Returns the list of processes to run.
728 xbt_dynar_t SIMIX_process_get_runnable(void)
730 return simix_global->process_to_run;
734 * \brief Returns the process from PID.
736 smx_process_t SIMIX_process_from_PID(int PID)
739 xbt_swag_foreach(proc, simix_global->process_list)
747 /** @brief returns a dynar containg all currently existing processes */
748 xbt_dynar_t SIMIX_processes_as_dynar(void) {
750 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
751 xbt_swag_foreach(proc, simix_global->process_list) {
752 xbt_dynar_push(res,&proc);
756 void SIMIX_process_on_exit_runall(smx_process_t process) {
757 s_smx_process_exit_fun_t exit_fun;
759 while (!xbt_dynar_is_empty(process->on_exit)) {
760 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
761 (exit_fun.fun)(exit_fun.arg);
764 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data) {
765 xbt_assert(process, "current process not found: are you in maestro context ?");
767 if (!process->on_exit) {
768 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
771 s_smx_process_exit_fun_t exit_fun = {fun, data};
773 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
776 * \brief Sets the auto-restart status of the process.
777 * If set to 1, the process will be automatically restarted when its host
780 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
781 process->auto_restart = auto_restart;