1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 xbt_dict_t watched_hosts_lib;
18 static void SIMIX_execution_finish(smx_action_t action);
21 * \brief Internal function to create a SIMIX host.
22 * \param name name of the host to create
23 * \param workstation the SURF workstation to encapsulate
24 * \param data some user data (may be NULL)
26 smx_host_t SIMIX_host_create(const char *name,
27 void *workstation, void *data)
29 smx_host_t smx_host = xbt_new0(s_smx_host_t, 1);
33 smx_host->name = xbt_strdup(name);
34 smx_host->data = data;
35 smx_host->host = workstation;
36 smx_host->process_list =
37 xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
39 /* Update global variables */
40 xbt_lib_set(host_lib,smx_host->name,SIMIX_HOST_LEVEL,smx_host);
46 * \brief Internal function to destroy a SIMIX host.
48 * \param h the host to destroy (a smx_host_t)
50 void SIMIX_host_destroy(void *h)
52 smx_host_t host = (smx_host_t) h;
54 xbt_assert((host != NULL), "Invalid parameters");
56 /* Clean Simulator data */
57 if (xbt_swag_size(host->process_list) != 0) {
59 bprintf("Shutting down host %s, but it's not empty:", host->name);
61 smx_process_t process = NULL;
63 xbt_swag_foreach(process, host->process_list) {
64 tmp = bprintf("%s\n\t%s", msg, process->name);
68 SIMIX_display_process_status();
69 THROWF(arg_error, 0, "%s", msg);
71 xbt_dynar_free(&host->auto_restart_processes);
72 xbt_swag_free(host->process_list);
74 /* Clean host structure */
82 * \brief Returns a dict of all hosts.
84 * \return List of all hosts (as a #xbt_dict_t)
86 xbt_dict_t SIMIX_host_get_dict(void)
88 xbt_dict_t host_dict = xbt_dict_new_homogeneous(NULL);
89 xbt_lib_cursor_t cursor = NULL;
93 xbt_lib_foreach(host_lib, cursor, name, host){
94 if(host[SIMIX_HOST_LEVEL])
95 xbt_dict_set(host_dict,name,host[SIMIX_HOST_LEVEL], NULL);
100 smx_host_t SIMIX_host_get_by_name(const char *name)
102 xbt_assert(((simix_global != NULL)
103 && (host_lib != NULL)),
104 "Environment not set yet");
106 return xbt_lib_get_or_null(host_lib, name, SIMIX_HOST_LEVEL);
109 smx_host_t SIMIX_host_self(void)
111 smx_process_t process = SIMIX_process_self();
112 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
115 /* needs to be public and without simcall because it is called
116 by exceptions and logging events */
117 const char* SIMIX_host_self_get_name(void)
119 smx_host_t host = SIMIX_host_self();
120 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
123 return SIMIX_host_get_name(host);
126 const char* SIMIX_host_get_name(smx_host_t host)
128 xbt_assert((host != NULL), "Invalid parameters");
133 xbt_dict_t SIMIX_host_get_properties(smx_host_t host)
135 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
137 return surf_workstation_model->extension.workstation.get_properties(host->host);
140 double SIMIX_host_get_speed(smx_host_t host)
142 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
144 return surf_workstation_model->extension.workstation.
145 get_speed(host->host, 1.0);
148 double SIMIX_host_get_available_speed(smx_host_t host)
150 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
152 return surf_workstation_model->extension.workstation.
153 get_available_speed(host->host);
156 int SIMIX_host_get_state(smx_host_t host)
158 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
160 return surf_workstation_model->extension.workstation.
161 get_state(host->host);
164 void* SIMIX_host_self_get_data(void)
166 return SIMIX_host_get_data(SIMIX_host_self());
169 void SIMIX_host_self_set_data(void *data)
171 SIMIX_host_set_data(SIMIX_host_self(), data);
174 void* SIMIX_host_get_data(smx_host_t host)
176 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
180 void _SIMIX_host_free_process_arg(void *);
181 void _SIMIX_host_free_process_arg(void *data) {
182 smx_process_arg_t arg = *(void**)data;
185 for (i = 0; i < arg->argc; i++) {
186 xbt_free(arg->argv[i]);
191 void SIMIX_host_add_auto_restart_process(smx_host_t host,
193 xbt_main_func_t code,
195 const char *hostname,
197 int argc, char **argv,
198 xbt_dict_t properties,
200 if (!host->auto_restart_processes) {
201 host->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
203 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
205 arg->name = xbt_strdup(name);
208 arg->hostname = hostname;
209 arg->kill_time = kill_time;
211 arg->argv = xbt_new(char*,argc + 1);
214 for (i = 0; i < argc; i++) {
215 arg->argv[i] = xbt_strdup(argv[i]);
218 arg->properties = properties;
219 arg->auto_restart = auto_restart;
221 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
222 && !xbt_dict_get_or_null(watched_hosts_lib,host->name)){
223 xbt_dict_set(watched_hosts_lib,host->name,host,NULL);
224 XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",host->name);
226 xbt_dynar_push_as(host->auto_restart_processes,smx_process_arg_t,arg);
230 void SIMIX_host_set_data(smx_host_t host, void *data)
232 xbt_assert((host != NULL), "Invalid parameters");
233 xbt_assert((host->data == NULL), "Data already set");
238 smx_action_t SIMIX_host_execute(const char *name, smx_host_t host,
239 double computation_amount,
242 /* alloc structures and initialize */
243 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
244 action->type = SIMIX_ACTION_EXECUTE;
245 action->name = xbt_strdup(name);
246 action->state = SIMIX_RUNNING;
247 action->execution.host = host;
250 action->category = NULL;
253 /* set surf's action */
254 if (!MC_IS_ENABLED) {
255 action->execution.surf_exec =
256 surf_workstation_model->extension.workstation.execute(host->host,
258 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
259 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
262 XBT_DEBUG("Create execute action %p", action);
267 smx_action_t SIMIX_host_parallel_execute( const char *name,
268 int host_nb, smx_host_t *host_list,
269 double *computation_amount, double *communication_amount,
270 double amount, double rate)
272 void **workstation_list = NULL;
275 /* alloc structures and initialize */
276 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
277 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
278 action->name = xbt_strdup(name);
279 action->state = SIMIX_RUNNING;
280 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
283 action->category = NULL;
286 /* set surf's action */
287 workstation_list = xbt_new0(void *, host_nb);
288 for (i = 0; i < host_nb; i++)
289 workstation_list[i] = host_list[i]->host;
291 /* set surf's action */
292 if (!MC_IS_ENABLED) {
293 action->execution.surf_exec =
294 surf_workstation_model->extension.workstation.
295 execute_parallel_task(host_nb, workstation_list, computation_amount,
296 communication_amount, rate);
298 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
300 XBT_DEBUG("Create parallel execute action %p", action);
305 void SIMIX_host_execution_destroy(smx_action_t action)
308 XBT_DEBUG("Destroy action %p", action);
311 if (action->execution.surf_exec) {
312 destroyed = surf_workstation_model->action_unref(action->execution.surf_exec);
313 action->execution.surf_exec = NULL;
317 xbt_free(action->name);
318 xbt_mallocator_release(simix_global->action_mallocator, action);
322 void SIMIX_host_execution_cancel(smx_action_t action)
324 XBT_DEBUG("Cancel action %p", action);
326 if (action->execution.surf_exec)
327 surf_workstation_model->action_cancel(action->execution.surf_exec);
330 double SIMIX_host_execution_get_remains(smx_action_t action)
334 if (action->state == SIMIX_RUNNING)
335 result = surf_workstation_model->get_remains(action->execution.surf_exec);
340 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action)
342 return action->state;
345 void SIMIX_host_execution_set_priority(smx_action_t action, double priority)
347 if(action->execution.surf_exec)
348 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
351 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall)
353 smx_action_t action = simcall->host_execution_wait.execution;
355 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
357 /* Associate this simcall to the action */
358 xbt_fifo_push(action->simcalls, simcall);
359 simcall->issuer->waiting_action = action;
361 /* set surf's action */
363 action->state = SIMIX_DONE;
364 SIMIX_execution_finish(action);
368 /* If the action is already finished then perform the error handling */
369 if (action->state != SIMIX_RUNNING)
370 SIMIX_execution_finish(action);
373 void SIMIX_host_execution_suspend(smx_action_t action)
375 if(action->execution.surf_exec)
376 surf_workstation_model->suspend(action->execution.surf_exec);
379 void SIMIX_host_execution_resume(smx_action_t action)
381 if(action->execution.surf_exec)
382 surf_workstation_model->resume(action->execution.surf_exec);
385 void SIMIX_execution_finish(smx_action_t action)
387 xbt_fifo_item_t item;
388 smx_simcall_t simcall;
390 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
392 switch (action->state) {
395 /* do nothing, action done */
396 XBT_DEBUG("SIMIX_execution_finished: execution successful");
400 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->smx_host->name);
401 simcall->issuer->context->iwannadie = 1;
402 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
406 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
407 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
411 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
414 /* check if the host is down */
415 if (surf_workstation_model->extension.
416 workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
417 simcall->issuer->context->iwannadie = 1;
420 simcall->issuer->waiting_action = NULL;
421 simcall->host_execution_wait.result = action->state;
422 SIMIX_simcall_answer(simcall);
425 /* We no longer need it */
426 SIMIX_host_execution_destroy(action);
429 void SIMIX_post_host_execute(smx_action_t action)
431 if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
432 * for parallel tasks too */
433 surf_workstation_model->extension.workstation.get_state(action->execution.host->host) == SURF_RESOURCE_OFF) {
434 /* If the host running the action failed, notice it so that the asking
435 * process can be killed if it runs on that host itself */
436 action->state = SIMIX_FAILED;
437 } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
438 /* If the host running the action didn't fail, then the action was
440 action->state = SIMIX_CANCELED;
442 action->state = SIMIX_DONE;
445 if (action->execution.surf_exec) {
446 surf_workstation_model->action_unref(action->execution.surf_exec);
447 action->execution.surf_exec = NULL;
450 /* If there are simcalls associated with the action, then answer them */
451 if (xbt_fifo_size(action->simcalls)) {
452 SIMIX_execution_finish(action);
458 void SIMIX_set_category(smx_action_t action, const char *category)
460 if (action->state != SIMIX_RUNNING) return;
461 if (action->type == SIMIX_ACTION_EXECUTE){
462 surf_workstation_model->set_category(action->execution.surf_exec, category);
463 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
464 surf_workstation_model->set_category(action->comm.surf_comm, category);