2010-11-05 Francois Chouinard <fchouinard@gmail.com> Fix for Bug329473
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng / src / org / eclipse / linuxtools / lttng / 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
13package org.eclipse.linuxtools.lttng.trace;
14
15import org.eclipse.linuxtools.tmf.event.TmfEvent;
16import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
17import org.eclipse.linuxtools.tmf.experiment.TmfExperiment;
18import org.eclipse.linuxtools.tmf.experiment.TmfExperimentContext;
19import org.eclipse.linuxtools.tmf.experiment.TmfExperimentLocation;
20import org.eclipse.linuxtools.tmf.signal.TmfSignalManager;
21import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
22import org.eclipse.linuxtools.tmf.trace.TmfContext;
23
24/**
25 * <b><u>LTTngExperiment</u></b>
26 * <p>
27 * Temporary class to resolve a basic incompatibility between TMF and LTTng.
28 * <p>
29 */
30public class LTTngExperiment<T extends TmfEvent> extends TmfExperiment<T> implements ITmfTrace {
31
32 private static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
33
34 // ------------------------------------------------------------------------
35 // Constructors
36 // ------------------------------------------------------------------------
37
38 /**
39 * @param type
40 * @param id
41 * @param traces
42 * @param epoch
43 * @param indexPageSize
44 */
45 public LTTngExperiment(Class<T> type, String id, ITmfTrace[] traces, TmfTimestamp epoch, int indexPageSize) {
46 this(type, id, traces, TmfTimestamp.Zero, indexPageSize, false);
47 }
48
49 public LTTngExperiment(Class<T> type, String id, ITmfTrace[] traces, TmfTimestamp epoch, int indexPageSize, boolean preIndexExperiment) {
50 super(type, id, traces, epoch, indexPageSize, preIndexExperiment);
51 }
52
53 /**
54 * @param type
55 * @param id
56 * @param traces
57 */
58 public LTTngExperiment(Class<T> type, String id, ITmfTrace[] traces) {
59 this(type, id, traces, TmfTimestamp.Zero, DEFAULT_INDEX_PAGE_SIZE);
60 }
61
62 /**
63 * @param type
64 * @param id
65 * @param traces
66 * @param indexPageSize
67 */
68 public LTTngExperiment(Class<T> type, String id, ITmfTrace[] traces, int indexPageSize) {
69 this(type, id, traces, TmfTimestamp.Zero, indexPageSize);
70 }
71
72 public LTTngExperiment(LTTngExperiment<T> other) {
73 super(other.getName() + "(clone)", other.fType);
74
75 fEpoch = other.fEpoch;
76 fIndexPageSize = other.fIndexPageSize;
77
78 fTraces = new ITmfTrace[other.fTraces.length];
79 for (int trace = 0; trace < other.fTraces.length; trace++) {
80 fTraces[trace] = other.fTraces[trace].createTraceCopy();
81 }
82
83 fNbEvents = other.fNbEvents;
84 fTimeRange = other.fTimeRange;
85 }
86
87 @Override
88 public LTTngExperiment<T> createTraceCopy() {
89 LTTngExperiment<T> experiment = new LTTngExperiment<T>(this);
90 TmfSignalManager.deregister(experiment);
91 return experiment;
92 }
93
94 // ------------------------------------------------------------------------
95 // ITmfTrace trace positioning
96 // ------------------------------------------------------------------------
97
98 @Override
99 public synchronized TmfEvent getNextEvent(TmfContext context) {
100
101 // Validate the context
102 if (!(context instanceof TmfExperimentContext)) {
103 return null; // Throw an exception?
104 }
105
f6b14ce2 106 if (!context.equals(fExperimentContext)) {
82e04272
FC
107// Tracer.trace("Ctx: Restoring context");
108 seekLocation(context.getLocation());
109 }
110
111 TmfExperimentContext expContext = (TmfExperimentContext) context;
112
113// dumpContext(expContext, true);
114
115 // If an event was consumed previously, get the next one from that trace
116 int lastTrace = expContext.getLastTrace();
117 if (lastTrace != TmfExperimentContext.NO_TRACE) {
118 TmfContext traceContext = expContext.getContexts()[lastTrace];
119 expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext);
120 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
121 }
122
64fe8e8a
FC
123 // Scan the candidate events and identify the "next" trace to read from
124 TmfEvent eventArray[] = expContext.getEvents();
125 if (eventArray == null) {
126 return null;
127 }
82e04272
FC
128 int trace = TmfExperimentContext.NO_TRACE;
129 TmfTimestamp timestamp = TmfTimestamp.BigCrunch;
64fe8e8a
FC
130 if (eventArray.length == 1) {
131 timestamp = eventArray[0].getTimestamp();
132 trace = 0;
133 } else {
134 for (int i = 0; i < eventArray.length; i++) {
135 TmfEvent event = eventArray[i];
136 if (event != null && event.getTimestamp() != null) {
137 TmfTimestamp otherTS = event.getTimestamp();
138 if (otherTS.compareTo(timestamp, true) < 0) {
139 trace = i;
140 timestamp = otherTS;
141 }
82e04272
FC
142 }
143 }
144 }
145
146 // Update the experiment context and set the "next" event
147 TmfEvent event = null;
148 if (trace != TmfExperimentContext.NO_TRACE) {
149// updateIndex(expContext, timestamp);
150
151 TmfContext traceContext = expContext.getContexts()[trace];
152 TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation();
64fe8e8a 153 expLocation.getLocation()[trace] = traceContext.getLocation();
82e04272
FC
154
155 updateIndex(expContext, timestamp);
156
157 expLocation.getRanks()[trace] = traceContext.getRank();
158 expContext.setLastTrace(trace);
159 expContext.updateRank(1);
160 event = expContext.getEvents()[trace];
161 }
162
163// if (event != null) {
164// Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
165// dumpContext(expContext, false);
166// Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
167// }
168
82e04272
FC
169 return event;
170 }
171
172 /* (non-Javadoc)
173 * @see java.lang.Object#toString()
174 */
175 @Override
176 public String toString() {
177 return "[LTTngExperiment (" + getName() + ")]";
178 }
179
180}
This page took 0.031073 seconds and 5 git commands to generate.