lttng: Track the CPU statuses in the event handler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.core / src / org / eclipse / linuxtools / internal / lttng2 / kernel / core / stateprovider / CtfKernelHandler.java
index 8f965f6df24cc03ff853710fa1944b19b35e5033..f3f5d4ef6f6346801a37f84831f90ed7e7303bc9 100644 (file)
@@ -20,11 +20,11 @@ import org.eclipse.linuxtools.lttng2.kernel.core.trace.Attributes;
 import org.eclipse.linuxtools.lttng2.kernel.core.trace.LttngStrings;
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
-import org.eclipse.linuxtools.tmf.core.statesystem.AttributeNotFoundException;
-import org.eclipse.linuxtools.tmf.core.statesystem.TimeRangeException;
-import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateSystemBuilder;
+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.IStateSystemBuilder;
 import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue;
-import org.eclipse.linuxtools.tmf.core.statevalue.StateValueTypeException;
 import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue;
 
 /**
@@ -50,6 +50,12 @@ class CtfKernelHandler implements Runnable {
     /* Event names HashMap. TODO: This can be discarded once we move to Java 7 */
     private final HashMap<String, Integer> knownEventNames;
 
+    /* Common locations in the attribute tree */
+    private int cpusNode = -1;
+    private int threadsNode = -1;
+    private int irqsNode = -1;
+    private int softIrqsNode = -1;
+
     CtfKernelHandler(BlockingQueue<CtfTmfEvent> eventsQueue) {
         assert (eventsQueue != null);
         this.inQueue = eventsQueue;
@@ -70,6 +76,7 @@ class CtfKernelHandler implements Runnable {
             return;
         }
         CtfTmfEvent event;
+        setupCommonLocations();
 
         try {
             event = inQueue.take();
@@ -89,6 +96,9 @@ class CtfKernelHandler implements Runnable {
 
     private void closeStateSystem() {
         /* Close the History system, if there is one */
+        if (currentEvent == null) {
+            return;
+        }
         try {
             ss.closeHistory(currentEvent.getTimestamp().getValue());
         } catch (TimeRangeException e) {
@@ -115,10 +125,10 @@ class CtfKernelHandler implements Runnable {
         if (eventCpu >= currentCPUNodes.size()) {
             /* We need to add this node to the vector */
             for (Integer i = currentCPUNodes.size(); i < eventCpu + 1; i++) {
-                quark = ss.getQuarkAbsoluteAndAdd(Attributes.CPUS, i.toString());
+                quark = ss.getQuarkRelativeAndAdd(cpusNode, i.toString());
                 currentCPUNodes.add(quark);
 
-                quark = ss.getQuarkAbsoluteAndAdd(Attributes.THREADS, Attributes.UNKNOWN);
+                quark = ss.getQuarkRelativeAndAdd(threadsNode, Attributes.UNKNOWN);
                 currentThreadNodes.add(quark);
             }
         }
@@ -131,136 +141,192 @@ class CtfKernelHandler implements Runnable {
         try {
             /*
              * Feed event to the history system if it's known to cause a state
-             * transition See:
-             * https://projectwiki.dorsal.polymtl.ca/index.php/State_transitions
+             * transition.
              */
             switch (getEventIndex(eventName)) {
 
             case 1: // "exit_syscall":
-                /* Fields: int64 ret */
-                /* Pop "syscall" from the Exec_mode_stack */
-                quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.EXEC_MODE_STACK);
-                try {
-                    ss.popAttribute(ts, quark);
-                } catch (AttributeNotFoundException e1) {
-                    /*
-                     * meh, can happen if we're missing events, we'll just
-                     * silently ignore it.
-                     */
-                    System.err.println(event.getTimestamp()
-                            + " Popping empty attribute: " + e1.getMessage()); //$NON-NLS-1$
-                }
+            /* Fields: int64 ret */
+            {
+                /* Clear the current system call on the process */
+                quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.SYSTEM_CALL);
+                value = TmfStateValue.nullValue();
+                ss.modifyAttribute(ts, value, quark);
+
+                /* Put the process' status back to user mode */
+                quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
+                value = TmfStateValue.newValueInt(Attributes.STATUS_RUN_USERMODE);
+                ss.modifyAttribute(ts, value, quark);
+            }
                 break;
 
             case 2: // "irq_handler_entry":
-                /* Fields: int32 irq, string name */
+            /* Fields: int32 irq, string name */
+            {
                 Integer irqId = ((Long) content.getField(LttngStrings.IRQ).getValue()).intValue();
 
-                /* Push the IRQ to the CPU's IRQ_stack */
-                quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.IRQ_STACK);
-                value = TmfStateValue.newValueInt(irqId);
-                ss.pushAttribute(ts, value, quark);
+                /* Mark this IRQ as active in the resource tree.
+                 * The state value = the CPU on which this IRQ is sitting */
+                quark = ss.getQuarkRelativeAndAdd(irqsNode, irqId.toString());
+                value = TmfStateValue.newValueInt(event.getCPU());
+                ss.modifyAttribute(ts, value, quark);
 
                 /* Change the status of the running process to interrupted */
                 quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
-                value = TmfStateValue.newValueInt(Attributes.STATUS_WAIT_CPU);
+                value = TmfStateValue.newValueInt(Attributes.STATUS_INTERRUPTED);
                 ss.modifyAttribute(ts, value, quark);
+\r
+                /* Change the status of the CPU to interrupted */\r
+                quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS);\r
+                value = TmfStateValue.newValueInt(Attributes.CPU_STATUS_INTERRUPTED);\r
+                ss.modifyAttribute(ts, value, quark);\r
+            }
                 break;
 
             case 3: // "irq_handler_exit":
-                /* Fields: int32 irq, int32 ret */
-                int stackDepth = 0;
-
-                /* Pop the IRQ from the CPU's IRQ_stack */
-                quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.IRQ_STACK);
-                try {
-                    ss.popAttribute(ts, quark);
-                } catch (AttributeNotFoundException e1) {
-                    System.err.print(event.getTimestamp()
-                            + " Popping empty attribute: " + e1.getMessage()); //$NON-NLS-1$
-                }
+            /* Fields: int32 irq, int32 ret */
+            {
+                Integer irqId = ((Long) content.getField(LttngStrings.IRQ).getValue()).intValue();
 
-                /*
-                 * If this was the last IRQ on the stack, set the process back
-                 * to running
-                 */
-                /* 'quark' should still be valid */
-                try {
-                    stackDepth = ss.queryOngoingState(quark).unboxInt();
-                } catch (StateValueTypeException e) {
-                    /* IRQ_stack SHOULD be of int type, this shouldn't happen */
-                    e.printStackTrace();
-                }
-                if (stackDepth == 0) {
-                    quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
-                    value = TmfStateValue.newValueInt(Attributes.STATUS_RUN);
-                    ss.modifyAttribute(ts, value, quark);
-                }
+                /* Put this IRQ back to inactive in the resource tree */
+                quark = ss.getQuarkRelativeAndAdd(irqsNode, irqId.toString());
+                value = TmfStateValue.nullValue();
+                ss.modifyAttribute(ts, value, quark);
+
+                /* Set the previous process back to running */
+                setProcessToRunning(ts, currentThreadNode);
+\r
+                /* Set the CPU status back to "busy" or "idle" */\r
+                cpuExitInterrupt(ts, currentCPUNode);\r
+            }
                 break;
 
             case 4: // "softirq_entry":
-                /* Fields: int32 vec */
+            /* Fields: int32 vec */
+            {
+                Integer softIrqId = ((Long) content.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 */
+                quark = ss.getQuarkRelativeAndAdd(softIrqsNode, softIrqId.toString());
+                value = TmfStateValue.newValueInt(event.getCPU());
+                ss.modifyAttribute(ts, value, quark);
+
+                /* Change the status of the running process to interrupted */
+                quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
+                value = TmfStateValue.newValueInt(Attributes.STATUS_INTERRUPTED);
+                ss.modifyAttribute(ts, value, quark);
+\r
+                /* Change the status of the CPU to interrupted */\r
+                quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS);\r
+                value = TmfStateValue.newValueInt(Attributes.CPU_STATUS_INTERRUPTED);\r
+                ss.modifyAttribute(ts, value, quark);\r
+            }
                 break;
 
             case 5: // "softirq_exit":
-                /* Fields: int32 vec */
+            /* Fields: int32 vec */
+            {
+                Integer softIrqId = ((Long) content.getField(LttngStrings.VEC).getValue()).intValue();
+
+                /* Put this SoftIRQ back to inactive (= -1) in the resource tree */
+                quark = ss.getQuarkRelativeAndAdd(softIrqsNode, softIrqId.toString());
+                value = TmfStateValue.nullValue();
+                ss.modifyAttribute(ts, value, quark);
+
+                /* Set the previous process back to running */
+                setProcessToRunning(ts, currentThreadNode);
+\r
+                /* Set the CPU status back to "busy" or "idle" */\r
+                cpuExitInterrupt(ts, currentCPUNode);\r
+            }
                 break;
 
             case 6: // "softirq_raise":
-                /* Fields: int32 vec */
+            /* Fields: int32 vec */
+            {
+                Integer softIrqId = ((Long) content.getField(LttngStrings.VEC).getValue()).intValue();
+
+                /* Mark this SoftIRQ as *raised* in the resource tree.
+                 * State value = -2 */
+                quark = ss.getQuarkRelativeAndAdd(softIrqsNode, softIrqId.toString());
+                value = TmfStateValue.newValueInt(Attributes.SOFT_IRQ_RAISED);
+                ss.modifyAttribute(ts, value, quark);
+            }
                 break;
 
             case 7: // "sched_switch":
-                /*
-                 * Fields: string prev_comm, int32 prev_tid, int32 prev_prio,
-                 * int64 prev_state, string next_comm, int32 next_tid, int32
-                 * next_prio
-                 */
-
-                // prev_comm doesn't seem to get populated...
-                String prevProcessName = (String) content.getField(LttngStrings.PREV_COMM).getValue();
+            /*
+             * Fields: string prev_comm, int32 prev_tid, int32 prev_prio, int64 prev_state,
+             *         string next_comm, int32 next_tid, int32 next_prio
+             */
+            {
                 Integer prevTid = ((Long) content.getField(LttngStrings.PREV_TID).getValue()).intValue();
-                Long prevState = (Long) content.getField(LttngStrings.PREV_STATE).getValue();
+                //Long prevState = (Long) content.getField(LttngStrings.PREV_STATE).getValue();
 
                 String nextProcessName = (String) content.getField(LttngStrings.NEXT_COMM).getValue();
                 Integer nextTid = ((Long) content.getField(LttngStrings.NEXT_TID).getValue()).intValue();
 
-                /* Update the name of the process going out (if needed) */
-                quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.EXEC_NAME);
-                value = TmfStateValue.newValueString(prevProcessName);
-                ss.updateOngoingState(value, quark);
-
                 /* Update the currentThreadNodes pointer */
-                Integer newCurrentThreadNode = ss.getQuarkAbsoluteAndAdd(Attributes.THREADS, nextTid.toString());
+                Integer newCurrentThreadNode = ss.getQuarkRelativeAndAdd(threadsNode, nextTid.toString());
                 currentThreadNodes.set(eventCpu, newCurrentThreadNode);
 
+                /*
+                 * Set the status of the process that got scheduled out, but
+                 * only in the case where that process is currently active.
+                 */
+                Integer formerThreadNode = ss.getQuarkRelativeAndAdd(threadsNode, prevTid.toString());
+                quark = ss.getQuarkRelativeAndAdd(formerThreadNode, Attributes.EXEC_NAME);
+                value = ss.queryOngoingState(quark);
+                if (!value.isNull()) {
+                    quark = ss.getQuarkRelativeAndAdd(formerThreadNode, Attributes.STATUS);
+                    value = TmfStateValue.newValueInt(Attributes.STATUS_WAIT);
+                    ss.modifyAttribute(ts, value, quark);
+                }
+
                 /* Set the status of the new scheduled process */
-                quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.STATUS);
-                value = TmfStateValue.newValueInt(Attributes.STATUS_RUN);
-                ss.modifyAttribute(ts, value, quark);
+                setProcessToRunning(ts, newCurrentThreadNode);
 
                 /* Set the exec name of the new process */
                 quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.EXEC_NAME);
                 value = TmfStateValue.newValueString(nextProcessName);
                 ss.modifyAttribute(ts, value, quark);
 
-                /* Set the status of the process that got scheduled out */
-                quark = ss.getQuarkAbsoluteAndAdd(Attributes.THREADS, prevTid.toString(), Attributes.STATUS);
-                value = TmfStateValue.newValueInt(prevState.intValue());
-                ss.modifyAttribute(ts, value, quark);
+                /*
+                 * Check if we need to set the syscall state and the PPID of
+                 * the new process (in case we haven't seen this process before)
+                 */
+                quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.SYSTEM_CALL);
+                if (quark == ss.getNbAttributes()) { /* Did we just add this attribute? */
+                    value = TmfStateValue.nullValue();
+                    ss.modifyAttribute(ts, value, quark);
+                }
+                quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.PPID);
+                if (quark == ss.getNbAttributes()) {
+                    value = TmfStateValue.nullValue();
+                    ss.modifyAttribute(ts, value, quark);
+                }
 
                 /* Set the current scheduled process on the relevant CPU */
                 quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.CURRENT_THREAD);
                 value = TmfStateValue.newValueInt(nextTid);
                 ss.modifyAttribute(ts, value, quark);
