From f6e0702b3be6bf9a0dc8e9c7bdc56e3f1a0fbebf Mon Sep 17 00:00:00 2001 From: joxe Date: Mon, 22 Sep 2008 16:18:22 +0000 Subject: [PATCH] optimized some loops for increased performance and added runprof for profiling --- .../src/se/sics/cooja/mspmote/MspMote.java | 12 +++++-- tools/cooja/build.xml | 13 ++++++++ .../se/sics/cooja/MoteInterfaceHandler.java | 17 +++++++--- .../cooja/java/se/sics/cooja/Simulation.java | 33 ++++++++++--------- .../sics/cooja/plugins/LogScriptEngine.java | 8 ++++- 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java index 356fac589..419af7724 100644 --- a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: MspMote.java,v 1.11 2008/09/22 09:32:13 joxe Exp $ + * $Id: MspMote.java,v 1.12 2008/09/22 16:18:48 joxe Exp $ */ package se.sics.cooja.mspmote; @@ -262,6 +262,8 @@ public abstract class MspMote implements Mote { protected abstract boolean initEmulator(File ELFFile); private int currentSimTime = -1; + + /* return false when done - e.g. true means more work to do before finished with this tick */ public boolean tick(int simTime) { if (stopNextInstruction) { stopNextInstruction = false; @@ -281,12 +283,16 @@ public abstract class MspMote implements Mote { return false; } - myMoteInterfaceHandler.doActiveActionsBeforeTick(); - // Leave control to emulated CPU cycleCounter += 1; MSP430 cpu = getCPU(); + if (cpu.cycles > cycleCounter) { + /* CPU already ticked too far - just wait it out */ + return true; + } + myMoteInterfaceHandler.doActiveActionsBeforeTick(); + cpu.step(cycleCounter); /* Check if radio has pending incoming bytes */ diff --git a/tools/cooja/build.xml b/tools/cooja/build.xml index a9251360e..cc4196dcb 100644 --- a/tools/cooja/build.xml +++ b/tools/cooja/build.xml @@ -71,6 +71,19 @@ The COOJA Simulator + + + + + + + + + + + + + diff --git a/tools/cooja/java/se/sics/cooja/MoteInterfaceHandler.java b/tools/cooja/java/se/sics/cooja/MoteInterfaceHandler.java index ae86a224b..868637a4b 100644 --- a/tools/cooja/java/se/sics/cooja/MoteInterfaceHandler.java +++ b/tools/cooja/java/se/sics/cooja/MoteInterfaceHandler.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: MoteInterfaceHandler.java,v 1.2 2007/01/10 14:57:42 fros4943 Exp $ + * $Id: MoteInterfaceHandler.java,v 1.3 2008/09/22 16:18:22 joxe Exp $ */ package se.sics.cooja; @@ -74,7 +74,8 @@ public class MoteInterfaceHandler { private Radio myRadio; private Vector myActiveInterfaces = new Vector(); - + private MoteInterface[] activeCache = null; + private Vector myPassiveInterfaces = new Vector(); /** @@ -283,8 +284,15 @@ public class MoteInterfaceHandler { * tick before the mote software is executed. */ public void doActiveActionsBeforeTick() { - for (int i = 0; i < myActiveInterfaces.size(); i++) - myActiveInterfaces.get(i).doActionsBeforeTick(); + // Assuming only one caller!!! + if (activeCache == null) { + activeCache = (MoteInterface[]) myActiveInterfaces.toArray(new MoteInterface[myActiveInterfaces.size()]); + } +// for (int i = 0; i < myActiveInterfaces.size(); i++) +// myActiveInterfaces.get(i).doActionsBeforeTick(); + for (int i = 0, n = activeCache.length; i < n; i++) { + activeCache[i].doActionsBeforeTick(); + } } /** @@ -352,6 +360,7 @@ public class MoteInterfaceHandler { */ public void addActiveInterface(MoteInterface newInterface) { myActiveInterfaces.add(newInterface); + activeCache = null; } /** diff --git a/tools/cooja/java/se/sics/cooja/Simulation.java b/tools/cooja/java/se/sics/cooja/Simulation.java index 1967d73f9..f24e5c679 100644 --- a/tools/cooja/java/se/sics/cooja/Simulation.java +++ b/tools/cooja/java/se/sics/cooja/Simulation.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: Simulation.java,v 1.22 2008/04/01 08:12:16 fros4943 Exp $ + * $Id: Simulation.java,v 1.23 2008/09/22 16:18:22 joxe Exp $ */ package se.sics.cooja; @@ -162,26 +162,27 @@ public class Simulation extends Observable implements Runnable { } } + Mote[] mspArray = mspMotes.toArray(new Mote[mspMotes.size()]); + try { while (isRunning) { - try { /* Tick MSP motes */ - try { +// try { boolean wantMoreTicks = true; while (wantMoreTicks) { /* Tick all MSP motes until none need more ticks */ wantMoreTicks = false; - for (Mote moteToTick : mspMotes) { - if (moteToTick.tick(currentSimulationTime)) { + for (int i = 0, n = mspArray.length; i < n; i++) { + if (mspArray[i].tick(currentSimulationTime)) { wantMoreTicks = true; } } } - } catch (RuntimeException e) { - isRunning = false; - thread = null; - break; - } +// } catch (RuntimeException e) { +// isRunning = false; +// thread = null; +// break; +// } // Tick current mote subset for (Mote moteToTick : allLists[currentTickListIndex]) { @@ -212,23 +213,23 @@ public class Simulation extends Observable implements Runnable { thread = null; } - } catch (InterruptedException e) { + + } + } catch (InterruptedException e) { isRunning = false; thread = null; - break; +// break; } catch (IllegalArgumentException e) { logger.warn("llegalArgumentException:" + e); isRunning = false; thread = null; - break; +// break; } catch (IllegalMonitorStateException e) { logger.warn("IllegalMonitorStateException:" + e); isRunning = false; thread = null; - break; +// break; } - } - isRunning = false; thread = null; stopSimulation = false; diff --git a/tools/cooja/java/se/sics/cooja/plugins/LogScriptEngine.java b/tools/cooja/java/se/sics/cooja/plugins/LogScriptEngine.java index 41a6ca07a..9a5588842 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/LogScriptEngine.java +++ b/tools/cooja/java/se/sics/cooja/plugins/LogScriptEngine.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: LogScriptEngine.java,v 1.2 2008/09/17 16:30:57 fros4943 Exp $ + * $Id: LogScriptEngine.java,v 1.3 2008/09/22 16:18:22 joxe Exp $ */ package se.sics.cooja.plugins; @@ -65,6 +65,8 @@ public class LogScriptEngine { private String scriptCode; + private ScriptMote scriptMote; + private interface ScriptLog { public void log(String log); } @@ -180,6 +182,7 @@ public class LogScriptEngine { " mote = obj;" + " id = mote.getInterfaces().getMoteID().getMoteID();" + " msg = mote.getInterfaces().getLog().getLastLogMessages();" + + " node.setMoteMsg(mote, msg);" + "} else {" + " return;" + "} " + @@ -201,6 +204,9 @@ public class LogScriptEngine { Hashtable hash = new Hashtable(); engine.put("global", hash); + scriptMote = new ScriptMote(); + engine.put("node", scriptMote); + /* TODO Test script */ logObserver.update(null, null); }