Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make MSG fade away (part 2)
[simgrid.git] / src / bindings / java / org / simgrid / msg / VM.java
1 /* Java bindings of the s4u::VirtualMachine */
2
3 /* Copyright (c) 2006-2017. The SimGrid Team. 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 org.simgrid.msg;
9
10 public class VM extends Host {
11         // No need to declare a new bind variable: we use the one inherited from the super class Host
12
13         private Host currentHost; 
14         private int coreAmount = 1;
15
16         /**
17          * Create a `basic' VM : 1 core and 1GB of RAM.
18          * @param host Host node
19          * @param name name of the machine
20          */     
21         public VM(Host host, String name) {
22                 this(host,name, /*coreAmount*/1, 1024, 0, 0);
23         }
24
25         /**
26          * Create a VM without useless values (for humans)
27          * @param host Host node
28          * @param name name of the machine
29          * @param coreAmount the amount of cores of the VM
30          */     
31         public VM(Host host, String name, int coreAmount) {
32                 this(host,name, coreAmount, 1024, 0, 0);
33         }
34
35         /**
36          * Create a VM with 1 core
37          * @param host Host node
38          * @param name name of the machine
39          * @param ramSize size of the RAM that should be allocated (in MBytes)
40          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
41          * @param dpIntensity (dirty page percentage according to migNetSpeed, [0-100], if you don't know put zero ;))
42          */     
43         public VM(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity){
44                 this(host, name, /*coreAmount*/1, ramSize, migNetSpeed, dpIntensity);
45         }
46
47         /**
48          * Create a VM
49          * @param host Host node
50          * @param name name of the machine
51          * @param coreAmount the amount of cores of the VM
52          * @param ramSize size of the RAM that should be allocated (in MBytes)
53          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
54          * @param dpIntensity (dirty page percentage according to migNetSpeed, [0-100], if you don't know put zero ;))
55          */
56         public VM(Host host, String name, int coreAmount, int ramSize, int migNetSpeed, int dpIntensity){
57                 super();
58                 super.name = name;
59                 this.currentHost = host; 
60                 this.coreAmount = coreAmount;
61                 create(host, name, coreAmount, ramSize, migNetSpeed, dpIntensity);
62         }
63
64         /** Retrieve the list of all existing VMs */
65         public static native VM[] all();
66
67         /** Retrieve a VM from its name */
68         public static native VM getVMByName(String name);
69         
70         /** Shutdown and unref the VM. 
71          * 
72          * Actually, this strictly equivalent to shutdown().
73          * In C and in libvirt, the destroy function also releases the memory associated to the VM, 
74          * but this is not the way it goes in Java. The VM will only get destroyed by the garbage 
75          * collector when it is not referenced anymore by your variables. So, to see the VM really 
76          * destroyed, don't call this function but simply release any ref you have on it. 
77          */
78         public void destroy() {
79                 shutdown();
80         }
81
82         /* Make sure that the GC also destroys the C object */
83         protected void finalize() throws Throwable {
84                 nativeFinalize();
85         }
86         private native void nativeFinalize();
87
88         /** Returns whether the given VM is currently suspended */      
89         public native int isCreated();
90
91         /** Returns whether the given VM is currently running */
92         public native int isRunning();
93
94         /** Returns whether the given VM is currently running */
95         public native int isMigrating();
96
97         /** Returns whether the given VM is currently suspended */      
98         public native int isSuspended();
99
100         /** Returns the amount of virtual CPUs provided */
101         public int getCoreAmount() {
102                 return coreAmount;
103         }
104         
105         /**
106          * Natively implemented method create the VM.
107          * @param ramSize size of the RAM that should be allocated (in MB)
108          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
109          * @param dpIntensity (dirty page intensity, a percentage of migNetSpeed [0-100],  if you don't know put zero ;))
110          */
111         private native void create(Host host, String name, int coreAmount, int ramSize, int migNetSpeed, int dpIntensity);
112
113
114         /**
115          * Set a CPU bound for a given VM.
116          * @param bound in flops/s
117          */
118         public native void setBound(double bound);
119
120         /**  start the VM */
121         public native void start();
122
123
124         /**
125          * Immediately kills all processes within the given VM. 
126          * 
127          * No extra delay occurs. If you want to simulate this too, you want to use a MSG_process_sleep()
128          */
129         public native void shutdown();
130
131         /** Change the host on which all processes are running
132          * (pre-copy is implemented)
133          */     
134         public void migrate(Host destination) throws HostFailureException{
135                 try {
136                         this.nativeMigration(destination);
137                 } catch (Exception e){
138                   Msg.info("Migration of VM "+this.getName()+" to "+destination.getName()+" is impossible ("+e.getMessage()+")");
139                   throw new HostFailureException();
140                 }
141                 // If the migration correcly returned, then we should change the currentHost value. 
142                 this.currentHost = destination; 
143         }
144         private native void nativeMigration(Host destination) throws MsgException;
145
146         /** Immediately suspend the execution of all processes within the given VM
147          *
148          * No suspension cost occurs. If you want to simulate this too, you want to
149          * use a \ref File.write() before or after, depending on the exact semantic
150          * of VM suspend to you.
151          */     
152         public native void suspend();
153
154         /** Immediately resumes the execution of all processes within the given VM
155          *
156          * No resume cost occurs. If you want to simulate this too, you want to
157          * use a \ref File.read() before or after, depending on the exact semantic
158          * of VM resume to you.
159          */
160         public native void resume();
161
162         /**  Class initializer (for JNI), don't do it yourself */
163         private static native void nativeInit();
164         static {
165                 nativeInit();
166         }
167 }