1 /* Copyright (c) 2007, 2008, 2009, 2010. 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);
127 * \brief Creates and runs the maestro process
129 void SIMIX_create_maestro_process()
131 smx_process_t maestro = NULL;
133 /* Create maestro process and intilialize it */
134 maestro = xbt_new0(s_smx_process_t, 1);
135 maestro->pid = simix_process_maxpid++;
136 maestro->name = (char *) "";
137 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
138 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
139 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
140 maestro->simcall.issuer = maestro;
142 simix_global->maestro_process = maestro;
147 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
148 * \return the process created
150 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
152 smx_process_t process;
153 simix_global->create_process_function(
167 * \brief Internal function to create a process.
169 * This function actually creates the process.
170 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
171 * or directly for SIMIX internal purposes.
173 * \return the process created
175 void SIMIX_process_create(smx_process_t *process,
177 xbt_main_func_t code,
179 const char *hostname,
180 int argc, char **argv,
181 xbt_dict_t properties) {
184 smx_host_t host = SIMIX_host_get_by_name(hostname);
186 XBT_DEBUG("Start process %s on host %s", name, hostname);
188 if (!SIMIX_host_get_state(host)) {
189 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
193 *process = xbt_new0(s_smx_process_t, 1);
195 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
198 (*process)->pid = simix_process_maxpid++;
199 (*process)->name = xbt_strdup(name);
200 (*process)->smx_host = host;
201 (*process)->data = data;
202 (*process)->comms = xbt_fifo_new();
203 (*process)->simcall.issuer = *process;
205 XBT_VERB("Create context %s", (*process)->name);
206 (*process)->context = SIMIX_context_new(code, argc, argv,
207 simix_global->cleanup_process_function, *process);
209 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
210 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
213 (*process)->properties = properties;
215 /* Add the process to it's host process list */
216 xbt_swag_insert(*process, host->process_list);
218 XBT_DEBUG("Start context '%s'", (*process)->name);
220 /* Now insert it in the global process list and in the process to run list */
221 xbt_swag_insert(*process, simix_global->process_list);
222 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
223 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
228 * \brief Executes the processes from simix_global->process_to_run.
230 * The processes of simix_global->process_to_run are run (in parallel if
231 * possible). On exit, simix_global->process_to_run is empty, and
232 * simix_global->process_that_ran contains the list of processes that just ran.
233 * The two lists are swapped so, be careful when using them before and after a
234 * call to this function.
236 void SIMIX_process_runall(void)
238 SIMIX_context_runall();
240 xbt_dynar_t tmp = simix_global->process_that_ran;
241 simix_global->process_that_ran = simix_global->process_to_run;
242 simix_global->process_to_run = tmp;
243 xbt_dynar_reset(simix_global->process_to_run);
247 * \brief Internal function to kill a SIMIX process.
249 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
250 * or directly for SIMIX internal purposes.
252 * \param process poor victim
254 void SIMIX_process_kill(smx_process_t process) {
256 XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
258 process->context->iwannadie = 1;
259 process->blocked = 0;
260 process->suspended = 0;
261 /* FIXME: set doexception to 0 also? */
263 /* destroy the blocking action if any */
264 if (process->waiting_action) {
266 switch (process->waiting_action->type) {
268 case SIMIX_ACTION_EXECUTE:
269 case SIMIX_ACTION_PARALLEL_EXECUTE:
270 SIMIX_host_execution_destroy(process->waiting_action);
273 case SIMIX_ACTION_COMMUNICATE:
274 xbt_fifo_remove(process->comms, process->waiting_action);
275 SIMIX_comm_destroy(process->waiting_action);
278 case SIMIX_ACTION_SLEEP:
279 SIMIX_process_sleep_destroy(process->waiting_action);
282 case SIMIX_ACTION_SYNCHRO:
283 SIMIX_synchro_stop_waiting(process, &process->simcall);
284 SIMIX_synchro_destroy(process->waiting_action);
287 case SIMIX_ACTION_IO:
288 SIMIX_io_destroy(process->waiting_action);
292 if(!xbt_dynar_member(simix_global->process_to_run, &(process)))
293 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
297 * \brief Kills all running processes.
298 * \param issuer this one will not be killed
300 void SIMIX_process_killall(smx_process_t issuer)
302 smx_process_t p = NULL;
304 while ((p = xbt_swag_extract(simix_global->process_list))) {
306 SIMIX_process_kill(p);
310 SIMIX_context_runall();
312 SIMIX_process_empty_trash();
315 void SIMIX_process_change_host(smx_process_t process,
318 xbt_assert((process != NULL), "Invalid parameters");
319 xbt_swag_remove(process, process->smx_host->process_list);
320 process->smx_host = dest;
321 xbt_swag_insert(process, dest->process_list);
324 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
326 process->new_host = dest;
329 void SIMIX_pre_process_suspend(smx_simcall_t simcall)
331 smx_process_t process = simcall->process_suspend.process;
332 SIMIX_process_suspend(process, simcall->issuer);
334 if (process != simcall->issuer) {
335 SIMIX_simcall_answer(simcall);
337 /* If we are suspending ourselves, then just do not finish the simcall now */
340 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
342 xbt_assert((process != NULL), "Invalid parameters");
344 if (process->suspended) {
345 XBT_DEBUG("Process '%s' is already suspended", process->name);
349 process->suspended = 1;
351 /* If we are suspending another process, and it is waiting on an action,
352 suspend its action. */
353 if (process != issuer) {
355 if (process->waiting_action) {
357 switch (process->waiting_action->type) {
359 case SIMIX_ACTION_EXECUTE:
360 case SIMIX_ACTION_PARALLEL_EXECUTE:
361 SIMIX_host_execution_suspend(process->waiting_action);
364 case SIMIX_ACTION_COMMUNICATE:
365 SIMIX_comm_suspend(process->waiting_action);
368 case SIMIX_ACTION_SLEEP:
369 SIMIX_process_sleep_suspend(process->waiting_action);
373 xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
374 (int)process->waiting_action->type);
380 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
382 xbt_assert((process != NULL), "Invalid parameters");
384 if(process->context->iwannadie) return;
385 process->suspended = 0;
387 /* If we are resuming another process, resume the action it was waiting for
388 if any. Otherwise add it to the list of process to run in the next round. */
389 if (process != issuer) {
391 if (process->waiting_action) {
393 switch (process->waiting_action->type) {
395 case SIMIX_ACTION_EXECUTE:
396 case SIMIX_ACTION_PARALLEL_EXECUTE:
397 SIMIX_host_execution_resume(process->waiting_action);
400 case SIMIX_ACTION_COMMUNICATE:
401 SIMIX_comm_resume(process->waiting_action);
404 case SIMIX_ACTION_SLEEP:
405 SIMIX_process_sleep_resume(process->waiting_action);
409 xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
410 (int)process->waiting_action->type);
414 if(!xbt_dynar_member(simix_global->process_to_run, &(process)))
415 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
420 int SIMIX_process_get_maxpid(void) {
421 return simix_process_maxpid;
424 int SIMIX_process_count(void)
426 return xbt_swag_size(simix_global->process_list);
429 void* SIMIX_process_self_get_data(smx_process_t self)
431 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
436 return SIMIX_process_get_data(self);
439 void SIMIX_process_self_set_data(smx_process_t self, void *data)
441 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
443 SIMIX_process_set_data(self, data);
446 void* SIMIX_process_get_data(smx_process_t process)
448 return process->data;
451 void SIMIX_process_set_data(smx_process_t process, void *data)
453 process->data = data;
456 smx_host_t SIMIX_process_get_host(smx_process_t process)
458 return process->smx_host;
461 /* needs to be public and without simcall because it is called
462 by exceptions and logging events */
463 const char* SIMIX_process_self_get_name(void) {
465 smx_process_t process = SIMIX_process_self();
466 if (process == NULL || process == simix_global->maestro_process)
469 return SIMIX_process_get_name(process);
472 const char* SIMIX_process_get_name(smx_process_t process)
474 return process->name;
477 smx_process_t SIMIX_process_get_by_name(const char* name)
481 xbt_swag_foreach(proc, simix_global->process_list)
483 if(!strcmp(name, proc->name))
489 int SIMIX_process_is_suspended(smx_process_t process)
491 return process->suspended;
494 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
496 return process->properties;
499 void SIMIX_pre_process_sleep(smx_simcall_t simcall)
502 MC_process_clock_add(simcall->issuer, simcall->process_sleep.duration);
503 simcall->process_sleep.result = SIMIX_DONE;
504 SIMIX_simcall_answer(simcall);
507 smx_action_t action = SIMIX_process_sleep(simcall->issuer, simcall->process_sleep.duration);
508 xbt_fifo_push(action->simcalls, simcall);
509 simcall->issuer->waiting_action = action;
512 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
515 smx_host_t host = process->smx_host;
517 /* check if the host is active */
518 if (surf_workstation_model->extension.
519 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
520 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
524 action = xbt_mallocator_get(simix_global->action_mallocator);
525 action->type = SIMIX_ACTION_SLEEP;
528 action->category = NULL;
531 action->sleep.host = host;
532 action->sleep.surf_sleep =
533 surf_workstation_model->extension.workstation.sleep(host->host, duration);
535 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
536 XBT_DEBUG("Create sleep action %p", action);
541 void SIMIX_post_process_sleep(smx_action_t action)
543 smx_simcall_t simcall;
546 while ((simcall = xbt_fifo_shift(action->simcalls))) {
548 switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
549 case SURF_ACTION_FAILED:
550 state = SIMIX_SRC_HOST_FAILURE;
553 case SURF_ACTION_DONE:
561 simcall->process_sleep.result = state;
562 simcall->issuer->waiting_action = NULL;
563 SIMIX_simcall_answer(simcall);
565 SIMIX_process_sleep_destroy(action);
568 void SIMIX_process_sleep_destroy(smx_action_t action)
570 XBT_DEBUG("Destroy action %p", action);
571 if (action->sleep.surf_sleep)
572 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
573 xbt_mallocator_release(simix_global->action_mallocator, action);
576 void SIMIX_process_sleep_suspend(smx_action_t action)
578 surf_workstation_model->suspend(action->sleep.surf_sleep);
581 void SIMIX_process_sleep_resume(smx_action_t action)
583 surf_workstation_model->resume(action->sleep.surf_sleep);
587 * \brief Calling this function makes the process to yield.
589 * Only the current process can call this function, giving back the control to
592 * \param self the current process
594 void SIMIX_process_yield(smx_process_t self)
596 XBT_DEBUG("Yield process '%s'", self->name);
598 /* Go into sleep and return control to maestro */
599 SIMIX_context_suspend(self->context);
601 /* Ok, maestro returned control to us */
602 XBT_DEBUG("Control returned to me: '%s'", self->name);
604 if (self->context->iwannadie){
605 XBT_DEBUG("I wanna die!");
606 SIMIX_context_stop(self->context);
609 if (self->doexception) {
610 XBT_DEBUG("Wait, maestro left me an exception");
611 self->doexception = 0;
615 if (self->new_host) {
616 SIMIX_process_change_host(self, self->new_host);
617 self->new_host = NULL;
621 /* callback: context fetching */
622 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
624 return SIMIX_process_self()->running_ctx;
627 /* callback: termination */
628 void SIMIX_process_exception_terminate(xbt_ex_t * e)
634 smx_context_t SIMIX_process_get_context(smx_process_t p) {
638 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
643 * \brief Returns the list of processes to run.
645 xbt_dynar_t SIMIX_process_get_runnable(void)
647 return simix_global->process_to_run;
651 * \brief Returns the process from PID.
653 smx_process_t SIMIX_process_from_PID(int PID)
656 xbt_swag_foreach(proc, simix_global->process_list)
664 /** @brief returns a dynar containg all currently existing processes */
665 xbt_dynar_t SIMIX_processes_as_dynar(void) {
667 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
668 xbt_swag_foreach(proc, simix_global->process_list) {
669 xbt_dynar_push(res,&proc);