Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove the use of surf_workstation_model at smx_host.c
[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 =
55         bprintf("Shutting down host, but it's not empty:");
56     char *tmp;
57     smx_process_t process = NULL;
58
59     xbt_swag_foreach(process, host->process_list) {
60       tmp = bprintf("%s\n\t%s", msg, process->name);
61       free(msg);
62       msg = tmp;
63     }
64     SIMIX_display_process_status();
65     THROWF(arg_error, 0, "%s", msg);
66   }
67   xbt_dynar_free(&host->auto_restart_processes);
68   xbt_swag_free(host->process_list);
69
70   /* Clean host structure */
71   free(host); 
72   return;
73 }
74
75 ///**
76 // * \brief Returns a dict of all hosts.
77 // *
78 // * \return List of all hosts (as a #xbt_dict_t)
79 // */
80 //xbt_dict_t SIMIX_host_get_dict(void)
81 //{
82 //  xbt_dict_t host_dict = xbt_dict_new_homogeneous(NULL);
83 //  xbt_lib_cursor_t cursor = NULL;
84 //  char *name = NULL;
85 //  void **host = NULL;
86 //
87 //  xbt_lib_foreach(host_lib, cursor, name, host){
88 //    if(host[SIMIX_HOST_LEVEL])
89 //            xbt_dict_set(host_dict,name,host[SIMIX_HOST_LEVEL], NULL);
90 //  }
91 //  return host_dict;
92 //}
93 smx_host_t SIMIX_pre_host_get_by_name(smx_simcall_t simcall, const char *name){
94    return SIMIX_host_get_by_name(name);
95 }
96 smx_host_t SIMIX_host_get_by_name(const char *name){
97   xbt_assert(((simix_global != NULL)
98                && (host_lib != NULL)),
99               "Environment not set yet");
100
101   return xbt_lib_get_elm_or_null(host_lib, name);
102 }
103
104 smx_host_t SIMIX_host_self(void)
105 {
106   smx_process_t process = SIMIX_process_self();
107   return (process == NULL) ? NULL : SIMIX_process_get_host(process);
108 }
109
110 const char* SIMIX_pre_host_self_get_name(smx_simcall_t simcall){
111    return SIMIX_host_self_get_name();
112 }
113 /* needs to be public and without simcall because it is called
114    by exceptions and logging events */
115 const char* SIMIX_host_self_get_name(void)
116 {
117   smx_host_t host = SIMIX_host_self();
118   if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
119     return "";
120
121   return SIMIX_host_get_name(host);
122 }
123
124 const char* SIMIX_pre_host_get_name(smx_simcall_t simcall, smx_host_t host){
125    return SIMIX_host_get_name(host);
126 }
127 const char* SIMIX_host_get_name(smx_host_t host){
128   xbt_assert((host != NULL), "Invalid parameters");
129
130   return sg_host_name(host);
131 }
132
133 xbt_dict_t SIMIX_pre_host_get_properties(smx_simcall_t simcall, smx_host_t host){
134   return SIMIX_host_get_properties(host);
135 }
136 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
137   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
138
139   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
140   return ws_model->extension.workstation.get_properties(host);
141 }
142
143 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
144   return SIMIX_host_get_speed(host);
145 }
146 double SIMIX_host_get_speed(smx_host_t host){
147   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
148
149   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
150   return ws_model->extension.workstation.get_speed(host, 1.0);
151 }
152
153 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
154   return SIMIX_host_get_available_speed(host);
155 }
156 double SIMIX_host_get_available_speed(smx_host_t host){
157   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
158
159   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
160   return ws_model->extension.workstation.get_available_speed(host);
161 }
162
163 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
164   return SIMIX_host_get_state(host);
165 }
166 int SIMIX_host_get_state(smx_host_t host){
167   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
168
169   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
170   return ws_model->extension.workstation.get_state(host);
171 }
172
173 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
174   return SIMIX_host_self_get_data();
175 }
176 void* SIMIX_host_self_get_data(void)
177 {
178   smx_host_t self = SIMIX_host_self();
179   return SIMIX_host_get_data(self);
180 }
181
182 void SIMIX_host_self_set_data(void *data)
183 {
184   smx_host_t self = SIMIX_host_self();
185   SIMIX_host_set_data(self, data);
186 }
187
188 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
189   return SIMIX_host_get_data(host);
190 }
191 void* SIMIX_host_get_data(smx_host_t host){
192   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
193
194   return SIMIX_host_priv(host)->data;
195 }
196 void _SIMIX_host_free_process_arg(void *);
197 void _SIMIX_host_free_process_arg(void *data)
198 {
199   smx_process_arg_t arg = *(void**)data;
200   xbt_free(arg->name);
201   xbt_free(arg);
202 }
203 /**
204  * \brief Add a process to the list of the processes that the host will restart when it comes back
205  * This function add a process to the list of the processes that will be restarted when the host comes
206  * back. It is expected that this function is called when the host is down.
207  * The processes will only be restarted once, meaning that you will have to register the process
208  * again to restart the process again.
209  */
210 void SIMIX_host_add_auto_restart_process(smx_host_t host,
211                                          const char *name,
212                                          xbt_main_func_t code,
213                                          void *data,
214                                          const char *hostname,
215                                          double kill_time,
216                                          int argc, char **argv,
217                                          xbt_dict_t properties,
218                                          int auto_restart)
219 {
220   if (!SIMIX_host_priv(host)->auto_restart_processes) {
221     SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
222   }
223   smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
224   arg->name = xbt_strdup(name);
225   arg->code = code;
226   arg->data = data;
227   arg->hostname = hostname;
228   arg->kill_time = kill_time;
229   arg->argc = argc;
230
231   arg->argv = xbt_new(char*,argc + 1);
232
233   int i;
234   for (i = 0; i < argc; i++) {
235     arg->argv[i] = xbt_strdup(argv[i]);
236   }
237   arg->argv[argc] = NULL;
238
239   arg->properties = properties;
240   arg->auto_restart = auto_restart;
241
242   if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
243       && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
244     xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
245     XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
246   }
247   xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
248 }
249 /**
250  * \brief Restart the list of processes that have been registered to the host
251  */
252 void SIMIX_host_restart_processes(smx_host_t host)
253 {
254   unsigned int cpt;
255   smx_process_arg_t arg;
256   xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
257
258     smx_process_t process;
259
260     XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
261     if (simix_global->create_process_function) {
262       simix_global->create_process_function(&process,
263                                             arg->argv[0],
264                                             arg->code,
265                                             NULL,
266                                             arg->hostname,
267                                             arg->kill_time,
268                                             arg->argc,
269                                             arg->argv,
270                                             arg->properties,
271                                             arg->auto_restart);
272     }
273     else {
274       simcall_process_create(&process,
275                                             arg->argv[0],
276                                             arg->code,
277                                             NULL,
278                                             arg->hostname,
279                                             arg->kill_time,
280                                             arg->argc,
281                                             arg->argv,
282                                             arg->properties,
283                                             arg->auto_restart);
284
285     }
286   }
287   xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
288 }
289
290 void SIMIX_host_autorestart(smx_host_t host)
291 {
292   if(simix_global->autorestart)
293     simix_global->autorestart(host);
294   else
295     xbt_die("No function for simix_global->autorestart");
296 }
297
298 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
299   SIMIX_host_set_data(host, data);
300 }
301 void SIMIX_host_set_data(smx_host_t host, void *data){
302   xbt_assert((host != NULL), "Invalid parameters");
303   xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
304
305   SIMIX_host_priv(host)->data = data;
306 }
307
308 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
309     smx_host_t host, double computation_amount, double priority){
310   return SIMIX_host_execute(name, host, computation_amount, priority);
311 }
312 smx_action_t SIMIX_host_execute(const char *name,
313     smx_host_t host, double computation_amount, double priority){
314
315   /* alloc structures and initialize */
316   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
317   action->type = SIMIX_ACTION_EXECUTE;
318   action->name = xbt_strdup(name);
319   action->state = SIMIX_RUNNING;
320   action->execution.host = host;
321
322 #ifdef HAVE_TRACING
323   action->category = NULL;
324 #endif
325
326   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
327   /* set surf's action */
328   if (!MC_is_active()) {
329     action->execution.surf_exec = ws_model->extension.workstation.execute(host, computation_amount);
330     ws_model->action_data_set(action->execution.surf_exec, action);
331     ws_model->set_priority(action->execution.surf_exec, priority);
332   }
333
334   XBT_DEBUG("Create execute action %p", action);
335
336   return action;
337 }
338
339 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
340     int host_nb, smx_host_t *host_list,
341     double *computation_amount, double *communication_amount,
342     double amount, double rate){
343   return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
344                                      communication_amount, amount, rate);
345 }
346 smx_action_t SIMIX_host_parallel_execute(const char *name,
347     int host_nb, smx_host_t *host_list,
348     double *computation_amount, double *communication_amount,
349     double amount, double rate){
350
351   void **workstation_list = NULL;
352   int i;
353
354   /* alloc structures and initialize */
355   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
356   action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
357   action->name = xbt_strdup(name);
358   action->state = SIMIX_RUNNING;
359   action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
360
361 #ifdef HAVE_TRACING
362   action->category = NULL;
363 #endif
364
365   /* set surf's action */
366   workstation_list = xbt_new0(void *, host_nb);
367   for (i = 0; i < host_nb; i++)
368     workstation_list[i] = host_list[i];
369
370
371   /* FIXME: what happens if host_list contains VMs and PMs. If
372    * execute_parallel_task() does not change the state of the model, we can mix
373    * them. */
374   surf_model_t ws_model = surf_resource_model(host_list[0], SURF_WKS_LEVEL);
375   for (i = 1; i < host_nb; i++) {
376     surf_model_t ws_model_tmp = surf_resource_model(host_list[i], SURF_WKS_LEVEL);
377     if (ws_model_tmp != ws_model) {
378       XBT_CRITICAL("mixing VMs and PMs is not supported");
379       DIE_IMPOSSIBLE;
380     }
381   }
382
383   /* set surf's action */
384   if (!MC_is_active()) {
385     action->execution.surf_exec =
386       ws_model->extension.workstation.
387       execute_parallel_task(host_nb, workstation_list, computation_amount,
388                       communication_amount, rate);
389
390     ws_model->action_data_set(action->execution.surf_exec, action);
391   }
392   XBT_DEBUG("Create parallel execute action %p", action);
393
394   return action;
395 }
396
397 static surf_model_t get_ws_model_from_action(smx_action_t action)
398 {
399   xbt_assert(action->type == SIMIX_ACTION_EXECUTE);
400   smx_host_t host = action->execution.host;
401   surf_model_t model = surf_resource_model(host, SURF_WKS_LEVEL);
402
403   xbt_assert((model == surf_workstation_model) || (model == surf_vm_workstation_model));
404
405   return model;
406 }
407
408 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
409   SIMIX_host_execution_destroy(action);
410 }
411 void SIMIX_host_execution_destroy(smx_action_t action){
412   XBT_DEBUG("Destroy action %p", action);
413
414   surf_model_t ws_model = get_ws_model_from_action(action);
415
416   if (action->execution.surf_exec) {
417     ws_model->action_unref(action->execution.surf_exec);
418     action->execution.surf_exec = NULL;
419   }
420   xbt_free(action->name);
421   xbt_mallocator_release(simix_global->action_mallocator, action);
422 }
423
424 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
425   SIMIX_host_execution_cancel(action);
426 }
427 void SIMIX_host_execution_cancel(smx_action_t action){
428   XBT_DEBUG("Cancel action %p", action);
429
430   surf_model_t ws_model = get_ws_model_from_action(action);
431
432   if (action->execution.surf_exec)
433     ws_model->action_cancel(action->execution.surf_exec);
434 }
435
436 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
437   return SIMIX_host_execution_get_remains(action);
438 }
439 double SIMIX_host_execution_get_remains(smx_action_t action){
440   double result = 0.0;
441   surf_model_t ws_model = get_ws_model_from_action(action);
442
443   if (action->state == SIMIX_RUNNING)
444     result = ws_model->get_remains(action->execution.surf_exec);
445
446   return result;
447 }
448
449 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
450   return SIMIX_host_execution_get_state(action);
451 }
452 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
453   return action->state;
454 }
455
456 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
457                                         double priority){
458   return SIMIX_host_execution_set_priority(action, priority);
459 }
460 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
461   surf_model_t ws_model = get_ws_model_from_action(action);
462
463   if(action->execution.surf_exec)
464     ws_model->set_priority(action->execution.surf_exec, priority);
465 }
466
467 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
468
469   XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
470
471   /* Associate this simcall to the action */
472   xbt_fifo_push(action->simcalls, simcall);
473   simcall->issuer->waiting_action = action;
474
475   /* set surf's action */
476   if (MC_is_active()) {
477     action->state = SIMIX_DONE;
478     SIMIX_execution_finish(action);
479     return;
480   }
481
482   /* If the action is already finished then perform the error handling */
483   if (action->state != SIMIX_RUNNING)
484     SIMIX_execution_finish(action);
485 }
486
487 void SIMIX_host_execution_suspend(smx_action_t action)
488 {
489   surf_model_t ws_model = get_ws_model_from_action(action);
490
491   if(action->execution.surf_exec)
492     ws_model->suspend(action->execution.surf_exec);
493 }
494
495 void SIMIX_host_execution_resume(smx_action_t action)
496 {
497   surf_model_t ws_model = get_ws_model_from_action(action);
498
499   if(action->execution.surf_exec)
500     ws_model->resume(action->execution.surf_exec);
501 }
502
503 void SIMIX_execution_finish(smx_action_t action)
504 {
505   xbt_fifo_item_t item;
506   smx_simcall_t simcall;
507   surf_model_t ws_model = get_ws_model_from_action(action);
508
509   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
510
511     switch (action->state) {
512
513       case SIMIX_DONE:
514         /* do nothing, action done */
515   XBT_DEBUG("SIMIX_execution_finished: execution successful");
516         break;
517
518       case SIMIX_FAILED:
519         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
520         simcall->issuer->context->iwannadie = 1;
521         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
522         break;
523
524       case SIMIX_CANCELED:
525         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
526         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
527         break;
528
529       default:
530         xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
531             (int)action->state);
532     }
533     /* check if the host is down */
534     if (ws_model->extension.workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
535       simcall->issuer->context->iwannadie = 1;
536     }
537
538     simcall->issuer->waiting_action =    NULL;
539     simcall_host_execution_wait__set__result(simcall, action->state);
540     SIMIX_simcall_answer(simcall);
541   }
542
543   /* We no longer need it */
544   SIMIX_host_execution_destroy(action);
545 }
546
547
548 void SIMIX_post_host_execute(smx_action_t action)
549 {
550   if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
551                                                * for parallel tasks too */
552       surf_workstation_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
553     /* If the host running the action failed, notice it so that the asking
554      * process can be killed if it runs on that host itself */
555     action->state = SIMIX_FAILED;
556   } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
557     /* If the host running the action didn't fail, then the action was
558      * canceled */
559     action->state = SIMIX_CANCELED;
560   } else {
561     action->state = SIMIX_DONE;
562   }
563
564   if (action->execution.surf_exec) {
565     surf_workstation_model->action_unref(action->execution.surf_exec);
566     action->execution.surf_exec = NULL;
567   }
568
569   /* If there are simcalls associated with the action, then answer them */
570   if (xbt_fifo_size(action->simcalls)) {
571     SIMIX_execution_finish(action);
572   }
573 }
574
575
576 #ifdef HAVE_TRACING
577 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
578                             const char *category){
579   SIMIX_set_category(action, category);
580 }
581 void SIMIX_set_category(smx_action_t action, const char *category)
582 {
583   surf_model_t ws_model = get_ws_model_from_action(action);
584
585   if (action->state != SIMIX_RUNNING) return;
586   if (action->type == SIMIX_ACTION_EXECUTE){
587     ws_model->set_category(action->execution.surf_exec, category);
588   }else if (action->type == SIMIX_ACTION_COMMUNICATE){
589     ws_model->set_category(action->comm.surf_comm, category);
590   }
591 }
592 #endif
593