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)");
17 static void SIMIX_execution_finish(smx_action_t action);
20 * \brief Internal function to create a SIMIX host.
21 * \param name name of the host to create
22 * \param workstation the SURF workstation to encapsulate
23 * \param data some user data (may be NULL)
25 smx_host_t SIMIX_host_create(const char *name,
26 void *workstation, void *data)
28 smx_host_t smx_host = xbt_new0(s_smx_host_t, 1);
32 smx_host->name = xbt_strdup(name);
33 smx_host->data = data;
34 smx_host->host = workstation;
35 smx_host->process_list =
36 xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
38 /* Update global variables */
39 xbt_lib_set(host_lib,smx_host->name,SIMIX_HOST_LEVEL,smx_host);
45 * \brief Internal function to destroy a SIMIX host.
47 * \param h the host to destroy (a smx_host_t)
49 void SIMIX_host_destroy(void *h)
51 smx_host_t host = (smx_host_t) h;
53 xbt_assert((host != NULL), "Invalid parameters");
55 /* Clean Simulator data */
56 if (xbt_swag_size(host->process_list) != 0) {
58 bprintf("Shutting down host %s, but it's not empty:", host->name);
60 smx_process_t process = NULL;
62 xbt_swag_foreach(process, host->process_list) {
63 tmp = bprintf("%s\n\t%s", msg, process->name);
67 SIMIX_display_process_status();
68 THROWF(arg_error, 0, "%s", msg);
70 xbt_dynar_free(&host->auto_restart_processes);
71 xbt_swag_free(host->process_list);
73 /* Clean host structure */
81 * \brief Returns a dict of all hosts.
83 * \return List of all hosts (as a #xbt_dict_t)
85 xbt_dict_t SIMIX_host_get_dict(void)
87 xbt_dict_t host_dict = xbt_dict_new_homogeneous(NULL);
88 xbt_lib_cursor_t cursor = NULL;
92 xbt_lib_foreach(host_lib, cursor, name, host){
93 if(host[SIMIX_HOST_LEVEL])
94 xbt_dict_set(host_dict,name,host[SIMIX_HOST_LEVEL], NULL);
99 smx_host_t SIMIX_host_get_by_name(const char *name)
101 xbt_assert(((simix_global != NULL)
102 && (host_lib != NULL)),
103 "Environment not set yet");
105 return xbt_lib_get_or_null(host_lib, name, SIMIX_HOST_LEVEL);
108 smx_host_t SIMIX_host_self(void)
110 smx_process_t process = SIMIX_process_self();
111 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
114 /* needs to be public and without simcall because it is called
115 by exceptions and logging events */
116 const char* SIMIX_host_self_get_name(void)
118 smx_host_t host = SIMIX_host_self();
119 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
122 return SIMIX_host_get_name(host);
125 const char* SIMIX_host_get_name(smx_host_t host)
127 xbt_assert((host != NULL), "Invalid parameters");
132 xbt_dict_t SIMIX_host_get_properties(smx_host_t host)
134 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
136 return surf_workstation_model->extension.workstation.get_properties(host->host);
139 double SIMIX_host_get_speed(smx_host_t host)
141 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
143 return surf_workstation_model->extension.workstation.
144 get_speed(host->host, 1.0);
147 double SIMIX_host_get_available_speed(smx_host_t host)
149 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
151 return surf_workstation_model->extension.workstation.
152 get_available_speed(host->host);
155 int SIMIX_host_get_state(smx_host_t host)
157 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
159 return surf_workstation_model->extension.workstation.
160 get_state(host->host);
163 void* SIMIX_host_self_get_data(void)
165 return SIMIX_host_get_data(SIMIX_host_self());
168 void SIMIX_host_self_set_data(void *data)
170 SIMIX_host_set_data(SIMIX_host_self(), data);
173 void* SIMIX_host_get_data(smx_host_t host)
175 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
179 void _SIMIX_host_free_process_arg(void *);
180 void _SIMIX_host_free_process_arg(void *data) {
181 smx_process_arg_t arg = *(void**)data;
184 for (i = 0; i < arg->argc; i++) {
185 xbt_free(arg->argv[i]);
190 void SIMIX_host_add_auto_restart_process(smx_host_t host,
192 xbt_main_func_t code,
194 const char *hostname,
196 int argc, char **argv,
197 xbt_dict_t properties,
199 if (!host->auto_restart_processes) {
200 host->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
202 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
204 arg->name = xbt_strdup(name);
207 arg->hostname = hostname;
208 arg->kill_time = kill_time;
210 arg->argv = xbt_new(char*,argc + 1);
213 for (i = 0; i < argc; i++) {
214 arg->argv[i] = xbt_strdup(argv[i]);
217 arg->properties = properties;
218 arg->auto_restart = auto_restart;
220 xbt_dynar_push_as(host->auto_restart_processes,smx_process_arg_t,arg);
224 void SIMIX_host_set_data(smx_host_t host, void *data)
226 xbt_assert((host != NULL), "Invalid parameters");
227 xbt_assert((host->data == NULL), "Data already set");
232 smx_action_t SIMIX_host_execute(const char *name, smx_host_t host,
233 double computation_amount,
236 /* alloc structures and initialize */
237 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
238 action->type = SIMIX_ACTION_EXECUTE;
239 action->name = xbt_strdup(name);
240 action->state = SIMIX_RUNNING;
241 action->execution.host = host;
244 action->category = NULL;
247 /* set surf's action */
248 if (!MC_IS_ENABLED) {
249 action->execution.surf_exec =
250 surf_workstation_model->extension.workstation.execute(host->host,
252 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
253 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
256 XBT_DEBUG("Create execute action %p", action);
261 smx_action_t SIMIX_host_parallel_execute( const char *name,
262 int host_nb, smx_host_t *host_list,
263 double *computation_amount, double *communication_amount,
264 double amount, double rate)
266 void **workstation_list = NULL;
269 /* alloc structures and initialize */
270 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
271 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
272 action->name = xbt_strdup(name);
273 action->state = SIMIX_RUNNING;
274 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
277 action->category = NULL;
280 /* set surf's action */
281 workstation_list = xbt_new0(void *, host_nb);
282 for (i = 0; i < host_nb; i++)
283 workstation_list[i] = host_list[i]->host;
285 /* set surf's action */
286 if (!MC_IS_ENABLED) {
287 action->execution.surf_exec =
288 surf_workstation_model->extension.workstation.
289 execute_parallel_task(host_nb, workstation_list, computation_amount,
290 communication_amount, rate);
292 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
294 XBT_DEBUG("Create parallel execute action %p", action);
299 void SIMIX_host_execution_destroy(smx_action_t action)
302 XBT_DEBUG("Destroy action %p", action);
305 if (action->execution.surf_exec) {
306 destroyed = surf_workstation_model->action_unref(action->execution.surf_exec);
307 action->execution.surf_exec = NULL;
311 xbt_free(action->name);
312 xbt_mallocator_release(simix_global->action_mallocator, action);
316 void SIMIX_host_execution_cancel(smx_action_t action)
318 XBT_DEBUG("Cancel action %p", action);
320 if (action->execution.surf_exec)
321 surf_workstation_model->action_cancel(action->execution.surf_exec);
324 double SIMIX_host_execution_get_remains(smx_action_t action)
328 if (action->state == SIMIX_RUNNING)
329 result = surf_workstation_model->get_remains(action->execution.surf_exec);
334 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action)
336 return action->state;
339 void SIMIX_host_execution_set_priority(smx_action_t action, double priority)
341 if(action->execution.surf_exec)
342 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
345 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall)
347 smx_action_t action = simcall->host_execution_wait.execution;
349 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
351 /* Associate this simcall to the action */
352 xbt_fifo_push(action->simcalls, simcall);
353 simcall->issuer->waiting_action = action;
355 /* set surf's action */
357 action->state = SIMIX_DONE;
358 SIMIX_execution_finish(action);
362 /* If the action is already finished then perform the error handling */
363 if (action->state != SIMIX_RUNNING)
364 SIMIX_execution_finish(action);
367 void SIMIX_host_execution_suspend(smx_action_t action)
369 if(action->execution.surf_exec)
370 surf_workstation_model->suspend(action->execution.surf_exec);
373 void SIMIX_host_execution_resume(smx_action_t action)
375 if(action->execution.surf_exec)
376 surf_workstation_model->resume(action->execution.surf_exec);
379 void SIMIX_execution_finish(smx_action_t action)
381 xbt_fifo_item_t item;
382 smx_simcall_t simcall;
384 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
386 switch (action->state) {
389 /* do nothing, action done */
390 XBT_DEBUG("SIMIX_execution_finished: execution successful");
394 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->smx_host->name);
395 simcall->issuer->context->iwannadie = 1;
396 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
400 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
401 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
405 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
408 /* check if the host is down */
409 if (surf_workstation_model->extension.
410 workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
411 simcall->issuer->context->iwannadie = 1;
414 simcall->issuer->waiting_action = NULL;
415 simcall->host_execution_wait.result = action->state;
416 SIMIX_simcall_answer(simcall);
419 /* We no longer need it */
420 SIMIX_host_execution_destroy(action);
423 void SIMIX_post_host_execute(smx_action_t action)
425 if (surf_workstation_model->extension.workstation.get_state(action->execution.host->host)==SURF_RESOURCE_OFF) {
426 /* if the host running the action failed, notice it so that the asking process can be killed if it runs on that host itself */
427 action->state = SIMIX_FAILED;
428 } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
429 /* If the host running the action didn't fail, then the action was canceled */
430 action->state = SIMIX_CANCELED;
432 action->state = SIMIX_DONE;
435 if (action->execution.surf_exec) {
436 surf_workstation_model->action_unref(action->execution.surf_exec);
437 action->execution.surf_exec = NULL;
440 /* If there are simcalls associated with the action, then answer them */
441 if (xbt_fifo_size(action->simcalls)) {
442 SIMIX_execution_finish(action);
448 void SIMIX_set_category(smx_action_t action, const char *category)
450 if (action->state != SIMIX_RUNNING) return;
451 if (action->type == SIMIX_ACTION_EXECUTE){
452 surf_workstation_model->set_category(action->execution.surf_exec, category);
453 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
454 surf_workstation_model->set_category(action->comm.surf_comm, category);