TMF: Add functions to verify if events are present in a CTF Trace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statesystem / AbstractTmfStateProvider.java
CommitLineData
79e0a1df 1/*******************************************************************************
61759503 2 * Copyright (c) 2012, 2013 Ericsson
79e0a1df
AM
3 *
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
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.statesystem;
14
15import java.util.concurrent.ArrayBlockingQueue;
16import java.util.concurrent.BlockingQueue;
17
6cfa0200 18import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEventFactory;
79e0a1df 19import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
1b9d3765 20import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
79e0a1df 21import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
3bd46eef 22import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
1b9d3765 23import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
79e0a1df
AM
24import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
25
26
27/**
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.
30 *
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.
33 *
34 * processEvent() is replaced with eventHandle(), so that all the multi-thread
35 * logic is abstracted away.
36 *
37 * @author Alexandre Montplaisir
38 * @since 2.0
39 */
0fe46f2a 40public abstract class AbstractTmfStateProvider implements ITmfStateProvider {
79e0a1df
AM
41
42 private static final int DEFAULT_EVENTS_QUEUE_SIZE = 10000;
43
79e0a1df 44 private final ITmfTrace trace;
79044a66
AM
45 private final Class<? extends ITmfEvent> eventType;
46 private final BlockingQueue<ITmfEvent> eventsQueue;
79e0a1df
AM
47 private final Thread eventHandlerThread;
48
49 private boolean ssAssigned;
79e0a1df
AM
50 private ITmfEvent currentEvent;
51
6f4e8ec0 52 /** State system in which to insert the state changes */
339d27b4 53 protected ITmfStateSystemBuilder ss = null;
6f4e8ec0 54
79e0a1df
AM
55 /**
56 * Instantiate a new state provider plugin.
57 *
58 * @param trace
59 * The LTTng 2.0 kernel trace directory
79044a66
AM
60 * @param eventType
61 * The specific class for the event type that will be used within
62 * the subclass
71f2da63
AM
63 * @param id
64 * Name given to this state change input. Only used internally.
79e0a1df 65 */
0fe46f2a 66 public AbstractTmfStateProvider(ITmfTrace trace,
71f2da63 67 Class<? extends ITmfEvent> eventType, String id) {
79e0a1df 68 this.trace = trace;
79044a66
AM
69 this.eventType = eventType;
70 eventsQueue = new ArrayBlockingQueue<ITmfEvent>(DEFAULT_EVENTS_QUEUE_SIZE);
79044a66 71 ssAssigned = false;
71f2da63
AM
72
73 String id2 = (id == null ? "Unamed" : id); //$NON-NLS-1$
74 eventHandlerThread = new Thread(new EventProcessor(), id2 + " Event Handler"); //$NON-NLS-1$
75
79e0a1df
AM
76 }
77
78 @Override
79 public ITmfTrace getTrace() {
80 return trace;
81 }
82
83 @Override
84 public long getStartTime() {
faa38350 85 return trace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
79e0a1df
AM
86 }
87
88 @Override
f1f86dfb 89 public void assignTargetStateSystem(ITmfStateSystemBuilder ssb) {
79e0a1df
AM
90 ss = ssb;
91 ssAssigned = true;
92 eventHandlerThread.start();
93 }
94
7e634be6
AM
95 @Override
96 public ITmfStateSystem getAssignedStateSystem() {
97 return ss;
98 }
99
79e0a1df
AM
100 @Override
101 public void dispose() {
102 /* Insert a null event in the queue to stop the event handler's thread. */
103 try {
6cfa0200 104 eventsQueue.put(CtfTmfEventFactory.getNullEvent());
79e0a1df
AM
105 eventHandlerThread.join();
106 } catch (InterruptedException e) {
107 e.printStackTrace();
108 }
109 ssAssigned = false;
110 ss = null;
111 }
112
113 @Override
79044a66
AM
114 public final Class<? extends ITmfEvent> getExpectedEventType() {
115 return eventType;
116 }
117
118 @Override
119 public final void processEvent(ITmfEvent event) {
79e0a1df
AM
120 /* Make sure the target state system has been assigned */
121 if (!ssAssigned) {
122 System.err.println("Cannot process event without a target state system"); //$NON-NLS-1$
123 return;
124 }
125
126 /* Insert the event we're received into the events queue */
127 ITmfEvent curEvent = event;
128 try {
129 eventsQueue.put(curEvent);
130 } catch (InterruptedException e) {
131 e.printStackTrace();
132 }
133 }
134
1b9d3765
AM
135 /**
136 * Block the caller until the events queue is empty.
137 */
138 public void waitForEmptyQueue() {
139 /*
140 * We will first insert a dummy event that is guaranteed to not modify
141 * the state. That way, when that event leaves the queue, we will know
142 * for sure that the state system processed the preceding real event.
143 */
144 TmfTimestamp ts = new TmfTimestamp(0); /* it must not be -1! */
145 TmfEvent ev = new TmfEvent(null, ts, null, null, null, null);
146
147 try {
148 eventsQueue.put(ev);
149 while (!eventsQueue.isEmpty()) {
150 Thread.sleep(100);
151 }
152 } catch (InterruptedException e) {
153 e.printStackTrace();
154 }
155 }
156
79e0a1df
AM
157 /**
158 * This is the runner class for the second thread, which will take the
159 * events from the queue and pass them through the state system.
160 */
161 private class EventProcessor implements Runnable {
162
163 @Override
164 public void run() {
165 if (ss == null) {
166 System.err.println("Cannot run event manager without assigning a target state system first!"); //$NON-NLS-1$
167 return;
168 }
169 ITmfEvent event;
170
171 try {
172 event = eventsQueue.take();
173 while (event.getTimestamp().getValue() != -1) {
174 currentEvent = event;
79044a66
AM
175
176 /* Make sure this is an event the sub-class can process */
1b9d3765 177 if (eventType.isInstance(event) && event.getType() != null) {
79044a66
AM
178 eventHandle(event);
179 }
79e0a1df
AM
180 event = eventsQueue.take();
181 }
182 /* We've received the last event, clean up */
183 closeStateSystem();
184 return;
185 } catch (InterruptedException e) {
186 /* We've been interrupted abnormally */
187 System.out.println("Event handler interrupted!"); //$NON-NLS-1$
188 e.printStackTrace();
189 }
190 }
191
192 private void closeStateSystem() {
193 /* Close the History system, if there is one */
194 if (currentEvent == null) {
195 return;
196 }
197 try {
faa38350 198 ss.closeHistory(currentEvent.getTimestamp().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue());
79e0a1df
AM
199 } catch (TimeRangeException e) {
200 /*
201 * Since we're using currentEvent.getTimestamp, this shouldn't
202 * cause any problem
203 */
204 e.printStackTrace();
205 }
206 }
207 }
208
209 // ------------------------------------------------------------------------
210 // Abstract methods
211 // ------------------------------------------------------------------------
212
79e0a1df
AM
213 /**
214 * Handle the given event and send the appropriate state transitions into
215 * the the state system.
216 *
217 * This is basically the same thing as IStateChangeInput.processEvent(),
218 * except here processEvent() and eventHandle() are run in two different
219 * threads (and the AbstractStateChangeInput takes care of processEvent()
220 * already).
221 *
222 * @param event
223 * The event to process. If you need a specific event type, you
224 * should check for its instance right at the beginning.
225 */
226 protected abstract void eventHandle(ITmfEvent event);
227}
This page took 0.056056 seconds and 5 git commands to generate.