ConstantMap.java 5.6 KB
Newer Older
A
apetushkov 已提交
1
/*
2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
A
apetushkov 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

26
package jdk.jfr.internal.consumer;
A
apetushkov 已提交
27

28 29 30 31
import jdk.jfr.internal.LogLevel;
import jdk.jfr.internal.LogTag;
import jdk.jfr.internal.Logger;

32
import jdk.jfr.internal.LongMap;
A
apetushkov 已提交
33 34 35 36 37 38 39 40

/**
 * Holds mapping between a set of keys and their corresponding object.
 *
 * If the type is a known type, i.e. {@link RecordedThread}, an
 * {@link ObjectFactory} can be supplied which will instantiate a typed object.
 */
final class ConstantMap {
41 42 43 44 45 46 47 48 49

    private static final int RESOLUTION_FINISHED = 0;
    private static final int RESOLUTION_STARTED = 1;
    public static final ConstantMap EMPTY = new ConstantMap();

    // A temporary placeholder, so objects can
    // reference themselves (directly, or indirectly),
    // when making a transition from numeric id references
    // to normal Java references.
A
apetushkov 已提交
50 51 52 53 54 55 56 57 58 59 60 61
    private final static class Reference {
        private final long key;
        private final ConstantMap pool;

        Reference(ConstantMap pool, long key) {
            this.pool = pool;
            this.key = key;
        }

        Object resolve() {
            return pool.get(key);
        }
62 63 64 65

        public String toString() {
            return "ref: " + pool.name + "[" + key + "]";
        }
A
apetushkov 已提交
66 67
    }

68 69 70
    final ObjectFactory<?> factory;
    final String name;

A
apetushkov 已提交
71 72
    private final LongMap<Object> objects;

73
    private boolean resolving;
A
apetushkov 已提交
74
    private boolean allResolved;
75 76 77 78 79

    private ConstantMap() {
        this(null, "<empty>");
        allResolved = true;
    }
A
apetushkov 已提交
80 81 82

    ConstantMap(ObjectFactory<?> factory, String name) {
        this.name = name;
83
        this.objects = new LongMap<>(2);
A
apetushkov 已提交
84 85 86 87 88 89 90 91 92
        this.factory = factory;
    }

    Object get(long id) {
        // fast path, all objects in pool resolved
        if (allResolved) {
            return objects.get(id);
        }
        // referenced from a pool, deal with this later
93
        if (!resolving) {
A
apetushkov 已提交
94 95 96
            return new Reference(this, id);
        }

97
        // should ideally always have a value
98 99
        Object value = objects.get(id);
        if (value == null) {
100 101 102
            // unless id is 0 which is used to represent null
            if (id != 0) {
                Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Missing object id=" + id + " in pool " + name + ". All ids should reference an object");
103
            }
104
            return null;
105
        }
A
apetushkov 已提交
106

107 108 109
        // id is resolved (but not the whole pool)
        if (objects.isSetId(id, RESOLUTION_FINISHED)) {
            return value;
A
apetushkov 已提交
110 111 112
        }

        // resolving ourself, abort to avoid infinite recursion
113
        if (objects.isSetId(id, RESOLUTION_STARTED)) {
A
apetushkov 已提交
114 115 116
            return null;
        }

117 118 119 120 121 122 123 124 125 126 127 128
        // mark id as STARTED if we should
        // come back during object resolution
        objects.setId(id, RESOLUTION_STARTED);

        // resolve object!
        Object resolved = resolve(value);

        // mark id as FINISHED
        objects.setId(id, RESOLUTION_FINISHED);

        // if a factory exists, convert to RecordedThread.
        // RecordedClass etc. and store back results
A
apetushkov 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        if (factory != null) {
            Object factorized = factory.createObject(id, resolved);
            objects.put(id, factorized);
            return factorized;
        } else {
            objects.put(id, resolved);
            return resolved;
        }
    }

    private static Object resolve(Object o) {
        if (o instanceof Reference) {
            return resolve(((Reference) o).resolve());
        }
        if (o != null && o.getClass().isArray()) {
            final Object[] array = (Object[]) o;
            for (int i = 0; i < array.length; i++) {
146 147
                Object element = array[i];
                array[i] = resolve(element);
A
apetushkov 已提交
148 149 150 151 152 153 154
            }
            return array;
        }
        return o;
    }

    public void resolve() {
155
        objects.forEachKey(k -> get(k));
A
apetushkov 已提交
156 157 158 159 160 161
    }

    public void put(long key, Object value) {
        objects.put(key, value);
    }

162 163 164
    public void setResolving() {
        resolving = true;
        allResolved = false;
A
apetushkov 已提交
165 166 167 168
    }

    public void setResolved() {
        allResolved = true;
169
        resolving = false;
A
apetushkov 已提交
170 171 172 173 174
    }

    public String getName() {
        return name;
    }
175 176 177 178 179 180 181 182 183 184 185 186 187

    public Object getResolved(long id) {
        return objects.get(id);
    }

    public void putResolved(long id, Object object) {
        objects.put(id, object);
        objects.setId(id, RESOLUTION_FINISHED);
    }

    public void setAllResolved(boolean allResolved) {
        this.allResolved = allResolved;
    }
A
apetushkov 已提交
188
}