Logo AND Algorithmique Numérique Distribuée

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