Logo AND Algorithmique Numérique Distribuée

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