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. */
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, 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, 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);
104 * Should be called some time to time to free the memory allocated for processes
105 * that have finished (or killed).
107 void SIMIX_process_empty_trash(void)
109 smx_process_t process = NULL;
111 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
112 SIMIX_context_free(process->context);
114 /* Free the exception allocated at creation time */
115 free(process->running_ctx);
116 xbt_dict_free(&process->properties);
118 xbt_fifo_free(process->comms);
126 * \brief Creates and runs the maestro process
128 void SIMIX_create_maestro_process()
130 smx_process_t maestro = NULL;
132 /* Create maestro process and intilialize it */
133 maestro = xbt_new0(s_smx_process_t, 1);
134 maestro->pid = simix_process_maxpid++;
135 maestro->name = (char *) "";
136 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
137 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
138 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
139 maestro->request.issuer = maestro;
141 simix_global->maestro_process = maestro;
146 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
147 * \return the process created
149 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
151 smx_process_t process;
152 simix_global->create_process_function(
166 * \brief Internal function to create a process.
168 * This function actually creates the process.
169 * It may be called when a REQ_PROCESS_CREATE request occurs,
170 * or directly for SIMIX internal purposes.
172 * \return the process created
174 void SIMIX_process_create(smx_process_t *process,
176 xbt_main_func_t code,
178 const char *hostname,
179 int argc, char **argv,
180 xbt_dict_t properties) {
183 smx_host_t host = SIMIX_host_get_by_name(hostname);
185 XBT_DEBUG("Start process %s on host %s", name, hostname);
187 if (!SIMIX_host_get_state(host)) {
188 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
192 *process = xbt_new0(s_smx_process_t, 1);
194 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
197 (*process)->pid = simix_process_maxpid++;
198 (*process)->name = xbt_strdup(name);
199 (*process)->smx_host = host;
200 (*process)->data = data;
201 (*process)->comms = xbt_fifo_new();
202 (*process)->request.issuer = *process;
204 XBT_VERB("Create context %s", (*process)->name);
205 (*process)->context = SIMIX_context_new(code, argc, argv,
206 simix_global->cleanup_process_function, *process);
208 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
209 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
212 (*process)->properties = properties;
214 /* Add the process to it's host process list */
215 xbt_swag_insert(*process, host->process_list);
217 XBT_DEBUG("Start context '%s'", (*process)->name);
219 /* Now insert it in the global process list and in the process to run list */
220 xbt_swag_insert(*process, simix_global->process_list);
221 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
222 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
227 * \brief Executes the processes from simix_global->process_to_run.
229 * The processes of simix_global->process_to_run are run (in parallel if
230 * possible). On exit, simix_global->process_to_run is empty, and
231 * simix_global->process_that_ran contains the list of processes that just ran.
232 * The two lists are swapped so, be careful when using them before and after a
233 * call to this function.
235 void SIMIX_process_runall(void)
237 SIMIX_context_runall();
239 xbt_dynar_t tmp = simix_global->process_that_ran;
240 simix_global->process_that_ran = simix_global->process_to_run;
241 simix_global->process_to_run = tmp;
242 xbt_dynar_reset(simix_global->process_to_run);
246 * \brief Internal function to kill a SIMIX process.
248 * This function may be called when a REQ_PROCESS_KILL request occurs,
249 * or directly for SIMIX internal purposes.
251 * \param process poor victim
253 void SIMIX_process_kill(smx_process_t process) {
255 XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
257 process->context->iwannadie = 1;
258 process->blocked = 0;
259 process->suspended = 0;
260 /* FIXME: set doexception to 0 also? */
262 /* destroy the blocking action if any */
263 if (process->waiting_action) {
265 switch (process->waiting_action->type) {
267 case SIMIX_ACTION_EXECUTE:
268 case SIMIX_ACTION_PARALLEL_EXECUTE:
269 SIMIX_host_execution_destroy(process->waiting_action);
272 case SIMIX_ACTION_COMMUNICATE:
273 SIMIX_comm_destroy(process->waiting_action);
276 case SIMIX_ACTION_SLEEP:
277 SIMIX_process_sleep_destroy(process->waiting_action);
280 case SIMIX_ACTION_SYNCHRO:
281 SIMIX_synchro_stop_waiting(process, &process->request);
282 SIMIX_synchro_destroy(process->waiting_action);
285 case SIMIX_ACTION_IO:
291 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
295 * \brief Kills all running processes.
296 * \param issuer this one will not be killed
298 void SIMIX_process_killall(smx_process_t issuer)
300 smx_process_t p = NULL;
302 while ((p = xbt_swag_extract(simix_global->process_list))) {
304 SIMIX_process_kill(p);
308 SIMIX_context_runall(simix_global->process_to_run);
310 SIMIX_process_empty_trash();
313 void SIMIX_process_change_host(smx_process_t process,
316 xbt_assert((process != NULL), "Invalid parameters");
317 xbt_swag_remove(process, process->smx_host->process_list);
318 process->smx_host = dest;
319 xbt_swag_insert(process, dest->process_list);
322 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
324 process->new_host = dest;
327 void SIMIX_pre_process_suspend(smx_req_t req)
329 smx_process_t process = req->process_suspend.process;
330 SIMIX_process_suspend(process, req->issuer);
332 if (process != req->issuer) {
333 SIMIX_request_answer(req);
335 /* If we are suspending ourselves, then just do not replay the request. */
338 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
340 xbt_assert((process != NULL), "Invalid parameters");
342 if (process->suspended) {
343 XBT_DEBUG("Process '%s' is already suspended", process->name);
347 process->suspended = 1;
349 /* If we are suspending another process, and it is waiting on an action,
350 suspend its action. */
351 if (process != issuer) {
353 if (process->waiting_action) {
355 switch (process->waiting_action->type) {
357 case SIMIX_ACTION_EXECUTE:
358 case SIMIX_ACTION_PARALLEL_EXECUTE:
359 SIMIX_host_execution_suspend(process->waiting_action);
362 case SIMIX_ACTION_COMMUNICATE:
363 SIMIX_comm_suspend(process->waiting_action);
366 case SIMIX_ACTION_SLEEP:
367 SIMIX_process_sleep_suspend(process->waiting_action);
371 xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
372 process->waiting_action->type);
378 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
380 xbt_assert((process != NULL), "Invalid parameters");
382 if (!process->suspended) {
383 XBT_DEBUG("Process '%s' is not suspended", process->name);
387 process->suspended = 0;
389 /* If we are resuming another process, resume the action it was waiting for
390 if any. Otherwise add it to the list of process to run in the next round. */
391 if (process != issuer) {
393 if (process->waiting_action) {
395 switch (process->waiting_action->type) {
397 case SIMIX_ACTION_EXECUTE:
398 case SIMIX_ACTION_PARALLEL_EXECUTE:
399 SIMIX_host_execution_resume(process->waiting_action);
402 case SIMIX_ACTION_COMMUNICATE:
403 SIMIX_comm_resume(process->waiting_action);
406 case SIMIX_ACTION_SLEEP:
407 SIMIX_process_sleep_resume(process->waiting_action);
411 xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
412 process->waiting_action->type);
416 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
421 int SIMIX_process_get_maxpid(void) {
422 return simix_process_maxpid;
425 int SIMIX_process_count(void)
427 return xbt_swag_size(simix_global->process_list);
430 void* SIMIX_process_self_get_data(smx_process_t self)
432 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
437 return SIMIX_process_get_data(self);
440 void SIMIX_process_self_set_data(smx_process_t self, void *data)
442 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
444 SIMIX_process_set_data(self, data);
447 void* SIMIX_process_get_data(smx_process_t process)
449 return process->data;
452 void SIMIX_process_set_data(smx_process_t process, void *data)
454 process->data = data;
457 smx_host_t SIMIX_process_get_host(smx_process_t process)
459 return process->smx_host;
462 /* needs to be public and without request because it is called
463 by exceptions and logging events */
464 const char* SIMIX_process_self_get_name(void) {
466 smx_process_t process = SIMIX_process_self();
467 if (process == NULL || process == simix_global->maestro_process)
470 return SIMIX_process_get_name(process);
473 const char* SIMIX_process_get_name(smx_process_t process)
475 return process->name;
478 smx_process_t SIMIX_process_get_by_name(const char* name)
482 xbt_swag_foreach(proc, simix_global->process_list)
484 if(!strcmp(name, proc->name))
490 int SIMIX_process_is_suspended(smx_process_t process)
492 return process->suspended;
495 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
497 return process->properties;
500 void SIMIX_pre_process_sleep(smx_req_t req)
503 MC_process_clock_add(req->issuer, req->process_sleep.duration);
504 req->process_sleep.result = SIMIX_DONE;
505 SIMIX_request_answer(req);
508 smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
509 xbt_fifo_push(action->request_list, req);
510 req->issuer->waiting_action = action;
513 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
516 smx_host_t host = process->smx_host;
518 /* check if the host is active */
519 if (surf_workstation_model->extension.
520 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
521 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
525 action = xbt_mallocator_get(simix_global->action_mallocator);
526 action->type = SIMIX_ACTION_SLEEP;
529 action->category = NULL;
532 action->sleep.host = host;
533 action->sleep.surf_sleep =
534 surf_workstation_model->extension.workstation.sleep(host->host, duration);
536 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
537 XBT_DEBUG("Create sleep action %p", action);
542 void SIMIX_post_process_sleep(smx_action_t action)
547 while ((req = xbt_fifo_shift(action->request_list))) {
549 switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
550 case SURF_ACTION_FAILED:
551 state = SIMIX_SRC_HOST_FAILURE;
554 case SURF_ACTION_DONE:
562 req->process_sleep.result = state;
563 req->issuer->waiting_action = NULL;
564 SIMIX_request_answer(req);
566 SIMIX_process_sleep_destroy(action);
569 void SIMIX_process_sleep_destroy(smx_action_t action)
571 XBT_DEBUG("Destroy action %p", action);
572 if (action->sleep.surf_sleep)
573 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
574 xbt_mallocator_release(simix_global->action_mallocator, action);
577 void SIMIX_process_sleep_suspend(smx_action_t action)
579 surf_workstation_model->suspend(action->sleep.surf_sleep);
582 void SIMIX_process_sleep_resume(smx_action_t action)
584 surf_workstation_model->resume(action->sleep.surf_sleep);
588 * \brief Calling this function makes the process to yield.
590 * Only the current process can call this function, giving back the control to
593 * \param self the current process
595 void SIMIX_process_yield(smx_process_t self)
597 XBT_DEBUG("Yield process '%s'", self->name);
599 /* Go into sleep and return control to maestro */
600 SIMIX_context_suspend(self->context);
602 /* Ok, maestro returned control to us */
603 XBT_DEBUG("Control returned to me: '%s'", self->name);
605 if (self->context->iwannadie){
606 XBT_DEBUG("I wanna die!");
607 SIMIX_context_stop(self->context);
610 if (self->doexception) {
611 XBT_DEBUG("Wait, maestro left me an exception");
612 self->doexception = 0;
616 if (self->new_host) {
617 SIMIX_process_change_host(self, self->new_host);
618 self->new_host = NULL;
622 /* callback: context fetching */
623 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
625 return SIMIX_process_self()->running_ctx;
628 /* callback: termination */
629 void SIMIX_process_exception_terminate(xbt_ex_t * e)
635 smx_context_t SIMIX_process_get_context(smx_process_t p) {
639 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
644 * \brief Returns the list of processes to run.
646 XBT_INLINE xbt_dynar_t SIMIX_process_get_runnable(void)
648 return simix_global->process_to_run;