Logo AND Algorithmique Numérique Distribuée

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