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 = 10;
27                 
28                 ArrayList<VM> vms = new ArrayList<VM>();
29                 
30                 for (int i = 0; i < slavesCount; i++) {
31                         Slave slave = new Slave(hosts[i],i);
32                         slave.start();
33                         VM vm = new VM(hosts[i],hosts[i]+"_"+i,1);
34                         vm.bind(slave);
35                         vms.add(vm);
36                 }
37                 Msg.info("Launched " + vms.size() + " VMs");
38                 
39                 Msg.info("Send a first batch of work to everyone");
40                 workBatch(slavesCount);
41                 
42                 Msg.info("Now suspend all VMs, just for fun");
43                 for (int i = 0; i < vms.size(); i++) {
44                         vms.get(i).suspend();
45                 }
46                 
47                 Msg.info("Wait a while");
48                 waitFor(2);
49                 
50                 Msg.info("Enough. Let's resume everybody.");
51                 for (int i = 0; i < vms.size(); i++) {
52                         vms.get(i).resume();
53                 }
54                 
55                 Msg.info("Sleep long enough for everyone to be done with previous batch of work");
56                 waitFor(1000 - Msg.getClock());
57                 
58                 Msg.info("Add one more process per VM.");
59                 for (int i = 0; i < vms.size(); i++) {
60                         VM vm = vms.get(i);
61                         Slave slave = new Slave(hosts[i],i + vms.size());
62                         slave.start();
63                         vm.bind(slave);
64                 }
65                 
66                 Msg.info("Migrate everyone to the second host.");
67                 for (int i = 0; i < vms.size(); i++) {
68                         vms.get(i).migrate(hosts[1]);
69                 }
70                 
71                 Msg.info("Suspend everyone, move them to the third host, and resume them.");
72                 for (int i = 0; i < vms.size(); i++) {
73                         VM vm = vms.get(i);
74                         vm.suspend();
75                         vm.migrate(hosts[2]);
76                         vm.resume();
77                 }
78                 
79                 workBatch(slavesCount * 2);
80                 
81                 Msg.info("Let's shut down the simulation and kill everyone.");
82                 
83                 for (int i = 0; i < vms.size(); i++) {
84                         vms.get(i).shutdown();
85                 }                               
86                 Msg.info("Master done.");
87         }
88         
89         public void workBatch(int slavesCount) throws MsgException {
90                 for (int i = 0; i < slavesCount; i++) {
91                         Task task = new Task("Task_" + i, Cloud.task_comp_size, Cloud.task_comm_size);
92                         Msg.info("Sending to " + i);
93                         task.send("slave_" + i);
94                 }
95         }
96 }