Skip to content

Commit

Permalink
Reduce runtime overhead of CausalityExport when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophTF committed Aug 29, 2023
1 parent 2adf9b2 commit 7fcf51d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,12 @@ public void registerAsAssignable(BigBang bb) {

public boolean registerAsReachable(Object reason) {
assert isValidReason(reason) : "Registering a type as reachable needs to provide a valid reason.";
CausalityExport.Event thisReachable = new CausalityExport.TypeReachable(this);
CausalityExport.get().registerEvent(thisReachable);
CausalityExport.get().registerEvent(new CausalityExport.TypeReachable(this));
if (!AtomicUtils.isSet(this, isReachableUpdater)) {
/* Mark this type and all its super types as reachable. */
forAllSuperTypes(type -> {
if(type != this) {
CausalityExport.get().registerEdge(thisReachable, new CausalityExport.TypeReachable(type));
CausalityExport.get().registerEdge(new CausalityExport.TypeReachable(this), new CausalityExport.TypeReachable(type));
}
AtomicUtils.atomicSetAndRun(type, reason, isReachableUpdater, type::onReachable);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.oracle.graal.pointsto.BigBang;
import com.oracle.graal.pointsto.ObjectScanner;
import com.oracle.graal.pointsto.PointsToAnalysis;
import com.oracle.graal.pointsto.constraints.UnsupportedFeatureException;
import com.oracle.graal.pointsto.flow.AbstractVirtualInvokeTypeFlow;
import com.oracle.graal.pointsto.flow.TypeFlow;
import com.oracle.graal.pointsto.meta.AnalysisElement;
Expand All @@ -15,11 +14,11 @@
import com.oracle.graal.pointsto.reports.causality.Graph;
import com.oracle.graal.pointsto.reports.causality.TypeflowImpl;
import com.oracle.graal.pointsto.typestate.TypeState;
import com.oracle.graal.pointsto.util.AnalysisError;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaField;
import jdk.vm.ci.meta.JavaMethod;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.MetaUtil;
import jdk.vm.ci.meta.Signature;

Expand All @@ -31,7 +30,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;
Expand All @@ -41,30 +39,46 @@ public class CausalityExport {
protected CausalityExport() {
}

private static final CausalityExport dummyInstance = new CausalityExport();
private static ThreadLocal<Impl> instances;
private static List<Impl> instancesOfAllThreads;
private static boolean collectTypeflowInformation;

// Starts collection of Causality Data
public static synchronized void activate(boolean collectTypeflowInformation) {
CausalityExport.collectTypeflowInformation = collectTypeflowInformation;
instances = ThreadLocal.withInitial(CausalityExport::createInstance);
instancesOfAllThreads = new ArrayList<>();
public enum Level {
DISABLED,
ENABLED_WITHOUT_TYPEFLOW,
ENABLED;
}

private static Level requestedLevel = Level.DISABLED;

public static final class InitializationOnDemandHolder {
private static final Level frozenLevel = CausalityExport.requestedLevel;
}

/**
* Must be called before any usage of {@link #get()}
*/
public static void activate(Level level) {
requestedLevel = level;
if (level != InitializationOnDemandHolder.frozenLevel) {
throw AnalysisError.shouldNotReachHere("Causality Export must have been activated before the first usage of CausalityExport.get()");
}
}

private static final CausalityExport dummyInstance = new CausalityExport();
private static ThreadLocal<Impl> instances = ThreadLocal.withInitial(CausalityExport::createInstance);
private static List<Impl> instancesOfAllThreads = new ArrayList<>();

private static synchronized Impl createInstance() {
Impl instance = collectTypeflowInformation ? new TypeflowImpl() : new Impl();
Impl instance = InitializationOnDemandHolder.frozenLevel == Level.ENABLED ? new TypeflowImpl() : new Impl();
instancesOfAllThreads.add(instance);
return instance;
}

public static CausalityExport get() {
return instances != null ? instances.get() : dummyInstance;
if (InitializationOnDemandHolder.frozenLevel == Level.DISABLED || instances == null)
return dummyInstance;
return instances.get();
}

public static synchronized void dump(PointsToAnalysis bb, ZipOutputStream zip, boolean exportTypeflowNames) throws java.io.IOException {
Impl data = collectTypeflowInformation ? new TypeflowImpl((Iterable<TypeflowImpl>)(Iterable<? extends Impl>) instancesOfAllThreads, bb) : new Impl(instancesOfAllThreads, bb);
Impl data = InitializationOnDemandHolder.frozenLevel == Level.ENABLED ? new TypeflowImpl((Iterable<TypeflowImpl>)(Iterable<? extends Impl>) instancesOfAllThreads, bb) : new Impl(instancesOfAllThreads, bb);
// Let GC collect intermediate data structures
instances = null;
instancesOfAllThreads = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public static void updateMaxJavaStackTraceDepth(EconomicMap<OptionKey<?>, Object
public static final String REACHABILITY_FILE_NAME = "reachability.json";
@Option(help = "Create a " + REACHABILITY_FILE_NAME + " file in the build directory. The output conforms to the JSON schema located at: " +
"docs/reference-manual/native-image/assets/reachability-schema-v0.9.0.json", type = OptionType.User)//
public static final HostedOptionKey<Boolean> GenerateReachabilityFile = new HostedOptionKey<>(true);
public static final HostedOptionKey<Boolean> GenerateReachabilityFile = new HostedOptionKey<>(false);

/*
* Build output options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,10 @@ protected void setupNativeImage(OptionValues options, Map<Method, CEntryPointDat
if(AnalysisReportsOptions.PrintCausalityGraph.getValue(options)) {
// This cannot be done in the "CausalityExporter"-Feature since Feature-registration should already
// be logged by CausalityExport...
CausalityExport.activate(AnalysisReportsOptions.CausalityGraphWithTypeflow.getValue(options));
CausalityExport.activate(AnalysisReportsOptions.CausalityGraphWithTypeflow.getValue(options)
? CausalityExport.Level.ENABLED
: CausalityExport.Level.ENABLED_WITHOUT_TYPEFLOW
);
}

ClassLoaderSupportImpl classLoaderSupport = new ClassLoaderSupportImpl(loader.classLoaderSupport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import com.oracle.graal.pointsto.meta.AnalysisType;
import com.oracle.graal.pointsto.meta.AnalysisUniverse;
import com.oracle.graal.pointsto.reports.AnalysisReportsOptions;
import com.oracle.graal.pointsto.reports.CausalityExport;
import com.oracle.svm.core.ClassLoaderSupport;
import com.oracle.svm.core.JavaMainWrapper;
Expand Down Expand Up @@ -356,7 +357,7 @@ private Type getType(AnalysisType type, Map<Class<?>, InitKind> classInitKinds,

@Override
public boolean isInConfiguration(IsInConfigurationAccess access) {
return SubstrateOptions.GenerateReachabilityFile.getValue();
return SubstrateOptions.GenerateReachabilityFile.getValue() || AnalysisReportsOptions.PrintCausalityGraph.getValue(HostedOptionValues.singleton());
}

@Override
Expand Down

0 comments on commit 7fcf51d

Please sign in to comment.