1 /*******************************************************************************
2 * Copyright (c) 2012, 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 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.statesystem
;
15 import java
.util
.concurrent
.ArrayBlockingQueue
;
16 import java
.util
.concurrent
.BlockingQueue
;
18 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystem
;
19 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystemBuilder
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
26 * Instead of using IStateChangeInput directly, one can extend this class, which
27 * defines a lot of the common functions of the state change input plugin.
29 * It will handle the state-system-processing in a separate thread, which is
30 * normally not a bad idea for traces of some size.
32 * processEvent() is replaced with eventHandle(), so that all the multi-thread
33 * logic is abstracted away.
35 * @author Alexandre Montplaisir
38 public abstract class AbstractTmfStateProvider
implements ITmfStateProvider
{
40 private static final int DEFAULT_EVENTS_QUEUE_SIZE
= 10000;
42 private final ITmfTrace trace
;
43 private final Class
<?
extends ITmfEvent
> eventType
;
44 private final BlockingQueue
<ITmfEvent
> eventsQueue
;
45 private final Thread eventHandlerThread
;
47 private boolean ssAssigned
;
49 /** State system in which to insert the state changes */
50 protected ITmfStateSystemBuilder ss
= null;
53 * Instantiate a new state provider plugin.
56 * The LTTng 2.0 kernel trace directory
58 * The specific class for the event type that will be used within
61 * Name given to this state change input. Only used internally.
63 public AbstractTmfStateProvider(ITmfTrace trace
,
64 Class
<?
extends ITmfEvent
> eventType
, String id
) {
66 this.eventType
= eventType
;
67 eventsQueue
= new ArrayBlockingQueue
<>(DEFAULT_EVENTS_QUEUE_SIZE
);
70 String id2
= (id
== null ?
"Unamed" : id
); //$NON-NLS-1$
71 eventHandlerThread
= new Thread(new EventProcessor(), id2
+ " Event Handler"); //$NON-NLS-1$
75 public ITmfTrace
getTrace() {
83 public long getStartTime() {
84 return trace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
91 public void assignTargetStateSystem(ITmfStateSystemBuilder ssb
) {
94 eventHandlerThread
.start();
101 public ITmfStateSystem
getAssignedStateSystem() {
106 public void dispose() {
107 /* Insert a null event in the queue to stop the event handler's thread. */
109 eventsQueue
.put(END_EVENT
);
110 eventHandlerThread
.join();
111 } catch (InterruptedException e
) {
119 public final Class
<?
extends ITmfEvent
> getExpectedEventType() {
124 public final void processEvent(ITmfEvent event
) {
125 /* Make sure the target state system has been assigned */
127 System
.err
.println("Cannot process event without a target state system"); //$NON-NLS-1$
131 /* Insert the event we're received into the events queue */
132 ITmfEvent curEvent
= event
;
134 eventsQueue
.put(curEvent
);
135 } catch (InterruptedException e
) {
141 * Block the caller until the events queue is empty.
143 public void waitForEmptyQueue() {
145 * We will first insert a dummy event that is guaranteed to not modify
146 * the state. That way, when that event leaves the queue, we will know
147 * for sure that the state system processed the preceding real event.
150 eventsQueue
.put(EMPTY_QUEUE_EVENT
);
151 while (!eventsQueue
.isEmpty()) {
154 } catch (InterruptedException e
) {
159 // ------------------------------------------------------------------------
160 // Special event types
161 // ------------------------------------------------------------------------
163 /** Fake event indicating the build is over, and the provider should close */
164 private static class EndEvent
extends TmfEvent
{}
165 /** Fake event indicating we want to clear the current queue */
166 private static class EmptyQueueEvent
extends TmfEvent
{}
168 private static final EndEvent END_EVENT
= new EndEvent();
169 private static final EmptyQueueEvent EMPTY_QUEUE_EVENT
= new EmptyQueueEvent();
171 // ------------------------------------------------------------------------
173 // ------------------------------------------------------------------------
176 * This is the runner class for the second thread, which will take the
177 * events from the queue and pass them through the state system.
179 private class EventProcessor
implements Runnable
{
181 private ITmfEvent currentEvent
;
186 System
.err
.println("Cannot run event manager without assigning a target state system first!"); //$NON-NLS-1$
192 event
= eventsQueue
.take();
193 /* This is a singleton, we want to do != instead of !x.equals */
194 while (event
!= END_EVENT
) {
195 if (event
== EMPTY_QUEUE_EVENT
) {
196 /* Synchronization event, should be ignored */
197 event
= eventsQueue
.take();
201 currentEvent
= event
;
203 /* Make sure this is an event the sub-class can process */
204 if (eventType
.isInstance(event
) && event
.getType() != null) {
207 event
= eventsQueue
.take();
209 /* We've received the last event, clean up */
211 } catch (InterruptedException e
) {
212 /* We've been interrupted abnormally */
213 System
.out
.println("Event handler interrupted!"); //$NON-NLS-1$
218 private void closeStateSystem() {
219 final long endTime
= (currentEvent
== null) ?
0 :
220 currentEvent
.getTimestamp().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
221 ss
.closeHistory(endTime
);
225 // ------------------------------------------------------------------------
227 // ------------------------------------------------------------------------
230 * Handle the given event and send the appropriate state transitions into
231 * the the state system.
233 * This is basically the same thing as IStateChangeInput.processEvent(),
234 * except here processEvent() and eventHandle() are run in two different
235 * threads (and the AbstractStateChangeInput takes care of processEvent()
239 * The event to process. If you need a specific event type, you
240 * should check for its instance right at the beginning.
242 protected abstract void eventHandle(ITmfEvent event
);