Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 865c551544b42094c3ce2383133dd44f7ea4e10c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************************************************************************
 * Copyright (c) 2012 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Matthew Khouzam - Initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.tmf.core.ctfadaptor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

/**
 * Ctf Iterator Manager, allows mapping of iterators (a limited resource) to
 * contexts (many many resources).
 *
 * @author Matthew Khouzam
 * @version 1.0
 * @since 1.1
 */
public abstract class CtfIteratorManager {
    /*
     * A side note synchronized works on the whole object, Therefore add and
     * remove will be thread safe.
     */

    /*
     * The map of traces to trace managers.
     */
    private static HashMap<CtfTmfTrace, CtfTraceManager> map = new HashMap<CtfTmfTrace, CtfTraceManager>();

    /**
     * Registers a trace to the iterator manager, the trace can now get
     * iterators.
     *
     * @param trace
     *            the trace to register.
     */
    public static synchronized void addTrace(final CtfTmfTrace trace) {
        map.put(trace, new CtfTraceManager(trace));
    }

    /**
     * Removes a trace to the iterator manager.
     *
     * @param trace
     *            the trace to register.
     */
    public static synchronized void removeTrace(final CtfTmfTrace trace) {
        map.remove(trace);
    }

    /**
     * Get an iterator for a given trace and context.
     *
     * @param trace
     *            the trace
     * @param ctx
     *            the context
     * @return the iterator
     */
    public static synchronized CtfIterator getIterator(final CtfTmfTrace trace,
            final CtfTmfLightweightContext ctx) {
        return map.get(trace).getIterator(ctx);
    }
}

/**
 * A trace manager
 *
 * @author Matthew Khouzam
 */
class CtfTraceManager {
    /*
     * Cache size. Under 1023 on linux32 systems. Number of file handles
     * created.
     */
    private final static int MAX_SIZE = 100;
    /*
     * The map of the cache.
     */
    private final HashMap<CtfTmfLightweightContext, CtfIterator> fMap;
    /*
     * An array pointing to the same cache. this allows fast "random" accesses.
     */
    private final ArrayList<CtfTmfLightweightContext> fRandomAccess;
    /*
     * The parent trace
     */
    private final CtfTmfTrace fTrace;
    /*
     * Random number generator
     */
    private final Random fRnd;

    public CtfTraceManager(CtfTmfTrace trace) {
        fMap = new HashMap<CtfTmfLightweightContext, CtfIterator>();
        fRandomAccess = new ArrayList<CtfTmfLightweightContext>();
        fRnd = new Random(System.nanoTime());
        fTrace = trace;
    }

    /**
     * This needs explaining: the iterator table is effectively a cache.
     * Originally the contexts had a 1 to 1 structure with the file handles of a
     * trace. This failed since there is a limit to how many file handles we can
     * have opened simultaneously. Then a round-robin scheme was implemented,
     * this lead up to a two competing contexts syncing up and using the same
     * file handler, causing horrible slowdowns. Now a random replacement
     * algorithm is selected. This is the same as used by arm processors, and it
     * works quite well when many cores so this looks promising for very
     * multi-threaded systems.
     *
     * @param context
     *            the context to look up
     * @return the iterator refering to the context
     */
    public CtfIterator getIterator(final CtfTmfLightweightContext context) {
        /*
         * if the element is in the map, we don't need to do anything else.
         */
        CtfIterator retVal = fMap.get(context);
        if (retVal == null) {
            /*
             * Assign an iterator to a context, this means we will need to seek
             * at the end.
             */
            if (fRandomAccess.size() < MAX_SIZE) {
                /*
                 * if we're not full yet, just add an element.
                 */
                retVal = new CtfIterator(fTrace);
                addElement(context, retVal);

            } else {
                /*
                 * if we're full, randomly replace an element
                 */
                retVal = replaceRandomElement(context);
            }
            retVal.seek((Long) context.getLocation().getLocation());
        }
        return retVal;
    }

    /**
     * Add a pair of context and element to the hashmap and the arraylist.
     *
     * @param context
     *            the context
     * @param elem
     *            the iterator
     */
    private void addElement(final CtfTmfLightweightContext context,
            final CtfIterator elem) {
        fMap.put(context, elem);
        fRandomAccess.add(context);
    }

    /**
     * Replace a random element
     *
     * @param context
     *            the context to swap in
     * @return the iterator of the removed elements.
     */
    private CtfIterator replaceRandomElement(
            final CtfTmfLightweightContext context) {
        /*
         * This needs some explanation too: We need to select a random victim
         * and remove it. The order of the elements is not important, so instead
         * of just calling arraylist.remove(element) which has an O(n)
         * complexity, we pick an random number. The element is swapped out of
         * the array and removed and replaced in the hashmap.
         */
        final int size = fRandomAccess.size();
        final int pos = fRnd.nextInt(size);
        final CtfTmfLightweightContext victim = fRandomAccess.get(pos);
        fRandomAccess.set(pos, context);
        final CtfIterator elem = fMap.remove(victim);
        fMap.put(context, elem);
        return elem;
    }

}

Back to the top