Logo AND Algorithmique Numérique Distribuée

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