1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 * Matthew Khouzam - Add the task state system from figure 3 from the spec
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.btf
.core
.analysis
;
16 import org
.eclipse
.linuxtools
.btf
.core
.event
.BtfEvent
;
17 import org
.eclipse
.linuxtools
.btf
.core
.trace
.BtfColumnNames
;
18 import org
.eclipse
.linuxtools
.btf
.core
.trace
.BtfTrace
;
19 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.AbstractTmfStateProvider
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
29 * State provider for the default BTF analysis.
31 * The generated attribute tree will look like this:
36 * | +- Core 1 (not running/Name of the running Task)
37 * | +- Core 2 (not running/Name of the running Task)
41 * | +- Core 1 (not running/running/suspended)
42 * | | +- Runnable A1 (not running/running/suspended)
43 * | | +- Runnable A2 (not running/running/suspended)
59 * @author Alexandre Montplaisir
61 public class BtfStateProvider
extends AbstractTmfStateProvider
{
63 private static final int PROVIDER_VERSION
= 3;
65 private static class TmfNamedStateValue
{
66 private final String fName
;
67 private final TmfStateValue fValue
;
69 public TmfNamedStateValue(TmfStateValue value
, String name
) {
74 public TmfStateValue
getValue() {
79 public String
toString() {
84 private static final TmfNamedStateValue STATE_CORE_IDLE
= new TmfNamedStateValue(TmfStateValue
.newValueInt(0), "Idle"); //$NON-NLS-1$
86 private static final TmfNamedStateValue STATE_NOT_RUNNING
= new TmfNamedStateValue(TmfStateValue
.nullValue(), "Not Running"); //$NON-NLS-1$
87 private static final TmfNamedStateValue STATE_RUNNING
= new TmfNamedStateValue(TmfStateValue
.newValueInt((1)), "RUNNING"); //$NON-NLS-1$
88 private static final TmfNamedStateValue STATE_SUSPENDED
= new TmfNamedStateValue(TmfStateValue
.newValueInt((2)), "SUSPENDED"); //$NON-NLS-1$
90 private static final TmfNamedStateValue STATE_TASK_ACTIVE
= new TmfNamedStateValue(TmfStateValue
.newValueInt((4)), "Active"); //$NON-NLS-1$
91 private static final TmfNamedStateValue STATE_TASK_READY
= new TmfNamedStateValue(TmfStateValue
.newValueInt((5)), "Ready"); //$NON-NLS-1$
92 private static final TmfNamedStateValue STATE_TASK_RUNNING
= new TmfNamedStateValue(TmfStateValue
.newValueInt((6)), "Task Running"); //$NON-NLS-1$
93 private static final TmfNamedStateValue STATE_TASK_WAITING
= new TmfNamedStateValue(TmfStateValue
.newValueInt((7)), "Waiting"); //$NON-NLS-1$
94 private static final TmfNamedStateValue STATE_TASK_PARKING
= new TmfNamedStateValue(TmfStateValue
.newValueInt((8)), "Parking"); //$NON-NLS-1$
95 private static final TmfNamedStateValue STATE_TASK_POLLING
= new TmfNamedStateValue(TmfStateValue
.newValueInt((9)), "Polling"); //$NON-NLS-1$
96 private static final TmfNamedStateValue STATE_TASK_TERMINATED
= new TmfNamedStateValue(TmfStateValue
.nullValue(), "Terminated"); //$NON-NLS-1$
98 private static final String ENTITY_CORE
= "Core"; //$NON-NLS-1$
99 private static final String ENTITY_TASK
= "TASK"; //$NON-NLS-1$
100 // private static final String ENTITY_RUNNABLE = "RUNNABLE";
102 private static final String ATTRIBUTE_TASKS
= "Tasks"; //$NON-NLS-1$
103 private static final String ATTRIBUTE_CORES
= "Cores"; //$NON-NLS-1$
104 private static final String ATTRIBUTE_ACTIVE_CORE
= "ActiveCore"; //$NON-NLS-1$
110 * The trace for which we will be building this state system
112 public BtfStateProvider(BtfTrace trace
) {
113 super(trace
, TmfEvent
.class, "Btf State Provider"); //$NON-NLS-1$
117 public BtfTrace
getTrace() {
118 return (BtfTrace
) super.getTrace();
122 public int getVersion() {
123 return PROVIDER_VERSION
;
127 public ITmfStateProvider
getNewInstance() {
128 return new BtfStateProvider(getTrace());
132 protected void eventHandle(ITmfEvent ev
) {
133 BtfEvent event
= (BtfEvent
) ev
;
134 final ITmfStateSystemBuilder ssb
= this.ss
;
136 final long ts
= event
.getTimestamp().getValue();
137 final String eventType
= (String
) event
.getContent().getField(BtfColumnNames
.EVENT
.toString()).getValue();
138 final String source
= event
.getSource();
139 final String target
= event
.getReference();
145 case "activate": //$NON-NLS-1$
146 updateTaskStateSystem(ssb
, ts
, event
, STATE_TASK_ACTIVE
);
149 case "start": //$NON-NLS-1$
150 case "resume": //$NON-NLS-1$
151 updateTaskStateSystem(ssb
, ts
, event
, STATE_TASK_RUNNING
);
153 if (source
.startsWith(ENTITY_CORE
)) {
154 String core
= source
;
157 quark
= ssb
.getQuarkAbsoluteAndAdd(ATTRIBUTE_TASKS
, task
, core
);
158 ssb
.modifyAttribute(ts
, STATE_RUNNING
.getValue(), quark
);
160 /* Mark this task as active in the ActiveCore attribute */
161 quark
= ssb
.getQuarkAbsoluteAndAdd(ATTRIBUTE_TASKS
, task
, ATTRIBUTE_ACTIVE_CORE
);
162 ITmfStateValue value
= TmfStateValue
.newValueString(core
);
163 ssb
.modifyAttribute(ts
, value
, quark
);
165 /* Mark this task as active in the Cores/* attribute */
166 quark
= ssb
.getQuarkAbsoluteAndAdd(ATTRIBUTE_CORES
, core
);
167 /* Until the view can display the string */
168 value
= STATE_RUNNING
.getValue();
169 ssb
.modifyAttribute(ts
, value
, quark
);
171 } else if (source
.startsWith(ENTITY_TASK
)) {
173 String runnable
= target
;
174 String core
= getCoreOfTask(ssb
, task
);
176 quark
= ssb
.getQuarkAbsoluteAndAdd(ATTRIBUTE_TASKS
, task
, core
, runnable
);
177 ssb
.modifyAttribute(ts
, STATE_RUNNING
.getValue(), quark
);
181 case "suspend": //$NON-NLS-1$
182 /* "suspend" events only happen on Tasks */
183 if (source
.startsWith(ENTITY_TASK
)) {
185 String runnable
= target
;
186 String core
= getCoreOfTask(ssb
, task
);
188 /* We'll update both the Core and Runnable attributes */
189 quark
= ssb
.getQuarkAbsoluteAndAdd(ATTRIBUTE_TASKS
, task
, core
);
190 ssb
.modifyAttribute(ts
, STATE_SUSPENDED
.getValue(), quark
);
191 quark
= ssb
.getQuarkRelativeAndAdd(quark
, runnable
);
192 ss
.modifyAttribute(ts
, STATE_SUSPENDED
.getValue(), quark
);
196 case "terminate": //$NON-NLS-1$
197 updateTaskStateSystem(ssb
, ts
, event
, STATE_TASK_TERMINATED
);
199 if (source
.startsWith(ENTITY_CORE
)) {
200 String core
= source
;
203 quark
= ssb
.getQuarkAbsoluteAndAdd(ATTRIBUTE_TASKS
, task
, core
);
204 ssb
.modifyAttribute(ts
, STATE_NOT_RUNNING
.getValue(), quark
);
206 /* Remove our "active task on core" bookmark */
207 quark
= ssb
.getQuarkAbsoluteAndAdd(ATTRIBUTE_TASKS
, task
, ATTRIBUTE_ACTIVE_CORE
);
208 ITmfStateValue value
= TmfStateValue
.nullValue();
209 ssb
.modifyAttribute(ts
, value
, quark
);
211 /* Mark the Cores/* attribute as not running */
212 quark
= ssb
.getQuarkAbsoluteAndAdd(ATTRIBUTE_CORES
, core
);
213 ssb
.modifyAttribute(ts
, STATE_CORE_IDLE
.getValue(), quark
);
215 } else if (source
.startsWith(ENTITY_TASK
)) {
217 String runnable
= target
;
218 String core
= getCoreOfTask(ssb
, task
);
220 quark
= ssb
.getQuarkAbsoluteAndAdd(ATTRIBUTE_TASKS
, task
, core
, runnable
);
221 ssb
.modifyAttribute(ts
, STATE_NOT_RUNNING
.getValue(), quark
);
225 case "preempt": //$NON-NLS-1$
226 case "release": //$NON-NLS-1$
227 case "release_parking": //$NON-NLS-1$
228 updateTaskStateSystem(ssb
, ts
, event
, STATE_TASK_READY
);
230 case "wait": //$NON-NLS-1$
231 updateTaskStateSystem(ssb
, ts
, event
, STATE_TASK_WAITING
);
233 case "park": //$NON-NLS-1$
234 updateTaskStateSystem(ssb
, ts
, event
, STATE_TASK_PARKING
);
236 case "poll": //$NON-NLS-1$
238 case "poll_parking": //$NON-NLS-1$
239 updateTaskStateSystem(ssb
, ts
, event
, STATE_TASK_POLLING
);
241 case "run": //$NON-NLS-1$
242 updateTaskStateSystem(ssb
, ts
, event
, STATE_TASK_RUNNING
);
248 } catch (AttributeNotFoundException e
) {
249 throw new IllegalStateException(e
);
253 private static void updateTaskStateSystem(
254 final ITmfStateSystemBuilder ssb
,
255 final long ts
, BtfEvent event
,
256 TmfNamedStateValue stateValue
)
257 throws AttributeNotFoundException
{
258 String name
= event
.getType().getName();
259 if (name
.equals("Task")) { //$NON-NLS-1$
260 String task
= event
.getReference();
261 int quark
= ssb
.getQuarkAbsoluteAndAdd(ATTRIBUTE_TASKS
, task
);
262 ssb
.modifyAttribute(ts
, stateValue
.getValue(), quark
);
266 private static String
getCoreOfTask(ITmfStateSystemBuilder ssb
, String task
) {
268 int quark
= ssb
.getQuarkAbsolute(ATTRIBUTE_TASKS
, task
, ATTRIBUTE_ACTIVE_CORE
);
269 ITmfStateValue value
= ssb
.queryOngoingState(quark
);
270 return value
.unboxStr();
271 } catch (AttributeNotFoundException e
) {
272 throw new IllegalStateException(e
);