Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
48ef546185966b99b91619a1adcf3b655c6a54b7
[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 {
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;
190
191 #ifdef HAVE_TRACING
192   action->category = NULL;
193 #endif
194
195   /* set surf's action */
196   if (!MC_IS_ENABLED) {
197     action->execution.surf_exec =
198       surf_workstation_model->extension.workstation.execute(host->host,
199           computation_amount);
200     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
201   }
202
203 #ifdef HAVE_TRACING
204   TRACE_smx_host_execute(action);
205   TRACE_surf_action(action->execution.surf_exec, action->category);
206 #endif
207
208   DEBUG1("Create execute action %p", action);
209
210   return action;
211 }
212
213 smx_action_t SIMIX_host_parallel_execute( const char *name,
214     int host_nb, smx_host_t *host_list,
215     double *computation_amount, double *communication_amount,
216     double amount, double rate)
217 {
218   void **workstation_list = NULL;
219   int i;
220
221   /* alloc structures and initialize */
222   smx_action_t action = xbt_new0(s_smx_action_t, 1);
223   action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
224   action->name = xbt_strdup(name);
225   action->request_list = xbt_fifo_new();
226   action->state = SIMIX_RUNNING;
227   action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
228
229 #ifdef HAVE_TRACING
230   action->category = NULL;
231 #endif
232
233   /* set surf's action */
234   workstation_list = xbt_new0(void *, host_nb);
235   for (i = 0; i < host_nb; i++)
236     workstation_list[i] = host_list[i]->host;
237
238   /* set surf's action */
239   if (!MC_IS_ENABLED) {
240     action->execution.surf_exec =
241       surf_workstation_model->extension.workstation.
242       execute_parallel_task(host_nb, workstation_list, computation_amount,
243                             communication_amount, amount, rate);
244
245     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
246   }
247   DEBUG1("Create parallel execute action %p", action);
248
249   return action;
250 }
251
252 void SIMIX_host_execution_destroy(smx_action_t action)
253 {
254   DEBUG1("Destroy action %p", action);
255
256   if (action->name)
257     xbt_free(action->name);
258
259   xbt_fifo_free(action->request_list);
260
261   if (action->execution.surf_exec) {
262     surf_workstation_model->action_unref(action->execution.surf_exec);
263     action->execution.surf_exec = NULL;
264   }
265
266 #ifdef HAVE_TRACING
267   TRACE_smx_action_destroy(action);
268 #endif
269   xbt_free(action);
270 }
271
272 void SIMIX_host_execution_cancel(smx_action_t action)
273 {
274   DEBUG1("Cancel action %p", action);
275
276   if (action->execution.surf_exec)
277     surf_workstation_model->action_cancel(action->execution.surf_exec);
278 }
279
280 double SIMIX_host_execution_get_remains(smx_action_t action)
281 {
282   double result = 0.0;
283
284   if (action->state == SIMIX_RUNNING)
285     result = surf_workstation_model->get_remains(action->execution.surf_exec);
286
287   return result;
288 }
289
290 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action)
291 {
292   return action->state;
293 }
294
295 void SIMIX_host_execution_set_priority(smx_action_t action, double priority)
296 {
297   surf_workstation_model->set_priority(action->execution.surf_exec, priority);
298 }
299
300 #ifdef HAVE_TRACING
301 void SIMIX_host_execution_set_category(smx_action_t action, const char *category)
302 {
303   surf_workstation_model->set_category(action->execution.surf_exec, category);
304 }
305 #endif
306
307 void SIMIX_pre_host_execution_wait(smx_req_t req)
308 {
309   smx_action_t action = req->host_execution_wait.execution;
310
311   DEBUG2("Wait for execution of action %p, state %d", action, action->state);
312
313   /* Associate this request to the action */
314   xbt_fifo_push(action->request_list, req);
315   req->issuer->waiting_action = action;
316
317   /* set surf's action */
318   if (MC_IS_ENABLED){
319     action->state = SIMIX_DONE;
320     SIMIX_execution_finish(action);
321   }
322
323   /* If the action is already finished then perform the error handling */
324   if (action->state != SIMIX_RUNNING)
325     SIMIX_execution_finish(action);
326 }
327
328 void SIMIX_host_execution_suspend(smx_action_t action)
329 {
330   surf_workstation_model->suspend(action->execution.surf_exec);
331 }
332
333 void SIMIX_host_execution_resume(smx_action_t action)
334 {
335   surf_workstation_model->suspend(action->execution.surf_exec);
336 }
337
338 void SIMIX_execution_finish(smx_action_t action)
339 {
340   xbt_fifo_item_t item;
341   smx_req_t req;
342
343   xbt_fifo_foreach(action->request_list, item, req, smx_req_t) {
344
345     switch (action->state) {
346
347       case SIMIX_DONE:
348         /* do nothing, action done*/
349         DEBUG0("SIMIX_execution_finished: execution successful");
350         break;
351
352       case SIMIX_FAILED:
353         TRY {
354           DEBUG1("SIMIX_execution_finished: host '%s' failed", req->issuer->smx_host->name);
355           THROW0(host_error, 0, "Host failed");
356         }
357         CATCH(req->issuer->running_ctx->exception) {
358           req->issuer->doexception = 1;
359         }
360       break;
361
362       case SIMIX_CANCELED:
363         TRY {
364           DEBUG0("SIMIX_execution_finished: execution canceled");
365           THROW0(cancel_error, 0, "Canceled");
366         }
367         CATCH(req->issuer->running_ctx->exception) {
368           req->issuer->doexception = 1;
369         }
370         break;
371
372       default:
373         THROW_IMPOSSIBLE;
374     }
375     req->issuer->waiting_action = NULL;
376     SIMIX_request_answer(req);
377   }
378 }
379
380 void SIMIX_post_host_execute(smx_action_t action)
381 {
382   /* FIXME: check if the host running the action failed or not*/
383   /*if(surf_workstation_model->extension.workstation.get_state(action->host->host))*/
384
385   /* If the host running the action didn't fail, then the action was cancelled */
386   if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED)
387      action->state = SIMIX_CANCELED;
388   else
389      action->state = SIMIX_DONE;
390
391   if (action->execution.surf_exec) {
392     surf_workstation_model->action_unref(action->execution.surf_exec);
393     action->execution.surf_exec = NULL;
394   }
395
396   /* If there are requests associated with the action, then answer them */
397   if (xbt_fifo_size(action->request_list))
398     SIMIX_execution_finish(action);
399 }
400