Logo AND Algorithmique Numérique Distribuée

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