Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement natively VM_getVMByName. Java VMs are now freed
authorMartin Quinson <martin.quinson@loria.fr>
Wed, 22 Mar 2017 00:38:05 +0000 (01:38 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Wed, 22 Mar 2017 00:44:13 +0000 (01:44 +0100)
It's amusing to think that no java VM were ever freed before...

src/bindings/java/jmsg_vm.cpp
src/bindings/java/jmsg_vm.h
src/bindings/java/org/simgrid/msg/Host.java
src/bindings/java/org/simgrid/msg/VM.java

index 031238f..7a58af6 100644 (file)
@@ -165,4 +165,25 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_resume(JNIEnv *env, jobject jvm)
   MSG_vm_resume(vm);
 }
 
   MSG_vm_resume(vm);
 }
 
+JNIEXPORT jobject JNICALL Java_org_simgrid_msg_VM_getVMByName(JNIEnv* env, jclass cls, jstring jname)
+{
+
+  /* get the C string from the java string */
+  if (jname == nullptr) {
+    jxbt_throw_null(env, bprintf("No VM can have a null name"));
+    return nullptr;
+  }
+  const char* name = env->GetStringUTFChars(jname, 0);
+  /* get the VM by name   (VMs are just special hosts, unfortunately) */
+  msg_host_t host = MSG_host_by_name(name);
+
+  if (!host) { /* invalid name */
+    jxbt_throw_host_not_found(env, name);
+    env->ReleaseStringUTFChars(jname, name);
+    return nullptr;
+  }
+  env->ReleaseStringUTFChars(jname, name);
+
+  return static_cast<jobject>(host->extension(JAVA_HOST_LEVEL));
+}
 SG_END_DECL()
 SG_END_DECL()
index 70106d7..19da959 100644 (file)
@@ -30,14 +30,16 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_nativeInit(JNIEnv* env, jclass cl
 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_VM_all(JNIEnv* env, jclass cls_arg);
 
 JNIEXPORT jint JNICALL Java_org_simgrid_msg_VM_isCreated(JNIEnv* env, jobject jvm);
 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_VM_all(JNIEnv* env, jclass cls_arg);
 
 JNIEXPORT jint JNICALL Java_org_simgrid_msg_VM_isCreated(JNIEnv* env, jobject jvm);
-
 JNIEXPORT jint JNICALL Java_org_simgrid_msg_VM_isRunning(JNIEnv* env, jobject jvm);
 JNIEXPORT jint JNICALL Java_org_simgrid_msg_VM_isMigrating(JNIEnv* env, jobject jvm);
 JNIEXPORT jint JNICALL Java_org_simgrid_msg_VM_isSuspended(JNIEnv* env, jobject jvm);
 JNIEXPORT jint JNICALL Java_org_simgrid_msg_VM_isResuming(JNIEnv* env, jobject jvm);
 JNIEXPORT jint JNICALL Java_org_simgrid_msg_VM_isRunning(JNIEnv* env, jobject jvm);
 JNIEXPORT jint JNICALL Java_org_simgrid_msg_VM_isMigrating(JNIEnv* env, jobject jvm);
 JNIEXPORT jint JNICALL Java_org_simgrid_msg_VM_isSuspended(JNIEnv* env, jobject jvm);
 JNIEXPORT jint JNICALL Java_org_simgrid_msg_VM_isResuming(JNIEnv* env, jobject jvm);
+
 JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_setBound(JNIEnv* env, jobject jvm, jdouble bound);
 JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_setBound(JNIEnv* env, jobject jvm, jdouble bound);
+
 JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_create(JNIEnv* env, jobject jvm, jobject jhost, jstring jname,
                                                       jint jramsize, jint dprate, jint mig_netspeed);
 JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_create(JNIEnv* env, jobject jvm, jobject jhost, jstring jname,
                                                       jint jramsize, jint dprate, jint mig_netspeed);
+JNIEXPORT jobject JNICALL Java_org_simgrid_msg_VM_getVMByName(JNIEnv* env, jclass cls, jstring jname);
 JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_nativeFinalize(JNIEnv* env, jobject jvm);
 JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_start(JNIEnv* env, jobject jvm);
 JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_nativeMigration(JNIEnv* env, jobject jvm, jobject jhost);
 JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_nativeFinalize(JNIEnv* env, jobject jvm);
 JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_start(JNIEnv* env, jobject jvm);
 JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_nativeMigration(JNIEnv* env, jobject jvm, jobject jhost);
index bf2b589..9165a10 100644 (file)
@@ -148,7 +148,7 @@ public class Host {
        
 
        /** Class initializer, to initialize various JNI stuff */
        
 
        /** Class initializer, to initialize various JNI stuff */
-       public static native void nativeInit();
+       private static native void nativeInit();
        static {
                nativeInit();
        }       
        static {
                nativeInit();
        }       
index bfd2715..30b8852 100644 (file)
@@ -1,17 +1,15 @@
-/* JNI interface to virtual machine in Simgrid */
+/* Java bindings of the s4u::VirtualMachine */
 
 
-/* Copyright (c) 2006-2014. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 package org.simgrid.msg;
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 package org.simgrid.msg;
-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
 
 
 public class VM extends Host {
        // No need to declare a new bind variable: we use the one inherited from the super class Host
 
-       private static ArrayList<VM> vms= new ArrayList<>();
        private Host currentHost; 
 
        /** Create a `basic' VM (i.e. 1GB of RAM, other values are not taken into account). */
        private Host currentHost; 
 
        /** Create a `basic' VM (i.e. 1GB of RAM, other values are not taken into account). */
@@ -32,18 +30,13 @@ public class VM extends Host {
                super.name = name;
                this.currentHost = host; 
                create(host, name, ramSize, migNetSpeed, dpIntensity);
                super.name = name;
                this.currentHost = host; 
                create(host, name, ramSize, migNetSpeed, dpIntensity);
-               vms.add(this);
        }
 
        }
 
+       /** Retrieve the list of all existing VMs */
        public native static VM[] all();
 
        public native static VM[] all();
 
-       public static VM getVMByName(String name){
-               for (VM vm : vms){
-                       if (vm.getName().equals(name))
-                               return vm;
-               }
-               return null; 
-       }
+       /** Retrieve a VM from its name */
+       public native static VM getVMByName(String name);
        
        /** Shutdown and unref the VM. 
         * 
        
        /** Shutdown and unref the VM. 
         * 
@@ -55,14 +48,13 @@ public class VM extends Host {
         */
        public void destroy() {
                shutdown();
         */
        public void destroy() {
                shutdown();
-///            vms.remove(this);
        }
 
        /* Make sure that the GC also destroys the C object */
        protected void finalize() throws Throwable {
                nativeFinalize();
        }
        }
 
        /* Make sure that the GC also destroys the C object */
        protected void finalize() throws Throwable {
                nativeFinalize();
        }
-       public native void nativeFinalize();
+       private native void nativeFinalize();
 
        /** Returns whether the given VM is currently suspended */      
        public native int isCreated();
 
        /** Returns whether the given VM is currently suspended */      
        public native int isCreated();
@@ -134,7 +126,7 @@ public class VM extends Host {
        public native void resume();
 
        /**  Class initializer (for JNI), don't do it yourself */
        public native void resume();
 
        /**  Class initializer (for JNI), don't do it yourself */
-       public static native void nativeInit();
+       private static native void nativeInit();
        static {
                nativeInit();
        }
        static {
                nativeInit();
        }