Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[simgrid.git] / examples / java / cloud / Master.java
1 /*
2  * Copyright 2012. The SimGrid Team. 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 package cloud;
8
9 import java.util.ArrayList;
10
11 import org.simgrid.msg.Host;
12 import org.simgrid.msg.Msg;
13 import org.simgrid.msg.MsgException;
14 import org.simgrid.msg.Process;
15 import org.simgrid.msg.Task;
16 import org.simgrid.msg.VM;
17
18 public class Master extends Process {
19         private Host[] hosts;
20         
21         public Master(Host host, String name, Host[] hosts) {
22                 super(host,name,null);
23                 this.hosts = hosts;
24         }
25         public void main(String[] args) throws MsgException {
26                 int slavesCount = 5;
27                 
28                 ArrayList<VM> vms = new ArrayList<VM>();
29                 
30                 // Create one VM per host and bind a process inside each one. 
31                 for (int i = 0; i < slavesCount; i++) {
32                         VM vm = new VM(hosts[i],"VM_"+i);
33                         vm.start();
34                         vms.add(vm);
35                         Slave slave = new Slave(vm,i);
36                         slave.start();
37         
38                 }
39                 Msg.info("Launched " + vms.size() + " VMs");
40                 
41                 Msg.info("Send a first batch of work to everyone");
42                 workBatch(slavesCount);
43                 
44                 Msg.info("Now suspend all VMs, just for fun");
45                 for (int i = 0; i < vms.size(); i++) {
46                         vms.get(i).suspend();
47                 }
48                 
49                 Msg.info("Wait a while");
50                 waitFor(2);
51                 
52                 Msg.info("Enough. Let's resume everybody.");
53                 for (int i = 0; i < vms.size(); i++) {
54                         vms.get(i).resume();
55                 }
56                 
57                 Msg.info("Sleep long enough for everyone to be done with previous batch of work");
58                 waitFor(1000 - Msg.getClock());
59                 
60                 Msg.info("Add one more process per VM.");
61                 for (int i = 0; i < vms.size(); i++) {
62                         VM vm = vms.get(i);
63                         Slave slave = new Slave(vm,i + vms.size());
64                         slave.start();
65                 }
66                 
67                 Msg.info("Migrate everyone to the second host.");
68                 for (int i = 0; i < vms.size(); i++) {
69                         vms.get(i).migrate(hosts[1]);
70                 }
71                 
72 //              Msg.info("Suspend everyone, move them to the third host, and resume them.");
73                 Msg.info("Migrate everyone to the third host (please note that cold migration is not yet available");
74                 
75
76                 for (int i = 0; i < vms.size(); i++) {
77                         VM vm = vms.get(i);
78         //              vm.suspend();
79                         vm.migrate(hosts[2]);
80                 //      vm.resume();
81                 }
82                 
83                 workBatch(slavesCount * 2);
84                 
85                 Msg.info("Let's shut down the simulation and kill everyone.");
86                 
87                 for (int i = 0; i < vms.size(); i++) {
88                         vms.get(i).shutdown();
89                 }                               
90                 Msg.info("Master done.");
91         }
92         
93         public void workBatch(int slavesCount) throws MsgException {
94                 for (int i = 0; i < slavesCount; i++) {
95                         Task task = new Task("Task_" + i, Cloud.task_comp_size, Cloud.task_comm_size);
96                         Msg.info("Sending to " + i);
97                         task.send("slave_" + i);
98                 }
99         }
100 }