Logo AND Algorithmique Numérique Distribuée

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