+/* $Id: PNProfileSummarizer.java 1 2010-02-24 13:03:05Z haidw $ */\r
+package dol.helper.profiler;\r
+\r
+import java.util.HashMap;\r
+import java.util.StringTokenizer;\r
+\r
+import dol.datamodel.pn.Process;\r
+import dol.datamodel.pn.ProcessNetwork;\r
+import dol.datamodel.pn.ProfilingConfiguration;\r
+import dol.parser.xml.pnschema.PNXmlParser;\r
+import dol.visitor.xml.PNXmlVisitor;\r
+\r
+/**\r
+ * Class to post-process profiling info in a process network XML file.\r
+ */\r
+public class PNProfileSummarizer {\r
+ /**\r
+ * Add up all WCED, BCED, and ACED numbers for each process in the\r
+ * given process network file. The computed numbers are back-annotated\r
+ * to a new file with the suffix _summary.\r
+ *\r
+ * @param filename filename of annotated process network XML file\r
+ */\r
+ public static void sumDemands(String filename) {\r
+ HashMap<String, ProfilingConfiguration> configs =\r
+ new HashMap<String, ProfilingConfiguration>();\r
+\r
+ //load process network specification from XML\r
+ PNXmlParser parserPn = new PNXmlParser();\r
+ ProcessNetwork pn = parserPn.doParse(filename);\r
+\r
+ for (Process process : pn.getProcessList()) {\r
+ //System.out.println(process.getName());\r
+ for (ProfilingConfiguration cfg :\r
+ process.getProfilingList()) {\r
+ StringTokenizer tokenizer =\r
+ new StringTokenizer(cfg.getName(), " ");\r
+ //System.out.println(cfg.getName());\r
+ if (tokenizer.countTokens() < 2) {\r
+ continue;\r
+ }\r
+\r
+ String demandId = tokenizer.nextToken();\r
+ String processorName = tokenizer.nextToken();\r
+ String key = process.getName() + " " + demandId + " "\r
+ + processorName + " sum";\r
+ if (configs.get(key) == null) {\r
+ ProfilingConfiguration config =\r
+ new ProfilingConfiguration(demandId + " "\r
+ + processorName + " sum");\r
+ config.setValue("0");\r
+ configs.put(key, config);\r
+ }\r
+ ProfilingConfiguration currentCfg = configs.get(key);\r
+ /*\r
+ System.out.print(currentCfg.getName() + ": "\r
+ + currentCfg.getValue() + " / "\r
+ + cfg.getName() + ": "\r
+ + cfg.getValue());\r
+ */\r
+ currentCfg.setValue(\r
+ Long.toString(Long.parseLong(\r
+ currentCfg.getValue())\r
+ + Long.parseLong(cfg.getValue())));\r
+ /*\r
+ System.out.println(currentCfg.getName() + ": "\r
+ + currentCfg.getValue());\r
+ */\r
+ }\r
+ }\r
+\r
+ for(String key : configs.keySet()) {\r
+ ProfilingConfiguration value = configs.get(key);\r
+ String process = key.substring(0, key.indexOf(" "));\r
+ pn.getProcess(process).getProfilingList().add(value);\r
+ }\r
+\r
+ System.out.println("Write process network XML file");\r
+ StringBuffer buffer = new StringBuffer();\r
+ pn.accept(new PNXmlVisitor(buffer));\r
+ try {\r
+ java.io.BufferedWriter writer =\r
+ new java.io.BufferedWriter(\r
+ new java.io.FileWriter(\r
+ filename.replaceAll(".xml", "_summary.xml")));\r
+ writer.write(buffer.toString());\r
+ writer.close();\r
+ } catch (java.io.IOException e) {\r
+ System.out.println("Caught an exception while "\r
+ + "creating XML file: " + e.getMessage());\r
+ e.printStackTrace(System.out);\r
+ }\r
+ System.out.println(" -- Process network XML file [Finished]");\r
+ }\r
+\r
+\r
+ /**\r
+ * Run summarize operations on annotated process network.\r
+ *\r
+ * @param args filename of annotated process network XML file\r
+ */\r
+ public static void main(String args[]) throws Exception {\r
+ PNProfileSummarizer.sumDemands(args[0]);\r
+ }\r
+}\r