Logo AND Algorithmique Numérique Distribuée

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