+\r
+                /* Set the status of the CPU itself */\r
+                quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS);\r
+                if (nextTid > 0) {\r
+                    value = TmfStateValue.newValueInt(Attributes.CPU_STATUS_BUSY);\r
+                } else {\r
+                    value = TmfStateValue.newValueInt(Attributes.CPU_STATUS_IDLE);\r
+                }\r
+                ss.modifyAttribute(ts, value, quark);\r
+            }
                 break;
 
             case 8: // "sched_process_fork":
-                /*
-                 * Fields: string parent_comm, int32 parent_tid, string
-                 * child_comm, int32 child_tid
-                 */
-
+            /* Fields: string parent_comm, int32 parent_tid,
+             *         string child_comm, int32 child_tid */
+            {
                 // String parentProcessName = (String)
                 // event.getFieldValue("parent_comm");
                 String childProcessName;
@@ -270,12 +336,9 @@ class CtfKernelHandler implements Runnable {
                 Integer parentTid = ((Long) content.getField(LttngStrings.PARENT_TID).getValue()).intValue();
                 Integer childTid = ((Long) content.getField(LttngStrings.CHILD_TID).getValue()).intValue();
 
-                tidNode = ss.getQuarkAbsoluteAndAdd(Attributes.THREADS, childTid.toString());
+                tidNode = ss.getQuarkRelativeAndAdd(threadsNode, childTid.toString());
 
-                /*
-                 * Add the new process with its known TID, PPID, and initial
-                 * Exec_name
-                 */
+                /* Assign the PPID to the new process */
                 quark = ss.getQuarkRelativeAndAdd(tidNode, Attributes.PPID);
                 value = TmfStateValue.newValueInt(parentTid);
                 ss.modifyAttribute(ts, value, quark);
@@ -284,15 +347,27 @@ class CtfKernelHandler implements Runnable {
                 quark = ss.getQuarkRelativeAndAdd(tidNode, Attributes.EXEC_NAME);
                 value = TmfStateValue.newValueString(childProcessName);
                 ss.modifyAttribute(ts, value, quark);
+
+                /* Set the new process' status */
+                quark = ss.getQuarkRelativeAndAdd(tidNode, Attributes.STATUS);
+                value = TmfStateValue.newValueInt(Attributes.STATUS_WAIT);
+                ss.modifyAttribute(ts, value, quark);
+
+                /* Set the process' syscall state */
+                quark = ss.getQuarkRelativeAndAdd(tidNode, Attributes.SYSTEM_CALL);
+                value = TmfStateValue.nullValue();
+                ss.modifyAttribute(ts, value, quark);
+            }
                 break;
 
             case 9: // "sched_process_exit":
-                /* Fields: string comm, int32 tid, int32 prio */
+            /* Fields: string comm, int32 tid, int32 prio */
+            {
                 String processName = (String) content.getField(LttngStrings.COMM).getValue();
                 Integer tid = ((Long) content.getField(LttngStrings.TID).getValue()).intValue();
 
                 /* Update the process' name, if we don't have it */
-                quark = ss.getQuarkAbsoluteAndAdd(Attributes.THREADS, tid.toString(), Attributes.EXEC_NAME);
+                quark = ss.getQuarkRelativeAndAdd(threadsNode, tid.toString(), Attributes.EXEC_NAME);
                 value = TmfStateValue.newValueString(processName);
                 ss.updateOngoingState(value, quark);
 
@@ -300,12 +375,13 @@ class CtfKernelHandler implements Runnable {
                  * Remove the process and all its sub-attributes from the
                  * current state
                  */
-                quark = ss.getQuarkAbsoluteAndAdd(Attributes.THREADS, tid.toString());
+                quark = ss.getQuarkRelativeAndAdd(threadsNode, tid.toString());
                 ss.removeAttribute(ts, quark);
+            }
                 break;
 
             case 10: // "sched_process_free":
-                /* Fields: string comm, int32 tid, int32 prio */
+            /* Fields: string comm, int32 tid, int32 prio */
                 break;
 
             // FIXME In CTF it's as "syscall_exec". Will have to be adapted.
@@ -319,8 +395,8 @@ class CtfKernelHandler implements Runnable {
             // break;
 
             default:
-                /* Other event types not covered by the main switch */
-
+            /* Other event types not covered by the main switch */
+            {
                 if (eventName.startsWith(LttngStrings.SYSCALL_PREFIX)
                         || eventName.startsWith(LttngStrings.COMPAT_SYSCALL_PREFIX)) {
                     /*
@@ -328,17 +404,19 @@ class CtfKernelHandler implements Runnable {
                      * syscall names are listed into the event type
                      */
 
-                    /*
-                     * Push the syscall name on the Exec_mode_stack of the
-                     * relevant PID
-                     */
-                    quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.EXEC_MODE_STACK);
+                    /* Assign the new system call to the process */
+                    quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.SYSTEM_CALL);
                     value = TmfStateValue.newValueString(eventName);
-                    ss.pushAttribute(ts, value, quark);
-                }
+                    ss.modifyAttribute(ts, value, quark);
 
+                    /* Put the process in system call mode */
+                    quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
+                    value = TmfStateValue.newValueInt(Attributes.STATUS_RUN_SYSCALL);
+                    ss.modifyAttribute(ts, value, quark);
+                }
+            }
                 break;
-            } // End of switch
+            } // End of big switch
 
             /*
              * Statistics
@@ -382,7 +460,13 @@ class CtfKernelHandler implements Runnable {
              */
             sve.printStackTrace();
         }
