Logo AND Algorithmique Numérique Distribuée

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