tmf: Split the state system in a separate plugin
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.core / src / org / eclipse / linuxtools / internal / lttng2 / kernel / core / stateprovider / LttngKernelStateProvider.java
index 38bf88ff7b7fcee0de298956233d0a639337d810..300486b1197f86c6d24283195defc92fecdbe8ec 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2013 Ericsson
+ * Copyright (c) 2012, 2014 Ericsson
  * Copyright (c) 2010, 2011 École Polytechnique de Montréal
  * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
  *
 
 package org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider;
 
-import java.util.HashMap;
-
 import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
 import org.eclipse.linuxtools.internal.lttng2.kernel.core.LttngStrings;
 import org.eclipse.linuxtools.internal.lttng2.kernel.core.StateValues;
-import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
-import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace;
+import org.eclipse.linuxtools.statesystem.core.ITmfStateSystemBuilder;
+import org.eclipse.linuxtools.statesystem.core.exceptions.AttributeNotFoundException;
+import org.eclipse.linuxtools.statesystem.core.exceptions.StateValueTypeException;
+import org.eclipse.linuxtools.statesystem.core.exceptions.TimeRangeException;
+import org.eclipse.linuxtools.statesystem.core.statevalue.ITmfStateValue;
+import org.eclipse.linuxtools.statesystem.core.statevalue.TmfStateValue;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
-import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
-import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
-import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
 import org.eclipse.linuxtools.tmf.core.statesystem.AbstractTmfStateProvider;
-import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystemBuilder;
-import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue;
-import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue;
+import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEvent;
+import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
 
 /**
  * This is the state change input plugin for TMF's state system which handles
@@ -44,10 +42,7 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
      * Version number of this state provider. Please bump this if you modify the
      * contents of the generated state history in some way.
      */
-    private static final int VERSION = 3;
-
-    /* Event names HashMap. TODO: This can be discarded once we move to Java 7 */
-    private final HashMap<String, Integer> knownEventNames;
+    private static final int VERSION = 4;
 
     // ------------------------------------------------------------------------
     // Constructor
@@ -61,7 +56,6 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
      */
     public LttngKernelStateProvider(CtfTmfTrace trace) {
         super(trace, CtfTmfEvent.class, "LTTng Kernel"); //$NON-NLS-1$
-        knownEventNames = fillEventNames();
     }
 
     // ------------------------------------------------------------------------
@@ -90,13 +84,9 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
          * AbstractStateChangeInput should have already checked for the correct
          * class type
          */
-        CtfTmfEvent event = (CtfTmfEvent) ev;
-
-        int quark;
-        ITmfStateValue value;
+        final CtfTmfEvent event = (CtfTmfEvent) ev;
 
-        final ITmfEventField content = event.getContent();
-        final String eventName = event.getEventName();
+        final String eventName = event.getType().getName();
         final long ts = event.getTimestamp().getValue();
 
         try {
@@ -107,18 +97,18 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
              * Shortcut for the "current thread" attribute node. It requires
              * querying the current CPU's current thread.
              */
-            quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.CURRENT_THREAD);
-            value = ss.queryOngoingState(quark);
-            int thread = value.unboxInt();
+            int quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.CURRENT_THREAD);
+            ITmfStateValue value = ss.queryOngoingState(quark);
+            int thread = value.isNull() ? -1 : value.unboxInt();
             final Integer currentThreadNode = ss.getQuarkRelativeAndAdd(getNodeThreads(), String.valueOf(thread));
 
             /*
              * Feed event to the history system if it's known to cause a state
              * transition.
              */
