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"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14 "Logging specific to SIMIX (process)");
16 unsigned long simix_process_maxpid = 0;
19 * \brief Returns the current agent.
21 * This functions returns the currently running SIMIX process.
23 * \return The SIMIX process
25 XBT_INLINE smx_process_t SIMIX_process_self(void)
27 smx_context_t self_context = SIMIX_context_self();
29 return self_context ? SIMIX_context_get_data(self_context) : NULL;
33 * \brief Move a process to the list of processes to destroy.
35 void SIMIX_process_cleanup(smx_process_t process)
37 XBT_DEBUG("Cleanup process %s (%p), waiting action %p",
38 process->name, process, process->waiting_action);
40 /* cancel non-blocking communications */
42 while ((action = xbt_fifo_pop(process->comms))) {
44 /* make sure no one will finish the comm after this process is destroyed,
45 * because src_proc or dst_proc would be an invalid pointer */
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 XBT_DEBUG("Increase the refcount before destroying it since it's detached");
56 /* I'm not supposed to destroy a detached comm from the sender side,
57 * unless there is no receiver matching the rdv */
58 action->comm.refcount++;
59 SIMIX_comm_destroy(action);
62 XBT_DEBUG("Don't destroy it since its refcount is %d", action->comm.refcount);
65 SIMIX_comm_destroy(action);
68 else if (action->comm.dst_proc == process){
69 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
70 action, action->state, action->comm.src_proc, action->comm.dst_proc);
71 action->comm.dst_proc = NULL;
73 if (action->comm.detached && action->comm.refcount == 1
74 && action->comm.src_proc != NULL) {
75 /* the comm will be freed right now, remove it from the sender */
76 xbt_fifo_remove(action->comm.src_proc->comms, action);
78 SIMIX_comm_destroy(action);
81 xbt_die("Communication action %p is in my list but I'm not the sender "
82 "or the receiver", action);
86 /*xbt_swag_remove(process, simix_global->process_to_run);*/
87 xbt_swag_remove(process, simix_global->process_list);
88 xbt_swag_remove(process, process->smx_host->process_list);
89 xbt_swag_insert(process, simix_global->process_to_destroy);
95 * Should be called some time to time to free the memory allocated for processes
96 * that have finished (or killed).
98 void SIMIX_process_empty_trash(void)
100 smx_process_t process = NULL;
102 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
103 SIMIX_context_free(process->context);
105 /* Free the exception allocated at creation time */
106 free(process->running_ctx);
107 xbt_dict_free(&process->properties);
109 xbt_fifo_free(process->comms);
117 * \brief Creates and runs the maestro process
119 void SIMIX_create_maestro_process()
121 smx_process_t maestro = NULL;
123 /* Create maestro process and intilialize it */
124 maestro = xbt_new0(s_smx_process_t, 1);
125 maestro->pid = simix_process_maxpid++;
126 maestro->name = (char *) "";
127 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
128 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
129 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
130 maestro->request.issuer = maestro;
132 simix_global->maestro_process = maestro;
137 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
138 * \return the process created
140 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
142 smx_process_t process;
143 simix_global->create_process_function(
157 * \brief Internal function to create a process.
159 * This function actually creates the process.
160 * It may be called when a REQ_PROCESS_CREATE request occurs,
161 * or directly for SIMIX internal purposes.
163 * \return the process created
165 void SIMIX_process_create(smx_process_t *process,
167 xbt_main_func_t code,
169 const char *hostname,
170 int argc, char **argv,
171 xbt_dict_t properties) {
174 smx_host_t host = SIMIX_host_get_by_name(hostname);
176 XBT_DEBUG("Start process %s on host %s", name, hostname);
178 if (!SIMIX_host_get_state(host)) {
179 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
183 *process = xbt_new0(s_smx_process_t, 1);
185 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
188 (*process)->pid = simix_process_maxpid++;
189 (*process)->name = xbt_strdup(name);
190 (*process)->smx_host = host;
191 (*process)->data = data;
192 (*process)->comms = xbt_fifo_new();
193 (*process)->request.issuer = *process;
195 XBT_VERB("Create context %s", (*process)->name);
196 (*process)->context = SIMIX_context_new(code, argc, argv,
197 simix_global->cleanup_process_function, *process);
199 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
200 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
203 (*process)->properties = properties;
205 /* Add the process to it's host process list */
206 xbt_swag_insert(*process, host->process_list);
208 XBT_DEBUG("Start context '%s'", (*process)->name);
210 /* Now insert it in the global process list and in the process to run list */
211 xbt_swag_insert(*process, simix_global->process_list);
212 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
213 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
218 * \brief Executes the processes from simix_global->process_to_run.
220 * The processes of simix_global->process_to_run are run (in parallel if
221 * possible). On exit, simix_global->process_to_run is empty, and
222 * simix_global->process_that_ran contains the list of processes that just ran.
223 * The two lists are swapped so, be careful when using them before and after a
224 * call to this function.
226 void SIMIX_process_runall(void)
228 SIMIX_context_runall();
230 xbt_dynar_t tmp = simix_global->process_that_ran;
231 simix_global->process_that_ran = simix_global->process_to_run;
232 simix_global->process_to_run = tmp;
233 xbt_dynar_reset(simix_global->process_to_run);
237 * \brief Internal function to kill a SIMIX process.
239 * This function may be called when a REQ_PROCESS_KILL request occurs,
240 * or directly for SIMIX internal purposes.
242 * \param process poor victim
244 void SIMIX_process_kill(smx_process_t process) {
246 XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
248 process->context->iwannadie = 1;
249 process->blocked = 0;
250 process->suspended = 0;
251 /* FIXME: set doexception to 0 also? */
253 /* destroy the blocking action if any */
254 if (process->waiting_action) {
256 switch (process->waiting_action->type) {
258 case SIMIX_ACTION_EXECUTE:
259 case SIMIX_ACTION_PARALLEL_EXECUTE:
260 SIMIX_host_execution_destroy(process->waiting_action);
263 case SIMIX_ACTION_COMMUNICATE:
264 SIMIX_comm_destroy(process->waiting_action);
267 case SIMIX_ACTION_SLEEP:
268 SIMIX_process_sleep_destroy(process->waiting_action);
271 case SIMIX_ACTION_SYNCHRO:
272 SIMIX_synchro_stop_waiting(process, &process->request);
273 SIMIX_synchro_destroy(process->waiting_action);
276 case SIMIX_ACTION_IO:
282 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
286 * \brief Kills all running processes.
287 * \param issuer this one will not be killed
289 void SIMIX_process_killall(smx_process_t issuer)
291 smx_process_t p = NULL;
293 while ((p = xbt_swag_extract(simix_global->process_list))) {
295 SIMIX_process_kill(p);
299 SIMIX_context_runall(simix_global->process_to_run);
301 SIMIX_process_empty_trash();
304 void SIMIX_process_change_host(smx_process_t process,
307 xbt_assert((process != NULL), "Invalid parameters");
308 xbt_swag_remove(process, process->smx_host->process_list);
309 process->smx_host = dest;
310 xbt_swag_insert(process, dest->process_list);
313 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
315 process->new_host = dest;
318 void SIMIX_pre_process_suspend(smx_req_t req)
320 smx_process_t process = req->process_suspend.process;
321 SIMIX_process_suspend(process, req->issuer);
323 if (process != req->issuer) {
324 SIMIX_request_answer(req);
326 /* If we are suspending ourselves, then just do not replay the request. */
329 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
331 xbt_assert((process != NULL), "Invalid parameters");
333 if (process->suspended) {
334 XBT_DEBUG("Process '%s' is already suspended", process->name);
338 process->suspended = 1;
340 /* If we are suspending another process, and it is waiting on an action,
341 suspend its action. */
342 if (process != issuer) {
344 if (process->waiting_action) {
346 switch (process->waiting_action->type) {
348 case SIMIX_ACTION_EXECUTE:
349 case SIMIX_ACTION_PARALLEL_EXECUTE:
350 SIMIX_host_execution_suspend(process->waiting_action);
353 case SIMIX_ACTION_COMMUNICATE:
354 SIMIX_comm_suspend(process->waiting_action);
357 case SIMIX_ACTION_SLEEP:
358 SIMIX_process_sleep_suspend(process->waiting_action);
362 xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
363 process->waiting_action->type);
369 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
371 xbt_assert((process != NULL), "Invalid parameters");
373 if (!process->suspended) {
374 XBT_DEBUG("Process '%s' is not suspended", process->name);
378 process->suspended = 0;
380 /* If we are resuming another process, resume the action it was waiting for
381 if any. Otherwise add it to the list of process to run in the next round. */
382 if (process != issuer) {
384 if (process->waiting_action) {
386 switch (process->waiting_action->type) {
388 case SIMIX_ACTION_EXECUTE:
389 case SIMIX_ACTION_PARALLEL_EXECUTE:
390 SIMIX_host_execution_resume(process->waiting_action);
393 case SIMIX_ACTION_COMMUNICATE:
394 SIMIX_comm_resume(process->waiting_action);
397 case SIMIX_ACTION_SLEEP:
398 SIMIX_process_sleep_resume(process->waiting_action);
402 xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
403 process->waiting_action->type);
407 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
412 int SIMIX_process_get_maxpid(void) {
413 return simix_process_maxpid;
416 int SIMIX_process_count(void)
418 return xbt_swag_size(simix_global->process_list);
421 void* SIMIX_process_self_get_data(smx_process_t self)
423 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
428 return SIMIX_process_get_data(self);
431 void SIMIX_process_self_set_data(smx_process_t self, void *data)
433 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
435 SIMIX_process_set_data(self, data);
438 void* SIMIX_process_get_data(smx_process_t process)
440 return process->data;
443 void SIMIX_process_set_data(smx_process_t process, void *data)
445 process->data = data;
448 smx_host_t SIMIX_process_get_host(smx_process_t process)
450 return process->smx_host;
453 /* needs to be public and without request because it is called
454 by exceptions and logging events */
455 const char* SIMIX_process_self_get_name(void) {
457 smx_process_t process = SIMIX_process_self();
458 if (process == NULL || process == simix_global->maestro_process)
461 return SIMIX_process_get_name(process);
464 const char* SIMIX_process_get_name(smx_process_t process)
466 return process->name;
469 smx_process_t SIMIX_process_get_by_name(const char* name)
473 xbt_swag_foreach(proc, simix_global->process_list)
475 if(!strcmp(name, proc->name))
481 int SIMIX_process_is_suspended(smx_process_t process)
483 return process->suspended;
486 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
488 return process->properties;
491 void SIMIX_pre_process_sleep(smx_req_t req)
494 MC_process_clock_add(req->issuer, req->process_sleep.duration);
495 req->process_sleep.result = SIMIX_DONE;
496 SIMIX_request_answer(req);
499 smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
500 xbt_fifo_push(action->request_list, req);
501 req->issuer->waiting_action = action;
504 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
507 smx_host_t host = process->smx_host;
509 /* check if the host is active */
510 if (surf_workstation_model->extension.
511 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
512 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
516 action = xbt_mallocator_get(simix_global->action_mallocator);
517 action->type = SIMIX_ACTION_SLEEP;
520 action->category = NULL;
523 action->sleep.host = host;
524 action->sleep.surf_sleep =
525 surf_workstation_model->extension.workstation.sleep(host->host, duration);
527 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
528 XBT_DEBUG("Create sleep action %p", action);
533 void SIMIX_post_process_sleep(smx_action_t action)
538 while ((req = xbt_fifo_shift(action->request_list))) {
540 switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
541 case SURF_ACTION_FAILED:
542 state = SIMIX_SRC_HOST_FAILURE;
545 case SURF_ACTION_DONE:
553 req->process_sleep.result = state;
554 req->issuer->waiting_action = NULL;
555 SIMIX_request_answer(req);
557 SIMIX_process_sleep_destroy(action);
560 void SIMIX_process_sleep_destroy(smx_action_t action)
562 XBT_DEBUG("Destroy action %p", action);
563 if (action->sleep.surf_sleep)
564 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
565 xbt_mallocator_release(simix_global->action_mallocator, action);
568 void SIMIX_process_sleep_suspend(smx_action_t action)
570 surf_workstation_model->suspend(action->sleep.surf_sleep);
573 void SIMIX_process_sleep_resume(smx_action_t action)
575 surf_workstation_model->resume(action->sleep.surf_sleep);
579 * \brief Calling this function makes the process to yield.
581 * Only the current process can call this function, giving back the control to
584 * \param self the current process
586 void SIMIX_process_yield(smx_process_t self)
588 XBT_DEBUG("Yield process '%s'", self->name);
590 /* Go into sleep and return control to maestro */
591 SIMIX_context_suspend(self->context);
593 /* Ok, maestro returned control to us */
594 XBT_DEBUG("Control returned to me: '%s'", self->name);
596 if (self->context->iwannadie){
597 XBT_DEBUG("I wanna die!");
598 SIMIX_context_stop(self->context);
601 if (self->doexception) {
602 XBT_DEBUG("Wait, maestro left me an exception");
603 self->doexception = 0;
607 if (self->new_host) {
608 SIMIX_process_change_host(self, self->new_host);
609 self->new_host = NULL;
613 /* callback: context fetching */
614 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
616 return SIMIX_process_self()->running_ctx;
619 /* callback: termination */
620 void SIMIX_process_exception_terminate(xbt_ex_t * e)
626 smx_context_t SIMIX_process_get_context(smx_process_t p) {
630 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
635 * \brief Returns the list of processes to run.
637 XBT_INLINE xbt_dynar_t SIMIX_process_get_runnable(void)
639 return simix_global->process_to_run;