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;
67 SIMIX_comm_destroy(action);
70 /*xbt_swag_remove(process, simix_global->process_to_run);*/
71 xbt_swag_remove(process, simix_global->process_list);
72 xbt_swag_remove(process, process->smx_host->process_list);
73 xbt_swag_insert(process, simix_global->process_to_destroy);
79 * Should be called some time to time to free the memory allocated for processes
80 * that have finished (or killed).
82 void SIMIX_process_empty_trash(void)
84 smx_process_t process = NULL;
86 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
87 SIMIX_context_free(process->context);
89 /* Free the exception allocated at creation time */
90 if (process->running_ctx)
91 free(process->running_ctx);
92 if (process->properties)
93 xbt_dict_free(&process->properties);
95 xbt_fifo_free(process->comms);
104 * \brief Creates and runs the maestro process
106 void SIMIX_create_maestro_process()
108 smx_process_t maestro = NULL;
110 /* Create maestro process and intilialize it */
111 maestro = xbt_new0(s_smx_process_t, 1);
112 maestro->pid = simix_process_maxpid++;
113 maestro->name = (char *) "";
114 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
115 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
116 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
117 maestro->request.issuer = maestro;
119 simix_global->maestro_process = maestro;
124 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
125 * \return the process created
127 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
129 smx_process_t process;
130 simix_global->create_process_function(
144 * \brief Internal function to create a process.
146 * This function actually creates the process.
147 * It may be called when a REQ_PROCESS_CREATE request occurs,
148 * or directly for SIMIX internal purposes.
150 * \return the process created
152 void SIMIX_process_create(smx_process_t *process,
154 xbt_main_func_t code,
156 const char *hostname,
157 int argc, char **argv,
158 xbt_dict_t properties) {
161 smx_host_t host = SIMIX_host_get_by_name(hostname);
163 XBT_DEBUG("Start process %s on host %s", name, hostname);
165 if (!SIMIX_host_get_state(host)) {
166 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
170 *process = xbt_new0(s_smx_process_t, 1);
172 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
175 (*process)->pid = simix_process_maxpid++;
176 (*process)->name = xbt_strdup(name);
177 (*process)->smx_host = host;
178 (*process)->data = data;
179 (*process)->comms = xbt_fifo_new();
181 XBT_VERB("Create context %s", (*process)->name);
182 (*process)->context = SIMIX_context_new(code, argc, argv,
183 simix_global->cleanup_process_function, *process);
185 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
186 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
189 (*process)->properties = properties;
191 /* Add the process to it's host process list */
192 xbt_swag_insert(*process, host->process_list);
194 XBT_DEBUG("Start context '%s'", (*process)->name);
196 /* Now insert it in the global process list and in the process to run list */
197 xbt_swag_insert(*process, simix_global->process_list);
198 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
199 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
204 * \brief Executes the processes from simix_global->process_to_run.
206 * The processes of simix_global->process_to_run are run (in parallel if
207 * possible). On exit, simix_global->process_to_run is empty, and
208 * simix_global->process_that_ran contains the list of processes that just ran.
209 * The two lists are swapped so, be careful when using them before and after a
210 * call to this function.
212 void SIMIX_process_runall(void)
214 SIMIX_context_runall(simix_global->process_to_run);
215 xbt_dynar_t tmp = simix_global->process_that_ran;
216 simix_global->process_that_ran = simix_global->process_to_run;
217 simix_global->process_to_run = tmp;
218 xbt_dynar_reset(simix_global->process_to_run);
222 * \brief Internal function to kill a SIMIX process.
224 * This function may be called when a REQ_PROCESS_KILL request occurs,
225 * or directly for SIMIX internal purposes.
227 * \param process poor victim
229 void SIMIX_process_kill(smx_process_t process) {
231 XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
233 process->context->iwannadie = 1;
234 process->blocked = 0;
235 process->suspended = 0;
236 /* FIXME: set doexception to 0 also? */
238 /* destroy the blocking action if any */
239 if (process->waiting_action) {
241 switch (process->waiting_action->type) {
243 case SIMIX_ACTION_EXECUTE:
244 case SIMIX_ACTION_PARALLEL_EXECUTE:
245 SIMIX_host_execution_destroy(process->waiting_action);
248 case SIMIX_ACTION_COMMUNICATE:
249 SIMIX_comm_destroy(process->waiting_action);
252 case SIMIX_ACTION_SLEEP:
253 SIMIX_process_sleep_destroy(process->waiting_action);
256 case SIMIX_ACTION_SYNCHRO:
257 SIMIX_synchro_stop_waiting(process, &process->request);
258 SIMIX_synchro_destroy(process->waiting_action);
261 case SIMIX_ACTION_IO:
267 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
271 * \brief Kills all running processes.
272 * \param issuer this one will not be killed
274 void SIMIX_process_killall(smx_process_t issuer)
276 smx_process_t p = NULL;
278 while ((p = xbt_swag_extract(simix_global->process_list))) {
280 SIMIX_process_kill(p);
284 SIMIX_context_runall(simix_global->process_to_run);
286 SIMIX_process_empty_trash();
289 void SIMIX_process_change_host(smx_process_t process,
292 xbt_assert((process != NULL), "Invalid parameters");
293 xbt_swag_remove(process, process->smx_host->process_list);
294 process->smx_host = dest;
295 xbt_swag_insert(process, dest->process_list);
298 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
300 process->new_host = dest;
303 void SIMIX_pre_process_suspend(smx_req_t req)
305 smx_process_t process = req->process_suspend.process;
306 SIMIX_process_suspend(process, req->issuer);
308 if (process != req->issuer) {
309 SIMIX_request_answer(req);
311 /* If we are suspending ourselves, then just do not replay the request. */
314 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
316 process->suspended = 1;
318 /* If we are suspending another process, and it is waiting on an action,
319 suspend it's action. */
320 if (process != issuer) {
322 if (process->waiting_action) {
324 switch (process->waiting_action->type) {
326 case SIMIX_ACTION_EXECUTE:
327 case SIMIX_ACTION_PARALLEL_EXECUTE:
328 SIMIX_host_execution_suspend(process->waiting_action);
331 case SIMIX_ACTION_COMMUNICATE:
332 SIMIX_comm_suspend(process->waiting_action);
335 case SIMIX_ACTION_SLEEP:
336 SIMIX_process_sleep_suspend(process->waiting_action);
346 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
348 xbt_assert((process != NULL), "Invalid parameters");
350 process->suspended = 0;
352 /* If we are resuming another process, resume the action it was waiting for
353 if any. Otherwise add it to the list of process to run in the next round. */
354 if (process != issuer) {
356 if (process->waiting_action) {
358 switch (process->waiting_action->type) {
360 case SIMIX_ACTION_EXECUTE:
361 case SIMIX_ACTION_PARALLEL_EXECUTE:
362 SIMIX_host_execution_resume(process->waiting_action);
365 case SIMIX_ACTION_COMMUNICATE:
366 SIMIX_comm_resume(process->waiting_action);
369 case SIMIX_ACTION_SLEEP:
370 SIMIX_process_sleep_resume(process->waiting_action);
378 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
383 int SIMIX_process_get_maxpid(void) {
384 return simix_process_maxpid;
387 int SIMIX_process_count(void)
389 return xbt_swag_size(simix_global->process_list);
392 void* SIMIX_process_self_get_data(void)
394 smx_process_t me = SIMIX_process_self();
398 return SIMIX_process_get_data(me);
401 void SIMIX_process_self_set_data(void *data)
403 SIMIX_process_set_data(SIMIX_process_self(), data);
406 void* SIMIX_process_get_data(smx_process_t process)
408 return process->data;
411 void SIMIX_process_set_data(smx_process_t process, void *data)
413 process->data = data;
416 smx_host_t SIMIX_process_get_host(smx_process_t process)
418 return process->smx_host;
421 /* needs to be public and without request because it is called
422 by exceptions and logging events */
423 const char* SIMIX_process_self_get_name(void) {
425 smx_process_t process = SIMIX_process_self();
426 if (process == NULL || process == simix_global->maestro_process)
429 return SIMIX_process_get_name(process);
432 const char* SIMIX_process_get_name(smx_process_t process)
434 return process->name;
437 smx_process_t SIMIX_process_get_by_name(const char* name)
441 xbt_swag_foreach(proc, simix_global->process_list)
443 if(!strcmp(name, proc->name))
449 int SIMIX_process_is_suspended(smx_process_t process)
451 return process->suspended;
454 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
456 return process->properties;
459 void SIMIX_pre_process_sleep(smx_req_t req)
462 MC_process_clock_add(req->issuer, req->process_sleep.duration);
463 req->process_sleep.result = SIMIX_DONE;
464 SIMIX_request_answer(req);
467 smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
468 xbt_fifo_push(action->request_list, req);
469 req->issuer->waiting_action = action;
472 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
475 smx_host_t host = process->smx_host;
477 /* check if the host is active */
478 if (surf_workstation_model->extension.
479 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
480 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
484 action = xbt_mallocator_get(simix_global->action_mallocator);
485 action->type = SIMIX_ACTION_SLEEP;
488 action->category = NULL;
491 action->sleep.host = host;
492 action->sleep.surf_sleep =
493 surf_workstation_model->extension.workstation.sleep(host->host, duration);
495 surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
496 XBT_DEBUG("Create sleep action %p", action);
501 void SIMIX_post_process_sleep(smx_action_t action)
506 while ((req = xbt_fifo_shift(action->request_list))) {
508 switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
509 case SURF_ACTION_FAILED:
510 state = SIMIX_SRC_HOST_FAILURE;
513 case SURF_ACTION_DONE:
521 req->process_sleep.result = state;
522 req->issuer->waiting_action = NULL;
523 SIMIX_request_answer(req);
525 SIMIX_process_sleep_destroy(action);
528 void SIMIX_process_sleep_destroy(smx_action_t action)
530 XBT_DEBUG("Destroy action %p", action);
531 if (action->sleep.surf_sleep)
532 action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
533 xbt_mallocator_release(simix_global->action_mallocator, action);
536 void SIMIX_process_sleep_suspend(smx_action_t action)
538 surf_workstation_model->suspend(action->sleep.surf_sleep);
541 void SIMIX_process_sleep_resume(smx_action_t action)
543 surf_workstation_model->resume(action->sleep.surf_sleep);
547 * Calling this function makes the process to yield.
548 * Only the processes can call this function, giving back the control to maestro
550 void SIMIX_process_yield(void)
552 smx_process_t self = SIMIX_process_self();
554 XBT_DEBUG("Yield process '%s'", self->name);
556 /* Go into sleep and return control to maestro */
557 SIMIX_context_suspend(self->context);
559 /* Ok, maestro returned control to us */
560 XBT_DEBUG("Maestro returned control to me: '%s'", self->name);
562 if (self->context->iwannadie){
563 XBT_DEBUG("I wanna die!");
564 SIMIX_context_stop(self->context);
567 if (self->doexception) {
568 XBT_DEBUG("Wait, maestro left me an exception");
569 self->doexception = 0;
573 if (self->new_host) {
574 SIMIX_process_change_host(self, self->new_host);
575 self->new_host = NULL;
579 /* callback: context fetching */
580 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
582 return SIMIX_process_self()->running_ctx;
585 /* callback: termination */
586 void SIMIX_process_exception_terminate(xbt_ex_t * e)
592 smx_context_t SIMIX_process_get_context(smx_process_t p) {
596 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {