Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / bindings / java / org / simgrid / msg / VM.java
1 /* JNI interface to virtual machine in Simgrid */
2
3 /* Copyright (c) 2006-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 package org.simgrid.msg;
10
11 import org.simgrid.msg.Host;
12 import org.simgrid.msg.Process;
13
14 public class VM extends Host{
15         // Please note that we are not declaring a new bind variable 
16         //(the bind variable has been inherited from the super class Host)
17         
18         /* Static functions */ 
19         // GetByName is inherited from the super class Host
20         
21
22          private static VM[] vms=null;    
23     private Host currentHost; 
24
25         /* Constructors / destructors */
26     /**
27          * Create a `basic' VM (i.e. 1 core, 1GB of RAM, other values are not taken into account).
28          */
29         public VM(Host host, String name) {
30                 this(host,name,1,1024, -1, null, -1,0 , 0);
31         }
32
33         /**
34          * Create a  VM
35          * @param host  Host node
36          * @param name name of the machine
37          * @param nCore number of core
38          * @param ramSize size of the RAM that should be allocated (in MBytes)
39          * @param netCap (not used for the moment)
40          * @param diskPath (not used for the moment)
41          * @param diskSize (not used for the moment)
42          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
43          * @param dpIntensity (dirty page percentage according to migNetSpeed, [0-100], if you don't know put zero ;))
44          */
45         public VM(Host host, String name, int nCore,  int ramSize, 
46                         int netCap, String diskPath, int diskSize, int migNetSpeed, int dpIntensity){
47                 super();
48                 super.name = name; 
49                 this.currentHost = host; 
50                 create(host, name, nCore, ramSize, netCap, diskPath, diskSize, migNetSpeed, dpIntensity);
51                 VM.addVM(this);
52         }
53
54         private static void addVM(VM vm){
55                 VM[] vmsN=null; 
56                 int i=0;
57                 if(VM.vms == null)
58                         vmsN = new VM[1]; 
59                 else
60                         vmsN = new VM[vms.length+1]; 
61                 
62                 for (i=0; i<vmsN.length-1 ; i ++){
63                         vmsN[i]=vms[i]; 
64                 } 
65                 vmsN[i]=vm;
66                 vms=vmsN;
67         }
68    public static VM[] all(){
69                 return vms;
70         }
71         public static VM getVMByName(String name){
72                 for (int i=0 ; i < vms.length ; i++){
73                           if (vms[i].getName().equals(name))
74                                         return vms[i];          
75                 }
76                 return null; 
77         }
78         protected void finalize() {
79                 destroy();
80         }
81         
82
83         /* JNI / Native code */
84
85         /* get/set property methods are inherited from the Host class. */
86         
87         /** Returns whether the given VM is currently suspended
88          */     
89         public native int isCreated();
90         
91         /** Returns whether the given VM is currently running
92          */
93         public native int isRunning();
94
95         /** Returns whether the given VM is currently running
96          */
97         public native int isMigrating();
98         
99         /** Returns whether the given VM is currently suspended
100          */     
101         public native int isSuspended();
102                 
103         /** Returns whether the given VM is currently saving
104          */
105         public native int isSaving();
106         
107         /** Returns whether the given VM is currently saved
108          */
109         public native int isSaved();
110
111         /** Returns whether the given VM is currently restoring its state
112          */
113         public native boolean isRestoring();
114         
115         /**
116          * Natively implemented method create the VM.
117          * @param nCore number of core
118          * @param ramSize size of the RAM that should be allocated (in MB)
119          * @param netCap (not used for the moment)
120          * @param diskPath (not used for the moment)
121          * @param diskSize (not used for the moment)
122          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
123          * @param dpIntensity (dirty page intensity, a percentage of migNetSpeed [0-100],  if you don't know put zero ;))
124          */
125         private native void create(Host host, String name, int nCore, int ramSize, 
126                          int netCap, String diskPath, int diskSize, int migNetSpeed, int dpIntensity);
127
128
129         /**
130          * Bound the VM to a certain % of its vcpu capability (e.g. 75% of vm.getSpeed())
131          * @param load percentage (between [0,100]
132          */
133         public native void setBound(int load);
134
135         /**
136          * start the VM
137          */
138         public native void start();
139
140         
141         /**
142          * Immediately kills all processes within the given VM. Any memory that they allocated will be leaked.
143          * No extra delay occurs. If you want to simulate this too, you want to use a MSG_process_sleep() or something
144          */
145         public native void shutdown();
146         
147         /**  
148          * Invoke native migration routine
149         */
150         public native void internalmig(Host destination) throws Exception; // TODO add throws DoubleMigrationException (i.e. when you call migrate on a VM that is already migrating);
151
152
153         
154         /** Change the host on which all processes are running
155          * (pre-copy is implemented)
156          */     
157         public void migrate(Host destination) throws HostFailureException{
158                 try {
159                         Msg.info("Migrate begins");
160                         this.internalmig(destination);
161                         Msg.info("Migrate ends");
162                 } catch (Exception e){
163                   Msg.info("an exception occurs during the migration of VM "+this.getName());
164                   throw new HostFailureException();
165                 }
166                 // If the migration correcly returned, then we should change the currentHost value. 
167                 this.currentHost = destination; 
168         }
169         
170         /** Immediately suspend the execution of all processes within the given VM
171          *
172          * No suspension cost occurs. If you want to simulate this too, you want to
173          * use a \ref File.write() before or after, depending on the exact semantic
174          * of VM suspend to you.
175          */     
176         public native void suspend();
177         
178         /** Immediately resumes the execution of all processes within the given VM
179          *
180          * No resume cost occurs. If you want to simulate this too, you want to
181          * use a \ref File.read() before or after, depending on the exact semantic
182          * of VM resume to you.
183          */
184         public native void resume();
185         
186         /** Immediately suspend the execution of all processes within the given VM 
187          *  and save its state on the persistent HDD
188          *  Not yet implemented (for the moment it behaves like suspend)
189          *  No suspension cost occurs. If you want to simulate this too, you want to
190          *  use a \ref File.write() before or after, depending on the exact semantic
191          *  of VM suspend to you.
192          */     
193         public native void save();
194         
195         /** Immediately resumes the execution of all processes previously saved 
196          * within the given VM
197          *  Not yet implemented (for the moment it behaves like resume)
198          *
199          * No resume cost occurs. If you want to simulate this too, you want to
200          * use a \ref File.read() before or after, depending on the exact semantic
201          * of VM resume to you.
202          */
203         public native void restore();
204         
205
206         /**
207          * Destroy the VM
208          */
209         public native void destroy();
210
211         
212
213         /**
214          * Class initializer, to initialize various JNI stuff
215          */
216         public static native void nativeInit();
217         static {
218                 nativeInit();
219         }
220 }