Add support for LTTng 2.0 command calibrate
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / service / LTTngControlService.java
index 69aea44014d0dbcf0f97dce4ec2aeea6825bdfc3..83c44a081caab83302f858b5b82c4ee4fad1b89d 100644 (file)
@@ -98,8 +98,20 @@ public class LTTngControlService implements ILttngControlService {
      * Command to disable a event. 
      */
     private final static String COMMAND_DISABLE_EVENT = CONTROL_COMMAND + " disable-event "; //$NON-NLS-1$
+    /**
+     * Command to add a context to channels and/or events
+     */
+    private final static String COMMAND_ADD_CONTEXT = CONTROL_COMMAND + " add-context "; //$NON-NLS-1$
+    /**
+     * Command to execute calibrate command to quantify LTTng overhead
+     */
+    private final static String COMMAND_CALIBRATE = CONTROL_COMMAND + " calibrate "; //$NON-NLS-1$
 
     // Command options constants 
+    /**
+     * Command line option for output path.
+     */
+    private final static String OPTION_OUTPUT_PATH = " -o "; //$NON-NLS-1$
     /**
      * Command line option for kernel tracer.
      */
@@ -116,10 +128,18 @@ public class LTTngControlService implements ILttngControlService {
      * Command line option for specifying a channel.
      */
     private final static String OPTION_CHANNEL = " -c ";  //$NON-NLS-1$
+    /**
+     * Command line option for specifying a event.
+     */
+    private final static String OPTION_EVENT = " -e ";  //$NON-NLS-1$
     /**
      * Command line option for specifying all events.
      */
     private final static String OPTION_ALL = " -a ";  //$NON-NLS-1$
+    /**
+     * Command line option for specifying a context.
+     */
+    private final static String OPTION_CONTEXT_TYPE = " -t ";  //$NON-NLS-1$
     /**
      * Command line option for specifying tracepoint events.
      */
@@ -164,6 +184,10 @@ public class LTTngControlService implements ILttngControlService {
      * Optional command line option for configuring a channel's read timer interval.
      */
     private final static String OPTION_READ_TIMER = " --read-timer ";  //$NON-NLS-1$
+    /**
+     * Command line option for printing the help of a specif command 
+     */
+    private final static String OPTION_HELP = " -h ";  //$NON-NLS-1$
     
     // Parsing constants
     /**
@@ -280,6 +304,15 @@ public class LTTngControlService implements ILttngControlService {
      * Pattern to match for session command output for "session name not found".
      */
     private final static Pattern SESSION_NOT_FOUND_ERROR_PATTERN = Pattern.compile("\\s*Error:\\s+Session\\s+name\\s+not\\s+found"); //$NON-NLS-1$
+    /**
+     * Pattern to match introduction line of context list.
+     */
+    private final static Pattern ADD_CONTEXT_HELP_CONTEXTS_INTRO = Pattern.compile("\\s*TYPE can\\s+be\\s+one\\s+of\\s+the\\s+strings\\s+below.*"); //$NON-NLS-1$
+    
+    /**
+     * Pattern to match introduction line of context list.
+     */
+    private final static Pattern ADD_CONTEXT_HELP_CONTEXTS_END_LINE = Pattern.compile("\\s*Example.*"); //$NON-NLS-1$
     
     // ------------------------------------------------------------------------
     // Attributes
@@ -358,8 +391,7 @@ public class LTTngControlService implements ILttngControlService {
         
         String command = COMMAND_LIST + sessionName;
         ICommandResult result = fCommandShell.executeCommand(command, monitor);
-        
-        
+
         if (isError(result)) {
             throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
         }
@@ -531,12 +563,16 @@ public class LTTngControlService implements ILttngControlService {
         String newName = formatParameter(sessionName);
         String newPath = formatParameter(sessionPath);
 
-        String command = COMMAND_CREATE_SESSION + newName;
+        StringBuffer command = new StringBuffer(); 
+        command.append(COMMAND_CREATE_SESSION);
+        command.append(newName);
+
         if (newPath != null && !"".equals(newPath)) { //$NON-NLS-1$
-            command += " -o " + newPath; //$NON-NLS-1$
+            command.append(OPTION_OUTPUT_PATH);
+            command.append(newPath);
         }
 
-        ICommandResult result = fCommandShell.executeCommand(command, monitor);
+        ICommandResult result = fCommandShell.executeCommand(command.toString(), monitor);
         
         if (isError(result)) {
             throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
@@ -597,15 +633,12 @@ public class LTTngControlService implements ILttngControlService {
         ICommandResult result = fCommandShell.executeCommand(command, monitor);
         String[] output = result.getOutput();
         
-        if (isError(result)) {
-            // In case "session not found" treat it as success 
-            if ((output == null) || (!SESSION_NOT_FOUND_ERROR_PATTERN.matcher(output[0]).matches())) {
+        if (isError(result) && ((output == null) || (!SESSION_NOT_FOUND_ERROR_PATTERN.matcher(output[0]).matches()))) {
                 throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
-            }
-        }
+       }
         //Session <sessionName> destroyed
     }
-
+    
     /*
      * (non-Javadoc)
      * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#startSession(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
@@ -651,7 +684,7 @@ public class LTTngControlService implements ILttngControlService {
     public void enableChannels(String sessionName, List<String> channelNames, boolean isKernel, IChannelInfo info, IProgressMonitor monitor) throws ExecutionException {
 
         // no channels to enable
-        if (channelNames.size() == 0) {
+        if (channelNames.isEmpty()) {
             return;
         }
 
@@ -663,7 +696,7 @@ public class LTTngControlService implements ILttngControlService {
             String channel = (String) iterator.next();
             command.append(channel);
             if (iterator.hasNext()) {
-                command.append(","); //$NON-NLS-1$
+                command.append(',');
             }
         }
 
@@ -717,7 +750,7 @@ public class LTTngControlService implements ILttngControlService {
     public void disableChannels(String sessionName, List<String> channelNames, boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
         
         // no channels to enable
-        if (channelNames.size() == 0) {
+        if (channelNames.isEmpty()) {
             return;
         }
 
@@ -729,7 +762,7 @@ public class LTTngControlService implements ILttngControlService {
             String channel = (String) iterator.next();
             command.append(channel);
             if (iterator.hasNext()) {
-                command.append(","); //$NON-NLS-1$
+                command.append(',');
             }
         }
 
@@ -760,7 +793,7 @@ public class LTTngControlService implements ILttngControlService {
         
         StringBuffer command = new StringBuffer(COMMAND_ENABLE_EVENT);
 
-        if (eventNames == null || eventNames.size() == 0) {
+        if (eventNames == null || eventNames.isEmpty()) {
             command.append(OPTION_ALL);
         } else {
 
@@ -768,7 +801,7 @@ public class LTTngControlService implements ILttngControlService {
                 String event = (String) iterator.next();
                 command.append(event);
                 if (iterator.hasNext()) {
-                    command.append(","); //$NON-NLS-1$
+                    command.append(',');
                 }
             }
         }
@@ -912,7 +945,7 @@ public class LTTngControlService implements ILttngControlService {
             command.append(OPTION_ALL);
         } else {
             // no events to enable
-            if (eventNames.size() == 0) {
+            if (eventNames.isEmpty()) {
                 return;
             }
 
@@ -920,7 +953,7 @@ public class LTTngControlService implements ILttngControlService {
                 String event = (String) iterator.next();
                 command.append(event);
                 if (iterator.hasNext()) {
-                    command.append(","); //$NON-NLS-1$
+                    command.append(',');
                 }
             }
         }
@@ -945,6 +978,117 @@ public class LTTngControlService implements ILttngControlService {
             throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
         }
     }
+
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#getContexts(org.eclipse.core.runtime.IProgressMonitor)
+     */
+    @Override
+    public List<String> getContextList(IProgressMonitor monitor) throws ExecutionException {
+
+        String command = COMMAND_ADD_CONTEXT + OPTION_HELP;
+
+        ICommandResult result = fCommandShell.executeCommand(command, monitor);
+
+        if (isError(result)) {
+            throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
+        }
+
+        String[] output = result.getOutput(); 
+        
+        List<String> contexts = new ArrayList<String>(0);
+        
+        int index = 0;
+        boolean inList = false;
+        while (index < output.length) {
+            String line = result.getOutput()[index];
+            
+            Matcher startMatcher = ADD_CONTEXT_HELP_CONTEXTS_INTRO.matcher(line);
+            Matcher endMatcher = ADD_CONTEXT_HELP_CONTEXTS_END_LINE.matcher(line);
+
+            if (startMatcher.matches()) {
+                inList = true;
+            } else if (endMatcher.matches()) {
+                break;
+            } else if (inList == true) {
+                String[] tmp = line.split(","); //$NON-NLS-1$
+                for (int i = 0; i < tmp.length; i++) {
+                    contexts.add(tmp[i].trim());
+                }
+            }
+            index++;
+        }
+        return contexts;
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#addContexts(java.lang.String, java.lang.String, java.lang.String, boolean, java.util.List, org.eclipse.core.runtime.IProgressMonitor)
+     */
+    @Override
+    public void addContexts(String sessionName, String channelName, String eventName, boolean isKernel, List<String> contextNames, IProgressMonitor monitor) throws ExecutionException {
+        
+        String newSessionName = formatParameter(sessionName);
+        StringBuffer command = new StringBuffer(COMMAND_ADD_CONTEXT);
+
+        command.append(OPTION_SESSION);
+        command.append(newSessionName);
+        
+        if (channelName != null) {
+            command.append(OPTION_CHANNEL);
+            command.append(channelName);
+        }
+
+        if (eventName != null) {
+            command.append(OPTION_EVENT);
+            command.append(eventName);
+        }
+
+        if (isKernel) {
+            command.append(OPTION_KERNEL);
+        } else {
+            command.append(OPTION_UST);
+        }
+        
+        for (Iterator<String> iterator = contextNames.iterator(); iterator.hasNext();) {
+            String context = (String) iterator.next();
+            command.append(OPTION_CONTEXT_TYPE);
+            command.append(context);
+        }
+
+        ICommandResult result = fCommandShell.executeCommand(command.toString(), monitor);
+        
+        if (isError(result)) {
+            throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService#calibrate(boolean, org.eclipse.core.runtime.IProgressMonitor)
+     */
+    @Override
+    public void calibrate(boolean isKernel, IProgressMonitor monitor) throws ExecutionException {
+//        String newSessionName = formatParameter(sessionName);
+        StringBuffer command = new StringBuffer(COMMAND_CALIBRATE);
+//
+//        command.append(OPTION_SESSION);
+//        command.append(newSessionName);
+
+        if (isKernel) {
+            command.append(OPTION_KERNEL);
+        } else {
+            command.append(OPTION_UST);
+        }
+
+        command.append(OPTION_FUNCTION_PROBE);
+
+        ICommandResult result = fCommandShell.executeCommand(command.toString(), monitor);
+
+        if (isError(result)) {
+            throw new ExecutionException(Messages.TraceControl_CommandError + " " + command + "\n" + formatOutput(result.getOutput())); //$NON-NLS-1$ //$NON-NLS-2$
+        }        
+    }
     
     // ------------------------------------------------------------------------
     // Helper methods
@@ -1223,14 +1367,15 @@ public class LTTngControlService implements ILttngControlService {
      */
     private String formatParameter(String parameter) {
         if (parameter != null) {
-            String newString = String.valueOf(parameter);
+            StringBuffer newString = new StringBuffer();
+            newString.append(parameter);
 
             if (parameter.contains(" ")) { //$NON-NLS-1$
-                newString = "\"" + newString + "\""; //$NON-NLS-1$ //$NON-NLS-2$
+                newString.insert(0, "\""); //$NON-NLS-1$
+                newString.append("\""); //$NON-NLS-1$
             }
-            return newString;
+            return newString.toString();
         }
         return null;
     }
-    
 }
This page took 0.027753 seconds and 5 git commands to generate.