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_host_destroy(void *h)
89 smx_host_priv_t host = (smx_host_priv_t) h;
91 xbt_assert((host != NULL), "Invalid parameters");
93 /* Clean Simulator data */
94 if (xbt_swag_size(host->process_list) != 0) {
96 bprintf("Shutting down host, but it's not empty:");
98 smx_process_t process = NULL;
100 xbt_swag_foreach(process, host->process_list) {
101 tmp = bprintf("%s\n\t%s", msg, process->name);
105 SIMIX_display_process_status();
106 THROWF(arg_error, 0, "%s", msg);
108 xbt_dynar_free(&host->auto_restart_processes);
109 xbt_swag_free(host->process_list);
111 /* Clean host structure */
117 smx_host_t SIMIX_host_get_by_name(const char *name){
118 xbt_assert(((simix_global != NULL)
119 && (host_lib != NULL)),
120 "Environment not set yet");
122 return xbt_lib_get_elm_or_null(host_lib, name);
125 smx_host_t SIMIX_host_self(void)
127 smx_process_t process = SIMIX_process_self();
128 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
131 const char* SIMIX_pre_host_self_get_name(smx_simcall_t simcall){
132 return SIMIX_host_self_get_name();
134 /* needs to be public and without simcall because it is called
135 by exceptions and logging events */
136 const char* SIMIX_host_self_get_name(void)
138 smx_host_t host = SIMIX_host_self();
139 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
142 return SIMIX_host_get_name(host);
145 const char* SIMIX_pre_host_get_name(smx_simcall_t simcall, smx_host_t host){
146 return SIMIX_host_get_name(host);
148 const char* SIMIX_host_get_name(smx_host_t host){
149 xbt_assert((host != NULL), "Invalid parameters");
151 return sg_host_name(host);
154 xbt_dict_t SIMIX_pre_host_get_properties(smx_simcall_t simcall, smx_host_t host){
155 return SIMIX_host_get_properties(host);
157 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
158 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
160 return surf_workstation_model->extension.workstation.get_properties(host);
163 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
164 return SIMIX_host_get_speed(host);
166 double SIMIX_host_get_speed(smx_host_t host){
167 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
169 return surf_workstation_model->extension.workstation.
170 get_speed(host, 1.0);
173 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
174 return SIMIX_host_get_available_speed(host);
176 double SIMIX_host_get_available_speed(smx_host_t host){
177 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
179 return surf_workstation_model->extension.workstation.
180 get_available_speed(host);
183 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
184 return SIMIX_host_get_state(host);
186 int SIMIX_host_get_state(smx_host_t host){
187 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
189 return surf_workstation_model->extension.workstation.
193 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
194 return SIMIX_host_self_get_data();
196 void* SIMIX_host_self_get_data(void)
198 smx_host_t self = SIMIX_host_self();
199 return SIMIX_host_get_data(self);
202 void SIMIX_host_self_set_data(void *data)
204 smx_host_t self = SIMIX_host_self();
205 SIMIX_host_set_data(self, data);
208 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
209 return SIMIX_host_get_data(host);
211 void* SIMIX_host_get_data(smx_host_t host){
212 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
214 return SIMIX_host_priv(host)->data;
216 void _SIMIX_host_free_process_arg(void *);
217 void _SIMIX_host_free_process_arg(void *data)
219 smx_process_arg_t arg = *(void**)data;
224 * \brief Add a process to the list of the processes that the host will restart when it comes back
225 * This function add a process to the list of the processes that will be restarted when the host comes
226 * back. It is expected that this function is called when the host is down.
227 * The processes will only be restarted once, meaning that you will have to register the process
228 * again to restart the process again.
230 void SIMIX_host_add_auto_restart_process(smx_host_t host,
232 xbt_main_func_t code,
234 const char *hostname,
236 int argc, char **argv,
237 xbt_dict_t properties,
240 if (!SIMIX_host_priv(host)->auto_restart_processes) {
241 SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
243 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
244 arg->name = xbt_strdup(name);
247 arg->hostname = hostname;
248 arg->kill_time = kill_time;
251 arg->argv = xbt_new(char*,argc + 1);
254 for (i = 0; i < argc; i++) {
255 arg->argv[i] = xbt_strdup(argv[i]);
257 arg->argv[argc] = NULL;
259 arg->properties = properties;
260 arg->auto_restart = auto_restart;
262 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
263 && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
264 xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
265 XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
267 xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
270 * \brief Restart the list of processes that have been registered to the host
272 void SIMIX_host_restart_processes(smx_host_t host)
275 smx_process_arg_t arg;
276 xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
278 smx_process_t process;
280 XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
281 if (simix_global->create_process_function) {
282 simix_global->create_process_function(&process,
294 simcall_process_create(&process,
307 xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
310 void SIMIX_host_autorestart(smx_host_t host)
312 if(simix_global->autorestart)
313 simix_global->autorestart(host);
315 xbt_die("No function for simix_global->autorestart");
318 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
319 SIMIX_host_set_data(host, data);
321 void SIMIX_host_set_data(smx_host_t host, void *data){
322 xbt_assert((host != NULL), "Invalid parameters");
323 xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
325 SIMIX_host_priv(host)->data = data;
328 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
329 smx_host_t host, double computation_amount, double priority){
330 return SIMIX_host_execute(name, host, computation_amount, priority);
332 smx_action_t SIMIX_host_execute(const char *name,
333 smx_host_t host, double computation_amount, double priority){
335 /* alloc structures and initialize */
336 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
337 action->type = SIMIX_ACTION_EXECUTE;
338 action->name = xbt_strdup(name);
339 action->state = SIMIX_RUNNING;
340 action->execution.host = host;
343 action->category = NULL;
346 /* set surf's action */
347 if (!MC_is_active()) {
348 action->execution.surf_exec =
349 surf_workstation_model->extension.workstation.execute(host,
351 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
352 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
355 XBT_DEBUG("Create execute action %p", action);
360 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
361 int host_nb, smx_host_t *host_list,
362 double *computation_amount, double *communication_amount,
363 double amount, double rate){
364 return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
365 communication_amount, amount, rate);
367 smx_action_t SIMIX_host_parallel_execute(const char *name,
368 int host_nb, smx_host_t *host_list,
369 double *computation_amount, double *communication_amount,
370 double amount, double rate){
372 void **workstation_list = NULL;
375 /* alloc structures and initialize */
376 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
377 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
378 action->name = xbt_strdup(name);
379 action->state = SIMIX_RUNNING;
380 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
383 action->category = NULL;
386 /* set surf's action */
387 workstation_list = xbt_new0(void *, host_nb);
388 for (i = 0; i < host_nb; i++)
389 workstation_list[i] = host_list[i];
391 /* set surf's action */
392 if (!MC_is_active()) {
393 action->execution.surf_exec =
394 surf_workstation_model->extension.workstation.
395 execute_parallel_task(host_nb, workstation_list, computation_amount,
396 communication_amount, rate);
398 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
400 XBT_DEBUG("Create parallel execute action %p", action);
405 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
406 SIMIX_host_execution_destroy(action);
408 void SIMIX_host_execution_destroy(smx_action_t action){
409 XBT_DEBUG("Destroy action %p", action);
411 if (action->execution.surf_exec) {
412 surf_workstation_model->action_unref(action->execution.surf_exec);
413 action->execution.surf_exec = NULL;
415 xbt_free(action->name);
416 xbt_mallocator_release(simix_global->action_mallocator, action);
419 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
420 SIMIX_host_execution_cancel(action);
422 void SIMIX_host_execution_cancel(smx_action_t action){
423 XBT_DEBUG("Cancel action %p", action);
425 if (action->execution.surf_exec)
426 surf_workstation_model->action_cancel(action->execution.surf_exec);
429 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
430 return SIMIX_host_execution_get_remains(action);
432 double SIMIX_host_execution_get_remains(smx_action_t action){
435 if (action->state == SIMIX_RUNNING)
436 result = surf_workstation_model->get_remains(action->execution.surf_exec);
441 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
442 return SIMIX_host_execution_get_state(action);
444 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
445 return action->state;
448 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
450 return SIMIX_host_execution_set_priority(action, priority);
452 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
453 if(action->execution.surf_exec)
454 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
457 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
459 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
461 /* Associate this simcall to the action */
462 xbt_fifo_push(action->simcalls, simcall);
463 simcall->issuer->waiting_action = action;
465 /* set surf's action */
466 if (MC_is_active()) {
467 action->state = SIMIX_DONE;
468 SIMIX_execution_finish(action);
472 /* If the action is already finished then perform the error handling */
473 if (action->state != SIMIX_RUNNING)
474 SIMIX_execution_finish(action);
477 void SIMIX_host_execution_suspend(smx_action_t action)
479 if(action->execution.surf_exec)
480 surf_workstation_model->suspend(action->execution.surf_exec);
483 void SIMIX_host_execution_resume(smx_action_t action)
485 if(action->execution.surf_exec)
486 surf_workstation_model->resume(action->execution.surf_exec);
489 void SIMIX_execution_finish(smx_action_t action)
491 xbt_fifo_item_t item;
492 smx_simcall_t simcall;
494 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
496 switch (action->state) {
499 /* do nothing, action done */
500 XBT_DEBUG("SIMIX_execution_finished: execution successful");
504 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
505 simcall->issuer->context->iwannadie = 1;
506 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
510 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
511 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
515 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
518 /* check if the host is down */
519 if (surf_workstation_model->extension.
520 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
521 simcall->issuer->context->iwannadie = 1;
524 simcall->issuer->waiting_action = NULL;
525 simcall_host_execution_wait__set__result(simcall, action->state);
526 SIMIX_simcall_answer(simcall);
529 /* We no longer need it */
530 SIMIX_host_execution_destroy(action);
533 void SIMIX_post_host_execute(smx_action_t action)
535 if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
536 * for parallel tasks too */
537 surf_workstation_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
538 /* If the host running the action failed, notice it so that the asking
539 * process can be killed if it runs on that host itself */
540 action->state = SIMIX_FAILED;
541 } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
542 /* If the host running the action didn't fail, then the action was
544 action->state = SIMIX_CANCELED;
546 action->state = SIMIX_DONE;
549 if (action->execution.surf_exec) {
550 surf_workstation_model->action_unref(action->execution.surf_exec);
551 action->execution.surf_exec = NULL;
554 /* If there are simcalls associated with the action, then answer them */
555 if (xbt_fifo_size(action->simcalls)) {
556 SIMIX_execution_finish(action);
562 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
563 const char *category){
564 SIMIX_set_category(action, category);
566 void SIMIX_set_category(smx_action_t action, const char *category)
568 if (action->state != SIMIX_RUNNING) return;
569 if (action->type == SIMIX_ACTION_EXECUTE){
570 surf_workstation_model->set_category(action->execution.surf_exec, category);
571 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
572 surf_workstation_model->set_category(action->comm.surf_comm, category);