Logo AND Algorithmique Numérique Distribuée

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