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);
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)");
180 void SIMIX_host_set_data(smx_host_t host, void *data)
182 xbt_assert((host != NULL), "Invalid parameters");
183 xbt_assert((host->data == NULL), "Data already set");
188 smx_action_t SIMIX_host_execute(const char *name, smx_host_t host,
189 double computation_amount,
192 /* alloc structures and initialize */
193 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
194 action->type = SIMIX_ACTION_EXECUTE;
195 action->name = xbt_strdup(name);
196 action->state = SIMIX_RUNNING;
197 action->execution.host = host;
200 action->category = NULL;
203 /* set surf's action */
204 if (!MC_IS_ENABLED) {
205 action->execution.surf_exec =
206 surf_workstation_model->extension.workstation.execute(host->host,
208 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
209 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
212 XBT_DEBUG("Create execute action %p", action);
217 smx_action_t SIMIX_host_parallel_execute( const char *name,
218 int host_nb, smx_host_t *host_list,
219 double *computation_amount, double *communication_amount,
220 double amount, double rate)
222 void **workstation_list = NULL;
225 /* alloc structures and initialize */
226 smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
227 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
228 action->name = xbt_strdup(name);
229 action->state = SIMIX_RUNNING;
230 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
233 action->category = NULL;
236 /* set surf's action */
237 workstation_list = xbt_new0(void *, host_nb);
238 for (i = 0; i < host_nb; i++)
239 workstation_list[i] = host_list[i]->host;
241 /* set surf's action */
242 if (!MC_IS_ENABLED) {
243 action->execution.surf_exec =
244 surf_workstation_model->extension.workstation.
245 execute_parallel_task(host_nb, workstation_list, computation_amount,
246 communication_amount, rate);
248 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
250 XBT_DEBUG("Create parallel execute action %p", action);
255 void SIMIX_host_execution_destroy(smx_action_t action)
258 XBT_DEBUG("Destroy action %p", action);
261 if (action->execution.surf_exec) {
262 destroyed = surf_workstation_model->action_unref(action->execution.surf_exec);
263 action->execution.surf_exec = NULL;
267 xbt_free(action->name);
268 xbt_mallocator_release(simix_global->action_mallocator, action);
272 void SIMIX_host_execution_cancel(smx_action_t action)
274 XBT_DEBUG("Cancel action %p", action);
276 if (action->execution.surf_exec)
277 surf_workstation_model->action_cancel(action->execution.surf_exec);
280 double SIMIX_host_execution_get_remains(smx_action_t action)
284 if (action->state == SIMIX_RUNNING)
285 result = surf_workstation_model->get_remains(action->execution.surf_exec);
290 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action)
292 return action->state;
295 void SIMIX_host_execution_set_priority(smx_action_t action, double priority)
297 if(action->execution.surf_exec)
298 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
301 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall)
303 smx_action_t action = simcall->host_execution_wait.execution;
305 XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
307 /* Associate this simcall to the action */
308 xbt_fifo_push(action->simcalls, simcall);
309 simcall->issuer->waiting_action = action;
311 /* set surf's action */
313 action->state = SIMIX_DONE;
314 SIMIX_execution_finish(action);
318 /* If the action is already finished then perform the error handling */
319 if (action->state != SIMIX_RUNNING)
320 SIMIX_execution_finish(action);
323 void SIMIX_host_execution_suspend(smx_action_t action)
325 if(action->execution.surf_exec)
326 surf_workstation_model->suspend(action->execution.surf_exec);
329 void SIMIX_host_execution_resume(smx_action_t action)
331 if(action->execution.surf_exec)
332 surf_workstation_model->resume(action->execution.surf_exec);
335 void SIMIX_execution_finish(smx_action_t action)
337 xbt_fifo_item_t item;
338 smx_simcall_t simcall;
340 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
342 switch (action->state) {
345 /* do nothing, action done */
346 XBT_DEBUG("SIMIX_execution_finished: execution successful");
350 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->smx_host->name);
351 if (simcall->issuer->smx_host == action->execution.host) {
352 // add a reference to the action that will be destroyed when the killed process is cleaned up, and by the end of the current function
353 surf_action_ref(action->execution.surf_exec);
354 SIMIX_process_kill(simcall->issuer);
356 SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
361 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
362 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
366 xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
369 simcall->issuer->waiting_action = NULL;
370 simcall->host_execution_wait.result = action->state;
371 SIMIX_simcall_answer(simcall);
374 /* We no longer need it */
375 SIMIX_host_execution_destroy(action);
378 void SIMIX_post_host_execute(smx_action_t action)
380 if (surf_workstation_model->extension.workstation.get_state(action->execution.host->host)==SURF_RESOURCE_OFF) {
381 /* if the host running the action failed, notice it so that the asking process can be killed if it runs on that host itself */
382 action->state = SIMIX_FAILED;
383 } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
384 /* If the host running the action didn't fail, then the action was canceled */
385 action->state = SIMIX_CANCELED;
387 action->state = SIMIX_DONE;
390 if (action->execution.surf_exec) {
391 surf_workstation_model->action_unref(action->execution.surf_exec);
392 action->execution.surf_exec = NULL;
395 /* If there are simcalls associated with the action, then answer them */
396 if (xbt_fifo_size(action->simcalls)) {
397 SIMIX_execution_finish(action);
403 void SIMIX_set_category(smx_action_t action, const char *category)
405 if (action->state != SIMIX_RUNNING) return;
406 if (action->type == SIMIX_ACTION_EXECUTE){
407 surf_workstation_model->set_category(action->execution.surf_exec, category);
408 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
409 surf_workstation_model->set_category(action->comm.surf_comm, category);