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_resource_get_properties(surf_workstation_resource_priv(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)");
146 return surf_workstation_get_speed(host, 1.0);
149 int SIMIX_pre_host_get_core(smx_simcall_t simcall, smx_host_t host){
150 return SIMIX_host_get_core(host);
152 int SIMIX_host_get_core(smx_host_t host){
153 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
155 return surf_workstation_get_core(host);
160 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
161 return SIMIX_host_get_available_speed(host);
163 double SIMIX_host_get_available_speed(smx_host_t host){
164 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
166 return surf_workstation_get_available_speed(host);
169 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
170 return SIMIX_host_get_state(host);
172 int SIMIX_host_get_state(smx_host_t host){
173 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
175 return surf_resource_get_state(surf_workstation_resource_priv(host));
178 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
179 return SIMIX_host_self_get_data();
181 void* SIMIX_host_self_get_data(void)
183 smx_host_t self = SIMIX_host_self();
184 return SIMIX_host_get_data(self);
187 void SIMIX_host_self_set_data(void *data)
189 smx_host_t self = SIMIX_host_self();
190 SIMIX_host_set_data(self, data);
193 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
194 return SIMIX_host_get_data(host);
196 void* SIMIX_host_get_data(smx_host_t host){
197 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
199 return SIMIX_host_priv(host)->data;
201 void _SIMIX_host_free_process_arg(void *);
202 void _SIMIX_host_free_process_arg(void *data)
204 smx_process_arg_t arg = *(void**)data;
209 * \brief Add a process to the list of the processes that the host will restart when it comes back
210 * This function add a process to the list of the processes that will be restarted when the host comes
211 * back. It is expected that this function is called when the host is down.
212 * The processes will only be restarted once, meaning that you will have to register the process
213 * again to restart the process again.
215 void SIMIX_host_add_auto_restart_process(smx_host_t host,
217 xbt_main_func_t code,
219 const char *hostname,
221 int argc, char **argv,
222 xbt_dict_t properties,
225 if (!SIMIX_host_priv(host)->auto_restart_processes) {
226 SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
228 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
229 arg->name = xbt_strdup(name);
232 arg->hostname = hostname;
233 arg->kill_time = kill_time;
236 arg->argv = xbt_new(char*,argc + 1);
239 for (i = 0; i < argc; i++) {
240 arg->argv[i] = xbt_strdup(argv[i]);
242 arg->argv[argc] = NULL;
244 arg->properties = properties;
245 arg->auto_restart = auto_restart;
247 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
248 && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
249 xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
250 XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
252 xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
255 * \brief Restart the list of processes that have been registered to the host
257 void SIMIX_host_restart_processes(smx_host_t host)
260 smx_process_arg_t arg;
261 xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
263 smx_process_t process;
265 XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
266 if (simix_global->create_process_function) {
267 simix_global->create_process_function(&process,
279 simcall_process_create(&process,
292 xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
295 void SIMIX_host_autorestart(smx_host_t host)
297 if(simix_global->autorestart)
298 simix_global->autorestart(host);
300 xbt_die("No function for simix_global->autorestart");
303 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
304 SIMIX_host_set_data(host, data);
306 void SIMIX_host_set_data(smx_host_t host, void *data){
307 xbt_assert((host != NULL), "Invalid parameters");
308 xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
310 SIMIX_host_priv(host)->data = data;
313 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
314 smx_host_t host, double computation_amount, double priority){
315 return SIMIX_host_execute(name, host, computation_amount, priority);
317 smx_action_t SIMIX_host_execute(const char *name,
318 smx_host_t host, double computation_amount, double priority){
320 /* alloc structures and initialize */
321 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
322 action->type = SIMIX_ACTION_EXECUTE;
323 action->name = xbt_strdup(name);
324 action->state = SIMIX_RUNNING;
325 action->execution.host = host;
328 action->category = NULL;
331 /* set surf's action */
332 if (!MC_is_active()) {
333 action->execution.surf_exec =
334 surf_workstation_execute(host, computation_amount);
335 surf_action_set_data(action->execution.surf_exec, action);
336 surf_action_set_priority(action->execution.surf_exec, priority);
339 XBT_DEBUG("Create execute action %p", action);
344 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
345 int host_nb, smx_host_t *host_list,
346 double *computation_amount, double *communication_amount,
347 double amount, double rate){
348 return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
349 communication_amount, amount, rate);
351 smx_action_t SIMIX_host_parallel_execute(const char *name,
352 int host_nb, smx_host_t *host_list,
353 double *computation_amount, double *communication_amount,
354 double amount, double rate){
356 void **workstation_list = NULL;
359 /* alloc structures and initialize */
360 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
361 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
362 action->name = xbt_strdup(name);
363 action->state = SIMIX_RUNNING;
364 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
367 action->category = NULL;
370 /* set surf's action */
371 workstation_list = xbt_new0(void *, host_nb);
372 for (i = 0; i < host_nb; i++)
373 workstation_list[i] = surf_workstation_resource_priv(host_list[i]);
375 /* set surf's action */
376 if (!MC_is_active()) {
377 action->execution.surf_exec =
378 surf_workstation_model_execute_parallel_task((surf_workstation_model_t)surf_workstation_model,
379 host_nb, workstation_list, computation_amount, communication_amount, rate);
381 surf_action_set_data(action->execution.surf_exec, action);
383 XBT_DEBUG("Create parallel execute action %p", action);
388 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
389 SIMIX_host_execution_destroy(action);
391 void SIMIX_host_execution_destroy(smx_action_t action){
392 XBT_DEBUG("Destroy action %p", action);
394 if (action->execution.surf_exec) {
395 surf_action_unref(action->execution.surf_exec);
396 action->execution.surf_exec = NULL;
398 xbt_free(action->name);
399 xbt_mallocator_release(simix_global->action_mallocator, action);
402 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
403 SIMIX_host_execution_cancel(action);
405 void SIMIX_host_execution_cancel(smx_action_t action){
406 XBT_DEBUG("Cancel action %p", action);
408 if (action->execution.surf_exec)
409 surf_action_cancel(action->execution.surf_exec);
412 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
413 return SIMIX_host_execution_get_remains(action);
415 double SIMIX_host_execution_get_remains(smx_action_t action){
418 if (action->state == SIMIX_RUNNING)
419 result = surf_action_get_remains(action->execution.surf_exec);
424 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
425 return SIMIX_host_execution_get_state(action);
427 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
428 return action->state;
431 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
433 return SIMIX_host_execution_set_priority(action, priority);
435 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
436 if(action->execution.surf_exec)
437 surf_action_set_priority(action->execution.surf_exec, priority);
440 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
442 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
444 /* Associate this simcall to the action */
445 xbt_fifo_push(action->simcalls, simcall);
446 simcall->issuer->waiting_action = action;
448 /* set surf's action */
449 if (MC_is_active()) {
450 action->state = SIMIX_DONE;
451 SIMIX_execution_finish(action);
455 /* If the action is already finished then perform the error handling */
456 if (action->state != SIMIX_RUNNING)
457 SIMIX_execution_finish(action);
460 void SIMIX_host_execution_suspend(smx_action_t action)
462 if(action->execution.surf_exec)
463 surf_action_suspend(action->execution.surf_exec);
466 void SIMIX_host_execution_resume(smx_action_t action)
468 if(action->execution.surf_exec)
469 surf_action_resume(action->execution.surf_exec);
472 void SIMIX_execution_finish(smx_action_t action)
474 xbt_fifo_item_t item;
475 smx_simcall_t simcall;
477 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
479 switch (action->state) {
482 /* do nothing, action done */
483 XBT_DEBUG("SIMIX_execution_finished: execution successful");
487 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
488 simcall->issuer->context->iwannadie = 1;
489 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
493 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
494 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
498 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
501 /* check if the host is down */
503 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
504 simcall->issuer->context->iwannadie = 1;
507 simcall->issuer->waiting_action = NULL;
508 simcall_host_execution_wait__set__result(simcall, action->state);
509 SIMIX_simcall_answer(simcall);
512 /* We no longer need it */
513 SIMIX_host_execution_destroy(action);
516 void SIMIX_post_host_execute(smx_action_t action)
518 if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
519 * for parallel tasks too */
520 surf_resource_get_state(surf_workstation_resource_priv(action->execution.host)) == SURF_RESOURCE_OFF) {
521 /* If the host running the action failed, notice it so that the asking
522 * process can be killed if it runs on that host itself */
523 action->state = SIMIX_FAILED;
524 } else if (surf_action_get_state(action->execution.surf_exec) == SURF_ACTION_FAILED) {
525 /* If the host running the action didn't fail, then the action was
527 action->state = SIMIX_CANCELED;
529 action->state = SIMIX_DONE;
532 if (action->execution.surf_exec) {
533 surf_action_unref(action->execution.surf_exec);
534 action->execution.surf_exec = NULL;
537 /* If there are simcalls associated with the action, then answer them */
538 if (xbt_fifo_size(action->simcalls)) {
539 SIMIX_execution_finish(action);
545 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
546 const char *category){
547 SIMIX_set_category(action, category);
549 void SIMIX_set_category(smx_action_t action, const char *category)
551 if (action->state != SIMIX_RUNNING) return;
552 if (action->type == SIMIX_ACTION_EXECUTE){
553 surf_action_set_category(action->execution.surf_exec, category);
554 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
555 surf_action_set_category(action->comm.surf_comm, category);