Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'surf++'
[simgrid.git] / examples / java / cloud / Master.java
1 /*
2  * Copyright (c) 2012-2013. The SimGrid Team.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. 
7  */
8 package cloud;
9
10 import java.util.ArrayList;
11
12 import org.simgrid.msg.Host;
13 import org.simgrid.msg.Msg;
14 import org.simgrid.msg.MsgException;
15 import org.simgrid.msg.Process;
16 import org.simgrid.msg.Task;
17 import org.simgrid.msg.VM;
18
19 public class Master extends Process {
20         private Host[] hosts;
21         
22         public Master(Host host, String name, Host[] hosts) {
23                 super(host,name,null);
24                 this.hosts = hosts;
25         }
26         public void main(String[] args) throws MsgException {
27                 int slavesCount = Cloud.hostNB;
28                 
29                 ArrayList<VM> vms = new ArrayList<VM>();
30                 
31                 // Create one VM per host and bind a process inside each one. 
32                 for (int i = 0; i < slavesCount; i++) {
33                         Msg.info("create VM0"+i);       
34                         VM vm = new VM(hosts[i+1],"VM0"+i);
35                         vm.start();
36                         vms.add(vm);
37                         Slave slave = new Slave(vm,i);
38                         Msg.info("Put Worker "+slave.msgName()+ " on "+vm.getName());
39                         slave.start();
40         
41                 }
42                 Msg.info("Launched " + vms.size() + " VMs");
43                 
44                 Msg.info("Send a first batch of work to everyone");
45                 workBatch(slavesCount);
46                 
47                 Msg.info("Suspend all VMs");
48                 for (int i = 0; i < vms.size(); i++) {
49                         Msg.info("Suspend "+vms.get(i).getName());
50                         vms.get(i).suspend();
51                 }
52                 
53                 Msg.info("Wait a while");
54                 waitFor(2);
55                 
56                 Msg.info("Resume all VMs.");
57                 for (int i = 0; i < vms.size(); i++) {
58                         vms.get(i).resume();
59                 }
60                 
61                 Msg.info("Sleep long enough for everyone to be done with previous batch of work");
62                 waitFor(1000 - Msg.getClock());
63                 
64                 Msg.info("Add one more process per VM.");
65                 for (int i = 0; i < vms.size(); i++) {
66                         VM vm = vms.get(i);
67                         Slave slave = new Slave(vm,i + vms.size());
68                         slave.start();
69                 }
70         
71                 workBatch(slavesCount * 2);
72                 
73                 Msg.info("Migrate everyone to "+hosts[2].getName());
74                 for (int i = 0; i < vms.size(); i++) {
75                         vms.get(i).migrate(hosts[2]);
76                 }
77                 
78 //              Msg.info("Suspend everyone, move them to the third host, and resume them.");
79                 Msg.info("Migrate everyone to the third host (please note that cold migration is not yet available");
80                 
81
82                 for (int i = 0; i < vms.size(); i++) {
83                         VM vm = vms.get(i);
84         //              vm.suspend();
85                         vm.migrate(hosts[3]);
86                 //      vm.resume();
87                 }
88                 
89         
90                 
91                 Msg.info("Let's shut down the simulation and kill everyone.");
92                 
93                 for (int i = 0; i < vms.size(); i++) {
94                         vms.get(i).shutdown();
95                         vms.get(i).destroy();
96                 }                               
97                 Msg.info("Master done.");
98         }
99         
100         public void workBatch(int slavesCount) throws MsgException {
101                 for (int i = 0; i < slavesCount; i++) {
102                         Task task = new Task("Task0" + i, Cloud.task_comp_size, Cloud.task_comm_size);
103                         Msg.info("Sending to WRK0" + i);
104                         task.send("MBOX:WRK0" + i);
105                 }
106         }
107 }