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;
20 * \brief Returns the current agent.
22 * This functions returns the currently running SIMIX process.
24 * \return The SIMIX process
26 XBT_INLINE smx_process_t SIMIX_process_self(void)
28 smx_context_t self_context = SIMIX_context_self();
30 return self_context ? SIMIX_context_get_data(self_context) : NULL;
34 * \brief Move a process to the list of processes to destroy.
36 void SIMIX_process_cleanup(smx_process_t process)
38 DEBUG1("Cleanup process %s", process->name);
39 /*xbt_swag_remove(process, simix_global->process_to_run);*/
40 xbt_swag_remove(process, simix_global->process_list);
41 xbt_swag_remove(process, process->smx_host->process_list);
42 xbt_swag_insert(process, simix_global->process_to_destroy);
48 * Should be called some time to time to free the memory allocated for processes
49 * that have finished (or killed).
51 void SIMIX_process_empty_trash(void)
53 smx_process_t process = NULL;
55 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
56 SIMIX_context_free(process->context);
58 /* Free the exception allocated at creation time */
59 if (process->running_ctx)
60 free(process->running_ctx);
61 if (process->properties)
62 xbt_dict_free(&process->properties);
71 * \brief Creates and runs the maestro process
73 void SIMIX_create_maestro_process()
75 smx_process_t maestro = NULL;
77 /* Create maestro process and intilialize it */
78 maestro = xbt_new0(s_smx_process_t, 1);
79 maestro->pid = simix_process_maxpid++;
80 maestro->name = (char *) "";
81 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
82 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
83 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
84 maestro->request.issuer = maestro;
86 simix_global->maestro_process = maestro;
91 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
92 * \return the process created
94 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
96 smx_process_t process;
98 if (simix_global->create_process_function) {
99 process = simix_global->create_process_function(args->name,
108 process = SIMIX_process_create(args->name,
116 // FIXME: to simplify this, simix_global->create_process_function could just
117 // be SIMIX_process_create() by default (and the same thing in smx_deployment.c)
123 * \brief Internal function to create a process.
125 * This function actually creates the process.
126 * It may be called when a REQ_PROCESS_CREATE request occurs,
127 * or directly for SIMIX internal purposes.
129 * \return the process created
131 smx_process_t SIMIX_process_create(const char *name,
132 xbt_main_func_t code,
134 const char *hostname,
135 int argc, char **argv,
136 xbt_dict_t properties) {
138 smx_process_t process = NULL;
139 smx_host_t host = SIMIX_host_get_by_name(hostname);
141 DEBUG2("Start process %s on host %s", name, hostname);
143 if (!SIMIX_host_get_state(host)) {
144 WARN2("Cannot launch process '%s' on failed host '%s'", name,
148 process = xbt_new0(s_smx_process_t, 1);
150 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
153 process->pid = simix_process_maxpid++;
154 process->name = xbt_strdup(name);
155 process->smx_host = host;
156 process->data = data;
158 VERB1("Create context %s", process->name);
159 process->context = SIMIX_context_new(code, argc, argv,
160 simix_global->cleanup_process_function, process);
162 process->running_ctx = xbt_new(xbt_running_ctx_t, 1);
163 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
166 process->properties = properties;
168 /* Add the process to it's host process list */
169 xbt_swag_insert(process, host->process_list);
171 DEBUG1("Start context '%s'", process->name);
173 /* Now insert it in the global process list and in the process to run list */
174 xbt_swag_insert(process, simix_global->process_list);
175 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
176 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
183 * \brief Internal function to kill a SIMIX process.
185 * This function may be called when a REQ_PROCESS_KILL request occurs,
186 * or directly for SIMIX internal purposes.
188 * \param process poor victim
190 void SIMIX_process_kill(smx_process_t process, smx_process_t killer) {
192 DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
194 process->context->iwannadie = 1;
195 process->blocked = 0;
196 process->suspended = 0;
197 /* FIXME: set doexception to 0 also? */
199 if (process->waiting_action) {
201 switch (process->waiting_action->type) {
203 case SIMIX_ACTION_EXECUTE:
204 case SIMIX_ACTION_PARALLEL_EXECUTE:
205 SIMIX_host_execution_destroy(process->waiting_action);
208 case SIMIX_ACTION_COMMUNICATE:
209 SIMIX_comm_destroy(process->waiting_action);
212 case SIMIX_ACTION_SLEEP:
213 SIMIX_process_sleep_destroy(process->waiting_action);
216 case SIMIX_ACTION_SYNCHRO:
217 SIMIX_synchro_stop_waiting(process, &process->request);
218 SIMIX_synchro_destroy(process->waiting_action);
221 case SIMIX_ACTION_IO:
227 /* If I'm killing myself then stop, otherwise schedule the process to kill. */
228 if (process == killer) {
229 SIMIX_context_stop(process->context);
232 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
237 * \brief Kills all running processes.
239 * Only maestro can kill everyone.
241 void SIMIX_process_killall(void)
243 smx_process_t p = NULL;
245 while ((p = xbt_swag_extract(simix_global->process_list)))
246 SIMIX_process_kill(p, SIMIX_process_self());
248 SIMIX_context_runall(simix_global->process_to_run);
249 SIMIX_context_runall(simix_global->process_to_run);
250 SIMIX_process_empty_trash();
253 void SIMIX_process_change_host(smx_process_t process,
254 const char *source, const char *dest)
256 smx_host_t h1 = NULL;
257 smx_host_t h2 = NULL;
258 xbt_assert0((process != NULL), "Invalid parameters");
259 h1 = SIMIX_host_get_by_name(source);
260 h2 = SIMIX_host_get_by_name(dest);
261 process->smx_host = h2;
262 xbt_swag_remove(process, h1->process_list);
263 xbt_swag_insert(process, h2->process_list);
266 void SIMIX_pre_process_suspend(smx_req_t req)
268 smx_process_t process = req->process_suspend.process;
269 SIMIX_process_suspend(process, req->issuer);
271 if (process != req->issuer) {
272 SIMIX_request_answer(req);
274 /* If we are suspending ourselves, then just do not replay the request. */
277 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
279 xbt_assert0(process, "Invalid parameters");
281 process->suspended = 1;
283 /* If we are suspending another process, and it is waiting on an action,
284 suspend it's action. */
285 if (process != issuer) {
287 if (process->waiting_action) {
289 switch (process->waiting_action->type) {
291 case SIMIX_ACTION_EXECUTE:
292 case SIMIX_ACTION_PARALLEL_EXECUTE:
293 SIMIX_host_execution_suspend(process->waiting_action);
296 case SIMIX_ACTION_COMMUNICATE:
297 SIMIX_comm_suspend(process->waiting_action);
300 case SIMIX_ACTION_SLEEP:
301 SIMIX_process_sleep_suspend(process->waiting_action);
311 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
313 xbt_assert0((process != NULL), "Invalid parameters");
315 process->suspended = 0;
317 /* If we are resuming another process, resume the action it was waiting for
318 if any. Otherwise add it to the list of process to run in the next round. */
319 if (process != issuer) {
321 if (process->waiting_action) {
323 switch (process->waiting_action->type) {
325 case SIMIX_ACTION_EXECUTE:
326 case SIMIX_ACTION_PARALLEL_EXECUTE:
327 SIMIX_host_execution_resume(process->waiting_action);
330 case SIMIX_ACTION_COMMUNICATE:
331 SIMIX_comm_resume(process->waiting_action);
334 case SIMIX_ACTION_SLEEP:
335 SIMIX_process_sleep_resume(process->waiting_action);
343 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
348 int SIMIX_process_get_maxpid(void) {
349 return simix_process_maxpid;
351 int SIMIX_process_count(void)
353 return xbt_swag_size(simix_global->process_list);
356 void* SIMIX_process_self_get_data(void)
358 smx_process_t me = SIMIX_process_self();
362 return SIMIX_process_get_data(me);
365 void SIMIX_process_self_set_data(void *data)
367 SIMIX_process_set_data(SIMIX_process_self(), data);
370 void* SIMIX_process_get_data(smx_process_t process)
372 return process->data;
375 void SIMIX_process_set_data(smx_process_t process, void *data)
377 process->data = data;
380 smx_host_t SIMIX_process_get_host(smx_process_t process)
382 return process->smx_host;
385 /* needs to be public and without request because it is called
386 by exceptions and logging events */
387 const char* SIMIX_process_self_get_name(void) {
389 smx_process_t process = SIMIX_process_self();
390 if (process == NULL || process == simix_global->maestro_process)
393 return SIMIX_process_get_name(process);
396 const char* SIMIX_process_get_name(smx_process_t process)
398 return process->name;
401 int SIMIX_process_is_suspended(smx_process_t process)
403 return process->suspended;
406 int SIMIX_process_is_enabled(smx_process_t process)
408 if (process->request.call != REQ_NO_REQ && SIMIX_request_is_enabled(&process->request))
414 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
416 return process->properties;
419 void SIMIX_pre_process_sleep(smx_req_t req)
422 req->process_sleep.result = SIMIX_DONE;
423 SIMIX_request_answer(req);
425 smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
426 xbt_fifo_push(action->request_list, req);
427 req->issuer->waiting_action = action;
430 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
433 smx_host_t host = process->smx_host;
435 /* check if the host is active */
436 if (surf_workstation_model->extension.
437 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
438 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
442 action = xbt_new0(s_smx_action_t, 1);
443 action->type = SIMIX_ACTION_SLEEP;
444 action->request_list = xbt_fifo_new();
445 action->name = xbt_strdup("sleep");
447 action->category = NULL;
450 action->sleep.host = host;
451 action->sleep.surf_sleep =
452 surf_workstation_model->extension.workstation.sleep(host->host, duration);
454 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
455 DEBUG1("Create sleep action %p", action);
460 void SIMIX_post_process_sleep(smx_action_t action)
462 e_smx_state_t state = SIMIX_action_map_state(surf_workstation_model->action_state_get(action->sleep.surf_sleep));
465 while ((req = xbt_fifo_shift(action->request_list))) {
466 req->process_sleep.result = state;
467 req->issuer->waiting_action = NULL;
468 SIMIX_request_answer(req);
471 SIMIX_process_sleep_destroy(action);
474 void SIMIX_process_sleep_destroy(smx_action_t action)
476 DEBUG1("Destroy action %p", action);
478 xbt_free(action->name);
479 if (action->sleep.surf_sleep)
480 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
482 TRACE_smx_action_destroy(action);
484 xbt_fifo_free(action->request_list);
488 void SIMIX_process_sleep_suspend(smx_action_t action)
490 surf_workstation_model->suspend(action->sleep.surf_sleep);
493 void SIMIX_process_sleep_resume(smx_action_t action)
495 surf_workstation_model->resume(action->sleep.surf_sleep);
499 * Calling this function makes the process to yield.
500 * Only the processes can call this function, giving back the control to maestro
502 void SIMIX_process_yield(void)
504 smx_process_t self = SIMIX_process_self();
506 DEBUG1("Yield process '%s'", self->name);
508 /* Go into sleep and return control to maestro */
509 SIMIX_context_suspend(self->context);
511 /* Ok, maestro returned control to us */
512 DEBUG1("Maestro returned control to me: '%s'", self->name);
514 if (self->context->iwannadie)
515 SIMIX_context_stop(self->context);
517 if (self->doexception) {
518 DEBUG0("Wait, maestro left me an exception");
519 self->doexception = 0;
524 /* callback: context fetching */
525 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
527 return SIMIX_process_self()->running_ctx;
530 /* callback: termination */
531 void SIMIX_process_exception_terminate(xbt_ex_t * e)
537 smx_context_t SIMIX_process_get_context(smx_process_t p) {
540 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {