Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First bricks for auto_restart support in SIMIX/MSG.
[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 "smx_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   xbt_dynar_free(&host->auto_restart_processes);
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_homogeneous(NULL);
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 simcall 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 void _SIMIX_host_free_process_arg(void *);
180 void _SIMIX_host_free_process_arg(void *data) {
181   smx_process_arg_t arg = *(void**)data;
182   int i;
183   xbt_free(arg->name);
184   for (i = 0; i < arg->argc; i++) {
185     xbt_free(arg->argv[i]);
186   }
187   xbt_free(arg->argv);
188   xbt_free(arg);
189 }
190 void SIMIX_host_add_auto_restart_process(smx_host_t host,
191                                          const char *name,
192                                          xbt_main_func_t code,
193                                          void *data,
194                                          const char *hostname,
195                                          double kill_time,
196                                          int argc, char **argv,
197                                          xbt_dict_t properties,
198                                          int auto_restart) {
199   if (!host->auto_restart_processes) {
200     host->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
201   }
202   smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
203
204   arg->name = xbt_strdup(name);
205   arg->code = code;
206   arg->data = data;
207   arg->hostname = hostname;
208   arg->kill_time = kill_time;
209   arg->argc = argc;
210   arg->argv = xbt_new(char*,argc + 1);
211
212   int i;
213   for (i = 0; i < argc; i++) {
214     arg->argv[i] = xbt_strdup(argv[i]);
215   }
216
217   arg->properties = properties;
218   arg->auto_restart = auto_restart;
219
220   xbt_dynar_push_as(host->auto_restart_processes,smx_process_arg_t,arg);
221 }
222
223
224 void SIMIX_host_set_data(smx_host_t host, void *data)
225 {
226   xbt_assert((host != NULL), "Invalid parameters");
227   xbt_assert((host->data == NULL), "Data already set");
228
229   host->data = data;
230 }
231
232 smx_action_t SIMIX_host_execute(const char *name, smx_host_t host,
233                                 double computation_amount,
234                                 double priority)
235 {
236   /* alloc structures and initialize */
237   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
238   action->type = SIMIX_ACTION_EXECUTE;
239   action->name = xbt_strdup(name);
240   action->state = SIMIX_RUNNING;
241   action->execution.host = host;
242
243 #ifdef HAVE_TRACING
244   action->category = NULL;
245 #endif
246
247   /* set surf's action */
248   if (!MC_IS_ENABLED) {
249     action->execution.surf_exec =
250       surf_workstation_model->extension.workstation.execute(host->host,
251     computation_amount);
252     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
253     surf_workstation_model->set_priority(action->execution.surf_exec, priority);
254   }
255
256   XBT_DEBUG("Create execute action %p", action);
257
258   return action;
259 }
260
261 smx_action_t SIMIX_host_parallel_execute( const char *name,
262     int host_nb, smx_host_t *host_list,
263     double *computation_amount, double *communication_amount,
264     double amount, double rate)
265 {
266   void **workstation_list = NULL;
267   int i;
268
269   /* alloc structures and initialize */
270   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
271   action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
272   action->name = xbt_strdup(name);
273   action->state = SIMIX_RUNNING;
274   action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
275
276 #ifdef HAVE_TRACING
277   action->category = NULL;
278 #endif
279
280   /* set surf's action */
281   workstation_list = xbt_new0(void *, host_nb);
282   for (i = 0; i < host_nb; i++)
283     workstation_list[i] = host_list[i]->host;
284
285   /* set surf's action */
286   if (!MC_IS_ENABLED) {
287     action->execution.surf_exec =
288       surf_workstation_model->extension.workstation.
289       execute_parallel_task(host_nb, workstation_list, computation_amount,
290                       communication_amount, rate);
291
292     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
293   }
294   XBT_DEBUG("Create parallel execute action %p", action);
295
296   return action;
297 }
298
299 void SIMIX_host_execution_destroy(smx_action_t action)
300 {
301   int destroyed=0;
302   XBT_DEBUG("Destroy action %p", action);
303
304
305   if (action->execution.surf_exec) {
306     destroyed = surf_workstation_model->action_unref(action->execution.surf_exec);
307     action->execution.surf_exec = NULL;
308   }
309
310   if (destroyed) {
311     xbt_free(action->name);
312     xbt_mallocator_release(simix_global->action_mallocator, action);
313   }
314 }
315
316 void SIMIX_host_execution_cancel(smx_action_t action)
317 {
318   XBT_DEBUG("Cancel action %p", action);
319
320   if (action->execution.surf_exec)
321     surf_workstation_model->action_cancel(action->execution.surf_exec);
322 }
323
324 double SIMIX_host_execution_get_remains(smx_action_t action)
325 {
326   double result = 0.0;
327
328   if (action->state == SIMIX_RUNNING)
329     result = surf_workstation_model->get_remains(action->execution.surf_exec);
330
331   return result;
332 }
333
334 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action)
335 {
336   return action->state;
337 }
338
339 void SIMIX_host_execution_set_priority(smx_action_t action, double priority)
340 {
341   if(action->execution.surf_exec)
342     surf_workstation_model->set_priority(action->execution.surf_exec, priority);
343 }
344
345 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall)
346 {
347   smx_action_t action = simcall->host_execution_wait.execution;
348
349   XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
350
351   /* Associate this simcall to the action */
352   xbt_fifo_push(action->simcalls, simcall);
353   simcall->issuer->waiting_action = action;
354
355   /* set surf's action */
356   if (MC_IS_ENABLED) {
357     action->state = SIMIX_DONE;
358     SIMIX_execution_finish(action);
359     return;
360   }
361
362   /* If the action is already finished then perform the error handling */
363   if (action->state != SIMIX_RUNNING)
364     SIMIX_execution_finish(action);
365 }
366
367 void SIMIX_host_execution_suspend(smx_action_t action)
368 {
369   if(action->execution.surf_exec)
370     surf_workstation_model->suspend(action->execution.surf_exec);
371 }
372
373 void SIMIX_host_execution_resume(smx_action_t action)
374 {
375   if(action->execution.surf_exec)
376     surf_workstation_model->resume(action->execution.surf_exec);
377 }
378
379 void SIMIX_execution_finish(smx_action_t action)
380 {
381   xbt_fifo_item_t item;
382   smx_simcall_t simcall;
383
384   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
385
386     switch (action->state) {
387
388       case SIMIX_DONE:
389         /* do nothing, action done */
390   XBT_DEBUG("SIMIX_execution_finished: execution successful");
391         break;
392
393       case SIMIX_FAILED:
394         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->smx_host->name);
395         simcall->issuer->context->iwannadie = 1;
396         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
397         break;
398
399       case SIMIX_CANCELED:
400         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
401         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
402         break;
403
404       default:
405         xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
406             (int)action->state);
407     }
408     /* check if the host is down */
409     if (surf_workstation_model->extension.
410         workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
411       simcall->issuer->context->iwannadie = 1;
412     }
413
414     simcall->issuer->waiting_action =    NULL;
415     simcall->host_execution_wait.result = action->state;
416     SIMIX_simcall_answer(simcall);
417   }
418
419   /* We no longer need it */
420   SIMIX_host_execution_destroy(action);
421 }
422
423 void SIMIX_post_host_execute(smx_action_t action)
424 {
425   if (surf_workstation_model->extension.workstation.get_state(action->execution.host->host)==SURF_RESOURCE_OFF) {
426     /* if the host running the action failed, notice it so that the asking process can be killed if it runs on that host itself */
427     action->state = SIMIX_FAILED;
428   } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
429     /* If the host running the action didn't fail, then the action was canceled */
430      action->state = SIMIX_CANCELED;
431   } else {
432      action->state = SIMIX_DONE;
433   }
434
435   if (action->execution.surf_exec) {
436     surf_workstation_model->action_unref(action->execution.surf_exec);
437     action->execution.surf_exec = NULL;
438   }
439
440   /* If there are simcalls associated with the action, then answer them */
441   if (xbt_fifo_size(action->simcalls)) {
442     SIMIX_execution_finish(action);
443   }
444 }
445
446
447 #ifdef HAVE_TRACING
448 void SIMIX_set_category(smx_action_t action, const char *category)
449 {
450   if (action->state != SIMIX_RUNNING) return;
451   if (action->type == SIMIX_ACTION_EXECUTE){
452     surf_workstation_model->set_category(action->execution.surf_exec, category);
453   }else if (action->type == SIMIX_ACTION_COMMUNICATE){
454     surf_workstation_model->set_category(action->comm.surf_comm, category);
455   }
456 }
457 #endif
458