Fix timing issue in UI tests.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core / src / org / eclipse / linuxtools / lttng / core / trace / LTTngExperiment.java
CommitLineData
82e04272
FC
1/*******************************************************************************
2 * Copyright (c) 2010 Ericsson
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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
6c13869b 13package org.eclipse.linuxtools.lttng.core.trace;
82e04272 14
6c13869b
FC
15import org.eclipse.linuxtools.lttng.core.event.LttngEvent;
16import org.eclipse.linuxtools.lttng.core.event.LttngTimestamp;
1b70b6dc 17import org.eclipse.linuxtools.lttng.core.tracecontrol.utility.LiveTraceManager;
a79913eb 18import org.eclipse.linuxtools.lttng.jni.JniTrace;
6c13869b
FC
19import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
20import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
21import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
22import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment;
23import org.eclipse.linuxtools.tmf.core.experiment.TmfExperimentContext;
24import org.eclipse.linuxtools.tmf.core.experiment.TmfExperimentLocation;
25import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
6c13869b 26import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
1b70b6dc 27import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
6c13869b
FC
28import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentRangeUpdatedSignal;
29import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
30import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
31import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
32import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
82e04272
FC
33
34/**
35 * <b><u>LTTngExperiment</u></b>
36 * <p>
37 * Temporary class to resolve a basic incompatibility between TMF and LTTng.
38 * <p>
39 */
12c155f5 40public class LTTngExperiment<T extends TmfEvent> extends TmfExperiment<T> {
82e04272 41
a79913eb 42 private static final int DEFAULT_INDEX_PAGE_SIZE = 50000;
82e04272 43
a79913eb 44 // ------------------------------------------------------------------------
82e04272
FC
45 // Constructors
46 // ------------------------------------------------------------------------
47
48 /**
49 * @param type
50 * @param id
51 * @param traces
52 * @param epoch
53 * @param indexPageSize
54 */
12c155f5 55 public LTTngExperiment(Class<T> type, String id, ITmfTrace<T>[] traces, TmfTimestamp epoch, int indexPageSize) {
82e04272 56 this(type, id, traces, TmfTimestamp.Zero, indexPageSize, false);
a79913eb 57 }
82e04272 58
12c155f5 59 public LTTngExperiment(Class<T> type, String id, ITmfTrace<T>[] traces, TmfTimestamp epoch, int indexPageSize, boolean preIndexExperiment) {
a79913eb
FC
60 super(type, id, traces, epoch, indexPageSize, preIndexExperiment);
61 }
82e04272
FC
62
63 /**
64 * @param type
65 * @param id
66 * @param traces
67 */
12c155f5 68 public LTTngExperiment(Class<T> type, String id, ITmfTrace<T>[] traces) {
82e04272
FC
69 this(type, id, traces, TmfTimestamp.Zero, DEFAULT_INDEX_PAGE_SIZE);
70 }
71
72 /**
73 * @param type
74 * @param id
75 * @param traces
76 * @param indexPageSize
77 */
12c155f5 78 public LTTngExperiment(Class<T> type, String id, ITmfTrace<T>[] traces, int indexPageSize) {
82e04272
FC
79 this(type, id, traces, TmfTimestamp.Zero, indexPageSize);
80 }
a79913eb 81
12c155f5 82 @SuppressWarnings("unchecked")
82e04272 83 public LTTngExperiment(LTTngExperiment<T> other) {
a79913eb
FC
84 super(other.getName() + "(clone)", other.fType); //$NON-NLS-1$
85
86 fEpoch = other.fEpoch;
87 fIndexPageSize = other.fIndexPageSize;
88
89 fTraces = new ITmfTrace[other.fTraces.length];
90 for (int trace = 0; trace < other.fTraces.length; trace++) {
12c155f5 91 fTraces[trace] = other.fTraces[trace].copy();
a79913eb
FC
92 }
93
94 fNbEvents = other.fNbEvents;
95 fTimeRange = other.fTimeRange;
96 }
97
98 @Override
12c155f5 99 public LTTngExperiment<T> copy() {
a79913eb
FC
100 LTTngExperiment<T> experiment = new LTTngExperiment<T>(this);
101 TmfSignalManager.deregister(experiment);
102 return experiment;
82e04272 103 }
a79913eb
FC
104
105 // ------------------------------------------------------------------------
82e04272
FC
106 // ITmfTrace trace positioning
107 // ------------------------------------------------------------------------
108
a79913eb
FC
109 @Override
110 public synchronized TmfEvent getNextEvent(TmfContext context) {
82e04272 111
a79913eb
FC
112 // Validate the context
113 if (!(context instanceof TmfExperimentContext)) {
114 return null; // Throw an exception?
115 }
82e04272 116
a79913eb 117 if (!context.equals(fExperimentContext)) {
82e04272 118// Tracer.trace("Ctx: Restoring context");
a79913eb
FC
119 fExperimentContext = seekLocation(context.getLocation());
120 }
121
122 TmfExperimentContext expContext = (TmfExperimentContext) context;
82e04272
FC
123
124// dumpContext(expContext, true);
125
a79913eb
FC
126 // If an event was consumed previously, get the next one from that trace
127 int lastTrace = expContext.getLastTrace();
128 if (lastTrace != TmfExperimentContext.NO_TRACE) {
129 TmfContext traceContext = expContext.getContexts()[lastTrace];
130 expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext);
131 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
132 }
133
134 // Scan the candidate events and identify the "next" trace to read from
135 TmfEvent eventArray[] = expContext.getEvents();
136 if (eventArray == null) {
137 return null;
138 }
139 int trace = TmfExperimentContext.NO_TRACE;
140 TmfTimestamp timestamp = TmfTimestamp.BigCrunch;
141 if (eventArray.length == 1) {
142 if (eventArray[0] != null) {
143 timestamp = eventArray[0].getTimestamp();
144 trace = 0;
145 }
146 } else {
147 for (int i = 0; i < eventArray.length; i++) {
148 TmfEvent event = eventArray[i];
149 if (event != null && event.getTimestamp() != null) {
150 TmfTimestamp otherTS = event.getTimestamp();
151 if (otherTS.compareTo(timestamp, true) < 0) {
152 trace = i;
153 timestamp = otherTS;
154 }
155 }
156 }
157 }
158
159 // Update the experiment context and set the "next" event
160 TmfEvent event = null;
161 if (trace != TmfExperimentContext.NO_TRACE) {
82e04272
FC
162// updateIndex(expContext, timestamp);
163
a79913eb
FC
164 TmfContext traceContext = expContext.getContexts()[trace];
165 TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation();
166 expLocation.getLocation().locations[trace] = traceContext.getLocation();
82e04272 167
a79913eb 168 updateIndex(expContext, timestamp);
82e04272 169
a79913eb
FC
170 expLocation.getRanks()[trace] = traceContext.getRank();
171 expContext.setLastTrace(trace);
172 expContext.updateRank(1);
173 event = expContext.getEvents()[trace];
174 fExperimentContext = expContext;
175 }
82e04272
FC
176
177// if (event != null) {
178// Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
179// dumpContext(expContext, false);
180// Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
181// }
182
a79913eb
FC
183 return event;
184 }
185
186 @SuppressWarnings("unchecked")
187 @Override
188 protected void indexExperiment(final boolean waitForCompletion) {
189 if (waitForCompletion) {
190 TmfExperimentRangeUpdatedSignal signal = new TmfExperimentRangeUpdatedSignal(LTTngExperiment.this, LTTngExperiment.this,
191 TmfTimeRange.Eternity);
192 broadcast(signal);
193 while (isIndexingBusy()) {
194 try {
195 Thread.sleep(100);
196 } catch (InterruptedException e) {
197 e.printStackTrace();
198 }
199 }
200 ;
201 return;
202 }
203 for (ITmfTrace trace : fTraces) {
204 if (trace instanceof LTTngTrace) {
205 JniTrace jniTrace = ((LTTngTrace) trace).getCurrentJniTrace();
1b70b6dc 206 if (jniTrace != null && (!jniTrace.isLiveTraceSupported() || !LiveTraceManager.isLiveTrace(jniTrace.getTracepath()))) {
a79913eb
FC
207 updateTimeRange();
208 TmfExperimentRangeUpdatedSignal signal = new TmfExperimentRangeUpdatedSignal(LTTngExperiment.this, LTTngExperiment.this,
209 getTimeRange());
210 broadcast(signal);
211 return;
212 }
213 }
214 }
215 final Thread thread = new Thread("Streaming Monitor for " + getName()) { //$NON-NLS-1$
216 LttngTimestamp safeTimestamp = null;
217 TmfTimeRange timeRange = null;
82e04272 218
a79913eb
FC
219 @Override
220 public void run() {
221 while (!fExecutor.isShutdown()) {
222 final TmfEventRequest<LttngEvent> request = new TmfEventRequest<LttngEvent>(LttngEvent.class, TmfTimeRange.Eternity, 0,
223 ExecutionType.FOREGROUND) {
224 @Override
225 public void handleCompleted() {
226 super.handleCompleted();
227 if (isIndexingBusy()) {
228 timeRange = null;
229 return;
230 }
231 long startTime = Long.MAX_VALUE;
232 long endTime = Long.MIN_VALUE;
233 for (ITmfTrace trace : getTraces()) {
234 if (trace instanceof LTTngTrace) {
235 LTTngTrace lttngTrace = (LTTngTrace) trace;
236 JniTrace jniTrace = lttngTrace.getCurrentJniTrace();
237 jniTrace.updateTrace();
74237cc3 238 startTime = Math.min(startTime, lttngTrace.getStartTime().getValue());
a79913eb
FC
239 endTime = Math.max(endTime, jniTrace.getEndTime().getTime());
240 }
241 }
242 LttngTimestamp startTimestamp = new LttngTimestamp(startTime);
243 LttngTimestamp endTimestamp = new LttngTimestamp(endTime);
244 if (safeTimestamp != null && safeTimestamp.compareTo(getTimeRange().getEndTime(), false) > 0) {
245 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
246 } else {
247 timeRange = null;
248 }
249 safeTimestamp = endTimestamp;
250 }
251 };
252 try {
253 sendRequest((ITmfDataRequest<T>) request);
254 request.waitForCompletion();
b626c6f7 255 if (timeRange != null && !timeRange.equals(TmfTimeRange.Null)) {
a79913eb
FC
256 TmfExperimentRangeUpdatedSignal signal = new TmfExperimentRangeUpdatedSignal(LTTngExperiment.this, LTTngExperiment.this,
257 timeRange);
258 broadcast(signal);
259 }
74237cc3 260 Thread.sleep(2000);
a79913eb
FC
261 } catch (InterruptedException e) {
262 e.printStackTrace();
263 }
264 }
265 }
266 };
267 thread.start();
268 }
269
270 @TmfSignalHandler
271 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal) {
272 indexExperiment(false, (int) fNbEvents, signal.getRange());
273 }
274
275 /*
276 * (non-Javadoc)
277 *
278 * @see java.lang.Object#toString()
279 */
280 @Override
3b38ea61 281 @SuppressWarnings("nls")
a79913eb
FC
282 public String toString() {
283 return "[LTTngExperiment (" + getName() + ")]";
284 }
82e04272
FC
285
286}
This page took 0.041287 seconds and 5 git commands to generate.