1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.core
.kernel
.handlers
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.Attributes
;
18 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.LinuxValues
;
19 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.StateValues
;
20 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.trace
.IKernelAnalysisEventLayout
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
29 * Scheduler switch event handler
31 public class SchedSwitchHandler
extends KernelEventHandler
{
39 public SchedSwitchHandler(IKernelAnalysisEventLayout layout
) {
44 public void handleEvent(ITmfStateSystemBuilder ss
, ITmfEvent event
) throws AttributeNotFoundException
{
45 Integer cpu
= KernelEventHandlerUtils
.getCpu(event
);
50 ITmfEventField content
= event
.getContent();
51 Integer prevTid
= ((Long
) content
.getField(getLayout().fieldPrevTid()).getValue()).intValue();
52 Long prevState
= checkNotNull((Long
) content
.getField(getLayout().fieldPrevState()).getValue());
53 String nextProcessName
= checkNotNull((String
) content
.getField(getLayout().fieldNextComm()).getValue());
54 Integer nextTid
= ((Long
) content
.getField(getLayout().fieldNextTid()).getValue()).intValue();
55 Integer nextPrio
= ((Long
) content
.getField(getLayout().fieldNextPrio()).getValue()).intValue();
57 int nodeThreads
= KernelEventHandlerUtils
.getNodeThreads(ss
);
58 int formerThreadNode
= ss
.getQuarkRelativeAndAdd(nodeThreads
, prevTid
.toString());
59 int newCurrentThreadNode
= ss
.getQuarkRelativeAndAdd(nodeThreads
, nextTid
.toString());
61 long timestamp
= KernelEventHandlerUtils
.getTimestamp(event
);
62 /* Set the status of the process that got scheduled out. */
63 setOldProcessStatus(ss
, prevState
, formerThreadNode
, timestamp
);
65 /* Set the status of the new scheduled process */
66 KernelEventHandlerUtils
.setProcessToRunning(timestamp
, newCurrentThreadNode
, ss
);
68 /* Set the exec name of the new process */
69 setNewProcessExecName(ss
, nextProcessName
, newCurrentThreadNode
, timestamp
);
71 /* Set the current prio for the new process */
72 setNewProcessPio(ss
, nextPrio
, newCurrentThreadNode
, timestamp
);
74 /* Make sure the PPID and system_call sub-attributes exist */
75 ss
.getQuarkRelativeAndAdd(newCurrentThreadNode
, Attributes
.SYSTEM_CALL
);
76 ss
.getQuarkRelativeAndAdd(newCurrentThreadNode
, Attributes
.PPID
);
78 /* Set the current scheduled process on the relevant CPU */
79 int currentCPUNode
= KernelEventHandlerUtils
.getCurrentCPUNode(cpu
, ss
);
80 setCpuProcess(ss
, nextTid
, timestamp
, currentCPUNode
);
82 /* Set the status of the CPU itself */
83 setCpuStatus(ss
, nextTid
, newCurrentThreadNode
, timestamp
, currentCPUNode
);
86 private static void setOldProcessStatus(ITmfStateSystemBuilder ss
, Long prevState
, Integer formerThreadNode
, long timestamp
) throws AttributeNotFoundException
{
89 * Empirical observations and look into the linux code have
90 * shown that the TASK_STATE_MAX flag is used internally and
91 * |'ed with other states, most often the running state, so it
92 * is ignored from the prevState value.
94 int state
= (int) (prevState
& ~
(LinuxValues
.TASK_STATE_MAX
));
97 case LinuxValues
.TASK_STATE_RUNNING
:
98 value
= StateValues
.PROCESS_STATUS_WAIT_FOR_CPU_VALUE
;
100 case LinuxValues
.TASK_INTERRUPTIBLE
:
101 case LinuxValues
.TASK_UNINTERRUPTIBLE
:
102 value
= StateValues
.PROCESS_STATUS_WAIT_BLOCKED_VALUE
;
104 case LinuxValues
.TASK_DEAD
:
105 value
= TmfStateValue
.nullValue();
108 value
= StateValues
.PROCESS_STATUS_WAIT_UNKNOWN_VALUE
;
111 int quark
= ss
.getQuarkRelativeAndAdd(formerThreadNode
, Attributes
.STATUS
);
112 ss
.modifyAttribute(timestamp
, value
, quark
);
116 private static void setCpuStatus(ITmfStateSystemBuilder ss
, Integer nextTid
, Integer newCurrentThreadNode
, long timestamp
, int currentCPUNode
) throws AttributeNotFoundException
{
118 ITmfStateValue value
;
120 /* Check if the entering process is in kernel or user mode */
121 quark
= ss
.getQuarkRelativeAndAdd(newCurrentThreadNode
, Attributes
.SYSTEM_CALL
);
122 ITmfStateValue queryOngoingState
= ss
.queryOngoingState(quark
);
123 if (queryOngoingState
.isNull()) {
124 value
= StateValues
.CPU_STATUS_RUN_USERMODE_VALUE
;
126 value
= StateValues
.CPU_STATUS_RUN_SYSCALL_VALUE
;
129 value
= StateValues
.CPU_STATUS_IDLE_VALUE
;
131 quark
= ss
.getQuarkRelativeAndAdd(currentCPUNode
, Attributes
.STATUS
);
132 ss
.modifyAttribute(timestamp
, value
, quark
);
135 private static void setCpuProcess(ITmfStateSystemBuilder ss
, Integer nextTid
, long timestamp
, int currentCPUNode
) throws AttributeNotFoundException
{
137 ITmfStateValue value
;
138 quark
= ss
.getQuarkRelativeAndAdd(currentCPUNode
, Attributes
.CURRENT_THREAD
);
139 value
= TmfStateValue
.newValueInt(nextTid
);
140 ss
.modifyAttribute(timestamp
, value
, quark
);
143 private static void setNewProcessPio(ITmfStateSystemBuilder ss
, Integer nextPrio
, Integer newCurrentThreadNode
, long timestamp
) throws AttributeNotFoundException
{
145 ITmfStateValue value
;
146 quark
= ss
.getQuarkRelativeAndAdd(newCurrentThreadNode
, Attributes
.PRIO
);
147 value
= TmfStateValue
.newValueInt(nextPrio
);
148 ss
.modifyAttribute(timestamp
, value
, quark
);
151 private static void setNewProcessExecName(ITmfStateSystemBuilder ss
, String nextProcessName
, Integer newCurrentThreadNode
, long timestamp
) throws AttributeNotFoundException
{
153 ITmfStateValue value
;
154 quark
= ss
.getQuarkRelativeAndAdd(newCurrentThreadNode
, Attributes
.EXEC_NAME
);
155 value
= TmfStateValue
.newValueString(nextProcessName
);
156 ss
.modifyAttribute(timestamp
, value
, quark
);