[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/wustl/tools/java/edu/wustl/mobilab/directory ExperimentDataCollector.java, NONE, 1.1 TraceAnalyser.java, NONE, 1.1

Chien-Liang Fok chien-liang at users.sourceforge.net
Wed Apr 5 11:34:02 PDT 2006


Update of /cvsroot/tinyos/tinyos-1.x/contrib/wustl/tools/java/edu/wustl/mobilab/directory
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22766/edu/wustl/mobilab/directory

Added Files:
	ExperimentDataCollector.java TraceAnalyser.java 
Log Message:


--- NEW FILE: ExperimentDataCollector.java ---
package edu.wustl.mobilab.directory;

import java.awt.event.ActionEvent;
import java.util.*;
import java.io.*;

import javax.swing.JButton;
import javax.swing.JFrame;

import edu.wustl.mobilab.agilla.AgillaConstants;
import edu.wustl.mobilab.agilla.messages.*;

import net.tinyos.message.*;
import net.tinyos.packet.BuildSource;
import net.tinyos.util.PrintStreamMessenger;

public class ExperimentDataCollector implements AgillaConstants {
	public static final int AGENT_MOVED   = 0;
	public static final int QUERY_ISSUED  = 1;
	public static final int QUERY_RESULTS_RECEIVED = 2;
	
	private boolean debug;
	private Vector<Mote> motes = new Vector<Mote>();	
	private TraceReceiver traceRcvr = new TraceReceiver();
	
	public ExperimentDataCollector(int[] ports, boolean debug) {
		this.debug = debug;				
		
		for (int i = 0; i < ports.length; i++) {
			String source = "sf at localhost:" + ports[i];
			log("Connecting to " + source);
			MoteIF moteIF = new MoteIF(BuildSource.makePhoenix(source, PrintStreamMessenger.err));
			Mote mote = new Mote(ports[i], moteIF);			
			moteIF.registerListener(new AgillaExpLatencyMsg(), mote);
			moteIF.registerListener(new AgillaExpResultsMsg(), mote);
			moteIF.registerListener(new AgillaTraceMsg(), traceRcvr);
			motes.add(mote);
		}		
		createGUI();
	}

	/**
	 *  Gather all of the data from the experiment and print them to StdOut.
	 */
	private void doFinish() {
		// for each mote, get the number of queries and updates
		int totalNumQueries = 0, totalNumUpdates = 0;
		long totalLatencies = 0, totalLatenciesCount = 0;
		
		// stop receiving trace messages
		for (int i = 0; i < motes.size(); i++) {
			Mote c = motes.get(i);
			c.moteIF.deregisterListener(new AgillaTraceMsg(), traceRcvr);
		}
		
		
		for (int i = 0; i < motes.size(); i++) {
			Mote c = motes.get(i);
			c.fetchResults();
			System.out.println(c);
			totalNumQueries += c.numQueries;
			totalNumUpdates += c.numUpdates;
			totalLatencies += c.totalLatency();
			totalLatenciesCount += c.numLatencies();
		}
		
		System.out.println("-------- Overall Results --------");
		System.out.println("Total Number of Queries: " + totalNumQueries);
		System.out.println("Total Number of Updates: " + totalNumUpdates);
		System.out.println("Average Query Latency = " + (totalLatenciesCount == 0 ? "NaN" : totalLatencies/totalLatenciesCount));		
		System.exit(0);
	}
	
	private void createGUI() {				
		JButton button = new JButton("Stop");
		button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				doFinish();
			}
		});
		JFrame frame = new JFrame();
		frame.setTitle("Exp Data Collector");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(button);
		frame.pack();
		frame.setVisible(true);
	}
	
	private class TraceReceiver implements MessageListener {		
		public TraceReceiver() {
		}
				
		private String convString(int action) {
			switch(action) {
			case AGENT_MOVED:
				return "AGENT_MOVED";
			case QUERY_ISSUED:
				return "QUERY_ISSUED";
			case QUERY_RESULTS_RECEIVED:
				return "QUERY_RESULTS_RECEIVED";
			}
			return "UNKNOWN";
		}
		
		public void messageReceived(int to, Message m) {				
			AgillaTraceMsg trace = (AgillaTraceMsg)m;
			System.out.println("TRACE: " + trace.get_timestamp_high32() + "" + trace.get_timestamp_low32() + " "
					+ trace.get_agentID() + " " + trace.get_nodeID() + " " + convString(trace.get_action()) + " " 
					+ trace.get_success() + " " + trace.get_loc_x()+ " " + trace.get_loc_y());
		}		
	}
	
	/**
	 * Keeps track of the statistics for a single mote.
	 * 
	 * @author liang	 
	 */
	private class Mote implements MessageListener  {
		int tcpPort;
		int numQueries;
		int numUpdates;
		MoteIF moteIF;
		Vector<AgillaExpLatencyMsgJ> latencies = new Vector<AgillaExpLatencyMsgJ>();
		Object lock = new Object();
		boolean gotResults = false;
		FetchResultsTimer timer = null;
		
		public Mote(int tcpPort, MoteIF moteIF) {
			this.tcpPort = tcpPort;
			this.moteIF = moteIF;
		}
		
		public void messageReceived(int to, Message m) {
			if (m instanceof AgillaExpLatencyMsg) {				
				AgillaExpLatencyMsg ajm = (AgillaExpLatencyMsg)m;
				AgillaExpLatencyMsgJ msgj = new AgillaExpLatencyMsgJ(ajm);
				//log("Port " + tcpPort + ": Latency " + msgj);
				log("Port " + tcpPort + ": Latency " + ajm.get_latency() + " " + msgj);
				latencies.add(msgj);
			} else if (m instanceof AgillaExpResultsMsg) {
				synchronized(lock) {
					numQueries = ((AgillaExpResultsMsg)m).get_numQueries();
					numUpdates = ((AgillaExpResultsMsg)m).get_numUpdates();
					log("\tnumQueries = " + numQueries + ", numUpdates = " + numUpdates);
					gotResults = true;
					if (timer != null) {
						timer.kill();
						timer = null;
					}
					lock.notify();
				}
			}			
		}
		
		public void fetchResults() {
			gotResults = false;
			while (!gotResults) {
				try {				
					synchronized(lock) {
						System.out.println("Fetching results from TCP Port " + tcpPort + "...");
						moteIF.send(TOS_BCAST_ADDRESS, new AgillaExpQueryResultsMsg());
						timer = new FetchResultsTimer(lock);
						try {
							lock.wait();
						} catch(InterruptedException e) {
							e.printStackTrace();
						}
						if (!gotResults)
							log("Did not get results, trying again...");
					}
				} catch(IOException ioe) {
					ioe.printStackTrace();
				}
			}
		}
		
		private class FetchResultsTimer implements Runnable {
			private static final int FETCH_RESULTS_TIMER = 1000;
			private Object lock;
			private boolean alive = true;
			
			public FetchResultsTimer(Object lock) {
			    this.lock = lock;
			    new Thread(this).start();
			}
			
			public void kill() {
				synchronized(lock) {
					alive = false;
				}
			}
			
			public void run () {
			    try {
					Thread.sleep(FETCH_RESULTS_TIMER);
			    } catch(Exception e) {
					e.printStackTrace();
			    }
			    synchronized(lock) {
			    	if (alive) lock.notify();				
			    }
			}
		}
		
		public int numLatencies() {
			return latencies.size();
		}
		
		public long totalLatency() {
			long result = 0;
			if (latencies.size() == 0)
				return 0;
			for (int i = 0; i < latencies.size(); i++) {
				result += latencies.get(i).latency();
			}
			return result;
		}
		
		public long avgLatency() {
			return totalLatency() / numLatencies();
		}
		
		public String printLatencies() {
			String result = "";
			for (int i = 0; i < latencies.size(); i++) {
				result += latencies.get(i) + " ";
			}
			return result;
		}
		
		public String toString() {
			return "TCP Port " + tcpPort + ", numQueries = " + numQueries + ", numUpdates = " + numUpdates + "\n" + printLatencies();
		}
	}
	
	private void log(String msg) {
		if (debug)
			System.out.println("ExpDataCollector: " + msg);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		boolean debug = false;
		Vector<String> portStrings = new Vector<String>();

		if (args.length == 0) {
			usage();
			System.exit(0);
		}
					
		try {
			int index = 0;
			while (index < args.length) {
				String arg = args[index];
				if (arg.equals("-h") || arg.equals("--help")) {
					usage();
					System.exit(0);
				} else if (arg.equals("-p")) {
					index++;
					while (index < args.length) {
						portStrings.add(args[index++]);
					}
					//numMotes = Integer.valueOf(args[index]);
				}else if (arg.equals("-d")) {
					debug = true;
				} else {
					usage();
					System.exit(1);
				}
				index++;
			}			
			if (portStrings.size() == 0)
				throw new Exception();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		int ports[] = new int[portStrings.size()];
		for (int i = 0; i < portStrings.size(); i++) {
			ports[i] = Integer.valueOf(portStrings.get(i));
		}
		
		try {
			new ExperimentDataCollector(ports, debug);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void usage() {
		System.err.println("Usage: ExperimentDataCollector [-h | -d | -p ports]");
		System.err.println("\t-h Print this help message");
		System.err.println("\t-d Enable Debug mode");		
		System.err.println("\tports A list of TCP ports to connect to.");		
	}
}

--- NEW FILE: TraceAnalyser.java ---
package edu.wustl.mobilab.directory;

import edu.wustl.mobilab.agilla.variables.*;
import java.util.*;
import java.io.*;

public class TraceAnalyser {
	public static final int INTRUDER_AGENT_ID = 0;

	private boolean debug;
	private Vector<TraceLine> trace = new Vector<TraceLine>();
	
	public TraceAnalyser(String file, boolean debug) throws Exception {		
		this.debug = debug;
		
		// Read in the data
		File f = new File(file);
		BufferedReader reader = new BufferedReader(new FileReader(f));			
		String nextLine = reader.readLine();
		while (nextLine != null) {
			if (nextLine.startsWith("TRACE:"))
				trace.add(new TraceLine(nextLine));
			nextLine = reader.readLine();
		}
		analyze();
	}
	
	private void analyze() {
		AgillaLocation intruderLoc = null;
		
		int numGoodQueries = 0, numBadQueries = 0, numFailedQueries = 0;
		
		// Find out the number of erroneous location lookups
		for (int i = 0; i < trace.size(); i++) {
			TraceLine line = trace.get(i);
			
			if (line.action.equals("AGENT_MOVED")) {
				if (line.agentID == INTRUDER_AGENT_ID) {
					intruderLoc = line.loc;
				}					
			}
			else if (line.action.equals("QUERY_RESULTS_RECEIVED")) {
				if (line.agentID != INTRUDER_AGENT_ID) {
					if (line.success) {
						if (line.loc.equals(intruderLoc))
							numGoodQueries++;
						else
							numBadQueries++;
					} else
						numFailedQueries++;
				}
			}
		}
		
		System.out.println("Query Statistics:  Good: " + numGoodQueries + ", Bad: " + numBadQueries + ", Failed: " + numFailedQueries);
	}
	
	private class TraceLine {
		long timeStamp;
		int agentID, nodeID;
		String action; 
		boolean success;
		AgillaLocation loc;
		
		public TraceLine(String line) {
			int sIndex = line.indexOf(" ");
			int eIndex = line.indexOf(" ", sIndex+1);
			timeStamp = Long.valueOf(line.substring(sIndex+1, eIndex));
			log("timeStamp = " + timeStamp);
			
			sIndex = eIndex;
			eIndex = line.indexOf(" ", sIndex+1);
			agentID = Integer.valueOf(line.substring(sIndex+1, eIndex));
			log("agentID = " + agentID);
			
			sIndex = eIndex;
			eIndex = line.indexOf(" ", sIndex+1);
			nodeID = Integer.valueOf(line.substring(sIndex+1, eIndex));
			log("nodeID = " + nodeID);
			
			sIndex = eIndex;
			eIndex = line.indexOf(" ", sIndex+1);
			action = line.substring(sIndex+1, eIndex);
			log("action = " + action);
			
			sIndex = eIndex;
			eIndex = line.indexOf(" ", sIndex+1);
			success = (Integer.valueOf(line.substring(sIndex+1, eIndex)) == 1);
			log("success = " + success);
			
			int x, y;
			sIndex = eIndex;
			eIndex = line.indexOf(" ", sIndex+1);
			x = Integer.valueOf(line.substring(sIndex+1, eIndex));
			log("x = " + x);
			
			sIndex = eIndex;
			y = Integer.valueOf(line.substring(sIndex+1));
			loc = new AgillaLocation(x,y);
			log("y = " + y);
		}
	}
	
	private void log(String msg) {
		if (debug)
			System.out.println(msg);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String file = null;
		boolean debug = false;
		
		if (args.length == 0) {
			usage();
			System.exit(0);
		}
					
		try {
			int index = 0;
			while (index < args.length) {
				String arg = args[index];
				if (arg.equals("-h") || arg.equals("--help")) {
					usage();
					System.exit(0);
				} else if (arg.equals("-f")) {
					index++;
					file = args[index];
				}else if (arg.equals("-d")) {
					debug = true;
				} else {
					usage();
					System.exit(1);
				}
				index++;
			}			
			if (file == null)
				throw new Exception();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			new TraceAnalyser(file, debug);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void usage() {
		System.err.println("Usage: TraceAnalyser [-h | -d | -f <file>]");
		System.err.println("\t-h Print this help message");
		System.err.println("\t-d Enable Debug mode");		
		System.err.println("\t-f <file> Where <file> is contains the experiment trace data.");		
	}
}



More information about the Tinyos-contrib-commits mailing list