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 workstation the SURF workstation to encapsulate
23 * \param data some user data (may be NULL)
25 smx_host_t SIMIX_host_create(const char *name,
26 void *workstation, void *data)
28 smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
32 smx_host->process_list =
33 xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
35 /* Update global variables */
36 xbt_lib_set(host_lib,name,SIMIX_HOST_LEVEL,smx_host);
38 return xbt_lib_get_elm_or_null(host_lib, name);
42 * \brief Start the host if it is off
45 void SIMIX_host_on(smx_host_t h)
47 smx_host_priv_t host = SIMIX_host_priv(h);
49 xbt_assert((host != NULL), "Invalid parameters");
51 if (surf_resource_get_state(surf_workstation_resource_priv(h))==SURF_RESOURCE_OFF) {
52 surf_resource_set_state(surf_workstation_resource_priv(h), SURF_RESOURCE_ON);
55 smx_process_arg_t arg;
56 xbt_dynar_foreach(host->boot_processes,cpt,arg) {
57 smx_process_t process;
59 char** argv = xbt_new(char*, arg->argc);
60 for (int i=0; i<arg->argc; i++)
61 argv[i] = xbt_strdup(arg->argv[i]);
63 XBT_DEBUG("Booting Process %s(%s) right now", arg->argv[0], arg->hostname);
64 if (simix_global->create_process_function) {
65 simix_global->create_process_function(&process,
77 simcall_process_create(&process,
92 void simcall_HANDLER_host_off(smx_simcall_t simcall, smx_host_t h)
94 SIMIX_host_off(h, simcall->issuer);
98 * \brief Stop the host if it is on
101 void SIMIX_host_off(smx_host_t h, smx_process_t issuer)
103 smx_host_priv_t host = SIMIX_host_priv(h);
105 xbt_assert((host != NULL), "Invalid parameters");
107 if (surf_resource_get_state(surf_workstation_resource_priv(h))==SURF_RESOURCE_ON) {
108 surf_resource_set_state(surf_workstation_resource_priv(h), SURF_RESOURCE_OFF);
110 /* Clean Simulator data */
111 if (xbt_swag_size(host->process_list) != 0) {
112 smx_process_t process = NULL;
113 xbt_swag_foreach(process, host->process_list) {
114 SIMIX_process_kill(process, issuer);
115 XBT_DEBUG("Killing %s on %s by %s", process->name, sg_host_name(process->smx_host), issuer->name);
122 * \brief Internal function to destroy a SIMIX host.
124 * \param h the host to destroy (a smx_host_t)
126 void SIMIX_host_destroy(void *h)
128 smx_host_priv_t host = (smx_host_priv_t) h;
130 xbt_assert((host != NULL), "Invalid parameters");
132 /* Clean Simulator data */
133 if (xbt_swag_size(host->process_list) != 0) {
134 char *msg = xbt_strdup("Shutting down host, but it's not empty:");
136 smx_process_t process = NULL;
138 xbt_swag_foreach(process, host->process_list) {
139 tmp = bprintf("%s\n\t%s", msg, process->name);
143 SIMIX_display_process_status();
144 THROWF(arg_error, 0, "%s", msg);
146 xbt_dynar_free(&host->auto_restart_processes);
147 xbt_dynar_free(&host->boot_processes);
148 xbt_swag_free(host->process_list);
150 /* Clean host structure */
155 smx_host_t SIMIX_host_get_by_name(const char *name){
156 xbt_assert(((simix_global != NULL)
157 && (host_lib != NULL)),
158 "Environment not set yet");
160 return xbt_lib_get_elm_or_null(host_lib, name);
163 smx_host_t SIMIX_host_self(void)
165 smx_process_t process = SIMIX_process_self();
166 return (process == NULL) ? NULL : SIMIX_process_get_host(process);
169 /* needs to be public and without simcall because it is called
170 by exceptions and logging events */
171 const char* SIMIX_host_self_get_name(void)
173 smx_host_t host = SIMIX_host_self();
174 if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
177 return SIMIX_host_get_name(host);
180 const char* SIMIX_host_get_name(smx_host_t host){
181 xbt_assert((host != NULL), "Invalid parameters");
183 return sg_host_name(host);
186 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
187 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
189 return surf_resource_get_properties(surf_workstation_resource_priv(host));
192 double SIMIX_host_get_speed(smx_host_t host){
193 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
194 return surf_workstation_get_speed(host, 1.0);
197 int SIMIX_host_get_core(smx_host_t host){
198 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
200 return surf_workstation_get_core(host);
203 xbt_swag_t SIMIX_host_get_process_list(smx_host_t host){
204 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
205 smx_host_priv_t host_priv = SIMIX_host_priv(host);
207 return host_priv->process_list;
211 double SIMIX_host_get_available_speed(smx_host_t host){
212 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
214 return surf_workstation_get_available_speed(host);
217 double SIMIX_host_get_current_power_peak(smx_host_t host) {
218 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
219 return surf_workstation_get_current_power_peak(host);
222 double SIMIX_host_get_power_peak_at(smx_host_t host, int pstate_index) {
223 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
225 return surf_workstation_get_power_peak_at(host, pstate_index);
228 int SIMIX_host_get_nb_pstates(smx_host_t host) {
229 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
231 return surf_workstation_get_nb_pstates(host);
235 void SIMIX_host_set_power_peak_at(smx_host_t host, int pstate_index) {
236 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
238 surf_workstation_set_power_peak_at(host, pstate_index);
241 double SIMIX_host_get_consumed_energy(smx_host_t host) {
242 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
243 return surf_workstation_get_consumed_energy(host);
246 int SIMIX_host_get_state(smx_host_t host){
247 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
249 return surf_resource_get_state(surf_workstation_resource_priv(host));
252 void _SIMIX_host_free_process_arg(void *data)
254 smx_process_arg_t arg = *(void**)data;
256 for (i = 0; i < arg->argc; i++)
257 xbt_free(arg->argv[i]);
263 * \brief Add a process to the list of the processes that the host will restart when it comes back
264 * This function add a process to the list of the processes that will be restarted when the host comes
265 * back. It is expected that this function is called when the host is down.
266 * The processes will only be restarted once, meaning that you will have to register the process
267 * again to restart the process again.
269 void SIMIX_host_add_auto_restart_process(smx_host_t host,
271 xbt_main_func_t code,
273 const char *hostname,
275 int argc, char **argv,
276 xbt_dict_t properties,
279 if (!SIMIX_host_priv(host)->auto_restart_processes) {
280 SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
282 smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
283 arg->name = xbt_strdup(name);
286 arg->hostname = hostname;
287 arg->kill_time = kill_time;
290 arg->argv = xbt_new(char*,argc + 1);
293 for (i = 0; i < argc; i++) {
294 arg->argv[i] = xbt_strdup(argv[i]);
296 arg->argv[argc] = NULL;
298 arg->properties = properties;
299 arg->auto_restart = auto_restart;
301 if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
302 && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
303 xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
304 XBT_DEBUG("Have pushed host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
306 xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
309 * \brief Restart the list of processes that have been registered to the host
311 void SIMIX_host_restart_processes(smx_host_t host)
314 smx_process_arg_t arg;
315 xbt_dynar_t process_list = SIMIX_host_priv(host)->auto_restart_processes;
319 xbt_dynar_foreach (process_list, cpt, arg) {
321 smx_process_t process;
323 XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
324 if (simix_global->create_process_function) {
325 simix_global->create_process_function(&process,
337 simcall_process_create(&process,
349 /* arg->argv is used by the process created above. Hide it to
350 * _SIMIX_host_free_process_arg() which is called by xbt_dynar_reset()
355 xbt_dynar_reset(process_list);
358 void SIMIX_host_autorestart(smx_host_t host)
360 if(simix_global->autorestart)
361 simix_global->autorestart(host);
363 xbt_die("No function for simix_global->autorestart");
366 smx_synchro_t SIMIX_host_execute(const char *name,
367 smx_host_t host, double flops_amount, double priority, double bound, unsigned long affinity_mask){
369 /* alloc structures and initialize */
370 smx_synchro_t synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
371 synchro->type = SIMIX_SYNC_EXECUTE;
372 synchro->name = xbt_strdup(name);
373 synchro->state = SIMIX_RUNNING;
374 synchro->execution.host = host;
375 synchro->category = NULL;
377 /* set surf's action */
378 if (!MC_is_active() && !MC_record_replay_is_active()) {
380 synchro->execution.surf_exec = surf_workstation_execute(host, flops_amount);
381 surf_action_set_data(synchro->execution.surf_exec, synchro);
382 surf_action_set_priority(synchro->execution.surf_exec, priority);
384 /* Note (hypervisor): for multicore, the bound value being passed to the
385 * surf layer should not be zero (i.e., unlimited). It should be the
386 * capacity of a CPU core. */
388 surf_cpu_action_set_bound(synchro->execution.surf_exec, SIMIX_host_get_speed(host));
390 surf_cpu_action_set_bound(synchro->execution.surf_exec, bound);
392 if (affinity_mask != 0) {
393 /* just a double check to confirm that this host is the host where this task is running. */
394 xbt_assert(synchro->execution.host == host);
395 surf_cpu_action_set_affinity(synchro->execution.surf_exec, host, affinity_mask);
399 XBT_DEBUG("Create execute synchro %p: %s", synchro, synchro->name);
404 smx_synchro_t SIMIX_host_parallel_execute(const char *name,
405 int host_nb, smx_host_t *host_list,
406 double *flops_amount, double *bytes_amount,
407 double amount, double rate){
409 void **workstation_list = NULL;
412 /* alloc structures and initialize */
413 smx_synchro_t synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
414 synchro->type = SIMIX_SYNC_PARALLEL_EXECUTE;
415 synchro->name = xbt_strdup(name);
416 synchro->state = SIMIX_RUNNING;
417 synchro->execution.host = NULL; /* FIXME: do we need the list of hosts? */
418 synchro->category = NULL;
420 /* set surf's synchro */
421 workstation_list = xbt_new0(void *, host_nb);
422 for (i = 0; i < host_nb; i++)
423 workstation_list[i] = surf_workstation_resource_priv(host_list[i]);
426 /* FIXME: what happens if host_list contains VMs and PMs. If
427 * execute_parallel_task() does not change the state of the model, we can mix
429 surf_model_t ws_model = surf_resource_model(host_list[0], SURF_WKS_LEVEL);
430 for (i = 1; i < host_nb; i++) {
431 surf_model_t ws_model_tmp = surf_resource_model(host_list[i], SURF_WKS_LEVEL);
432 if (ws_model_tmp != ws_model) {
433 XBT_CRITICAL("mixing VMs and PMs is not supported");
438 /* set surf's synchro */
439 if (!MC_is_active() && !MC_record_replay_is_active()) {
440 synchro->execution.surf_exec =
441 surf_workstation_model_execute_parallel_task((surf_workstation_model_t)surf_workstation_model,
442 host_nb, workstation_list, flops_amount, bytes_amount, rate);
444 surf_action_set_data(synchro->execution.surf_exec, synchro);
446 XBT_DEBUG("Create parallel execute synchro %p", synchro);
451 void SIMIX_host_execution_destroy(smx_synchro_t synchro){
452 XBT_DEBUG("Destroy synchro %p", synchro);
454 if (synchro->execution.surf_exec) {
455 surf_action_unref(synchro->execution.surf_exec);
456 synchro->execution.surf_exec = NULL;
458 xbt_free(synchro->name);
459 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
462 void SIMIX_host_execution_cancel(smx_synchro_t synchro){
463 XBT_DEBUG("Cancel synchro %p", synchro);
465 if (synchro->execution.surf_exec)
466 surf_action_cancel(synchro->execution.surf_exec);
469 double SIMIX_host_execution_get_remains(smx_synchro_t synchro){
472 if (synchro->state == SIMIX_RUNNING)
473 result = surf_action_get_remains(synchro->execution.surf_exec);
478 e_smx_state_t SIMIX_host_execution_get_state(smx_synchro_t synchro){
479 return synchro->state;
482 void SIMIX_host_execution_set_priority(smx_synchro_t synchro, double priority){
484 if(synchro->execution.surf_exec)
485 surf_action_set_priority(synchro->execution.surf_exec, priority);
488 void SIMIX_host_execution_set_bound(smx_synchro_t synchro, double bound){
490 if(synchro->execution.surf_exec)
491 surf_cpu_action_set_bound(synchro->execution.surf_exec, bound);
494 void SIMIX_host_execution_set_affinity(smx_synchro_t synchro, smx_host_t host, unsigned long mask){
495 xbt_assert(synchro->type == SIMIX_SYNC_EXECUTE);
497 if (synchro->execution.surf_exec) {
498 /* just a double check to confirm that this host is the host where this task is running. */
499 xbt_assert(synchro->execution.host == host);
500 surf_cpu_action_set_affinity(synchro->execution.surf_exec, host, mask);
504 void simcall_HANDLER_host_execution_wait(smx_simcall_t simcall, smx_synchro_t synchro){
506 XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state);
508 /* Associate this simcall to the synchro */
509 xbt_fifo_push(synchro->simcalls, simcall);
510 simcall->issuer->waiting_synchro = synchro;
512 /* set surf's synchro */
513 if (MC_is_active() || MC_record_replay_is_active()) {
514 synchro->state = SIMIX_DONE;
515 SIMIX_execution_finish(synchro);
519 /* If the synchro is already finished then perform the error handling */
520 if (synchro->state != SIMIX_RUNNING)
521 SIMIX_execution_finish(synchro);
524 void SIMIX_host_execution_suspend(smx_synchro_t synchro)
526 if(synchro->execution.surf_exec)
527 surf_action_suspend(synchro->execution.surf_exec);
530 void SIMIX_host_execution_resume(smx_synchro_t synchro)
532 if(synchro->execution.surf_exec)
533 surf_action_resume(synchro->execution.surf_exec);
536 void SIMIX_execution_finish(smx_synchro_t synchro)
538 xbt_fifo_item_t item;
539 smx_simcall_t simcall;
541 xbt_fifo_foreach(synchro->simcalls, item, simcall, smx_simcall_t) {
543 switch (synchro->state) {
546 /* do nothing, synchro done */
547 XBT_DEBUG("SIMIX_execution_finished: execution successful");
551 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
552 simcall->issuer->context->iwannadie = 1;
553 SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
557 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
558 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
562 xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d",
563 (int)synchro->state);
565 /* check if the host is down */
566 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
567 simcall->issuer->context->iwannadie = 1;
570 simcall->issuer->waiting_synchro = NULL;
571 simcall_host_execution_wait__set__result(simcall, synchro->state);
572 SIMIX_simcall_answer(simcall);
575 /* We no longer need it */
576 SIMIX_host_execution_destroy(synchro);
580 void SIMIX_post_host_execute(smx_synchro_t synchro)
582 if (synchro->type == SIMIX_SYNC_EXECUTE && /* FIMXE: handle resource failure
583 * for parallel tasks too */
584 surf_resource_get_state(surf_workstation_resource_priv(synchro->execution.host)) == SURF_RESOURCE_OFF) {
585 /* If the host running the synchro failed, notice it so that the asking
586 * process can be killed if it runs on that host itself */
587 synchro->state = SIMIX_FAILED;
588 } else if (surf_action_get_state(synchro->execution.surf_exec) == SURF_ACTION_FAILED) {
589 /* If the host running the synchro didn't fail, then the synchro was
591 synchro->state = SIMIX_CANCELED;
593 synchro->state = SIMIX_DONE;
596 if (synchro->execution.surf_exec) {
597 surf_action_unref(synchro->execution.surf_exec);
598 synchro->execution.surf_exec = NULL;
601 /* If there are simcalls associated with the synchro, then answer them */
602 if (xbt_fifo_size(synchro->simcalls)) {
603 SIMIX_execution_finish(synchro);
608 void SIMIX_set_category(smx_synchro_t synchro, const char *category)
610 if (synchro->state != SIMIX_RUNNING) return;
611 if (synchro->type == SIMIX_SYNC_EXECUTE){
612 surf_action_set_category(synchro->execution.surf_exec, category);
613 }else if (synchro->type == SIMIX_SYNC_COMMUNICATE){
614 surf_action_set_category(synchro->comm.surf_comm, category);
619 * \brief Function to get the parameters of the given the SIMIX host.
621 * \param host the host to get_phys_host (a smx_host_t)
622 * \param param the parameter object space to be overwritten (a ws_params_t)
624 void SIMIX_host_get_params(smx_host_t ind_vm, ws_params_t params)
626 /* jump to ws_get_params(). */
627 surf_workstation_get_params(ind_vm, params);
630 void SIMIX_host_set_params(smx_host_t ind_vm, ws_params_t params)
632 /* jump to ws_set_params(). */
633 surf_workstation_set_params(ind_vm, params);
636 xbt_dict_t SIMIX_host_get_mounted_storage_list(smx_host_t host){
637 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
639 return surf_workstation_get_mounted_storage_list(host);
642 xbt_dynar_t SIMIX_host_get_attached_storage_list(smx_host_t host){
643 xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
645 return surf_workstation_get_attached_storage_list(host);