Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use XBT_INFO only after MSG_init, so that log parameters given on the command line...
[simgrid.git] / examples / msg / cloud / masterslave_virtual_machines.c
index dd77241..eb9d072 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (c) 2007-2012. The SimGrid Team. All rights reserved.                             */
+/* Copyright (c) 2007-2014. The SimGrid Team.
+ * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -19,13 +20,14 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
  *    example on a cloud</b>. The classical example revisited to demonstrate the use of virtual machines.
  */
 
-double task_comp_size = 10000000;
-double task_comm_size = 10000000;
+const double task_comp_size = 10000000;
+const double task_comm_size = 10000000;
 
 
 int master_fun(int argc, char *argv[]);
 int worker_fun(int argc, char *argv[]);
 
+
 static void work_batch(int workers_count)
 {
   int i;
@@ -52,14 +54,14 @@ int master_fun(int argc, char *argv[])
   msg_host_t *pms = xbt_new(msg_host_t, workers_count);
   xbt_dynar_t vms = xbt_dynar_new(sizeof(msg_vm_t), NULL);
 
-  /* Retrive the PMs that launch worker processes. */
+  /* Retrieve the PMs that will launch worker processes. */
   for (i = 1; i < argc; i++)
     pms[i - 1] = MSG_get_host_by_name(argv[i]);
 
 
   /* Launch VMs and worker processes. One VM per PM, and one worker process per VM. */
 
-  XBT_INFO("Launch %ld VMs", workers_count);
+  XBT_INFO("Launch %d VMs", workers_count);
   for (i=0; i< workers_count; i++) {
     char *vm_name = bprintf("VM%02d", i);
     char *pr_name = bprintf("WRK%02d", i);
@@ -72,11 +74,18 @@ int master_fun(int argc, char *argv[])
 
     XBT_INFO("create %s", vm_name);
     msg_vm_t vm = MSG_vm_create_core(pms[i], vm_name);
+
+    s_ws_params_t params;
+    memset(&params, 0, sizeof(params));
+    params.ramsize = 1L * 1024 * 1024 * 1024; // 1Gbytes
+    MSG_host_set_params(vm, &params);
+
     MSG_vm_start(vm);
     xbt_dynar_push(vms, &vm);
 
     XBT_INFO("put %s on %s", pr_name, vm_name);
     MSG_process_create_with_arguments(pr_name, worker_fun, NULL, vm, 2, wrk_argv);
+    xbt_free(vm_name);
   }
 
 
@@ -117,6 +126,7 @@ int master_fun(int argc, char *argv[])
 
     XBT_INFO("put %s on %s", pr_name, vm_name);
     MSG_process_create_with_arguments(pr_name, worker_fun, NULL, vm, 2, wrk_argv);
+    xbt_free(vm_name);
   }
 
   XBT_INFO("Send a task to %d worker process", workers_count * 2);
@@ -127,8 +137,8 @@ int master_fun(int argc, char *argv[])
     MSG_vm_migrate(vm, pms[1]);
   }
 
-  /* FIXME: Do we need to support cold migration? Yes, but how should
-   * parameters of a migration be passed? */
+  /* Migration with default policy is called (i.e. live migration with pre-copy strategy) */
+  /* If you want to use other policy such as post-copy or cold migration, you should add a third parameter that defines the policy */
   XBT_INFO("Migrate all VMs to PM(%s)", MSG_host_get_name(pms[2]));
   xbt_dynar_foreach(vms, i, vm) {
     // MSG_vm_suspend(vm);
@@ -137,7 +147,7 @@ int master_fun(int argc, char *argv[])
   }
 
 
-  XBT_INFO("Shutdown the first 10 worker processes gracefuly. The   the second half will forcefully get killed");
+  XBT_INFO("Shutdown the half of worker processes gracefuly. The remaining half will be forcibly killed");
   for (i = 0; i < workers_count; i++) {
     char mbox[64];
     sprintf(mbox, "MBOX:WRK%02d", i);
@@ -198,9 +208,12 @@ int worker_fun(int argc, char *argv[])
   return 0;
 }
 
+
 /** Main function */
 int main(int argc, char *argv[])
 {
+  const int nb_hosts = 3;
+
   MSG_init(&argc, argv);
   if (argc != 2) {
     printf("Usage: %s example/msg/msg_platform.xml\n", argv[0]);
@@ -210,15 +223,15 @@ int main(int argc, char *argv[])
   /* Load the platform file */
   MSG_create_environment(argv[1]);
 
-  /* Retrieve the 10 first hosts from the platform file */
+  /* Retrieve hosts from the platform file */
   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
 
-  if (xbt_dynar_length(hosts_dynar) <= 10) {
-    XBT_CRITICAL("need 10 hosts");
+  if (xbt_dynar_length(hosts_dynar) <= nb_hosts) {
+    XBT_CRITICAL("need %d hosts", nb_hosts);
     return 1;
   }
 
-  msg_host_t master_pm;
+  msg_host_t master_pm = NULL;
   char **master_argv = xbt_new(char *, 12);
   master_argv[0] = xbt_strdup("master");
   master_argv[11] = NULL;
@@ -233,16 +246,17 @@ int main(int argc, char *argv[])
 
     master_argv[i] = xbt_strdup(MSG_host_get_name(host));
 
-    if (i == 10)
+    if (i == nb_hosts)
       break;
   }
 
+  msg_error_t res = 1;
+  if (master_pm!=NULL){
+    MSG_process_create_with_arguments("master", master_fun, NULL, master_pm, nb_hosts + 1, master_argv);
 
-  MSG_process_create_with_arguments("master", master_fun, NULL, master_pm, 11, master_argv);
-
-  msg_error_t res = MSG_main();
-  XBT_INFO("Simulation time %g", MSG_get_clock());
-
+    res = MSG_main();
+    XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
+  }
   xbt_dynar_free(&hosts_dynar);
 
   return !(res == MSG_OK);