315eaf6914ed1b2c55028a1ca74102fbe802a3e8
1 /*******************************************************************************
2 * Copyright (c) 2014 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
10 * Alexandre Montplaisir - Renamed/extracted from CtfTraceManager
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.trace
.iterator
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
19 import java
.util
.Random
;
20 import java
.util
.concurrent
.locks
.Lock
;
21 import java
.util
.concurrent
.locks
.ReentrantLock
;
23 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocationInfo
;
24 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfTmfContext
;
25 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
28 * A CTF trace iterator manager.
30 * Each instance of {@link CtfTmfTrace} should possess one of these, which will
31 * manage the iterators that are opened to read that trace. This will allow
32 * controlling the number of opened file handles per trace.
34 * @author Matthew Khouzam
36 public class CtfIteratorManager
{
38 * Cache size. Under 1023 on linux32 systems. Number of file handles
41 private static final int MAX_SIZE
= 100;
43 /** The map of the cache */
44 private final Map
<CtfTmfContext
, CtfIterator
> fMap
;
46 /** An array pointing to the same cache. this allows fast "random" accesses */
47 private final List
<CtfTmfContext
> fRandomAccess
;
49 /** Lock for when we access the two previous data structures */
50 private final Lock fAccessLock
= new ReentrantLock();
52 /** The parent trace */
53 private final CtfTmfTrace fTrace
;
55 /** Random number generator */
56 private final Random fRnd
;
62 * The trace whose iterators this manager will manage
64 public CtfIteratorManager(CtfTmfTrace trace
) {
65 fMap
= new HashMap
<>();
66 fRandomAccess
= new ArrayList
<>();
67 fRnd
= new Random(System
.nanoTime());
72 * This needs explaining: the iterator table is effectively a cache.
73 * Originally the contexts had a 1 to 1 structure with the file handles of a
74 * trace. This failed since there is a limit to how many file handles we can
75 * have opened simultaneously. Then a round-robin scheme was implemented,
76 * this lead up to a two competing contexts syncing up and using the same
77 * file handler, causing horrible slowdowns. Now a random replacement
78 * algorithm is selected. This is the same as used by arm processors, and it
79 * works quite well when many cores so this looks promising for very
80 * multi-threaded systems.
83 * the context to look up
84 * @return the iterator referring to the context
86 public CtfIterator
getIterator(final CtfTmfContext context
) {
88 * if the element is in the map, we don't need to do anything else.
90 CtfIterator iter
= fMap
.get(context
);
96 * Assign an iterator to a context.
98 if (fRandomAccess
.size() < MAX_SIZE
) {
100 * if we're not full yet, just add an element.
102 iter
= (CtfIterator
) fTrace
.createIterator();
103 addElement(context
, iter
);
107 * if we're full, randomly replace an element
109 iter
= replaceRandomElement(context
);
111 if (context
.getLocation() != null) {
112 final CtfLocationInfo location
= (CtfLocationInfo
) context
.getLocation().getLocationInfo();
116 fAccessLock
.unlock();
123 * Remove an iterator from this manager
126 * The context of the iterator to remove
128 public void removeIterator(CtfTmfContext context
) {
131 /* The try below is only to auto-call CtfIterator.close() */
132 try (CtfIterator removed
= fMap
.remove(context
)) {
135 fRandomAccess
.remove(context
);
138 fAccessLock
.unlock();
143 * Add a pair of context and element to the hashmap and the arraylist.
150 private void addElement(final CtfTmfContext context
,
151 final CtfIterator elem
) {
154 fMap
.put(context
, elem
);
155 fRandomAccess
.add(context
);
158 fAccessLock
.unlock();
163 * Replace a random element
166 * the context to swap in
167 * @return the iterator of the removed elements.
169 private CtfIterator
replaceRandomElement(final CtfTmfContext context
) {
171 * This needs some explanation too: We need to select a random victim
172 * and remove it. The order of the elements is not important, so instead
173 * of just calling arraylist.remove(element) which has an O(n)
174 * complexity, we pick an random number. The element is swapped out of
175 * the array and removed and replaced in the hashmap.
177 fAccessLock
.lock(); // just in case, should only be called when already locked
179 final int size
= fRandomAccess
.size();
180 final int pos
= fRnd
.nextInt(size
);
181 final CtfTmfContext victim
= fRandomAccess
.get(pos
);
182 fRandomAccess
.set(pos
, context
);
183 CtfIterator elem
= fMap
.remove(victim
);
184 if (elem
.isClosed()) {
186 * In case the iterator streams have been closed, we need to
187 * replace it by a fresh new one to access the trace.
190 elem
= (CtfIterator
) fTrace
.createIterator();
192 fMap
.put(context
, elem
);
197 fAccessLock
.unlock();
202 * Dispose this iterator manager, which will close all the remaining
205 public void dispose() {
208 for (CtfIterator iterator
: fMap
.values()) {
212 fRandomAccess
.clear();
215 fAccessLock
.unlock();
This page took 0.042912 seconds and 4 git commands to generate.