Logo AND Algorithmique Numérique Distribuée

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