performance improvement when fetching memory (throwing pointer to array instead of creating new one)

This commit is contained in:
fros4943 2006-08-23 17:11:09 +00:00
parent 52c14b8a4e
commit a05f9acab4
11 changed files with 100 additions and 86 deletions

View file

@ -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 * This is a JNI function and should only be called via the
* responsible Java part (MoteType.java). * responsible Java part (MoteType.java).
*/ */
JNIEXPORT jbyteArray JNICALL JNIEXPORT void JNICALL
Java_se_sics_cooja_corecomm_[CLASS_NAME]_getMemory(JNIEnv *env, jobject obj, jint start, jint length) 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); // jbyteArray ret=(*env)->NewByteArray(env, length);
(*env)->SetByteArrayRegion(env, ret, 0, (size_t) length, (jbyte *) start); (*env)->SetByteArrayRegion(env, mem_arr, 0, (size_t) length, (jbyte *) start);
return (ret); // return (ret);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/** /**

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja;
@ -36,18 +36,21 @@ import se.sics.cooja.corecomm.*;
/** /**
* The package corecomm's purpose is communicating with the simulation core * The package corecomm's purpose is communicating with the simulation core
* using Java Native Interface (JNI). Each implementing class (named Lib[1-MAX]), * using Java Native Interface (JNI). Each implementing class (named
* loads a shared library which belongs to one mote type. The reason for this * Lib[1-MAX]), loads a shared library which belongs to one mote type. The
* somewhat strange design is that once loaded, a native library cannot be unloaded * reason for this somewhat strange design is that once loaded, a native library
* in Java (yet). Therefore if we wish to load several libraries, the names and associated * cannot be unloaded in Java (yet). Therefore if we wish to load several
* native functions must have unique names. And those names are defined via the calling class in JNI. * libraries, the names and associated native functions must have unique names.
* For example, the native tick function in class Lib1 is named contiki_javasim_corecomm_Lib1_tick. * And those names are defined via the calling class in JNI. For example, the
* When creating a new mote type, the main contiki source file is generated with function names * native tick function in class Lib1 is named
* compatible with the next available corecomm. * contiki_javasim_corecomm_Lib1_tick. When creating a new mote type, the main
* This also implies that even if a mote type is deleted, a new one cannot be created without * contiki source file is generated with function names compatible with the next
* restarting the JVM and thus the entire simulation. * 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
* Each implemented CoreComm class needs read access to the following core variables: * simulation.
*
* Each implemented CoreComm class needs read access to the following core
* variables:
* <ul> * <ul>
* <li>referenceVar * <li>referenceVar
* </ul> * </ul>
@ -59,7 +62,7 @@ import se.sics.cooja.corecomm.*;
* <li>getMemory(int start, int length) * <li>getMemory(int start, int length)
* <li>setMemory(int start, int length, byte[] mem) * <li>setMemory(int start, int length, byte[] mem)
* </ul> * </ul>
*
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
public abstract class CoreComm { public abstract class CoreComm {
@ -71,41 +74,42 @@ public abstract class CoreComm {
// Static pointers to current libraries // Static pointers to current libraries
private final static CoreComm[] coreComms = new CoreComm[MAX_LIBRARIES]; private final static CoreComm[] coreComms = new CoreComm[MAX_LIBRARIES];
private final static File[] coreCommFiles = new File[MAX_LIBRARIES]; private final static File[] coreCommFiles = new File[MAX_LIBRARIES];
/** /**
* Has any library been loaded? Since libraries can't be unloaded * Has any library been loaded? Since libraries can't be unloaded the entire
* the entire simulator may have to be restarted. * simulator may have to be restarted.
* *
* @return True if any library has been loaded this session * @return True if any library has been loaded this session
*/ */
public static boolean hasLibraryBeenLoaded() { public static boolean hasLibraryBeenLoaded() {
for (int i=0; i < coreComms.length; i++) for (int i = 0; i < coreComms.length; i++)
if (coreComms[i] != null) if (coreComms[i] != null)
return true; return true;
return false; return false;
} }
/** /**
* Has given library file already been loaded during this session? * Has given library file already been loaded during this session? A loaded
* A loaded library can be removed, but not unloaded * library can be removed, but not unloaded during one session. And a new
* during one session. And a new library file, named * library file, named the same as an earlier loaded and removed file, can't
* the same as an earlier loaded and removed file, * be loaded either.
* can't be loaded either. *
* * @param libraryFile
* @param libraryFile Library file * Library file
* @return True if a library has already been loaded from the given file's filename * @return True if a library has already been loaded from the given file's
* filename
*/ */
public static boolean hasLibraryFileBeenLoaded(File libraryFile) { public static boolean hasLibraryFileBeenLoaded(File libraryFile) {
for (File libFile: coreCommFiles) for (File libFile : coreCommFiles)
if (libFile != null && libFile.getName().equals(libraryFile.getName())) if (libFile != null && libFile.getName().equals(libraryFile.getName()))
return true; return true;
return false; return false;
} }
/** /**
* Get the class name of next free core communicator class. * Get the class name of next free core communicator class. If null is
* If null is returned, no classes are available. * returned, no classes are available.
* *
* @return Class name * @return Class name
*/ */
public static String getAvailableClassName() { public static String getAvailableClassName() {
@ -130,11 +134,13 @@ public abstract class CoreComm {
} }
/** /**
* Create and return an instance of the core communicator identified * Create and return an instance of the core communicator identified by
* by className. This core communicator will load the native library libFile. * className. This core communicator will load the native library libFile.
* *
* @param className Class name of core communicator * @param className
* @param libFile Native library file * Class name of core communicator
* @param libFile
* Native library file
* @return Core Communicator * @return Core Communicator
*/ */
public static CoreComm createCoreComm(String className, File libFile) { public static CoreComm createCoreComm(String className, File libFile) {
@ -182,43 +188,47 @@ public abstract class CoreComm {
return null; return null;
} }
/** /**
* Ticks a mote once. This should not be used directly, * Ticks a mote once. This should not be used directly, but instead via
* but instead via Mote.tick(). * Mote.tick().
*/ */
public abstract void tick(); public abstract void tick();
/** /**
* Initializes a mote by running a startup script in the core. * Initializes a mote by running a startup script in the core. (Should only by
* (Should only by run once, at the same time as the library is loaded) * run once, at the same time as the library is loaded)
*/ */
protected abstract void init(); protected abstract void init();
/** /**
* Returns absolute memory location of the core variable * Returns absolute memory location of the core variable referenceVar. Used to
* referenceVar. Used to get offset between relative and absolute * get offset between relative and absolute memory addresses.
* memory addresses. *
*
* @return Absolute memory address * @return Absolute memory address
*/ */
public abstract int getReferenceAbsAddr(); public abstract int getReferenceAbsAddr();
/** /**
* Returns a memory segment identified by start and length. * Fills an byte array with memory segment identified by start and length.
* *
* @param start Start address of segment * @param start
* @param length Length of segment * Start address of segment
* @return Memory 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. * Overwrites a memory segment identified by start and length.
* *
* @param start Start address of segment * @param start
* @param length Length of segment * Start address of segment
* @param mem Data to fill memory segment * @param length
* Length of segment
* @param mem
* New memory segment data
*/ */
public abstract void setMemory(int start, int length, byte[] mem); public abstract void setMemory(int start, int length, byte[] mem);

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.contikimote;
@ -239,10 +239,12 @@ public class ContikiMoteType implements MoteType {
} }
// Create initial memory // Create initial memory
byte[] initialDataSection = getCoreMemory(relDataSectionAddr byte[] initialDataSection = new byte[dataSectionSize];
+ offsetRelToAbs, dataSectionSize); getCoreMemory(relDataSectionAddr
byte[] initialBssSection = getCoreMemory( + offsetRelToAbs, dataSectionSize, initialDataSection);
relBssSectionAddr + offsetRelToAbs, bssSectionSize); byte[] initialBssSection = new byte[bssSectionSize];
getCoreMemory(
relBssSectionAddr + offsetRelToAbs, bssSectionSize, initialBssSection);
initialMemory = new SectionMoteMemory(varAddresses); initialMemory = new SectionMoteMemory(varAddresses);
initialMemory.setMemorySegment(relDataSectionAddr, initialDataSection); initialMemory.setMemorySegment(relDataSectionAddr, initialDataSection);
initialMemory.setMemorySegment(relBssSectionAddr, initialBssSection); initialMemory.setMemorySegment(relBssSectionAddr, initialBssSection);
@ -294,8 +296,10 @@ public class ContikiMoteType implements MoteType {
for (int i = 0; i < mem.getNumberOfSections(); i++) { for (int i = 0; i < mem.getNumberOfSections(); i++) {
int startAddr = mem.getStartAddrOfSection(i); int startAddr = mem.getStartAddrOfSection(i);
int size = mem.getSizeOfSection(i); int size = mem.getSizeOfSection(i);
mem.setMemorySegment(startAddr, getCoreMemory(startAddr + offsetRelToAbs, byte[] data = mem.getDataOfSection(i);
size));
getCoreMemory(startAddr + offsetRelToAbs,
size, data);
} }
} }
@ -344,8 +348,8 @@ public class ContikiMoteType implements MoteType {
return myCoreComm.getReferenceAbsAddr(); return myCoreComm.getReferenceAbsAddr();
} }
private byte[] getCoreMemory(int start, int length) { private void getCoreMemory(int start, int length, byte[] data) {
return myCoreComm.getMemory(start, length); myCoreComm.getMemory(start, length, data);
} }
private void setCoreMemory(int start, int length, byte[] mem) { private void setCoreMemory(int start, int length, byte[] mem) {

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.corecomm;
@ -54,7 +54,7 @@ public class Lib1 extends CoreComm {
public native void tick(); public native void tick();
public native void init(); public native void init();
public native int getReferenceAbsAddr(); 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); public native void setMemory(int start, int length, byte[] mem);
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.corecomm;
@ -54,7 +54,7 @@ public class Lib2 extends CoreComm {
public native void tick(); public native void tick();
public native void init(); public native void init();
public native int getReferenceAbsAddr(); 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); public native void setMemory(int start, int length, byte[] mem);
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.corecomm;
@ -54,7 +54,7 @@ public class Lib3 extends CoreComm {
public native void tick(); public native void tick();
public native void init(); public native void init();
public native int getReferenceAbsAddr(); 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); public native void setMemory(int start, int length, byte[] mem);
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.corecomm;
@ -54,7 +54,7 @@ public class Lib4 extends CoreComm {
public native void tick(); public native void tick();
public native void init(); public native void init();
public native int getReferenceAbsAddr(); 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); public native void setMemory(int start, int length, byte[] mem);
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.corecomm;
@ -54,7 +54,7 @@ public class Lib5 extends CoreComm {
public native void tick(); public native void tick();
public native void init(); public native void init();
public native int getReferenceAbsAddr(); 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); public native void setMemory(int start, int length, byte[] mem);
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.corecomm;
@ -54,7 +54,7 @@ public class Lib6 extends CoreComm {
public native void tick(); public native void tick();
public native void init(); public native void init();
public native int getReferenceAbsAddr(); 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); public native void setMemory(int start, int length, byte[] mem);
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.corecomm;
@ -54,7 +54,7 @@ public class Lib7 extends CoreComm {
public native void tick(); public native void tick();
public native void init(); public native void init();
public native int getReferenceAbsAddr(); 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); public native void setMemory(int start, int length, byte[] mem);
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.corecomm;
@ -54,7 +54,7 @@ public class Lib8 extends CoreComm {
public native void tick(); public native void tick();
public native void init(); public native void init();
public native int getReferenceAbsAddr(); 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); public native void setMemory(int start, int length, byte[] mem);
} }