Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / src / simix / smx_host.cpp
1 /* Copyright (c) 2007-2015. 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 #include "src/mc/mc_replay.h"
13 #include "src/surf/host_interface.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix,
16                                 "SIMIX hosts");
17
18 static void SIMIX_execution_finish(smx_synchro_t synchro);
19
20 /**
21  * \brief Internal function to create a SIMIX host.
22  * \param name name of the host to create
23  */
24 void SIMIX_host_create(sg_host_t host) // FIXME: braindead prototype. Take sg_host as parameter
25 {
26   smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
27   s_smx_process_t proc;
28
29   /* Host structure */
30   smx_host->process_list =
31       xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
32
33   /* Update global variables */
34   sg_host_simix_set(host, smx_host);
35 }
36
37 /**
38  * \brief Start the host if it is off
39  *
40  */
41 void SIMIX_host_on(sg_host_t h)
42 {
43   smx_host_priv_t host = sg_host_simix(h);
44
45   xbt_assert((host != NULL), "Invalid parameters");
46
47   if (h->is_off()) {
48     simgrid::surf::Host* surf_host = h->extension<simgrid::surf::Host>();
49     surf_host->turnOn();
50
51     unsigned int cpt;
52     smx_process_arg_t arg;
53     xbt_dynar_foreach(host->boot_processes,cpt,arg) {
54
55       char** argv = xbt_new(char*, arg->argc);
56       for (int i=0; i<arg->argc; i++)
57         argv[i] = xbt_strdup(arg->argv[i]);
58
59       XBT_DEBUG("Booting Process %s(%s) right now", arg->argv[0], arg->hostname);
60       if (simix_global->create_process_function) {
61         simix_global->create_process_function(argv[0],
62                                               arg->code,
63                                               NULL,
64                                               arg->hostname,
65                                               arg->kill_time,
66                                               arg->argc,
67                                               argv,
68                                               arg->properties,
69                                               arg->auto_restart,
70                                               NULL);
71       } else {
72         simcall_process_create(arg->argv[0],
73                                arg->code,
74                                NULL,
75                                arg->hostname,
76                                arg->kill_time,
77                                arg->argc,
78                                argv,
79                                arg->properties,
80                                arg->auto_restart);
81       }
82     }
83   }
84 }
85
86 /**
87  * \brief Stop the host if it is on
88  *
89  */
90 void SIMIX_host_off(sg_host_t h, smx_process_t issuer)
91 {
92   smx_host_priv_t host = sg_host_simix(h);
93
94   xbt_assert((host != NULL), "Invalid parameters");
95
96   if (h->is_on()) {
97     simgrid::surf::Host* surf_host = h->extension<simgrid::surf::Host>();
98     surf_host->turnOff();
99
100     /* Clean Simulator data */
101     if (xbt_swag_size(host->process_list) != 0) {
102       smx_process_t process = NULL;
103       xbt_swag_foreach(process, host->process_list) {
104         SIMIX_process_kill(process, issuer);
105         XBT_DEBUG("Killing %s on %s by %s", process->name,  sg_host_get_name(process->host), issuer->name);
106       }
107     }
108   } else {
109     XBT_INFO("Host %s is already off",h->name().c_str());
110   }
111 }
112
113 /**
114  * \brief Internal function to destroy a SIMIX host.
115  *
116  * \param h the host to destroy (a sg_host_t)
117  */
118 void SIMIX_host_destroy(void *h)
119 {
120   smx_host_priv_t host = (smx_host_priv_t) h;
121
122   xbt_assert((host != NULL), "Invalid parameters");
123
124   /* Clean Simulator data */
125   if (xbt_swag_size(host->process_list) != 0) {
126     char *msg = xbt_strdup("Shutting down host, but it's not empty:");
127     char *tmp;
128     smx_process_t process = NULL;
129
130     xbt_swag_foreach(process, host->process_list) {
131       tmp = bprintf("%s\n\t%s", msg, process->name);
132       free(msg);
133       msg = tmp;
134     }
135     SIMIX_display_process_status();
136     THROWF(arg_error, 0, "%s", msg);
137   }
138   xbt_dynar_free(&host->auto_restart_processes);
139   xbt_dynar_free(&host->boot_processes);
140   xbt_swag_free(host->process_list);
141
142   /* Clean host structure */
143   free(host);
144   return;
145 }
146
147 sg_host_t SIMIX_host_self(void)
148 {
149   smx_process_t process = SIMIX_process_self();
150   return (process == NULL) ? NULL : SIMIX_process_get_host(process);
151 }
152
153 /* needs to be public and without simcall because it is called
154    by exceptions and logging events */
155 const char* SIMIX_host_self_get_name(void)
156 {
157   sg_host_t host = SIMIX_host_self();
158   if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
159     return "";
160
161   return sg_host_get_name(host);
162 }
163
164 void _SIMIX_host_free_process_arg(void *data)
165 {
166   smx_process_arg_t arg = *(smx_process_arg_t*)data;
167   int i;
168   for (i = 0; i < arg->argc; i++)
169     xbt_free(arg->argv[i]);
170   xbt_free(arg->argv);
171   xbt_free(arg->name);
172   xbt_free(arg);
173 }
174 /**
175  * \brief Add a process to the list of the processes that the host will restart when it comes back
176  * This function add a process to the list of the processes that will be restarted when the host comes
177  * back. It is expected that this function is called when the host is down.
178  * The processes will only be restarted once, meaning that you will have to register the process
179  * again to restart the process again.
180  */
181 void SIMIX_host_add_auto_restart_process(sg_host_t host,
182                                          const char *name,
183                                          xbt_main_func_t code,
184                                          void *data,
185                                          const char *hostname,
186                                          double kill_time,
187                                          int argc, char **argv,
188                                          xbt_dict_t properties,
189                                          int auto_restart)
190 {
191   if (!sg_host_simix(host)->auto_restart_processes) {
192     sg_host_simix(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
193   }
194   smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
195   arg->name = xbt_strdup(name);
196   arg->code = code;
197   arg->data = data;
198   arg->hostname = hostname;
199   arg->kill_time = kill_time;
200   arg->argc = argc;
201
202   arg->argv = xbt_new(char*,argc + 1);
203
204   int i;
205   for (i = 0; i < argc; i++) {
206     arg->argv[i] = xbt_strdup(argv[i]);
207   }
208   arg->argv[argc] = NULL;
209
210   arg->properties = properties;
211   arg->auto_restart = auto_restart;
212
213   if( ! sg_host_is_on(host)
214       && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_get_name(host))){
215     xbt_dict_set(watched_hosts_lib,sg_host_get_name(host),host,NULL);
216     XBT_DEBUG("Have pushed host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_get_name(host));
217   }
218   xbt_dynar_push_as(sg_host_simix(host)->auto_restart_processes,smx_process_arg_t,arg);
219 }
220 /**
221  * \brief Restart the list of processes that have been registered to the host
222  */
223 void SIMIX_host_autorestart(sg_host_t host)
224 {
225   unsigned int cpt;
226   smx_process_arg_t arg;
227   xbt_dynar_t process_list = sg_host_simix(host)->auto_restart_processes;
228   if (!process_list)
229     return;
230
231   xbt_dynar_foreach (process_list, cpt, arg) {
232
233     XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
234     if (simix_global->create_process_function) {
235       simix_global->create_process_function(arg->argv[0],
236                                             arg->code,
237                                             NULL,
238                                             arg->hostname,
239                                             arg->kill_time,
240                                             arg->argc,
241                                             arg->argv,
242                                             arg->properties,
243                                             arg->auto_restart,
244                                             NULL);
245     } else {
246       simcall_process_create(arg->argv[0],
247                              (xbt_main_func_t) arg->code,
248                              NULL,
249                              arg->hostname,
250                              arg->kill_time,
251                              arg->argc,
252                              arg->argv,
253                              arg->properties,
254                              arg->auto_restart);
255
256     }
257     /* arg->argv is used by the process created above.  Hide it to
258      * _SIMIX_host_free_process_arg() which is called by xbt_dynar_reset()
259      * below. */
260     arg->argc = 0;
261     arg->argv = NULL;
262   }
263   xbt_dynar_reset(process_list);
264 }
265
266 smx_synchro_t simcall_HANDLER_execution_start(smx_simcall_t simcall,
267                 const char* name, double flops_amount, double priority, double bound, unsigned long affinity_mask) {
268         return SIMIX_execution_start(simcall->issuer, name,flops_amount,priority,bound,affinity_mask);
269 }
270 smx_synchro_t SIMIX_execution_start(smx_process_t issuer, const char *name,
271      double flops_amount, double priority, double bound, unsigned long affinity_mask){
272
273   /* alloc structures and initialize */
274   smx_synchro_t synchro = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
275   synchro->type = SIMIX_SYNC_EXECUTE;
276   synchro->name = xbt_strdup(name);
277   synchro->state = SIMIX_RUNNING;
278   synchro->execution.host = issuer->host;
279   synchro->category = NULL;
280
281   /* set surf's action */
282   if (!MC_is_active() && !MC_record_replay_is_active()) {
283
284     synchro->execution.surf_exec = issuer->host->pimpl_cpu->execution_start(flops_amount);
285     synchro->execution.surf_exec->setData(synchro);
286     synchro->execution.surf_exec->setPriority(priority);
287
288     if (bound != 0)
289       static_cast<simgrid::surf::CpuAction*>(synchro->execution.surf_exec)
290         ->setBound(bound);
291
292     if (affinity_mask != 0) {
293       /* just a double check to confirm that this host is the host where this task is running. */
294       xbt_assert(synchro->execution.host == issuer->host);
295       static_cast<simgrid::surf::CpuAction*>(synchro->execution.surf_exec)
296         ->setAffinity(issuer->host->pimpl_cpu, affinity_mask);
297     }
298   }
299
300   XBT_DEBUG("Create execute synchro %p: %s", synchro, synchro->name);
301
302   return synchro;
303 }
304
305 smx_synchro_t SIMIX_execution_parallel_start(const char *name,
306     int host_nb, sg_host_t *host_list,
307     double *flops_amount, double *bytes_amount,
308     double amount, double rate){
309
310   sg_host_t*host_list_cpy = NULL;
311   int i;
312
313   /* alloc structures and initialize */
314   smx_synchro_t synchro = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
315   synchro->type = SIMIX_SYNC_PARALLEL_EXECUTE;
316   synchro->name = xbt_strdup(name);
317   synchro->state = SIMIX_RUNNING;
318   synchro->execution.host = NULL; /* FIXME: do we need the list of hosts? */
319   synchro->category = NULL;
320
321   /* set surf's synchro */
322   host_list_cpy = xbt_new0(sg_host_t, host_nb);
323   for (i = 0; i < host_nb; i++)
324     host_list_cpy[i] = host_list[i];
325
326
327   /* FIXME: what happens if host_list contains VMs and PMs. If
328    * execute_parallel_task() does not change the state of the model, we can mix
329    * them. */
330   surf_host_model_t ws_model =
331     host_list[0]->extension<simgrid::surf::Host>()->getModel();
332   for (i = 1; i < host_nb; i++) {
333     surf_host_model_t ws_model_tmp =
334       host_list[i]->extension<simgrid::surf::Host>()->getModel();
335     if (ws_model_tmp != ws_model) {
336       XBT_CRITICAL("mixing VMs and PMs is not supported");
337       DIE_IMPOSSIBLE;
338     }
339   }
340
341   /* set surf's synchro */
342   if (!MC_is_active() && !MC_record_replay_is_active()) {
343     synchro->execution.surf_exec =
344       surf_host_model->executeParallelTask(
345                   host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
346
347     synchro->execution.surf_exec->setData(synchro);
348   }
349   XBT_DEBUG("Create parallel execute synchro %p", synchro);
350
351   return synchro;
352 }
353
354 void SIMIX_execution_destroy(smx_synchro_t synchro)
355 {
356   XBT_DEBUG("Destroy synchro %p", synchro);
357
358   if (synchro->execution.surf_exec) {
359     synchro->execution.surf_exec->unref();
360     synchro->execution.surf_exec = NULL;
361   }
362   xbt_free(synchro->name);
363   xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
364 }
365
366 void SIMIX_execution_cancel(smx_synchro_t synchro)
367 {
368   XBT_DEBUG("Cancel synchro %p", synchro);
369
370   if (synchro->execution.surf_exec)
371     synchro->execution.surf_exec->cancel();
372 }
373
374 double SIMIX_execution_get_remains(smx_synchro_t synchro)
375 {
376   double result = 0.0;
377
378   if (synchro->state == SIMIX_RUNNING)
379     result = surf_action_get_remains(synchro->execution.surf_exec);
380
381   return result;
382 }
383
384 e_smx_state_t SIMIX_execution_get_state(smx_synchro_t synchro)
385 {
386   return synchro->state;
387 }
388
389 void SIMIX_execution_set_priority(smx_synchro_t synchro, double priority)
390 {
391   if(synchro->execution.surf_exec)
392     synchro->execution.surf_exec->setPriority(priority);
393 }
394
395 void SIMIX_execution_set_bound(smx_synchro_t synchro, double bound)
396 {
397   if(synchro->execution.surf_exec)
398     static_cast<simgrid::surf::CpuAction*>(synchro->execution.surf_exec)->setBound(bound);
399 }
400
401 void SIMIX_execution_set_affinity(smx_synchro_t synchro, sg_host_t host, unsigned long mask)
402 {
403   xbt_assert(synchro->type == SIMIX_SYNC_EXECUTE);
404
405   if (synchro->execution.surf_exec) {
406     /* just a double check to confirm that this host is the host where this task is running. */
407     xbt_assert(synchro->execution.host == host);
408     static_cast<simgrid::surf::CpuAction*>(synchro->execution.surf_exec)
409       ->setAffinity(host->pimpl_cpu, mask);
410   }
411 }
412
413 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_synchro_t synchro)
414 {
415
416   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state);
417
418   /* Associate this simcall to the synchro */
419   xbt_fifo_push(synchro->simcalls, simcall);
420   simcall->issuer->waiting_synchro = synchro;
421
422   /* set surf's synchro */
423   if (MC_is_active() || MC_record_replay_is_active()) {
424     synchro->state = SIMIX_DONE;
425     SIMIX_execution_finish(synchro);
426     return;
427   }
428
429   /* If the synchro is already finished then perform the error handling */
430   if (synchro->state != SIMIX_RUNNING)
431     SIMIX_execution_finish(synchro);
432 }
433
434 void SIMIX_execution_suspend(smx_synchro_t synchro)
435 {
436   if(synchro->execution.surf_exec)
437     synchro->execution.surf_exec->suspend();
438 }
439
440 void SIMIX_execution_resume(smx_synchro_t synchro)
441 {
442   if(synchro->execution.surf_exec)
443     synchro->execution.surf_exec->resume();
444 }
445
446 void SIMIX_execution_finish(smx_synchro_t synchro)
447 {
448   xbt_fifo_item_t item;
449   smx_simcall_t simcall;
450
451   xbt_fifo_foreach(synchro->simcalls, item, simcall, smx_simcall_t) {
452
453     switch (synchro->state) {
454
455       case SIMIX_DONE:
456         /* do nothing, synchro done */
457         XBT_DEBUG("SIMIX_execution_finished: execution successful");
458         break;
459
460       case SIMIX_FAILED:
461         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_get_name(simcall->issuer->host));
462         simcall->issuer->context->iwannadie = 1;
463         SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
464         break;
465
466       case SIMIX_CANCELED:
467         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
468         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
469         break;
470
471       default:
472         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d",
473             (int)synchro->state);
474     }
475     /* check if the host is down */
476     if (simcall->issuer->host->is_off()) {
477       simcall->issuer->context->iwannadie = 1;
478     }
479
480     simcall->issuer->waiting_synchro =    NULL;
481     simcall_execution_wait__set__result(simcall, synchro->state);
482     SIMIX_simcall_answer(simcall);
483   }
484
485   /* We no longer need it */
486   SIMIX_execution_destroy(synchro);
487 }
488
489
490 void SIMIX_post_host_execute(smx_synchro_t synchro)
491 {
492   if (synchro->type == SIMIX_SYNC_EXECUTE && /* FIMXE: handle resource failure
493                                                * for parallel tasks too */
494       synchro->execution.host->is_off()) {
495     /* If the host running the synchro failed, notice it so that the asking
496      * process can be killed if it runs on that host itself */
497     synchro->state = SIMIX_FAILED;
498   } else if (synchro->execution.surf_exec->getState() == SURF_ACTION_FAILED) {
499     /* If the host running the synchro didn't fail, then the synchro was
500      * canceled */
501     synchro->state = SIMIX_CANCELED;
502   } else {
503     synchro->state = SIMIX_DONE;
504   }
505
506   if (synchro->execution.surf_exec) {
507     synchro->execution.surf_exec->unref();
508     synchro->execution.surf_exec = NULL;
509   }
510
511   /* If there are simcalls associated with the synchro, then answer them */
512   if (xbt_fifo_size(synchro->simcalls)) {
513     SIMIX_execution_finish(synchro);
514   }
515 }
516
517
518 void SIMIX_set_category(smx_synchro_t synchro, const char *category)
519 {
520   if (synchro->state != SIMIX_RUNNING) return;
521   if (synchro->type == SIMIX_SYNC_EXECUTE){
522     synchro->execution.surf_exec->setCategory(category);
523   }else if (synchro->type == SIMIX_SYNC_COMMUNICATE){
524     synchro->comm.surf_comm->setCategory(category);
525   }
526 }