dol: initial dol commit
[jump.git] / dol / src / dol / helper / profiler / Profiler.java
diff --git a/dol/src/dol/helper/profiler/Profiler.java b/dol/src/dol/helper/profiler/Profiler.java
new file mode 100644 (file)
index 0000000..e24d456
--- /dev/null
@@ -0,0 +1,212 @@
+/* $Id: Profiler.java 203 2010-10-11 08:59:47Z dchokshi $ */\r
+package dol.helper.profiler;\r
+\r
+import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.Vector;\r
+\r
+import dol.datamodel.XmlTag;\r
+import dol.datamodel.pn.Channel;\r
+import dol.datamodel.pn.Port;\r
+import dol.datamodel.pn.Process;\r
+import dol.datamodel.pn.ProcessNetwork;\r
+import dol.datamodel.pn.ProfilingConfiguration;\r
+\r
+\r
+/**\r
+ * DOL profiler main class. Reads in profile data and generates the\r
+ * profiling information.\r
+ */\r
+public class Profiler {\r
+\r
+    /**\r
+     * Constructor\r
+     */\r
+    public Profiler() {\r
+    }\r
+\r
+    /**\r
+     * @param name Name of the trace file\r
+     * @param pn The process network need to be annotated\r
+     */\r
+    public void profilePN(String name, ProcessNetwork pn) {\r
+        ProfileParser profileParser = new ProfileParser(name);\r
+        profileParser.parseProfile();\r
+\r
+        HashMap<String, ChannelProfile> channelProfiles =\r
+            profileParser.getChannelProfiles();\r
+        Iterator<String> iterator = channelProfiles.keySet().iterator();\r
+\r
+        while (iterator.hasNext()) {\r
+            ChannelProfile profile = channelProfiles.get(iterator.next());\r
+            Channel channel = pn.getChannel(profile.getName());\r
+            ProfilingConfiguration c = channel.getProfilingCfg(\r
+                    _xt.getProfilingTotalReadData());\r
+            if (c == null) {\r
+                //no profiling present, add new one\r
+                c = new ProfilingConfiguration(\r
+                        _xt.getProfilingTotalReadData());\r
+                c.setValue(Long.toString(profile.getTotalReadData()));\r
+                channel.getProfilingList().add(c);\r
+            } else {\r
+                // profiling already present, extend existing one\r
+                Long curr = Long.decode(c.getValue());\r
+                curr += profile.getTotalReadData();\r
+                c.setValue(Long.toString(curr));\r
+            }\r
+\r
+            c = channel.getProfilingCfg(_xt.getProfilingNumOfReads());\r
+            if (c == null) {\r
+                c = new ProfilingConfiguration(\r
+                        _xt.getProfilingNumOfReads());\r
+                c.setValue(Integer.toString(profile.getNumOfReads()));\r
+                channel.getProfilingList().add(c);\r
+            } else {\r
+                Long curr = Long.decode(c.getValue());\r
+                curr += profile.getNumOfReads();\r
+                c.setValue(Long.toString(curr));\r
+            }\r
+\r
+            c = channel.getProfilingCfg(_xt.getProfilingNumOfWrites());\r
+            if (c == null) {\r
+                c = new ProfilingConfiguration(\r
+                        _xt.getProfilingNumOfWrites());\r
+                c.setValue(Integer.toString(profile.getNumOfWrites()));\r
+                channel.getProfilingList().add(c);\r
+            } else {\r
+                Long curr = Long.decode(c.getValue());\r
+                curr += profile.getNumOfWrites();\r
+                c.setValue( Long.toString(curr) );\r
+            }\r
+        }\r
+\r
+        HashMap<String, ProcessProfile> processProfiles =\r
+            profileParser.getProcessProfiles();\r
+        iterator = processProfiles.keySet().iterator();\r
+\r
+        while (iterator.hasNext()) {\r
+            ProcessProfile profile = processProfiles.get(iterator.next());\r
+            Process process = pn.getProcess(profile.getName());\r
+            ProfilingConfiguration c = process.getProfilingCfg(\r
+                    _xt.getProfilingNumOfFires());\r
+\r
+            if (c == null) {\r
+                c = new ProfilingConfiguration(\r
+                        _xt.getProfilingNumOfFires());\r
+                c.setValue(Integer.toString(profile.getNumOfFires()));\r
+                process.getProfilingList().add(c);\r
+            } else {\r
+                int curr = Integer.valueOf(c.getValue());\r
+                curr += profile.getNumOfFires();\r
+                c.setValue(Integer.toString(curr));\r
+            }\r
+\r
+            HashMap<String, PortProfile> portProfiles =\r
+                profile.getPortProfiles();\r
+            Iterator<String> iteratorB = portProfiles.keySet().iterator();\r
+\r
+            while (iteratorB.hasNext()) {\r
+                PortProfile portProfile =\r
+                    portProfiles.get(iteratorB.next());\r
+                String portName = getPNPortName(portProfile.getName(), profileParser, pn);\r
+                c = new ProfilingConfiguration(portName + ".accesses");\r
+                c.setValue(portProfile.getAccesses().toString());\r
+                process.getProfilingList().add(c);\r
+                c = new ProfilingConfiguration(portName + ".tokensize");\r
+                c.setValue(portProfile.getTokenSize().toString());\r
+                process.getProfilingList().add(c);\r
+                c = new ProfilingConfiguration(portName + ".initialAccesses");\r
+                c.setValue(Integer.toString(portProfile.getInitialAccesses()));\r
+                process.getProfilingList().add(c);\r
+                c = new ProfilingConfiguration(portName + ".initialtokensize");\r
+                c.setValue(portProfile.getInitialTokenSize().toString());\r
+                process.getProfilingList().add(c);\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     *\r
+     */\r
+    protected String getPNPortName(String profilePortName,\r
+            ProfileParser parser, ProcessNetwork pn) {\r
+        Vector<Port> ports = pn.getChannel(parser.\r
+                getChannel(profilePortName)).getPortList();\r
+        for (int i = 0; i < ports.size(); i++) {\r
+            if (parser.getPortType(profilePortName).equals("INPUT") &&\r
+                    ports.elementAt(i).isOutPort()) {\r
+                    return ports.elementAt(i).getPeerPort().getName();\r
+            } else if (parser.getPortType(profilePortName).equals("OUTPUT") &&\r
+                    ports.elementAt(i).isInPort()) {\r
+                    return ports.elementAt(i).getPeerPort().getName();\r
+            }\r
+        }\r
+        return null;\r
+    }\r
+\r
+    /**\r
+     * Main function\r
+     *\r
+     * @param args command line arguments. args[0] is the filename of\r
+     * the profile file. args[1] is the filename of the process network\r
+     * XML file.\r
+     */\r
+    public static void main(String[] args) {\r
+        //check command line parameters\r
+        if (args.length != 2) {\r
+            System.out.println("Usage: Profiler <trace> <pn>");\r
+            System.out.println();\r
+            return;\r
+        }\r
+\r
+        ProfileParser profileParser = new ProfileParser(args[0]);\r
+\r
+        //parse input file\r
+        profileParser.parseProfile();\r
+\r
+        //print the results\r
+        System.out.println("----- Channel Analyzer Report -----");\r
+        System.out.println("<channel name> <maximum fill level> "\r
+                + "<total amount of transferred data> "\r
+                + "<num of reads> <num of writes>");\r
+\r
+        HashMap<String, ChannelProfile> channelProfiles =\r
+            profileParser.getChannelProfiles();\r
+        Iterator<String> iterator = channelProfiles.keySet().iterator();\r
+\r
+        while (iterator.hasNext()) {\r
+            ChannelProfile profile = channelProfiles.get(iterator.next());\r
+            System.out.println( "<" + profile.getName() +\r
+                    "> "\r
+                    + profile.getMaxFillLevel() + " "\r
+                    + profile.getTotalReadData() + " "\r
+                    + profile.getNumOfReads() + " "\r
+                    + profile.getNumOfWrites());\r
+        }\r
+        System.out.println();\r
+\r
+        System.out.println("----- Processnetwork Report -----");\r
+        System.out.println("<process name> <num of activations>");\r
+\r
+        HashMap<String, ProcessProfile> processProfiles =\r
+            profileParser.getProcessProfiles();\r
+        iterator = processProfiles.keySet().iterator();\r
+        while (iterator.hasNext()) {\r
+            ProcessProfile profile = processProfiles.get(iterator.next());\r
+            System.out.println( "<" + profile.getName()\r
+                    + "> " + profile.getNumOfFires());\r
+        }\r
+        System.out.println();\r
+\r
+        /*\r
+        //another test\r
+        PNXmlParser parserPN = new PNXmlParser();\r
+        ProcessNetwork pn = parserPN.doParse(args[1]);\r
+        Profiler p = new Profiler();\r
+        p.profilePN(args[0], pn);\r
+        pn.accept(new PNXmlVisitor("processnetwork.xml"));\r
+        */\r
+    }\r
+\r
+    protected XmlTag _xt = XmlTag.getInstance();\r
+}\r