| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015 Ericsson |
| 3 | * |
| 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 |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Matthew Khouzam - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers; |
| 14 | |
| 15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 16 | |
| 17 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.LinuxValues; |
| 18 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.StateValues; |
| 19 | import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout; |
| 20 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes; |
| 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; |
| 27 | |
| 28 | /** |
| 29 | * Scheduler switch event handler |
| 30 | */ |
| 31 | public class SchedSwitchHandler extends KernelEventHandler { |
| 32 | |
| 33 | /** |
| 34 | * Constructor |
| 35 | * |
| 36 | * @param layout |
| 37 | * event layout |
| 38 | */ |
| 39 | public SchedSwitchHandler(IKernelAnalysisEventLayout layout) { |
| 40 | super(layout); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public void handleEvent(ITmfStateSystemBuilder ss, ITmfEvent event) throws AttributeNotFoundException { |
| 45 | Integer cpu = KernelEventHandlerUtils.getCpu(event); |
| 46 | if (cpu == null) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 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 | Integer prevPrio = ((Long) content.getField(getLayout().fieldPrevPrio()).getValue()).intValue(); |
| 54 | String nextProcessName = checkNotNull((String) content.getField(getLayout().fieldNextComm()).getValue()); |
| 55 | Integer nextTid = ((Long) content.getField(getLayout().fieldNextTid()).getValue()).intValue(); |
| 56 | Integer nextPrio = ((Long) content.getField(getLayout().fieldNextPrio()).getValue()).intValue(); |
| 57 | |
| 58 | /* Will never return null since "cpu" is null checked */ |
| 59 | String formerThreadAttributeName = Attributes.buildThreadAttributeName(prevTid, cpu); |
| 60 | String currenThreadAttributeName = Attributes.buildThreadAttributeName(nextTid, cpu); |
| 61 | |
| 62 | int nodeThreads = KernelEventHandlerUtils.getNodeThreads(ss); |
| 63 | int formerThreadNode = ss.getQuarkRelativeAndAdd(nodeThreads, formerThreadAttributeName); |
| 64 | int newCurrentThreadNode = ss.getQuarkRelativeAndAdd(nodeThreads, currenThreadAttributeName); |
| 65 | |
| 66 | long timestamp = KernelEventHandlerUtils.getTimestamp(event); |
| 67 | /* Set the status of the process that got scheduled out. */ |
| 68 | setOldProcessStatus(ss, prevState, formerThreadNode, timestamp); |
| 69 | |
| 70 | /* Set the status of the new scheduled process */ |
| 71 | KernelEventHandlerUtils.setProcessToRunning(timestamp, newCurrentThreadNode, ss); |
| 72 | |
| 73 | /* Set the exec name of the new process */ |
| 74 | setNewProcessExecName(ss, nextProcessName, newCurrentThreadNode, timestamp); |
| 75 | |
| 76 | /* Set the current prio for the former process */ |
| 77 | setNewProcessPrio(ss, prevPrio, formerThreadNode, timestamp); |
| 78 | |
| 79 | /* Set the current prio for the new process */ |
| 80 | setNewProcessPrio(ss, nextPrio, newCurrentThreadNode, timestamp); |
| 81 | |
| 82 | /* Make sure the PPID and system_call sub-attributes exist */ |
| 83 | ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.SYSTEM_CALL); |
| 84 | ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.PPID); |
| 85 | |
| 86 | /* Set the current scheduled process on the relevant CPU */ |
| 87 | int currentCPUNode = KernelEventHandlerUtils.getCurrentCPUNode(cpu, ss); |
| 88 | setCpuProcess(ss, nextTid, timestamp, currentCPUNode); |
| 89 | |
| 90 | /* Set the status of the CPU itself */ |
| 91 | setCpuStatus(ss, nextTid, newCurrentThreadNode, timestamp, currentCPUNode); |
| 92 | } |
| 93 | |
| 94 | private static void setOldProcessStatus(ITmfStateSystemBuilder ss, Long prevState, Integer formerThreadNode, long timestamp) throws AttributeNotFoundException { |
| 95 | ITmfStateValue value; |
| 96 | /* |
| 97 | * Empirical observations and look into the linux code have |
| 98 | * shown that the TASK_STATE_MAX flag is used internally and |
| 99 | * |'ed with other states, most often the running state, so it |
| 100 | * is ignored from the prevState value. |
| 101 | * |
| 102 | * Since Linux 4.1, the TASK_NOLOAD state was created and |
| 103 | * TASK_STATE_MAX is now 2048. We use TASK_NOLOAD as the new max |
| 104 | * because it does not modify the displayed state value. |
| 105 | */ |
| 106 | int state = (int) (prevState & (LinuxValues.TASK_NOLOAD - 1)); |
| 107 | |
| 108 | if (isRunning(state)) { |
| 109 | value = StateValues.PROCESS_STATUS_WAIT_FOR_CPU_VALUE; |
| 110 | } else if (isWaiting(state)) { |
| 111 | value = StateValues.PROCESS_STATUS_WAIT_BLOCKED_VALUE; |
| 112 | } else if (isDead(state)) { |
| 113 | value = TmfStateValue.nullValue(); |
| 114 | } else { |
| 115 | value = StateValues.PROCESS_STATUS_WAIT_UNKNOWN_VALUE; |
| 116 | } |
| 117 | int quark = ss.getQuarkRelativeAndAdd(formerThreadNode, Attributes.STATUS); |
| 118 | ss.modifyAttribute(timestamp, value, quark); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | private static boolean isDead(int state) { |
| 123 | return (state & LinuxValues.TASK_DEAD) != 0; |
| 124 | } |
| 125 | |
| 126 | private static boolean isWaiting(int state) { |
| 127 | return (state & (LinuxValues.TASK_INTERRUPTIBLE | LinuxValues.TASK_UNINTERRUPTIBLE)) != 0; |
| 128 | } |
| 129 | |
| 130 | private static boolean isRunning(int state) { |
| 131 | // special case, this means ALL STATES ARE 0 |
| 132 | // this is effectively an anti-state |
| 133 | return state == 0; |
| 134 | } |
| 135 | |
| 136 | private static void setCpuStatus(ITmfStateSystemBuilder ss, Integer nextTid, Integer newCurrentThreadNode, long timestamp, int currentCPUNode) throws AttributeNotFoundException { |
| 137 | int quark; |
| 138 | ITmfStateValue value; |
| 139 | if (nextTid > 0) { |
| 140 | /* Check if the entering process is in kernel or user mode */ |
| 141 | quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.SYSTEM_CALL); |
| 142 | ITmfStateValue queryOngoingState = ss.queryOngoingState(quark); |
| 143 | if (queryOngoingState.isNull()) { |
| 144 | value = StateValues.CPU_STATUS_RUN_USERMODE_VALUE; |
| 145 | } else { |
| 146 | value = StateValues.CPU_STATUS_RUN_SYSCALL_VALUE; |
| 147 | } |
| 148 | } else { |
| 149 | value = StateValues.CPU_STATUS_IDLE_VALUE; |
| 150 | } |
| 151 | quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS); |
| 152 | ss.modifyAttribute(timestamp, value, quark); |
| 153 | } |
| 154 | |
| 155 | private static void setCpuProcess(ITmfStateSystemBuilder ss, Integer nextTid, long timestamp, int currentCPUNode) throws AttributeNotFoundException { |
| 156 | int quark; |
| 157 | ITmfStateValue value; |
| 158 | quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.CURRENT_THREAD); |
| 159 | value = TmfStateValue.newValueInt(nextTid); |
| 160 | ss.modifyAttribute(timestamp, value, quark); |
| 161 | } |
| 162 | |
| 163 | private static void setNewProcessPrio(ITmfStateSystemBuilder ss, Integer nextPrio, Integer newCurrentThreadNode, long timestamp) throws AttributeNotFoundException { |
| 164 | int quark; |
| 165 | ITmfStateValue value; |
| 166 | quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.PRIO); |
| 167 | value = TmfStateValue.newValueInt(nextPrio); |
| 168 | ss.modifyAttribute(timestamp, value, quark); |
| 169 | } |
| 170 | |
| 171 | private static void setNewProcessExecName(ITmfStateSystemBuilder ss, String nextProcessName, Integer newCurrentThreadNode, long timestamp) throws AttributeNotFoundException { |
| 172 | int quark; |
| 173 | ITmfStateValue value; |
| 174 | quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.EXEC_NAME); |
| 175 | value = TmfStateValue.newValueString(nextProcessName); |
| 176 | ss.modifyAttribute(timestamp, value, quark); |
| 177 | } |
| 178 | |
| 179 | } |