| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Alexis Cabana-Loriaux - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.analysis.os.linux.core.contextswitch; |
| 14 | |
| 15 | import org.eclipse.jdt.annotation.NonNullByDefault; |
| 16 | import org.eclipse.jdt.annotation.Nullable; |
| 17 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.Attributes; |
| 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.statesystem.core.ITmfStateSystemBuilder; |
| 22 | import org.eclipse.tracecompass.statesystem.core.StateSystemBuilderUtils; |
| 23 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
| 24 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; |
| 25 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 26 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect; |
| 27 | import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider; |
| 28 | import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider; |
| 29 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 30 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
| 31 | |
| 32 | /** |
| 33 | * Class used to build a state system of the context switches of a trace |
| 34 | * |
| 35 | * @author Alexis Cabana-Loriaux |
| 36 | * @since 2.0 |
| 37 | */ |
| 38 | @NonNullByDefault |
| 39 | public class KernelContextSwitchStateProvider extends AbstractTmfStateProvider { |
| 40 | |
| 41 | private static final String ID = "org.eclipse.tracecompass.analysis.os.linux.contextswitch.stateprovider"; //$NON-NLS-1$ |
| 42 | private static final int STARTING_QUARK = -1; |
| 43 | private int fCpuAttributeQuark = STARTING_QUARK; |
| 44 | private @Nullable ITmfStateSystemBuilder fStateSystemBuilder; |
| 45 | private IKernelAnalysisEventLayout fLayout; |
| 46 | |
| 47 | /** |
| 48 | * Default constructor |
| 49 | * |
| 50 | * @param trace |
| 51 | * the trace |
| 52 | * @param layout |
| 53 | * the associated layout |
| 54 | */ |
| 55 | public KernelContextSwitchStateProvider(ITmfTrace trace, IKernelAnalysisEventLayout layout) { |
| 56 | super(trace, ID); |
| 57 | fLayout = layout; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public int getVersion() { |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public ITmfStateProvider getNewInstance() { |
| 67 | return new KernelContextSwitchStateProvider(getTrace(), fLayout); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Classify sched_switch events for every CPU |
| 72 | */ |
| 73 | @Override |
| 74 | protected void eventHandle(ITmfEvent event) { |
| 75 | ITmfStateSystemBuilder stateSystemBuilder = fStateSystemBuilder; |
| 76 | if (stateSystemBuilder == null) { |
| 77 | stateSystemBuilder = (ITmfStateSystemBuilder) getAssignedStateSystem(); |
| 78 | fStateSystemBuilder = stateSystemBuilder; |
| 79 | } |
| 80 | if (stateSystemBuilder == null) { |
| 81 | return; |
| 82 | } |
| 83 | if (fCpuAttributeQuark == STARTING_QUARK) { |
| 84 | fCpuAttributeQuark = stateSystemBuilder.getQuarkAbsoluteAndAdd(Attributes.CPUS); |
| 85 | } |
| 86 | if (event.getName().equals(fLayout.eventSchedSwitch())) { |
| 87 | Object cpuObj = TmfTraceUtils.resolveEventAspectOfClassForEvent(event.getTrace(), TmfCpuAspect.class, event); |
| 88 | if (cpuObj == null) { |
| 89 | /* We couldn't find any CPU information, ignore this event */ |
| 90 | return; |
| 91 | } |
| 92 | int cpuQuark = stateSystemBuilder.getQuarkRelativeAndAdd(fCpuAttributeQuark, cpuObj.toString()); |
| 93 | try { |
| 94 | StateSystemBuilderUtils.incrementAttributeInt(stateSystemBuilder, event.getTimestamp().getValue(), cpuQuark, 1); |
| 95 | } catch (StateValueTypeException | AttributeNotFoundException e) { |
| 96 | Activator.getDefault().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | } |