Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mv example from cloud to energy
[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.*;
9 import org.simgrid.msg.Process;
10
11 /* This class is a process in charge of running the test. It creates and starts the VMs, and fork processes within VMs */
12 public class EnergyVMRunner extends Process {
13
14   public class DummyProcess extends Process {
15     public  DummyProcess (Host host, String name) {
16       super(host, name); 
17     }
18
19     public void main(String[] args) {    
20       Task  task = new Task(this.getHost().getName()+"-task", 300E6 , 0);
21       try {
22         task.execute();   
23       } catch (Exception e) {
24         e.printStackTrace();
25       } 
26       Msg.info("This worker is done."); 
27     }
28   }
29
30   EnergyVMRunner(Host host, String name, String[] args) throws HostNotFoundException, NativeException  {
31     super(host, name, args);
32   }
33
34   public void main(String[] strings) throws MsgException, HostNotFoundException {
35     double startTime = 0;
36     double endTime = 0;
37
38     /* get hosts */
39     Host host1 = Host.getByName("MyHost1");
40     Host host2 = Host.getByName("MyHost2");
41     Host host3 = Host.getByName("MyHost3");
42
43     Msg.info("Creating and starting two VMs");
44     VM vmHost1 = new VM(host1, "vmHost1", 4, 2048, 100, null, 1024 * 20, 10,50);
45     vmHost1.start();
46
47     VM vmHost3 = new VM(host3, "vmHost3", 4, 2048, 100, null, 1024 * 20, 10,50);
48     vmHost3.start();
49
50     Msg.info("Create two tasks on Host1: one inside a VM, the other directly on the host");
51     new DummyProcess (vmHost1, "p11"); 
52     new DummyProcess (host1, "p12"); 
53
54     Msg.info("Create two tasks on Host2: both directly on the host");
55     new DummyProcess (host2, "p21"); 
56     new DummyProcess (host2, "p22"); 
57
58     Msg.info("Create two tasks on Host3: both inside a VM");
59     new DummyProcess (vmHost3, "p31"); 
60     new DummyProcess (vmHost3, "p312"); 
61
62     Msg.info("Wait 5 seconds. The tasks are still running (they run for 3 seconds, but 2 tasks are co-located, "
63              + "so they run for 6 seconds)"); 
64     waitFor(5); 
65     Msg.info("Wait another 5 seconds. The tasks stop at some point in between"); 
66     waitFor(5); 
67
68     vmHost1.shutdown(); 
69     vmHost3.shutdown(); 
70     vmHost1.finalize(); 
71     vmHost3.finalize(); 
72   }
73 }