Logo AND Algorithmique Numérique Distribuée

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