1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 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 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.statesystem
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import org
.eclipse
.jdt
.annotation
.NonNull
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
19 import org
.eclipse
.tracecompass
.common
.core
.collect
.BufferedBlockingQueue
;
20 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
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
.timestamp
.ITmfTimestamp
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
30 * Instead of using IStateChangeInput directly, one can extend this class, which
31 * defines a lot of the common functions of the state change input plugin.
33 * It will handle the state-system-processing in a separate thread, which is
34 * normally not a bad idea for traces of some size.
36 * processEvent() is replaced with eventHandle(), so that all the multi-thread
37 * logic is abstracted away.
39 * @author Alexandre Montplaisir
41 public abstract class AbstractTmfStateProvider
implements ITmfStateProvider
{
43 private static final int DEFAULT_EVENTS_QUEUE_SIZE
= 127;
44 private static final int DEFAULT_EVENTS_CHUNK_SIZE
= 127;
46 private final ITmfTrace fTrace
;
47 private final BufferedBlockingQueue
<ITmfEvent
> fEventsQueue
;
48 private final Thread fEventHandlerThread
;
50 private boolean fStateSystemAssigned
;
52 /** State system in which to insert the state changes */
53 private @Nullable ITmfStateSystemBuilder fSS
= null;
56 * Instantiate a new state provider plugin.
59 * The LTTng 2.0 kernel trace directory
61 * Name given to this state change input. Only used internally.
63 public AbstractTmfStateProvider(ITmfTrace trace
, String id
) {
65 fEventsQueue
= new BufferedBlockingQueue
<>(DEFAULT_EVENTS_QUEUE_SIZE
, DEFAULT_EVENTS_CHUNK_SIZE
);
66 fStateSystemAssigned
= false;
68 fEventHandlerThread
= new Thread(new EventProcessor(), id
+ " Event Handler"); //$NON-NLS-1$
72 * Get the state system builder of this provider (to insert states in).
74 * @return The state system object to be filled
76 protected @Nullable ITmfStateSystemBuilder
getStateSystemBuilder() {
81 public ITmfTrace
getTrace() {
86 public long getStartTime() {
87 return fTrace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
91 public void assignTargetStateSystem(ITmfStateSystemBuilder ssb
) {
93 fStateSystemAssigned
= true;
94 fEventHandlerThread
.start();
98 public @Nullable ITmfStateSystem
getAssignedStateSystem() {
103 public void dispose() {
104 /* Insert a null event in the queue to stop the event handler's thread. */
106 fEventsQueue
.put(END_EVENT
);
107 fEventsQueue
.flushInputBuffer();
108 fEventHandlerThread
.join();
109 } catch (InterruptedException e
) {
112 fStateSystemAssigned
= false;
117 public final void processEvent(ITmfEvent event
) {
118 /* Make sure the target state system has been assigned */
119 if (!fStateSystemAssigned
) {
120 Activator
.logError("Cannot process event without a target state system"); //$NON-NLS-1$
124 /* Insert the event we're received into the events queue */
125 ITmfEvent curEvent
= event
;
126 fEventsQueue
.put(curEvent
);
130 * Block the caller until the events queue is empty.
132 public void waitForEmptyQueue() {
134 * We will first insert a dummy event that is guaranteed to not modify
135 * the state. That way, when that event leaves the queue, we will know
136 * for sure that the state system processed the preceding real event.
139 fEventsQueue
.put(EMPTY_QUEUE_EVENT
);
140 fEventsQueue
.flushInputBuffer();
141 while (!fEventsQueue
.isEmpty()) {
144 } catch (InterruptedException e
) {
149 // ------------------------------------------------------------------------
150 // Special event types
151 // ------------------------------------------------------------------------
153 /** Fake event indicating the build is over, and the provider should close */
154 private static class EndEvent
extends TmfEvent
{
156 super(null, ITmfContext
.UNKNOWN_RANK
, null, null, null);
160 /** Fake event indicating we want to clear the current queue */
161 private static class EmptyQueueEvent
extends TmfEvent
{
162 public EmptyQueueEvent() {
163 super(null, ITmfContext
.UNKNOWN_RANK
, null, null, null);
167 private static final EndEvent END_EVENT
= new EndEvent();
168 private static final EmptyQueueEvent EMPTY_QUEUE_EVENT
= new EmptyQueueEvent();
170 // ------------------------------------------------------------------------
172 // ------------------------------------------------------------------------
175 * This is the runner class for the second thread, which will take the
176 * events from the queue and pass them through the state system.
178 private class EventProcessor
implements Runnable
{
180 private @Nullable ITmfEvent currentEvent
;
184 if (!fStateSystemAssigned
) {
185 Activator
.logError("Cannot run event manager without assigning a target state system first!"); //$NON-NLS-1$
191 * We never insert null in the queue. Cannot be checked at
192 * compile-time until Java 8 annotations...
194 @NonNull ITmfEvent event
= checkNotNull(fEventsQueue
.take());
195 /* This is a singleton, we want to do != instead of !x.equals */
196 while (event
!= END_EVENT
) {
197 if (event
== EMPTY_QUEUE_EVENT
) {
198 /* Synchronization event, should be ignored */
199 event
= checkNotNull(fEventsQueue
.take());
202 currentEvent
= event
;
204 event
= checkNotNull(fEventsQueue
.take());
206 /* We've received the last event, clean up */
210 private void closeStateSystem() {
211 ITmfEvent event
= currentEvent
;
212 final long endTime
= (event
== null) ?
0 :
213 event
.getTimestamp().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
216 fSS
.closeHistory(endTime
);
221 // ------------------------------------------------------------------------
223 // ------------------------------------------------------------------------
226 * Handle the given event and send the appropriate state transitions into
227 * the the state system.
229 * This is basically the same thing as IStateChangeInput.processEvent(),
230 * except here processEvent() and eventHandle() are run in two different
231 * threads (and the AbstractStateChangeInput takes care of processEvent()
235 * The event to process. If you need a specific event type, you
236 * should check for its instance right at the beginning.
238 protected abstract void eventHandle(ITmfEvent event
);