1 /*******************************************************************************
2 * Copyright (c) 2012 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
.event
.ITmfEvent
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
25 * Instead of using IStateChangeInput directly, one can extend this class, which
26 * defines a lot of the common functions of the state change input plugin.
28 * It will handle the state-system-processing in a separate thread, which is
29 * normally not a bad idea for traces of some size.
31 * processEvent() is replaced with eventHandle(), so that all the multi-thread
32 * logic is abstracted away.
34 * @author Alexandre Montplaisir
37 public abstract class AbstractStateChangeInput
implements IStateChangeInput
{
39 private static final int DEFAULT_EVENTS_QUEUE_SIZE
= 10000;
41 private final ITmfTrace trace
;
42 private final Class
<?
extends ITmfEvent
> eventType
;
43 private final BlockingQueue
<ITmfEvent
> eventsQueue
;
44 private final Thread eventHandlerThread
;
46 private boolean ssAssigned
;
47 private ITmfEvent currentEvent
;
49 /** State system in which to insert the state changes */
50 protected ITmfStateSystemBuilder ss
;
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 AbstractStateChangeInput(ITmfTrace trace
,
64 Class
<?
extends ITmfEvent
> eventType
, String id
) {
66 this.eventType
= eventType
;
67 eventsQueue
= new ArrayBlockingQueue
<ITmfEvent
>(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$
76 public ITmfTrace
getTrace() {
81 public long getStartTime() {
82 return trace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
86 public void assignTargetStateSystem(ITmfStateSystemBuilder ssb
) {
89 eventHandlerThread
.start();
93 public void dispose() {
94 /* Insert a null event in the queue to stop the event handler's thread. */
96 eventsQueue
.put(org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfEvent
.getNullEvent());
97 eventHandlerThread
.join();
98 } catch (InterruptedException e
) {
106 public final Class
<?
extends ITmfEvent
> getExpectedEventType() {
111 public final void processEvent(ITmfEvent event
) {
112 /* Make sure the target state system has been assigned */
114 System
.err
.println("Cannot process event without a target state system"); //$NON-NLS-1$
118 /* Insert the event we're received into the events queue */
119 ITmfEvent curEvent
= event
;
121 eventsQueue
.put(curEvent
);
122 } catch (InterruptedException e
) {
128 * This is the runner class for the second thread, which will take the
129 * events from the queue and pass them through the state system.
131 private class EventProcessor
implements Runnable
{
136 System
.err
.println("Cannot run event manager without assigning a target state system first!"); //$NON-NLS-1$
142 event
= eventsQueue
.take();
143 while (event
.getTimestamp().getValue() != -1) {
144 currentEvent
= event
;
146 /* Make sure this is an event the sub-class can process */
147 if (eventType
.isInstance(event
)) {
150 event
= eventsQueue
.take();
152 /* We've received the last event, clean up */
155 } catch (InterruptedException e
) {
156 /* We've been interrupted abnormally */
157 System
.out
.println("Event handler interrupted!"); //$NON-NLS-1$
162 private void closeStateSystem() {
163 /* Close the History system, if there is one */
164 if (currentEvent
== null) {
168 ss
.closeHistory(currentEvent
.getTimestamp().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue());
169 } catch (TimeRangeException e
) {
171 * Since we're using currentEvent.getTimestamp, this shouldn't
179 // ------------------------------------------------------------------------
181 // ------------------------------------------------------------------------
184 * Handle the given event and send the appropriate state transitions into
185 * the the state system.
187 * This is basically the same thing as IStateChangeInput.processEvent(),
188 * except here processEvent() and eventHandle() are run in two different
189 * threads (and the AbstractStateChangeInput takes care of processEvent()
193 * The event to process. If you need a specific event type, you
194 * should check for its instance right at the beginning.
196 protected abstract void eventHandle(ITmfEvent event
);