Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement VM::all() as a native, and use it in tests
[simgrid.git] / src / bindings / java / org / simgrid / msg / VM.java
index b9becc6..bfd2715 100644 (file)
@@ -11,8 +11,6 @@ import java.util.ArrayList;
 public class VM extends Host {
        // No need to declare a new bind variable: we use the one inherited from the super class Host
 
-       /* Static functions */ 
-
        private static ArrayList<VM> vms= new ArrayList<>();
        private Host currentHost; 
 
@@ -37,11 +35,7 @@ public class VM extends Host {
                vms.add(this);
        }
 
-       public static VM[] all(){
-               VM[] allvms = new VM[vms.size()];
-               vms.toArray(allvms);
-               return allvms;
-       }
+       public native static VM[] all();
 
        public static VM getVMByName(String name){
                for (VM vm : vms){
@@ -51,13 +45,24 @@ public class VM extends Host {
                return null; 
        }
        
+       /** Shutdown and unref the VM. 
+        * 
+        * Actually, this strictly equivalent to shutdown().
+        * In C and in libvirt, the destroy function also releases the memory associated to the VM, 
+        * but this is not the way it goes in Java. The VM will only get destroyed by the garbage 
+        * collector when it is not referenced anymore by your variables. So, to see the VM really 
+        * destroyed, don't call this function but simply release any ref you have on it. 
+        */
        public void destroy() {
-               nativeFinalize();
+               shutdown();
+///            vms.remove(this);
        }
-       private native void nativeFinalize();
-
 
-       /* JNI / Native code */
+       /* Make sure that the GC also destroys the C object */
+       protected void finalize() throws Throwable {
+               nativeFinalize();
+       }
+       public native void nativeFinalize();
 
        /** Returns whether the given VM is currently suspended */      
        public native int isCreated();
@@ -97,17 +102,12 @@ public class VM extends Host {
         */
        public native void shutdown();
 
-       /** Invoke native migration routine */
-       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);
-
-
-
        /** Change the host on which all processes are running
         * (pre-copy is implemented)
         */     
        public void migrate(Host destination) throws HostFailureException{
                try {
-                       this.internalmig(destination);
+                       this.nativeMigration(destination);
                } catch (Exception e){
                  Msg.info("Migration of VM "+this.getName()+" to "+destination.getName()+" is impossible ("+e.getMessage()+")");
                  throw new HostFailureException();
@@ -115,6 +115,7 @@ public class VM extends Host {
                // If the migration correcly returned, then we should change the currentHost value. 
                this.currentHost = destination; 
        }
+       private native void nativeMigration(Host destination) throws Exception;
 
        /** Immediately suspend the execution of all processes within the given VM
         *