|
24 | 24 | */
|
25 | 25 | package com.oracle.svm.core.heap;
|
26 | 26 |
|
| 27 | +import java.util.Collections; |
| 28 | +import java.util.EnumSet; |
27 | 29 | import java.util.List;
|
| 30 | +import java.util.function.Function; |
28 | 31 |
|
29 | 32 | import org.graalvm.nativeimage.ImageSingletons;
|
30 | 33 | import org.graalvm.nativeimage.Platform;
|
|
34 | 37 | import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
|
35 | 38 | import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
|
36 | 39 | import com.oracle.svm.core.feature.InternalFeature;
|
| 40 | +import com.oracle.svm.core.imagelayer.BuildingImageLayerPredicate; |
| 41 | +import com.oracle.svm.core.imagelayer.ImageLayerBuildingSupport; |
| 42 | +import com.oracle.svm.core.layeredimagesingleton.ImageSingletonLoader; |
| 43 | +import com.oracle.svm.core.layeredimagesingleton.ImageSingletonWriter; |
| 44 | +import com.oracle.svm.core.layeredimagesingleton.InitialLayerOnlyImageSingleton; |
| 45 | +import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingleton; |
| 46 | +import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonBuilderFlags; |
37 | 47 | import com.oracle.svm.core.util.DuplicatedInNativeCode;
|
38 | 48 | import com.oracle.svm.core.util.ImageHeapList;
|
39 | 49 | import com.oracle.svm.core.util.VMError;
|
40 | 50 |
|
| 51 | +import jdk.graal.compiler.debug.Assertions; |
| 52 | + |
41 | 53 | /**
|
42 | 54 | * This class holds garbage collection causes that are common and therefore shared between different
|
43 | 55 | * garbage collector implementations.
|
@@ -77,34 +89,138 @@ public static GCCause fromId(int causeId) {
|
77 | 89 | public static List<GCCause> getGCCauses() {
|
78 | 90 | return ImageSingletons.lookup(GCCauseSupport.class).gcCauses;
|
79 | 91 | }
|
| 92 | + |
| 93 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 94 | + public static void registerGCCause(GCCause cause) { |
| 95 | + ImageSingletons.lookup(GCCauseSupport.class).installGCCause(cause); |
| 96 | + } |
80 | 97 | }
|
81 | 98 |
|
82 | 99 | @AutomaticallyRegisteredImageSingleton
|
83 |
| -class GCCauseSupport { |
| 100 | +class GCCauseSupport implements InitialLayerOnlyImageSingleton { |
84 | 101 | final List<GCCause> gcCauses = ImageHeapList.create(GCCause.class, null);
|
85 | 102 |
|
86 | 103 | @Platforms(Platform.HOSTED_ONLY.class)
|
87 | 104 | Object collectGCCauses(Object obj) {
|
88 | 105 | if (obj instanceof GCCause gcCause) {
|
89 | 106 | synchronized (gcCauses) {
|
90 |
| - int id = gcCause.getId(); |
91 |
| - while (gcCauses.size() <= id) { |
92 |
| - gcCauses.add(null); |
93 |
| - } |
94 |
| - var existing = gcCauses.set(id, gcCause); |
95 |
| - if (existing != null && existing != gcCause) { |
96 |
| - throw VMError.shouldNotReachHere("Two GCCause objects have the same id " + id + ": " + gcCause.getName() + ", " + existing.getName()); |
97 |
| - } |
| 107 | + installGCCause(gcCause); |
98 | 108 | }
|
99 | 109 | }
|
100 | 110 | return obj;
|
101 | 111 | }
|
| 112 | + |
| 113 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 114 | + void installGCCause(GCCause gcCause) { |
| 115 | + int id = gcCause.getId(); |
| 116 | + while (gcCauses.size() <= id) { |
| 117 | + gcCauses.add(null); |
| 118 | + } |
| 119 | + var existing = gcCauses.set(id, gcCause); |
| 120 | + if (existing != null && existing != gcCause) { |
| 121 | + throw VMError.shouldNotReachHere("Two GCCause objects have the same id " + id + ": " + gcCause.getName() + ", " + existing.getName()); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean accessibleInFutureLayers() { |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() { |
| 132 | + return LayeredImageSingletonBuilderFlags.ALL_ACCESS; |
| 133 | + } |
102 | 134 | }
|
103 | 135 |
|
104 | 136 | @AutomaticallyRegisteredFeature
|
105 | 137 | class GCCauseFeature implements InternalFeature {
|
| 138 | + |
106 | 139 | @Override
|
107 | 140 | public void duringSetup(DuringSetupAccess access) {
|
108 |
| - access.registerObjectReplacer(ImageSingletons.lookup(GCCauseSupport.class)::collectGCCauses); |
| 141 | + if (!ImageLayerBuildingSupport.buildingImageLayer()) { |
| 142 | + /* |
| 143 | + * In traditional builds we lazily register GCCauses as they become visible to the |
| 144 | + * native-image generator. |
| 145 | + */ |
| 146 | + access.registerObjectReplacer(ImageSingletons.lookup(GCCauseSupport.class)::collectGCCauses); |
| 147 | + } else { |
| 148 | + /* |
| 149 | + * For layered builds we eagerly register all GCCauses in the initial layer. In all |
| 150 | + * layers, via an object replacer, we then validate all referenced GCCauses have been |
| 151 | + * registered. |
| 152 | + */ |
| 153 | + Function<Integer, String> idToGCCauseName; |
| 154 | + if (ImageLayerBuildingSupport.buildingInitialLayer()) { |
| 155 | + GCCauseSupport support = ImageSingletons.lookup(GCCauseSupport.class); |
| 156 | + support.installGCCause(GCCause.JavaLangSystemGC); |
| 157 | + support.installGCCause(GCCause.UnitTest); |
| 158 | + support.installGCCause(GCCause.TestGCInDeoptimizer); |
| 159 | + support.installGCCause(GCCause.HintedGC); |
| 160 | + support.installGCCause(GCCause.JvmtiForceGC); |
| 161 | + support.installGCCause(GCCause.HeapDump); |
| 162 | + support.installGCCause(GCCause.DiagnosticCommand); |
| 163 | + |
| 164 | + var gcCauseList = GCCause.getGCCauses(); |
| 165 | + idToGCCauseName = (idx) -> gcCauseList.get(idx).getName(); |
| 166 | + } else { |
| 167 | + var gcCauseNames = LayeredGCCauseTracker.getRegisteredGCCauses(); |
| 168 | + idToGCCauseName = gcCauseNames::get; |
| 169 | + } |
| 170 | + access.registerObjectReplacer(obj -> { |
| 171 | + if (obj instanceof GCCause gcCause) { |
| 172 | + if (!idToGCCauseName.apply(gcCause.getId()).equals(gcCause.getName())) { |
| 173 | + var id = gcCause.getId(); |
| 174 | + VMError.shouldNotReachHere("Mismatch in GCCause name for id %s: %s %s", id, idToGCCauseName.apply(id), gcCause.getName()); |
| 175 | + } |
| 176 | + } |
| 177 | + return obj; |
| 178 | + }); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * In layered builds all {@link GCCause}s are registered and installed in the initial layer. Here we |
| 185 | + * track which {@link GCCause}s were installed in the initial layer to detect issues. |
| 186 | + */ |
| 187 | +@AutomaticallyRegisteredImageSingleton(onlyWith = BuildingImageLayerPredicate.class) |
| 188 | +class LayeredGCCauseTracker implements LayeredImageSingleton { |
| 189 | + List<String> registeredGCCauses; |
| 190 | + |
| 191 | + public static List<String> getRegisteredGCCauses() { |
| 192 | + assert ImageLayerBuildingSupport.buildingExtensionLayer(); |
| 193 | + return ImageSingletons.lookup(LayeredGCCauseTracker.class).registeredGCCauses; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() { |
| 198 | + return LayeredImageSingletonBuilderFlags.BUILDTIME_ACCESS_ONLY; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public PersistFlags preparePersist(ImageSingletonWriter writer) { |
| 203 | + List<String> gcCauses; |
| 204 | + if (ImageLayerBuildingSupport.buildingInitialLayer()) { |
| 205 | + gcCauses = GCCause.getGCCauses().stream().map(gcCause -> { |
| 206 | + if (gcCause == null) { |
| 207 | + return ""; |
| 208 | + } else { |
| 209 | + assert !gcCause.getName().isEmpty() : Assertions.errorMessage("Empty string is reserved for non-existent GCCauses", gcCause); |
| 210 | + return gcCause.getName(); |
| 211 | + } |
| 212 | + }).toList(); |
| 213 | + } else { |
| 214 | + gcCauses = registeredGCCauses; |
| 215 | + } |
| 216 | + writer.writeStringList("registeredGCCauses", gcCauses); |
| 217 | + return PersistFlags.CREATE; |
| 218 | + } |
| 219 | + |
| 220 | + @SuppressWarnings("unused") |
| 221 | + public static Object createFromLoader(ImageSingletonLoader loader) { |
| 222 | + var causeTracker = new LayeredGCCauseTracker(); |
| 223 | + causeTracker.registeredGCCauses = Collections.unmodifiableList(loader.readStringList("registeredGCCauses")); |
| 224 | + return causeTracker; |
109 | 225 | }
|
110 | 226 | }
|
0 commit comments