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 XBT_DEBUG("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;
97 simix_global->create_process_function(
111 * \brief Internal function to create a process.
113 * This function actually creates the process.
114 * It may be called when a REQ_PROCESS_CREATE request occurs,
115 * or directly for SIMIX internal purposes.
117 * \return the process created
119 void SIMIX_process_create(smx_process_t *process,
121 xbt_main_func_t code,
123 const char *hostname,
124 int argc, char **argv,
125 xbt_dict_t properties) {
128 smx_host_t host = SIMIX_host_get_by_name(hostname);
130 XBT_DEBUG("Start process %s on host %s", name, hostname);
132 if (!SIMIX_host_get_state(host)) {
133 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
137 *process = xbt_new0(s_smx_process_t, 1);
139 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
142 (*process)->pid = simix_process_maxpid++;
143 (*process)->name = xbt_strdup(name);
144 (*process)->smx_host = host;
145 (*process)->data = data;
147 XBT_VERB("Create context %s", (*process)->name);
148 (*process)->context = SIMIX_context_new(code, argc, argv,
149 simix_global->cleanup_process_function, *process);
151 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
152 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
155 (*process)->properties = properties;
157 /* Add the process to it's host process list */
158 xbt_swag_insert(*process, host->process_list);
160 XBT_DEBUG("Start context '%s'", (*process)->name);
162 /* Now insert it in the global process list and in the process to run list */
163 xbt_swag_insert(*process, simix_global->process_list);
164 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
165 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
170 * \brief Executes the processes from simix_global->process_to_run.
172 * The processes of simix_global->process_to_run are run (in parallel if
173 * possible). On exit, simix_global->process_to_run is empty, and
174 * simix_global->process_that_ran contains the list of processes that just ran.
175 * The two lists are swapped so, be careful when using them before and after a
176 * call to this function.
178 void SIMIX_process_runall(void)
180 SIMIX_context_runall(simix_global->process_to_run);
181 xbt_dynar_t tmp = simix_global->process_that_ran;
182 simix_global->process_that_ran = simix_global->process_to_run;
183 simix_global->process_to_run = tmp;
184 xbt_dynar_reset(simix_global->process_to_run);
188 * \brief Internal function to kill a SIMIX process.
190 * This function may be called when a REQ_PROCESS_KILL request occurs,
191 * or directly for SIMIX internal purposes.
193 * \param process poor victim
195 void SIMIX_process_kill(smx_process_t process) {
197 XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
199 process->context->iwannadie = 1;
200 process->blocked = 0;
201 process->suspended = 0;
202 /* FIXME: set doexception to 0 also? */
204 if (process->waiting_action) {
206 switch (process->waiting_action->type) {
208 case SIMIX_ACTION_EXECUTE:
209 case SIMIX_ACTION_PARALLEL_EXECUTE:
210 SIMIX_host_execution_destroy(process->waiting_action);
213 case SIMIX_ACTION_COMMUNICATE:
214 SIMIX_comm_destroy(process->waiting_action);
217 case SIMIX_ACTION_SLEEP:
218 SIMIX_process_sleep_destroy(process->waiting_action);
221 case SIMIX_ACTION_SYNCHRO:
222 SIMIX_synchro_stop_waiting(process, &process->request);
223 SIMIX_synchro_destroy(process->waiting_action);
226 case SIMIX_ACTION_IO:
232 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
236 * \brief Kills all running processes.
237 * \param issuer this one will not be killed
239 void SIMIX_process_killall(smx_process_t issuer)
241 smx_process_t p = NULL;
243 while ((p = xbt_swag_extract(simix_global->process_list))) {
245 SIMIX_process_kill(p);
249 SIMIX_context_runall(simix_global->process_to_run);
251 SIMIX_process_empty_trash();
254 void SIMIX_process_change_host(smx_process_t process,
257 xbt_assert((process != NULL), "Invalid parameters");
258 xbt_swag_remove(process, process->smx_host->process_list);
259 process->smx_host = dest;
260 xbt_swag_insert(process, dest->process_list);
263 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
265 process->new_host = dest;
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 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_assert((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;
352 int SIMIX_process_count(void)
354 return xbt_swag_size(simix_global->process_list);
357 void* SIMIX_process_self_get_data(void)
359 smx_process_t me = SIMIX_process_self();
363 return SIMIX_process_get_data(me);
366 void SIMIX_process_self_set_data(void *data)
368 SIMIX_process_set_data(SIMIX_process_self(), data);
371 void* SIMIX_process_get_data(smx_process_t process)
373 return process->data;
376 void SIMIX_process_set_data(smx_process_t process, void *data)
378 process->data = data;
381 smx_host_t SIMIX_process_get_host(smx_process_t process)
383 return process->smx_host;
386 /* needs to be public and without request because it is called
387 by exceptions and logging events */
388 const char* SIMIX_process_self_get_name(void) {
390 smx_process_t process = SIMIX_process_self();
391 if (process == NULL || process == simix_global->maestro_process)
394 return SIMIX_process_get_name(process);
397 const char* SIMIX_process_get_name(smx_process_t process)
399 return process->name;
402 smx_process_t SIMIX_process_get_by_name(const char* name)
406 xbt_swag_foreach(proc, simix_global->process_list)
408 if(!strcmp(name, proc->name))
414 int SIMIX_process_is_suspended(smx_process_t process)
416 return process->suspended;
419 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
421 return process->properties;
424 void SIMIX_pre_process_sleep(smx_req_t req)
427 MC_process_clock_add(req->issuer, req->process_sleep.duration);
428 req->process_sleep.result = SIMIX_DONE;
429 SIMIX_request_answer(req);
432 smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
433 xbt_fifo_push(action->request_list, req);
434 req->issuer->waiting_action = action;
437 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
440 smx_host_t host = process->smx_host;
442 /* check if the host is active */
443 if (surf_workstation_model->extension.
444 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
445 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
449 action = xbt_mallocator_get(simix_global->action_mallocator);
450 action->type = SIMIX_ACTION_SLEEP;
453 action->category = NULL;
456 action->sleep.host = host;
457 action->sleep.surf_sleep =
458 surf_workstation_model->extension.workstation.sleep(host->host, duration);
460 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
461 XBT_DEBUG("Create sleep action %p", action);
466 void SIMIX_post_process_sleep(smx_action_t action)
471 while ((req = xbt_fifo_shift(action->request_list))) {
473 switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
474 case SURF_ACTION_FAILED:
475 state = SIMIX_SRC_HOST_FAILURE;
478 case SURF_ACTION_DONE:
486 req->process_sleep.result = state;
487 req->issuer->waiting_action = NULL;
488 SIMIX_request_answer(req);
490 SIMIX_process_sleep_destroy(action);
493 void SIMIX_process_sleep_destroy(smx_action_t action)
495 XBT_DEBUG("Destroy action %p", action);
496 if (action->sleep.surf_sleep)
497 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
499 TRACE_smx_action_destroy(action);
501 xbt_mallocator_release(simix_global->action_mallocator, action);
504 void SIMIX_process_sleep_suspend(smx_action_t action)
506 surf_workstation_model->suspend(action->sleep.surf_sleep);
509 void SIMIX_process_sleep_resume(smx_action_t action)
511 surf_workstation_model->resume(action->sleep.surf_sleep);
515 * Calling this function makes the process to yield.
516 * Only the processes can call this function, giving back the control to maestro
518 void SIMIX_process_yield(void)
520 smx_process_t self = SIMIX_process_self();
522 XBT_DEBUG("Yield process '%s'", self->name);
524 /* Go into sleep and return control to maestro */
525 SIMIX_context_suspend(self->context);
527 /* Ok, maestro returned control to us */
528 XBT_DEBUG("Maestro returned control to me: '%s'", self->name);
530 if (self->context->iwannadie){
531 XBT_DEBUG("I wanna die!");
532 SIMIX_context_stop(self->context);
535 if (self->doexception) {
536 XBT_DEBUG("Wait, maestro left me an exception");
537 self->doexception = 0;
541 if (self->new_host) {
542 SIMIX_process_change_host(self, self->new_host);
543 self->new_host = NULL;
547 /* callback: context fetching */
548 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
550 return SIMIX_process_self()->running_ctx;
553 /* callback: termination */
554 void SIMIX_process_exception_terminate(xbt_ex_t * e)
560 smx_context_t SIMIX_process_get_context(smx_process_t p) {
564 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {