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, src = %p, dst = %p",
50 action, action->comm.detached, action->state, action->comm.src_proc, action->comm.dst_proc);
51 action->comm.src_proc = NULL;
53 if (action->comm.detached) {
54 if (action->comm.refcount == 0) {
55 /* I'm not supposed to destroy a detached comm from the sender side,
56 * unless there is no receiver matching the rdv */
57 action->comm.refcount++;
58 SIMIX_comm_destroy(action);
62 SIMIX_comm_destroy(action);
65 else if (action->comm.dst_proc == process){
66 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
67 action, action->state, action->comm.src_proc, action->comm.dst_proc);
68 action->comm.dst_proc = NULL;
70 if (action->comm.detached && action->comm.refcount == 1
71 && action->comm.src_proc != NULL) {
72 /* the comm will be freed right now, remove it from the sender */
73 xbt_fifo_remove(action->comm.src_proc->comms, action);
75 SIMIX_comm_destroy(action);
78 xbt_die("Communication action %p is in my list but I'm not the sender "
79 "or the receiver", action);
83 /*xbt_swag_remove(process, simix_global->process_to_run);*/
84 xbt_swag_remove(process, simix_global->process_list);
85 xbt_swag_remove(process, process->smx_host->process_list);
86 xbt_swag_insert(process, simix_global->process_to_destroy);
92 * Should be called some time to time to free the memory allocated for processes
93 * that have finished (or killed).
95 void SIMIX_process_empty_trash(void)
97 smx_process_t process = NULL;
99 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
100 SIMIX_context_free(process->context);
102 /* Free the exception allocated at creation time */
103 free(process->running_ctx);
104 xbt_dict_free(&process->properties);
106 xbt_fifo_free(process->comms);
114 * \brief Creates and runs the maestro process
116 void SIMIX_create_maestro_process()
118 smx_process_t maestro = NULL;
120 /* Create maestro process and intilialize it */
121 maestro = xbt_new0(s_smx_process_t, 1);
122 maestro->pid = simix_process_maxpid++;
123 maestro->name = (char *) "";
124 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
125 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
126 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
127 maestro->request.issuer = maestro;
129 simix_global->maestro_process = maestro;
134 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
135 * \return the process created
137 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
139 smx_process_t process;
140 simix_global->create_process_function(
154 * \brief Internal function to create a process.
156 * This function actually creates the process.
157 * It may be called when a REQ_PROCESS_CREATE request occurs,
158 * or directly for SIMIX internal purposes.
160 * \return the process created
162 void SIMIX_process_create(smx_process_t *process,
164 xbt_main_func_t code,
166 const char *hostname,
167 int argc, char **argv,
168 xbt_dict_t properties) {
171 smx_host_t host = SIMIX_host_get_by_name(hostname);
173 XBT_DEBUG("Start process %s on host %s", name, hostname);
175 if (!SIMIX_host_get_state(host)) {
176 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
180 *process = xbt_new0(s_smx_process_t, 1);
182 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
185 (*process)->pid = simix_process_maxpid++;
186 (*process)->name = xbt_strdup(name);
187 (*process)->smx_host = host;
188 (*process)->data = data;
189 (*process)->comms = xbt_fifo_new();
191 XBT_VERB("Create context %s", (*process)->name);
192 (*process)->context = SIMIX_context_new(code, argc, argv,
193 simix_global->cleanup_process_function, *process);
195 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
196 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
199 (*process)->properties = properties;
201 /* Add the process to it's host process list */
202 xbt_swag_insert(*process, host->process_list);
204 XBT_DEBUG("Start context '%s'", (*process)->name);
206 /* Now insert it in the global process list and in the process to run list */
207 xbt_swag_insert(*process, simix_global->process_list);
208 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
209 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
214 * \brief Executes the processes from simix_global->process_to_run.
216 * The processes of simix_global->process_to_run are run (in parallel if
217 * possible). On exit, simix_global->process_to_run is empty, and
218 * simix_global->process_that_ran contains the list of processes that just ran.
219 * The two lists are swapped so, be careful when using them before and after a
220 * call to this function.
222 void SIMIX_process_runall(void)
224 SIMIX_context_runall();
226 xbt_dynar_t tmp = simix_global->process_that_ran;
227 simix_global->process_that_ran = simix_global->process_to_run;
228 simix_global->process_to_run = tmp;
229 xbt_dynar_reset(simix_global->process_to_run);
233 * \brief Internal function to kill a SIMIX process.
235 * This function may be called when a REQ_PROCESS_KILL request occurs,
236 * or directly for SIMIX internal purposes.
238 * \param process poor victim
240 void SIMIX_process_kill(smx_process_t process) {
242 XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
244 process->context->iwannadie = 1;
245 process->blocked = 0;
246 process->suspended = 0;
247 /* FIXME: set doexception to 0 also? */
249 /* destroy the blocking action if any */
250 if (process->waiting_action) {
252 switch (process->waiting_action->type) {
254 case SIMIX_ACTION_EXECUTE:
255 case SIMIX_ACTION_PARALLEL_EXECUTE:
256 SIMIX_host_execution_destroy(process->waiting_action);
259 case SIMIX_ACTION_COMMUNICATE:
260 SIMIX_comm_destroy(process->waiting_action);
263 case SIMIX_ACTION_SLEEP:
264 SIMIX_process_sleep_destroy(process->waiting_action);
267 case SIMIX_ACTION_SYNCHRO:
268 SIMIX_synchro_stop_waiting(process, &process->request);
269 SIMIX_synchro_destroy(process->waiting_action);
272 case SIMIX_ACTION_IO:
278 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
282 * \brief Kills all running processes.
283 * \param issuer this one will not be killed
285 void SIMIX_process_killall(smx_process_t issuer)
287 smx_process_t p = NULL;
289 while ((p = xbt_swag_extract(simix_global->process_list))) {
291 SIMIX_process_kill(p);
295 SIMIX_context_runall(simix_global->process_to_run);
297 SIMIX_process_empty_trash();
300 void SIMIX_process_change_host(smx_process_t process,
303 xbt_assert((process != NULL), "Invalid parameters");
304 xbt_swag_remove(process, process->smx_host->process_list);
305 process->smx_host = dest;
306 xbt_swag_insert(process, dest->process_list);
309 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
311 process->new_host = dest;
314 void SIMIX_pre_process_suspend(smx_req_t req)
316 smx_process_t process = req->process_suspend.process;
317 SIMIX_process_suspend(process, req->issuer);
319 if (process != req->issuer) {
320 SIMIX_request_answer(req);
322 /* If we are suspending ourselves, then just do not replay the request. */
325 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
327 xbt_assert((process != NULL), "Invalid parameters");
329 if (process->suspended) {
330 XBT_DEBUG("Process '%s' is already suspended", process->name);
334 process->suspended = 1;
336 /* If we are suspending another process, and it is waiting on an action,
337 suspend its action. */
338 if (process != issuer) {
340 if (process->waiting_action) {
342 switch (process->waiting_action->type) {
344 case SIMIX_ACTION_EXECUTE:
345 case SIMIX_ACTION_PARALLEL_EXECUTE:
346 SIMIX_host_execution_suspend(process->waiting_action);
349 case SIMIX_ACTION_COMMUNICATE:
350 SIMIX_comm_suspend(process->waiting_action);
353 case SIMIX_ACTION_SLEEP:
354 SIMIX_process_sleep_suspend(process->waiting_action);
358 xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
359 process->waiting_action->type);
365 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
367 xbt_assert((process != NULL), "Invalid parameters");
369 if (!process->suspended) {
370 XBT_DEBUG("Process '%s' is not suspended", process->name);
374 process->suspended = 0;
376 /* If we are resuming another process, resume the action it was waiting for
377 if any. Otherwise add it to the list of process to run in the next round. */
378 if (process != issuer) {
380 if (process->waiting_action) {
382 switch (process->waiting_action->type) {
384 case SIMIX_ACTION_EXECUTE:
385 case SIMIX_ACTION_PARALLEL_EXECUTE:
386 SIMIX_host_execution_resume(process->waiting_action);
389 case SIMIX_ACTION_COMMUNICATE:
390 SIMIX_comm_resume(process->waiting_action);
393 case SIMIX_ACTION_SLEEP:
394 SIMIX_process_sleep_resume(process->waiting_action);
398 xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
399 process->waiting_action->type);
403 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
408 int SIMIX_process_get_maxpid(void) {
409 return simix_process_maxpid;
412 int SIMIX_process_count(void)
414 return xbt_swag_size(simix_global->process_list);
417 void* SIMIX_process_self_get_data(void)
419 smx_process_t me = SIMIX_process_self();
423 return SIMIX_process_get_data(me);
426 void SIMIX_process_self_set_data(void *data)
428 SIMIX_process_set_data(SIMIX_process_self(), data);
431 void* SIMIX_process_get_data(smx_process_t process)
433 return process->data;
436 void SIMIX_process_set_data(smx_process_t process, void *data)
438 process->data = data;
441 smx_host_t SIMIX_process_get_host(smx_process_t process)
443 return process->smx_host;
446 /* needs to be public and without request because it is called
447 by exceptions and logging events */
448 const char* SIMIX_process_self_get_name(void) {
450 smx_process_t process = SIMIX_process_self();
451 if (process == NULL || process == simix_global->maestro_process)
454 return SIMIX_process_get_name(process);
457 const char* SIMIX_process_get_name(smx_process_t process)
459 return process->name;
462 smx_process_t SIMIX_process_get_by_name(const char* name)
466 xbt_swag_foreach(proc, simix_global->process_list)
468 if(!strcmp(name, proc->name))
474 int SIMIX_process_is_suspended(smx_process_t process)
476 return process->suspended;
479 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
481 return process->properties;
484 void SIMIX_pre_process_sleep(smx_req_t req)
487 MC_process_clock_add(req->issuer, req->process_sleep.duration);
488 req->process_sleep.result = SIMIX_DONE;
489 SIMIX_request_answer(req);
492 smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
493 xbt_fifo_push(action->request_list, req);
494 req->issuer->waiting_action = action;
497 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
500 smx_host_t host = process->smx_host;
502 /* check if the host is active */
503 if (surf_workstation_model->extension.
504 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
505 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
509 action = xbt_mallocator_get(simix_global->action_mallocator);
510 action->type = SIMIX_ACTION_SLEEP;
513 action->category = NULL;
516 action->sleep.host = host;
517 action->sleep.surf_sleep =
518 surf_workstation_model->extension.workstation.sleep(host->host, duration);
520 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
521 XBT_DEBUG("Create sleep action %p", action);
526 void SIMIX_post_process_sleep(smx_action_t action)
531 while ((req = xbt_fifo_shift(action->request_list))) {
533 switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
534 case SURF_ACTION_FAILED:
535 state = SIMIX_SRC_HOST_FAILURE;
538 case SURF_ACTION_DONE:
546 req->process_sleep.result = state;
547 req->issuer->waiting_action = NULL;
548 SIMIX_request_answer(req);
550 SIMIX_process_sleep_destroy(action);
553 void SIMIX_process_sleep_destroy(smx_action_t action)
555 XBT_DEBUG("Destroy action %p", action);
556 if (action->sleep.surf_sleep)
557 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
558 xbt_mallocator_release(simix_global->action_mallocator, action);
561 void SIMIX_process_sleep_suspend(smx_action_t action)
563 surf_workstation_model->suspend(action->sleep.surf_sleep);
566 void SIMIX_process_sleep_resume(smx_action_t action)
568 surf_workstation_model->resume(action->sleep.surf_sleep);
572 * Calling this function makes the process to yield.
573 * Only the processes can call this function, giving back the control to maestro
575 void SIMIX_process_yield(void)
577 smx_process_t self = SIMIX_process_self();
579 XBT_DEBUG("Yield process '%s'", self->name);
581 /* Go into sleep and return control to maestro */
582 SIMIX_context_suspend(self->context);
584 /* Ok, maestro returned control to us */
585 XBT_DEBUG("Control returned to me: '%s'", self->name);
587 if (self->context->iwannadie){
588 XBT_DEBUG("I wanna die!");
589 SIMIX_context_stop(self->context);
592 if (self->doexception) {
593 XBT_DEBUG("Wait, maestro left me an exception");
594 self->doexception = 0;
598 if (self->new_host) {
599 SIMIX_process_change_host(self, self->new_host);
600 self->new_host = NULL;
604 /* callback: context fetching */
605 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
607 return SIMIX_process_self()->running_ctx;
610 /* callback: termination */
611 void SIMIX_process_exception_terminate(xbt_ex_t * e)
617 smx_context_t SIMIX_process_get_context(smx_process_t p) {
621 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {