Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.api.dsl;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.ref.WeakReference;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IntSummaryStatistics;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.SourceSection;
/**
* Represents a specialization statistics utiltiy that can be {@link #enter() entered} to collect
* additional statistics about Truffle DSL nodes. In order for the statistics to be useful the nodes
* need to be regenerated using the -Atruffle.dsl.GenerateSpecializationStatistics=true
* flag or using the {@link AlwaysEnabled} annotation.
*
* The easiest way to use this utility is to enable the
* --engine.SpecializationStatistics polyglot option. This should print the histogram
* when the engine is closed.
*
* See also the usage
* tutorial on the website.
*
* @since 20.3
*/
public final class SpecializationStatistics {
private static final ThreadLocal STATISTICS = new ThreadLocal<>();
private final Map, NodeClassStatistics> classStatistics = new HashMap<>();
private final Map uncachedStatistics = new HashMap<>();
SpecializationStatistics() {
}
/**
* Returns true if the statistics did collect any data, else false.
*
* @since 20.3
*/
public synchronized boolean hasData() {
for (NodeClassStatistics classStatistic : classStatistics.values()) {
if (classStatistic.createHistogram().getNodeStat().getSum() > 0) {
return true;
}
}
return false;
}
/**
* Prints the specialization histogram to the provided writer. Does not print anything if no
* {@link #hasData() data} was collected.
*
* @see #printHistogram(PrintWriter)
* @since 20.3
*/
public synchronized void printHistogram(PrintWriter writer) {
List histograms = new ArrayList<>();
long parentSum = 0;
long parentCount = 0;
for (NodeClassStatistics classStatistic : classStatistics.values()) {
NodeClassHistogram histogram = classStatistic.createHistogram();
histograms.add(histogram);
parentSum += histogram.getNodeStat().getSum();
parentCount += histogram.getNodeStat().getCount();
}
Collections.sort(histograms, new Comparator() {
public int compare(NodeClassHistogram o1, NodeClassHistogram o2) {
return Long.compare(o1.getNodeStat().getSum(), o2.getNodeStat().getSum());
}
});
int width = 0;
for (NodeClassHistogram histogram : histograms) {
if (histogram.getNodeStat().getSum() == 0) {
continue;
}
width = Math.max(histogram.getLabelWidth(), width);
}
width = Math.min(width, 80);
NodeClassHistogram.printLine(writer, " ", width);
for (NodeClassHistogram histogram : histograms) {
if (histogram.getNodeStat().getSum() == 0) {
continue;
}
histogram.print(writer, width, parentCount, parentSum);
}
}
/**
* Prints the specialization histogram to the provided stream. Does not print anything if no
* {@link #hasData() data} was collected.
*
* @see #printHistogram(PrintWriter)
* @since 20.3
*/
public synchronized void printHistogram(PrintStream stream) {
printHistogram(new PrintWriter(stream));
}
/**
* Creates a new specialization statistics instance. Note specialization statistics need to be
* {@link #enter() entered} to collect data on a thread.
*
* @since 20.3
*/
public static SpecializationStatistics create() {
return new SpecializationStatistics();
}
private synchronized NodeStatistics createCachedNodeStatistic(Node node, String[] specializations) {
NodeClassStatistics classStatistic = getClassStatistics(node.getClass(), specializations);
EnabledNodeStatistics stat = new EnabledNodeStatistics(node, classStatistic);
classStatistic.statistics.add(stat);
if (classStatistic.nodeCounter++ % 1024 == 0) {
/*
* In order to not crash for code load benchmarks we need to process collected nodes
* from time to time to clean them up.
*/
classStatistic.processCollectedStatistics();
}
return stat;
}
private NodeClassStatistics getClassStatistics(Class> nodeClass, String[] specializations) {
assert Thread.holdsLock(this);
return this.classStatistics.computeIfAbsent(nodeClass, (c) -> new NodeClassStatistics(c, specializations));
}
private static NodeStatistics createUncachedNodeStatistic(Node node, String[] specializations) {
return new UncachedNodeStatistics(node, specializations);
}
/**
* Enters this specialization instance object on the current thread. After entering a
* specialization statistics instance will gather statistics for all nodes with
* {@link Specialization specializations} that were created on this entered thread. Multiple
* threads may be entered at the same time. The caller must make sure to
* {@link #leave(SpecializationStatistics)} the current statistics after entering in all cases.
*
* @since 20.3
*/
@TruffleBoundary
public SpecializationStatistics enter() {
SpecializationStatistics prev = STATISTICS.get();
STATISTICS.set(this);
return prev;
}
/**
* Leaves the currently {@link #enter() entered} entered statistics. It is required to leave a
* statistics block after it was entered. It is recommended to use a finally block for this
* purpose.
*
* @since 20.3
*/
@SuppressWarnings("static-method")
@TruffleBoundary
public void leave(SpecializationStatistics prev) {
STATISTICS.set(prev);
}
/**
* Used on nodes to always enable specialization statistics. The Truffle DSL processor will not
* generate statistics code unless the
* -J-Dtruffle.dsl.GenerateSpecializationStatistics=true javac system property is
* set. This annotation can be used to annotate node types that want to force enable the
* statistics independent of the system property. This annotation is inherited by sub classes.
*
* @since 20.3
*/
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.TYPE})
public @interface AlwaysEnabled {
}
static final class NodeClassStatistics {
private List statistics = new ArrayList<>();
/*
* Combines data from all collected nodes.
*/
private final NodeClassHistogram collectedHistogram;
private int nodeCounter;
NodeClassStatistics(Class> nodeClass, String[] specializations) {
this.collectedHistogram = new NodeClassHistogram(nodeClass, specializations);
}
private void processCollectedStatistics() {
boolean found = false;
/*
* Most calls to processStatistics don't actually need to remove anything. But if
* something is removed it is typically more than one entry, so we do a first pass over
* the references to find out whether there is a removed node and then recreate the
* entire list.
*/
for (EnabledNodeStatistics statistic : this.statistics) {
if (statistic.isCollected()) {
found = true;
break;
}
}
if (found) {
List newStatistics = new ArrayList<>();
for (EnabledNodeStatistics statistic : this.statistics) {
if (statistic.isCollected()) {
collectedHistogram.accept(statistic);
} else {
newStatistics.add(statistic);
}
}
statistics = newStatistics;
}
}
public NodeClassHistogram createHistogram() {
NodeClassHistogram h = new NodeClassHistogram(collectedHistogram.getNodeClass(), collectedHistogram.getSpecializationNames());
h.combine(this.collectedHistogram);
for (EnabledNodeStatistics stat : statistics) {
h.accept(stat);
}
return h;
}
}
static final class IntStatistics extends IntSummaryStatistics {
private SourceSection maxSourceSection;
@Override
@Deprecated(since = "20.3")
public void accept(int value) {
throw new UnsupportedOperationException();
}
public void accept(int value, SourceSection sourceSection) {
if (value > getMax()) {
this.maxSourceSection = sourceSection;
}
super.accept(value);
}
public void combine(IntStatistics other) {
if (other.getMax() > this.getMax()) {
this.maxSourceSection = other.maxSourceSection;
}
super.combine(other);
}
@Override
@Deprecated(since = "20.3")
public void combine(IntSummaryStatistics other) {
throw new UnsupportedOperationException();
}
}
static final class NodeClassHistogram {
private final Class> nodeClass;
private final String[] specializationNames;
private final IntStatistics nodeStat;
private final IntStatistics[] specializationStat;
private final List