1 /* Copyright (c) 2007-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "smx_private.h"
8 #include "xbt/sysdep.h"
12 #include "mc/mc_replay.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix,
17 static void SIMIX_execution_finish(smx_synchro_t synchro);
20 * \brief Internal function to create a SIMIX host.
21 * \param name name of the host to create
22 * \param data some user data (may be NULL)
24 smx_host_t SIMIX_host_create(const char *name, void *data)
26 smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
30 smx_host->process_list =
31 xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
33 /* Update global variables */
34 xbt_lib_set(host_lib,name,SIMIX_HOST_LEVEL,smx_host);
36 return xbt_lib_get_elm_or_null(host_lib, name);
40 * \brief Start the host if it is off
43 void SIMIX_host_on(smx_host_t h)
45 smx_host_priv_t host = SIMIX_host_priv(h);
47 xbt_assert((host != NULL), "Invalid parameters");
49 if (surf_resource_get_state(surf_host_resource_priv(h))==SURF_RESOURCE_OFF) {
50 surf_resource_set_state(surf_host_resource_priv(h), SURF_RESOURCE_ON);
53 smx_process_arg_t arg;
54 xbt_dynar_foreach(host->boot_processes,cpt,arg) {
55 smx_process_t process;
57 char** argv = xbt_new(char*, arg->argc);
58 for (int i=0; i<arg->argc; i++)
59 argv[i] = xbt_strdup(arg->argv[i]);
61 XBT_DEBUG("Booting Process %s(%s) right now", arg->argv[0], arg->hostname);
62 if (simix_global->create_process_function) {
63 simix_global->create_process_function(&process,
75 simcall_process_create(&process,
90 void simcall_HANDLER_host_off(smx_simcall_t simcall, smx_host_t h)
92 SIMIX_host_off(h, simcall->issuer);
96 * \brief Stop the host if it is on
99 void SIMIX_host_off(smx_host_t h, smx_process_t issuer)
101 smx_host_priv_t host = SIMIX_host_priv(h);
103 xbt_assert((host != NULL), "Invalid parameters");
105 if (surf_resource_get_state(surf_host_resource_priv(h))==SURF_RESOURCE_ON) {
106 surf_resource_set_state(surf_host_resource_priv(h), SURF_RESOURCE_OFF);
108 /* Clean Simulator data */
109 if (xbt_swag_size(host->process_list) != 0) {
110 smx_process_t process = NULL;
111 xbt_swag_foreach(process, host->process_list) {
112 SIMIX_process_kill(process, issuer);
113 XBT_DEBUG("Killing %s on %s by %s", process->name, sg_host_name(process->smx_host), issuer->name);
120 * \brief Internal function to destroy a SIMIX host.
122 * \param h the host to destroy (a smx_host_t)
124 void SIMIX_host_destroy(void *h)
126 smx_host_priv_t host = (smx_host_priv_t) h;
128 xbt_assert((host != NULL), "Invalid parameters");
130 /* Clean Simulator data */
131 if (xbt_swag_size(host->process_list) != 0) {
132 char *msg = xbt_strdup("Shutting down host, but it's not empty:");
134 smx_process_t process = NULL;
136 xbt_swag_foreach(process, host->process_list) {
137 tmp = bprintf("%s\n\t%s", msg, process->name);
141 SIMIX_display_process_status();
142 THROWF(arg_error, 0, "%s", msg);
144 xbt_dynar_free(&host->auto_restart_processes);
145 xbt_dynar_free(&host->boot_processes);
146 xbt_swag_free(host->process_list);
148 /* Clean host structure */
153 smx_host_t SIMIX_host_get_by_name(const char *name){
154 xbt_assert(((simix_global != NULL)
155 && (host_lib != NULL)),
156 "Environment not set yet");
158 return xbt_lib_get_elm_or_null(host_lib, name);
161 smx_host_t SIMIX_host_self(void)
163 smx_process_t process = SIMIX_process_self();
164 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
167 /* needs to be public and without simcall because it is called
168 by exceptions and logging events */
169 const char* SIMIX_host_self_get_name(void)
171 smx_host_t host = SIMIX_host_self();
172 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
175 return SIMIX_host_get_name(host);
178 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
179 return surf_resource_get_properties(surf_host_resource_priv(host));
182 double SIMIX_host_get_speed(smx_host_t host){
183 return surf_host_get_speed(host, 1.0);
186 int SIMIX_host_get_core(smx_host_t host){
187 return surf_host_get_core(host);
190 xbt_swag_t SIMIX_host_get_process_list(smx_host_t host){
191 smx_host_priv_t host_priv = SIMIX_host_priv(host);
193 return host_priv->process_list;
197 double SIMIX_host_get_available_speed(smx_host_t host){
198 return surf_host_get_available_speed(host);
201 double SIMIX_host_get_current_power_peak(smx_host_t host) {
202 return surf_host_get_current_power_peak(host);
205 double SIMIX_host_get_power_peak_at(smx_host_t host, int pstate_index) {
206 return surf_host_get_power_peak_at(host, pstate_index);
209 int SIMIX_host_get_nb_pstates(smx_host_t host) {
210 return surf_host_get_nb_pstates(host);
214 void SIMIX_host_set_pstate(smx_host_t host, int pstate_index) {
215 surf_host_set_pstate(host, pstate_index);
217 int SIMIX_host_get_pstate(smx_host_t host) {
218 return surf_host_get_pstate(host);
221 double SIMIX_host_get_consumed_energy(smx_host_t host) {
222 return surf_host_get_consumed_energy(host);
224 double SIMIX_host_get_wattmin_at(smx_host_t host,int pstate) {
225 return surf_host_get_wattmin_at(host,pstate);
227 double SIMIX_host_get_wattmax_at(smx_host_t host,int pstate) {
228 return surf_host_get_wattmax_at(host,pstate);
231 int SIMIX_host_get_state(smx_host_t host){
232 return surf_resource_get_state(surf_host_resource_priv(host));
235 void _SIMIX_host_free_process_arg(void *data)
237 smx_process_arg_t arg = *(void**)data;
239 for (i = 0; i < arg->argc; i++)
240 xbt_free(arg->argv[i]);
246 * \brief Add a process to the list of the processes that the host will restart when it comes back
247 * This function add a process to the list of the processes that will be restarted when the host comes
248 * back. It is expected that this function is called when the host is down.
249 * The processes will only be restarted once, meaning that you will have to register the process
250 * again to restart the process again.
252 void SIMIX_host_add_auto_restart_process(smx_host_t host,
254 xbt_main_func_t code,
256 const char *hostname,
258 int argc, char **argv,
259 xbt_dict_t properties,
262 if (!SIMIX_host_priv(host)->auto_restart_processes) {
263 SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
265 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
266 arg->name = xbt_strdup(name);
269 arg->hostname = hostname;
270 arg->kill_time = kill_time;
273 arg->argv = xbt_new(char*,argc + 1);
276 for (i = 0; i < argc; i++) {
277 arg->argv[i] = xbt_strdup(argv[i]);
279 arg->argv[argc] = NULL;
281 arg->properties = properties;
282 arg->auto_restart = auto_restart;
284 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
285 && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
286 xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
287 XBT_DEBUG("Have pushed host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
289 xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
292 * \brief Restart the list of processes that have been registered to the host
294 void SIMIX_host_restart_processes(smx_host_t host)
297 smx_process_arg_t arg;
298 xbt_dynar_t process_list = SIMIX_host_priv(host)->auto_restart_processes;
302 xbt_dynar_foreach (process_list, cpt, arg) {
304 smx_process_t process;
306 XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
307 if (simix_global->create_process_function) {
308 simix_global->create_process_function(&process,
320 simcall_process_create(&process,
332 /* arg->argv is used by the process created above. Hide it to
333 * _SIMIX_host_free_process_arg() which is called by xbt_dynar_reset()
338 xbt_dynar_reset(process_list);
341 void SIMIX_host_autorestart(smx_host_t host)
343 if(simix_global->autorestart)
344 simix_global->autorestart(host);
346 xbt_die("No function for simix_global->autorestart");
349 smx_synchro_t SIMIX_host_execute(const char *name,
350 smx_host_t host, double flops_amount, double priority, double bound, unsigned long affinity_mask){
352 /* alloc structures and initialize */
353 smx_synchro_t synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
354 synchro->type = SIMIX_SYNC_EXECUTE;
355 synchro->name = xbt_strdup(name);
356 synchro->state = SIMIX_RUNNING;
357 synchro->execution.host = host;
358 synchro->category = NULL;
360 /* set surf's action */
361 if (!MC_is_active() && !MC_record_replay_is_active()) {
363 synchro->execution.surf_exec = surf_host_execute(host, flops_amount);
364 surf_action_set_data(synchro->execution.surf_exec, synchro);
365 surf_action_set_priority(synchro->execution.surf_exec, priority);
367 /* Note (hypervisor): for multicore, the bound value being passed to the
368 * surf layer should not be zero (i.e., unlimited). It should be the
369 * capacity of a CPU core. */
371 surf_cpu_action_set_bound(synchro->execution.surf_exec, SIMIX_host_get_speed(host));
373 surf_cpu_action_set_bound(synchro->execution.surf_exec, bound);
375 if (affinity_mask != 0) {
376 /* just a double check to confirm that this host is the host where this task is running. */
377 xbt_assert(synchro->execution.host == host);
378 surf_cpu_action_set_affinity(synchro->execution.surf_exec, host, affinity_mask);
382 XBT_DEBUG("Create execute synchro %p: %s", synchro, synchro->name);
387 smx_synchro_t SIMIX_host_parallel_execute(const char *name,
388 int host_nb, smx_host_t *host_list,
389 double *flops_amount, double *bytes_amount,
390 double amount, double rate){
392 void **surf_host_list = NULL;
395 /* alloc structures and initialize */
396 smx_synchro_t synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
397 synchro->type = SIMIX_SYNC_PARALLEL_EXECUTE;
398 synchro->name = xbt_strdup(name);
399 synchro->state = SIMIX_RUNNING;
400 synchro->execution.host = NULL; /* FIXME: do we need the list of hosts? */
401 synchro->category = NULL;
403 /* set surf's synchro */
404 surf_host_list = xbt_new0(void *, host_nb);
405 for (i = 0; i < host_nb; i++)
406 surf_host_list[i] = surf_host_resource_priv(host_list[i]);
409 /* FIXME: what happens if host_list contains VMs and PMs. If
410 * execute_parallel_task() does not change the state of the model, we can mix
412 surf_model_t ws_model = surf_resource_model(host_list[0], SURF_HOST_LEVEL);
413 for (i = 1; i < host_nb; i++) {
414 surf_model_t ws_model_tmp = surf_resource_model(host_list[i], SURF_HOST_LEVEL);
415 if (ws_model_tmp != ws_model) {
416 XBT_CRITICAL("mixing VMs and PMs is not supported");
421 /* set surf's synchro */
422 if (!MC_is_active() && !MC_record_replay_is_active()) {
423 synchro->execution.surf_exec =
424 surf_host_model_execute_parallel_task((surf_host_model_t)surf_host_model,
425 host_nb, surf_host_list, flops_amount, bytes_amount, rate);
427 surf_action_set_data(synchro->execution.surf_exec, synchro);
429 XBT_DEBUG("Create parallel execute synchro %p", synchro);
434 void SIMIX_host_execution_destroy(smx_synchro_t synchro){
435 XBT_DEBUG("Destroy synchro %p", synchro);
437 if (synchro->execution.surf_exec) {
438 surf_action_unref(synchro->execution.surf_exec);
439 synchro->execution.surf_exec = NULL;
441 xbt_free(synchro->name);
442 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
445 void SIMIX_host_execution_cancel(smx_synchro_t synchro){
446 XBT_DEBUG("Cancel synchro %p", synchro);
448 if (synchro->execution.surf_exec)
449 surf_action_cancel(synchro->execution.surf_exec);
452 double SIMIX_host_execution_get_remains(smx_synchro_t synchro){
455 if (synchro->state == SIMIX_RUNNING)
456 result = surf_action_get_remains(synchro->execution.surf_exec);
461 e_smx_state_t SIMIX_host_execution_get_state(smx_synchro_t synchro){
462 return synchro->state;
465 void SIMIX_host_execution_set_priority(smx_synchro_t synchro, double priority){
467 if(synchro->execution.surf_exec)
468 surf_action_set_priority(synchro->execution.surf_exec, priority);
471 void SIMIX_host_execution_set_bound(smx_synchro_t synchro, double bound){
473 if(synchro->execution.surf_exec)
474 surf_cpu_action_set_bound(synchro->execution.surf_exec, bound);
477 void SIMIX_host_execution_set_affinity(smx_synchro_t synchro, smx_host_t host, unsigned long mask){
478 xbt_assert(synchro->type == SIMIX_SYNC_EXECUTE);
480 if (synchro->execution.surf_exec) {
481 /* just a double check to confirm that this host is the host where this task is running. */
482 xbt_assert(synchro->execution.host == host);
483 surf_cpu_action_set_affinity(synchro->execution.surf_exec, host, mask);
487 void simcall_HANDLER_host_execution_wait(smx_simcall_t simcall, smx_synchro_t synchro){
489 XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state);
491 /* Associate this simcall to the synchro */
492 xbt_fifo_push(synchro->simcalls, simcall);
493 simcall->issuer->waiting_synchro = synchro;
495 /* set surf's synchro */
496 if (MC_is_active() || MC_record_replay_is_active()) {
497 synchro->state = SIMIX_DONE;
498 SIMIX_execution_finish(synchro);
502 /* If the synchro is already finished then perform the error handling */
503 if (synchro->state != SIMIX_RUNNING)
504 SIMIX_execution_finish(synchro);
507 void SIMIX_host_execution_suspend(smx_synchro_t synchro)
509 if(synchro->execution.surf_exec)
510 surf_action_suspend(synchro->execution.surf_exec);
513 void SIMIX_host_execution_resume(smx_synchro_t synchro)
515 if(synchro->execution.surf_exec)
516 surf_action_resume(synchro->execution.surf_exec);
519 void SIMIX_execution_finish(smx_synchro_t synchro)
521 xbt_fifo_item_t item;
522 smx_simcall_t simcall;
524 xbt_fifo_foreach(synchro->simcalls, item, simcall, smx_simcall_t) {
526 switch (synchro->state) {
529 /* do nothing, synchro done */
530 XBT_DEBUG("SIMIX_execution_finished: execution successful");
534 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
535 simcall->issuer->context->iwannadie = 1;
536 SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
540 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
541 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
545 xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d",
546 (int)synchro->state);
548 /* check if the host is down */
549 if (surf_resource_get_state(surf_host_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
550 simcall->issuer->context->iwannadie = 1;
553 simcall->issuer->waiting_synchro = NULL;
554 simcall_host_execution_wait__set__result(simcall, synchro->state);
555 SIMIX_simcall_answer(simcall);
558 /* We no longer need it */
559 SIMIX_host_execution_destroy(synchro);
563 void SIMIX_post_host_execute(smx_synchro_t synchro)
565 if (synchro->type == SIMIX_SYNC_EXECUTE && /* FIMXE: handle resource failure
566 * for parallel tasks too */
567 surf_resource_get_state(surf_host_resource_priv(synchro->execution.host)) == SURF_RESOURCE_OFF) {
568 /* If the host running the synchro failed, notice it so that the asking
569 * process can be killed if it runs on that host itself */
570 synchro->state = SIMIX_FAILED;
571 } else if (surf_action_get_state(synchro->execution.surf_exec) == SURF_ACTION_FAILED) {
572 /* If the host running the synchro didn't fail, then the synchro was
574 synchro->state = SIMIX_CANCELED;
576 synchro->state = SIMIX_DONE;
579 if (synchro->execution.surf_exec) {
580 surf_action_unref(synchro->execution.surf_exec);
581 synchro->execution.surf_exec = NULL;
584 /* If there are simcalls associated with the synchro, then answer them */
585 if (xbt_fifo_size(synchro->simcalls)) {
586 SIMIX_execution_finish(synchro);
591 void SIMIX_set_category(smx_synchro_t synchro, const char *category)
593 if (synchro->state != SIMIX_RUNNING) return;
594 if (synchro->type == SIMIX_SYNC_EXECUTE){
595 surf_action_set_category(synchro->execution.surf_exec, category);
596 }else if (synchro->type == SIMIX_SYNC_COMMUNICATE){
597 surf_action_set_category(synchro->comm.surf_comm, category);
602 * \brief Function to get the parameters of the given the SIMIX host.
604 * \param host the host to get_phys_host (a smx_host_t)
605 * \param param the parameter object space to be overwritten (a ws_params_t)
607 void SIMIX_host_get_params(smx_host_t ind_vm, ws_params_t params)
609 /* jump to ws_get_params(). */
610 surf_host_get_params(ind_vm, params);
613 void SIMIX_host_set_params(smx_host_t ind_vm, ws_params_t params)
615 /* jump to ws_set_params(). */
616 surf_host_set_params(ind_vm, params);
619 xbt_dict_t SIMIX_host_get_mounted_storage_list(smx_host_t host){
620 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
622 return surf_host_get_mounted_storage_list(host);
625 xbt_dynar_t SIMIX_host_get_attached_storage_list(smx_host_t host){
626 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
628 return surf_host_get_attached_storage_list(host);