summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorCamilo Bernal2013-01-04 19:04:16 (EST)
committer Alexander Kurtakov2013-01-07 12:34:15 (EST)
commit16edc98e51a9687cd05a3e0c6049bd383b53d06c (patch)
treefe794a6edb12282549897fde2ece182fcc80d9ae
parenta31ec49f3cb77be56402dcd9e614721013534ef9 (diff)
downloadorg.eclipse.linuxtools-16edc98e51a9687cd05a3e0c6049bd383b53d06c.zip
org.eclipse.linuxtools-16edc98e51a9687cd05a3e0c6049bd383b53d06c.tar.gz
org.eclipse.linuxtools-16edc98e51a9687cd05a3e0c6049bd383b53d06c.tar.bz2
Extract opreport execution functionality in OpxmlRunner.refs/changes/76/9476/4
Isolated duplicate code dealing with execution of the opreport tool. Change-Id: Ic89e3fe8d6fd2d3c5e9806eb453610f885d8b756 Reviewed-on: https://git.eclipse.org/r/9476 Tested-by: Hudson CI Reviewed-by: Alexander Kurtakov <akurtako@redhat.com> IP-Clean: Alexander Kurtakov <akurtako@redhat.com> Tested-by: Alexander Kurtakov <akurtako@redhat.com>
-rw-r--r--oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/OpxmlRunner.java206
1 files changed, 89 insertions, 117 deletions
diff --git a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/OpxmlRunner.java b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/OpxmlRunner.java
index 680bb9a..a12c20d 100644
--- a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/OpxmlRunner.java
+++ b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/OpxmlRunner.java
@@ -22,6 +22,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
+import java.util.Collections;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -183,77 +184,24 @@ public class OpxmlRunner {
}
private boolean handleModelData (String [] args){
- Process p;
- try {
- ArrayList<String> cmd = new ArrayList<String>();
- cmd.add("opreport"); //$NON-NLS-1$
- cmd.add("-Xdg"); //$NON-NLS-1$
- if (!InfoAdapter.hasTimerSupport()){
- cmd.add("event:" + args[1]); //$NON-NLS-1$
- }
- String [] a = {};
- p = RuntimeProcessFactory.getFactory().exec(cmd.toArray(a), Oprofile.OprofileProject.getProject());
-
- StringBuilder output = new StringBuilder();
- StringBuilder errorOutput = new StringBuilder();
- String s = null;
- try {
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(
- p.getInputStream()));
- BufferedReader stdError = new BufferedReader(new InputStreamReader(
- p.getErrorStream()));
- try {
- // Read output of opreport. We need to do this, since this might
- // cause the plug-in to hang. See Eclipse bug 341621 for more info.
- // FIXME: Both of those while loops should really be done in two separate
- // threads, so that we avoid this very problem when the error input
- // stream buffer fills up.
- while ((s = stdInput.readLine()) != null) {
- output.append(s + System.getProperty("line.separator")); //$NON-NLS-1$
- }
- while ((s = stdError.readLine()) != null) {
- errorOutput.append(s + System.getProperty("line.separator")); //$NON-NLS-1$
- }
- } finally {
- stdInput.close();
- stdError.close();
- }
- if (!errorOutput.toString().trim().equals("")) { //$NON-NLS-1$
- OprofileCorePlugin
- .log(IStatus.ERROR,
- NLS.bind(
- OprofileProperties
- .getString("process.log.stderr"), "opreport", errorOutput.toString().trim())); //$NON-NLS-1$ //$NON-NLS-2$
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- // convert the string to inputstream to pass
- InputStream is = null;
- try {
- is = new ByteArrayInputStream(output.toString().getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
+ ArrayList<String> cmd = new ArrayList<String>();
+ cmd.add("-Xdg"); //$NON-NLS-1$
+ if (!InfoAdapter.hasTimerSupport()){
+ cmd.add("event:" + args[1]); //$NON-NLS-1$
+ }
+ String [] a = {};
+ InputStream is = runOpReport(cmd.toArray(a));
- if (p.waitFor() != 0){
- return false;
- }
- ModelDataAdapter mda = new ModelDataAdapter(is);
- if (! mda.isParseable()){
- return false;
- }
- mda.process();
- BufferedReader bi = new BufferedReader(new InputStreamReader(mda.getInputStream()));
- saveOpxmlToFile(bi, args);
- } catch (IOException e) {
- e.printStackTrace();
- OprofileCorePlugin.showErrorDialog("opxmlParse", null); //$NON-NLS-1$
- } catch (InterruptedException e) {
- e.printStackTrace();
+ if (is == null){
+ return false;
+ }
+ ModelDataAdapter mda = new ModelDataAdapter(is);
+ if (! mda.isParseable()){
+ return false;
}
+ mda.process();
+ BufferedReader bi = new BufferedReader(new InputStreamReader(mda.getInputStream()));
+ saveOpxmlToFile(bi, args);
return true;
}
@@ -275,54 +223,10 @@ public class OpxmlRunner {
private String[] getEventNames (){
String [] ret = null;
try {
- String cmd[] = {"opreport", "-X", "-d"};
+ String cmd[] = {"-X", "-d"};
+ InputStream is = runOpReport(cmd);
- Process p = RuntimeProcessFactory.getFactory().exec(cmd, Oprofile.OprofileProject.getProject());
- StringBuilder output = new StringBuilder();
- StringBuilder errorOutput = new StringBuilder();
- String s = null;
- try {
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(
- p.getInputStream()));
- BufferedReader stdError = new BufferedReader(new InputStreamReader(
- p.getErrorStream()));
- try {
- // Read output of opreport. We need to do this, since this might
- // cause the plug-in to hang. See Eclipse bug 341621 for more info.
- // FIXME: Both of those while loops should really be done in two separate
- // threads, so that we avoid this very problem when the error input
- // stream buffer fills up.
- while ((s = stdInput.readLine()) != null) {
- output.append(s + System.getProperty("line.separator")); //$NON-NLS-1$
- }
- while ((s = stdError.readLine()) != null) {
- errorOutput.append(s + System.getProperty("line.separator")); //$NON-NLS-1$
- }
- } finally {
- stdInput.close();
- stdError.close();
- }
- if (!errorOutput.toString().trim().equals("")) { //$NON-NLS-1$
- OprofileCorePlugin
- .log(IStatus.ERROR,
- NLS.bind(
- OprofileProperties
- .getString("process.log.stderr"), "opreport", errorOutput.toString().trim())); //$NON-NLS-1$ //$NON-NLS-2$
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- // convert the string to inputstream to pass to builder.parse
- InputStream is = null;
- try {
- is = new ByteArrayInputStream(output.toString().getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
-
- if (p.waitFor() == 0){
+ if (is != null){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
@@ -359,9 +263,77 @@ public class OpxmlRunner {
} catch (SAXException e) {
e.printStackTrace();
OprofileCorePlugin.showErrorDialog("opxmlSAXParseException", null); //$NON-NLS-1$
+ }
+ return ret;
+ }
+
+ /**
+ * Run opreport with specified arguments <code>args</code> and
+ * return InputStream to output of report for parsing.
+ *
+ * @param args arguments to run with opreport
+ * @return InputStream to output of report
+ */
+ private InputStream runOpReport(String[] args){
+
+ ArrayList<String> cmd = new ArrayList<String>();
+ cmd.add("opreport"); //$NON-NLS-1$
+ Collections.addAll(cmd, args);
+
+ Process p = null;
+ try {
+ p = RuntimeProcessFactory.getFactory().exec(cmd.toArray(new String[0]), Oprofile.OprofileProject.getProject());
+
+ StringBuilder output = new StringBuilder();
+ StringBuilder errorOutput = new StringBuilder();
+ String s = null;
+ try {
+ BufferedReader stdInput = new BufferedReader(new InputStreamReader(
+ p.getInputStream()));
+ BufferedReader stdError = new BufferedReader(new InputStreamReader(
+ p.getErrorStream()));
+ try {
+ // Read output of opreport. We need to do this, since this might
+ // cause the plug-in to hang. See Eclipse bug 341621 for more info.
+ // FIXME: Both of those while loops should really be done in two separate
+ // threads, so that we avoid this very problem when the error input
+ // stream buffer fills up.
+ while ((s = stdInput.readLine()) != null) {
+ output.append(s + System.getProperty("line.separator")); //$NON-NLS-1$
+ }
+ while ((s = stdError.readLine()) != null) {
+ errorOutput.append(s + System.getProperty("line.separator")); //$NON-NLS-1$
+ }
+ } finally {
+ stdInput.close();
+ stdError.close();
+ }
+ if (!errorOutput.toString().trim().equals("")) { //$NON-NLS-1$
+ OprofileCorePlugin
+ .log(IStatus.ERROR,
+ NLS.bind(
+ OprofileProperties
+ .getString("process.log.stderr"), "opreport", errorOutput.toString().trim())); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ if(p.waitFor() == 0){
+ // convert the string to inputstream to pass to builder.parse
+ try {
+ return new ByteArrayInputStream(output.toString().getBytes("UTF-8"));
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ }
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ OprofileCorePlugin.showErrorDialog("opxmlParse", null); //$NON-NLS-1$
} catch (InterruptedException e) {
e.printStackTrace();
}
- return ret;
+
+ return null;
}
}