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 Move a process to the list of processes to destroy.
35 void SIMIX_process_cleanup(smx_process_t process)
37 XBT_DEBUG("Cleanup process %s (%p), waiting action %p",
38 process->name, process, process->waiting_action);
40 /* cancel non-blocking communications */
42 while ((action = xbt_fifo_pop(process->comms))) {
44 /* make sure no one will finish the comm after this process is destroyed */
45 SIMIX_comm_cancel(action);
47 if (action->comm.src_proc == process) {
48 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
49 action, action->comm.detached, action->state, action->comm.src_proc, action->comm.dst_proc);
50 action->comm.src_proc = NULL;
52 if (action->comm.detached) {
53 if (action->comm.refcount == 0) {
54 XBT_DEBUG("Increase the refcount before destroying it");
55 /* I'm not supposed to destroy a detached comm from the sender side,
56 * unless there is no receiver matching the rdv */
57 action->comm.refcount++;
58 SIMIX_comm_destroy(action);
60 XBT_DEBUG("Don't destroy it since its refcount is %d",action->comm.refcount);
63 SIMIX_comm_destroy(action);
66 else if (action->comm.dst_proc == process){
67 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
68 action, action->state, action->comm.src_proc, action->comm.dst_proc);
69 action->comm.dst_proc = NULL;
71 if (action->comm.detached && action->comm.refcount == 1
72 && action->comm.src_proc != NULL) {
73 /* the comm will be freed right now, remove it from the sender */
74 xbt_fifo_remove(action->comm.src_proc->comms, action);
76 SIMIX_comm_destroy(action);
79 xbt_die("Communication action %p is in my list but I'm not the sender "
80 "or the receiver", action);
84 /*xbt_swag_remove(process, simix_global->process_to_run);*/
85 xbt_swag_remove(process, simix_global->process_list);
86 xbt_swag_remove(process, process->smx_host->process_list);
87 xbt_swag_insert(process, simix_global->process_to_destroy);
93 * Should be called some time to time to free the memory allocated for processes
94 * that have finished (or killed).
96 void SIMIX_process_empty_trash(void)
98 smx_process_t process = NULL;
100 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
101 SIMIX_context_free(process->context);
103 /* Free the exception allocated at creation time */
104 free(process->running_ctx);
105 xbt_dict_free(&process->properties);
107 xbt_fifo_free(process->comms);
115 * \brief Creates and runs the maestro process
117 void SIMIX_create_maestro_process()
119 smx_process_t maestro = NULL;
121 /* Create maestro process and intilialize it */
122 maestro = xbt_new0(s_smx_process_t, 1);
123 maestro->pid = simix_process_maxpid++;
124 maestro->name = (char *) "";
125 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
126 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
127 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
128 maestro->request.issuer = maestro;
130 simix_global->maestro_process = maestro;
135 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
136 * \return the process created
138 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
140 smx_process_t process;
141 simix_global->create_process_function(
155 * \brief Internal function to create a process.
157 * This function actually creates the process.
158 * It may be called when a REQ_PROCESS_CREATE request occurs,
159 * or directly for SIMIX internal purposes.
161 * \return the process created
163 void SIMIX_process_create(smx_process_t *process,
165 xbt_main_func_t code,
167 const char *hostname,
168 int argc, char **argv,
169 xbt_dict_t properties) {
172 smx_host_t host = SIMIX_host_get_by_name(hostname);
174 XBT_DEBUG("Start process %s on host %s", name, hostname);
176 if (!SIMIX_host_get_state(host)) {
177 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
181 *process = xbt_new0(s_smx_process_t, 1);
183 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
186 (*process)->pid = simix_process_maxpid++;
187 (*process)->name = xbt_strdup(name);
188 (*process)->smx_host = host;
189 (*process)->data = data;
190 (*process)->comms = xbt_fifo_new();
191 (*process)->request.issuer = *process;
193 XBT_VERB("Create context %s", (*process)->name);
194 (*process)->context = SIMIX_context_new(code, argc, argv,
195 simix_global->cleanup_process_function, *process);
197 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
198 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
201 (*process)->properties = properties;
203 /* Add the process to it's host process list */
204 xbt_swag_insert(*process, host->process_list);
206 XBT_DEBUG("Start context '%s'", (*process)->name);
208 /* Now insert it in the global process list and in the process to run list */
209 xbt_swag_insert(*process, simix_global->process_list);
210 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
211 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
216 * \brief Executes the processes from simix_global->process_to_run.
218 * The processes of simix_global->process_to_run are run (in parallel if
219 * possible). On exit, simix_global->process_to_run is empty, and
220 * simix_global->process_that_ran contains the list of processes that just ran.
221 * The two lists are swapped so, be careful when using them before and after a
222 * call to this function.
224 void SIMIX_process_runall(void)
226 SIMIX_context_runall();
228 xbt_dynar_t tmp = simix_global->process_that_ran;
229 simix_global->process_that_ran = simix_global->process_to_run;
230 simix_global->process_to_run = tmp;
231 xbt_dynar_reset(simix_global->process_to_run);
235 * \brief Internal function to kill a SIMIX process.
237 * This function may be called when a REQ_PROCESS_KILL request occurs,
238 * or directly for SIMIX internal purposes.
240 * \param process poor victim
242 void SIMIX_process_kill(smx_process_t process) {
244 XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
246 process->context->iwannadie = 1;
247 process->blocked = 0;
248 process->suspended = 0;
249 /* FIXME: set doexception to 0 also? */
251 /* destroy the blocking action if any */
252 if (process->waiting_action) {
254 switch (process->waiting_action->type) {
256 case SIMIX_ACTION_EXECUTE:
257 case SIMIX_ACTION_PARALLEL_EXECUTE:
258 SIMIX_host_execution_destroy(process->waiting_action);
261 case SIMIX_ACTION_COMMUNICATE:
262 SIMIX_comm_destroy(process->waiting_action);
265 case SIMIX_ACTION_SLEEP:
266 SIMIX_process_sleep_destroy(process->waiting_action);
269 case SIMIX_ACTION_SYNCHRO:
270 SIMIX_synchro_stop_waiting(process, &process->request);
271 SIMIX_synchro_destroy(process->waiting_action);
274 case SIMIX_ACTION_IO:
280 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
284 * \brief Kills all running processes.
285 * \param issuer this one will not be killed
287 void SIMIX_process_killall(smx_process_t issuer)
289 smx_process_t p = NULL;
291 while ((p = xbt_swag_extract(simix_global->process_list))) {
293 SIMIX_process_kill(p);
297 SIMIX_context_runall(simix_global->process_to_run);
299 SIMIX_process_empty_trash();
302 void SIMIX_process_change_host(smx_process_t process,
305 xbt_assert((process != NULL), "Invalid parameters");
306 xbt_swag_remove(process, process->smx_host->process_list);
307 process->smx_host = dest;
308 xbt_swag_insert(process, dest->process_list);
311 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
313 process->new_host = dest;
316 void SIMIX_pre_process_suspend(smx_req_t req)
318 smx_process_t process = req->process_suspend.process;
319 SIMIX_process_suspend(process, req->issuer);
321 if (process != req->issuer) {
322 SIMIX_request_answer(req);
324 /* If we are suspending ourselves, then just do not replay the request. */
327 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
329 xbt_assert((process != NULL), "Invalid parameters");
331 if (process->suspended) {
332 XBT_DEBUG("Process '%s' is already suspended", process->name);
336 process->suspended = 1;
338 /* If we are suspending another process, and it is waiting on an action,
339 suspend its action. */
340 if (process != issuer) {
342 if (process->waiting_action) {
344 switch (process->waiting_action->type) {
346 case SIMIX_ACTION_EXECUTE:
347 case SIMIX_ACTION_PARALLEL_EXECUTE:
348 SIMIX_host_execution_suspend(process->waiting_action);
351 case SIMIX_ACTION_COMMUNICATE:
352 SIMIX_comm_suspend(process->waiting_action);
355 case SIMIX_ACTION_SLEEP:
356 SIMIX_process_sleep_suspend(process->waiting_action);
360 xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
361 process->waiting_action->type);
367 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
369 xbt_assert((process != NULL), "Invalid parameters");
371 if (!process->suspended) {
372 XBT_DEBUG("Process '%s' is not suspended", process->name);
376 process->suspended = 0;
378 /* If we are resuming another process, resume the action it was waiting for
379 if any. Otherwise add it to the list of process to run in the next round. */
380 if (process != issuer) {
382 if (process->waiting_action) {
384 switch (process->waiting_action->type) {
386 case SIMIX_ACTION_EXECUTE:
387 case SIMIX_ACTION_PARALLEL_EXECUTE:
388 SIMIX_host_execution_resume(process->waiting_action);
391 case SIMIX_ACTION_COMMUNICATE:
392 SIMIX_comm_resume(process->waiting_action);
395 case SIMIX_ACTION_SLEEP:
396 SIMIX_process_sleep_resume(process->waiting_action);
400 xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
401 process->waiting_action->type);
405 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
410 int SIMIX_process_get_maxpid(void) {
411 return simix_process_maxpid;
414 int SIMIX_process_count(void)
416 return xbt_swag_size(simix_global->process_list);
419 void* SIMIX_process_self_get_data(smx_process_t self)
421 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
426 return SIMIX_process_get_data(self);
429 void SIMIX_process_self_set_data(smx_process_t self, void *data)
431 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
433 SIMIX_process_set_data(self, data);
436 void* SIMIX_process_get_data(smx_process_t process)
438 return process->data;
441 void SIMIX_process_set_data(smx_process_t process, void *data)
443 process->data = data;
446 smx_host_t SIMIX_process_get_host(smx_process_t process)
448 return process->smx_host;
451 /* needs to be public and without request because it is called
452 by exceptions and logging events */
453 const char* SIMIX_process_self_get_name(void) {
455 smx_process_t process = SIMIX_process_self();
456 if (process == NULL || process == simix_global->maestro_process)
459 return SIMIX_process_get_name(process);
462 const char* SIMIX_process_get_name(smx_process_t process)
464 return process->name;
467 smx_process_t SIMIX_process_get_by_name(const char* name)
471 xbt_swag_foreach(proc, simix_global->process_list)
473 if(!strcmp(name, proc->name))
479 int SIMIX_process_is_suspended(smx_process_t process)
481 return process->suspended;
484 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
486 return process->properties;
489 void SIMIX_pre_process_sleep(smx_req_t req)
492 MC_process_clock_add(req->issuer, req->process_sleep.duration);
493 req->process_sleep.result = SIMIX_DONE;
494 SIMIX_request_answer(req);
497 smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
498 xbt_fifo_push(action->request_list, req);
499 req->issuer->waiting_action = action;
502 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
505 smx_host_t host = process->smx_host;
507 /* check if the host is active */
508 if (surf_workstation_model->extension.
509 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
510 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
514 action = xbt_mallocator_get(simix_global->action_mallocator);
515 action->type = SIMIX_ACTION_SLEEP;
518 action->category = NULL;
521 action->sleep.host = host;
522 action->sleep.surf_sleep =
523 surf_workstation_model->extension.workstation.sleep(host->host, duration);
525 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
526 XBT_DEBUG("Create sleep action %p", action);
531 void SIMIX_post_process_sleep(smx_action_t action)
536 while ((req = xbt_fifo_shift(action->request_list))) {
538 switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
539 case SURF_ACTION_FAILED:
540 state = SIMIX_SRC_HOST_FAILURE;
543 case SURF_ACTION_DONE:
551 req->process_sleep.result = state;
552 req->issuer->waiting_action = NULL;
553 SIMIX_request_answer(req);
555 SIMIX_process_sleep_destroy(action);
558 void SIMIX_process_sleep_destroy(smx_action_t action)
560 XBT_DEBUG("Destroy action %p", action);
561 if (action->sleep.surf_sleep)
562 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
563 xbt_mallocator_release(simix_global->action_mallocator, action);
566 void SIMIX_process_sleep_suspend(smx_action_t action)
568 surf_workstation_model->suspend(action->sleep.surf_sleep);
571 void SIMIX_process_sleep_resume(smx_action_t action)
573 surf_workstation_model->resume(action->sleep.surf_sleep);
577 * \brief Calling this function makes the process to yield.
579 * Only the current process can call this function, giving back the control to
582 * \param self the current process
584 void SIMIX_process_yield(smx_process_t self)
586 XBT_DEBUG("Yield process '%s'", self->name);
588 /* Go into sleep and return control to maestro */
589 SIMIX_context_suspend(self->context);
591 /* Ok, maestro returned control to us */
592 XBT_DEBUG("Control returned to me: '%s'", self->name);
594 if (self->context->iwannadie){
595 XBT_DEBUG("I wanna die!");
596 SIMIX_context_stop(self->context);
599 if (self->doexception) {
600 XBT_DEBUG("Wait, maestro left me an exception");
601 self->doexception = 0;
605 if (self->new_host) {
606 SIMIX_process_change_host(self, self->new_host);
607 self->new_host = NULL;
611 /* callback: context fetching */
612 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
614 return SIMIX_process_self()->running_ctx;
617 /* callback: termination */
618 void SIMIX_process_exception_terminate(xbt_ex_t * e)
624 smx_context_t SIMIX_process_get_context(smx_process_t p) {
628 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
633 * \brief Returns the list of processes to run.
635 XBT_INLINE xbt_dynar_t SIMIX_process_get_runnable(void)
637 return simix_global->process_to_run;