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) {
54 char *msg = xbt_strdup("Shutting down host, but it's not empty:");
56 smx_process_t process = NULL;
58 xbt_swag_foreach(process, host->process_list) {
59 tmp = bprintf("%s\n\t%s", msg, process->name);
63 SIMIX_display_process_status();
64 THROWF(arg_error, 0, "%s", msg);
66 xbt_dynar_free(&host->auto_restart_processes);
67 xbt_swag_free(host->process_list);
69 /* Clean host structure */
75 // * \brief Returns a dict of all hosts.
77 // * \return List of all hosts (as a #xbt_dict_t)
79 //xbt_dict_t SIMIX_host_get_dict(void)
81 // xbt_dict_t host_dict = xbt_dict_new_homogeneous(NULL);
82 // xbt_lib_cursor_t cursor = NULL;
84 // void **host = NULL;
86 // xbt_lib_foreach(host_lib, cursor, name, host){
87 // if(host[SIMIX_HOST_LEVEL])
88 // xbt_dict_set(host_dict,name,host[SIMIX_HOST_LEVEL], NULL);
92 smx_host_t SIMIX_pre_host_get_by_name(smx_simcall_t simcall, const char *name){
93 return SIMIX_host_get_by_name(name);
95 smx_host_t SIMIX_host_get_by_name(const char *name){
96 xbt_assert(((simix_global != NULL)
97 && (host_lib != NULL)),
98 "Environment not set yet");
100 return xbt_lib_get_elm_or_null(host_lib, name);
103 smx_host_t SIMIX_host_self(void)
105 smx_process_t process = SIMIX_process_self();
106 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
109 const char* SIMIX_pre_host_self_get_name(smx_simcall_t simcall){
110 return SIMIX_host_self_get_name();
112 /* needs to be public and without simcall because it is called
113 by exceptions and logging events */
114 const char* SIMIX_host_self_get_name(void)
116 smx_host_t host = SIMIX_host_self();
117 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
120 return SIMIX_host_get_name(host);
123 const char* SIMIX_pre_host_get_name(smx_simcall_t simcall, smx_host_t host){
124 return SIMIX_host_get_name(host);
126 const char* SIMIX_host_get_name(smx_host_t host){
127 xbt_assert((host != NULL), "Invalid parameters");
129 return sg_host_name(host);
132 xbt_dict_t SIMIX_pre_host_get_properties(smx_simcall_t simcall, smx_host_t host){
133 return SIMIX_host_get_properties(host);
135 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
136 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
138 return surf_workstation_model->extension.workstation.get_properties(host);
141 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
142 return SIMIX_host_get_speed(host);
144 double SIMIX_host_get_speed(smx_host_t host){
145 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
147 return surf_workstation_model->extension.workstation.
148 get_speed(host, 1.0);
151 int SIMIX_pre_host_get_core(smx_simcall_t simcall, smx_host_t host){
152 return SIMIX_host_get_core(host);
154 int SIMIX_host_get_core(smx_host_t host){
155 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
157 return surf_workstation_model->extension.workstation.
163 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
164 return SIMIX_host_get_available_speed(host);
166 double SIMIX_host_get_available_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_available_speed(host);
173 double SIMIX_pre_host_get_current_power_peak(smx_simcall_t simcall, smx_host_t host){
174 return SIMIX_host_get_current_power_peak(host);
176 double SIMIX_host_get_current_power_peak(smx_host_t host) {
177 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
178 return surf_workstation_model->extension.workstation.
179 get_current_power_peak(host);
182 double SIMIX_pre_host_get_power_peak_at(smx_simcall_t simcall, smx_host_t host, int pstate_index){
183 return SIMIX_host_get_power_peak_at(host, pstate_index);
185 double SIMIX_host_get_power_peak_at(smx_host_t host, int pstate_index) {
186 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
188 return surf_workstation_model->extension.workstation.
189 get_power_peak_at(host, pstate_index);
192 int SIMIX_pre_host_get_nb_pstates(smx_simcall_t simcall, smx_host_t host){
193 return SIMIX_host_get_nb_pstates(host);
195 int SIMIX_host_get_nb_pstates(smx_host_t host) {
196 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
198 return surf_workstation_model->extension.workstation.
199 get_nb_pstates(host);
203 void SIMIX_pre_host_set_power_peak_at(smx_simcall_t simcall, smx_host_t host, int pstate_index){
204 SIMIX_host_set_power_peak_at(host, pstate_index);
206 void SIMIX_host_set_power_peak_at(smx_host_t host, int pstate_index) {
207 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
209 surf_workstation_model->extension.workstation.
210 set_power_peak_at(host, pstate_index);
213 double SIMIX_pre_host_get_consumed_energy(smx_simcall_t simcall, smx_host_t host){
214 return SIMIX_host_get_consumed_energy(host);
216 double SIMIX_host_get_consumed_energy(smx_host_t host) {
217 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
218 return surf_workstation_model->extension.workstation.
219 get_consumed_energy(host);
222 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
223 return SIMIX_host_get_state(host);
225 int SIMIX_host_get_state(smx_host_t host){
226 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
228 return surf_workstation_model->extension.workstation.
232 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
233 return SIMIX_host_self_get_data();
235 void* SIMIX_host_self_get_data(void)
237 smx_host_t self = SIMIX_host_self();
238 return SIMIX_host_get_data(self);
241 void SIMIX_host_self_set_data(void *data)
243 smx_host_t self = SIMIX_host_self();
244 SIMIX_host_set_data(self, data);
247 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
248 return SIMIX_host_get_data(host);
250 void* SIMIX_host_get_data(smx_host_t host){
251 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
253 return SIMIX_host_priv(host)->data;
255 void _SIMIX_host_free_process_arg(void *);
256 void _SIMIX_host_free_process_arg(void *data)
258 smx_process_arg_t arg = *(void**)data;
263 * \brief Add a process to the list of the processes that the host will restart when it comes back
264 * This function add a process to the list of the processes that will be restarted when the host comes
265 * back. It is expected that this function is called when the host is down.
266 * The processes will only be restarted once, meaning that you will have to register the process
267 * again to restart the process again.
269 void SIMIX_host_add_auto_restart_process(smx_host_t host,
271 xbt_main_func_t code,
273 const char *hostname,
275 int argc, char **argv,
276 xbt_dict_t properties,
279 if (!SIMIX_host_priv(host)->auto_restart_processes) {
280 SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
282 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
283 arg->name = xbt_strdup(name);
286 arg->hostname = hostname;
287 arg->kill_time = kill_time;
290 arg->argv = xbt_new(char*,argc + 1);
293 for (i = 0; i < argc; i++) {
294 arg->argv[i] = xbt_strdup(argv[i]);
296 arg->argv[argc] = NULL;
298 arg->properties = properties;
299 arg->auto_restart = auto_restart;
301 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
302 && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
303 xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
304 XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
306 xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
309 * \brief Restart the list of processes that have been registered to the host
311 void SIMIX_host_restart_processes(smx_host_t host)
314 smx_process_arg_t arg;
315 xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
317 smx_process_t process;
319 XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
320 if (simix_global->create_process_function) {
321 simix_global->create_process_function(&process,
333 simcall_process_create(&process,
346 xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
349 void SIMIX_host_autorestart(smx_host_t host)
351 if(simix_global->autorestart)
352 simix_global->autorestart(host);
354 xbt_die("No function for simix_global->autorestart");
357 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
358 SIMIX_host_set_data(host, data);
360 void SIMIX_host_set_data(smx_host_t host, void *data){
361 xbt_assert((host != NULL), "Invalid parameters");
362 xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
364 SIMIX_host_priv(host)->data = data;
367 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
368 smx_host_t host, double computation_amount, double priority){
369 return SIMIX_host_execute(name, host, computation_amount, priority);
371 smx_action_t SIMIX_host_execute(const char *name,
372 smx_host_t host, double computation_amount, double priority){
374 /* alloc structures and initialize */
375 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
376 action->type = SIMIX_ACTION_EXECUTE;
377 action->name = xbt_strdup(name);
378 action->state = SIMIX_RUNNING;
379 action->execution.host = host;
382 action->category = NULL;
385 /* set surf's action */
386 if (!MC_is_active()) {
387 action->execution.surf_exec =
388 surf_workstation_model->extension.workstation.execute(host,
390 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
391 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
394 XBT_DEBUG("Create execute action %p", action);
399 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
400 int host_nb, smx_host_t *host_list,
401 double *computation_amount, double *communication_amount,
402 double amount, double rate){
403 return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
404 communication_amount, amount, rate);
406 smx_action_t SIMIX_host_parallel_execute(const char *name,
407 int host_nb, smx_host_t *host_list,
408 double *computation_amount, double *communication_amount,
409 double amount, double rate){
411 void **workstation_list = NULL;
414 /* alloc structures and initialize */
415 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
416 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
417 action->name = xbt_strdup(name);
418 action->state = SIMIX_RUNNING;
419 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
422 action->category = NULL;
425 /* set surf's action */
426 workstation_list = xbt_new0(void *, host_nb);
427 for (i = 0; i < host_nb; i++)
428 workstation_list[i] = host_list[i];
430 /* set surf's action */
431 if (!MC_is_active()) {
432 action->execution.surf_exec =
433 surf_workstation_model->extension.workstation.
434 execute_parallel_task(host_nb, workstation_list, computation_amount,
435 communication_amount, rate);
437 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
439 XBT_DEBUG("Create parallel execute action %p", action);
444 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
445 SIMIX_host_execution_destroy(action);
447 void SIMIX_host_execution_destroy(smx_action_t action){
448 XBT_DEBUG("Destroy action %p", action);
450 if (action->execution.surf_exec) {
451 surf_workstation_model->action_unref(action->execution.surf_exec);
452 action->execution.surf_exec = NULL;
454 xbt_free(action->name);
455 xbt_mallocator_release(simix_global->action_mallocator, action);
458 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
459 SIMIX_host_execution_cancel(action);
461 void SIMIX_host_execution_cancel(smx_action_t action){
462 XBT_DEBUG("Cancel action %p", action);
464 if (action->execution.surf_exec)
465 surf_workstation_model->action_cancel(action->execution.surf_exec);
468 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
469 return SIMIX_host_execution_get_remains(action);
471 double SIMIX_host_execution_get_remains(smx_action_t action){
474 if (action->state == SIMIX_RUNNING)
475 result = surf_workstation_model->get_remains(action->execution.surf_exec);
480 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
481 return SIMIX_host_execution_get_state(action);
483 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
484 return action->state;
487 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
489 return SIMIX_host_execution_set_priority(action, priority);
491 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
492 if(action->execution.surf_exec)
493 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
496 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
498 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
500 /* Associate this simcall to the action */
501 xbt_fifo_push(action->simcalls, simcall);
502 simcall->issuer->waiting_action = action;
504 /* set surf's action */
505 if (MC_is_active()) {
506 action->state = SIMIX_DONE;
507 SIMIX_execution_finish(action);
511 /* If the action is already finished then perform the error handling */
512 if (action->state != SIMIX_RUNNING)
513 SIMIX_execution_finish(action);
516 void SIMIX_host_execution_suspend(smx_action_t action)
518 if(action->execution.surf_exec)
519 surf_workstation_model->suspend(action->execution.surf_exec);
522 void SIMIX_host_execution_resume(smx_action_t action)
524 if(action->execution.surf_exec)
525 surf_workstation_model->resume(action->execution.surf_exec);
528 void SIMIX_execution_finish(smx_action_t action)
530 xbt_fifo_item_t item;
531 smx_simcall_t simcall;
533 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
535 switch (action->state) {
538 /* do nothing, action done */
539 XBT_DEBUG("SIMIX_execution_finished: execution successful");
543 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
544 simcall->issuer->context->iwannadie = 1;
545 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
549 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
550 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
554 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
557 /* check if the host is down */
558 if (surf_workstation_model->extension.
559 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
560 simcall->issuer->context->iwannadie = 1;
563 simcall->issuer->waiting_action = NULL;
564 simcall_host_execution_wait__set__result(simcall, action->state);
565 SIMIX_simcall_answer(simcall);
568 /* We no longer need it */
569 SIMIX_host_execution_destroy(action);
572 void SIMIX_post_host_execute(smx_action_t action)
574 if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
575 * for parallel tasks too */
576 surf_workstation_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
577 /* If the host running the action failed, notice it so that the asking
578 * process can be killed if it runs on that host itself */
579 action->state = SIMIX_FAILED;
580 } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
581 /* If the host running the action didn't fail, then the action was
583 action->state = SIMIX_CANCELED;
585 action->state = SIMIX_DONE;
588 if (action->execution.surf_exec) {
589 surf_workstation_model->action_unref(action->execution.surf_exec);
590 action->execution.surf_exec = NULL;
593 /* If there are simcalls associated with the action, then answer them */
594 if (xbt_fifo_size(action->simcalls)) {
595 SIMIX_execution_finish(action);
601 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
602 const char *category){
603 SIMIX_set_category(action, category);
605 void SIMIX_set_category(smx_action_t action, const char *category)
607 if (action->state != SIMIX_RUNNING) return;
608 if (action->type == SIMIX_ACTION_EXECUTE){
609 surf_workstation_model->set_category(action->execution.surf_exec, category);
610 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
611 surf_workstation_model->set_category(action->comm.surf_comm, category);