Logo AND Algorithmique Numérique Distribuée

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