Logo AND Algorithmique Numérique Distribuée

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