Guest User

Untitled

a guest
Mar 31st, 2011
143
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************************************************************************
  2. * Copyright (c) 2004, 2008, 2009 Red Hat, Inc.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Keith Seitz <keiths@redhat.com> - initial API and implementation
  10. * Kent Sebastian <ksebasti@redhat.com> -
  11. *******************************************************************************/
  12. package org.eclipse.linuxtools.oprofile.core.linux;
  13.  
  14. import java.io.BufferedReader;
  15. import java.io.BufferedWriter;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.File;
  18. import java.io.FileReader;
  19. import java.io.FileWriter;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.InputStreamReader;
  23. import java.io.UnsupportedEncodingException;
  24.  
  25. import javax.xml.parsers.DocumentBuilder;
  26. import javax.xml.parsers.DocumentBuilderFactory;
  27. import javax.xml.parsers.ParserConfigurationException;
  28. import javax.xml.parsers.SAXParserFactory;
  29.  
  30. import org.eclipse.linuxtools.oprofile.core.OprofileCorePlugin;
  31. import org.eclipse.linuxtools.oprofile.core.OpxmlException;
  32. import org.eclipse.linuxtools.oprofile.core.opxml.AbstractDataAdapter;
  33. import org.eclipse.linuxtools.oprofile.core.opxml.OprofileSAXHandler;
  34. import org.eclipse.linuxtools.oprofile.core.opxml.XMLProcessor;
  35. import org.eclipse.linuxtools.oprofile.core.opxml.checkevent.CheckEventAdapter;
  36. import org.eclipse.linuxtools.oprofile.core.opxml.info.InfoAdapter;
  37. import org.eclipse.linuxtools.oprofile.core.opxml.modeldata.ModelDataAdapter;
  38. import org.eclipse.linuxtools.oprofile.core.opxml.sessions.SessionManager;
  39. import org.w3c.dom.Document;
  40. import org.w3c.dom.Element;
  41. import org.w3c.dom.NodeList;
  42. import org.xml.sax.InputSource;
  43. import org.xml.sax.SAXException;
  44. import org.xml.sax.XMLReader;
  45.  
  46. /**
  47. * This class will run opxml.
  48. *
  49. * opxml is a small program which acts as a textual interface between Oprofile and
  50. * BFD and the oprofile plugins.
  51. */
  52. public class OpxmlRunner {
  53. private OprofileSAXHandler _handler;
  54.  
  55. /**
  56. * Returns the current XMLProcessor handling parsing of opxml output.
  57. * @return the processor
  58. */
  59. public XMLProcessor getProcessor() {
  60. return _handler.getProcessor();
  61. }
  62.  
  63. /**
  64. * Runs opxml with the given arguments.
  65. * @param args the arguments to pass to opxml
  66. * @param callData any callData to pass to the processor
  67. * @return boolean indicating the success/failure of opxml
  68. * @throws OpxmlException
  69. */
  70. public boolean run(String[] args, Object callData) {
  71. XMLReader reader = null;
  72. _handler = OprofileSAXHandler.getInstance(callData);
  73.  
  74. // Create XMLReader
  75. SAXParserFactory factory = SAXParserFactory.newInstance();
  76. try {
  77. reader = factory.newSAXParser().getXMLReader();
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. return false;
  81. }
  82.  
  83. // Set content/error handlers
  84. reader.setContentHandler(_handler);
  85. reader.setErrorHandler(_handler);
  86.  
  87. // Run opxml
  88. try {
  89. File file = constructFile(args);
  90.  
  91. //handle the opxml_session file
  92. if (args[0].equals(SessionManager.SESSIONS)){
  93. SessionManager sessManNew = new SessionManager(SessionManager.SESSION_LOCATION);
  94. populateWithCurrentSession(sessManNew);
  95. sessManNew.write();
  96. FileReader fr = new FileReader(file);
  97. reader.parse(new InputSource(fr));
  98. // file has not been saved
  99. } else if (! file.exists()){
  100. AbstractDataAdapter aea;
  101. if (args[0].equals(CheckEventAdapter.CHECK_EVENTS)){
  102. aea = new CheckEventAdapter(args[1], args[2], args[3]);
  103. aea.process();
  104. BufferedReader bi = new BufferedReader(new InputStreamReader(aea.getInputStream()));
  105. reader.parse(new InputSource(bi));
  106. }else if (args[0].equals(InfoAdapter.INFO)){
  107. aea = new InfoAdapter();
  108. aea.process();
  109. BufferedReader bi = new BufferedReader(new InputStreamReader(aea.getInputStream()));
  110. reader.parse(new InputSource(bi));
  111. }else if (args[0].equals(ModelDataAdapter.MODEL_DATA)){
  112. // this should only happen initially when the current session
  113. // has not been generated
  114. if (! handleModelData(args)){
  115. return false;
  116. }
  117. FileReader fr = new FileReader(file);
  118. reader.parse(new InputSource(fr));
  119. }else{
  120. throw new RuntimeException("Unrecognized argument encountered");
  121. }
  122. }else{
  123. // always regenerate the 'current' session file
  124. if (args.length == 3
  125. && args[0].equals(SessionManager.MODEL_DATA)
  126. && args[2].equals(SessionManager.CURRENT)){
  127. if (! handleModelData(args)){
  128. return false;
  129. }
  130. }
  131. FileReader fr = new FileReader(file);
  132. reader.parse(new InputSource(fr));
  133. }
  134.  
  135. return true;
  136. } catch (SAXException e) {
  137. e.printStackTrace();
  138. OprofileCorePlugin.showErrorDialog("opxmlSAXParseException", null); //$NON-NLS-1$
  139. } catch (IOException e) {
  140. e.printStackTrace();
  141. OprofileCorePlugin.showErrorDialog("opxmlParse", null); //$NON-NLS-1$
  142. }
  143. return false;
  144. }
  145.  
  146. private File saveOpxmlToFile(BufferedReader bi, String [] args) {
  147. String fileName = "";
  148. for (int i = 0; i < args.length; i++){
  149. fileName += args[i];
  150. }
  151. File file = new File(SessionManager.OPXML_PREFIX + fileName);
  152. String line;
  153. try {
  154. file.createNewFile();
  155. BufferedWriter bw = new BufferedWriter(new FileWriter(file));
  156. while ((line = bi.readLine()) != null){
  157. bw.write(line + "\n");
  158. }
  159. bi.close();
  160. bw.close();
  161. } catch (IOException e) {
  162. e.printStackTrace();
  163. }
  164. return file;
  165. }
  166.  
  167. private File constructFile(String [] args){
  168. String fileName = "";
  169. for (int i = 0; i < args.length; i++){
  170. fileName += args[i];
  171. }
  172. return new File (SessionManager.OPXML_PREFIX + fileName);
  173. }
  174.  
  175. private boolean handleModelData (String [] args){
  176. Process p;
  177. try {
  178. String cmd[] = {"opreport", "-X", "--details", "event:" + args[1]};
  179. //p = Runtime.getRuntime().exec("opreport -X --details event:" + args[1]); //$NON-NLS-1$
  180. p = Runtime.getRuntime().exec(cmd); //$NON-NLS-1$
  181.  
  182. StringBuilder output = new StringBuilder();
  183. String s = null;
  184. try {
  185. BufferedReader stdInput = new BufferedReader(new InputStreamReader(
  186. p.getInputStream()));
  187. BufferedReader stdError = new BufferedReader(new InputStreamReader(
  188. p.getErrorStream()));
  189. try {
  190. // read the output
  191. while ((s = stdInput.readLine()) != null) {
  192. output.append(s + System.getProperty("line.separator"));
  193. // System.out.println(s);
  194. }
  195. // print out any errors
  196. while ((s = stdError.readLine()) != null) {
  197. System.out.println(s);
  198. }
  199. } finally {
  200. stdInput.close();
  201. stdError.close();
  202. }
  203. } catch (IOException e) {
  204. e.printStackTrace();
  205. }
  206.  
  207.  
  208. // convert the stream to inputstream to pass to builder.parse
  209. InputStream is = null;
  210. try {
  211. is = new ByteArrayInputStream(output.toString().getBytes("UTF-8"));
  212. } catch (UnsupportedEncodingException e) {
  213. e.printStackTrace();
  214. }
  215.  
  216. if (p.waitFor() != 0){
  217. return false;
  218. }
  219. ModelDataAdapter mda = new ModelDataAdapter(is);
  220. if (! mda.isParseable()){
  221. return false;
  222. }
  223. mda.process();
  224. BufferedReader bi = new BufferedReader(new InputStreamReader(mda.getInputStream()));
  225. saveOpxmlToFile(bi, args);
  226. } catch (IOException e) {
  227. e.printStackTrace();
  228. OprofileCorePlugin.showErrorDialog("opxmlParse", null); //$NON-NLS-1$
  229. } catch (InterruptedException e) {
  230. e.printStackTrace();
  231. }
  232. return true;
  233. }
  234.  
  235. /**
  236. * Add the current session to the session manager for each event
  237. * that it was profiled under.
  238. * @param session the session manager to populate
  239. */
  240. private void populateWithCurrentSession (SessionManager session){
  241. session.removeAllCurrentSessions();
  242. String[] eventName = getEventNames();
  243. if (eventName != null) {
  244. for (int i = 0; i < eventName.length; i++) {
  245. session.addSession(SessionManager.CURRENT, eventName[i]);
  246. }
  247. }
  248. }
  249.  
  250. /**
  251. * @return the name of the events used on the current session
  252. */
  253. /*
  254. private String[] getEventNames () {
  255. String [] ret = null;
  256. try {
  257. System.out.println("himom");
  258. Process p = Runtime.getRuntime().exec("opreport -X --details");
  259. InputStream is = p.getInputStream();
  260. if (p.waitFor() == 0){
  261. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  262. DocumentBuilder builder;
  263. builder = factory.newDocumentBuilder();
  264. Document doc = builder.parse(is);
  265. Element root = (Element) doc.getElementsByTagName(ModelDataAdapter.PROFILE).item(0);
  266.  
  267. Element setupTag = (Element) root.getElementsByTagName(ModelDataAdapter.SETUP).item(0);
  268. NodeList eventSetupList = setupTag.getElementsByTagName(ModelDataAdapter.EVENT_SETUP);
  269.  
  270. // get the event names for the current session
  271. ret = new String[eventSetupList.getLength()];
  272. for (int i = 0; i < eventSetupList.getLength(); i++) {
  273. Element elm = (Element) eventSetupList.item(i);
  274. ret[i] = elm.getAttribute(ModelDataAdapter.EVENT_NAME);
  275. }
  276. }
  277. } catch (IOException e) {
  278. e.printStackTrace();
  279. OprofileCorePlugin.showErrorDialog("opxmlParse", null); //$NON-NLS-1$
  280. } catch (ParserConfigurationException e) {
  281. e.printStackTrace();
  282. } catch (SAXException e) {
  283. e.printStackTrace();
  284. OprofileCorePlugin.showErrorDialog("opxmlSAXParseException", null); //$NON-NLS-1$
  285. } catch (InterruptedException e) {
  286. e.printStackTrace();
  287. }
  288. System.out.println("returning");
  289. return ret;
  290. } */
  291.  
  292.  
  293. private String[] getEventNames (){
  294. String [] ret = null;
  295. try {
  296. String cmd[] = {"opreport", "-X", "-d"};
  297. Process p = Runtime.getRuntime().exec(cmd);
  298.  
  299. StringBuilder output = new StringBuilder();
  300. String s = null;
  301. try {
  302. BufferedReader stdInput = new BufferedReader(new InputStreamReader(
  303. p.getInputStream()));
  304. BufferedReader stdError = new BufferedReader(new InputStreamReader(
  305. p.getErrorStream()));
  306. try {
  307. // read the output
  308. while ((s = stdInput.readLine()) != null) {
  309. output.append(s + System.getProperty("line.separator"));
  310. // System.out.println(s);
  311. }
  312. // print out any errors
  313. while ((s = stdError.readLine()) != null) {
  314. System.out.println(s);
  315. }
  316. } finally {
  317. stdInput.close();
  318. stdError.close();
  319. }
  320. } catch (IOException e) {
  321. e.printStackTrace();
  322. }
  323.  
  324.  
  325. // convert the stream to inputstream to pass to builder.parse
  326. InputStream is = null;
  327. try {
  328. is = new ByteArrayInputStream(output.toString().getBytes("UTF-8"));
  329. } catch (UnsupportedEncodingException e) {
  330. e.printStackTrace();
  331. }
  332.  
  333. if (p.waitFor() == 0){
  334. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  335. DocumentBuilder builder;
  336. builder = factory.newDocumentBuilder();
  337. Document doc = builder.parse(is);
  338. Element root = (Element) doc.getElementsByTagName(ModelDataAdapter.PROFILE).item(0);
  339.  
  340. Element setupTag = (Element) root.getElementsByTagName(ModelDataAdapter.SETUP).item(0);
  341. NodeList eventSetupList = setupTag.getElementsByTagName(ModelDataAdapter.EVENT_SETUP);
  342.  
  343. // get the event names for the current session
  344. ret = new String[eventSetupList.getLength()];
  345. for (int i = 0; i < eventSetupList.getLength(); i++) {
  346. Element elm = (Element) eventSetupList.item(i);
  347. ret[i] = elm.getAttribute(ModelDataAdapter.EVENT_NAME);
  348. }
  349. }
  350. } catch (IOException e) {
  351. e.printStackTrace();
  352. OprofileCorePlugin.showErrorDialog("opxmlParse", null); //$NON-NLS-1$
  353. } catch (ParserConfigurationException e) {
  354. e.printStackTrace();
  355. } catch (SAXException e) {
  356. e.printStackTrace();
  357. OprofileCorePlugin.showErrorDialog("opxmlSAXParseException", null); //$NON-NLS-1$
  358. } catch (InterruptedException e) {
  359. e.printStackTrace();
  360. }
  361.  
  362. return ret;
  363. }
  364.  
  365. }
RAW Paste Data