X-Git-Url: http://sraa.de/git/?a=blobdiff_plain;f=dol%2Fsrc%2Fdol%2Fhelper%2Fprofiler%2FProfileParser.java;fp=dol%2Fsrc%2Fdol%2Fhelper%2Fprofiler%2FProfileParser.java;h=5ad130496c728d747ed6fec0ed4d9c302970a9cb;hb=8c411cf24ed0eb889191aaeafd8fa1e69081df42;hp=0000000000000000000000000000000000000000;hpb=dea7a4fb1ed110d3ce6e6d9255103d724bd66c0e;p=jump.git diff --git a/dol/src/dol/helper/profiler/ProfileParser.java b/dol/src/dol/helper/profiler/ProfileParser.java new file mode 100644 index 0000000..5ad1304 --- /dev/null +++ b/dol/src/dol/helper/profiler/ProfileParser.java @@ -0,0 +1,273 @@ +/* $Id: ProfileParser.java 203 2010-10-11 08:59:47Z dchokshi $ */ +package dol.helper.profiler; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.StringTokenizer; +import java.util.Vector; + +/** + * Class for parsing a profile and collecting parameter statistics. + */ +public class ProfileParser { + + /** + * Constructor. + **/ + public ProfileParser(String filename) { + try { + _in = new BufferedReader(new FileReader(filename)); + } catch (IOException e) { + System.err.println(e.getLocalizedMessage()); + } + + _processProfiles = new HashMap(); + _channelProfiles = new HashMap(); + _inPortToChannelMapping = new HashMap(); + _outPortToChannelMapping = new HashMap(); + _processCommOrder = new HashMap>(); + } + + /** + * Parse the profile file and generate the profiles for processes and + * channels. After calling this function, use + * {@link #getProcessProfiles()} and {@link #getChannelProfiles()} to + * obtain the result of parsing. + * + * @see #getChannelProfiles() + * @see #getProcessProfiles() + */ + public void parseProfile() { + String nextWord; + String line = null; + + while (true) { + try { + //PERFORMANCE: do not read line-by-line but read in larger + //chunks from the file. has much more influence on the + //performance than the data structures in this class. + line = _in.readLine(); + if (line == null) { + _in.close(); + + Iterator iterator = _processProfiles.keySet().iterator(); + while (iterator.hasNext()) { + _processProfiles.get(iterator.next()).stop(); + } + return; + } + } catch (IOException e) { + System.err.println(e.getLocalizedMessage()); + return; + } + + StringTokenizer tokenizer = new StringTokenizer(line); + nextWord = tokenizer.nextToken(); + + if (nextWord.equals("c")) { + //'c' stands for a channel connection line. example: + //c filterchannel 8 o filter 0x23c738 i filter 0x23c6e8 + String channelName = tokenizer.nextToken(); + int capacity = Integer.parseInt( + tokenizer.nextToken()); + String portAType = tokenizer.nextToken(); + String processAName = tokenizer.nextToken(); + String portAName = tokenizer.nextToken(); + String portBType = tokenizer.nextToken(); + String processBName = tokenizer.nextToken(); + String portBName = tokenizer.nextToken(); + addChannelProfile(channelName, capacity, + portAType, processAName, portAName, + portBType, processBName, portBName); + } else { + //current line is an event. examples: + //examples: + //78 filter started. + //79 filter r 0x23c6c0 8 + //80 filter w 0x23c738 8 + //81 filter stopped. + + int i = Integer.parseInt(nextWord); + if (i != _lineCounter) { + System.err.println("Input file corrupt: line number " + + "expected."); + return; + } + + //get process name + String processName = tokenizer.nextToken(); + if (_processProfiles.get(processName) == null) { + ProcessProfile processProfile = new ProcessProfile( + processName); + _processProfiles.put(processName, processProfile); + } + if(_processCommOrder.get(processName) == null) { + _processCommOrder.put(processName, new Vector()); + } + + nextWord = tokenizer.nextToken(); + if(nextWord.equals("started.")) { + _processProfiles.get(processName).start(); + } + else if(nextWord.equals("stopped.")) { + _processProfiles.get(processName).stop(); + } + else if (nextWord.equals("r") || nextWord.equals("w")) { + String accessType = nextWord; + String portName = tokenizer.nextToken(); + int amount = Integer.parseInt( + tokenizer.nextToken()); + try { + _processProfiles.get(processName). + portAccess(portName, amount); + if (accessType.equals("r")) { + String channelName = + _inPortToChannelMapping.get(portName); + _channelProfiles.get(channelName). + readAccess(amount); + _processCommOrder.get(processName).add(channelName); + + } else { + String channelName = + _outPortToChannelMapping.get(portName); + _channelProfiles.get(channelName). + writeAccess(amount); + _processCommOrder.get(processName).add(channelName); + } + } catch (NullPointerException e) { + System.err.println("Input file corrupt: cannot " + + "find channel associated to port " + + portName + "(line " + + _lineCounter + ")."); + e.printStackTrace(); + return; + } + } else { + System.err.println("Input file corrupt: unknown " + + "event type (line " + _lineCounter + ")."); + return; + } + _lineCounter++; + } + } + } + + /** + * Return the profiles of all processes. + * + * @return profile of all processes + */ + public HashMap getProcessProfiles() { + return _processProfiles; + } + + /** + * Return the profiles of all channels. + * + * @return profile of all channels + */ + public HashMap getChannelProfiles() { + return _channelProfiles; + } + + /** + * Return the order in which processes write to channels. + * + * @return profile of all processes + */ + public HashMap> getProcessCommOrder() { + return _processCommOrder; + } + + /** + * Return the channel to which the port with the specified name is + * connected. + * + * @param port port name + * @return name of connected channel + */ + public String getChannel(String port) { + if (_inPortToChannelMapping.get(port) != null) { + return _inPortToChannelMapping.get(port); + } else if (_outPortToChannelMapping.get(port) != null) { + return _outPortToChannelMapping.get(port); + } + return null; + } + + /** + * Return the type of the channel with the specified name. + * + * @param port port name + * @return type of port + */ + public String getPortType(String port) { + if (_inPortToChannelMapping.get(port) != null) { + return "INPUT"; + } else if (_outPortToChannelMapping.get(port) != null) { + return "OUTPUT"; + } + return null; + } + + /** + * Add a channel profile to the HashMap of channel profiles. + * + * @param channelName name of the channel + * @param capacity capacity of the channel + * @param portAType type of first port (either "o" or "i") + * @param processAName name of process connected to first port + * @param portAName name of first port + * @param portBType type of second port (either "o" or "i") + * @param processBName name of process connected to second port + * @param portBName name of second port + */ + protected void addChannelProfile(String channelName, int capacity, + String portAType, String processAName, String portAName, + String portBType, String processBName, String portBName) { + + ChannelProfile channelProfile = + new ChannelProfile(channelName, capacity); + _channelProfiles.put(channelName, channelProfile); + + if (portAType.equals("o")) { + //first output port, then input port + _outPortToChannelMapping.put(portAName, channelName); + if (!(portBType.equals("i"))) { + System.err.println("Input file corrupt: each " + + "channel needs one input- and one " + + "output port."); + return; + } + _inPortToChannelMapping.put(portBName, channelName); + } else if (portAType.equals("i")) { + //first input port, then output port + _inPortToChannelMapping.put(portAName, channelName); + if (!(portBType.equals("o"))) { + System.err.println("Input file corrupt: each " + + "channel needs one input- and one " + + "output port."); + return; + } + _outPortToChannelMapping.put(portBName, channelName); + } else { + System.err.println("Input file corrupt: bad channel " + + "specification:"); + System.err.println(channelName + " " + capacity + " " + + portAType + " " + processAName + " " + portAName + + " " + + portBType + " " + processBName + " " + portBName); + } + } + + private BufferedReader _in = null; + protected int _lineCounter = 0; + HashMap _processProfiles; + HashMap _channelProfiles; + HashMap _inPortToChannelMapping; + HashMap _outPortToChannelMapping; + HashMap> _processCommOrder; +}