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. */
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_dict_set(simix_global->host, smx_host->name, 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_assert0((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 THROW1(arg_error, 0, "%s", msg);
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 return simix_global->host;
91 smx_host_t SIMIX_host_get_by_name(const char *name)
93 xbt_assert0(((simix_global != NULL)
94 && (simix_global->host != NULL)),
95 "Environment not set yet");
97 return xbt_dict_get_or_null(simix_global->host, name);
100 smx_host_t SIMIX_host_self(void)
102 smx_process_t process = SIMIX_process_self();
103 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
106 /* needs to be public and without request because it is called
107 by exceptions and logging events */
108 const char* SIMIX_host_self_get_name(void)
110 smx_host_t host = SIMIX_host_self();
111 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
114 return SIMIX_host_get_name(host);
117 const char* SIMIX_host_get_name(smx_host_t host)
119 xbt_assert0((host != NULL), "Invalid parameters");
124 xbt_dict_t SIMIX_host_get_properties(smx_host_t host)
126 xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
128 return surf_workstation_model->extension.workstation.get_properties(host->host);
131 double SIMIX_host_get_speed(smx_host_t host)
133 xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
135 return surf_workstation_model->extension.workstation.
136 get_speed(host->host, 1.0);
139 double SIMIX_host_get_available_speed(smx_host_t host)
141 xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
143 return surf_workstation_model->extension.workstation.
144 get_available_speed(host->host);
147 int SIMIX_host_get_state(smx_host_t host)
149 xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
151 return surf_workstation_model->extension.workstation.
152 get_state(host->host);
155 void* SIMIX_host_self_get_data(void)
157 return SIMIX_host_get_data(SIMIX_host_self());
160 void SIMIX_host_self_set_data(void *data)
162 SIMIX_host_set_data(SIMIX_host_self(), data);
165 void* SIMIX_host_get_data(smx_host_t host)
167 xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
172 void SIMIX_host_set_data(smx_host_t host, void *data)
174 xbt_assert0((host != NULL), "Invalid parameters");
175 xbt_assert0((host->data == NULL), "Data already set");
180 smx_action_t SIMIX_host_execute(const char *name, smx_host_t host,
181 double computation_amount)
183 /* alloc structures and initialize */
184 smx_action_t action = xbt_new0(s_smx_action_t, 1);
185 action->type = SIMIX_ACTION_EXECUTE;
186 action->name = xbt_strdup(name);
187 action->request_list = xbt_fifo_new();
188 action->state = SIMIX_RUNNING;
189 action->execution.host = host;
192 action->category = NULL;
195 /* set surf's action */
196 if (!MC_IS_ENABLED) {
197 action->execution.surf_exec =
198 surf_workstation_model->extension.workstation.execute(host->host,
200 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
204 TRACE_smx_host_execute(action);
207 DEBUG1("Create execute action %p", action);
212 smx_action_t SIMIX_host_parallel_execute( const char *name,
213 int host_nb, smx_host_t *host_list,
214 double *computation_amount, double *communication_amount,
215 double amount, double rate)
217 void **workstation_list = NULL;
220 /* alloc structures and initialize */
221 smx_action_t action = xbt_new0(s_smx_action_t, 1);
222 action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
223 action->name = xbt_strdup(name);
224 action->request_list = xbt_fifo_new();
225 action->state = SIMIX_RUNNING;
226 action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
229 action->category = NULL;
232 /* set surf's action */
233 workstation_list = xbt_new0(void *, host_nb);
234 for (i = 0; i < host_nb; i++)
235 workstation_list[i] = host_list[i]->host;
237 /* set surf's action */
238 if (!MC_IS_ENABLED) {
239 action->execution.surf_exec =
240 surf_workstation_model->extension.workstation.
241 execute_parallel_task(host_nb, workstation_list, computation_amount,
242 communication_amount, amount, rate);
244 surf_workstation_model->action_data_set(action->execution.surf_exec, action);
246 DEBUG1("Create parallel execute action %p", action);
251 void SIMIX_host_execution_destroy(smx_action_t action)
253 DEBUG1("Destroy action %p", action);
256 xbt_free(action->name);
258 xbt_fifo_free(action->request_list);
260 if (action->execution.surf_exec) {
261 surf_workstation_model->action_unref(action->execution.surf_exec);
262 action->execution.surf_exec = NULL;
266 TRACE_smx_action_destroy(action);
271 void SIMIX_host_execution_cancel(smx_action_t action)
273 DEBUG1("Cancel action %p", action);
275 if (action->execution.surf_exec)
276 surf_workstation_model->action_cancel(action->execution.surf_exec);
279 double SIMIX_host_execution_get_remains(smx_action_t action)
283 if (action->state == SIMIX_RUNNING)
284 result = surf_workstation_model->get_remains(action->execution.surf_exec);
289 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action)
291 return action->state;
294 void SIMIX_host_execution_set_priority(smx_action_t action, double priority)
296 if(action->execution.surf_exec)
297 surf_workstation_model->set_priority(action->execution.surf_exec, priority);
300 void SIMIX_pre_host_execution_wait(smx_req_t req)
302 smx_action_t action = req->host_execution_wait.execution;
304 DEBUG2("Wait for execution of action %p, state %d", action, action->state);
306 /* Associate this request to the action */
307 xbt_fifo_push(action->request_list, req);
308 req->issuer->waiting_action = action;
310 /* set surf's action */
312 action->state = SIMIX_DONE;
313 SIMIX_execution_finish(action);
317 /* If the action is already finished then perform the error handling */
318 if (action->state != SIMIX_RUNNING)
319 SIMIX_execution_finish(action);
322 void SIMIX_host_execution_suspend(smx_action_t action)
324 if(action->execution.surf_exec)
325 surf_workstation_model->suspend(action->execution.surf_exec);
328 void SIMIX_host_execution_resume(smx_action_t action)
330 if(action->execution.surf_exec)
331 surf_workstation_model->suspend(action->execution.surf_exec);
334 void SIMIX_execution_finish(smx_action_t action)
336 xbt_fifo_item_t item;
339 xbt_fifo_foreach(action->request_list, item, req, smx_req_t) {
341 switch (action->state) {
344 /* do nothing, action done*/
345 DEBUG0("SIMIX_execution_finished: execution successful");
350 DEBUG1("SIMIX_execution_finished: host '%s' failed", req->issuer->smx_host->name);
351 THROW0(host_error, 0, "Host failed");
353 CATCH(req->issuer->running_ctx->exception) {
354 req->issuer->doexception = 1;
360 DEBUG0("SIMIX_execution_finished: execution canceled");
361 THROW0(cancel_error, 0, "Canceled");
363 CATCH(req->issuer->running_ctx->exception) {
364 req->issuer->doexception = 1;
371 req->issuer->waiting_action = NULL;
372 SIMIX_request_answer(req);
376 void SIMIX_post_host_execute(smx_action_t action)
378 /* FIXME: check if the host running the action failed or not*/
379 /*if(surf_workstation_model->extension.workstation.get_state(action->host->host))*/
381 /* If the host running the action didn't fail, then the action was cancelled */
382 if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED)
383 action->state = SIMIX_CANCELED;
385 action->state = SIMIX_DONE;
387 if (action->execution.surf_exec) {
388 surf_workstation_model->action_unref(action->execution.surf_exec);
389 action->execution.surf_exec = NULL;
392 /* If there are requests associated with the action, then answer them */
393 if (xbt_fifo_size(action->request_list))
394 SIMIX_execution_finish(action);
399 void SIMIX_set_category(smx_action_t action, const char *category)
401 if (action->state != SIMIX_RUNNING) return;
402 if (action->type == SIMIX_ACTION_EXECUTE){
403 surf_workstation_model->set_category(action->execution.surf_exec, category);
404 }else if (action->type == SIMIX_ACTION_COMMUNICATE){
405 surf_workstation_model->set_category(action->comm.surf_comm, category);