-            switch (getEventIndex(eventName)) {
+            switch (eventName) {
 
-            case 1: // "exit_syscall":
+            case LttngStrings.EXIT_SYSCALL:
             /* Fields: int64 ret */
             {
                 /* Clear the current system call on the process */
@@ -138,10 +128,10 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
             }
                 break;
 
-            case 2: // "irq_handler_entry":
+            case LttngStrings.IRQ_HANDLER_ENTRY:
             /* Fields: int32 irq, string name */
             {
-                Integer irqId = ((Long) content.getField(LttngStrings.IRQ).getValue()).intValue();
+                Integer irqId = ((Long) event.getContent().getField(LttngStrings.IRQ).getValue()).intValue();
 
                 /* Mark this IRQ as active in the resource tree.
                  * The state value = the CPU on which this IRQ is sitting */
@@ -161,10 +151,10 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
             }
                 break;
 
-            case 3: // "irq_handler_exit":
+            case LttngStrings.IRQ_HANDLER_EXIT:
             /* Fields: int32 irq, int32 ret */
             {
-                Integer irqId = ((Long) content.getField(LttngStrings.IRQ).getValue()).intValue();
+                Integer irqId = ((Long) event.getContent().getField(LttngStrings.IRQ).getValue()).intValue();
 
                 /* Put this IRQ back to inactive in the resource tree */
                 quark = ss.getQuarkRelativeAndAdd(getNodeIRQs(), irqId.toString());
@@ -179,10 +169,10 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
             }
                 break;
 
-            case 4: // "softirq_entry":
+            case LttngStrings.SOFTIRQ_ENTRY:
             /* Fields: int32 vec */
             {
-                Integer softIrqId = ((Long) content.getField(LttngStrings.VEC).getValue()).intValue();
+                Integer softIrqId = ((Long) event.getContent().getField(LttngStrings.VEC).getValue()).intValue();
 
                 /* Mark this SoftIRQ as active in the resource tree.
                  * The state value = the CPU on which this SoftIRQ is processed */
@@ -202,10 +192,10 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
             }
                 break;
 
-            case 5: // "softirq_exit":
+            case LttngStrings.SOFTIRQ_EXIT:
             /* Fields: int32 vec */
             {
-                Integer softIrqId = ((Long) content.getField(LttngStrings.VEC).getValue()).intValue();
+                Integer softIrqId = ((Long) event.getContent().getField(LttngStrings.VEC).getValue()).intValue();
 
                 /* Put this SoftIRQ back to inactive (= -1) in the resource tree */
                 quark = ss.getQuarkRelativeAndAdd(getNodeSoftIRQs(), softIrqId.toString());
@@ -220,10 +210,10 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
             }
                 break;
 
-            case 6: // "softirq_raise":
+            case LttngStrings.SOFTIRQ_RAISE:
             /* Fields: int32 vec */
             {
-                Integer softIrqId = ((Long) content.getField(LttngStrings.VEC).getValue()).intValue();
+                Integer softIrqId = ((Long) event.getContent().getField(LttngStrings.VEC).getValue()).intValue();
 
                 /* Mark this SoftIRQ as *raised* in the resource tree.
                  * State value = -2 */
@@ -233,12 +223,13 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
             }
                 break;
 
-            case 7: // "sched_switch":
+            case LttngStrings.SCHED_SWITCH:
             /*
              * Fields: string prev_comm, int32 prev_tid, int32 prev_prio, int64 prev_state,
              *         string next_comm, int32 next_tid, int32 next_prio
              */
             {
+                ITmfEventField content = event.getContent();
                 Integer prevTid = ((Long) content.getField(LttngStrings.PREV_TID).getValue()).intValue();
                 Long prevState = (Long) content.getField(LttngStrings.PREV_STATE).getValue();
                 String nextProcessName = (String) content.getField(LttngStrings.NEXT_COMM).getValue();
@@ -290,10 +281,11 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
             }
                 break;
 
-            case 8: // "sched_process_fork":
+            case LttngStrings.SCHED_PROCESS_FORK:
             /* Fields: string parent_comm, int32 parent_tid,
              *         string child_comm, int32 child_tid */
             {
+                ITmfEventField content = event.getContent();
                 // String parentProcessName = (String) event.getFieldValue("parent_comm");
                 String childProcessName = (String) content.getField(LttngStrings.CHILD_COMM).getValue();
                 // assert ( parentProcessName.equals(childProcessName) );
@@ -334,11 +326,11 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
             }
                 break;
 
-            case 9: // "sched_process_exit":
+            case LttngStrings.SCHED_PROCESS_EXIT:
             /* Fields: string comm, int32 tid, int32 prio */
                 break;
 
-            case 10: // "sched_process_free":
+            case LttngStrings.SCHED_PROCESS_FREE:
             /* Fields: string comm, int32 tid, int32 prio */
             /*
              * A sched_process_free will always happen after the sched_switch
@@ -346,7 +338,7 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
              * this is when we should delete everything wrt to the process.
              */
             {
-                Integer tid = ((Long) content.getField(LttngStrings.TID).getValue()).intValue();
+                Integer tid = ((Long) event.getContent().getField(LttngStrings.TID).getValue()).intValue();
                 /*
                  * Remove the process and all its sub-attributes from the
                  * current state
@@ -356,12 +348,14 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
             }
                 break;
 
-            case 11: // "lttng_statedump_process_state":
+            case LttngStrings.STATEDUMP_PROCESS_STATE:
             /* Fields:
              * int32 type, int32 mode, int32 pid, int32 submode, int32 vpid,
              * int32 ppid, int32 tid, string name, int32 status, int32 vtid */
             {
-                Integer tid = ((Long) content.getField(LttngStrings.TID).getValue()).intValue();
+                ITmfEventField content = event.getContent();
+                int tid = ((Long) content.getField(LttngStrings.TID).getValue()).intValue();
+                int pid = ((Long) content.getField(LttngStrings.PID).getValue()).intValue();
                 int ppid = ((Long) content.getField(LttngStrings.PPID).getValue()).intValue();
                 int status = ((Long) content.getField(LttngStrings.STATUS).getValue()).intValue();
                 String name = (String) content.getField(LttngStrings.NAME).getValue();
@@ -370,7 +364,7 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
                  * populated with anything relevant for now.
                  */
 
-                int curThreadNode = ss.getQuarkRelativeAndAdd(getNodeThreads(), tid.toString());
+                int curThreadNode = ss.getQuarkRelativeAndAdd(getNodeThreads(), String.valueOf(tid));
 
                 /* Set the process' name */
                 quark = ss.getQuarkRelativeAndAdd(curThreadNode, Attributes.EXEC_NAME);
@@ -383,7 +377,13 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
                 /* Set the process' PPID */
                 quark = ss.getQuarkRelativeAndAdd(curThreadNode, Attributes.PPID);
                 if (ss.queryOngoingState(quark).isNull()) {
-                    value = TmfStateValue.newValueInt(ppid);
+                    if (pid == tid) {
+                        /* We have a process. Use the 'PPID' field. */
+                        value = TmfStateValue.newValueInt(ppid);
+                    } else {
+                        /* We have a thread, use the 'PID' field for the parent. */
+                        value = TmfStateValue.newValueInt(pid);
+                    }
                     ss.modifyAttribute(ts, value, quark);
                 }
 
@@ -403,13 +403,13 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
             }
                 break;
 
-            case 12: // "sched_wakeup":
-            case 13: // "sched_wakeup_new":
+            case LttngStrings.SCHED_WAKEUP:
+            case LttngStrings.SCHED_WAKEUP_NEW:
             /* Fields (same fields for both types):
              * string comm, int32 pid, int32 prio, int32 success,
              * int32 target_cpu */
             {
-                final int tid = ((Long) content.getField(LttngStrings.TID).getValue()).intValue();
+                final int tid = ((Long) event.getContent().getField(LttngStrings.TID).getValue()).intValue();
                 final int threadNode = ss.getQuarkRelativeAndAdd(getNodeThreads(), String.valueOf(tid));
 
                 /*
@@ -502,39 +502,6 @@ public class LttngKernelStateProvider extends AbstractTmfStateProvider {
         return ss.getQuarkAbsoluteAndAdd(Attributes.RESOURCES, Attributes.SOFT_IRQS);
     }
 
-    // ------------------------------------------------------------------------
-    // Workaround for the lack of switch-on-strings in Java < 7
-    // ------------------------------------------------------------------------
-
-    private static HashMap<String, Integer> fillEventNames() {
-        /*
-         * TODO Replace with straight strings in the switch/case once we move to
-         * Java 7
-         */
-        HashMap<String, Integer> map = new HashMap<String, Integer>();
-
-        map.put(LttngStrings.EXIT_SYSCALL, 1);
-        map.put(LttngStrings.IRQ_HANDLER_ENTRY, 2);
-        map.put(LttngStrings.IRQ_HANDLER_EXIT, 3);
-        map.put(LttngStrings.SOFTIRQ_ENTRY, 4);
-        map.put(LttngStrings.SOFTIRQ_EXIT, 5);
-        map.put(LttngStrings.SOFTIRQ_RAISE, 6);
-        map.put(LttngStrings.SCHED_SWITCH, 7);
-        map.put(LttngStrings.SCHED_PROCESS_FORK, 8);
-        map.put(LttngStrings.SCHED_PROCESS_EXIT, 9);
-        map.put(LttngStrings.SCHED_PROCESS_FREE, 10);
-        map.put(LttngStrings.STATEDUMP_PROCESS_STATE, 11);
-        map.put(LttngStrings.SCHED_WAKEUP, 12);
-        map.put(LttngStrings.SCHED_WAKEUP_NEW, 13);
-
-        return map;
-    }
-
-    private int getEventIndex(String eventName) {
-        Integer ret = knownEventNames.get(eventName);
-        return (ret != null) ? ret : -1;
-    }
-
     // ------------------------------------------------------------------------
     // Advanced state-setting methods
     // ------------------------------------------------------------------------
This page took 0.030195 seconds and 5 git commands to generate.