Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'origin/libdw2'
[simgrid.git] / src / simix / smx_host.c
1 /* Copyright (c) 2007-2013. 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_or_null(host_lib, name, SIMIX_HOST_LEVEL);
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   return surf_workstation_model->extension.workstation.get_properties(host);
139 }
140
141 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
142   return SIMIX_host_get_speed(host);
143 }
144 double SIMIX_host_get_speed(smx_host_t host){
145   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
146
147   return surf_workstation_model->extension.workstation.
148       get_speed(host, 1.0);
149 }
150
151 int SIMIX_pre_host_get_core(smx_simcall_t simcall, smx_host_t host){
152   return SIMIX_host_get_core(host);
153 }
154 int SIMIX_host_get_core(smx_host_t host){
155   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
156
157   return surf_workstation_model->extension.workstation.
158       get_core(host);
159 }
160
161 xbt_swag_t SIMIX_pre_host_get_process_list(smx_simcall_t simcall, smx_host_t host){
162   return SIMIX_host_get_process_list(host);
163 }
164
165 xbt_swag_t SIMIX_host_get_process_list(smx_host_t host){
166   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
167   smx_host_priv_t host_priv = SIMIX_host_priv(host);
168
169   return host_priv->process_list;
170 }
171
172
173 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
174   return SIMIX_host_get_available_speed(host);
175 }
176 double SIMIX_host_get_available_speed(smx_host_t host){
177   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
178
179   return surf_workstation_model->extension.workstation.
180       get_available_speed(host);
181 }
182
183 double SIMIX_pre_host_get_current_power_peak(smx_simcall_t simcall, smx_host_t host){
184   return SIMIX_host_get_current_power_peak(host);
185 }
186 double SIMIX_host_get_current_power_peak(smx_host_t host) {
187           xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
188           return surf_workstation_model->extension.workstation.
189                       get_current_power_peak(host);
190 }
191
192 double SIMIX_pre_host_get_power_peak_at(smx_simcall_t simcall, smx_host_t host, int pstate_index){
193   return SIMIX_host_get_power_peak_at(host, pstate_index);
194 }
195 double SIMIX_host_get_power_peak_at(smx_host_t host, int pstate_index) {
196           xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
197
198           return surf_workstation_model->extension.workstation.
199               get_power_peak_at(host, pstate_index);
200 }
201
202 int SIMIX_pre_host_get_nb_pstates(smx_simcall_t simcall, smx_host_t host){
203   return SIMIX_host_get_nb_pstates(host);
204 }
205 int SIMIX_host_get_nb_pstates(smx_host_t host) {
206           xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
207
208           return surf_workstation_model->extension.workstation.
209               get_nb_pstates(host);
210 }
211
212
213 void SIMIX_pre_host_set_power_peak_at(smx_simcall_t simcall, smx_host_t host, int pstate_index){
214   SIMIX_host_set_power_peak_at(host, pstate_index);
215 }
216 void SIMIX_host_set_power_peak_at(smx_host_t host, int pstate_index) {
217           xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
218
219           surf_workstation_model->extension.workstation.
220               set_power_peak_at(host, pstate_index);
221 }
222
223 double SIMIX_pre_host_get_consumed_energy(smx_simcall_t simcall, smx_host_t host){
224   return SIMIX_host_get_consumed_energy(host);
225 }
226 double SIMIX_host_get_consumed_energy(smx_host_t host) {
227           xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
228           return surf_workstation_model->extension.workstation.
229                       get_consumed_energy(host);
230 }
231
232 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
233   return SIMIX_host_get_state(host);
234 }
235 int SIMIX_host_get_state(smx_host_t host){
236   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
237
238   return surf_workstation_model->extension.workstation.
239       get_state(host);
240 }
241
242 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
243   return SIMIX_host_self_get_data();
244 }
245 void* SIMIX_host_self_get_data(void)
246 {
247   smx_host_t self = SIMIX_host_self();
248   return SIMIX_host_get_data(self);
249 }
250
251 void SIMIX_host_self_set_data(void *data)
252 {
253   smx_host_t self = SIMIX_host_self();
254   SIMIX_host_set_data(self, data);
255 }
256
257 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
258   return SIMIX_host_get_data(host);
259 }
260 void* SIMIX_host_get_data(smx_host_t host){
261   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
262
263   return SIMIX_host_priv(host)->data;
264 }
265
266 static void _SIMIX_host_free_process_arg(void *data)
267 {
268   smx_process_arg_t arg = *(void**)data;
269   int i;
270   for (i = 0; i < arg->argc; i++)
271     xbt_free(arg->argv[i]);
272   xbt_free(arg->argv);
273   xbt_free(arg->name);
274   xbt_free(arg);
275 }
276 /**
277  * \brief Add a process to the list of the processes that the host will restart when it comes back
278  * This function add a process to the list of the processes that will be restarted when the host comes
279  * back. It is expected that this function is called when the host is down.
280  * The processes will only be restarted once, meaning that you will have to register the process
281  * again to restart the process again.
282  */
283 void SIMIX_host_add_auto_restart_process(smx_host_t host,
284                                          const char *name,
285                                          xbt_main_func_t code,
286                                          void *data,
287                                          const char *hostname,
288                                          double kill_time,
289                                          int argc, char **argv,
290                                          xbt_dict_t properties,
291                                          int auto_restart)
292 {
293   if (!SIMIX_host_priv(host)->auto_restart_processes) {
294     SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
295   }
296   smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
297   arg->name = xbt_strdup(name);
298   arg->code = code;
299   arg->data = data;
300   arg->hostname = hostname;
301   arg->kill_time = kill_time;
302   arg->argc = argc;
303
304   arg->argv = xbt_new(char*,argc + 1);
305
306   int i;
307   for (i = 0; i < argc; i++) {
308     arg->argv[i] = xbt_strdup(argv[i]);
309   }
310   arg->argv[argc] = NULL;
311
312   arg->properties = properties;
313   arg->auto_restart = auto_restart;
314
315   if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
316       && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
317     xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
318     XBT_DEBUG("Have pushed host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
319   }
320   xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
321 }
322 /**
323  * \brief Restart the list of processes that have been registered to the host
324  */
325 void SIMIX_host_restart_processes(smx_host_t host)
326 {
327   unsigned int cpt;
328   smx_process_arg_t arg;
329   xbt_dynar_t process_list = SIMIX_host_priv(host)->auto_restart_processes;
330   if (!process_list)
331     return;
332
333   xbt_dynar_foreach (process_list, cpt, arg) {
334
335     smx_process_t process;
336
337     XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
338     if (simix_global->create_process_function) {
339       simix_global->create_process_function(&process,
340                                             arg->argv[0],
341                                             arg->code,
342                                             NULL,
343                                             arg->hostname,
344                                             arg->kill_time,
345                                             arg->argc,
346                                             arg->argv,
347                                             arg->properties,
348                                             arg->auto_restart);
349     } else {
350       simcall_process_create(&process,
351                              arg->argv[0],
352                              arg->code,
353                              NULL,
354                              arg->hostname,
355                              arg->kill_time,
356                              arg->argc,
357                              arg->argv,
358                              arg->properties,
359                              arg->auto_restart);
360
361     }
362     /* arg->argv is used by the process created above.  Hide it to
363      * _SIMIX_host_free_process_arg() which is called by xbt_dynar_reset()
364      * below. */
365     arg->argc = 0;
366     arg->argv = NULL;
367   }
368   xbt_dynar_reset(process_list);
369 }
370
371 void SIMIX_host_autorestart(smx_host_t host)
372 {
373   if(simix_global->autorestart)
374     simix_global->autorestart(host);
375   else
376     xbt_die("No function for simix_global->autorestart");
377 }
378
379 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
380   SIMIX_host_set_data(host, data);
381 }
382 void SIMIX_host_set_data(smx_host_t host, void *data){
383   xbt_assert((host != NULL), "Invalid parameters");
384   xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
385
386   SIMIX_host_priv(host)->data = data;
387 }
388
389 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
390     smx_host_t host, double computation_amount, double priority){
391   return SIMIX_host_execute(name, host, computation_amount, priority);
392 }
393 smx_action_t SIMIX_host_execute(const char *name,
394     smx_host_t host, double computation_amount, double priority){
395
396   /* alloc structures and initialize */
397   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
398   action->type = SIMIX_ACTION_EXECUTE;
399   action->name = xbt_strdup(name);
400   action->state = SIMIX_RUNNING;
401   action->execution.host = host;
402
403 #ifdef HAVE_TRACING
404   action->category = NULL;
405 #endif
406
407   /* set surf's action */
408   if (!MC_is_active()) {
409     action->execution.surf_exec =
410       surf_workstation_model->extension.workstation.execute(host,
411     computation_amount);
412     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
413     surf_workstation_model->set_priority(action->execution.surf_exec, priority);
414   }
415
416   XBT_DEBUG("Create execute action %p", action);
417
418   return action;
419 }
420
421 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
422     int host_nb, smx_host_t *host_list,
423     double *computation_amount, double *communication_amount,
424     double amount, double rate){
425   return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
426                                      communication_amount, amount, rate);
427 }
428 smx_action_t SIMIX_host_parallel_execute(const char *name,
429     int host_nb, smx_host_t *host_list,
430     double *computation_amount, double *communication_amount,
431     double amount, double rate){
432
433   void **workstation_list = NULL;
434   int i;
435
436   /* alloc structures and initialize */
437   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
438   action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
439   action->name = xbt_strdup(name);
440   action->state = SIMIX_RUNNING;
441   action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
442
443 #ifdef HAVE_TRACING
444   action->category = NULL;
445 #endif
446
447   /* set surf's action */
448   workstation_list = xbt_new0(void *, host_nb);
449   for (i = 0; i < host_nb; i++)
450     workstation_list[i] = host_list[i];
451
452   /* set surf's action */
453   if (!MC_is_active()) {
454     action->execution.surf_exec =
455       surf_workstation_model->extension.workstation.
456       execute_parallel_task(host_nb, workstation_list, computation_amount,
457                       communication_amount, rate);
458
459     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
460   }
461   XBT_DEBUG("Create parallel execute action %p", action);
462
463   return action;
464 }
465
466 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
467   SIMIX_host_execution_destroy(action);
468 }
469 void SIMIX_host_execution_destroy(smx_action_t action){
470   XBT_DEBUG("Destroy action %p", action);
471
472   if (action->execution.surf_exec) {
473     surf_workstation_model->action_unref(action->execution.surf_exec);
474     action->execution.surf_exec = NULL;
475   }
476   xbt_free(action->name);
477   xbt_mallocator_release(simix_global->action_mallocator, action);
478 }
479
480 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
481   SIMIX_host_execution_cancel(action);
482 }
483 void SIMIX_host_execution_cancel(smx_action_t action){
484   XBT_DEBUG("Cancel action %p", action);
485
486   if (action->execution.surf_exec)
487     surf_workstation_model->action_cancel(action->execution.surf_exec);
488 }
489
490 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
491   return SIMIX_host_execution_get_remains(action);
492 }
493 double SIMIX_host_execution_get_remains(smx_action_t action){
494   double result = 0.0;
495
496   if (action->state == SIMIX_RUNNING)
497     result = surf_workstation_model->get_remains(action->execution.surf_exec);
498
499   return result;
500 }
501
502 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
503   return SIMIX_host_execution_get_state(action);
504 }
505 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
506   return action->state;
507 }
508
509 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
510                                         double priority){
511   return SIMIX_host_execution_set_priority(action, priority);
512 }
513 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
514   if(action->execution.surf_exec)
515     surf_workstation_model->set_priority(action->execution.surf_exec, priority);
516 }
517
518 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
519
520   XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
521
522   /* Associate this simcall to the action */
523   xbt_fifo_push(action->simcalls, simcall);
524   simcall->issuer->waiting_action = action;
525
526   /* set surf's action */
527   if (MC_is_active()) {
528     action->state = SIMIX_DONE;
529     SIMIX_execution_finish(action);
530     return;
531   }
532
533   /* If the action is already finished then perform the error handling */
534   if (action->state != SIMIX_RUNNING)
535     SIMIX_execution_finish(action);
536 }
537
538 void SIMIX_host_execution_suspend(smx_action_t action)
539 {
540   if(action->execution.surf_exec)
541     surf_workstation_model->suspend(action->execution.surf_exec);
542 }
543
544 void SIMIX_host_execution_resume(smx_action_t action)
545 {
546   if(action->execution.surf_exec)
547     surf_workstation_model->resume(action->execution.surf_exec);
548 }
549
550 void SIMIX_execution_finish(smx_action_t action)
551 {
552   xbt_fifo_item_t item;
553   smx_simcall_t simcall;
554
555   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
556
557     switch (action->state) {
558
559       case SIMIX_DONE:
560         /* do nothing, action done */
561   XBT_DEBUG("SIMIX_execution_finished: execution successful");
562         break;
563
564       case SIMIX_FAILED:
565         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
566         simcall->issuer->context->iwannadie = 1;
567         SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
568         break;
569
570       case SIMIX_CANCELED:
571         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
572         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
573         break;
574
575       default:
576         xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
577             (int)action->state);
578     }
579     /* check if the host is down */
580     if (surf_workstation_model->extension.
581         workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
582       simcall->issuer->context->iwannadie = 1;
583     }
584
585     simcall->issuer->waiting_action =    NULL;
586     simcall_host_execution_wait__set__result(simcall, action->state);
587     SIMIX_simcall_answer(simcall);
588   }
589
590   /* We no longer need it */
591   SIMIX_host_execution_destroy(action);
592 }
593
594 void SIMIX_post_host_execute(smx_action_t action)
595 {
596   if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
597                                                * for parallel tasks too */
598       surf_workstation_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
599     /* If the host running the action failed, notice it so that the asking
600      * process can be killed if it runs on that host itself */
601     action->state = SIMIX_FAILED;
602   } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
603     /* If the host running the action didn't fail, then the action was
604      * canceled */
605     action->state = SIMIX_CANCELED;
606   } else {
607     action->state = SIMIX_DONE;
608   }
609
610   if (action->execution.surf_exec) {
611     surf_workstation_model->action_unref(action->execution.surf_exec);
612     action->execution.surf_exec = NULL;
613   }
614
615   /* If there are simcalls associated with the action, then answer them */
616   if (xbt_fifo_size(action->simcalls)) {
617     SIMIX_execution_finish(action);
618   }
619 }
620
621
622 #ifdef HAVE_TRACING
623 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
624                             const char *category){
625   SIMIX_set_category(action, category);
626 }
627 void SIMIX_set_category(smx_action_t action, const char *category)
628 {
629   if (action->state != SIMIX_RUNNING) return;
630   if (action->type == SIMIX_ACTION_EXECUTE){
631     surf_workstation_model->set_category(action->execution.surf_exec, category);
632   }else if (action->type == SIMIX_ACTION_COMMUNICATE){
633     surf_workstation_model->set_category(action->comm.surf_comm, category);
634   }
635 }
636 #endif
637
638 xbt_dict_t SIMIX_pre_host_get_storage_list(smx_simcall_t simcall, smx_host_t host){
639   return SIMIX_host_get_storage_list(host);
640 }
641 xbt_dict_t SIMIX_host_get_storage_list(smx_host_t host){
642   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
643
644   return surf_workstation_model->extension.workstation.get_storage_list(host);
645 }