Commit | Line | Data |
---|---|---|
62d1696a FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 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.tmf.trace; | |
14 | ||
15 | import java.util.Vector; | |
16 | ||
17 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
18 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
19 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
20 | import org.eclipse.linuxtools.tmf.request.ITmfRequestHandler; | |
21 | import org.eclipse.linuxtools.tmf.request.TmfDataRequest; | |
22 | import org.eclipse.linuxtools.tmf.signal.TmfSignalManager; | |
23 | import org.eclipse.linuxtools.tmf.trace.ITmfTrace.StreamContext; | |
24 | ||
25 | /** | |
26 | * <b><u>TmfTrace</u></b> | |
27 | * <p> | |
28 | * TmfTrace represents a time-ordered set of events tied to a single event | |
29 | * stream. It keeps track of the global information about the event log: | |
30 | * <ul> | |
31 | * <li> the epoch, a reference timestamp for the whole log (t0) | |
32 | * <li> the span of the log i.e. the timestamps range | |
33 | * <li> the total number of events | |
34 | * </ul> | |
35 | * As an ITmfRequestHandler, it provides an implementation of processRequest() | |
36 | * which handles event requests. | |
37 | * <p> | |
38 | * TODO: Handle concurrent and possibly overlapping requests in a way that | |
39 | * optimizes the stream access and event parsing. | |
40 | */ | |
41 | public class TmfTrace_old implements ITmfRequestHandler<TmfEvent> { | |
42 | ||
43 | // ======================================================================== | |
44 | // Attributes | |
45 | // ======================================================================== | |
46 | ||
47 | private final String fTraceId; | |
48 | private final ITmfTrace fStream; | |
49 | private final TmfTimestamp fEpoch; | |
50 | ||
51 | // ======================================================================== | |
52 | // Constructors | |
53 | // ======================================================================== | |
54 | ||
55 | public TmfTrace_old(String id, ITmfTrace stream) { | |
56 | this(id, stream, TmfTimestamp.BigBang); | |
57 | } | |
58 | ||
59 | public TmfTrace_old(String id, ITmfTrace stream, TmfTimestamp epoch) { | |
60 | assert stream != null; | |
61 | fTraceId = id; | |
62 | fStream = stream; | |
63 | fEpoch = epoch; | |
64 | TmfSignalManager.addListener(this); | |
65 | } | |
66 | ||
67 | public void dispose() { | |
68 | TmfSignalManager.removeListener(this); | |
69 | } | |
70 | ||
71 | // ======================================================================== | |
72 | // Accessors | |
73 | // ======================================================================== | |
74 | ||
75 | public String getTraceId() { | |
76 | return fTraceId; | |
77 | } | |
78 | ||
79 | public ITmfTrace getStream() { | |
80 | return fStream; | |
81 | } | |
82 | ||
83 | public TmfTimestamp getEpoch() { | |
84 | return fEpoch; | |
85 | } | |
86 | ||
87 | public TmfTimeRange getTimeRange() { | |
88 | return fStream.getTimeRange(); | |
89 | } | |
90 | ||
91 | public int getNbEvents() { | |
92 | return fStream.getNbEvents(); | |
93 | } | |
94 | ||
95 | public int getIndex(TmfTimestamp ts) { | |
96 | return fStream.getIndex(ts); | |
97 | } | |
98 | ||
99 | // ======================================================================== | |
100 | // Operators | |
101 | // ======================================================================== | |
102 | ||
103 | private StreamContext fContext; | |
104 | ||
105 | public TmfEvent getEvent(TmfTimestamp timestamp) { | |
106 | fContext = fStream.seekEvent(timestamp); | |
107 | return fStream.getNextEvent(fContext); | |
108 | } | |
109 | ||
110 | public TmfEvent getEvent(int index) { | |
111 | fContext = fStream.seekEvent(index); | |
112 | return fStream.getNextEvent(fContext); | |
113 | } | |
114 | ||
115 | public TmfEvent getNextEvent() { | |
116 | if (fContext == null) { | |
117 | fContext = fStream.seekEvent(TmfTimestamp.BigBang); | |
118 | } | |
119 | return fStream.getNextEvent(fContext); | |
120 | } | |
121 | ||
122 | // ======================================================================== | |
123 | // ITmfRequestHandler | |
124 | // ======================================================================== | |
125 | ||
126 | /* (non-Javadoc) | |
127 | * @see org.eclipse.linuxtools.tmf.eventlog.ITmfRequestHandler#processRequest(org.eclipse.linuxtools.tmf.eventlog.TmfDataRequest, boolean) | |
128 | */ | |
129 | public void processRequest(TmfDataRequest<TmfEvent> request, boolean waitForCompletion) { | |
130 | if (request.getRange() != null) { | |
131 | serviceEventRequestByTimestamp(request); | |
132 | } else { | |
133 | serviceEventRequestByIndex(request); | |
134 | } | |
135 | if (waitForCompletion) { | |
136 | request.waitForCompletion(); | |
137 | } | |
138 | } | |
139 | ||
140 | /* (non-Javadoc) | |
141 | * | |
142 | * @param request | |
143 | */ | |
144 | private void serviceEventRequestByTimestamp(final TmfDataRequest<TmfEvent> request) { | |
145 | Thread thread = new Thread() { | |
146 | @Override | |
147 | public void run() { | |
148 | TmfTimestamp startTime = request.getRange().getStartTime(); | |
149 | TmfTimestamp endTime = request.getRange().getEndTime(); | |
150 | int blockSize = request.getBlockize(); | |
151 | ||
152 | int nbRequestedEvents = request.getNbRequestedItems(); | |
153 | if (nbRequestedEvents == -1) { | |
154 | nbRequestedEvents = Integer.MAX_VALUE; | |
155 | } | |
156 | ||
157 | Vector<TmfEvent> events = new Vector<TmfEvent>(); | |
158 | int nbEvents = 0; | |
159 | ||
160 | StreamContext context = new StreamContext(null); | |
161 | TmfEvent event = fStream.getEvent(context, startTime); | |
162 | ||
163 | while (!request.isCancelled() && nbEvents < nbRequestedEvents && event != null && | |
164 | event.getTimestamp().compareTo(endTime, false) <= 0 ) | |
165 | { | |
166 | events.add(event); | |
167 | if (++nbEvents % blockSize == 0) { | |
168 | TmfEvent[] result = new TmfEvent[events.size()]; | |
169 | events.toArray(result); | |
170 | request.setData(result); | |
171 | request.handleData(); | |
172 | events.removeAllElements(); | |
173 | } | |
174 | // To avoid an unnecessary read passed the last event requested | |
175 | if (nbEvents < nbRequestedEvents) | |
176 | event = fStream.getNextEvent(context); | |
177 | } | |
178 | TmfEvent[] result = new TmfEvent[events.size()]; | |
179 | events.toArray(result); | |
180 | request.setData(result); | |
181 | ||
182 | request.handleData(); | |
183 | request.done(); | |
184 | } | |
185 | }; | |
186 | thread.start(); | |
187 | } | |
188 | ||
189 | /* (non-Javadoc) | |
190 | * | |
191 | * @param request | |
192 | */ | |
193 | private void serviceEventRequestByIndex(final TmfDataRequest<TmfEvent> request) { | |
194 | // Thread thread = new Thread() { | |
195 | // @Override | |
196 | // public void run() { | |
197 | // int blockSize = request.getBlockize(); | |
198 | // | |
199 | // int nbRequestedEvents = request.getNbRequestedItems(); | |
200 | // if (nbRequestedEvents == -1) { | |
201 | // nbRequestedEvents = Integer.MAX_VALUE; | |
202 | // } | |
203 | // | |
204 | // Vector<TmfEvent> events = new Vector<TmfEvent>(); | |
205 | // int nbEvents = 0; | |
206 | // | |
207 | // StreamContext context = new StreamContext(null); | |
208 | // TmfEvent event = fStream.getEvent(context, request.getIndex()); | |
209 | // | |
210 | // while (!request.isCancelled() && nbEvents < nbRequestedEvents && event != null) | |
211 | // { | |
212 | // events.add(event); | |
213 | // if (++nbEvents % blockSize == 0) { | |
214 | // TmfEvent[] result = new TmfEvent[events.size()]; | |
215 | // events.toArray(result); | |
216 | // request.setData(result); | |
217 | // request.handleData(); | |
218 | // events.removeAllElements(); | |
219 | // } | |
220 | // // To avoid an unnecessary read passed the last event requested | |
221 | // if (nbEvents < nbRequestedEvents) | |
222 | // event = fStream.getNextEvent(context); | |
223 | // } | |
224 | // TmfEvent[] result = new TmfEvent[events.size()]; | |
225 | // events.toArray(result); | |
226 | // | |
227 | // request.setData(result); | |
228 | // request.handleData(); | |
229 | // request.done(); | |
230 | // } | |
231 | // }; | |
232 | // thread.start(); | |
233 | } | |
234 | ||
235 | // // ======================================================================== | |
236 | // // Signal handlers | |
237 | // // ======================================================================== | |
238 | // | |
239 | // @TmfSignalHandler | |
240 | // public void streamUpdated(TmfStreamUpdatedSignal signal) { | |
241 | // TmfSignalManager.dispatchSignal(new TmfTraceUpdatedSignal(this, this)); | |
242 | // } | |
243 | ||
244 | } |