Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[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_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
28   s_smx_process_t proc;
29
30   /* Host structure */
31   smx_host->data = data;
32   smx_host->process_list =
33       xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
34
35   /* Update global variables */
36   xbt_lib_set(host_lib,name,SIMIX_HOST_LEVEL,smx_host);
37   
38   return xbt_lib_get_elm_or_null(host_lib, name);
39 }
40
41 /**
42  * \brief Internal function to destroy a SIMIX host.
43  *
44  * \param h the host to destroy (a smx_host_t)
45  */
46 void SIMIX_host_destroy(void *h)
47 {
48   smx_host_priv_t host = (smx_host_priv_t) h;
49
50   xbt_assert((host != NULL), "Invalid parameters");
51
52   /* Clean Simulator data */
53   if (xbt_swag_size(host->process_list) != 0) {
54     char *msg = xbt_strdup("Shutting down host, but it's not empty:");
55     char *tmp;
56     smx_process_t process = NULL;
57
58     xbt_swag_foreach(process, host->process_list) {
59       tmp = bprintf("%s\n\t%s", msg, process->name);
60       free(msg);
61       msg = tmp;
62     }
63     SIMIX_display_process_status();
64     THROWF(arg_error, 0, "%s", msg);
65   }
66   xbt_dynar_free(&host->auto_restart_processes);
67   xbt_swag_free(host->process_list);
68
69   /* Clean host structure */
70   free(host); 
71   return;
72 }
73
74 ///**
75 // * \brief Returns a dict of all hosts.
76 // *
77 // * \return List of all hosts (as a #xbt_dict_t)
78 // */
79 //xbt_dict_t SIMIX_host_get_dict(void)
80 //{
81 //  xbt_dict_t host_dict = xbt_dict_new_homogeneous(NULL);
82 //  xbt_lib_cursor_t cursor = NULL;
83 //  char *name = NULL;
84 //  void **host = NULL;
85 //
86 //  xbt_lib_foreach(host_lib, cursor, name, host){
87 //    if(host[SIMIX_HOST_LEVEL])
88 //            xbt_dict_set(host_dict,name,host[SIMIX_HOST_LEVEL], NULL);
89 //  }
90 //  return host_dict;
91 //}
92 smx_host_t SIMIX_pre_host_get_by_name(smx_simcall_t simcall, const char *name){
93    return SIMIX_host_get_by_name(name);
94 }
95 smx_host_t SIMIX_host_get_by_name(const char *name){
96   xbt_assert(((simix_global != NULL)
97                && (host_lib != NULL)),
98               "Environment not set yet");
99
100   return xbt_lib_get_elm_or_null(host_lib, name);
101 }
102
103 smx_host_t SIMIX_host_self(void)
104 {
105   smx_process_t process = SIMIX_process_self();
106   return (process == NULL) ? NULL : SIMIX_process_get_host(process);
107 }
108
109 const char* SIMIX_pre_host_self_get_name(smx_simcall_t simcall){
110    return SIMIX_host_self_get_name();
111 }
112 /* needs to be public and without simcall because it is called
113    by exceptions and logging events */
114 const char* SIMIX_host_self_get_name(void)
115 {
116   smx_host_t host = SIMIX_host_self();
117   if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
118     return "";
119
120   return SIMIX_host_get_name(host);
121 }
122
123 const char* SIMIX_pre_host_get_name(smx_simcall_t simcall, smx_host_t host){
124    return SIMIX_host_get_name(host);
125 }
126 const char* SIMIX_host_get_name(smx_host_t host){
127   xbt_assert((host != NULL), "Invalid parameters");
128
129   return sg_host_name(host);
130 }
131
132 xbt_dict_t SIMIX_pre_host_get_properties(smx_simcall_t simcall, smx_host_t host){
133   return SIMIX_host_get_properties(host);
134 }
135 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
136   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
137
138   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
139   return ws_model->extension.workstation.get_properties(host);
140 }
141
142 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
143   return SIMIX_host_get_speed(host);
144 }
145 double SIMIX_host_get_speed(smx_host_t host){
146   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
147
148   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
149   return ws_model->extension.workstation.get_speed(host, 1.0);
150 }
151
152 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
153   return SIMIX_host_get_available_speed(host);
154 }
155 double SIMIX_host_get_available_speed(smx_host_t host){
156   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
157
158   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
159   return ws_model->extension.workstation.get_available_speed(host);
160 }
161
162 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
163   return SIMIX_host_get_state(host);
164 }
165 int SIMIX_host_get_state(smx_host_t host){
166   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
167
168   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
169   return ws_model->extension.workstation.get_state(host);
170 }
171
172 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
173   return SIMIX_host_self_get_data();
174 }
175 void* SIMIX_host_self_get_data(void)
176 {
177   smx_host_t self = SIMIX_host_self();
178   return SIMIX_host_get_data(self);
179 }
180
181 void SIMIX_host_self_set_data(void *data)
182 {
183   smx_host_t self = SIMIX_host_self();
184   SIMIX_host_set_data(self, data);
185 }
186
187 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
188   return SIMIX_host_get_data(host);
189 }
190 void* SIMIX_host_get_data(smx_host_t host){
191   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
192
193   return SIMIX_host_priv(host)->data;
194 }
195 void _SIMIX_host_free_process_arg(void *);
196 void _SIMIX_host_free_process_arg(void *data)
197 {
198   smx_process_arg_t arg = *(void**)data;
199   xbt_free(arg->name);
200   xbt_free(arg);
201 }
202 /**
203  * \brief Add a process to the list of the processes that the host will restart when it comes back
204  * This function add a process to the list of the processes that will be restarted when the host comes
205  * back. It is expected that this function is called when the host is down.
206  * The processes will only be restarted once, meaning that you will have to register the process
207  * again to restart the process again.
208  */
209 void SIMIX_host_add_auto_restart_process(smx_host_t host,
210                                          const char *name,
211                                          xbt_main_func_t code,
212                                          void *data,
213                                          const char *hostname,
214                                          double kill_time,
215                                          int argc, char **argv,
216                                          xbt_dict_t properties,
217                                          int auto_restart)
218 {
219   if (!SIMIX_host_priv(host)->auto_restart_processes) {
220     SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
221   }
222   smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
223   arg->name = xbt_strdup(name);
224   arg->code = code;
225   arg->data = data;
226   arg->hostname = hostname;
227   arg->kill_time = kill_time;
228   arg->argc = argc;
229
230   arg->argv = xbt_new(char*,argc + 1);
231
232   int i;
233   for (i = 0; i < argc; i++) {
234     arg->argv[i] = xbt_strdup(argv[i]);
235   }
236   arg->argv[argc] = NULL;
237
238   arg->properties = properties;
239   arg->auto_restart = auto_restart;
240
241   if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
242       && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
243     xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
244     XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
245   }
246   xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
247 }
248 /**
249  * \brief Restart the list of processes that have been registered to the host
250  */
251 void SIMIX_host_restart_processes(smx_host_t host)
252 {
253   unsigned int cpt;
254   smx_process_arg_t arg;
255   xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
256
257     smx_process_t process;
258
259     XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
260     if (simix_global->create_process_function) {
261       simix_global->create_process_function(&process,
262                                             arg->argv[0],
263                                             arg->code,
264                                             NULL,
265                                             arg->hostname,
266                                             arg->kill_time,
267                                             arg->argc,
268                                             arg->argv,
269                                             arg->properties,
270                                             arg->auto_restart);
271     }
272     else {
273       simcall_process_create(&process,
274                                             arg->argv[0],
275                                             arg->code,
276                                             NULL,
277                                             arg->hostname,
278                                             arg->kill_time,
279                                             arg->argc,
280                                             arg->argv,
281                                             arg->properties,
282                                             arg->auto_restart);
283
284     }
285   }
286   xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
287 }
288
289 void SIMIX_host_autorestart(smx_host_t host)
290 {
291   if(simix_global->autorestart)
292     simix_global->autorestart(host);
293   else
294     xbt_die("No function for simix_global->autorestart");
295 }
296
297 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
298   SIMIX_host_set_data(host, data);
299 }
300 void SIMIX_host_set_data(smx_host_t host, void *data){
301   xbt_assert((host != NULL), "Invalid parameters");
302   xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
303
304   SIMIX_host_priv(host)->data = data;
305 }
306
307 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
308     smx_host_t host, double computation_amount, double priority, double bound){
309   return SIMIX_host_execute(name, host, computation_amount, priority, bound);
310 }
311 smx_action_t SIMIX_host_execute(const char *name,
312     smx_host_t host, double computation_amount, double priority, double bound){
313
314   /* alloc structures and initialize */
315   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
316   action->type = SIMIX_ACTION_EXECUTE;
317   action->name = xbt_strdup(name);
318   action->state = SIMIX_RUNNING;
319   action->execution.host = host;
320
321 #ifdef HAVE_TRACING
322   action->category = NULL;
323 #endif
324
325   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
326   /* set surf's action */
327   if (!MC_is_active()) {
328     action->execution.surf_exec = ws_model->extension.workstation.execute(host, computation_amount);
329     ws_model->action_data_set(action->execution.surf_exec, action);
330     ws_model->set_priority(action->execution.surf_exec, priority);
331     ws_model->set_bound(action->execution.surf_exec, bound);
332   }
333
334   XBT_DEBUG("Create execute action %p", action);
335
336   return action;
337 }
338
339 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
340     int host_nb, smx_host_t *host_list,
341     double *computation_amount, double *communication_amount,
342     double amount, double rate){
343   return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
344                                      communication_amount, amount, rate);
345 }
346 smx_action_t SIMIX_host_parallel_execute(const char *name,
347     int host_nb, smx_host_t *host_list,
348     double *computation_amount, double *communication_amount,
349     double amount, double rate){
350
351   void **workstation_list = NULL;
352   int i;
353
354   /* alloc structures and initialize */
355   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
356   action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
357   action->name = xbt_strdup(name);
358   action->state = SIMIX_RUNNING;
359   action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
360
361 #ifdef HAVE_TRACING
362   action->category = NULL;
363 #endif
364
365   /* set surf's action */
366   workstation_list = xbt_new0(void *, host_nb);
367   for (i = 0; i < host_nb; i++)
368     workstation_list[i] = host_list[i];
369
370
371   /* FIXME: what happens if host_list contains VMs and PMs. If
372    * execute_parallel_task() does not change the state of the model, we can mix
373    * them. */
374   surf_model_t ws_model = surf_resource_model(host_list[0], SURF_WKS_LEVEL);
375   for (i = 1; i < host_nb; i++) {
376     surf_model_t ws_model_tmp = surf_resource_model(host_list[i], SURF_WKS_LEVEL);
377     if (ws_model_tmp != ws_model) {
378       XBT_CRITICAL("mixing VMs and PMs is not supported");
379       DIE_IMPOSSIBLE;
380     }
381   }
382
383   /* set surf's action */
384   if (!MC_is_active()) {
385     action->execution.surf_exec =
386       ws_model->extension.workstation.
387       execute_parallel_task(host_nb, workstation_list, computation_amount,
388                       communication_amount, rate);
389
390     ws_model->action_data_set(action->execution.surf_exec, action);
391   }
392   XBT_DEBUG("Create parallel execute action %p", action);
393
394   return action;
395 }
396
397 static surf_model_t get_ws_model_from_action(smx_action_t action)
398 {
399   xbt_assert(action->type == SIMIX_ACTION_EXECUTE);
400   smx_host_t host = action->execution.host;
401   surf_model_t model = surf_resource_model(host, SURF_WKS_LEVEL);
402
403   xbt_assert((model == surf_workstation_model) || (model == surf_vm_workstation_model));
404
405   return model;
406 }
407
408 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
409   SIMIX_host_execution_destroy(action);
410 }
411 void SIMIX_host_execution_destroy(smx_action_t action){
412   XBT_DEBUG("Destroy action %p", action);
413
414   surf_model_t ws_model = get_ws_model_from_action(action);
415
416   if (action->execution.surf_exec) {
417     ws_model->action_unref(action->execution.surf_exec);
418     action->execution.surf_exec = NULL;
419   }
420   xbt_free(action->name);
421   xbt_mallocator_release(simix_global->action_mallocator, action);
422 }
423
424 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
425   SIMIX_host_execution_cancel(action);
426 }
427 void SIMIX_host_execution_cancel(smx_action_t action){
428   XBT_DEBUG("Cancel action %p", action);
429
430   surf_model_t ws_model = get_ws_model_from_action(action);
431
432   if (action->execution.surf_exec)
433     ws_model->action_cancel(action->execution.surf_exec);
434 }
435
436 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
437   return SIMIX_host_execution_get_remains(action);
438 }
439 double SIMIX_host_execution_get_remains(smx_action_t action){
440   double result = 0.0;
441   surf_model_t ws_model = get_ws_model_from_action(action);
442
443   if (action->state == SIMIX_RUNNING)
444     result = ws_model->get_remains(action->execution.surf_exec);
445
446   return result;
447 }
448
449 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
450   return SIMIX_host_execution_get_state(action);
451 }
452 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
453   return action->state;
454 }
455
456 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
457                                         double priority){
458   SIMIX_host_execution_set_priority(action, priority);
459 }
460 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
461   surf_model_t ws_model = get_ws_model_from_action(action);
462
463   if(action->execution.surf_exec)
464     ws_model->set_priority(action->execution.surf_exec, priority);
465 }
466
467 void SIMIX_pre_host_execution_set_bound(smx_simcall_t simcall, smx_action_t action,
468                                         double bound){
469   SIMIX_host_execution_set_bound(action, bound);
470 }
471 void SIMIX_host_execution_set_bound(smx_action_t action, double bound){
472   surf_model_t ws_model = get_ws_model_from_action(action);
473
474   if(action->execution.surf_exec)
475     ws_model->set_bound(action->execution.surf_exec, bound);
476 }
477
478 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
479
480   XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
481
482   /* Associate this simcall to the action */
483   xbt_fifo_push(action->simcalls, simcall);
484   simcall->issuer->waiting_action = action;
485
486   /* set surf's action */
487   if (MC_is_active()) {
488     action->state = SIMIX_DONE;
489     SIMIX_execution_finish(action);
490     return;
491   }
492
493   /* If the action is already finished then perform the error handling */
494   if (action->state != SIMIX_RUNNING)
495     SIMIX_execution_finish(action);
496 }
497
498 void SIMIX_host_execution_suspend(smx_action_t action)
499 {
500   surf_model_t ws_model = get_ws_model_from_action(action);
501
502   if(action->execution.surf_exec)
503     ws_model->suspend(action->execution.surf_exec);
504 }
505
506 void SIMIX_host_execution_resume(smx_action_t action)
507 {
508   surf_model_t ws_model = get_ws_model_from_action(action);
509
510   if(action->execution.surf_exec)
511     ws_model->resume(action->execution.surf_exec);
512 }
513
514 void SIMIX_execution_finish(smx_action_t action)
515 {
516   xbt_fifo_item_t item;
517   smx_simcall_t simcall;
518   surf_model_t ws_model = get_ws_model_from_action(action);
519
520   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
521
522     switch (action->state) {
523
524       case SIMIX_DONE:
525         /* do nothing, action done */
526   XBT_DEBUG("SIMIX_execution_finished: execution successful");
527         break;
528
529       case SIMIX_FAILED:
530         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
531         simcall->issuer->context->iwannadie = 1;
532         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
533         break;
534
535       case SIMIX_CANCELED:
536         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
537         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
538         break;
539
540       default:
541         xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
542             (int)action->state);
543     }
544     /* check if the host is down */
545     if (ws_model->extension.workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
546       simcall->issuer->context->iwannadie = 1;
547     }
548
549     simcall->issuer->waiting_action =    NULL;
550     simcall_host_execution_wait__set__result(simcall, action->state);
551     SIMIX_simcall_answer(simcall);
552   }
553
554   /* We no longer need it */
555   SIMIX_host_execution_destroy(action);
556 }
557
558
559 void SIMIX_post_host_execute(smx_action_t action)
560 {
561   surf_model_t ws_model = get_ws_model_from_action(action);
562
563   if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
564                                                * for parallel tasks too */
565       ws_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
566     /* If the host running the action failed, notice it so that the asking
567      * process can be killed if it runs on that host itself */
568     action->state = SIMIX_FAILED;
569   } else if (ws_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
570     /* If the host running the action didn't fail, then the action was
571      * canceled */
572     action->state = SIMIX_CANCELED;
573   } else {
574     action->state = SIMIX_DONE;
575   }
576
577   if (action->execution.surf_exec) {
578     ws_model->action_unref(action->execution.surf_exec);
579     action->execution.surf_exec = NULL;
580   }
581
582   /* If there are simcalls associated with the action, then answer them */
583   if (xbt_fifo_size(action->simcalls)) {
584     SIMIX_execution_finish(action);
585   }
586 }
587
588
589 #ifdef HAVE_TRACING
590 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
591                             const char *category){
592   SIMIX_set_category(action, category);
593 }
594 void SIMIX_set_category(smx_action_t action, const char *category)
595 {
596   surf_model_t ws_model = get_ws_model_from_action(action);
597
598   if (action->state != SIMIX_RUNNING) return;
599   if (action->type == SIMIX_ACTION_EXECUTE){
600     ws_model->set_category(action->execution.surf_exec, category);
601   }else if (action->type == SIMIX_ACTION_COMMUNICATE){
602     ws_model->set_category(action->comm.surf_comm, category);
603   }
604 }
605 #endif
606
607
608 /**
609  * \brief Function to get the parameters of the given the SIMIX host.
610  *
611  * \param host the host to get_phys_host (a smx_host_t)
612  * \param param the parameter object space to be overwritten (a ws_params_t)
613  */
614 void SIMIX_host_get_params(smx_host_t ind_vm, ws_params_t params)
615 {
616   /* jump to ws_get_params(). */
617   surf_workstation_model->extension.workstation.get_params(ind_vm, params);
618 }
619
620 void SIMIX_pre_host_get_params(smx_simcall_t simcall, smx_host_t ind_vm, ws_params_t params)
621 {
622   SIMIX_host_get_params(ind_vm, params);
623   SIMIX_simcall_answer(simcall);
624 }
625
626 void SIMIX_host_set_params(smx_host_t ind_vm, ws_params_t params)
627 {
628   /* jump to ws_set_params(). */
629   surf_workstation_model->extension.workstation.set_params(ind_vm, params);
630 }
631
632 void SIMIX_pre_host_set_params(smx_simcall_t simcall, smx_host_t ind_vm, ws_params_t params)
633 {
634   SIMIX_host_set_params(ind_vm, params);
635   SIMIX_simcall_answer(simcall);
636 }