1 | package test.appProfConverter; |
---|
2 | import java.io.File; |
---|
3 | |
---|
4 | import javax.xml.parsers.DocumentBuilder; |
---|
5 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
6 | import javax.xml.parsers.ParserConfigurationException; |
---|
7 | import javax.xml.transform.Transformer; |
---|
8 | import javax.xml.transform.TransformerException; |
---|
9 | import javax.xml.transform.TransformerFactory; |
---|
10 | import javax.xml.transform.dom.DOMSource; |
---|
11 | import javax.xml.transform.stream.StreamResult; |
---|
12 | |
---|
13 | import org.w3c.dom.Document; |
---|
14 | import org.w3c.dom.Element; |
---|
15 | import org.w3c.dom.Node; |
---|
16 | import org.w3c.dom.NodeList; |
---|
17 | import org.xml.sax.SAXException; |
---|
18 | import org.xml.sax.SAXParseException; |
---|
19 | |
---|
20 | public class SoftwareProfile { |
---|
21 | |
---|
22 | public SoftwareProfile(String fileName) { |
---|
23 | this.LoadFromXml(fileName); |
---|
24 | } |
---|
25 | |
---|
26 | public SoftwareProfile() { |
---|
27 | this.TaskId = null; |
---|
28 | this.Phases = null; |
---|
29 | } |
---|
30 | |
---|
31 | public String ToString() { |
---|
32 | String str = ""; |
---|
33 | |
---|
34 | str += "Task Id: " + this.TaskId + "\n"; |
---|
35 | for (SoftwarePhase phase : Phases) { |
---|
36 | str += phase.ToString(); |
---|
37 | } |
---|
38 | |
---|
39 | return str; |
---|
40 | } |
---|
41 | |
---|
42 | public void StoreToXml(String fileName) { |
---|
43 | try { |
---|
44 | DocumentBuilderFactory docFactory = DocumentBuilderFactory |
---|
45 | .newInstance(); |
---|
46 | DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); |
---|
47 | |
---|
48 | // root elements |
---|
49 | Document doc = docBuilder.newDocument(); |
---|
50 | Element rootElement = doc.createElement("job"); |
---|
51 | doc.appendChild(rootElement); |
---|
52 | rootElement.setAttribute("xmlns:xsi", |
---|
53 | "http://www.w3.org/2001/XMLSchema-instance"); |
---|
54 | rootElement.setAttribute("xsi:noNamespaceSchemaLocation", |
---|
55 | "../QCGJobDescriptionSchemaNew.xsd"); |
---|
56 | |
---|
57 | // staff elements |
---|
58 | Element task = doc.createElement("task"); |
---|
59 | rootElement.appendChild(task); |
---|
60 | |
---|
61 | // set attribute to staff element |
---|
62 | task.setAttribute("id", this.TaskId); |
---|
63 | |
---|
64 | Element execution = doc.createElement("execution"); |
---|
65 | task.appendChild(execution); |
---|
66 | |
---|
67 | Element resourceConsumptionProfile = doc |
---|
68 | .createElement("resourceConsumptionProfile"); |
---|
69 | execution.appendChild(resourceConsumptionProfile); |
---|
70 | |
---|
71 | for (int i = 0; i < this.Phases.length; i++) { |
---|
72 | Element resourceConsumption = doc |
---|
73 | .createElement("resourceConsumption"); |
---|
74 | resourceConsumptionProfile.appendChild(resourceConsumption); |
---|
75 | |
---|
76 | Element referenceHardware = doc |
---|
77 | .createElement("referenceHardware"); |
---|
78 | resourceConsumption.appendChild(referenceHardware); |
---|
79 | |
---|
80 | Element referenceNode = doc.createElement("reference"); |
---|
81 | referenceNode.setAttribute("name", "node"); |
---|
82 | referenceNode.appendChild(doc |
---|
83 | .createTextNode(this.Phases[i].RefNode)); |
---|
84 | referenceHardware.appendChild(referenceNode); |
---|
85 | |
---|
86 | Element referenceCores = doc.createElement("reference"); |
---|
87 | referenceCores.setAttribute("name", "cpu_cores"); |
---|
88 | referenceCores.appendChild(doc.createTextNode(Integer |
---|
89 | .toString(this.Phases[i].RefCores))); |
---|
90 | referenceHardware.appendChild(referenceCores); |
---|
91 | |
---|
92 | Element referenceFreq = doc.createElement("reference"); |
---|
93 | referenceFreq.setAttribute("name", "cpu_maxfreq"); |
---|
94 | referenceFreq.appendChild(doc.createTextNode(Double |
---|
95 | .toString(this.Phases[i].RefFreq))); |
---|
96 | referenceHardware.appendChild(referenceFreq); |
---|
97 | |
---|
98 | Element duration = doc.createElement("duration"); |
---|
99 | resourceConsumption.appendChild(duration); |
---|
100 | duration.appendChild(doc.createTextNode("PT" |
---|
101 | + Integer.toString(this.Phases[i].Duration) + "S")); |
---|
102 | |
---|
103 | Element behaviour, value; |
---|
104 | behaviour = doc.createElement("behaviour"); |
---|
105 | resourceConsumption.appendChild(behaviour); |
---|
106 | behaviour.setAttribute("name", "PM_Power"); |
---|
107 | value = doc.createElement("value"); |
---|
108 | behaviour.appendChild(value); |
---|
109 | value.appendChild(doc.createTextNode(Double |
---|
110 | .toString(this.Phases[i].PM_Power))); |
---|
111 | |
---|
112 | behaviour = doc.createElement("behaviour"); |
---|
113 | resourceConsumption.appendChild(behaviour); |
---|
114 | behaviour.setAttribute("name", "PM_Disk_IO"); |
---|
115 | value = doc.createElement("value"); |
---|
116 | behaviour.appendChild(value); |
---|
117 | value.appendChild(doc.createTextNode(Double |
---|
118 | .toString(this.Phases[i].PM_Disk_IO))); |
---|
119 | |
---|
120 | behaviour = doc.createElement("behaviour"); |
---|
121 | resourceConsumption.appendChild(behaviour); |
---|
122 | behaviour.setAttribute("name", "PM_Memory_RSS"); |
---|
123 | value = doc.createElement("value"); |
---|
124 | behaviour.appendChild(value); |
---|
125 | value.appendChild(doc.createTextNode(Double |
---|
126 | .toString(this.Phases[i].PM_Memory_RSS))); |
---|
127 | |
---|
128 | behaviour = doc.createElement("behaviour"); |
---|
129 | resourceConsumption.appendChild(behaviour); |
---|
130 | behaviour.setAttribute("name", "PM_Memory_Usage"); |
---|
131 | value = doc.createElement("value"); |
---|
132 | behaviour.appendChild(value); |
---|
133 | value.appendChild(doc.createTextNode(Double |
---|
134 | .toString(this.Phases[i].PM_Memory_Usage))); |
---|
135 | |
---|
136 | behaviour = doc.createElement("behaviour"); |
---|
137 | resourceConsumption.appendChild(behaviour); |
---|
138 | behaviour.setAttribute("name", "PM_CPU_Usage"); |
---|
139 | value = doc.createElement("value"); |
---|
140 | behaviour.appendChild(value); |
---|
141 | value.appendChild(doc.createTextNode(Double |
---|
142 | .toString(this.Phases[i].PM_CPU_Usage))); |
---|
143 | |
---|
144 | behaviour = doc.createElement("behaviour"); |
---|
145 | resourceConsumption.appendChild(behaviour); |
---|
146 | behaviour.setAttribute("name", "PM_Threads"); |
---|
147 | value = doc.createElement("value"); |
---|
148 | behaviour.appendChild(value); |
---|
149 | value.appendChild(doc.createTextNode(Integer |
---|
150 | .toString(this.Phases[i].PM_Threads))); |
---|
151 | |
---|
152 | behaviour = doc.createElement("behaviour"); |
---|
153 | resourceConsumption.appendChild(behaviour); |
---|
154 | behaviour.setAttribute("name", "PM_Download"); |
---|
155 | value = doc.createElement("value"); |
---|
156 | behaviour.appendChild(value); |
---|
157 | value.appendChild(doc.createTextNode(Double |
---|
158 | .toString(this.Phases[i].PM_Download))); |
---|
159 | |
---|
160 | behaviour = doc.createElement("behaviour"); |
---|
161 | resourceConsumption.appendChild(behaviour); |
---|
162 | behaviour.setAttribute("name", "PM_Upload"); |
---|
163 | value = doc.createElement("value"); |
---|
164 | behaviour.appendChild(value); |
---|
165 | value.appendChild(doc.createTextNode(Double |
---|
166 | .toString(this.Phases[i].PM_Upload))); |
---|
167 | } |
---|
168 | |
---|
169 | // write the content into xml file |
---|
170 | TransformerFactory transformerFactory = TransformerFactory |
---|
171 | .newInstance(); |
---|
172 | Transformer transformer = transformerFactory.newTransformer(); |
---|
173 | DOMSource source = new DOMSource(doc); |
---|
174 | StreamResult result = new StreamResult(new File(fileName)); |
---|
175 | |
---|
176 | // Output to console for testing |
---|
177 | // StreamResult result = new StreamResult(System.out); |
---|
178 | |
---|
179 | transformer.transform(source, result); |
---|
180 | |
---|
181 | } catch (ParserConfigurationException pce) { |
---|
182 | pce.printStackTrace(); |
---|
183 | } catch (TransformerException tfe) { |
---|
184 | tfe.printStackTrace(); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | public void LoadFromXml(String fileName) { |
---|
189 | try { |
---|
190 | DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory |
---|
191 | .newInstance(); |
---|
192 | DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); |
---|
193 | Document doc = docBuilder.parse(new File(fileName)); |
---|
194 | NodeList nl; |
---|
195 | |
---|
196 | // task id="wget" |
---|
197 | Element e = (Element) doc.getElementsByTagName("task").item(0) |
---|
198 | .getChildNodes(); |
---|
199 | this.TaskId = e.getAttributes().getNamedItem("id").getNodeValue() |
---|
200 | .trim(); |
---|
201 | |
---|
202 | NodeList listOfAppPhases = doc |
---|
203 | .getElementsByTagName("resourceConsumption"); |
---|
204 | |
---|
205 | int nPhases = listOfAppPhases.getLength(); |
---|
206 | Phases = new SoftwarePhase[nPhases]; |
---|
207 | |
---|
208 | for (int p = 0; p < nPhases; p++) { |
---|
209 | Node phaseNode = listOfAppPhases.item(p); |
---|
210 | SoftwarePhase swPhase = new SoftwarePhase(); |
---|
211 | |
---|
212 | if (phaseNode.getNodeType() == Node.ELEMENT_NODE) { |
---|
213 | |
---|
214 | Element phaseElement = (Element) phaseNode; |
---|
215 | |
---|
216 | // Phase Duration |
---|
217 | nl = phaseElement.getElementsByTagName("duration"); |
---|
218 | |
---|
219 | NodeList textList = nl.item(0).getChildNodes(); |
---|
220 | if (textList.item(0).getNodeValue().trim().startsWith("PT")) { |
---|
221 | String str = textList.item(0).getNodeValue().trim(); |
---|
222 | swPhase.Duration = Integer.parseInt(str.substring(2, |
---|
223 | str.length() - 1)); |
---|
224 | if (str.endsWith("S")) { |
---|
225 | ; |
---|
226 | } else if (str.endsWith("M")) { |
---|
227 | swPhase.Duration *= 60; |
---|
228 | } else if (str.endsWith("H")) { |
---|
229 | swPhase.Duration *= 3600; |
---|
230 | } else { |
---|
231 | throw new Exception( |
---|
232 | "Invalid software phase duration."); |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | // Reference Hardware |
---|
237 | nl = phaseElement.getElementsByTagName("reference"); |
---|
238 | for (int r = 0; r < nl.getLength(); r++) { |
---|
239 | String name = nl.item(r).getAttributes() |
---|
240 | .getNamedItem("name").getNodeValue(); |
---|
241 | if (name.equals("node")) { |
---|
242 | swPhase.RefNode = nl.item(r).getChildNodes() |
---|
243 | .item(0).getNodeValue().trim(); |
---|
244 | } |
---|
245 | if (name.equals("cpu_cores")) { |
---|
246 | swPhase.RefCores = Integer.parseInt(nl |
---|
247 | .item(r).getChildNodes().item(0) |
---|
248 | .getNodeValue().trim()); |
---|
249 | } |
---|
250 | if (name.equals("cpu_maxfreq")) { |
---|
251 | swPhase.RefFreq = Double.parseDouble(nl |
---|
252 | .item(r).getChildNodes().item(0) |
---|
253 | .getNodeValue().trim()); |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | // Application Behavior |
---|
258 | nl = phaseElement.getElementsByTagName("behaviour"); |
---|
259 | for (int b = 0; b < nl.getLength(); b++) { |
---|
260 | String name = nl.item(b).getAttributes() |
---|
261 | .getNamedItem("name").getNodeValue(); |
---|
262 | if (name.equals("PM_Power")) { |
---|
263 | NodeList nl2 = nl.item(b).getChildNodes(); |
---|
264 | Element e2 = (Element) nl.item(b); |
---|
265 | nl2 = e2.getElementsByTagName("value"); |
---|
266 | textList = nl2.item(0).getChildNodes(); |
---|
267 | swPhase.PM_Power = Double.parseDouble(textList |
---|
268 | .item(0).getNodeValue().trim()); |
---|
269 | } |
---|
270 | if (name.equals("PM_Disk_IO")) { |
---|
271 | NodeList nl2 = nl.item(b).getChildNodes(); |
---|
272 | Element e2 = (Element) nl.item(b); |
---|
273 | nl2 = e2.getElementsByTagName("value"); |
---|
274 | textList = nl2.item(0).getChildNodes(); |
---|
275 | swPhase.PM_Disk_IO = Double.parseDouble(textList |
---|
276 | .item(0).getNodeValue().trim()); |
---|
277 | } |
---|
278 | if (name.equals("PM_Memory_RSS")) { |
---|
279 | NodeList nl2 = nl.item(b).getChildNodes(); |
---|
280 | Element e2 = (Element) nl.item(b); |
---|
281 | nl2 = e2.getElementsByTagName("value"); |
---|
282 | textList = nl2.item(0).getChildNodes(); |
---|
283 | swPhase.PM_Memory_RSS = Double.parseDouble(textList |
---|
284 | .item(0).getNodeValue().trim()); |
---|
285 | } |
---|
286 | if (name.equals("PM_Memory_Usage")) { |
---|
287 | NodeList nl2 = nl.item(b).getChildNodes(); |
---|
288 | Element e2 = (Element) nl.item(b); |
---|
289 | nl2 = e2.getElementsByTagName("value"); |
---|
290 | textList = nl2.item(0).getChildNodes(); |
---|
291 | swPhase.PM_Memory_Usage = Double |
---|
292 | .parseDouble(textList.item(0) |
---|
293 | .getNodeValue().trim()); |
---|
294 | } |
---|
295 | if (name.equals("PM_CPU_Usage")) { |
---|
296 | NodeList nl2 = nl.item(b).getChildNodes(); |
---|
297 | Element e2 = (Element) nl.item(b); |
---|
298 | nl2 = e2.getElementsByTagName("value"); |
---|
299 | textList = nl2.item(0).getChildNodes(); |
---|
300 | swPhase.PM_CPU_Usage = Double.parseDouble(textList |
---|
301 | .item(0).getNodeValue().trim()); |
---|
302 | } |
---|
303 | if (name.equals("PM_Threads")) { |
---|
304 | NodeList nl2 = nl.item(b).getChildNodes(); |
---|
305 | Element e2 = (Element) nl.item(b); |
---|
306 | nl2 = e2.getElementsByTagName("value"); |
---|
307 | textList = nl2.item(0).getChildNodes(); |
---|
308 | swPhase.PM_Threads = Integer.parseInt(textList |
---|
309 | .item(0).getNodeValue().trim()); |
---|
310 | } |
---|
311 | if (name.equals("PM_Download")) { |
---|
312 | NodeList nl2 = nl.item(b).getChildNodes(); |
---|
313 | Element e2 = (Element) nl.item(b); |
---|
314 | nl2 = e2.getElementsByTagName("value"); |
---|
315 | textList = nl2.item(0).getChildNodes(); |
---|
316 | swPhase.PM_Download = Double.parseDouble(textList |
---|
317 | .item(0).getNodeValue().trim()); |
---|
318 | } |
---|
319 | if (name.equals("PM_Upload")) { |
---|
320 | NodeList nl2 = nl.item(b).getChildNodes(); |
---|
321 | Element e2 = (Element) nl.item(b); |
---|
322 | nl2 = e2.getElementsByTagName("value"); |
---|
323 | textList = nl2.item(0).getChildNodes(); |
---|
324 | swPhase.PM_Upload = Double.parseDouble(textList |
---|
325 | .item(0).getNodeValue().trim()); |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | Phases[p] = swPhase; |
---|
330 | |
---|
331 | }// end of if clause |
---|
332 | |
---|
333 | }// end of for loop with s var |
---|
334 | |
---|
335 | } catch (SAXParseException err) { |
---|
336 | System.out.println("** Parsing error" + ", line " |
---|
337 | + err.getLineNumber() + ", uri " + err.getSystemId()); |
---|
338 | System.out.println(" " + err.getMessage()); |
---|
339 | |
---|
340 | } catch (SAXException e) { |
---|
341 | Exception x = e.getException(); |
---|
342 | ((x == null) ? e : x).printStackTrace(); |
---|
343 | |
---|
344 | } catch (Throwable t) { |
---|
345 | t.printStackTrace(); |
---|
346 | } |
---|
347 | } |
---|
348 | |
---|
349 | public String TaskId; |
---|
350 | public SoftwarePhase[] Phases; |
---|
351 | } |
---|