Logo AND Algorithmique Numérique Distribuée

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