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_simcall_t simcall, 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_simcall_t simcall, 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 int SIMIX_pre_host_get_core(smx_simcall_t simcall, smx_host_t host){
209 return SIMIX_host_get_core(host);
211 int SIMIX_host_get_core(smx_host_t host){
212 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
214 return surf_workstation_model->extension.workstation.
220 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
221 return SIMIX_host_get_available_speed(host);
223 double SIMIX_host_get_available_speed(smx_host_t host){
224 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
226 surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
227 return ws_model->extension.workstation.get_available_speed(host);
230 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
231 return SIMIX_host_get_state(host);
233 int SIMIX_host_get_state(smx_host_t host){
234 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
236 surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
237 return ws_model->extension.workstation.get_state(host);
240 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
241 return SIMIX_host_self_get_data();
243 void* SIMIX_host_self_get_data(void)
245 smx_host_t self = SIMIX_host_self();
246 return SIMIX_host_get_data(self);
249 void SIMIX_host_self_set_data(void *data)
251 smx_host_t self = SIMIX_host_self();
252 SIMIX_host_set_data(self, data);
255 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
256 return SIMIX_host_get_data(host);
258 void* SIMIX_host_get_data(smx_host_t host){
259 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
261 return SIMIX_host_priv(host)->data;
263 void _SIMIX_host_free_process_arg(void *);
264 void _SIMIX_host_free_process_arg(void *data)
266 smx_process_arg_t arg = *(void**)data;
271 * \brief Add a process to the list of the processes that the host will restart when it comes back
272 * This function add a process to the list of the processes that will be restarted when the host comes
273 * back. It is expected that this function is called when the host is down.
274 * The processes will only be restarted once, meaning that you will have to register the process
275 * again to restart the process again.
277 void SIMIX_host_add_auto_restart_process(smx_host_t host,
279 xbt_main_func_t code,
281 const char *hostname,
283 int argc, char **argv,
284 xbt_dict_t properties,
287 if (!SIMIX_host_priv(host)->auto_restart_processes) {
288 SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
290 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
291 arg->name = xbt_strdup(name);
294 arg->hostname = hostname;
295 arg->kill_time = kill_time;
298 arg->argv = xbt_new(char*,argc + 1);
301 for (i = 0; i < argc; i++) {
302 arg->argv[i] = xbt_strdup(argv[i]);
304 arg->argv[argc] = NULL;
306 arg->properties = properties;
307 arg->auto_restart = auto_restart;
309 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
310 && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
311 xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
312 XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
314 xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
317 * \brief Restart the list of processes that have been registered to the host
319 void SIMIX_host_restart_processes(smx_host_t host)
322 smx_process_arg_t arg;
323 xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
325 smx_process_t process;
327 XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
328 if (simix_global->create_process_function) {
329 simix_global->create_process_function(&process,
341 simcall_process_create(&process,
354 xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
357 void SIMIX_host_autorestart(smx_host_t host)
359 if(simix_global->autorestart)
360 simix_global->autorestart(host);
362 xbt_die("No function for simix_global->autorestart");
365 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
366 SIMIX_host_set_data(host, data);
368 void SIMIX_host_set_data(smx_host_t host, void *data){
369 xbt_assert((host != NULL), "Invalid parameters");
370 xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
372 SIMIX_host_priv(host)->data = data;
375 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
376 smx_host_t host, double computation_amount, double priority, double bound){
377 return SIMIX_host_execute(name, host, computation_amount, priority, bound);
379 smx_action_t SIMIX_host_execute(const char *name,
380 smx_host_t host, double computation_amount, double priority, double bound){
382 /* alloc structures and initialize */
383 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
384 action->type = SIMIX_ACTION_EXECUTE;
385 action->name = xbt_strdup(name);
386 action->state = SIMIX_RUNNING;
387 action->execution.host = host;
390 action->category = NULL;
393 surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
394 /* set surf's action */
395 if (!MC_is_active()) {
396 action->execution.surf_exec = ws_model->extension.workstation.execute(host, computation_amount);
397 ws_model->action_data_set(action->execution.surf_exec, action);
398 ws_model->set_priority(action->execution.surf_exec, priority);
399 ws_model->set_bound(action->execution.surf_exec, bound);
402 XBT_DEBUG("Create execute action %p", action);
407 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
408 int host_nb, smx_host_t *host_list,
409 double *computation_amount, double *communication_amount,
410 double amount, double rate){
411 return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
412 communication_amount, amount, rate);
414 smx_action_t SIMIX_host_parallel_execute(const char *name,
415 int host_nb, smx_host_t *host_list,
416 double *computation_amount, double *communication_amount,
417 double amount, double rate){
419 void **workstation_list = NULL;
422 /* alloc structures and initialize */
423 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
424 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
425 action->name = xbt_strdup(name);
426 action->state = SIMIX_RUNNING;
427 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
430 action->category = NULL;
433 /* set surf's action */
434 workstation_list = xbt_new0(void *, host_nb);
435 for (i = 0; i < host_nb; i++)
436 workstation_list[i] = host_list[i];
439 /* FIXME: what happens if host_list contains VMs and PMs. If
440 * execute_parallel_task() does not change the state of the model, we can mix
442 surf_model_t ws_model = surf_resource_model(host_list[0], SURF_WKS_LEVEL);
443 for (i = 1; i < host_nb; i++) {
444 surf_model_t ws_model_tmp = surf_resource_model(host_list[i], SURF_WKS_LEVEL);
445 if (ws_model_tmp != ws_model) {
446 XBT_CRITICAL("mixing VMs and PMs is not supported");
451 /* set surf's action */
452 if (!MC_is_active()) {
453 action->execution.surf_exec =
454 ws_model->extension.workstation.
455 execute_parallel_task(host_nb, workstation_list, computation_amount,
456 communication_amount, rate);
458 ws_model->action_data_set(action->execution.surf_exec, action);
460 XBT_DEBUG("Create parallel execute action %p", action);
465 static surf_model_t get_ws_model_from_action(smx_action_t action)
467 xbt_assert(action->type == SIMIX_ACTION_EXECUTE);
468 smx_host_t host = action->execution.host;
469 surf_model_t model = surf_resource_model(host, SURF_WKS_LEVEL);
471 xbt_assert((model == surf_workstation_model) || (model == surf_vm_workstation_model));
476 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
477 SIMIX_host_execution_destroy(action);
479 void SIMIX_host_execution_destroy(smx_action_t action){
480 XBT_DEBUG("Destroy action %p", action);
482 surf_model_t ws_model = get_ws_model_from_action(action);
484 if (action->execution.surf_exec) {
485 ws_model->action_unref(action->execution.surf_exec);
486 action->execution.surf_exec = NULL;
488 xbt_free(action->name);
489 xbt_mallocator_release(simix_global->action_mallocator, action);
492 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
493 SIMIX_host_execution_cancel(action);
495 void SIMIX_host_execution_cancel(smx_action_t action){
496 XBT_DEBUG("Cancel action %p", action);
498 surf_model_t ws_model = get_ws_model_from_action(action);
500 if (action->execution.surf_exec)
501 ws_model->action_cancel(action->execution.surf_exec);
504 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
505 return SIMIX_host_execution_get_remains(action);
507 double SIMIX_host_execution_get_remains(smx_action_t action){
509 surf_model_t ws_model = get_ws_model_from_action(action);
511 if (action->state == SIMIX_RUNNING)
512 result = ws_model->get_remains(action->execution.surf_exec);
517 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
518 return SIMIX_host_execution_get_state(action);
520 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
521 return action->state;
524 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
526 SIMIX_host_execution_set_priority(action, priority);
528 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
529 surf_model_t ws_model = get_ws_model_from_action(action);
531 if(action->execution.surf_exec)
532 ws_model->set_priority(action->execution.surf_exec, priority);
535 void SIMIX_pre_host_execution_set_bound(smx_simcall_t simcall, smx_action_t action,
537 SIMIX_host_execution_set_bound(action, bound);
539 void SIMIX_host_execution_set_bound(smx_action_t action, double bound){
540 surf_model_t ws_model = get_ws_model_from_action(action);
542 if(action->execution.surf_exec)
543 ws_model->set_bound(action->execution.surf_exec, bound);
546 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
548 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
550 /* Associate this simcall to the action */
551 xbt_fifo_push(action->simcalls, simcall);
552 simcall->issuer->waiting_action = action;
554 /* set surf's action */
555 if (MC_is_active()) {
556 action->state = SIMIX_DONE;
557 SIMIX_execution_finish(action);
561 /* If the action is already finished then perform the error handling */
562 if (action->state != SIMIX_RUNNING)
563 SIMIX_execution_finish(action);
566 void SIMIX_host_execution_suspend(smx_action_t action)
568 surf_model_t ws_model = get_ws_model_from_action(action);
570 if(action->execution.surf_exec)
571 ws_model->suspend(action->execution.surf_exec);
574 void SIMIX_host_execution_resume(smx_action_t action)
576 surf_model_t ws_model = get_ws_model_from_action(action);
578 if(action->execution.surf_exec)
579 ws_model->resume(action->execution.surf_exec);
582 void SIMIX_execution_finish(smx_action_t action)
584 xbt_fifo_item_t item;
585 smx_simcall_t simcall;
586 surf_model_t ws_model = get_ws_model_from_action(action);
588 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
590 switch (action->state) {
593 /* do nothing, action done */
594 XBT_DEBUG("SIMIX_execution_finished: execution successful");
598 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
599 simcall->issuer->context->iwannadie = 1;
600 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
604 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
605 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
609 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
612 /* check if the host is down */
613 if (ws_model->extension.workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
614 simcall->issuer->context->iwannadie = 1;
617 simcall->issuer->waiting_action = NULL;
618 simcall_host_execution_wait__set__result(simcall, action->state);
619 SIMIX_simcall_answer(simcall);
622 /* We no longer need it */
623 SIMIX_host_execution_destroy(action);
627 void SIMIX_post_host_execute(smx_action_t action)
629 surf_model_t ws_model = get_ws_model_from_action(action);
631 if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
632 * for parallel tasks too */
633 ws_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
634 /* If the host running the action failed, notice it so that the asking
635 * process can be killed if it runs on that host itself */
636 action->state = SIMIX_FAILED;
637 } else if (ws_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
638 /* If the host running the action didn't fail, then the action was
640 action->state = SIMIX_CANCELED;
642 action->state = SIMIX_DONE;
645 if (action->execution.surf_exec) {
646 ws_model->action_unref(action->execution.surf_exec);
647 action->execution.surf_exec = NULL;
650 /* If there are simcalls associated with the action, then answer them */
651 if (xbt_fifo_size(action->simcalls)) {
652 SIMIX_execution_finish(action);
658 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
659 const char *category){
660 SIMIX_set_category(action, category);
662 void SIMIX_set_category(smx_action_t action, const char *category)
664 surf_model_t ws_model = get_ws_model_from_action(action);
666 if (action->state != SIMIX_RUNNING) return;
667 if (action->type == SIMIX_ACTION_EXECUTE){
668 ws_model->set_category(action->execution.surf_exec, category);
669 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
670 ws_model->set_category(action->comm.surf_comm, category);
677 * \brief Function to get the parameters of the given the SIMIX host.
679 * \param host the host to get_phys_host (a smx_host_t)
680 * \param param the parameter object space to be overwritten (a ws_params_t)
682 void SIMIX_host_get_params(smx_host_t ind_vm, ws_params_t params)
684 /* jump to ws_get_params(). */
685 surf_workstation_model->extension.workstation.get_params(ind_vm, params);
688 void SIMIX_pre_host_get_params(smx_simcall_t simcall, smx_host_t ind_vm, ws_params_t params)
690 SIMIX_host_get_params(ind_vm, params);
691 SIMIX_simcall_answer(simcall);
694 void SIMIX_host_set_params(smx_host_t ind_vm, ws_params_t params)
696 /* jump to ws_set_params(). */
697 surf_workstation_model->extension.workstation.set_params(ind_vm, params);
700 void SIMIX_pre_host_set_params(smx_simcall_t simcall, smx_host_t ind_vm, ws_params_t params)
702 SIMIX_host_set_params(ind_vm, params);
703 SIMIX_simcall_answer(simcall);