Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Return the host failures in the same process sleep requests.
[simgrid.git] / src / simix / smx_host.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix,
14                                 "Logging specific to SIMIX (hosts)");
15
16
17 static void SIMIX_execution_finish(smx_action_t action);
18
19 /**
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)
24  */
25 smx_host_t SIMIX_host_create(const char *name,
26                                void *workstation, void *data)
27 {
28   smx_host_t smx_host = xbt_new0(s_smx_host_t, 1);
29   s_smx_process_t proc;
30
31   /* Host structure */
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));
37
38   /* Update global variables */
39   xbt_dict_set(simix_global->host, smx_host->name, smx_host,
40                &SIMIX_host_destroy);
41
42   return smx_host;
43 }
44
45 /**
46  * \brief Internal function to destroy a SIMIX host.
47  *
48  * \param h the host to destroy (a smx_host_t)
49  */
50 void SIMIX_host_destroy(void *h)
51 {
52   smx_host_t host = (smx_host_t) h;
53
54   xbt_assert0((host != NULL), "Invalid parameters");
55
56   /* Clean Simulator data */
57   if (xbt_swag_size(host->process_list) != 0) {
58     char *msg =
59         bprintf("Shutting down host %s, but it's not empty:", host->name);
60     char *tmp;
61     smx_process_t process = NULL;
62
63     xbt_swag_foreach(process, host->process_list) {
64       tmp = bprintf("%s\n\t%s", msg, process->name);
65       free(msg);
66       msg = tmp;
67     }
68     SIMIX_display_process_status();
69     THROW1(arg_error, 0, "%s", msg);
70   }
71
72   xbt_swag_free(host->process_list);
73
74   /* Clean host structure */
75   free(host->name);
76   free(host);
77
78   return;
79 }
80
81 /**
82  * \brief Returns a dict of all hosts.
83  *
84  * \return List of all hosts (as a #xbt_dict_t)
85  */
86 xbt_dict_t SIMIX_host_get_dict(void)
87 {
88   return simix_global->host;
89 }
90
91 smx_host_t SIMIX_host_get_by_name(const char *name)
92 {
93   xbt_assert0(((simix_global != NULL)
94                && (simix_global->host != NULL)),
95               "Environment not set yet");
96
97   return xbt_dict_get_or_null(simix_global->host, name);
98 }
99
100 smx_host_t SIMIX_host_self(void)
101 {
102   smx_process_t process = SIMIX_process_self();
103   return (process == NULL) ? NULL : SIMIX_process_get_host(process);
104 }
105
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)
109 {
110   smx_host_t host = SIMIX_host_self();
111   if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
112     return "";
113
114   return SIMIX_host_get_name(host);
115 }
116
117 const char* SIMIX_host_get_name(smx_host_t host)
118 {
119   xbt_assert0((host != NULL), "Invalid parameters");
120
121   return host->name;
122 }
123
124 xbt_dict_t SIMIX_host_get_properties(smx_host_t host)
125 {
126   xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
127
128   return surf_workstation_model->extension.workstation.get_properties(host->host);
129 }
130
131 double SIMIX_host_get_speed(smx_host_t host)
132 {
133   xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
134
135   return surf_workstation_model->extension.workstation.
136       get_speed(host->host, 1.0);
137 }
138
139 double SIMIX_host_get_available_speed(smx_host_t host)
140 {
141   xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
142
143   return surf_workstation_model->extension.workstation.
144       get_available_speed(host->host);
145 }
146
147 int SIMIX_host_get_state(smx_host_t host)
148 {
149   xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
150
151   return surf_workstation_model->extension.workstation.
152       get_state(host->host);
153 }
154
155 void* SIMIX_host_self_get_data(void)
156 {
157   return SIMIX_host_get_data(SIMIX_host_self());
158 }
159
160 void SIMIX_host_self_set_data(void *data)
161 {
162   SIMIX_host_set_data(SIMIX_host_self(), data);
163 }
164
165 void* SIMIX_host_get_data(smx_host_t host)
166 {
167   xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
168
169   return host->data;
170 }
171
172 void SIMIX_host_set_data(smx_host_t host, void *data)
173 {
174   xbt_assert0((host != NULL), "Invalid parameters");
175   xbt_assert0((host->data == NULL), "Data already set");
176
177   host->data = data;
178 }
179
180 smx_action_t SIMIX_host_execute(const char *name, smx_host_t host,
181                                 double computation_amount,
182                                 double priority)
183 {
184   /* alloc structures and initialize */
185   smx_action_t action = xbt_new0(s_smx_action_t, 1);
186   action->type = SIMIX_ACTION_EXECUTE;
187   action->name = xbt_strdup(name);
188   action->request_list = xbt_fifo_new();
189   action->state = SIMIX_RUNNING;
190   action->execution.host = host;
191
192 #ifdef HAVE_TRACING
193   action->category = NULL;
194 #endif
195
196   /* set surf's action */
197   if (!MC_IS_ENABLED) {
198     action->execution.surf_exec =
199       surf_workstation_model->extension.workstation.execute(host->host,
200           computation_amount);
201     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
202     surf_workstation_model->set_priority(action->execution.surf_exec, priority);
203   }
204
205 #ifdef HAVE_TRACING
206   TRACE_smx_host_execute(action);
207 #endif
208
209   DEBUG1("Create execute action %p", action);
210
211   return action;
212 }
213
214 smx_action_t SIMIX_host_parallel_execute( const char *name,
215     int host_nb, smx_host_t *host_list,
216     double *computation_amount, double *communication_amount,
217     double amount, double rate)
218 {
219   void **workstation_list = NULL;
220   int i;
221
222   /* alloc structures and initialize */
223   smx_action_t action = xbt_new0(s_smx_action_t, 1);
224   action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
225   action->name = xbt_strdup(name);
226   action->request_list = xbt_fifo_new();
227   action->state = SIMIX_RUNNING;
228   action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
229
230 #ifdef HAVE_TRACING
231   action->category = NULL;
232 #endif
233
234   /* set surf's action */
235   workstation_list = xbt_new0(void *, host_nb);
236   for (i = 0; i < host_nb; i++)
237     workstation_list[i] = host_list[i]->host;
238
239   /* set surf's action */
240   if (!MC_IS_ENABLED) {
241     action->execution.surf_exec =
242       surf_workstation_model->extension.workstation.
243       execute_parallel_task(host_nb, workstation_list, computation_amount,
244                             communication_amount, amount, rate);
245
246     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
247   }
248   DEBUG1("Create parallel execute action %p", action);
249
250   return action;
251 }
252
253 void SIMIX_host_execution_destroy(smx_action_t action)
254 {
255   DEBUG1("Destroy action %p", action);
256
257   if (action->name)
258     xbt_free(action->name);
259
260   xbt_fifo_free(action->request_list);
261
262   if (action->execution.surf_exec) {
263     surf_workstation_model->action_unref(action->execution.surf_exec);
264     action->execution.surf_exec = NULL;
265   }
266
267 #ifdef HAVE_TRACING
268   TRACE_smx_action_destroy(action);
269 #endif
270   xbt_free(action);
271 }
272
273 void SIMIX_host_execution_cancel(smx_action_t action)
274 {
275   DEBUG1("Cancel action %p", action);
276
277   if (action->execution.surf_exec)
278     surf_workstation_model->action_cancel(action->execution.surf_exec);
279 }
280
281 double SIMIX_host_execution_get_remains(smx_action_t action)
282 {
283   double result = 0.0;
284
285   if (action->state == SIMIX_RUNNING)
286     result = surf_workstation_model->get_remains(action->execution.surf_exec);
287
288   return result;
289 }
290
291 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action)
292 {
293   return action->state;
294 }
295
296 void SIMIX_host_execution_set_priority(smx_action_t action, double priority)
297 {
298   if(action->execution.surf_exec)
299     surf_workstation_model->set_priority(action->execution.surf_exec, priority);
300 }
301
302 void SIMIX_pre_host_execution_wait(smx_req_t req)
303 {
304   smx_action_t action = req->host_execution_wait.execution;
305
306   DEBUG2("Wait for execution of action %p, state %d", action, action->state);
307
308   /* Associate this request to the action */
309   xbt_fifo_push(action->request_list, req);
310   req->issuer->waiting_action = action;
311
312   /* set surf's action */
313   if (MC_IS_ENABLED){
314     action->state = SIMIX_DONE;
315     SIMIX_execution_finish(action);
316     return;
317   }
318
319   /* If the action is already finished then perform the error handling */
320   if (action->state != SIMIX_RUNNING)
321     SIMIX_execution_finish(action);
322 }
323
324 void SIMIX_host_execution_suspend(smx_action_t action)
325 {
326   if(action->execution.surf_exec)
327     surf_workstation_model->suspend(action->execution.surf_exec);
328 }
329
330 void SIMIX_host_execution_resume(smx_action_t action)
331 {
332   if(action->execution.surf_exec)
333     surf_workstation_model->suspend(action->execution.surf_exec);
334 }
335
336 void SIMIX_execution_finish(smx_action_t action)
337 {
338   xbt_fifo_item_t item;
339   smx_req_t req;
340
341   xbt_fifo_foreach(action->request_list, item, req, smx_req_t) {
342
343     switch (action->state) {
344
345       case SIMIX_DONE:
346         /* do nothing, action done*/
347         DEBUG0("SIMIX_execution_finished: execution successful");
348         break;
349
350       case SIMIX_FAILED:
351         TRY {
352           DEBUG1("SIMIX_execution_finished: host '%s' failed", req->issuer->smx_host->name);
353           THROW0(host_error, 0, "Host failed");
354         }
355         CATCH(req->issuer->running_ctx->exception) {
356           req->issuer->doexception = 1;
357         }
358       break;
359
360       case SIMIX_CANCELED:
361         TRY {
362           DEBUG0("SIMIX_execution_finished: execution canceled");
363           THROW0(cancel_error, 0, "Canceled");
364         }
365         CATCH(req->issuer->running_ctx->exception) {
366           req->issuer->doexception = 1;
367         }
368         break;
369
370       default:
371         THROW_IMPOSSIBLE;
372     }
373     req->issuer->waiting_action = NULL;
374     req->host_execution_wait.result = action->state;
375     SIMIX_request_answer(req);
376   }
377
378   /* We no longer need it */
379   SIMIX_host_execution_destroy(action);
380 }
381
382 void SIMIX_post_host_execute(smx_action_t action)
383 {
384   /* FIXME: check if the host running the action failed or not*/
385   /*if(surf_workstation_model->extension.workstation.get_state(action->host->host))*/
386
387   /* If the host running the action didn't fail, then the action was cancelled */
388   if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED)
389      action->state = SIMIX_CANCELED;
390   else
391      action->state = SIMIX_DONE;
392
393   if (action->execution.surf_exec) {
394     surf_workstation_model->action_unref(action->execution.surf_exec);
395     action->execution.surf_exec = NULL;
396   }
397
398   /* If there are requests associated with the action, then answer them */
399   if (xbt_fifo_size(action->request_list))
400     SIMIX_execution_finish(action);
401 }
402
403
404 #ifdef HAVE_TRACING
405 void SIMIX_set_category(smx_action_t action, const char *category)
406 {
407   if (action->state != SIMIX_RUNNING) return;
408   if (action->type == SIMIX_ACTION_EXECUTE){
409     surf_workstation_model->set_category(action->execution.surf_exec, category);
410   }else if (action->type == SIMIX_ACTION_COMMUNICATE){
411     surf_workstation_model->set_category(action->comm.surf_comm, category);
412   }
413 }
414 #endif
415