1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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
.linuxtools
.tmf
.core
.statesystem
;
15 import java
.util
.concurrent
.ArrayBlockingQueue
;
16 import java
.util
.concurrent
.BlockingQueue
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfEventFactory
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
28 * Instead of using IStateChangeInput directly, one can extend this class, which
29 * defines a lot of the common functions of the state change input plugin.
31 * It will handle the state-system-processing in a separate thread, which is
32 * normally not a bad idea for traces of some size.
34 * processEvent() is replaced with eventHandle(), so that all the multi-thread
35 * logic is abstracted away.
37 * @author Alexandre Montplaisir
40 public abstract class AbstractTmfStateProvider
implements ITmfStateProvider
{
42 private static final int DEFAULT_EVENTS_QUEUE_SIZE
= 10000;
44 private final ITmfTrace trace
;
45 private final Class
<?
extends ITmfEvent
> eventType
;
46 private final BlockingQueue
<ITmfEvent
> eventsQueue
;
47 private final Thread eventHandlerThread
;
49 private boolean ssAssigned
;
50 private ITmfEvent currentEvent
;
52 /** State system in which to insert the state changes */
53 protected ITmfStateSystemBuilder ss
= null;
56 * Instantiate a new state provider plugin.
59 * The LTTng 2.0 kernel trace directory
61 * The specific class for the event type that will be used within
64 * Name given to this state change input. Only used internally.
66 public AbstractTmfStateProvider(ITmfTrace trace
,
67 Class
<?
extends ITmfEvent
> eventType
, String id
) {
69 this.eventType
= eventType
;
70 eventsQueue
= new ArrayBlockingQueue
<>(DEFAULT_EVENTS_QUEUE_SIZE
);
73 String id2
= (id
== null ?
"Unamed" : id
); //$NON-NLS-1$
74 eventHandlerThread
= new Thread(new EventProcessor(), id2
+ " Event Handler"); //$NON-NLS-1$
78 public ITmfTrace
getTrace() {
83 public long getStartTime() {
84 return trace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
88 public void assignTargetStateSystem(ITmfStateSystemBuilder ssb
) {
91 eventHandlerThread
.start();
95 public ITmfStateSystem
getAssignedStateSystem() {
100 public void dispose() {
101 /* Insert a null event in the queue to stop the event handler's thread. */
103 eventsQueue
.put(CtfTmfEventFactory
.getNullEvent());
104 eventHandlerThread
.join();
105 } catch (InterruptedException e
) {
113 public final Class
<?
extends ITmfEvent
> getExpectedEventType() {
118 public final void processEvent(ITmfEvent event
) {
119 /* Make sure the target state system has been assigned */
121 System
.err
.println("Cannot process event without a target state system"); //$NON-NLS-1$
125 /* Insert the event we're received into the events queue */
126 ITmfEvent curEvent
= event
;
128 eventsQueue
.put(curEvent
);
129 } catch (InterruptedException e
) {
135 * Block the caller until the events queue is empty.
137 public void waitForEmptyQueue() {
139 * We will first insert a dummy event that is guaranteed to not modify
140 * the state. That way, when that event leaves the queue, we will know
141 * for sure that the state system processed the preceding real event.
143 TmfEvent ev
= new EmptyEvent();
147 while (!eventsQueue
.isEmpty()) {
150 } catch (InterruptedException e
) {
155 // ------------------------------------------------------------------------
157 // ------------------------------------------------------------------------
160 * Empty event that should be totally ignored by the event handler. It can
161 * by used for synchronisation purposes.
163 private class EmptyEvent
extends TmfEvent
{
164 public EmptyEvent() {
165 super(null, new TmfTimestamp(0), null, null, null, null);
170 * This is the runner class for the second thread, which will take the
171 * events from the queue and pass them through the state system.
173 private class EventProcessor
implements Runnable
{
178 System
.err
.println("Cannot run event manager without assigning a target state system first!"); //$NON-NLS-1$
184 event
= eventsQueue
.take();
185 while (event
.getTimestamp().getValue() != -1) {
186 if (event
instanceof EmptyEvent
) {
187 /* Synchronization event, should be ignored */
188 event
= eventsQueue
.take();
192 currentEvent
= event
;
194 /* Make sure this is an event the sub-class can process */
195 if (eventType
.isInstance(event
) && event
.getType() != null) {
198 event
= eventsQueue
.take();
200 /* We've received the last event, clean up */
202 } catch (InterruptedException e
) {
203 /* We've been interrupted abnormally */
204 System
.out
.println("Event handler interrupted!"); //$NON-NLS-1$
209 private void closeStateSystem() {
210 /* Close the History system, if there is one */
211 if (currentEvent
== null) {
215 ss
.closeHistory(currentEvent
.getTimestamp().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue());
216 } catch (TimeRangeException e
) {
218 * Since we're using currentEvent.getTimestamp, this shouldn't
226 // ------------------------------------------------------------------------
228 // ------------------------------------------------------------------------
231 * Handle the given event and send the appropriate state transitions into
232 * the the state system.
234 * This is basically the same thing as IStateChangeInput.processEvent(),
235 * except here processEvent() and eventHandle() are run in two different
236 * threads (and the AbstractStateChangeInput takes care of processEvent()
240 * The event to process. If you need a specific event type, you
241 * should check for its instance right at the beginning.
243 protected abstract void eventHandle(ITmfEvent event
);