Logo AND Algorithmique Numérique Distribuée

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