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"
11 #include "msg/mailbox.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14 "Logging specific to SIMIX (process)");
16 static unsigned long simix_process_count = 0;
19 * \brief Move a process to the list of processes to destroy.
21 void SIMIX_process_cleanup(smx_process_t process)
23 DEBUG1("Cleanup process %s", process->name);
24 xbt_swag_remove(process, simix_global->process_to_run);
25 xbt_swag_remove(process, simix_global->process_list);
26 xbt_swag_remove(process, process->smx_host->process_list);
27 xbt_swag_insert(process, simix_global->process_to_destroy);
33 * Should be called some time to time to free the memory allocated for processes
34 * that have finished (or killed).
36 void SIMIX_process_empty_trash(void)
38 smx_process_t process = NULL;
40 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
41 SIMIX_context_free(process->context);
43 /* Free the exception allocated at creation time */
44 if (process->running_ctx)
45 free(process->running_ctx);
46 if (process->properties)
47 xbt_dict_free(&process->properties);
56 * \brief Creates and runs the maestro process
58 void SIMIX_create_maestro_process()
60 smx_process_t maestro = NULL;
62 /* Create maestro process and intilialize it */
63 maestro = xbt_new0(s_smx_process_t, 1);
64 maestro->pid = simix_process_count++;
65 maestro->name = (char *) "";
66 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
67 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
68 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
70 simix_global->maestro_process = maestro;
71 simix_global->current_process = maestro;
77 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
78 * \return the process created
80 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
82 smx_process_t process;
84 if (simix_global->create_process_function) {
85 process = simix_global->create_process_function(args->name,
94 process = SIMIX_process_create(args->name,
102 // FIXME: to simplify this, simix_global->create_process_function could just
103 // be SIMIX_process_create() by default (and the same thing in smx_deployment.c)
109 * \brief Internal function to create a process.
111 * This function actually creates the process.
112 * It may be called when a REQ_PROCESS_CREATE request occurs,
113 * or directly for SIMIX internal purposes.
115 * \return the process created
117 smx_process_t SIMIX_process_create(const char *name,
118 xbt_main_func_t code,
120 const char *hostname,
121 int argc, char **argv,
122 xbt_dict_t properties) {
124 smx_process_t process = NULL;
125 smx_host_t host = SIMIX_host_get_by_name(hostname);
127 DEBUG2("Start process %s on host %s", name, hostname);
129 if (!SIMIX_host_get_state(host)) {
130 WARN2("Cannot launch process '%s' on failed host '%s'", name,
134 process = xbt_new0(s_smx_process_t, 1);
136 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
139 process->pid = simix_process_count++;
140 process->name = xbt_strdup(name);
141 process->smx_host = host;
142 process->iwannadie = 0;
143 process->data = data;
145 VERB1("Create context %s", process->name);
146 process->context = SIMIX_context_new(code, argc, argv,
147 simix_global->cleanup_process_function, process);
149 process->running_ctx = xbt_new(xbt_running_ctx_t, 1);
150 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
153 process->properties = properties;
155 /* Add the process to it's host process list */
156 xbt_swag_insert(process, host->process_list);
158 DEBUG1("Start context '%s'", process->name);
160 /* Now insert it in the global process list and in the process to run list */
161 xbt_swag_insert(process, simix_global->process_list);
162 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
163 xbt_swag_insert(process, simix_global->process_to_run);
170 * \brief Internal function to kill a SIMIX process.
172 * This function may be called when a REQ_PROCESS_KILL request occurs,
173 * or directly for SIMIX internal purposes.
175 * \param process poor victim
177 void SIMIX_process_kill(smx_process_t process, smx_process_t killer) {
179 DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
181 process->iwannadie = 1;
182 process->blocked = 0;
183 process->suspended = 0;
184 /* FIXME: set doexception to 0 also? */
186 if (process->waiting_action) {
188 switch (process->waiting_action->type) {
190 case SIMIX_ACTION_EXECUTE:
191 case SIMIX_ACTION_PARALLEL_EXECUTE:
192 SIMIX_host_execution_destroy(process->waiting_action);
195 case SIMIX_ACTION_COMMUNICATE:
196 SIMIX_comm_destroy(process->waiting_action);
199 case SIMIX_ACTION_SLEEP:
200 SIMIX_process_sleep_destroy(process->waiting_action);
203 case SIMIX_ACTION_SYNCHRO:
204 SIMIX_synchro_stop_waiting(process, process->request);
205 SIMIX_synchro_destroy(process->waiting_action);
208 case SIMIX_ACTION_IO:
214 /* If I'm killing myself then stop, otherwise schedule the process to kill. */
215 if (process == killer) {
216 SIMIX_context_stop(process->context);
219 xbt_swag_insert(process, simix_global->process_to_run);
224 * \brief Kills all running processes.
226 * Only maestro can kill everyone.
228 void SIMIX_process_killall(void)
230 smx_process_t p = NULL;
232 while ((p = xbt_swag_extract(simix_global->process_list)))
233 SIMIX_process_kill(p, SIMIX_process_self());
235 SIMIX_process_empty_trash();
238 void SIMIX_process_change_host(smx_process_t process,
239 const char *source, const char *dest)
241 smx_host_t h1 = NULL;
242 smx_host_t h2 = NULL;
243 xbt_assert0((process != NULL), "Invalid parameters");
244 h1 = SIMIX_host_get_by_name(source);
245 h2 = SIMIX_host_get_by_name(dest);
246 process->smx_host = h2;
247 xbt_swag_remove(process, h1->process_list);
248 xbt_swag_insert(process, h2->process_list);
251 void SIMIX_pre_process_suspend(smx_req_t req)
253 smx_process_t process = req->process_suspend.process;
254 SIMIX_process_suspend(process, req->issuer);
256 if (process != req->issuer) {
257 SIMIX_request_answer(req);
259 /* If we are suspending ourselves, then just do not replay the request. */
262 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
264 xbt_assert0(process, "Invalid parameters");
266 process->suspended = 1;
268 /* If we are suspending another process, and it is waiting on an action,
269 suspend it's action. */
270 if (process != issuer) {
272 if (process->waiting_action) {
274 switch (process->waiting_action->type) {
276 case SIMIX_ACTION_EXECUTE:
277 case SIMIX_ACTION_PARALLEL_EXECUTE:
278 SIMIX_host_execution_suspend(process->waiting_action);
281 case SIMIX_ACTION_COMMUNICATE:
282 SIMIX_comm_suspend(process->waiting_action);
285 case SIMIX_ACTION_SLEEP:
286 SIMIX_process_sleep_suspend(process->waiting_action);
296 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
298 xbt_assert0((process != NULL), "Invalid parameters");
300 process->suspended = 0;
302 /* If we are resuming another process, resume the action it was waiting for
303 if any. Otherwise add it to the list of process to run in the next round. */
304 if (process != issuer) {
306 if (process->waiting_action) {
308 switch(process->waiting_action->type) {
310 case SIMIX_ACTION_EXECUTE:
311 case SIMIX_ACTION_PARALLEL_EXECUTE:
312 SIMIX_host_execution_resume(process->waiting_action);
315 case SIMIX_ACTION_COMMUNICATE:
316 SIMIX_comm_resume(process->waiting_action);
319 case SIMIX_ACTION_SLEEP:
320 SIMIX_process_sleep_resume(process->waiting_action);
327 xbt_swag_insert(process, simix_global->process_to_run);
332 int SIMIX_process_count(void)
334 return xbt_swag_size(simix_global->process_list);
337 void* SIMIX_process_self_get_data(void)
339 smx_process_t me = SIMIX_process_self();
343 return SIMIX_process_get_data(me);
346 void SIMIX_process_self_set_data(void *data)
348 SIMIX_process_set_data(SIMIX_process_self(), data);
351 void* SIMIX_process_get_data(smx_process_t process)
353 return process->data;
356 void SIMIX_process_set_data(smx_process_t process, void *data)
358 process->data = data;
361 smx_host_t SIMIX_process_get_host(smx_process_t process)
363 return process->smx_host;
366 /* needs to be public and without request because it is called
367 by exceptions and logging events */
368 const char* SIMIX_process_self_get_name(void) {
370 smx_process_t process = SIMIX_process_self();
371 if (process == NULL || process == simix_global->maestro_process)
374 return SIMIX_process_get_name(process);
377 const char* SIMIX_process_get_name(smx_process_t process)
379 return process->name;
382 int SIMIX_process_is_suspended(smx_process_t process)
384 return process->suspended;
387 int SIMIX_process_is_enabled(smx_process_t process)
389 if(process->request && SIMIX_request_isEnabled(process->request))
395 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
397 return process->properties;
400 void SIMIX_pre_process_sleep(smx_req_t req)
403 if(_surf_do_model_check){
404 req->process_sleep.result = SIMIX_DONE;
405 SIMIX_request_answer(req);
408 smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
409 xbt_fifo_push(action->request_list, req);
410 req->issuer->waiting_action = action;
413 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
416 smx_host_t host = process->smx_host;
418 /* check if the host is active */
419 if (surf_workstation_model->extension.
420 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
421 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
425 action = xbt_new0(s_smx_action_t, 1);
426 action->type = SIMIX_ACTION_SLEEP;
427 action->request_list = xbt_fifo_new();
428 action->name = xbt_strdup("sleep");
430 action->category = NULL;
433 action->sleep.host = host;
434 action->sleep.surf_sleep =
435 surf_workstation_model->extension.workstation.sleep(host->host, duration);
437 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
438 DEBUG1("Create sleep action %p", action);
443 void SIMIX_post_process_sleep(smx_action_t action)
445 e_smx_state_t state = SIMIX_action_map_state(surf_workstation_model->action_state_get(action->sleep.surf_sleep));
448 while ((req = xbt_fifo_shift(action->request_list))) {
449 req->process_sleep.result = state;
450 req->issuer->waiting_action = NULL;
451 SIMIX_request_answer(req);
454 SIMIX_process_sleep_destroy(action);
457 void SIMIX_process_sleep_destroy(smx_action_t action)
459 DEBUG1("Destroy action %p", action);
461 xbt_free(action->name);
462 if (action->sleep.surf_sleep)
463 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
465 TRACE_smx_action_destroy(act);
467 xbt_fifo_free(action->request_list);
471 void SIMIX_process_sleep_suspend(smx_action_t action)
473 surf_workstation_model->suspend(action->sleep.surf_sleep);
476 void SIMIX_process_sleep_resume(smx_action_t action)
478 surf_workstation_model->resume(action->sleep.surf_sleep);
482 * \brief Returns the current agent.
484 * This functions returns the currently running SIMIX process.
486 * \return The SIMIX process
488 XBT_INLINE smx_process_t SIMIX_process_self(void)
491 return SIMIX_context_self();
496 * Calling this function makes the process to yield.
497 * Only the processes can call this function, giving back the control to maestro
499 void SIMIX_process_yield(void)
501 smx_process_t self = SIMIX_process_self();
503 DEBUG1("Yield process '%s'", self->name);
505 /* Go into sleep and return control to maestro */
506 SIMIX_context_suspend(self->context);
508 /* Ok, maestro returned control to us */
509 DEBUG1("Maestro returned control to me: '%s'", self->name);
512 SIMIX_context_stop(self->context);
514 if (self->doexception) {
515 DEBUG0("Wait, maestro left me an exception");
516 self->doexception = 0;
521 /* callback: context fetching */
522 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
524 return simix_global->current_process->running_ctx;
527 /* callback: termination */
528 void SIMIX_process_exception_terminate(xbt_ex_t * e)