Logo AND Algorithmique Numérique Distribuée

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