Skip to content

Commit 6f0d40b

Browse files
committed
[GR-35755] Introduce TimerCollection.
PullRequest: graal/10585
2 parents bf8e534 + 3cf5804 commit 6f0d40b

File tree

15 files changed

+276
-125
lines changed

15 files changed

+276
-125
lines changed

sdk/mx.sdk/mx_sdk_benchmark.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ def apply_command_mapper_hooks(self, cmd, vm):
191191
return mx.apply_command_mapper_hooks(cmd, vm.command_mapper_hooks)
192192

193193
def extra_image_build_argument(self, _, args):
194-
updated_args = parse_prefixed_args('-Dnative-image.benchmark.extra-image-build-argument=', args)
195-
updated_args.append('-H:-BuildOutputUseNewStyle') # remove after GR-35755 is resolved properly
196-
return updated_args
194+
return parse_prefixed_args('-Dnative-image.benchmark.extra-image-build-argument=', args)
197195

198196
def extra_run_arg(self, benchmark, args, image_run_args):
199197
"""Returns all arguments passed to the final image.

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/BigBang.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.oracle.graal.pointsto.meta.AnalysisType.UsageKind;
4343
import com.oracle.graal.pointsto.meta.AnalysisUniverse;
4444
import com.oracle.graal.pointsto.meta.HostedProviders;
45-
import com.oracle.graal.pointsto.util.Timer;
4645

4746
import jdk.vm.ci.meta.ConstantReflectionProvider;
4847

@@ -72,16 +71,6 @@ public interface BigBang extends ReachabilityAnalysis, HeapScanning {
7271

7372
List<DebugHandlersFactory> getDebugHandlerFactories();
7473

75-
/**
76-
* @return the timer for measuring the overall duration of the analysis
77-
*/
78-
Timer getAnalysisTimer();
79-
80-
/**
81-
* @return the timer for measuring the time spent in features
82-
*/
83-
Timer getProcessFeaturesTimer();
84-
8574
/**
8675
* Prints all analysis timers.
8776
*/

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/PointsToAnalysis.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import java.util.concurrent.atomic.AtomicLongArray;
4646
import java.util.function.Function;
4747

48+
import com.oracle.graal.pointsto.meta.PointsToAnalysisMethod;
49+
import com.oracle.graal.pointsto.util.TimerCollection;
4850
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
4951
import org.graalvm.compiler.core.common.SuppressFBWarnings;
5052
import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
@@ -75,7 +77,6 @@
7577
import com.oracle.graal.pointsto.meta.AnalysisType;
7678
import com.oracle.graal.pointsto.meta.AnalysisUniverse;
7779
import com.oracle.graal.pointsto.meta.HostedProviders;
78-
import com.oracle.graal.pointsto.meta.PointsToAnalysisMethod;
7980
import com.oracle.graal.pointsto.reports.StatisticsPrinter;
8081
import com.oracle.graal.pointsto.typestate.PointsToStats;
8182
import com.oracle.graal.pointsto.typestate.TypeState;
@@ -134,16 +135,16 @@ public abstract class PointsToAnalysis implements BigBang {
134135
private final boolean strengthenGraalGraphs;
135136

136137
public PointsToAnalysis(OptionValues options, AnalysisUniverse universe, HostedProviders providers, HostVM hostVM, ForkJoinPool executorService, Runnable heartbeatCallback,
137-
UnsupportedFeatures unsupportedFeatures, boolean strengthenGraalGraphs) {
138+
UnsupportedFeatures unsupportedFeatures, TimerCollection timerCollection, boolean strengthenGraalGraphs) {
138139
this.options = options;
139140
this.debugHandlerFactories = Collections.singletonList(new GraalDebugHandlersFactory(providers.getSnippetReflection()));
140141
this.debug = new Builder(options, debugHandlerFactories).build();
141142
this.hostVM = hostVM;
142143
String imageName = hostVM.getImageName();
143-
this.typeFlowTimer = new Timer(imageName, "(typeflow)", false);
144-
this.verifyHeapTimer = new Timer(imageName, "(verify)", false);
145-
this.processFeaturesTimer = new Timer(imageName, "(features)", false);
146-
this.analysisTimer = new Timer(imageName, "analysis", true);
144+
this.typeFlowTimer = timerCollection.createTimer(imageName, "(typeflow)", false);
145+
this.verifyHeapTimer = timerCollection.get(TimerCollection.Registry.VERIFY_HEAP);
146+
this.processFeaturesTimer = timerCollection.get(TimerCollection.Registry.FEATURES);
147+
this.analysisTimer = timerCollection.get(TimerCollection.Registry.ANALYSIS);
147148

148149
this.universe = universe;
149150
this.metaAccess = (AnalysisMetaAccess) providers.getMetaAccess();
@@ -180,16 +181,6 @@ public PointsToAnalysis(OptionValues options, AnalysisUniverse universe, HostedP
180181
: HeapScanningPolicy.skipTypes(skippedHeapTypes());
181182
}
182183

183-
@Override
184-
public Timer getAnalysisTimer() {
185-
return analysisTimer;
186-
}
187-
188-
@Override
189-
public Timer getProcessFeaturesTimer() {
190-
return processFeaturesTimer;
191-
}
192-
193184
@Override
194185
public void printTimers() {
195186
typeFlowTimer.print();

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/util/Timer.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,12 @@ public class Timer {
4040
/** Total VM memory in bytes recorded when the timer is printed. */
4141
private long totalMemory;
4242

43-
public Timer(String name) {
44-
this(null, name, true);
45-
}
46-
47-
public Timer(String prefix, String name) {
48-
this(prefix, name, true);
49-
}
50-
51-
public Timer(String name, boolean autoPrint) {
52-
this(null, name, autoPrint);
53-
}
54-
55-
public Timer(String prefix, String name, boolean autoPrint) {
43+
/**
44+
* Timers should only be instantiated via factory methods in TimerCollection.
45+
*
46+
* @see TimerCollection
47+
*/
48+
Timer(String prefix, String name, boolean autoPrint) {
5649
this.prefix = prefix;
5750
this.name = name;
5851
this.autoPrint = autoPrint;
@@ -78,12 +71,14 @@ public StopTimer start() {
7871
public void stop() {
7972
long addTime = System.nanoTime() - startTime;
8073
totalTime += addTime;
74+
totalMemory = Runtime.getRuntime().totalMemory();
8175
if (autoPrint) {
8276
print(addTime);
8377
}
8478
}
8579

8680
private void print(long time) {
81+
// TODO GR-35721
8782
if (disablePrinting) {
8883
return;
8984
}
@@ -95,7 +90,6 @@ private void print(long time) {
9590
} else {
9691
concurrentPrefix = "";
9792
}
98-
totalMemory = Runtime.getRuntime().totalMemory();
9993
double totalMemoryGB = totalMemory / 1024.0 / 1024.0 / 1024.0;
10094
System.out.format("%s%12s: %,10.2f ms, %,5.2f GB%n", concurrentPrefix, name, time / 1000000d, totalMemoryGB);
10195
}
@@ -114,6 +108,10 @@ public long getTotalMemory() {
114108
return totalMemory;
115109
}
116110

111+
public String getName() {
112+
return name;
113+
}
114+
117115
public class StopTimer implements AutoCloseable {
118116

119117
@Override
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.graal.pointsto.util;
26+
27+
import com.oracle.graal.pointsto.reports.StatisticsPrinter;
28+
import com.oracle.svm.util.ImageBuildStatistics;
29+
import org.graalvm.compiler.debug.GraalError;
30+
import org.graalvm.nativeimage.ImageSingletons;
31+
32+
import java.io.PrintWriter;
33+
import java.util.Iterator;
34+
import java.util.Map;
35+
import java.util.concurrent.ConcurrentHashMap;
36+
37+
public class TimerCollection implements ImageBuildStatistics.TimerCollectionPrinter {
38+
39+
public static TimerCollection singleton() {
40+
return ImageSingletons.lookup(TimerCollection.class);
41+
}
42+
43+
/**
44+
* A registry of well-known timers used when building images.
45+
*/
46+
public enum Registry {
47+
TOTAL("[total]", false),
48+
SETUP("setup", true),
49+
CLASSLIST("classlist", false),
50+
CLINIT("(clinit)", true),
51+
FEATURES("(features)", false),
52+
VERIFY_HEAP("(verify)", false),
53+
ANALYSIS("analysis", true),
54+
UNIVERSE("universe", true),
55+
COMPILE_TOTAL("compile", true),
56+
PARSE("(parse)", true),
57+
INLINE("(inline)", true),
58+
COMPILE("(compile)", true),
59+
DEBUG_INFO("dbginfo", true),
60+
IMAGE("image", true),
61+
WRITE("write", true);
62+
63+
public final String name;
64+
65+
public final boolean autoPrint;
66+
67+
Registry(String name, boolean autoPrint) {
68+
this.name = name;
69+
this.autoPrint = autoPrint;
70+
}
71+
72+
}
73+
74+
private final Map<String, Timer> timers = new ConcurrentHashMap<>();
75+
private final String imageName;
76+
77+
public TimerCollection(String imageName) {
78+
this.imageName = imageName;
79+
}
80+
81+
public Timer get(String name) {
82+
Timer timer = timers.get(name);
83+
GraalError.guarantee(timer != null, "Timer with name %s not found.", name);
84+
return timer;
85+
}
86+
87+
public Timer get(TimerCollection.Registry type) {
88+
return timers.computeIfAbsent(type.name, (name) -> new Timer(imageName, name, type.autoPrint));
89+
}
90+
91+
public static Timer.StopTimer createTimerAndStart(String prefix, String name) {
92+
return singleton().createTimer(prefix, name).start();
93+
}
94+
95+
public static Timer.StopTimer createTimerAndStart(TimerCollection.Registry type) {
96+
return singleton().get(type).start();
97+
}
98+
99+
public Timer createTimer(String name) {
100+
return createTimer(null, name, true);
101+
}
102+
103+
public Timer createTimer(String prefix, String name) {
104+
return createTimer(prefix, name, true);
105+
}
106+
107+
public Timer createTimer(String name, boolean autoPrint) {
108+
return createTimer(null, name, autoPrint);
109+
}
110+
111+
public Timer createTimer(String prefix, String name, boolean autoPrint) {
112+
GraalError.guarantee(!timers.containsKey(name), "Name %s for a timer is already taken.", name);
113+
Timer timer = new Timer(prefix, name, autoPrint);
114+
timers.put(timer.getName(), timer);
115+
return timer;
116+
}
117+
118+
@Override
119+
public void printTimerStats(PrintWriter out) {
120+
Iterator<Timer> it = this.timers.values().iterator();
121+
while (it.hasNext()) {
122+
Timer timer = it.next();
123+
StatisticsPrinter.print(out, timer.getName() + "_time", ((int) timer.getTotalTime()));
124+
if (it.hasNext()) {
125+
StatisticsPrinter.print(out, timer.getName() + "_memory", timer.getTotalMemory());
126+
} else {
127+
StatisticsPrinter.printLast(out, timer.getName() + "_memory", timer.getTotalMemory());
128+
}
129+
}
130+
}
131+
}

substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMNativeImageCodeCache.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.stream.Collectors;
4444
import java.util.stream.IntStream;
4545

46+
import com.oracle.graal.pointsto.util.TimerCollection;
4647
import org.graalvm.compiler.code.CompilationResult;
4748
import org.graalvm.compiler.core.common.NumUtil;
4849
import org.graalvm.compiler.debug.DebugContext;
@@ -55,7 +56,6 @@
5556
import com.oracle.graal.pointsto.BigBang;
5657
import com.oracle.graal.pointsto.util.CompletionExecutor;
5758
import com.oracle.graal.pointsto.util.CompletionExecutor.DebugContextRunnable;
58-
import com.oracle.graal.pointsto.util.Timer;
5959
import com.oracle.graal.pointsto.util.Timer.StopTimer;
6060
import com.oracle.objectfile.ObjectFile;
6161
import com.oracle.objectfile.ObjectFile.Element;
@@ -116,17 +116,17 @@ public int getCodeCacheSize() {
116116
public void layoutMethods(DebugContext debug, String imageName, BigBang bb, ForkJoinPool threadPool) {
117117
try (Indent indent = debug.logAndIndent("layout methods")) {
118118
BatchExecutor executor = new BatchExecutor(bb, threadPool);
119-
try (StopTimer t = new Timer(imageName, "(bitcode)").start()) {
119+
try (StopTimer t = TimerCollection.createTimerAndStart(imageName, "(bitcode)")) {
120120
writeBitcode(executor);
121121
}
122122
int numBatches;
123-
try (StopTimer t = new Timer(imageName, "(prelink)").start()) {
123+
try (StopTimer t = TimerCollection.createTimerAndStart(imageName, "(prelink)")) {
124124
numBatches = createBitcodeBatches(executor, debug);
125125
}
126-
try (StopTimer t = new Timer(imageName, "(llvm)").start()) {
126+
try (StopTimer t = TimerCollection.createTimerAndStart(imageName, "(llvm)")) {
127127
compileBitcodeBatches(executor, debug, numBatches);
128128
}
129-
try (StopTimer t = new Timer(imageName, "(postlink)").start()) {
129+
try (StopTimer t = TimerCollection.createTimerAndStart(imageName, "(postlink)")) {
130130
linkCompiledBatches(executor, debug, numBatches);
131131
}
132132
}

0 commit comments

Comments
 (0)