Skip to content

Commit d75d977

Browse files
author
Mandy Chung
committed
8285447: StackWalker minimal batch size should be optimized for getCallerClass
Reviewed-by: simonis
1 parent fc3e826 commit d75d977

File tree

3 files changed

+71
-18
lines changed

3 files changed

+71
-18
lines changed

src/java.base/share/classes/java/lang/StackStreamFactory.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ private StackStreamFactory() {}
6969
// lazily add subclasses when they are loaded.
7070
private static final Set<Class<?>> stackWalkImplClasses = init();
7171

72+
// Number of elements in the buffer reserved for VM to use
73+
private static final int RESERVED_ELEMENTS = 1;
74+
private static final int MIN_BATCH_SIZE = RESERVED_ELEMENTS + 2;
7275
private static final int SMALL_BATCH = 8;
7376
private static final int BATCH_SIZE = 32;
7477
private static final int LARGE_BATCH_SIZE = 256;
75-
private static final int MIN_BATCH_SIZE = SMALL_BATCH;
7678

7779
// These flags must match the values maintained in the VM
7880
@Native private static final int DEFAULT_MODE = 0x0;
@@ -196,7 +198,7 @@ protected AbstractStackWalker(StackWalker walker, int mode, int maxDepth) {
196198
protected abstract int batchSize(int lastBatchFrameCount);
197199

198200
/*
199-
* Returns the next batch size, always >= minimum batch size (32)
201+
* Returns the next batch size, always >= minimum batch size
200202
*
201203
* Subclass may override this method if the minimum batch size is different.
202204
*/
@@ -538,8 +540,7 @@ protected void initFrameBuffer() {
538540
protected int batchSize(int lastBatchFrameCount) {
539541
if (lastBatchFrameCount == 0) {
540542
// First batch, use estimateDepth if not exceed the large batch size
541-
// and not too small
542-
int initialBatchSize = Math.max(walker.estimateDepth(), SMALL_BATCH);
543+
int initialBatchSize = Math.max(walker.estimateDepth()+RESERVED_ELEMENTS, MIN_BATCH_SIZE);
543544
return Math.min(initialBatchSize, LARGE_BATCH_SIZE);
544545
} else {
545546
if (lastBatchFrameCount > BATCH_SIZE) {
@@ -747,19 +748,31 @@ protected Integer consumeFrames() {
747748
return n;
748749
}
749750

751+
/*
752+
* Typically finding the caller class only needs to walk two stack frames
753+
* 0: StackWalker::getCallerClass
754+
* 1: API
755+
* 2: caller class
756+
*
757+
* So start the initial batch size with the minimum size.
758+
*/
750759
@Override
751760
protected void initFrameBuffer() {
752-
this.frameBuffer = new ClassFrameBuffer(walker, getNextBatchSize());
761+
this.frameBuffer = new ClassFrameBuffer(walker, MIN_BATCH_SIZE);
753762
}
754763

755764
@Override
756765
protected int batchSize(int lastBatchFrameCount) {
757-
return MIN_BATCH_SIZE;
766+
// this method is only called when the caller class is not found in
767+
// the first batch. getCallerClass may be invoked via core reflection.
768+
// So increase the next batch size as there may be implementation-specific
769+
// frames before reaching the caller class's frame.
770+
return SMALL_BATCH;
758771
}
759772

760773
@Override
761774
protected int getNextBatchSize() {
762-
return MIN_BATCH_SIZE;
775+
return SMALL_BATCH;
763776
}
764777
}
765778

@@ -785,7 +798,7 @@ protected void initFrameBuffer() {
785798
* Each specialized AbstractStackWalker subclass may subclass the FrameBuffer.
786799
*/
787800
abstract static class FrameBuffer<F> {
788-
static final int START_POS = 2; // 0th and 1st elements are reserved
801+
static final int START_POS = RESERVED_ELEMENTS;
789802

790803
// buffers for VM to fill stack frame info
791804
int currentBatchSize; // current batch size
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2023, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.bench.java.lang;
24+
25+
import java.util.concurrent.TimeUnit;
26+
27+
import org.openjdk.jmh.annotations.Benchmark;
28+
import org.openjdk.jmh.annotations.BenchmarkMode;
29+
import org.openjdk.jmh.annotations.Fork;
30+
import org.openjdk.jmh.annotations.Measurement;
31+
import org.openjdk.jmh.annotations.Mode;
32+
import org.openjdk.jmh.annotations.OutputTimeUnit;
33+
import org.openjdk.jmh.annotations.Scope;
34+
import org.openjdk.jmh.annotations.State;
35+
import org.openjdk.jmh.annotations.Warmup;
36+
37+
@Warmup(iterations = 5, time = 1)
38+
@Measurement(iterations = 5, time = 1)
39+
@Fork(value = 3, jvmArgsAppend = {"-Xmx1g", "-Xms1g"})
40+
@State(Scope.Benchmark)
41+
@BenchmarkMode(Mode.AverageTime)
42+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
43+
public class CallerClassBench {
44+
static final StackWalker INST = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
45+
46+
@Benchmark
47+
public Class<?> getCallerClass() {
48+
return INST.getCallerClass();
49+
}
50+
}

test/micro/org/openjdk/bench/java/lang/StackWalkBench.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,16 +310,6 @@ public void run() {
310310
}
311311
}
312312

313-
/**
314-
* StackWalker.getCallerClass()
315-
*/
316-
@Benchmark
317-
public void getCallerClass(Blackhole bh) {
318-
final StackWalker sw = walker(walker);
319-
Class<?> c = sw.getCallerClass();
320-
bh.consume(c);
321-
}
322-
323313
/**
324314
* StackWalker.getCallerClass() with generated call stack of
325315
* the given depth.

0 commit comments

Comments
 (0)