8285447: StackWalker minimal batch size should be optimized for getCallerClass

Reviewed-by: simonis
This commit is contained in:
Mandy Chung 2023-09-12 16:24:16 +00:00
parent fc3e826692
commit d75d9774c8
3 changed files with 71 additions and 18 deletions

View File

@ -69,10 +69,12 @@ final class StackStreamFactory {
// lazily add subclasses when they are loaded.
private static final Set<Class<?>> stackWalkImplClasses = init();
// Number of elements in the buffer reserved for VM to use
private static final int RESERVED_ELEMENTS = 1;
private static final int MIN_BATCH_SIZE = RESERVED_ELEMENTS + 2;
private static final int SMALL_BATCH = 8;
private static final int BATCH_SIZE = 32;
private static final int LARGE_BATCH_SIZE = 256;
private static final int MIN_BATCH_SIZE = SMALL_BATCH;
// These flags must match the values maintained in the VM
@Native private static final int DEFAULT_MODE = 0x0;
@ -196,7 +198,7 @@ final class StackStreamFactory {
protected abstract int batchSize(int lastBatchFrameCount);
/*
* Returns the next batch size, always >= minimum batch size (32)
* Returns the next batch size, always >= minimum batch size
*
* Subclass may override this method if the minimum batch size is different.
*/
@ -538,8 +540,7 @@ final class StackStreamFactory {
protected int batchSize(int lastBatchFrameCount) {
if (lastBatchFrameCount == 0) {
// First batch, use estimateDepth if not exceed the large batch size
// and not too small
int initialBatchSize = Math.max(walker.estimateDepth(), SMALL_BATCH);
int initialBatchSize = Math.max(walker.estimateDepth()+RESERVED_ELEMENTS, MIN_BATCH_SIZE);
return Math.min(initialBatchSize, LARGE_BATCH_SIZE);
} else {
if (lastBatchFrameCount > BATCH_SIZE) {
@ -747,19 +748,31 @@ final class StackStreamFactory {
return n;
}
/*
* Typically finding the caller class only needs to walk two stack frames
* 0: StackWalker::getCallerClass
* 1: API
* 2: caller class
*
* So start the initial batch size with the minimum size.
*/
@Override
protected void initFrameBuffer() {
this.frameBuffer = new ClassFrameBuffer(walker, getNextBatchSize());
this.frameBuffer = new ClassFrameBuffer(walker, MIN_BATCH_SIZE);
}
@Override
protected int batchSize(int lastBatchFrameCount) {
return MIN_BATCH_SIZE;
// this method is only called when the caller class is not found in
// the first batch. getCallerClass may be invoked via core reflection.
// So increase the next batch size as there may be implementation-specific
// frames before reaching the caller class's frame.
return SMALL_BATCH;
}
@Override
protected int getNextBatchSize() {
return MIN_BATCH_SIZE;
return SMALL_BATCH;
}
}
@ -785,7 +798,7 @@ final class StackStreamFactory {
* Each specialized AbstractStackWalker subclass may subclass the FrameBuffer.
*/
abstract static class FrameBuffer<F> {
static final int START_POS = 2; // 0th and 1st elements are reserved
static final int START_POS = RESERVED_ELEMENTS;
// buffers for VM to fill stack frame info
int currentBatchSize; // current batch size

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.lang;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(value = 3, jvmArgsAppend = {"-Xmx1g", "-Xms1g"})
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class CallerClassBench {
static final StackWalker INST = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
@Benchmark
public Class<?> getCallerClass() {
return INST.getCallerClass();
}
}

View File

@ -310,16 +310,6 @@ public class StackWalkBench {
}
}
/**
* StackWalker.getCallerClass()
*/
@Benchmark
public void getCallerClass(Blackhole bh) {
final StackWalker sw = walker(walker);
Class<?> c = sw.getCallerClass();
bh.consume(c);
}
/**
* StackWalker.getCallerClass() with generated call stack of
* the given depth.