Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b771e858ca3ac9fc1728c86a48b3e8773a65bae8
[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 xbt_dict_t watched_hosts_lib;
17
18 static void SIMIX_execution_finish(smx_action_t action);
19
20 /**
21  * \brief Internal function to create a SIMIX host.
22  * \param name name of the host to create
23  * \param workstation the SURF workstation to encapsulate
24  * \param data some user data (may be NULL)
25  */
26 smx_host_t SIMIX_host_create(const char *name,
27                                void *workstation, void *data)
28 {
29   smx_host_t smx_host = xbt_new0(s_smx_host_t, 1);
30   s_smx_process_t proc;
31
32   /* Host structure */
33   smx_host->name = xbt_strdup(name);
34   smx_host->data = data;
35   smx_host->host = workstation;
36   smx_host->process_list =
37       xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
38
39   /* Update global variables */
40   xbt_lib_set(host_lib,smx_host->name,SIMIX_HOST_LEVEL,smx_host);
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_assert((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     THROWF(arg_error, 0, "%s", msg);
70   }
71   xbt_dynar_free(&host->auto_restart_processes);
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   xbt_dict_t host_dict = xbt_dict_new_homogeneous(NULL);
89   xbt_lib_cursor_t cursor = NULL;
90   char *name = NULL;
91   void **host = NULL;
92
93   xbt_lib_foreach(host_lib, cursor, name, host){
94     if(host[SIMIX_HOST_LEVEL])
95             xbt_dict_set(host_dict,name,host[SIMIX_HOST_LEVEL], NULL);
96   }
97   return host_dict;
98 }
99
100 smx_host_t SIMIX_host_get_by_name(const char *name)
101 {
102   xbt_assert(((simix_global != NULL)
103                && (host_lib != NULL)),
104               "Environment not set yet");
105
106   return xbt_lib_get_or_null(host_lib, name, SIMIX_HOST_LEVEL);
107 }
108
109 smx_host_t SIMIX_host_self(void)
110 {
111   smx_process_t process = SIMIX_process_self();
112   return (process == NULL) ? NULL : SIMIX_process_get_host(process);
113 }
114
115 /* needs to be public and without simcall because it is called
116    by exceptions and logging events */
117 const char* SIMIX_host_self_get_name(void)
118 {
119   smx_host_t host = SIMIX_host_self();
120   if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
121     return "";
122
123   return SIMIX_host_get_name(host);
124 }
125
126 const char* SIMIX_host_get_name(smx_host_t host)
127 {
128   xbt_assert((host != NULL), "Invalid parameters");
129
130   return host->name;
131 }
132
133 xbt_dict_t SIMIX_host_get_properties(smx_host_t host)
134 {
135   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
136
137   return surf_workstation_model->extension.workstation.get_properties(host->host);
138 }
139
140 double SIMIX_host_get_speed(smx_host_t host)
141 {
142   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
143
144   return surf_workstation_model->extension.workstation.
145       get_speed(host->host, 1.0);
146 }
147
148 double SIMIX_host_get_available_speed(smx_host_t host)
149 {
150   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
151
152   return surf_workstation_model->extension.workstation.
153       get_available_speed(host->host);
154 }
155
156 int SIMIX_host_get_state(smx_host_t host)
157 {
158   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
159
160   return surf_workstation_model->extension.workstation.
161       get_state(host->host);
162 }
163
164 void* SIMIX_host_self_get_data(void)
165 {
166   return SIMIX_host_get_data(SIMIX_host_self());
167 }
168
169 void SIMIX_host_self_set_data(void *data)
170 {
171   SIMIX_host_set_data(SIMIX_host_self(), data);
172 }
173
174 void* SIMIX_host_get_data(smx_host_t host)
175 {
176   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
177
178   return host->data;
179 }
180 void _SIMIX_host_free_process_arg(void *);
181 void _SIMIX_host_free_process_arg(void *data) {
182   smx_process_arg_t arg = *(void**)data;
183   int i;
184   xbt_free(arg->name);
185   for (i = 0; i < arg->argc; i++) {
186     xbt_free(arg->argv[i]);
187   }
188   xbt_free(arg->argv);
189   xbt_free(arg);
190 }
191 void SIMIX_host_add_auto_restart_process(smx_host_t host,
192                                          const char *name,
193                                          xbt_main_func_t code,
194                                          void *data,
195                                          const char *hostname,
196                                          double kill_time,
197                                          int argc, char **argv,
198                                          xbt_dict_t properties,
199                                          int auto_restart) {
200   if (!host->auto_restart_processes) {
201     host->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
202   }
203   smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
204
205   arg->name = xbt_strdup(name);
206   arg->code = code;
207   arg->data = data;
208   arg->hostname = hostname;
209   arg->kill_time = kill_time;
210   arg->argc = argc;
211   arg->argv = xbt_new(char*,argc + 1);
212
213   int i;
214   for (i = 0; i < argc; i++) {
215     arg->argv[i] = xbt_strdup(argv[i]);
216   }
217
218   arg->properties = properties;
219   arg->auto_restart = auto_restart;
220
221   if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
222       && !xbt_dict_get_or_null(watched_hosts_lib,host->name)){
223     xbt_dict_set(watched_hosts_lib,host->name,host,NULL);
224     XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",host->name);
225   }
226   xbt_dynar_push_as(host->auto_restart_processes,smx_process_arg_t,arg);
227 }
228
229
230 void SIMIX_host_set_data(smx_host_t host, void *data)
231 {
232   xbt_assert((host != NULL), "Invalid parameters");
233   xbt_assert((host->data == NULL), "Data already set");
234
235   host->data = data;
236 }
237
238 smx_action_t SIMIX_host_execute(const char *name, smx_host_t host,
239                                 double computation_amount,
240                                 double priority)
241 {
242   /* alloc structures and initialize */
243   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
244   action->type = SIMIX_ACTION_EXECUTE;
245   action->name = xbt_strdup(name);
246   action->state = SIMIX_RUNNING;
247   action->execution.host = host;
248
249 #ifdef HAVE_TRACING
250   action->category = NULL;
251 #endif
252
253   /* set surf's action */
254   if (!MC_IS_ENABLED) {
255     action->execution.surf_exec =
256       surf_workstation_model->extension.workstation.execute(host->host,
257     computation_amount);
258     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
259     surf_workstation_model->set_priority(action->execution.surf_exec, priority);
260   }
261
262   XBT_DEBUG("Create execute action %p", action);
263
264   return action;
265 }
266
267 smx_action_t SIMIX_host_parallel_execute( const char *name,
268     int host_nb, smx_host_t *host_list,
269     double *computation_amount, double *communication_amount,
270     double amount, double rate)
271 {
272   void **workstation_list = NULL;
273   int i;
274
275   /* alloc structures and initialize */
276   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
277   action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
278   action->name = xbt_strdup(name);
279   action->state = SIMIX_RUNNING;
280   action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
281
282 #ifdef HAVE_TRACING
283   action->category = NULL;
284 #endif
285
286   /* set surf's action */
287   workstation_list = xbt_new0(void *, host_nb);
288   for (i = 0; i < host_nb; i++)
289     workstation_list[i] = host_list[i]->host;
290
291   /* set surf's action */
292   if (!MC_IS_ENABLED) {
293     action->execution.surf_exec =
294       surf_workstation_model->extension.workstation.
295       execute_parallel_task(host_nb, workstation_list, computation_amount,
296                       communication_amount, rate);
297
298     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
299   }
300   XBT_DEBUG("Create parallel execute action %p", action);
301
302   return action;
303 }
304
305 void SIMIX_host_execution_destroy(smx_action_t action)
306 {
307   int destroyed=0;
308   XBT_DEBUG("Destroy action %p", action);
309
310
311   if (action->execution.surf_exec) {
312     destroyed = surf_workstation_model->action_unref(action->execution.surf_exec);
313     action->execution.surf_exec = NULL;
314   }
315
316   if (destroyed) {
317     xbt_free(action->name);
318     xbt_mallocator_release(simix_global->action_mallocator, action);
319   }
320 }
321
322 void SIMIX_host_execution_cancel(smx_action_t action)
323 {
324   XBT_DEBUG("Cancel action %p", action);
325
326   if (action->execution.surf_exec)
327     surf_workstation_model->action_cancel(action->execution.surf_exec);
328 }
329
330 double SIMIX_host_execution_get_remains(smx_action_t action)
331 {
332   double result = 0.0;
333
334   if (action->state == SIMIX_RUNNING)
335     result = surf_workstation_model->get_remains(action->execution.surf_exec);
336
337   return result;
338 }
339
340 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action)
341 {
342   return action->state;
343 }
344
345 void SIMIX_host_execution_set_priority(smx_action_t action, double priority)
346 {
347   if(action->execution.surf_exec)
348     surf_workstation_model->set_priority(action->execution.surf_exec, priority);
349 }
350
351 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall)
352 {
353   smx_action_t action = simcall->host_execution_wait.execution;
354
355   XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
356
357   /* Associate this simcall to the action */
358   xbt_fifo_push(action->simcalls, simcall);
359   simcall->issuer->waiting_action = action;
360
361   /* set surf's action */
362   if (MC_IS_ENABLED) {
363     action->state = SIMIX_DONE;
364     SIMIX_execution_finish(action);
365     return;
366   }
367
368   /* If the action is already finished then perform the error handling */
369   if (action->state != SIMIX_RUNNING)
370     SIMIX_execution_finish(action);
371 }
372
373 void SIMIX_host_execution_suspend(smx_action_t action)
374 {
375   if(action->execution.surf_exec)
376     surf_workstation_model->suspend(action->execution.surf_exec);
377 }
378
379 void SIMIX_host_execution_resume(smx_action_t action)
380 {
381   if(action->execution.surf_exec)
382     surf_workstation_model->resume(action->execution.surf_exec);
383 }
384
385 void SIMIX_execution_finish(smx_action_t action)
386 {
387   xbt_fifo_item_t item;
388   smx_simcall_t simcall;
389
390   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
391
392     switch (action->state) {
393
394       case SIMIX_DONE:
395         /* do nothing, action done */
396   XBT_DEBUG("SIMIX_execution_finished: execution successful");
397         break;
398
399       case SIMIX_FAILED:
400         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->smx_host->name);
401         simcall->issuer->context->iwannadie = 1;
402         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
403         break;
404
405       case SIMIX_CANCELED:
406         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
407         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
408         break;
409
410       default:
411         xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
412             (int)action->state);
413     }
414     /* check if the host is down */
415     if (surf_workstation_model->extension.
416         workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
417       simcall->issuer->context->iwannadie = 1;
418     }
419
420     simcall->issuer->waiting_action =    NULL;
421     simcall->host_execution_wait.result = action->state;
422     SIMIX_simcall_answer(simcall);
423   }
424
425   /* We no longer need it */
426   SIMIX_host_execution_destroy(action);
427 }
428
429 void SIMIX_post_host_execute(smx_action_t action)
430 {
431   if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
432                                                * for parallel tasks too */
433       surf_workstation_model->extension.workstation.get_state(action->execution.host->host) == SURF_RESOURCE_OFF) {
434     /* If the host running the action failed, notice it so that the asking
435      * process can be killed if it runs on that host itself */
436     action->state = SIMIX_FAILED;
437   } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
438     /* If the host running the action didn't fail, then the action was
439      * canceled */
440     action->state = SIMIX_CANCELED;
441   } else {
442     action->state = SIMIX_DONE;
443   }
444
445   if (action->execution.surf_exec) {
446     surf_workstation_model->action_unref(action->execution.surf_exec);
447     action->execution.surf_exec = NULL;
448   }
449
450   /* If there are simcalls associated with the action, then answer them */
451   if (xbt_fifo_size(action->simcalls)) {
452     SIMIX_execution_finish(action);
453   }
454 }
455
456
457 #ifdef HAVE_TRACING
458 void SIMIX_set_category(smx_action_t action, const char *category)
459 {
460   if (action->state != SIMIX_RUNNING) return;
461   if (action->type == SIMIX_ACTION_EXECUTE){
462     surf_workstation_model->set_category(action->execution.surf_exec, category);
463   }else if (action->type == SIMIX_ACTION_COMMUNICATE){
464     surf_workstation_model->set_category(action->comm.surf_comm, category);
465   }
466 }
467 #endif
468