| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2015 Ericsson |
| 3 | * Copyright (c) 2010, 2011 École Polytechnique de Montréal |
| 4 | * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com> |
| 5 | * |
| 6 | * All rights reserved. This program and the accompanying materials are |
| 7 | * made available under the terms of the Eclipse Public License v1.0 which |
| 8 | * accompanies this distribution, and is available at |
| 9 | * http://www.eclipse.org/legal/epl-v10.html |
| 10 | * |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel; |
| 14 | |
| 15 | import java.util.Map; |
| 16 | |
| 17 | import org.eclipse.jdt.annotation.Nullable; |
| 18 | import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout; |
| 19 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
| 20 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.Activator; |
| 21 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.IrqEntryHandler; |
| 22 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.IrqExitHandler; |
| 23 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.KernelEventHandler; |
| 24 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.PiSetprioHandler; |
| 25 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.ProcessExitHandler; |
| 26 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.ProcessForkHandler; |
| 27 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.ProcessFreeHandler; |
| 28 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.SchedSwitchHandler; |
| 29 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.SchedWakeupHandler; |
| 30 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.SoftIrqEntryHandler; |
| 31 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.SoftIrqExitHandler; |
| 32 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.SoftIrqRaiseHandler; |
| 33 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.StateDumpHandler; |
| 34 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.SysEntryHandler; |
| 35 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.SysExitHandler; |
| 36 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder; |
| 37 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
| 38 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; |
| 39 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; |
| 40 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 41 | import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider; |
| 42 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 43 | |
| 44 | import com.google.common.collect.ImmutableMap; |
| 45 | |
| 46 | /** |
| 47 | * This is the state change input plugin for the state system which handles the |
| 48 | * kernel traces. |
| 49 | * |
| 50 | * @author Alexandre Montplaisir |
| 51 | */ |
| 52 | public class KernelStateProvider extends AbstractTmfStateProvider { |
| 53 | |
| 54 | // ------------------------------------------------------------------------ |
| 55 | // Static fields |
| 56 | // ------------------------------------------------------------------------ |
| 57 | |
| 58 | /** |
| 59 | * Version number of this state provider. Please bump this if you modify the |
| 60 | * contents of the generated state history in some way. |
| 61 | */ |
| 62 | private static final int VERSION = 13; |
| 63 | |
| 64 | // ------------------------------------------------------------------------ |
| 65 | // Fields |
| 66 | // ------------------------------------------------------------------------ |
| 67 | |
| 68 | private final Map<String, KernelEventHandler> fEventNames; |
| 69 | private final IKernelAnalysisEventLayout fLayout; |
| 70 | |
| 71 | private final KernelEventHandler fSysEntryHandler; |
| 72 | private final KernelEventHandler fSysExitHandler; |
| 73 | |
| 74 | // ------------------------------------------------------------------------ |
| 75 | // Constructor |
| 76 | // ------------------------------------------------------------------------ |
| 77 | |
| 78 | /** |
| 79 | * Instantiate a new state provider plugin. |
| 80 | * |
| 81 | * @param trace |
| 82 | * The LTTng 2.0 kernel trace directory |
| 83 | * @param layout |
| 84 | * The event layout to use for this state provider. Usually |
| 85 | * depending on the tracer implementation. |
| 86 | */ |
| 87 | public KernelStateProvider(ITmfTrace trace, IKernelAnalysisEventLayout layout) { |
| 88 | super(trace, "Kernel"); //$NON-NLS-1$ |
| 89 | fLayout = layout; |
| 90 | fEventNames = buildEventNames(layout); |
| 91 | |
| 92 | fSysEntryHandler = new SysEntryHandler(fLayout); |
| 93 | fSysExitHandler = new SysExitHandler(fLayout); |
| 94 | } |
| 95 | |
| 96 | // ------------------------------------------------------------------------ |
| 97 | // Event names management |
| 98 | // ------------------------------------------------------------------------ |
| 99 | |
| 100 | private static Map<String, KernelEventHandler> buildEventNames(IKernelAnalysisEventLayout layout) { |
| 101 | ImmutableMap.Builder<String, KernelEventHandler> builder = ImmutableMap.builder(); |
| 102 | |
| 103 | builder.put(layout.eventIrqHandlerEntry(), new IrqEntryHandler(layout)); |
| 104 | builder.put(layout.eventIrqHandlerExit(), new IrqExitHandler(layout)); |
| 105 | builder.put(layout.eventSoftIrqEntry(), new SoftIrqEntryHandler(layout)); |
| 106 | builder.put(layout.eventSoftIrqExit(), new SoftIrqExitHandler(layout)); |
| 107 | builder.put(layout.eventSoftIrqRaise(), new SoftIrqRaiseHandler(layout)); |
| 108 | builder.put(layout.eventSchedSwitch(), new SchedSwitchHandler(layout)); |
| 109 | builder.put(layout.eventSchedPiSetprio(), new PiSetprioHandler(layout)); |
| 110 | builder.put(layout.eventSchedProcessFork(), new ProcessForkHandler(layout)); |
| 111 | builder.put(layout.eventSchedProcessExit(), new ProcessExitHandler(layout)); |
| 112 | builder.put(layout.eventSchedProcessFree(), new ProcessFreeHandler(layout)); |
| 113 | |
| 114 | final String eventStatedumpProcessState = layout.eventStatedumpProcessState(); |
| 115 | if (eventStatedumpProcessState != null) { |
| 116 | builder.put(eventStatedumpProcessState, new StateDumpHandler(layout)); |
| 117 | } |
| 118 | |
| 119 | for (String eventSchedWakeup : layout.eventsSchedWakeup()) { |
| 120 | builder.put(eventSchedWakeup, new SchedWakeupHandler(layout)); |
| 121 | } |
| 122 | |
| 123 | return builder.build(); |
| 124 | } |
| 125 | |
| 126 | // ------------------------------------------------------------------------ |
| 127 | // IStateChangeInput |
| 128 | // ------------------------------------------------------------------------ |
| 129 | |
| 130 | @Override |
| 131 | public int getVersion() { |
| 132 | return VERSION; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public KernelStateProvider getNewInstance() { |
| 137 | return new KernelStateProvider(this.getTrace(), fLayout); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | protected void eventHandle(@Nullable ITmfEvent event) { |
| 142 | if (event == null) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | final String eventName = event.getName(); |
| 147 | |
| 148 | try { |
| 149 | final ITmfStateSystemBuilder ss = NonNullUtils.checkNotNull(getStateSystemBuilder()); |
| 150 | /* |
| 151 | * Feed event to the history system if it's known to cause a state |
| 152 | * transition. |
| 153 | */ |
| 154 | KernelEventHandler handler = fEventNames.get(eventName); |
| 155 | if (handler == null) { |
| 156 | if (isSyscallExit(eventName)) { |
| 157 | handler = fSysExitHandler; |
| 158 | } else if (isSyscallEntry(eventName)) { |
| 159 | handler = fSysEntryHandler; |
| 160 | } |
| 161 | } |
| 162 | if (handler != null) { |
| 163 | handler.handleEvent(ss, event); |
| 164 | } |
| 165 | |
| 166 | } catch (AttributeNotFoundException ae) { |
| 167 | /* |
| 168 | * This would indicate a problem with the logic of the manager here, |
| 169 | * so it shouldn't happen. |
| 170 | */ |
| 171 | Activator.getDefault().logError("Attribute not found: " + ae.getMessage(), ae); //$NON-NLS-1$ |
| 172 | |
| 173 | } catch (TimeRangeException tre) { |
| 174 | /* |
| 175 | * This would happen if the events in the trace aren't ordered |
| 176 | * chronologically, which should never be the case ... |
| 177 | */ |
| 178 | Activator.getDefault().logError("TimeRangeExcpetion caught in the state system's event manager.\n" + //$NON-NLS-1$ |
| 179 | "Are the events in the trace correctly ordered?\n" + tre.getMessage(), tre); //$NON-NLS-1$ |
| 180 | |
| 181 | } catch (StateValueTypeException sve) { |
| 182 | /* |
| 183 | * This would happen if we were trying to push/pop attributes not of |
| 184 | * type integer. Which, once again, should never happen. |
| 185 | */ |
| 186 | Activator.getDefault().logError("State value error: " + sve.getMessage(), sve); //$NON-NLS-1$ |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private boolean isSyscallEntry(String eventName) { |
| 191 | return (eventName.startsWith(fLayout.eventSyscallEntryPrefix()) |
| 192 | || eventName.startsWith(fLayout.eventCompatSyscallEntryPrefix())); |
| 193 | } |
| 194 | |
| 195 | private boolean isSyscallExit(String eventName) { |
| 196 | return (eventName.startsWith(fLayout.eventSyscallExitPrefix()) || |
| 197 | eventName.startsWith(fLayout.eventCompatSyscallExitPrefix())); |
| 198 | } |
| 199 | |
| 200 | } |