Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' 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 = Cloud.hostNB;
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                         Msg.info("create VM0"+i);       
33                         VM vm = new VM(hosts[i+1],"VM0"+i);
34                         vm.start();
35                         vms.add(vm);
36                         Slave slave = new Slave(vm,i);
37                         Msg.info("Put Worker "+slave.msgName()+ " on "+vm.getName());
38                         slave.start();
39         
40                 }
41                 Msg.info("Launched " + vms.size() + " VMs");
42                 
43                 Msg.info("Send a first batch of work to everyone");
44                 workBatch(slavesCount);
45                 
46                 Msg.info("Suspend all VMs");
47                 for (int i = 0; i < vms.size(); i++) {
48                         Msg.info("Suspend "+vms.get(i).getName());
49                         vms.get(i).suspend();
50                 }
51                 
52                 Msg.info("Wait a while");
53                 waitFor(2);
54                 
55                 Msg.info("Resume all VMs.");
56                 for (int i = 0; i < vms.size(); i++) {
57                         vms.get(i).resume();
58                 }
59                 
60                 Msg.info("Sleep long enough for everyone to be done with previous batch of work");
61                 waitFor(1000 - Msg.getClock());
62                 
63                 Msg.info("Add one more process per VM.");
64                 for (int i = 0; i < vms.size(); i++) {
65                         VM vm = vms.get(i);
66                         Slave slave = new Slave(vm,i + vms.size());
67                         slave.start();
68                 }
69         
70                 workBatch(slavesCount * 2);
71                 
72                 Msg.info("Migrate everyone to "+hosts[2].getName());
73                 for (int i = 0; i < vms.size(); i++) {
74                         vms.get(i).migrate(hosts[2]);
75                 }
76                 
77 //              Msg.info("Suspend everyone, move them to the third host, and resume them.");
78                 Msg.info("Migrate everyone to the third host (please note that cold migration is not yet available");
79                 
80
81                 for (int i = 0; i < vms.size(); i++) {
82                         VM vm = vms.get(i);
83         //              vm.suspend();
84                         vm.migrate(hosts[3]);
85                 //      vm.resume();
86                 }
87                 
88         
89                 
90                 Msg.info("Let's shut down the simulation and kill everyone.");
91                 
92                 for (int i = 0; i < vms.size(); i++) {
93                         vms.get(i).shutdown();
94                         vms.get(i).destroy();
95                 }                               
96                 Msg.info("Master done.");
97         }
98         
99         public void workBatch(int slavesCount) throws MsgException {
100                 for (int i = 0; i < slavesCount; i++) {
101                         Task task = new Task("Task0" + i, Cloud.task_comp_size, Cloud.task_comm_size);
102                         Msg.info("Sending to WRK0" + i);
103                         task.send("MBOX:WRK0" + i);
104                 }
105         }
106 }