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 (%p), waiting action %p",
39 process->name, process, process->waiting_action);
41 /* cancel non-blocking communications */
43 while ((action = xbt_fifo_pop(process->comms))) {
45 /* make sure no one will finish the comm after this process is destroyed */
46 SIMIX_comm_cancel(action);
48 if (action->comm.src_proc == process) {
49 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d",
50 action, action->comm.detached, action->state);
51 action->comm.src_proc = NULL;
53 if (action->comm.detached) {
54 /* the receiver was supposed to destroy the comm after completion,
55 * but the comm will actually never finish */
56 action->comm.refcount++;
59 else if (action->comm.dst_proc == process){
60 XBT_DEBUG("Found an unfinished recv comm %p, state %d", action, action->state);
61 action->comm.dst_proc = NULL;
64 XBT_DEBUG("Strange, I'm not in comm %p, state = %d, src = %p, dst = %p", action,
65 action->state, action->comm.src_proc, action->comm.dst_proc);
69 /* FIXME uncommenting this instruction crashes complex simulations
70 SIMIX_comm_destroy(action); */
73 /*xbt_swag_remove(process, simix_global->process_to_run);*/
74 xbt_swag_remove(process, simix_global->process_list);
75 xbt_swag_remove(process, process->smx_host->process_list);
76 xbt_swag_insert(process, simix_global->process_to_destroy);
82 * Should be called some time to time to free the memory allocated for processes
83 * that have finished (or killed).
85 void SIMIX_process_empty_trash(void)
87 smx_process_t process = NULL;
89 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
90 SIMIX_context_free(process->context);
92 /* Free the exception allocated at creation time */
93 if (process->running_ctx)
94 free(process->running_ctx);
95 if (process->properties)
96 xbt_dict_free(&process->properties);
98 xbt_fifo_free(process->comms);
101 process->name = NULL;
107 * \brief Creates and runs the maestro process
109 void SIMIX_create_maestro_process()
111 smx_process_t maestro = NULL;
113 /* Create maestro process and intilialize it */
114 maestro = xbt_new0(s_smx_process_t, 1);
115 maestro->pid = simix_process_maxpid++;
116 maestro->name = (char *) "";
117 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
118 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
119 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
120 maestro->request.issuer = maestro;
122 simix_global->maestro_process = maestro;
127 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
128 * \return the process created
130 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
132 smx_process_t process;
133 simix_global->create_process_function(
147 * \brief Internal function to create a process.
149 * This function actually creates the process.
150 * It may be called when a REQ_PROCESS_CREATE request occurs,
151 * or directly for SIMIX internal purposes.
153 * \return the process created
155 void SIMIX_process_create(smx_process_t *process,
157 xbt_main_func_t code,
159 const char *hostname,
160 int argc, char **argv,
161 xbt_dict_t properties) {
164 smx_host_t host = SIMIX_host_get_by_name(hostname);
166 XBT_DEBUG("Start process %s on host %s", name, hostname);
168 if (!SIMIX_host_get_state(host)) {
169 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
173 *process = xbt_new0(s_smx_process_t, 1);
175 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
178 (*process)->pid = simix_process_maxpid++;
179 (*process)->name = xbt_strdup(name);
180 (*process)->smx_host = host;
181 (*process)->data = data;
182 (*process)->comms = xbt_fifo_new();
184 XBT_VERB("Create context %s", (*process)->name);
185 (*process)->context = SIMIX_context_new(code, argc, argv,
186 simix_global->cleanup_process_function, *process);
188 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
189 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
192 (*process)->properties = properties;
194 /* Add the process to it's host process list */
195 xbt_swag_insert(*process, host->process_list);
197 XBT_DEBUG("Start context '%s'", (*process)->name);
199 /* Now insert it in the global process list and in the process to run list */
200 xbt_swag_insert(*process, simix_global->process_list);
201 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
202 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
207 * \brief Executes the processes from simix_global->process_to_run.
209 * The processes of simix_global->process_to_run are run (in parallel if
210 * possible). On exit, simix_global->process_to_run is empty, and
211 * simix_global->process_that_ran contains the list of processes that just ran.
212 * The two lists are swapped so, be careful when using them before and after a
213 * call to this function.
215 void SIMIX_process_runall(void)
217 SIMIX_context_runall();
219 xbt_dynar_t tmp = simix_global->process_that_ran;
220 simix_global->process_that_ran = simix_global->process_to_run;
221 simix_global->process_to_run = tmp;
222 xbt_dynar_reset(simix_global->process_to_run);
226 * \brief Internal function to kill a SIMIX process.
228 * This function may be called when a REQ_PROCESS_KILL request occurs,
229 * or directly for SIMIX internal purposes.
231 * \param process poor victim
233 void SIMIX_process_kill(smx_process_t process) {
235 XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
237 process->context->iwannadie = 1;
238 process->blocked = 0;
239 process->suspended = 0;
240 /* FIXME: set doexception to 0 also? */
242 /* destroy the blocking action if any */
243 if (process->waiting_action) {
245 switch (process->waiting_action->type) {
247 case SIMIX_ACTION_EXECUTE:
248 case SIMIX_ACTION_PARALLEL_EXECUTE:
249 SIMIX_host_execution_destroy(process->waiting_action);
252 case SIMIX_ACTION_COMMUNICATE:
253 SIMIX_comm_destroy(process->waiting_action);
256 case SIMIX_ACTION_SLEEP:
257 SIMIX_process_sleep_destroy(process->waiting_action);
260 case SIMIX_ACTION_SYNCHRO:
261 SIMIX_synchro_stop_waiting(process, &process->request);
262 SIMIX_synchro_destroy(process->waiting_action);
265 case SIMIX_ACTION_IO:
271 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
275 * \brief Kills all running processes.
276 * \param issuer this one will not be killed
278 void SIMIX_process_killall(smx_process_t issuer)
280 smx_process_t p = NULL;
282 while ((p = xbt_swag_extract(simix_global->process_list))) {
284 SIMIX_process_kill(p);
288 SIMIX_context_runall(simix_global->process_to_run);
290 SIMIX_process_empty_trash();
293 void SIMIX_process_change_host(smx_process_t process,
296 xbt_assert((process != NULL), "Invalid parameters");
297 xbt_swag_remove(process, process->smx_host->process_list);
298 process->smx_host = dest;
299 xbt_swag_insert(process, dest->process_list);
302 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
304 process->new_host = dest;
307 void SIMIX_pre_process_suspend(smx_req_t req)
309 smx_process_t process = req->process_suspend.process;
310 SIMIX_process_suspend(process, req->issuer);
312 if (process != req->issuer) {
313 SIMIX_request_answer(req);
315 /* If we are suspending ourselves, then just do not replay the request. */
318 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
320 xbt_assert((process != NULL), "Invalid parameters");
322 if (process->suspended) {
323 XBT_DEBUG("Process '%s' is already suspended", process->name);
327 process->suspended = 1;
329 /* If we are suspending another process, and it is waiting on an action,
330 suspend its action. */
331 if (process != issuer) {
333 if (process->waiting_action) {
335 switch (process->waiting_action->type) {
337 case SIMIX_ACTION_EXECUTE:
338 case SIMIX_ACTION_PARALLEL_EXECUTE:
339 SIMIX_host_execution_suspend(process->waiting_action);
342 case SIMIX_ACTION_COMMUNICATE:
343 SIMIX_comm_suspend(process->waiting_action);
346 case SIMIX_ACTION_SLEEP:
347 SIMIX_process_sleep_suspend(process->waiting_action);
351 xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
352 process->waiting_action->type);
358 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
360 xbt_assert((process != NULL), "Invalid parameters");
362 if (!process->suspended) {
363 XBT_DEBUG("Process '%s' is not suspended", process->name);
367 process->suspended = 0;
369 /* If we are resuming another process, resume the action it was waiting for
370 if any. Otherwise add it to the list of process to run in the next round. */
371 if (process != issuer) {
373 if (process->waiting_action) {
375 switch (process->waiting_action->type) {
377 case SIMIX_ACTION_EXECUTE:
378 case SIMIX_ACTION_PARALLEL_EXECUTE:
379 SIMIX_host_execution_resume(process->waiting_action);
382 case SIMIX_ACTION_COMMUNICATE:
383 SIMIX_comm_resume(process->waiting_action);
386 case SIMIX_ACTION_SLEEP:
387 SIMIX_process_sleep_resume(process->waiting_action);
391 xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
392 process->waiting_action->type);
396 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
401 int SIMIX_process_get_maxpid(void) {
402 return simix_process_maxpid;
405 int SIMIX_process_count(void)
407 return xbt_swag_size(simix_global->process_list);
410 void* SIMIX_process_self_get_data(void)
412 smx_process_t me = SIMIX_process_self();
416 return SIMIX_process_get_data(me);
419 void SIMIX_process_self_set_data(void *data)
421 SIMIX_process_set_data(SIMIX_process_self(), data);
424 void* SIMIX_process_get_data(smx_process_t process)
426 return process->data;
429 void SIMIX_process_set_data(smx_process_t process, void *data)
431 process->data = data;
434 smx_host_t SIMIX_process_get_host(smx_process_t process)
436 return process->smx_host;
439 /* needs to be public and without request because it is called
440 by exceptions and logging events */
441 const char* SIMIX_process_self_get_name(void) {
443 smx_process_t process = SIMIX_process_self();
444 if (process == NULL || process == simix_global->maestro_process)
447 return SIMIX_process_get_name(process);
450 const char* SIMIX_process_get_name(smx_process_t process)
452 return process->name;
455 smx_process_t SIMIX_process_get_by_name(const char* name)
459 xbt_swag_foreach(proc, simix_global->process_list)
461 if(!strcmp(name, proc->name))
467 int SIMIX_process_is_suspended(smx_process_t process)
469 return process->suspended;
472 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
474 return process->properties;
477 void SIMIX_pre_process_sleep(smx_req_t req)
480 MC_process_clock_add(req->issuer, req->process_sleep.duration);
481 req->process_sleep.result = SIMIX_DONE;
482 SIMIX_request_answer(req);
485 smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
486 xbt_fifo_push(action->request_list, req);
487 req->issuer->waiting_action = action;
490 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
493 smx_host_t host = process->smx_host;
495 /* check if the host is active */
496 if (surf_workstation_model->extension.
497 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
498 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
502 action = xbt_mallocator_get(simix_global->action_mallocator);
503 action->type = SIMIX_ACTION_SLEEP;
506 action->category = NULL;
509 action->sleep.host = host;
510 action->sleep.surf_sleep =
511 surf_workstation_model->extension.workstation.sleep(host->host, duration);
513 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
514 XBT_DEBUG("Create sleep action %p", action);
519 void SIMIX_post_process_sleep(smx_action_t action)
524 while ((req = xbt_fifo_shift(action->request_list))) {
526 switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
527 case SURF_ACTION_FAILED:
528 state = SIMIX_SRC_HOST_FAILURE;
531 case SURF_ACTION_DONE:
539 req->process_sleep.result = state;
540 req->issuer->waiting_action = NULL;
541 SIMIX_request_answer(req);
543 SIMIX_process_sleep_destroy(action);
546 void SIMIX_process_sleep_destroy(smx_action_t action)
548 XBT_DEBUG("Destroy action %p", action);
549 if (action->sleep.surf_sleep)
550 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
551 xbt_mallocator_release(simix_global->action_mallocator, action);
554 void SIMIX_process_sleep_suspend(smx_action_t action)
556 surf_workstation_model->suspend(action->sleep.surf_sleep);
559 void SIMIX_process_sleep_resume(smx_action_t action)
561 surf_workstation_model->resume(action->sleep.surf_sleep);
565 * Calling this function makes the process to yield.
566 * Only the processes can call this function, giving back the control to maestro
568 void SIMIX_process_yield(void)
570 smx_process_t self = SIMIX_process_self();
572 XBT_DEBUG("Yield process '%s'", self->name);
574 /* Go into sleep and return control to maestro */
575 SIMIX_context_suspend(self->context);
577 /* Ok, maestro returned control to us */
578 XBT_DEBUG("Control returned to me: '%s'", self->name);
580 if (self->context->iwannadie){
581 XBT_DEBUG("I wanna die!");
582 SIMIX_context_stop(self->context);
585 if (self->doexception) {
586 XBT_DEBUG("Wait, maestro left me an exception");
587 self->doexception = 0;
591 if (self->new_host) {
592 SIMIX_process_change_host(self, self->new_host);
593 self->new_host = NULL;
597 /* callback: context fetching */
598 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
600 return SIMIX_process_self()->running_ctx;
603 /* callback: termination */
604 void SIMIX_process_exception_terminate(xbt_ex_t * e)
610 smx_context_t SIMIX_process_get_context(smx_process_t p) {
614 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {