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);
46 smx_host_t SIMIX_pre_vm_create(smx_simcall_t simcall, const char *name, smx_host_t phys_host){
47 return SIMIX_vm_create(name, phys_host);
51 /* **** start a VM **** */
52 int __can_be_started(smx_host_t vm){
53 // TODO add checking code related to overcommitment or not.
56 void SIMIX_vm_start(smx_host_t vm){
58 //TODO only start the VM if you can
59 if (can_be_started(vm))
60 SIMIX_set_vm_state(vm, msg_vm_state_running);
62 THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(vm));
65 void SIMIX_pre_vm_start(smx_simcall_t simcall, smx_host_t vm){
69 /* ***** set/get state of a VM ***** */
70 void SIMIX_set_vm_state(smx_host_t vm, int state){
73 void SIMIX_prev_set_vm_state(smx_host_t vm, int state){
74 SIMIX_set_vm_state(vm, state);
77 int SIMIX_get_vm_state(smx_host_t vm){
78 return surf_vm_workstation_model->extension.vm_workstation.get_state(vm);
80 int SIMIX_pre_vm_state(smx_host_t vm){
81 return SIMIX_get_vm_state(vm);
85 * \brief Function to destroy a SIMIX VM host.
87 * \param host the vm host to destroy (a smx_host_t)
89 void SIMIX_vm_destroy(smx_host_t host)
91 /* this code basically performs a similar thing like SIMIX_host_destroy() */
93 xbt_assert((host != NULL), "Invalid parameters");
94 char *hostname = host->key;
96 smx_host_priv_t host_priv = SIMIX_host_priv(host);
98 /* this will call the registered callback function, i.e., SIMIX_host_destroy(). */
99 xbt_lib_unset(host_lib, name, SIMIX_HOST_LEVEL)
101 /* jump to vm_ws_destroy(). The surf level resource will be freed. */
102 surf_vm_workstation_model->extension.vm_workstation.destroy(hostname);
105 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t vm){
109 smx_host_t SIMIX_host_get_by_name(const char *name){
110 xbt_assert(((simix_global != NULL)
111 && (host_lib != NULL)),
112 "Environment not set yet");
114 return xbt_lib_get_elm_or_null(host_lib, name);
117 smx_host_t SIMIX_host_self(void)
119 smx_process_t process = SIMIX_process_self();
120 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
123 const char* SIMIX_pre_host_self_get_name(smx_simcall_t simcall){
124 return SIMIX_host_self_get_name();
126 /* needs to be public and without simcall because it is called
127 by exceptions and logging events */
128 const char* SIMIX_host_self_get_name(void)
130 smx_host_t host = SIMIX_host_self();
131 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
134 return SIMIX_host_get_name(host);
137 const char* SIMIX_pre_host_get_name(smx_simcall_t simcall, smx_host_t host){
138 return SIMIX_host_get_name(host);
140 const char* SIMIX_host_get_name(smx_host_t host){
141 xbt_assert((host != NULL), "Invalid parameters");
143 return sg_host_name(host);
146 xbt_dict_t SIMIX_pre_host_get_properties(smx_simcall_t simcall, smx_host_t host){
147 return SIMIX_host_get_properties(host);
149 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
150 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
152 return surf_workstation_model->extension.workstation.get_properties(host);
155 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
156 return SIMIX_host_get_speed(host);
158 double SIMIX_host_get_speed(smx_host_t host){
159 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
161 return surf_workstation_model->extension.workstation.
162 get_speed(host, 1.0);
165 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
166 return SIMIX_host_get_available_speed(host);
168 double SIMIX_host_get_available_speed(smx_host_t host){
169 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
171 return surf_workstation_model->extension.workstation.
172 get_available_speed(host);
175 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
176 return SIMIX_host_get_state(host);
178 int SIMIX_host_get_state(smx_host_t host){
179 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
181 return surf_workstation_model->extension.workstation.
185 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
186 return SIMIX_host_self_get_data();
188 void* SIMIX_host_self_get_data(void)
190 smx_host_t self = SIMIX_host_self();
191 return SIMIX_host_get_data(self);
194 void SIMIX_host_self_set_data(void *data)
196 smx_host_t self = SIMIX_host_self();
197 SIMIX_host_set_data(self, data);
200 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
201 return SIMIX_host_get_data(host);
203 void* SIMIX_host_get_data(smx_host_t host){
204 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
206 return SIMIX_host_priv(host)->data;
208 void _SIMIX_host_free_process_arg(void *);
209 void _SIMIX_host_free_process_arg(void *data)
211 smx_process_arg_t arg = *(void**)data;
216 * \brief Add a process to the list of the processes that the host will restart when it comes back
217 * This function add a process to the list of the processes that will be restarted when the host comes
218 * back. It is expected that this function is called when the host is down.
219 * The processes will only be restarted once, meaning that you will have to register the process
220 * again to restart the process again.
222 void SIMIX_host_add_auto_restart_process(smx_host_t host,
224 xbt_main_func_t code,
226 const char *hostname,
228 int argc, char **argv,
229 xbt_dict_t properties,
232 if (!SIMIX_host_priv(host)->auto_restart_processes) {
233 SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
235 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
236 arg->name = xbt_strdup(name);
239 arg->hostname = hostname;
240 arg->kill_time = kill_time;
243 arg->argv = xbt_new(char*,argc + 1);
246 for (i = 0; i < argc; i++) {
247 arg->argv[i] = xbt_strdup(argv[i]);
249 arg->argv[argc] = NULL;
251 arg->properties = properties;
252 arg->auto_restart = auto_restart;
254 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
255 && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
256 xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
257 XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
259 xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
262 * \brief Restart the list of processes that have been registered to the host
264 void SIMIX_host_restart_processes(smx_host_t host)
267 smx_process_arg_t arg;
268 xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
270 smx_process_t process;
272 XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
273 if (simix_global->create_process_function) {
274 simix_global->create_process_function(&process,
286 simcall_process_create(&process,
299 xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
302 void SIMIX_host_autorestart(smx_host_t host)
304 if(simix_global->autorestart)
305 simix_global->autorestart(host);
307 xbt_die("No function for simix_global->autorestart");
310 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
311 SIMIX_host_set_data(host, data);
313 void SIMIX_host_set_data(smx_host_t host, void *data){
314 xbt_assert((host != NULL), "Invalid parameters");
315 xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
317 SIMIX_host_priv(host)->data = data;
320 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
321 smx_host_t host, double computation_amount, double priority){
322 return SIMIX_host_execute(name, host, computation_amount, priority);
324 smx_action_t SIMIX_host_execute(const char *name,
325 smx_host_t host, double computation_amount, double priority){
327 /* alloc structures and initialize */
328 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
329 action->type = SIMIX_ACTION_EXECUTE;
330 action->name = xbt_strdup(name);
331 action->state = SIMIX_RUNNING;
332 action->execution.host = host;
335 action->category = NULL;
338 /* set surf's action */
339 if (!MC_is_active()) {
340 action->execution.surf_exec =
341 surf_workstation_model->extension.workstation.execute(host,
343 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
344 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
347 XBT_DEBUG("Create execute action %p", action);
352 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
353 int host_nb, smx_host_t *host_list,
354 double *computation_amount, double *communication_amount,
355 double amount, double rate){
356 return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
357 communication_amount, amount, rate);
359 smx_action_t SIMIX_host_parallel_execute(const char *name,
360 int host_nb, smx_host_t *host_list,
361 double *computation_amount, double *communication_amount,
362 double amount, double rate){
364 void **workstation_list = NULL;
367 /* alloc structures and initialize */
368 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
369 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
370 action->name = xbt_strdup(name);
371 action->state = SIMIX_RUNNING;
372 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
375 action->category = NULL;
378 /* set surf's action */
379 workstation_list = xbt_new0(void *, host_nb);
380 for (i = 0; i < host_nb; i++)
381 workstation_list[i] = host_list[i];
383 /* set surf's action */
384 if (!MC_is_active()) {
385 action->execution.surf_exec =
386 surf_workstation_model->extension.workstation.
387 execute_parallel_task(host_nb, workstation_list, computation_amount,
388 communication_amount, rate);
390 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
392 XBT_DEBUG("Create parallel execute action %p", action);
397 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
398 SIMIX_host_execution_destroy(action);
400 void SIMIX_host_execution_destroy(smx_action_t action){
401 XBT_DEBUG("Destroy action %p", action);
403 if (action->execution.surf_exec) {
404 surf_workstation_model->action_unref(action->execution.surf_exec);
405 action->execution.surf_exec = NULL;
407 xbt_free(action->name);
408 xbt_mallocator_release(simix_global->action_mallocator, action);
411 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
412 SIMIX_host_execution_cancel(action);
414 void SIMIX_host_execution_cancel(smx_action_t action){
415 XBT_DEBUG("Cancel action %p", action);
417 if (action->execution.surf_exec)
418 surf_workstation_model->action_cancel(action->execution.surf_exec);
421 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
422 return SIMIX_host_execution_get_remains(action);
424 double SIMIX_host_execution_get_remains(smx_action_t action){
427 if (action->state == SIMIX_RUNNING)
428 result = surf_workstation_model->get_remains(action->execution.surf_exec);
433 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
434 return SIMIX_host_execution_get_state(action);
436 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
437 return action->state;
440 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
442 return SIMIX_host_execution_set_priority(action, priority);
444 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
445 if(action->execution.surf_exec)
446 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
449 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
451 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
453 /* Associate this simcall to the action */
454 xbt_fifo_push(action->simcalls, simcall);
455 simcall->issuer->waiting_action = action;
457 /* set surf's action */
458 if (MC_is_active()) {
459 action->state = SIMIX_DONE;
460 SIMIX_execution_finish(action);
464 /* If the action is already finished then perform the error handling */
465 if (action->state != SIMIX_RUNNING)
466 SIMIX_execution_finish(action);
469 void SIMIX_host_execution_suspend(smx_action_t action)
471 if(action->execution.surf_exec)
472 surf_workstation_model->suspend(action->execution.surf_exec);
475 void SIMIX_host_execution_resume(smx_action_t action)
477 if(action->execution.surf_exec)
478 surf_workstation_model->resume(action->execution.surf_exec);
481 void SIMIX_execution_finish(smx_action_t action)
483 xbt_fifo_item_t item;
484 smx_simcall_t simcall;
486 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
488 switch (action->state) {
491 /* do nothing, action done */
492 XBT_DEBUG("SIMIX_execution_finished: execution successful");
496 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
497 simcall->issuer->context->iwannadie = 1;
498 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
502 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
503 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
507 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
510 /* check if the host is down */
511 if (surf_workstation_model->extension.
512 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
513 simcall->issuer->context->iwannadie = 1;
516 simcall->issuer->waiting_action = NULL;
517 simcall_host_execution_wait__set__result(simcall, action->state);
518 SIMIX_simcall_answer(simcall);
521 /* We no longer need it */
522 SIMIX_host_execution_destroy(action);
525 void SIMIX_post_host_execute(smx_action_t action)
527 if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
528 * for parallel tasks too */
529 surf_workstation_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
530 /* If the host running the action failed, notice it so that the asking
531 * process can be killed if it runs on that host itself */
532 action->state = SIMIX_FAILED;
533 } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
534 /* If the host running the action didn't fail, then the action was
536 action->state = SIMIX_CANCELED;
538 action->state = SIMIX_DONE;
541 if (action->execution.surf_exec) {
542 surf_workstation_model->action_unref(action->execution.surf_exec);
543 action->execution.surf_exec = NULL;
546 /* If there are simcalls associated with the action, then answer them */
547 if (xbt_fifo_size(action->simcalls)) {
548 SIMIX_execution_finish(action);
554 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
555 const char *category){
556 SIMIX_set_category(action, category);
558 void SIMIX_set_category(smx_action_t action, const char *category)
560 if (action->state != SIMIX_RUNNING) return;
561 if (action->type == SIMIX_ACTION_EXECUTE){
562 surf_workstation_model->set_category(action->execution.surf_exec, category);
563 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
564 surf_workstation_model->set_category(action->comm.surf_comm, category);