From a05f9acab41f24bba6080918cf129810cfba1f99 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Wed, 23 Aug 2006 17:11:09 +0000 Subject: [PATCH] performance improvement when fetching memory (throwing pointer to array instead of creating new one) --- tools/cooja/config/code_main_template | 10 +- tools/cooja/java/se/sics/cooja/CoreComm.java | 122 ++++++++++-------- .../cooja/contikimote/ContikiMoteType.java | 22 ++-- .../java/se/sics/cooja/corecomm/Lib1.java | 4 +- .../java/se/sics/cooja/corecomm/Lib2.java | 4 +- .../java/se/sics/cooja/corecomm/Lib3.java | 4 +- .../java/se/sics/cooja/corecomm/Lib4.java | 4 +- .../java/se/sics/cooja/corecomm/Lib5.java | 4 +- .../java/se/sics/cooja/corecomm/Lib6.java | 4 +- .../java/se/sics/cooja/corecomm/Lib7.java | 4 +- .../java/se/sics/cooja/corecomm/Lib8.java | 4 +- 11 files changed, 100 insertions(+), 86 deletions(-) diff --git a/tools/cooja/config/code_main_template b/tools/cooja/config/code_main_template index 74a738945..f30d117c5 100644 --- a/tools/cooja/config/code_main_template +++ b/tools/cooja/config/code_main_template @@ -119,13 +119,13 @@ Java_se_sics_cooja_corecomm_[CLASS_NAME]_init(JNIEnv *env, jobject obj) * This is a JNI function and should only be called via the * responsible Java part (MoteType.java). */ -JNIEXPORT jbyteArray JNICALL -Java_se_sics_cooja_corecomm_[CLASS_NAME]_getMemory(JNIEnv *env, jobject obj, jint start, jint length) +JNIEXPORT void JNICALL +Java_se_sics_cooja_corecomm_[CLASS_NAME]_getMemory(JNIEnv *env, jobject obj, jint start, jint length, jbyteArray mem_arr) { - jbyteArray ret=(*env)->NewByteArray(env, length); - (*env)->SetByteArrayRegion(env, ret, 0, (size_t) length, (jbyte *) start); + // jbyteArray ret=(*env)->NewByteArray(env, length); + (*env)->SetByteArrayRegion(env, mem_arr, 0, (size_t) length, (jbyte *) start); - return (ret); +// return (ret); } /*---------------------------------------------------------------------------*/ /** diff --git a/tools/cooja/java/se/sics/cooja/CoreComm.java b/tools/cooja/java/se/sics/cooja/CoreComm.java index 2b82cc0c7..377d7c70a 100644 --- a/tools/cooja/java/se/sics/cooja/CoreComm.java +++ b/tools/cooja/java/se/sics/cooja/CoreComm.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: CoreComm.java,v 1.1 2006/08/21 12:12:56 fros4943 Exp $ + * $Id: CoreComm.java,v 1.2 2006/08/23 17:11:09 fros4943 Exp $ */ package se.sics.cooja; @@ -36,18 +36,21 @@ import se.sics.cooja.corecomm.*; /** * The package corecomm's purpose is communicating with the simulation core - * using Java Native Interface (JNI). Each implementing class (named Lib[1-MAX]), - * loads a shared library which belongs to one mote type. The reason for this - * somewhat strange design is that once loaded, a native library cannot be unloaded - * in Java (yet). Therefore if we wish to load several libraries, the names and associated - * native functions must have unique names. And those names are defined via the calling class in JNI. - * For example, the native tick function in class Lib1 is named contiki_javasim_corecomm_Lib1_tick. - * When creating a new mote type, the main contiki source file is generated with function names - * compatible with the next available corecomm. - * This also implies that even if a mote type is deleted, a new one cannot be created without - * restarting the JVM and thus the entire simulation. - * - * Each implemented CoreComm class needs read access to the following core variables: + * using Java Native Interface (JNI). Each implementing class (named + * Lib[1-MAX]), loads a shared library which belongs to one mote type. The + * reason for this somewhat strange design is that once loaded, a native library + * cannot be unloaded in Java (yet). Therefore if we wish to load several + * libraries, the names and associated native functions must have unique names. + * And those names are defined via the calling class in JNI. For example, the + * native tick function in class Lib1 is named + * contiki_javasim_corecomm_Lib1_tick. When creating a new mote type, the main + * contiki source file is generated with function names compatible with the next + * available corecomm. This also implies that even if a mote type is deleted, a + * new one cannot be created without restarting the JVM and thus the entire + * simulation. + * + * Each implemented CoreComm class needs read access to the following core + * variables: * @@ -59,7 +62,7 @@ import se.sics.cooja.corecomm.*; *
  • getMemory(int start, int length) *
  • setMemory(int start, int length, byte[] mem) * - + * * @author Fredrik Osterlind */ public abstract class CoreComm { @@ -71,41 +74,42 @@ public abstract class CoreComm { // Static pointers to current libraries private final static CoreComm[] coreComms = new CoreComm[MAX_LIBRARIES]; private final static File[] coreCommFiles = new File[MAX_LIBRARIES]; - + /** - * Has any library been loaded? Since libraries can't be unloaded - * the entire simulator may have to be restarted. - * + * Has any library been loaded? Since libraries can't be unloaded the entire + * simulator may have to be restarted. + * * @return True if any library has been loaded this session */ public static boolean hasLibraryBeenLoaded() { - for (int i=0; i < coreComms.length; i++) + for (int i = 0; i < coreComms.length; i++) if (coreComms[i] != null) - return true; + return true; return false; } /** - * Has given library file already been loaded during this session? - * A loaded library can be removed, but not unloaded - * during one session. And a new library file, named - * the same as an earlier loaded and removed file, - * can't be loaded either. - * - * @param libraryFile Library file - * @return True if a library has already been loaded from the given file's filename + * Has given library file already been loaded during this session? A loaded + * library can be removed, but not unloaded during one session. And a new + * library file, named the same as an earlier loaded and removed file, can't + * be loaded either. + * + * @param libraryFile + * Library file + * @return True if a library has already been loaded from the given file's + * filename */ public static boolean hasLibraryFileBeenLoaded(File libraryFile) { - for (File libFile: coreCommFiles) + for (File libFile : coreCommFiles) if (libFile != null && libFile.getName().equals(libraryFile.getName())) return true; return false; } /** - * Get the class name of next free core communicator class. - * If null is returned, no classes are available. - * + * Get the class name of next free core communicator class. If null is + * returned, no classes are available. + * * @return Class name */ public static String getAvailableClassName() { @@ -130,11 +134,13 @@ public abstract class CoreComm { } /** - * Create and return an instance of the core communicator identified - * by className. This core communicator will load the native library libFile. - * - * @param className Class name of core communicator - * @param libFile Native library file + * Create and return an instance of the core communicator identified by + * className. This core communicator will load the native library libFile. + * + * @param className + * Class name of core communicator + * @param libFile + * Native library file * @return Core Communicator */ public static CoreComm createCoreComm(String className, File libFile) { @@ -182,43 +188,47 @@ public abstract class CoreComm { return null; } - /** - * Ticks a mote once. This should not be used directly, - * but instead via Mote.tick(). + * Ticks a mote once. This should not be used directly, but instead via + * Mote.tick(). */ public abstract void tick(); /** - * Initializes a mote by running a startup script in the core. - * (Should only by run once, at the same time as the library is loaded) + * Initializes a mote by running a startup script in the core. (Should only by + * run once, at the same time as the library is loaded) */ protected abstract void init(); /** - * Returns absolute memory location of the core variable - * referenceVar. Used to get offset between relative and absolute - * memory addresses. - * + * Returns absolute memory location of the core variable referenceVar. Used to + * get offset between relative and absolute memory addresses. + * * @return Absolute memory address */ public abstract int getReferenceAbsAddr(); /** - * Returns a memory segment identified by start and length. - * - * @param start Start address of segment - * @param length Length of segment - * @return Memory segment + * Fills an byte array with memory segment identified by start and length. + * + * @param start + * Start address of segment + * @param length + * Length of segment + * @param mem + * Array to fill with memory segment */ - public abstract byte[] getMemory(int start, int length); + public abstract void getMemory(int start, int length, byte[] mem); /** * Overwrites a memory segment identified by start and length. - * - * @param start Start address of segment - * @param length Length of segment - * @param mem Data to fill memory segment + * + * @param start + * Start address of segment + * @param length + * Length of segment + * @param mem + * New memory segment data */ public abstract void setMemory(int start, int length, byte[] mem); diff --git a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java index 74798d798..fd72b4f5a 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ContikiMoteType.java,v 1.1 2006/08/21 12:13:09 fros4943 Exp $ + * $Id: ContikiMoteType.java,v 1.2 2006/08/23 17:11:59 fros4943 Exp $ */ package se.sics.cooja.contikimote; @@ -239,10 +239,12 @@ public class ContikiMoteType implements MoteType { } // Create initial memory - byte[] initialDataSection = getCoreMemory(relDataSectionAddr - + offsetRelToAbs, dataSectionSize); - byte[] initialBssSection = getCoreMemory( - relBssSectionAddr + offsetRelToAbs, bssSectionSize); + byte[] initialDataSection = new byte[dataSectionSize]; + getCoreMemory(relDataSectionAddr + + offsetRelToAbs, dataSectionSize, initialDataSection); + byte[] initialBssSection = new byte[bssSectionSize]; + getCoreMemory( + relBssSectionAddr + offsetRelToAbs, bssSectionSize, initialBssSection); initialMemory = new SectionMoteMemory(varAddresses); initialMemory.setMemorySegment(relDataSectionAddr, initialDataSection); initialMemory.setMemorySegment(relBssSectionAddr, initialBssSection); @@ -294,8 +296,10 @@ public class ContikiMoteType implements MoteType { for (int i = 0; i < mem.getNumberOfSections(); i++) { int startAddr = mem.getStartAddrOfSection(i); int size = mem.getSizeOfSection(i); - mem.setMemorySegment(startAddr, getCoreMemory(startAddr + offsetRelToAbs, - size)); + byte[] data = mem.getDataOfSection(i); + + getCoreMemory(startAddr + offsetRelToAbs, + size, data); } } @@ -344,8 +348,8 @@ public class ContikiMoteType implements MoteType { return myCoreComm.getReferenceAbsAddr(); } - private byte[] getCoreMemory(int start, int length) { - return myCoreComm.getMemory(start, length); + private void getCoreMemory(int start, int length, byte[] data) { + myCoreComm.getMemory(start, length, data); } private void setCoreMemory(int start, int length, byte[] mem) { diff --git a/tools/cooja/java/se/sics/cooja/corecomm/Lib1.java b/tools/cooja/java/se/sics/cooja/corecomm/Lib1.java index 53e1cad79..f8028ba4f 100644 --- a/tools/cooja/java/se/sics/cooja/corecomm/Lib1.java +++ b/tools/cooja/java/se/sics/cooja/corecomm/Lib1.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Lib1.java,v 1.1 2006/08/21 12:12:57 fros4943 Exp $ + * $Id: Lib1.java,v 1.2 2006/08/23 17:13:03 fros4943 Exp $ */ package se.sics.cooja.corecomm; @@ -54,7 +54,7 @@ public class Lib1 extends CoreComm { public native void tick(); public native void init(); public native int getReferenceAbsAddr(); - public native byte[] getMemory(int start, int length); + public native void getMemory(int start, int length, byte[] mem); public native void setMemory(int start, int length, byte[] mem); } diff --git a/tools/cooja/java/se/sics/cooja/corecomm/Lib2.java b/tools/cooja/java/se/sics/cooja/corecomm/Lib2.java index adee132f5..725badd88 100644 --- a/tools/cooja/java/se/sics/cooja/corecomm/Lib2.java +++ b/tools/cooja/java/se/sics/cooja/corecomm/Lib2.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Lib2.java,v 1.1 2006/08/21 12:12:57 fros4943 Exp $ + * $Id: Lib2.java,v 1.2 2006/08/23 17:13:03 fros4943 Exp $ */ package se.sics.cooja.corecomm; @@ -54,7 +54,7 @@ public class Lib2 extends CoreComm { public native void tick(); public native void init(); public native int getReferenceAbsAddr(); - public native byte[] getMemory(int start, int length); + public native void getMemory(int start, int length, byte[] mem); public native void setMemory(int start, int length, byte[] mem); } diff --git a/tools/cooja/java/se/sics/cooja/corecomm/Lib3.java b/tools/cooja/java/se/sics/cooja/corecomm/Lib3.java index 71b74342f..e9fe850e6 100644 --- a/tools/cooja/java/se/sics/cooja/corecomm/Lib3.java +++ b/tools/cooja/java/se/sics/cooja/corecomm/Lib3.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Lib3.java,v 1.1 2006/08/21 12:12:57 fros4943 Exp $ + * $Id: Lib3.java,v 1.2 2006/08/23 17:13:03 fros4943 Exp $ */ package se.sics.cooja.corecomm; @@ -54,7 +54,7 @@ public class Lib3 extends CoreComm { public native void tick(); public native void init(); public native int getReferenceAbsAddr(); - public native byte[] getMemory(int start, int length); + public native void getMemory(int start, int length, byte[] mem); public native void setMemory(int start, int length, byte[] mem); } diff --git a/tools/cooja/java/se/sics/cooja/corecomm/Lib4.java b/tools/cooja/java/se/sics/cooja/corecomm/Lib4.java index 2e45617b3..77713f5da 100644 --- a/tools/cooja/java/se/sics/cooja/corecomm/Lib4.java +++ b/tools/cooja/java/se/sics/cooja/corecomm/Lib4.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Lib4.java,v 1.1 2006/08/21 12:12:57 fros4943 Exp $ + * $Id: Lib4.java,v 1.2 2006/08/23 17:13:03 fros4943 Exp $ */ package se.sics.cooja.corecomm; @@ -54,7 +54,7 @@ public class Lib4 extends CoreComm { public native void tick(); public native void init(); public native int getReferenceAbsAddr(); - public native byte[] getMemory(int start, int length); + public native void getMemory(int start, int length, byte[] mem); public native void setMemory(int start, int length, byte[] mem); } diff --git a/tools/cooja/java/se/sics/cooja/corecomm/Lib5.java b/tools/cooja/java/se/sics/cooja/corecomm/Lib5.java index 7b10f0cf2..b1f3e3ead 100644 --- a/tools/cooja/java/se/sics/cooja/corecomm/Lib5.java +++ b/tools/cooja/java/se/sics/cooja/corecomm/Lib5.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Lib5.java,v 1.1 2006/08/21 12:12:57 fros4943 Exp $ + * $Id: Lib5.java,v 1.2 2006/08/23 17:13:03 fros4943 Exp $ */ package se.sics.cooja.corecomm; @@ -54,7 +54,7 @@ public class Lib5 extends CoreComm { public native void tick(); public native void init(); public native int getReferenceAbsAddr(); - public native byte[] getMemory(int start, int length); + public native void getMemory(int start, int length, byte[] mem); public native void setMemory(int start, int length, byte[] mem); } diff --git a/tools/cooja/java/se/sics/cooja/corecomm/Lib6.java b/tools/cooja/java/se/sics/cooja/corecomm/Lib6.java index db0d160d3..6b38cb5ae 100644 --- a/tools/cooja/java/se/sics/cooja/corecomm/Lib6.java +++ b/tools/cooja/java/se/sics/cooja/corecomm/Lib6.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Lib6.java,v 1.1 2006/08/21 12:12:58 fros4943 Exp $ + * $Id: Lib6.java,v 1.2 2006/08/23 17:13:03 fros4943 Exp $ */ package se.sics.cooja.corecomm; @@ -54,7 +54,7 @@ public class Lib6 extends CoreComm { public native void tick(); public native void init(); public native int getReferenceAbsAddr(); - public native byte[] getMemory(int start, int length); + public native void getMemory(int start, int length, byte[] mem); public native void setMemory(int start, int length, byte[] mem); } diff --git a/tools/cooja/java/se/sics/cooja/corecomm/Lib7.java b/tools/cooja/java/se/sics/cooja/corecomm/Lib7.java index 6e2815708..996e8998f 100644 --- a/tools/cooja/java/se/sics/cooja/corecomm/Lib7.java +++ b/tools/cooja/java/se/sics/cooja/corecomm/Lib7.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Lib7.java,v 1.1 2006/08/21 12:12:58 fros4943 Exp $ + * $Id: Lib7.java,v 1.2 2006/08/23 17:13:03 fros4943 Exp $ */ package se.sics.cooja.corecomm; @@ -54,7 +54,7 @@ public class Lib7 extends CoreComm { public native void tick(); public native void init(); public native int getReferenceAbsAddr(); - public native byte[] getMemory(int start, int length); + public native void getMemory(int start, int length, byte[] mem); public native void setMemory(int start, int length, byte[] mem); } diff --git a/tools/cooja/java/se/sics/cooja/corecomm/Lib8.java b/tools/cooja/java/se/sics/cooja/corecomm/Lib8.java index ceb3abf63..071de05a4 100644 --- a/tools/cooja/java/se/sics/cooja/corecomm/Lib8.java +++ b/tools/cooja/java/se/sics/cooja/corecomm/Lib8.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Lib8.java,v 1.1 2006/08/21 12:12:58 fros4943 Exp $ + * $Id: Lib8.java,v 1.2 2006/08/23 17:13:03 fros4943 Exp $ */ package se.sics.cooja.corecomm; @@ -54,7 +54,7 @@ public class Lib8 extends CoreComm { public native void tick(); public native void init(); public native int getReferenceAbsAddr(); - public native byte[] getMemory(int start, int length); + public native void getMemory(int start, int length, byte[] mem); public native void setMemory(int start, int length, byte[] mem); }