2010-11-23 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug325661
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng / src / org / eclipse / linuxtools / lttng / trace / LTTngExperiment.java
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
13 package org.eclipse.linuxtools.lttng.trace;
14
15 import org.eclipse.linuxtools.tmf.event.TmfEvent;
16 import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
17 import org.eclipse.linuxtools.tmf.experiment.TmfExperiment;
18 import org.eclipse.linuxtools.tmf.experiment.TmfExperimentContext;
19 import org.eclipse.linuxtools.tmf.experiment.TmfExperimentLocation;
20 import org.eclipse.linuxtools.tmf.signal.TmfSignalManager;
21 import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
22 import 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 */
30 public class LTTngExperiment<T extends TmfEvent> extends TmfExperiment<T> implements ITmfTrace {
31
32 private static final int DEFAULT_INDEX_PAGE_SIZE = 50000;
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); //$NON-NLS-1$
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
106 if (!context.equals(fExperimentContext)) {
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
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 }
128 int trace = TmfExperimentContext.NO_TRACE;
129 TmfTimestamp timestamp = TmfTimestamp.BigCrunch;
130 if (eventArray.length == 1) {
131 if (eventArray[0] != null) {
132 timestamp = eventArray[0].getTimestamp();
133 trace = 0;
134 }
135 } else {
136 for (int i = 0; i < eventArray.length; i++) {
137 TmfEvent event = eventArray[i];
138 if (event != null && event.getTimestamp() != null) {
139 TmfTimestamp otherTS = event.getTimestamp();
140 if (otherTS.compareTo(timestamp, true) < 0) {
141 trace = i;
142 timestamp = otherTS;
143 }
144 }
145 }
146 }
147
148 // Update the experiment context and set the "next" event
149 TmfEvent event = null;
150 if (trace != TmfExperimentContext.NO_TRACE) {
151 // updateIndex(expContext, timestamp);
152
153 TmfContext traceContext = expContext.getContexts()[trace];
154 TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation();
155 expLocation.getLocation()[trace] = traceContext.getLocation();
156
157 updateIndex(expContext, timestamp);
158
159 expLocation.getRanks()[trace] = traceContext.getRank();
160 expContext.setLastTrace(trace);
161 expContext.updateRank(1);
162 event = expContext.getEvents()[trace];
163 }
164
165 // if (event != null) {
166 // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
167 // dumpContext(expContext, false);
168 // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
169 // }
170
171 return event;
172 }
173
174 /* (non-Javadoc)
175 * @see java.lang.Object#toString()
176 */
177 @Override
178 @SuppressWarnings("nls")
179 public String toString() {
180 return "[LTTngExperiment (" + getName() + ")]";
181 }
182
183 }
This page took 0.035925 seconds and 5 git commands to generate.