1
/*******************************************************************************
2
* Copyright (c) 2012 Ericsson
4
* All rights reserved. This program and the accompanying materials are made
5
* 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
9
* Contributors: Matthew Khouzam - Initial API and implementation
10
*******************************************************************************/
12
package org.eclipse.linuxtools.tmf.core.ctfadaptor;
14
import java.util.ArrayList;
15
import java.util.ListIterator;
17
import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
18
import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
21
* Lightweight Context for CtfTmf traces. Should only use 3 references, 1 ref to
22
* a boxed Long, a long and an int.
25
* @author Matthew Khouzam
27
public class CtfTmfLightweightContext implements ITmfContext {
29
// -------------------------------------------
31
// -------------------------------------------
32
private static final int MAX_COLLISIONS = 10;
34
// -------------------------------------------
36
// -------------------------------------------
37
private CtfLocation curLocation;
39
private int collisions;
41
private CtfIterator fSeeker;
42
final private ArrayList<CtfIterator> fIteratorPool;
43
private ListIterator<CtfIterator> fCurrentIterator;
45
// -------------------------------------------
47
// -------------------------------------------
51
* the shared iterator pool.
53
* the iterator position.
55
public CtfTmfLightweightContext(ArrayList<CtfIterator> iters,
56
ListIterator<CtfIterator> pos) {
57
fIteratorPool = iters;
58
fCurrentIterator = pos;
59
fSeeker = getIterator();
60
curLocation = new CtfLocation((Long)null);
64
// -------------------------------------------
65
// TmfContext Overrides
66
// -------------------------------------------
69
public long getRank() {
74
public ITmfLocation<? extends Comparable<?>> getLocation() {
79
public boolean hasValidRank() {
80
return curRank != CtfLocation.INVALID_LOCATION;
84
public void setLocation(ITmfLocation<? extends Comparable<?>> location) {
85
curLocation = (CtfLocation) location;
90
public void setRank(long rank) {
96
public void increaseRank() {
102
// -------------------------------------------
103
// CtfTmfTrace Helpers
104
// -------------------------------------------
107
* Gets the current event. Wrapper to help CtfTmfTrace
108
* @return The event or null
110
public synchronized CtfTmfEvent getCurrentEvent() {
112
return fSeeker.getCurrentEvent();
116
* Advances to a the next event. Wrapper to help CtfTmfTrace
117
* @return success or not
119
public synchronized boolean advance() {
121
boolean retVal = fSeeker.advance();
122
CtfTmfEvent currentEvent = fSeeker.getCurrentEvent();
123
if (currentEvent != null) {
124
curLocation.setLocation(currentEvent.getTimestampValue());
126
curLocation.setLocation(CtfLocation.INVALID_LOCATION);
133
public void dispose() {
138
* Seeks to a given timestamp. Wrapper to help CtfTmfTrace
139
* @param timestamp desired timestamp
140
* @return success or not
142
public synchronized boolean seek(final long timestamp) {
143
curLocation.setLocation(timestamp);
145
fSeeker = getIterator();
146
return updateLocation();
152
* @see java.lang.Object#clone()
155
public CtfTmfLightweightContext clone() {
156
CtfTmfLightweightContext ret = new CtfTmfLightweightContext(
157
fIteratorPool, fCurrentIterator);
158
ret.curLocation = curLocation.clone();
159
ret.curRank = curRank;
163
// -------------------------------------------
165
// -------------------------------------------
167
* This updates the position of an iterator to the location(curLocation)
168
* Since the iterators are in a pool to not exhaust the number of file
169
* pointers some of them can be shared. This means there can be collisions
170
* between contexts fighting over the same resource. A heuristic is applied
171
* that if there are MAX_COLLISIONS collisions in a row, the iterator is
172
* changed for the next one in the iterator pool.
174
* @return true if the location is correct.
176
private synchronized boolean updateLocation() {
177
if (!curLocation.getLocation().equals(
178
(fSeeker.getLocation().getLocation()))) {
180
if (collisions > MAX_COLLISIONS) {
181
fSeeker = getIterator();
184
fSeeker.setRank(curRank);
185
return fSeeker.seek(curLocation.getLocation());
192
* gets the next iterator in a pool.
196
private CtfIterator getIterator() {
197
if (!fCurrentIterator.hasNext()) {
198
fCurrentIterator = fIteratorPool.listIterator(0);
200
return fCurrentIterator.next();