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"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
15 "Logging specific to SIMIX (process)");
17 unsigned long simix_process_maxpid = 0;
18 /* FIXME: Ugly hack!*/
22 * \brief Returns the current agent.
24 * This functions returns the currently running SIMIX process.
26 * \return The SIMIX process
28 XBT_INLINE smx_process_t SIMIX_process_self(void)
30 smx_context_t self_context = SIMIX_context_self();
32 return self_context ? SIMIX_context_get_data(self_context) : NULL;
36 * \brief Move a process to the list of processes to destroy.
38 void SIMIX_process_cleanup(smx_process_t process)
40 DEBUG1("Cleanup process %s", process->name);
41 /*xbt_swag_remove(process, simix_global->process_to_run);*/
42 xbt_swag_remove(process, simix_global->process_list);
43 xbt_swag_remove(process, process->smx_host->process_list);
44 xbt_swag_insert(process, simix_global->process_to_destroy);
50 * Should be called some time to time to free the memory allocated for processes
51 * that have finished (or killed).
53 void SIMIX_process_empty_trash(void)
55 smx_process_t process = NULL;
57 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
58 SIMIX_context_free(process->context);
60 /* Free the exception allocated at creation time */
61 if (process->running_ctx)
62 free(process->running_ctx);
63 if (process->properties)
64 xbt_dict_free(&process->properties);
73 * \brief Creates and runs the maestro process
75 void SIMIX_create_maestro_process()
77 smx_process_t maestro = NULL;
79 /* Create maestro process and intilialize it */
80 maestro = xbt_new0(s_smx_process_t, 1);
81 maestro->pid = simix_process_maxpid++;
82 maestro->name = (char *) "";
83 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
84 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
85 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
86 maestro->request.issuer = maestro;
88 simix_global->maestro_process = maestro;
93 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
94 * \return the process created
96 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
98 smx_process_t process;
100 if (simix_global->create_process_function) {
101 process = simix_global->create_process_function(args->name,
110 process = SIMIX_process_create(args->name,
118 // FIXME: to simplify this, simix_global->create_process_function could just
119 // be SIMIX_process_create() by default (and the same thing in smx_deployment.c)
125 * \brief Internal function to create a process.
127 * This function actually creates the process.
128 * It may be called when a REQ_PROCESS_CREATE request occurs,
129 * or directly for SIMIX internal purposes.
131 * \return the process created
133 smx_process_t SIMIX_process_create(const char *name,
134 xbt_main_func_t code,
136 const char *hostname,
137 int argc, char **argv,
138 xbt_dict_t properties) {
140 smx_process_t process = NULL;
141 smx_host_t host = SIMIX_host_get_by_name(hostname);
143 DEBUG2("Start process %s on host %s", name, hostname);
145 if (!SIMIX_host_get_state(host)) {
146 WARN2("Cannot launch process '%s' on failed host '%s'", name,
150 process = xbt_new0(s_smx_process_t, 1);
152 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
155 process->pid = simix_process_maxpid++;
156 process->name = xbt_strdup(name);
157 process->smx_host = host;
158 process->data = data;
160 VERB1("Create context %s", process->name);
161 process->context = SIMIX_context_new(code, argc, argv,
162 simix_global->cleanup_process_function, process);
164 process->running_ctx = xbt_new(xbt_running_ctx_t, 1);
165 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
168 process->properties = properties;
170 /* Add the process to it's host process list */
171 xbt_swag_insert(process, host->process_list);
173 DEBUG1("Start context '%s'", process->name);
175 /* Now insert it in the global process list and in the process to run list */
176 xbt_swag_insert(process, simix_global->process_list);
177 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
178 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
185 * \brief Internal function to kill a SIMIX process.
187 * This function may be called when a REQ_PROCESS_KILL request occurs,
188 * or directly for SIMIX internal purposes.
190 * \param process poor victim
192 void SIMIX_process_kill(smx_process_t process, smx_process_t killer) {
194 DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
196 process->context->iwannadie = 1;
197 process->blocked = 0;
198 process->suspended = 0;
199 /* FIXME: set doexception to 0 also? */
201 if (process->waiting_action) {
203 switch (process->waiting_action->type) {
205 case SIMIX_ACTION_EXECUTE:
206 case SIMIX_ACTION_PARALLEL_EXECUTE:
207 SIMIX_host_execution_destroy(process->waiting_action);
210 case SIMIX_ACTION_COMMUNICATE:
211 SIMIX_comm_destroy(process->waiting_action);
214 case SIMIX_ACTION_SLEEP:
215 SIMIX_process_sleep_destroy(process->waiting_action);
218 case SIMIX_ACTION_SYNCHRO:
219 SIMIX_synchro_stop_waiting(process, &process->request);
220 SIMIX_synchro_destroy(process->waiting_action);
223 case SIMIX_ACTION_IO:
229 /* If I'm killing myself then stop, otherwise schedule the process to kill. */
230 if (process == killer) {
231 SIMIX_context_stop(process->context);
234 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
239 * \brief Kills all running processes.
241 * Only maestro can kill everyone.
243 void SIMIX_process_killall(void)
245 smx_process_t p = NULL;
247 while ((p = xbt_swag_extract(simix_global->process_list)))
248 SIMIX_process_kill(p, SIMIX_process_self());
250 SIMIX_context_runall(simix_global->process_to_run);
252 SIMIX_process_empty_trash();
255 void SIMIX_process_change_host(smx_process_t process,
256 const char *source, const char *dest)
258 smx_host_t h1 = NULL;
259 smx_host_t h2 = NULL;
260 xbt_assert0((process != NULL), "Invalid parameters");
261 h1 = SIMIX_host_get_by_name(source);
262 h2 = SIMIX_host_get_by_name(dest);
263 process->smx_host = h2;
264 xbt_swag_remove(process, h1->process_list);
265 xbt_swag_insert(process, h2->process_list);
268 void SIMIX_pre_process_suspend(smx_req_t req)
270 smx_process_t process = req->process_suspend.process;
271 SIMIX_process_suspend(process, req->issuer);
273 if (process != req->issuer) {
274 SIMIX_request_answer(req);
276 /* If we are suspending ourselves, then just do not replay the request. */
279 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
281 xbt_assert0(process, "Invalid parameters");
283 process->suspended = 1;
285 /* If we are suspending another process, and it is waiting on an action,
286 suspend it's action. */
287 if (process != issuer) {
289 if (process->waiting_action) {
291 switch (process->waiting_action->type) {
293 case SIMIX_ACTION_EXECUTE:
294 case SIMIX_ACTION_PARALLEL_EXECUTE:
295 SIMIX_host_execution_suspend(process->waiting_action);
298 case SIMIX_ACTION_COMMUNICATE:
299 SIMIX_comm_suspend(process->waiting_action);
302 case SIMIX_ACTION_SLEEP:
303 SIMIX_process_sleep_suspend(process->waiting_action);
313 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
315 xbt_assert0((process != NULL), "Invalid parameters");
317 process->suspended = 0;
319 /* If we are resuming another process, resume the action it was waiting for
320 if any. Otherwise add it to the list of process to run in the next round. */
321 if (process != issuer) {
323 if (process->waiting_action) {
325 switch (process->waiting_action->type) {
327 case SIMIX_ACTION_EXECUTE:
328 case SIMIX_ACTION_PARALLEL_EXECUTE:
329 SIMIX_host_execution_resume(process->waiting_action);
332 case SIMIX_ACTION_COMMUNICATE:
333 SIMIX_comm_resume(process->waiting_action);
336 case SIMIX_ACTION_SLEEP:
337 SIMIX_process_sleep_resume(process->waiting_action);
345 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
350 int SIMIX_process_get_maxpid(void) {
351 return simix_process_maxpid;
353 int SIMIX_process_count(void)
355 return xbt_swag_size(simix_global->process_list);
358 void* SIMIX_process_self_get_data(void)
360 smx_process_t me = SIMIX_process_self();
364 return SIMIX_process_get_data(me);
367 void SIMIX_process_self_set_data(void *data)
369 SIMIX_process_set_data(SIMIX_process_self(), data);
372 void* SIMIX_process_get_data(smx_process_t process)
374 return process->data;
377 void SIMIX_process_set_data(smx_process_t process, void *data)
379 process->data = data;
382 smx_host_t SIMIX_process_get_host(smx_process_t process)
384 return process->smx_host;
387 /* needs to be public and without request because it is called
388 by exceptions and logging events */
389 const char* SIMIX_process_self_get_name(void) {
391 smx_process_t process = SIMIX_process_self();
392 if (process == NULL || process == simix_global->maestro_process)
395 return SIMIX_process_get_name(process);
398 const char* SIMIX_process_get_name(smx_process_t process)
400 return process->name;
403 int SIMIX_process_is_suspended(smx_process_t process)
405 return process->suspended;
408 int SIMIX_process_is_enabled(smx_process_t process)
410 if (process->request.call != REQ_NO_REQ && SIMIX_request_is_enabled(&process->request))
416 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
418 return process->properties;
421 void SIMIX_pre_process_sleep(smx_req_t req)
424 NOW += req->process_sleep.duration;
425 req->process_sleep.result = SIMIX_DONE;
426 SIMIX_request_answer(req);
429 smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
430 xbt_fifo_push(action->request_list, req);
431 req->issuer->waiting_action = action;
434 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
437 smx_host_t host = process->smx_host;
439 /* check if the host is active */
440 if (surf_workstation_model->extension.
441 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
442 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
446 action = xbt_new0(s_smx_action_t, 1);
447 action->type = SIMIX_ACTION_SLEEP;
448 action->request_list = xbt_fifo_new();
449 action->name = xbt_strdup("sleep");
451 action->category = NULL;
454 action->sleep.host = host;
455 action->sleep.surf_sleep =
456 surf_workstation_model->extension.workstation.sleep(host->host, duration);
458 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
459 DEBUG1("Create sleep action %p", action);
464 void SIMIX_post_process_sleep(smx_action_t action)
466 e_smx_state_t state = SIMIX_action_map_state(surf_workstation_model->action_state_get(action->sleep.surf_sleep));
469 while ((req = xbt_fifo_shift(action->request_list))) {
470 req->process_sleep.result = state;
471 req->issuer->waiting_action = NULL;
472 SIMIX_request_answer(req);
475 SIMIX_process_sleep_destroy(action);
478 void SIMIX_process_sleep_destroy(smx_action_t action)
480 DEBUG1("Destroy action %p", action);
482 xbt_free(action->name);
483 if (action->sleep.surf_sleep)
484 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
486 TRACE_smx_action_destroy(action);
488 xbt_fifo_free(action->request_list);
492 void SIMIX_process_sleep_suspend(smx_action_t action)
494 surf_workstation_model->suspend(action->sleep.surf_sleep);
497 void SIMIX_process_sleep_resume(smx_action_t action)
499 surf_workstation_model->resume(action->sleep.surf_sleep);
503 * Calling this function makes the process to yield.
504 * Only the processes can call this function, giving back the control to maestro
506 void SIMIX_process_yield(void)
508 smx_process_t self = SIMIX_process_self();
510 DEBUG1("Yield process '%s'", self->name);
512 /* Go into sleep and return control to maestro */
513 SIMIX_context_suspend(self->context);
515 /* Ok, maestro returned control to us */
516 DEBUG1("Maestro returned control to me: '%s'", self->name);
518 if (self->context->iwannadie){
519 DEBUG0("I wanna die!");
520 SIMIX_context_stop(self->context);
523 if (self->doexception) {
524 DEBUG0("Wait, maestro left me an exception");
525 self->doexception = 0;
530 /* callback: context fetching */
531 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
533 return SIMIX_process_self()->running_ctx;
536 /* callback: termination */
537 void SIMIX_process_exception_terminate(xbt_ex_t * e)
543 smx_context_t SIMIX_process_get_context(smx_process_t p) {
546 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {