Logo AND Algorithmique Numérique Distribuée

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