Logo AND Algorithmique Numérique Distribuée

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