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);
42 * \brief Internal function to destroy a SIMIX host.
44 * \param h the host to destroy (a smx_host_t)
46 void SIMIX_host_destroy(void *h)
48 smx_host_priv_t host = (smx_host_priv_t) h;
50 xbt_assert((host != NULL), "Invalid parameters");
52 /* Clean Simulator data */
53 if (xbt_swag_size(host->process_list) != 0) {
55 bprintf("Shutting down host, but it's not empty:");
57 smx_process_t process = NULL;
59 xbt_swag_foreach(process, host->process_list) {
60 tmp = bprintf("%s\n\t%s", msg, process->name);
64 SIMIX_display_process_status();
65 THROWF(arg_error, 0, "%s", msg);
67 xbt_dynar_free(&host->auto_restart_processes);
68 xbt_swag_free(host->process_list);
70 /* Clean host structure */
76 // * \brief Returns a dict of all hosts.
78 // * \return List of all hosts (as a #xbt_dict_t)
80 //xbt_dict_t SIMIX_host_get_dict(void)
82 // xbt_dict_t host_dict = xbt_dict_new_homogeneous(NULL);
83 // xbt_lib_cursor_t cursor = NULL;
85 // void **host = NULL;
87 // xbt_lib_foreach(host_lib, cursor, name, host){
88 // if(host[SIMIX_HOST_LEVEL])
89 // xbt_dict_set(host_dict,name,host[SIMIX_HOST_LEVEL], NULL);
93 smx_host_t SIMIX_pre_host_get_by_name(smx_simcall_t simcall, const char *name){
94 return SIMIX_host_get_by_name(name);
96 smx_host_t SIMIX_host_get_by_name(const char *name){
97 xbt_assert(((simix_global != NULL)
98 && (host_lib != NULL)),
99 "Environment not set yet");
101 return xbt_lib_get_elm_or_null(host_lib, name);
104 smx_host_t SIMIX_host_self(void)
106 smx_process_t process = SIMIX_process_self();
107 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
110 const char* SIMIX_pre_host_self_get_name(smx_simcall_t simcall){
111 return SIMIX_host_self_get_name();
113 /* needs to be public and without simcall because it is called
114 by exceptions and logging events */
115 const char* SIMIX_host_self_get_name(void)
117 smx_host_t host = SIMIX_host_self();
118 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
121 return SIMIX_host_get_name(host);
124 const char* SIMIX_pre_host_get_name(smx_simcall_t simcall, smx_host_t host){
125 return SIMIX_host_get_name(host);
127 const char* SIMIX_host_get_name(smx_host_t host){
128 xbt_assert((host != NULL), "Invalid parameters");
130 return sg_host_name(host);
133 xbt_dict_t SIMIX_pre_host_get_properties(smx_simcall_t simcall, smx_host_t host){
134 return SIMIX_host_get_properties(host);
136 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
137 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
139 return surf_workstation_model->extension.workstation.get_properties(host);
142 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
143 return SIMIX_host_get_speed(host);
145 double SIMIX_host_get_speed(smx_host_t host){
146 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
148 return surf_workstation_model->extension.workstation.
149 get_speed(host, 1.0);
152 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
153 return SIMIX_host_get_available_speed(host);
155 double SIMIX_host_get_available_speed(smx_host_t host){
156 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
158 return surf_workstation_model->extension.workstation.
159 get_available_speed(host);
162 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
163 return SIMIX_host_get_state(host);
165 int SIMIX_host_get_state(smx_host_t host){
166 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
168 return surf_workstation_model->extension.workstation.
172 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
173 return SIMIX_host_self_get_data();
175 void* SIMIX_host_self_get_data(void)
177 smx_host_t self = SIMIX_host_self();
178 return SIMIX_host_get_data(self);
181 void SIMIX_host_self_set_data(void *data)
183 smx_host_t self = SIMIX_host_self();
184 SIMIX_host_set_data(self, data);
187 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
188 return SIMIX_host_get_data(host);
190 void* SIMIX_host_get_data(smx_host_t host){
191 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
193 return SIMIX_host_priv(host)->data;
195 void _SIMIX_host_free_process_arg(void *);
196 void _SIMIX_host_free_process_arg(void *data)
198 smx_process_arg_t arg = *(void**)data;
203 * \brief Add a process to the list of the processes that the host will restart when it comes back
204 * This function add a process to the list of the processes that will be restarted when the host comes
205 * back. It is expected that this function is called when the host is down.
206 * The processes will only be restarted once, meaning that you will have to register the process
207 * again to restart the process again.
209 void SIMIX_host_add_auto_restart_process(smx_host_t host,
211 xbt_main_func_t code,
213 const char *hostname,
215 int argc, char **argv,
216 xbt_dict_t properties,
219 if (!SIMIX_host_priv(host)->auto_restart_processes) {
220 SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
222 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
223 arg->name = xbt_strdup(name);
226 arg->hostname = hostname;
227 arg->kill_time = kill_time;
230 arg->argv = xbt_new(char*,argc + 1);
233 for (i = 0; i < argc; i++) {
234 arg->argv[i] = xbt_strdup(argv[i]);
236 arg->argv[argc] = NULL;
238 arg->properties = properties;
239 arg->auto_restart = auto_restart;
241 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
242 && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
243 xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
244 XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
246 xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
249 * \brief Restart the list of processes that have been registered to the host
251 void SIMIX_host_restart_processes(smx_host_t host)
254 smx_process_arg_t arg;
255 xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
257 smx_process_t process;
259 XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
260 if (simix_global->create_process_function) {
261 simix_global->create_process_function(&process,
273 simcall_process_create(&process,
286 xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
289 void SIMIX_host_autorestart(smx_host_t host)
291 if(simix_global->autorestart)
292 simix_global->autorestart(host);
294 xbt_die("No function for simix_global->autorestart");
297 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
298 SIMIX_host_set_data(host, data);
300 void SIMIX_host_set_data(smx_host_t host, void *data){
301 xbt_assert((host != NULL), "Invalid parameters");
302 xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
304 SIMIX_host_priv(host)->data = data;
307 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
308 smx_host_t host, double computation_amount, double priority){
309 return SIMIX_host_execute(name, host, computation_amount, priority);
311 smx_action_t SIMIX_host_execute(const char *name,
312 smx_host_t host, double computation_amount, double priority){
314 /* alloc structures and initialize */
315 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
316 action->type = SIMIX_ACTION_EXECUTE;
317 action->name = xbt_strdup(name);
318 action->state = SIMIX_RUNNING;
319 action->execution.host = host;
322 action->category = NULL;
325 /* set surf's action */
326 if (!MC_is_active()) {
327 action->execution.surf_exec =
328 surf_workstation_model->extension.workstation.execute(host,
330 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
331 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
334 XBT_DEBUG("Create execute action %p", action);
339 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
340 int host_nb, smx_host_t *host_list,
341 double *computation_amount, double *communication_amount,
342 double amount, double rate){
343 return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
344 communication_amount, amount, rate);
346 smx_action_t SIMIX_host_parallel_execute(const char *name,
347 int host_nb, smx_host_t *host_list,
348 double *computation_amount, double *communication_amount,
349 double amount, double rate){
351 void **workstation_list = NULL;
354 /* alloc structures and initialize */
355 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
356 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
357 action->name = xbt_strdup(name);
358 action->state = SIMIX_RUNNING;
359 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
362 action->category = NULL;
365 /* set surf's action */
366 workstation_list = xbt_new0(void *, host_nb);
367 for (i = 0; i < host_nb; i++)
368 workstation_list[i] = host_list[i];
370 /* set surf's action */
371 if (!MC_is_active()) {
372 action->execution.surf_exec =
373 surf_workstation_model->extension.workstation.
374 execute_parallel_task(host_nb, workstation_list, computation_amount,
375 communication_amount, rate);
377 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
379 XBT_DEBUG("Create parallel execute action %p", action);
384 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
385 SIMIX_host_execution_destroy(action);
387 void SIMIX_host_execution_destroy(smx_action_t action){
388 XBT_DEBUG("Destroy action %p", action);
390 if (action->execution.surf_exec) {
391 surf_workstation_model->action_unref(action->execution.surf_exec);
392 action->execution.surf_exec = NULL;
394 xbt_free(action->name);
395 xbt_mallocator_release(simix_global->action_mallocator, action);
398 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
399 SIMIX_host_execution_cancel(action);
401 void SIMIX_host_execution_cancel(smx_action_t action){
402 XBT_DEBUG("Cancel action %p", action);
404 if (action->execution.surf_exec)
405 surf_workstation_model->action_cancel(action->execution.surf_exec);
408 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
409 return SIMIX_host_execution_get_remains(action);
411 double SIMIX_host_execution_get_remains(smx_action_t action){
414 if (action->state == SIMIX_RUNNING)
415 result = surf_workstation_model->get_remains(action->execution.surf_exec);
420 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
421 return SIMIX_host_execution_get_state(action);
423 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
424 return action->state;
427 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
429 return SIMIX_host_execution_set_priority(action, priority);
431 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
432 if(action->execution.surf_exec)
433 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
436 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
438 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
440 /* Associate this simcall to the action */
441 xbt_fifo_push(action->simcalls, simcall);
442 simcall->issuer->waiting_action = action;
444 /* set surf's action */
445 if (MC_is_active()) {
446 action->state = SIMIX_DONE;
447 SIMIX_execution_finish(action);
451 /* If the action is already finished then perform the error handling */
452 if (action->state != SIMIX_RUNNING)
453 SIMIX_execution_finish(action);
456 void SIMIX_host_execution_suspend(smx_action_t action)
458 if(action->execution.surf_exec)
459 surf_workstation_model->suspend(action->execution.surf_exec);
462 void SIMIX_host_execution_resume(smx_action_t action)
464 if(action->execution.surf_exec)
465 surf_workstation_model->resume(action->execution.surf_exec);
468 void SIMIX_execution_finish(smx_action_t action)
470 xbt_fifo_item_t item;
471 smx_simcall_t simcall;
473 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
475 switch (action->state) {
478 /* do nothing, action done */
479 XBT_DEBUG("SIMIX_execution_finished: execution successful");
483 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
484 simcall->issuer->context->iwannadie = 1;
485 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
489 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
490 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
494 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
497 /* check if the host is down */
498 if (surf_workstation_model->extension.
499 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
500 simcall->issuer->context->iwannadie = 1;
503 simcall->issuer->waiting_action = NULL;
504 simcall_host_execution_wait__set__result(simcall, action->state);
505 SIMIX_simcall_answer(simcall);
508 /* We no longer need it */
509 SIMIX_host_execution_destroy(action);
512 void SIMIX_post_host_execute(smx_action_t action)
514 if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
515 * for parallel tasks too */
516 surf_workstation_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
517 /* If the host running the action failed, notice it so that the asking
518 * process can be killed if it runs on that host itself */
519 action->state = SIMIX_FAILED;
520 } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
521 /* If the host running the action didn't fail, then the action was
523 action->state = SIMIX_CANCELED;
525 action->state = SIMIX_DONE;
528 if (action->execution.surf_exec) {
529 surf_workstation_model->action_unref(action->execution.surf_exec);
530 action->execution.surf_exec = NULL;
533 /* If there are simcalls associated with the action, then answer them */
534 if (xbt_fifo_size(action->simcalls)) {
535 SIMIX_execution_finish(action);
541 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
542 const char *category){
543 SIMIX_set_category(action, category);
545 void SIMIX_set_category(smx_action_t action, const char *category)
547 if (action->state != SIMIX_RUNNING) return;
548 if (action->type == SIMIX_ACTION_EXECUTE){
549 surf_workstation_model->set_category(action->execution.surf_exec, category);
550 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
551 surf_workstation_model->set_category(action->comm.surf_comm, category);