1 /* Copyright (c) 2007-2012. 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. */
7 #include "smx_private.h"
8 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix,
14 "Logging specific to SIMIX (vms)");
15 /* **** create a VM **** */
18 * \brief Internal function to create a SIMIX host.
19 * \param name name of the host to create
20 * \param data some user data (may be NULL)
22 smx_host_t SIMIX_vm_create(const char *name, smx_host_t phys_host)
25 smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
28 // TODO check why we do not have any VM here and why we have the host_proc_hookup ?
31 smx_host->data = NULL;
32 smx_host->process_list =
33 xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
35 /* Update global variables */
36 xbt_lib_set(host_lib,name,SIMIX_HOST_LEVEL,smx_host);
38 /* Create surf associated resource */
39 // TODO change phys_host into the right workstation surf model
40 surf_vm_workstation_model->extension.vm_workstation.create(name, phys_host);
42 return xbt_lib_get_elm_or_null(host_lib, name);
45 smx_host_t SIMIX_pre_vm_create(smx_simcall_t simcall, const char *name, smx_host_t phys_host){
46 return SIMIX_vm_create(name, phys_host);
50 /* **** start a VM **** */
51 int __can_be_started(smx_host_t vm){
52 // TODO add checking code related to overcommitment or not.
55 void SIMIX_vm_start(smx_host_t vm){
57 //TODO only start the VM if you can
58 if (can_be_started(vm))
59 SIMIX_set_vm_state(vm, msg_vm_state_running);
61 THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(vm));
64 void SIMIX_pre_vm_start(smx_simcall_t simcall, smx_host_t vm){
68 /* ***** set/get state of a VM ***** */
69 void SIMIX_set_vm_state(smx_host_t vm, int state){
72 void SIMIX_prev_set_vm_state(smx_host_t vm, int state){
73 SIMIX_set_vm_state(vm, state);
76 int SIMIX_get_vm_state(smx_host_t vm){
77 return surf_vm_workstation_model->extension.vm_workstation.get_state(vm);
79 int SIMIX_pre_vm_state(smx_host_t vm){
80 return SIMIX_get_vm_state(vm);
83 * \brief Internal function to destroy a SIMIX host.
85 * \param h the host to destroy (a smx_host_t)
87 void SIMIX_vm_host_destroy(void *h)
89 smx_host_priv_t host = (smx_host_priv_t) h;
92 xbt_lib_(host_lib,name,SIMIX_HOST_LEVEL,smx_host);
94 /* jump to vm_ws_destroy() */
95 surf_vm_workstation_model->extension.vm_workstation.destroy(name);
97 xbt_assert((host != NULL), "Invalid parameters");
99 /* Clean Simulator data */
100 if (xbt_swag_size(host->process_list) != 0) {
102 bprintf("Shutting down host, but it's not empty:");
104 smx_process_t process = NULL;
106 xbt_swag_foreach(process, host->process_list) {
107 tmp = bprintf("%s\n\t%s", msg, process->name);
111 SIMIX_display_process_status();
112 THROWF(arg_error, 0, "%s", msg);
114 xbt_dynar_free(&host->auto_restart_processes);
115 xbt_swag_free(host->process_list);
117 /* Clean host structure */
123 smx_host_t SIMIX_host_get_by_name(const char *name){
124 xbt_assert(((simix_global != NULL)
125 && (host_lib != NULL)),
126 "Environment not set yet");
128 return xbt_lib_get_elm_or_null(host_lib, name);
131 smx_host_t SIMIX_host_self(void)
133 smx_process_t process = SIMIX_process_self();
134 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
137 const char* SIMIX_pre_host_self_get_name(smx_simcall_t simcall){
138 return SIMIX_host_self_get_name();
140 /* needs to be public and without simcall because it is called
141 by exceptions and logging events */
142 const char* SIMIX_host_self_get_name(void)
144 smx_host_t host = SIMIX_host_self();
145 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
148 return SIMIX_host_get_name(host);
151 const char* SIMIX_pre_host_get_name(smx_simcall_t simcall, smx_host_t host){
152 return SIMIX_host_get_name(host);
154 const char* SIMIX_host_get_name(smx_host_t host){
155 xbt_assert((host != NULL), "Invalid parameters");
157 return sg_host_name(host);
160 xbt_dict_t SIMIX_pre_host_get_properties(smx_simcall_t simcall, smx_host_t host){
161 return SIMIX_host_get_properties(host);
163 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
164 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
166 return surf_workstation_model->extension.workstation.get_properties(host);
169 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
170 return SIMIX_host_get_speed(host);
172 double SIMIX_host_get_speed(smx_host_t host){
173 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
175 return surf_workstation_model->extension.workstation.
176 get_speed(host, 1.0);
179 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
180 return SIMIX_host_get_available_speed(host);
182 double SIMIX_host_get_available_speed(smx_host_t host){
183 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
185 return surf_workstation_model->extension.workstation.
186 get_available_speed(host);
189 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
190 return SIMIX_host_get_state(host);
192 int SIMIX_host_get_state(smx_host_t host){
193 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
195 return surf_workstation_model->extension.workstation.
199 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
200 return SIMIX_host_self_get_data();
202 void* SIMIX_host_self_get_data(void)
204 smx_host_t self = SIMIX_host_self();
205 return SIMIX_host_get_data(self);
208 void SIMIX_host_self_set_data(void *data)
210 smx_host_t self = SIMIX_host_self();
211 SIMIX_host_set_data(self, data);
214 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
215 return SIMIX_host_get_data(host);
217 void* SIMIX_host_get_data(smx_host_t host){
218 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
220 return SIMIX_host_priv(host)->data;
222 void _SIMIX_host_free_process_arg(void *);
223 void _SIMIX_host_free_process_arg(void *data)
225 smx_process_arg_t arg = *(void**)data;
230 * \brief Add a process to the list of the processes that the host will restart when it comes back
231 * This function add a process to the list of the processes that will be restarted when the host comes
232 * back. It is expected that this function is called when the host is down.
233 * The processes will only be restarted once, meaning that you will have to register the process
234 * again to restart the process again.
236 void SIMIX_host_add_auto_restart_process(smx_host_t host,
238 xbt_main_func_t code,
240 const char *hostname,
242 int argc, char **argv,
243 xbt_dict_t properties,
246 if (!SIMIX_host_priv(host)->auto_restart_processes) {
247 SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
249 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
250 arg->name = xbt_strdup(name);
253 arg->hostname = hostname;
254 arg->kill_time = kill_time;
257 arg->argv = xbt_new(char*,argc + 1);
260 for (i = 0; i < argc; i++) {
261 arg->argv[i] = xbt_strdup(argv[i]);
263 arg->argv[argc] = NULL;
265 arg->properties = properties;
266 arg->auto_restart = auto_restart;
268 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
269 && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
270 xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
271 XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
273 xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
276 * \brief Restart the list of processes that have been registered to the host
278 void SIMIX_host_restart_processes(smx_host_t host)
281 smx_process_arg_t arg;
282 xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
284 smx_process_t process;
286 XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
287 if (simix_global->create_process_function) {
288 simix_global->create_process_function(&process,
300 simcall_process_create(&process,
313 xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
316 void SIMIX_host_autorestart(smx_host_t host)
318 if(simix_global->autorestart)
319 simix_global->autorestart(host);
321 xbt_die("No function for simix_global->autorestart");
324 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
325 SIMIX_host_set_data(host, data);
327 void SIMIX_host_set_data(smx_host_t host, void *data){
328 xbt_assert((host != NULL), "Invalid parameters");
329 xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
331 SIMIX_host_priv(host)->data = data;
334 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
335 smx_host_t host, double computation_amount, double priority){
336 return SIMIX_host_execute(name, host, computation_amount, priority);
338 smx_action_t SIMIX_host_execute(const char *name,
339 smx_host_t host, double computation_amount, double priority){
341 /* alloc structures and initialize */
342 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
343 action->type = SIMIX_ACTION_EXECUTE;
344 action->name = xbt_strdup(name);
345 action->state = SIMIX_RUNNING;
346 action->execution.host = host;
349 action->category = NULL;
352 /* set surf's action */
353 if (!MC_is_active()) {
354 action->execution.surf_exec =
355 surf_workstation_model->extension.workstation.execute(host,
357 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
358 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
361 XBT_DEBUG("Create execute action %p", action);
366 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
367 int host_nb, smx_host_t *host_list,
368 double *computation_amount, double *communication_amount,
369 double amount, double rate){
370 return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
371 communication_amount, amount, rate);
373 smx_action_t SIMIX_host_parallel_execute(const char *name,
374 int host_nb, smx_host_t *host_list,
375 double *computation_amount, double *communication_amount,
376 double amount, double rate){
378 void **workstation_list = NULL;
381 /* alloc structures and initialize */
382 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
383 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
384 action->name = xbt_strdup(name);
385 action->state = SIMIX_RUNNING;
386 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
389 action->category = NULL;
392 /* set surf's action */
393 workstation_list = xbt_new0(void *, host_nb);
394 for (i = 0; i < host_nb; i++)
395 workstation_list[i] = host_list[i];
397 /* set surf's action */
398 if (!MC_is_active()) {
399 action->execution.surf_exec =
400 surf_workstation_model->extension.workstation.
401 execute_parallel_task(host_nb, workstation_list, computation_amount,
402 communication_amount, rate);
404 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
406 XBT_DEBUG("Create parallel execute action %p", action);
411 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
412 SIMIX_host_execution_destroy(action);
414 void SIMIX_host_execution_destroy(smx_action_t action){
415 XBT_DEBUG("Destroy action %p", action);
417 if (action->execution.surf_exec) {
418 surf_workstation_model->action_unref(action->execution.surf_exec);
419 action->execution.surf_exec = NULL;
421 xbt_free(action->name);
422 xbt_mallocator_release(simix_global->action_mallocator, action);
425 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
426 SIMIX_host_execution_cancel(action);
428 void SIMIX_host_execution_cancel(smx_action_t action){
429 XBT_DEBUG("Cancel action %p", action);
431 if (action->execution.surf_exec)
432 surf_workstation_model->action_cancel(action->execution.surf_exec);
435 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
436 return SIMIX_host_execution_get_remains(action);
438 double SIMIX_host_execution_get_remains(smx_action_t action){
441 if (action->state == SIMIX_RUNNING)
442 result = surf_workstation_model->get_remains(action->execution.surf_exec);
447 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
448 return SIMIX_host_execution_get_state(action);
450 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
451 return action->state;
454 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
456 return SIMIX_host_execution_set_priority(action, priority);
458 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
459 if(action->execution.surf_exec)
460 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
463 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
465 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
467 /* Associate this simcall to the action */
468 xbt_fifo_push(action->simcalls, simcall);
469 simcall->issuer->waiting_action = action;
471 /* set surf's action */
472 if (MC_is_active()) {
473 action->state = SIMIX_DONE;
474 SIMIX_execution_finish(action);
478 /* If the action is already finished then perform the error handling */
479 if (action->state != SIMIX_RUNNING)
480 SIMIX_execution_finish(action);
483 void SIMIX_host_execution_suspend(smx_action_t action)
485 if(action->execution.surf_exec)
486 surf_workstation_model->suspend(action->execution.surf_exec);
489 void SIMIX_host_execution_resume(smx_action_t action)
491 if(action->execution.surf_exec)
492 surf_workstation_model->resume(action->execution.surf_exec);
495 void SIMIX_execution_finish(smx_action_t action)
497 xbt_fifo_item_t item;
498 smx_simcall_t simcall;
500 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
502 switch (action->state) {
505 /* do nothing, action done */
506 XBT_DEBUG("SIMIX_execution_finished: execution successful");
510 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
511 simcall->issuer->context->iwannadie = 1;
512 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
516 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
517 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
521 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
524 /* check if the host is down */
525 if (surf_workstation_model->extension.
526 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
527 simcall->issuer->context->iwannadie = 1;
530 simcall->issuer->waiting_action = NULL;
531 simcall_host_execution_wait__set__result(simcall, action->state);
532 SIMIX_simcall_answer(simcall);
535 /* We no longer need it */
536 SIMIX_host_execution_destroy(action);
539 void SIMIX_post_host_execute(smx_action_t action)
541 if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
542 * for parallel tasks too */
543 surf_workstation_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
544 /* If the host running the action failed, notice it so that the asking
545 * process can be killed if it runs on that host itself */
546 action->state = SIMIX_FAILED;
547 } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
548 /* If the host running the action didn't fail, then the action was
550 action->state = SIMIX_CANCELED;
552 action->state = SIMIX_DONE;
555 if (action->execution.surf_exec) {
556 surf_workstation_model->action_unref(action->execution.surf_exec);
557 action->execution.surf_exec = NULL;
560 /* If there are simcalls associated with the action, then answer them */
561 if (xbt_fifo_size(action->simcalls)) {
562 SIMIX_execution_finish(action);
568 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
569 const char *category){
570 SIMIX_set_category(action, category);
572 void SIMIX_set_category(smx_action_t action, const char *category)
574 if (action->state != SIMIX_RUNNING) return;
575 if (action->type == SIMIX_ACTION_EXECUTE){
576 surf_workstation_model->set_category(action->execution.surf_exec, category);
577 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
578 surf_workstation_model->set_category(action->comm.surf_comm, category);