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_host, simix,
14 "Logging specific to SIMIX (hosts)");
16 static void SIMIX_execution_finish(smx_action_t action);
19 * \brief Internal function to create a SIMIX host.
20 * \param name name of the host to create
21 * \param workstation the SURF workstation to encapsulate
22 * \param data some user data (may be NULL)
24 smx_host_t SIMIX_host_create(const char *name,
25 void *workstation, void *data)
27 smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
31 smx_host->data = data;
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 return xbt_lib_get_elm_or_null(host_lib, name);
41 void SIMIX_pre_host_on(smx_host_t h)
47 * \brief Start the host if it is off
50 void SIMIX_host_on(smx_host_t h)
52 smx_host_priv_t host = (smx_host_priv_t) h;
54 xbt_assert((host != NULL), "Invalid parameters");
56 surf_model_t ws_model = surf_resource_model(h, SURF_WKS_LEVEL);
57 ws_model->extension.workstation.set_state(host, SURF_RESOURCE_ON);
59 SIMIX_host_restart_processes(h);
62 void SIMIX_pre_host_off(smx_host_t h)
68 * \brief Stop the host if it is on
71 void SIMIX_host_off(smx_host_t h)
73 smx_host_priv_t host = (smx_host_priv_t) h;
75 xbt_assert((host != NULL), "Invalid parameters");
77 /* Clean Simulator data */
78 if (xbt_swag_size(host->process_list) != 0) {
79 char *msg = xbt_strdup("Shutting down host, but it's not empty:");
81 smx_process_t process = NULL;
83 xbt_swag_foreach(process, host->process_list) {
84 tmp = bprintf("%s\n\t%s", msg, process->name);
88 SIMIX_display_process_status();
89 THROWF(arg_error, 0, "%s", msg);
91 xbt_swag_free(host->process_list);
93 surf_model_t ws_model = surf_resource_model(h, SURF_WKS_LEVEL);
94 ws_model->extension.workstation.set_state(host, SURF_RESOURCE_OFF);
98 * \brief Internal function to destroy a SIMIX host.
100 * \param h the host to destroy (a smx_host_t)
102 void SIMIX_host_destroy(void *h)
104 smx_host_priv_t host = (smx_host_priv_t) h;
106 xbt_assert((host != NULL), "Invalid parameters");
108 /* Clean Simulator data */
109 if (xbt_swag_size(host->process_list) != 0) {
110 char *msg = xbt_strdup("Shutting down host, but it's not empty:");
112 smx_process_t process = NULL;
114 xbt_swag_foreach(process, host->process_list) {
115 tmp = bprintf("%s\n\t%s", msg, process->name);
119 SIMIX_display_process_status();
120 THROWF(arg_error, 0, "%s", msg);
122 xbt_dynar_free(&host->auto_restart_processes);
123 xbt_swag_free(host->process_list);
125 /* Clean host structure */
131 // * \brief Returns a dict of all hosts.
133 // * \return List of all hosts (as a #xbt_dict_t)
135 //xbt_dict_t SIMIX_host_get_dict(void)
137 // xbt_dict_t host_dict = xbt_dict_new_homogeneous(NULL);
138 // xbt_lib_cursor_t cursor = NULL;
139 // char *name = NULL;
140 // void **host = NULL;
142 // xbt_lib_foreach(host_lib, cursor, name, host){
143 // if(host[SIMIX_HOST_LEVEL])
144 // xbt_dict_set(host_dict,name,host[SIMIX_HOST_LEVEL], NULL);
148 smx_host_t SIMIX_pre_host_get_by_name(smx_simcall_t simcall, const char *name){
149 return SIMIX_host_get_by_name(name);
151 smx_host_t SIMIX_host_get_by_name(const char *name){
152 xbt_assert(((simix_global != NULL)
153 && (host_lib != NULL)),
154 "Environment not set yet");
156 return xbt_lib_get_elm_or_null(host_lib, name);
159 smx_host_t SIMIX_host_self(void)
161 smx_process_t process = SIMIX_process_self();
162 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
165 const char* SIMIX_pre_host_self_get_name(smx_simcall_t simcall){
166 return SIMIX_host_self_get_name();
168 /* needs to be public and without simcall because it is called
169 by exceptions and logging events */
170 const char* SIMIX_host_self_get_name(void)
172 smx_host_t host = SIMIX_host_self();
173 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
176 return SIMIX_host_get_name(host);
179 const char* SIMIX_pre_host_get_name(smx_simcall_t simcall, smx_host_t host){
180 return SIMIX_host_get_name(host);
182 const char* SIMIX_host_get_name(smx_host_t host){
183 xbt_assert((host != NULL), "Invalid parameters");
185 return sg_host_name(host);
188 xbt_dict_t SIMIX_pre_host_get_properties(smx_simcall_t simcall, smx_host_t host){
189 return SIMIX_host_get_properties(host);
191 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
192 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
194 surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
195 return ws_model->extension.workstation.get_properties(host);
198 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
199 return SIMIX_host_get_speed(host);
201 double SIMIX_host_get_speed(smx_host_t host){
202 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
204 surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
205 return ws_model->extension.workstation.get_speed(host, 1.0);
208 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
209 return SIMIX_host_get_available_speed(host);
211 double SIMIX_host_get_available_speed(smx_host_t host){
212 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
214 surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
215 return ws_model->extension.workstation.get_available_speed(host);
218 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
219 return SIMIX_host_get_state(host);
221 int SIMIX_host_get_state(smx_host_t host){
222 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
224 surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
225 return ws_model->extension.workstation.get_state(host);
228 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
229 return SIMIX_host_self_get_data();
231 void* SIMIX_host_self_get_data(void)
233 smx_host_t self = SIMIX_host_self();
234 return SIMIX_host_get_data(self);
237 void SIMIX_host_self_set_data(void *data)
239 smx_host_t self = SIMIX_host_self();
240 SIMIX_host_set_data(self, data);
243 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
244 return SIMIX_host_get_data(host);
246 void* SIMIX_host_get_data(smx_host_t host){
247 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
249 return SIMIX_host_priv(host)->data;
251 void _SIMIX_host_free_process_arg(void *);
252 void _SIMIX_host_free_process_arg(void *data)
254 smx_process_arg_t arg = *(void**)data;
259 * \brief Add a process to the list of the processes that the host will restart when it comes back
260 * This function add a process to the list of the processes that will be restarted when the host comes
261 * back. It is expected that this function is called when the host is down.
262 * The processes will only be restarted once, meaning that you will have to register the process
263 * again to restart the process again.
265 void SIMIX_host_add_auto_restart_process(smx_host_t host,
267 xbt_main_func_t code,
269 const char *hostname,
271 int argc, char **argv,
272 xbt_dict_t properties,
275 if (!SIMIX_host_priv(host)->auto_restart_processes) {
276 SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
278 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
279 arg->name = xbt_strdup(name);
282 arg->hostname = hostname;
283 arg->kill_time = kill_time;
286 arg->argv = xbt_new(char*,argc + 1);
289 for (i = 0; i < argc; i++) {
290 arg->argv[i] = xbt_strdup(argv[i]);
292 arg->argv[argc] = NULL;
294 arg->properties = properties;
295 arg->auto_restart = auto_restart;
297 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
298 && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
299 xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
300 XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
302 xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
305 * \brief Restart the list of processes that have been registered to the host
307 void SIMIX_host_restart_processes(smx_host_t host)
310 smx_process_arg_t arg;
311 xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
313 smx_process_t process;
315 XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
316 if (simix_global->create_process_function) {
317 simix_global->create_process_function(&process,
329 simcall_process_create(&process,
342 xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
345 void SIMIX_host_autorestart(smx_host_t host)
347 if(simix_global->autorestart)
348 simix_global->autorestart(host);
350 xbt_die("No function for simix_global->autorestart");
353 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
354 SIMIX_host_set_data(host, data);
356 void SIMIX_host_set_data(smx_host_t host, void *data){
357 xbt_assert((host != NULL), "Invalid parameters");
358 xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
360 SIMIX_host_priv(host)->data = data;
363 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
364 smx_host_t host, double computation_amount, double priority, double bound){
365 return SIMIX_host_execute(name, host, computation_amount, priority, bound);
367 smx_action_t SIMIX_host_execute(const char *name,
368 smx_host_t host, double computation_amount, double priority, double bound){
370 /* alloc structures and initialize */
371 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
372 action->type = SIMIX_ACTION_EXECUTE;
373 action->name = xbt_strdup(name);
374 action->state = SIMIX_RUNNING;
375 action->execution.host = host;
378 action->category = NULL;
381 surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
382 /* set surf's action */
383 if (!MC_is_active()) {
384 action->execution.surf_exec = ws_model->extension.workstation.execute(host, computation_amount);
385 ws_model->action_data_set(action->execution.surf_exec, action);
386 ws_model->set_priority(action->execution.surf_exec, priority);
387 ws_model->set_bound(action->execution.surf_exec, bound);
390 XBT_DEBUG("Create execute action %p", action);
395 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
396 int host_nb, smx_host_t *host_list,
397 double *computation_amount, double *communication_amount,
398 double amount, double rate){
399 return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
400 communication_amount, amount, rate);
402 smx_action_t SIMIX_host_parallel_execute(const char *name,
403 int host_nb, smx_host_t *host_list,
404 double *computation_amount, double *communication_amount,
405 double amount, double rate){
407 void **workstation_list = NULL;
410 /* alloc structures and initialize */
411 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
412 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
413 action->name = xbt_strdup(name);
414 action->state = SIMIX_RUNNING;
415 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
418 action->category = NULL;
421 /* set surf's action */
422 workstation_list = xbt_new0(void *, host_nb);
423 for (i = 0; i < host_nb; i++)
424 workstation_list[i] = host_list[i];
427 /* FIXME: what happens if host_list contains VMs and PMs. If
428 * execute_parallel_task() does not change the state of the model, we can mix
430 surf_model_t ws_model = surf_resource_model(host_list[0], SURF_WKS_LEVEL);
431 for (i = 1; i < host_nb; i++) {
432 surf_model_t ws_model_tmp = surf_resource_model(host_list[i], SURF_WKS_LEVEL);
433 if (ws_model_tmp != ws_model) {
434 XBT_CRITICAL("mixing VMs and PMs is not supported");
439 /* set surf's action */
440 if (!MC_is_active()) {
441 action->execution.surf_exec =
442 ws_model->extension.workstation.
443 execute_parallel_task(host_nb, workstation_list, computation_amount,
444 communication_amount, rate);
446 ws_model->action_data_set(action->execution.surf_exec, action);
448 XBT_DEBUG("Create parallel execute action %p", action);
453 static surf_model_t get_ws_model_from_action(smx_action_t action)
455 xbt_assert(action->type == SIMIX_ACTION_EXECUTE);
456 smx_host_t host = action->execution.host;
457 surf_model_t model = surf_resource_model(host, SURF_WKS_LEVEL);
459 xbt_assert((model == surf_workstation_model) || (model == surf_vm_workstation_model));
464 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
465 SIMIX_host_execution_destroy(action);
467 void SIMIX_host_execution_destroy(smx_action_t action){
468 XBT_DEBUG("Destroy action %p", action);
470 surf_model_t ws_model = get_ws_model_from_action(action);
472 if (action->execution.surf_exec) {
473 ws_model->action_unref(action->execution.surf_exec);
474 action->execution.surf_exec = NULL;
476 xbt_free(action->name);
477 xbt_mallocator_release(simix_global->action_mallocator, action);
480 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
481 SIMIX_host_execution_cancel(action);
483 void SIMIX_host_execution_cancel(smx_action_t action){
484 XBT_DEBUG("Cancel action %p", action);
486 surf_model_t ws_model = get_ws_model_from_action(action);
488 if (action->execution.surf_exec)
489 ws_model->action_cancel(action->execution.surf_exec);
492 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
493 return SIMIX_host_execution_get_remains(action);
495 double SIMIX_host_execution_get_remains(smx_action_t action){
497 surf_model_t ws_model = get_ws_model_from_action(action);
499 if (action->state == SIMIX_RUNNING)
500 result = ws_model->get_remains(action->execution.surf_exec);
505 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
506 return SIMIX_host_execution_get_state(action);
508 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
509 return action->state;
512 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
514 SIMIX_host_execution_set_priority(action, priority);
516 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
517 surf_model_t ws_model = get_ws_model_from_action(action);
519 if(action->execution.surf_exec)
520 ws_model->set_priority(action->execution.surf_exec, priority);
523 void SIMIX_pre_host_execution_set_bound(smx_simcall_t simcall, smx_action_t action,
525 SIMIX_host_execution_set_bound(action, bound);
527 void SIMIX_host_execution_set_bound(smx_action_t action, double bound){
528 surf_model_t ws_model = get_ws_model_from_action(action);
530 if(action->execution.surf_exec)
531 ws_model->set_bound(action->execution.surf_exec, bound);
534 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
536 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
538 /* Associate this simcall to the action */
539 xbt_fifo_push(action->simcalls, simcall);
540 simcall->issuer->waiting_action = action;
542 /* set surf's action */
543 if (MC_is_active()) {
544 action->state = SIMIX_DONE;
545 SIMIX_execution_finish(action);
549 /* If the action is already finished then perform the error handling */
550 if (action->state != SIMIX_RUNNING)
551 SIMIX_execution_finish(action);
554 void SIMIX_host_execution_suspend(smx_action_t action)
556 surf_model_t ws_model = get_ws_model_from_action(action);
558 if(action->execution.surf_exec)
559 ws_model->suspend(action->execution.surf_exec);
562 void SIMIX_host_execution_resume(smx_action_t action)
564 surf_model_t ws_model = get_ws_model_from_action(action);
566 if(action->execution.surf_exec)
567 ws_model->resume(action->execution.surf_exec);
570 void SIMIX_execution_finish(smx_action_t action)
572 xbt_fifo_item_t item;
573 smx_simcall_t simcall;
574 surf_model_t ws_model = get_ws_model_from_action(action);
576 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
578 switch (action->state) {
581 /* do nothing, action done */
582 XBT_DEBUG("SIMIX_execution_finished: execution successful");
586 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
587 simcall->issuer->context->iwannadie = 1;
588 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
592 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
593 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
597 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
600 /* check if the host is down */
601 if (ws_model->extension.workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
602 simcall->issuer->context->iwannadie = 1;
605 simcall->issuer->waiting_action = NULL;
606 simcall_host_execution_wait__set__result(simcall, action->state);
607 SIMIX_simcall_answer(simcall);
610 /* We no longer need it */
611 SIMIX_host_execution_destroy(action);
615 void SIMIX_post_host_execute(smx_action_t action)
617 surf_model_t ws_model = get_ws_model_from_action(action);
619 if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
620 * for parallel tasks too */
621 ws_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
622 /* If the host running the action failed, notice it so that the asking
623 * process can be killed if it runs on that host itself */
624 action->state = SIMIX_FAILED;
625 } else if (ws_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
626 /* If the host running the action didn't fail, then the action was
628 action->state = SIMIX_CANCELED;
630 action->state = SIMIX_DONE;
633 if (action->execution.surf_exec) {
634 ws_model->action_unref(action->execution.surf_exec);
635 action->execution.surf_exec = NULL;
638 /* If there are simcalls associated with the action, then answer them */
639 if (xbt_fifo_size(action->simcalls)) {
640 SIMIX_execution_finish(action);
646 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
647 const char *category){
648 SIMIX_set_category(action, category);
650 void SIMIX_set_category(smx_action_t action, const char *category)
652 surf_model_t ws_model = get_ws_model_from_action(action);
654 if (action->state != SIMIX_RUNNING) return;
655 if (action->type == SIMIX_ACTION_EXECUTE){
656 ws_model->set_category(action->execution.surf_exec, category);
657 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
658 ws_model->set_category(action->comm.surf_comm, category);
665 * \brief Function to get the parameters of the given the SIMIX host.
667 * \param host the host to get_phys_host (a smx_host_t)
668 * \param param the parameter object space to be overwritten (a ws_params_t)
670 void SIMIX_host_get_params(smx_host_t ind_vm, ws_params_t params)
672 /* jump to ws_get_params(). */
673 surf_workstation_model->extension.workstation.get_params(ind_vm, params);
676 void SIMIX_pre_host_get_params(smx_simcall_t simcall, smx_host_t ind_vm, ws_params_t params)
678 SIMIX_host_get_params(ind_vm, params);
679 SIMIX_simcall_answer(simcall);
682 void SIMIX_host_set_params(smx_host_t ind_vm, ws_params_t params)
684 /* jump to ws_set_params(). */
685 surf_workstation_model->extension.workstation.set_params(ind_vm, params);
688 void SIMIX_pre_host_set_params(smx_simcall_t simcall, smx_host_t ind_vm, ws_params_t params)
690 SIMIX_host_set_params(ind_vm, params);
691 SIMIX_simcall_answer(simcall);