dol: initial dol commit
[jump.git] / dol / src / dol / helper / profiler / Profiler.java
1 /* $Id: Profiler.java 203 2010-10-11 08:59:47Z dchokshi $ */\r
2 package dol.helper.profiler;\r
3 \r
4 import java.util.HashMap;\r
5 import java.util.Iterator;\r
6 import java.util.Vector;\r
7 \r
8 import dol.datamodel.XmlTag;\r
9 import dol.datamodel.pn.Channel;\r
10 import dol.datamodel.pn.Port;\r
11 import dol.datamodel.pn.Process;\r
12 import dol.datamodel.pn.ProcessNetwork;\r
13 import dol.datamodel.pn.ProfilingConfiguration;\r
14 \r
15 \r
16 /**\r
17  * DOL profiler main class. Reads in profile data and generates the\r
18  * profiling information.\r
19  */\r
20 public class Profiler {\r
21 \r
22     /**\r
23      * Constructor\r
24      */\r
25     public Profiler() {\r
26     }\r
27 \r
28     /**\r
29      * @param name Name of the trace file\r
30      * @param pn The process network need to be annotated\r
31      */\r
32     public void profilePN(String name, ProcessNetwork pn) {\r
33         ProfileParser profileParser = new ProfileParser(name);\r
34         profileParser.parseProfile();\r
35 \r
36         HashMap<String, ChannelProfile> channelProfiles =\r
37             profileParser.getChannelProfiles();\r
38         Iterator<String> iterator = channelProfiles.keySet().iterator();\r
39 \r
40         while (iterator.hasNext()) {\r
41             ChannelProfile profile = channelProfiles.get(iterator.next());\r
42             Channel channel = pn.getChannel(profile.getName());\r
43             ProfilingConfiguration c = channel.getProfilingCfg(\r
44                     _xt.getProfilingTotalReadData());\r
45             if (c == null) {\r
46                 //no profiling present, add new one\r
47                 c = new ProfilingConfiguration(\r
48                         _xt.getProfilingTotalReadData());\r
49                 c.setValue(Long.toString(profile.getTotalReadData()));\r
50                 channel.getProfilingList().add(c);\r
51             } else {\r
52                 // profiling already present, extend existing one\r
53                 Long curr = Long.decode(c.getValue());\r
54                 curr += profile.getTotalReadData();\r
55                 c.setValue(Long.toString(curr));\r
56             }\r
57 \r
58             c = channel.getProfilingCfg(_xt.getProfilingNumOfReads());\r
59             if (c == null) {\r
60                 c = new ProfilingConfiguration(\r
61                         _xt.getProfilingNumOfReads());\r
62                 c.setValue(Integer.toString(profile.getNumOfReads()));\r
63                 channel.getProfilingList().add(c);\r
64             } else {\r
65                 Long curr = Long.decode(c.getValue());\r
66                 curr += profile.getNumOfReads();\r
67                 c.setValue(Long.toString(curr));\r
68             }\r
69 \r
70             c = channel.getProfilingCfg(_xt.getProfilingNumOfWrites());\r
71             if (c == null) {\r
72                 c = new ProfilingConfiguration(\r
73                         _xt.getProfilingNumOfWrites());\r
74                 c.setValue(Integer.toString(profile.getNumOfWrites()));\r
75                 channel.getProfilingList().add(c);\r
76             } else {\r
77                 Long curr = Long.decode(c.getValue());\r
78                 curr += profile.getNumOfWrites();\r
79                 c.setValue( Long.toString(curr) );\r
80             }\r
81         }\r
82 \r
83         HashMap<String, ProcessProfile> processProfiles =\r
84             profileParser.getProcessProfiles();\r
85         iterator = processProfiles.keySet().iterator();\r
86 \r
87         while (iterator.hasNext()) {\r
88             ProcessProfile profile = processProfiles.get(iterator.next());\r
89             Process process = pn.getProcess(profile.getName());\r
90             ProfilingConfiguration c = process.getProfilingCfg(\r
91                     _xt.getProfilingNumOfFires());\r
92 \r
93             if (c == null) {\r
94                 c = new ProfilingConfiguration(\r
95                         _xt.getProfilingNumOfFires());\r
96                 c.setValue(Integer.toString(profile.getNumOfFires()));\r
97                 process.getProfilingList().add(c);\r
98             } else {\r
99                 int curr = Integer.valueOf(c.getValue());\r
100                 curr += profile.getNumOfFires();\r
101                 c.setValue(Integer.toString(curr));\r
102             }\r
103 \r
104             HashMap<String, PortProfile> portProfiles =\r
105                 profile.getPortProfiles();\r
106             Iterator<String> iteratorB = portProfiles.keySet().iterator();\r
107 \r
108             while (iteratorB.hasNext()) {\r
109                 PortProfile portProfile =\r
110                     portProfiles.get(iteratorB.next());\r
111                 String portName = getPNPortName(portProfile.getName(), profileParser, pn);\r
112                 c = new ProfilingConfiguration(portName + ".accesses");\r
113                 c.setValue(portProfile.getAccesses().toString());\r
114                 process.getProfilingList().add(c);\r
115                 c = new ProfilingConfiguration(portName + ".tokensize");\r
116                 c.setValue(portProfile.getTokenSize().toString());\r
117                 process.getProfilingList().add(c);\r
118                 c = new ProfilingConfiguration(portName + ".initialAccesses");\r
119                 c.setValue(Integer.toString(portProfile.getInitialAccesses()));\r
120                 process.getProfilingList().add(c);\r
121                 c = new ProfilingConfiguration(portName + ".initialtokensize");\r
122                 c.setValue(portProfile.getInitialTokenSize().toString());\r
123                 process.getProfilingList().add(c);\r
124             }\r
125         }\r
126     }\r
127 \r
128     /**\r
129      *\r
130      */\r
131     protected String getPNPortName(String profilePortName,\r
132             ProfileParser parser, ProcessNetwork pn) {\r
133         Vector<Port> ports = pn.getChannel(parser.\r
134                 getChannel(profilePortName)).getPortList();\r
135         for (int i = 0; i < ports.size(); i++) {\r
136             if (parser.getPortType(profilePortName).equals("INPUT") &&\r
137                     ports.elementAt(i).isOutPort()) {\r
138                     return ports.elementAt(i).getPeerPort().getName();\r
139             } else if (parser.getPortType(profilePortName).equals("OUTPUT") &&\r
140                     ports.elementAt(i).isInPort()) {\r
141                     return ports.elementAt(i).getPeerPort().getName();\r
142             }\r
143         }\r
144         return null;\r
145     }\r
146 \r
147     /**\r
148      * Main function\r
149      *\r
150      * @param args command line arguments. args[0] is the filename of\r
151      * the profile file. args[1] is the filename of the process network\r
152      * XML file.\r
153      */\r
154     public static void main(String[] args) {\r
155         //check command line parameters\r
156         if (args.length != 2) {\r
157             System.out.println("Usage: Profiler <trace> <pn>");\r
158             System.out.println();\r
159             return;\r
160         }\r
161 \r
162         ProfileParser profileParser = new ProfileParser(args[0]);\r
163 \r
164         //parse input file\r
165         profileParser.parseProfile();\r
166 \r
167         //print the results\r
168         System.out.println("----- Channel Analyzer Report -----");\r
169         System.out.println("<channel name> <maximum fill level> "\r
170                 + "<total amount of transferred data> "\r
171                 + "<num of reads> <num of writes>");\r
172 \r
173         HashMap<String, ChannelProfile> channelProfiles =\r
174             profileParser.getChannelProfiles();\r
175         Iterator<String> iterator = channelProfiles.keySet().iterator();\r
176 \r
177         while (iterator.hasNext()) {\r
178             ChannelProfile profile = channelProfiles.get(iterator.next());\r
179             System.out.println( "<" + profile.getName() +\r
180                     "> "\r
181                     + profile.getMaxFillLevel() + " "\r
182                     + profile.getTotalReadData() + " "\r
183                     + profile.getNumOfReads() + " "\r
184                     + profile.getNumOfWrites());\r
185         }\r
186         System.out.println();\r
187 \r
188         System.out.println("----- Processnetwork Report -----");\r
189         System.out.println("<process name> <num of activations>");\r
190 \r
191         HashMap<String, ProcessProfile> processProfiles =\r
192             profileParser.getProcessProfiles();\r
193         iterator = processProfiles.keySet().iterator();\r
194         while (iterator.hasNext()) {\r
195             ProcessProfile profile = processProfiles.get(iterator.next());\r
196             System.out.println( "<" + profile.getName()\r
197                     + "> " + profile.getNumOfFires());\r
198         }\r
199         System.out.println();\r
200 \r
201         /*\r
202         //another test\r
203         PNXmlParser parserPN = new PNXmlParser();\r
204         ProcessNetwork pn = parserPN.doParse(args[1]);\r
205         Profiler p = new Profiler();\r
206         p.profilePN(args[0], pn);\r
207         pn.accept(new PNXmlVisitor("processnetwork.xml"));\r
208         */\r
209     }\r
210 \r
211     protected XmlTag _xt = XmlTag.getInstance();\r
212 }\r