Logo AND Algorithmique Numérique Distribuée

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