+    }
 
+    private void setupCommonLocations() {
+        cpusNode = ss.getQuarkAbsoluteAndAdd(Attributes.CPUS);
+        threadsNode = ss.getQuarkAbsoluteAndAdd(Attributes.THREADS);
+        irqsNode = ss.getQuarkAbsoluteAndAdd(Attributes.RESOURCES, Attributes.IRQS);
+        softIrqsNode = ss.getQuarkAbsoluteAndAdd(Attributes.RESOURCES, Attributes.SOFT_IRQS);
     }
 
     private static HashMap<String, Integer> fillEventNames() {
@@ -410,4 +494,53 @@ class CtfKernelHandler implements Runnable {
         Integer ret = knownEventNames.get(eventName);
         return (ret != null) ? ret : -1;
     }
+
+    /**
+     * When we want to set a process back to a "running" state, first check
+     * its current System_call attribute. If there is a system call active, we
+     * put the process back in the syscall state. If not, we put it back in
+     * user mode state.
+     */
+    private void setProcessToRunning(long ts, int currentThreadNode)
+            throws AttributeNotFoundException, TimeRangeException,
+            StateValueTypeException {
+        int quark;
+        ITmfStateValue value;
+
+        quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.SYSTEM_CALL);
+        if (ss.queryOngoingState(quark).isNull()) {
+            /* We were in user mode before the interruption */
+            value = TmfStateValue.newValueInt(Attributes.STATUS_RUN_USERMODE);
+        } else {
+            /* We were previously in kernel mode */
+            value = TmfStateValue.newValueInt(Attributes.STATUS_RUN_SYSCALL);
+        }
+        quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
+        ss.modifyAttribute(ts, value, quark);
+    }
+\r
+    /**\r
+     * Similar logic as above, but to set the CPU's status when it's coming out\r
+     * of an interruption.\r
+     * @throws AttributeNotFoundException \r
+     * @throws StateValueTypeException \r
+     * @throws TimeRangeException \r
+     */\r
+    private void cpuExitInterrupt(long ts, int currentCpuNode)\r
+            throws StateValueTypeException, AttributeNotFoundException,\r
+            TimeRangeException {\r
+        int quark;\r
+        ITmfStateValue value;\r
+\r
+        quark = ss.getQuarkRelativeAndAdd(currentCpuNode, Attributes.CURRENT_THREAD);\r
+        if (ss.queryOngoingState(quark).unboxInt() > 0) {\r
+            /* There was a process on the CPU */\r
+            value = TmfStateValue.newValueInt(Attributes.CPU_STATUS_BUSY);\r
+        } else {\r
+            /* There was no real process scheduled, CPU was idle */\r
+            value = TmfStateValue.newValueInt(Attributes.CPU_STATUS_IDLE);\r
+        }\r
+        quark = ss.getQuarkRelativeAndAdd(currentCpuNode, Attributes.STATUS);\r
+        ss.modifyAttribute(ts, value, quark);\r
+    }\r
 }
This page took 0.02929 seconds and 5 git commands to generate.