Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / examples / java / energy / vm / EnergyVMRunner.java
1 /* Copyright (c) 2016. The SimGrid Team. All rights reserved.               */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 package energy.vm;
7
8 import org.simgrid.msg.Msg;
9 import org.simgrid.msg.VM;
10 import org.simgrid.msg.Host;
11 import org.simgrid.msg.Task;
12 import org.simgrid.msg.Process;
13 import org.simgrid.msg.MsgException;
14 import org.simgrid.msg.HostFailureException;
15 import org.simgrid.msg.HostNotFoundException;
16 import org.simgrid.msg.TaskCancelledException;
17 import org.simgrid.msg.NativeException;
18
19 /* This class is a process in charge of running the test. It creates and starts the VMs, and fork processes within VMs */
20 public class EnergyVMRunner extends Process {
21
22   public class DummyProcess extends Process {
23     public  DummyProcess (Host host, String name) {
24       super(host, name); 
25     }
26
27     @Override
28     public void main(String[] strings) {
29       Task  task = new Task(this.getHost().getName()+"-task", 300E6 , 0);
30       try {
31         task.execute();
32       } catch (HostFailureException | TaskCancelledException e) {
33         e.printStackTrace();
34       } 
35       Msg.info("This worker is done."); 
36     }
37   }
38
39   EnergyVMRunner(Host host, String name, String[] args) throws HostNotFoundException, NativeException  {
40     super(host, name, args);
41   }
42
43   @Override
44   public void main(String[] strings) throws MsgException, HostNotFoundException {
45     double startTime = 0;
46     double endTime = 0;
47
48     /* get hosts */
49     Host host1 = Host.getByName("MyHost1");
50     Host host2 = Host.getByName("MyHost2");
51     Host host3 = Host.getByName("MyHost3");
52
53     Msg.info("Creating and starting two VMs");
54     VM vmHost1 = new VM(host1, "vmHost1", 4, 2048, 100, null, 1024 * 20, 10,50);
55     vmHost1.start();
56
57     VM vmHost3 = new VM(host3, "vmHost3", 4, 2048, 100, null, 1024 * 20, 10,50);
58     vmHost3.start();
59
60     Msg.info("Create two tasks on Host1: one inside a VM, the other directly on the host");
61     new DummyProcess (vmHost1, "p11"); 
62     new DummyProcess (host1, "p12"); 
63
64     Msg.info("Create two tasks on Host2: both directly on the host");
65     new DummyProcess (host2, "p21"); 
66     new DummyProcess (host2, "p22"); 
67
68     Msg.info("Create two tasks on Host3: both inside a VM");
69     new DummyProcess (vmHost3, "p31"); 
70     new DummyProcess (vmHost3, "p312"); 
71
72     Msg.info("Wait 5 seconds. The tasks are still running (they run for 3 seconds, but 2 tasks are co-located, "
73              + "so they run for 6 seconds)"); 
74     waitFor(5); 
75     Msg.info("Wait another 5 seconds. The tasks stop at some point in between"); 
76     waitFor(5); 
77
78     vmHost1.shutdown(); 
79     vmHost3.shutdown(); 
80     vmHost1.finalize(); 
81     vmHost3.finalize(); 
82   }
83 }