Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-53715] Add Java-level JFR event tests and annotate JfrAdaptiveSampler methods with BasedOnJDKFile #8878

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;

import com.oracle.svm.core.util.BasedOnJDKFile;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.headers.LibM;
import com.oracle.svm.core.jdk.UninterruptibleUtils;
Expand Down Expand Up @@ -65,6 +66,7 @@ abstract class JfrAdaptiveSampler {
activeWindow = window0;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L79-L89")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
protected boolean sample(long timestampNs) {
boolean expired = activeWindow.isExpired(timestampNs);
Expand Down Expand Up @@ -100,6 +102,7 @@ private void rotate(JfrSamplerWindow expired) {
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
protected abstract JfrSamplerParams nextWindowParams();

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L145-L156")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private void configure(JfrSamplerParams params, JfrSamplerWindow expired, JfrSamplerWindow next) {
if (params.reconfigure) {
Expand All @@ -116,11 +119,13 @@ private void configure(JfrSamplerParams params, JfrSamplerWindow expired, JfrSam
next.initialize(params.windowDurationMs);
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L173-L175")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private static double computeEwmaAlphaCoefficient(long lookbackCount) {
return lookbackCount <= 1 ? 1d : 1d / lookbackCount;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L177-L182")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private static long computeAccumulatedDebtCarryLimit(long windowDurationMs) {
if (windowDurationMs == 0 || windowDurationMs >= TimeUtils.millisPerSecond) {
Expand All @@ -129,6 +134,7 @@ private static long computeAccumulatedDebtCarryLimit(long windowDurationMs) {
return TimeUtils.millisPerSecond / windowDurationMs;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L217-L229")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private void setRate(JfrSamplerParams params, JfrSamplerWindow expired, JfrSamplerWindow next) {
long sampleSize = projectSampleSize(params, expired);
Expand All @@ -141,11 +147,13 @@ private void setRate(JfrSamplerParams params, JfrSamplerWindow expired, JfrSampl
next.setProjectedPopulationSize(sampleSize * next.getSamplingInterval());
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L236-L238")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private long projectSampleSize(JfrSamplerParams params, JfrSamplerWindow expired) {
return params.samplePointsPerWindow + amortizeDebt(expired);
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L259-L269")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
protected long amortizeDebt(JfrSamplerWindow expired) {
long accumulatedDebt = expired.getAccumulatedDebt();
Expand All @@ -158,6 +166,7 @@ protected long amortizeDebt(JfrSamplerWindow expired) {
return -accumulatedDebt;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L316-L325")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private long deriveSamplingInterval(double sampleSize, JfrSamplerWindow expired) {
assert sampleSize > 0;
Expand All @@ -176,11 +185,13 @@ private double projectPopulationSize(JfrSamplerWindow expired) {
return avgPopulationSize;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L169-L171")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private static double exponentiallyWeightedMovingAverage(double currentMeasurement, double alpha, double prevEwma) {
return alpha * currentMeasurement + (1 - alpha) * prevEwma;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L304-L314")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private long nextGeometric(double p) {
double u = prng.nextUniform();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;

import com.oracle.svm.core.util.BasedOnJDKFile;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.jfr.JfrTicks;
import com.oracle.svm.core.thread.JavaSpinLockUtils;
Expand All @@ -47,9 +48,11 @@ public class JfrEventThrottler extends JfrAdaptiveSampler {
private static final long TEN_PER_1000_MS_IN_HOURS = 36000;
private static final long DAY = 24 * HOUR;
private static final long TEN_PER_1000_MS_IN_DAYS = 864000;

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/recorder/service/jfrEventThrottler.cpp#L105") //
private static final long DEFAULT_WINDOW_LOOKBACK_COUNT = 25;
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/recorder/service/jfrEventThrottler.cpp#L112") //
private static final long LOW_RATE_UPPER_BOUND = 9;
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/recorder/service/jfrEventThrottler.cpp#L113") //
private static final long WINDOW_DIVISOR = 5;

private static final JfrSamplerParams DISABLED_PARAMS = new JfrSamplerParams();
Expand Down Expand Up @@ -96,6 +99,7 @@ protected JfrSamplerParams nextWindowParams() {
return disabled ? DISABLED_PARAMS : lastParams;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/recorder/service/jfrEventThrottler.cpp#L200-L212")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private void updateParams() {
normalize();
Expand All @@ -110,6 +114,7 @@ private static boolean isDisabled(long eventSampleSize) {
return eventSampleSize == Target_jdk_jfr_internal_settings_ThrottleSetting.OFF;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/recorder/service/jfrEventThrottler.cpp#L170-L194")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private void normalize() {
if (periodMs == TimeUtils.millisPerSecond) {
Expand All @@ -130,6 +135,7 @@ private void normalize() {
}
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/recorder/service/jfrEventThrottler.cpp#L145-L165")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private static void setSamplePointsAndWindowDuration(JfrSamplerParams params, long sampleSize, long periodMs) {
assert sampleSize != Target_jdk_jfr_internal_settings_ThrottleSetting.OFF;
Expand All @@ -150,12 +156,14 @@ private static void setSamplePointsAndWindowDuration(JfrSamplerParams params, lo
}
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/recorder/service/jfrEventThrottler.cpp#L134-L137")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private static void setLowRate(JfrSamplerParams params, long eventSampleSize, long periodMs) {
params.samplePointsPerWindow = eventSampleSize;
params.windowDurationMs = periodMs;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/recorder/service/jfrEventThrottler.cpp#L122-L132")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private static void setWindowLookback(JfrSamplerParams params) {
if (params.windowDurationMs <= TimeUtils.millisPerSecond) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;

import com.oracle.svm.core.util.BasedOnJDKFile;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.jdk.UninterruptibleUtils;
import com.oracle.svm.core.jfr.JfrTicks;
Expand Down Expand Up @@ -68,6 +69,7 @@ public void copyParams(JfrSamplerParams other) {
this.params.initializeFrom(other);
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L104-L108")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public boolean sample() {
long ordinal = measuredPopulationSize.incrementAndGet();
Expand Down Expand Up @@ -99,21 +101,25 @@ public void setProjectedPopulationSize(long value) {
projectedPopulationSize = value;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L285-L287")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public long getAccumulatedDebt() {
return projectedPopulationSize == 0 ? 0 : (params.samplePointsPerWindow - getMaxSampleSize()) + getDebt();
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L289-L291")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private long getDebt() {
return projectedPopulationSize == 0 ? 0 : getSampleSize() - params.samplePointsPerWindow;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L271-L273")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private long getMaxSampleSize() {
return projectedPopulationSize / samplingInterval;
}

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/share/jfr/support/jfrAdaptiveSampler.cpp#L276-L279")
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private long getSampleSize() {
long size = getPopulationSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@
* https://github.com/openjdk/jdk/blob/(tag or revision)/(path/to/the/source/file)(#L(line_start)-L(line_end))?
* </pre>
*
* To specify a line range, a suffix of the form {@code #L[0-9]+-L[0-9]+} might be added. Full
* example:
* To specify a line range, a suffix of the form {@code #L[0-9]+-L[0-9]+} might be added.
* Example:
*
* <pre>
* &#64;BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/cpu/x86/vm_version_x86.hpp#L40-L304")
* </pre>
*
* Single lines can use the {@code #L[0-9]} suffix. Example:
*
* <pre>
* &#64;BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-23+8/src/hotspot/cpu/x86/vm_version_x86.hpp#L40")
* </pre>
*/
String value();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public class BasedOnJDKFileProcessor extends AbstractProcessor {
static final String ANNOTATION_CLASS_NAME = "com.oracle.svm.core.util.BasedOnJDKFile";
static final String ANNOTATION_LIST_CLASS_NAME = "com.oracle.svm.core.util.BasedOnJDKFile.List";
static final Pattern FILE_PATTERN = Pattern
.compile("^https://github.com/openjdk/jdk/blob/(?<committish>[^/]+)/(?<path>[-_.A-Za-z0-9][-_./A-Za-z0-9]*)(#L(?<lineStart>[0-9]+)-L(?<lineEnd>[0-9]+))?$");
static final String FILE_PATTERN_STR = "https://github.com/openjdk/jdk/blob/<tag|revision>/path/to/file.ext(#L[0-9]+-L[0-9]+)?";
.compile("^https://github.com/openjdk/jdk/blob/(?<committish>[^/]+)/(?<path>[-_.A-Za-z0-9][-_./A-Za-z0-9]*)(#L(?<lineStart>[0-9]+)(-L(?<lineEnd>[0-9]+))?)?$");
static final String FILE_PATTERN_STR = "https://github.com/openjdk/jdk/blob/<tag|revision>/path/to/file.ext(#L[0-9]+(-L[0-9]+)?)?";
public static final int FULL_FILE_LINE_MARKER = 0;

private final Set<Element> processed = new HashSet<>();
Expand Down Expand Up @@ -181,10 +181,21 @@ private SourceInfo parseBasedOnJDKFileAnnotation(String annotationValue) {
env().getMessager().printMessage(ERROR, String.format("Invalid path: %s%nShould be %s", annotationValue, FILE_PATTERN_STR));
return null;
}
String lineStart = matcher.group("lineStart");
String lineEnd = matcher.group("lineEnd");
return new SourceInfo(matcher.group("committish"), matcher.group("path"), lineStart == null ? FULL_FILE_LINE_MARKER : Long.parseLong(lineStart),
lineEnd == null ? FULL_FILE_LINE_MARKER : Long.parseLong(lineEnd));
String lineStartStr = matcher.group("lineStart");
String lineEndStr = matcher.group("lineEnd");
long lineStart = lineStartStr == null ? FULL_FILE_LINE_MARKER : Long.parseLong(lineStartStr);
final long lineEnd;
if (lineEndStr == null) {
if (lineStartStr != null) {
// no lineEnd but lineStart -> single line url
lineEnd = lineStart;
} else {
lineEnd = FULL_FILE_LINE_MARKER;
}
} else {
lineEnd = Long.parseLong(lineEndStr);
}
return new SourceInfo(matcher.group("committish"), matcher.group("path"), lineStart, lineEnd);
}

private SourceInfo getAnnotatedSourceInfo(Element annotatedElement) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2024, Red Hat Inc. 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 com.oracle.svm.test.jfr;

import static org.junit.Assert.assertTrue;

import java.time.Duration;
import java.util.List;

import org.junit.Test;

import com.oracle.svm.core.jfr.JfrEvent;

import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;

public class TestFlightRecorderEvents extends JfrRecordingTest {
private static final long THRESHOLD = 12345678;
private static final long MAX_SIZE = 33554432;

@Test
public void test() throws Throwable {
String[] events = new String[]{"jdk.ActiveRecording", "jdk.ActiveSetting"};
/* Set properties before recording is started so we can accurately validate them later. */
Recording recording = prepareRecording(events, getDefaultConfiguration(), null, createTempJfrFile());
recording.enable(JfrEvent.ThreadPark.getName()).withThreshold(Duration.ofNanos(THRESHOLD));
recording.setMaxSize(MAX_SIZE);
recording.start();
stopRecording(recording, TestFlightRecorderEvents::validateEvents);
}

private static void validateEvents(List<RecordedEvent> events) {
boolean foundActiveRecording = false;
boolean foundActiveSetting = false;
for (RecordedEvent e : events) {
String name = e.getEventType().getName();
if (name.equals("jdk.ActiveSetting") &&
e.getLong("id") == JfrEvent.ThreadPark.getId() &&
e.getString("name").equals("threshold") &&
e.getString("value").equals(THRESHOLD + " ns")) {
foundActiveSetting = true;
} else if (name.equals("jdk.ActiveRecording") &&
e.getLong("maxSize") == MAX_SIZE) {
foundActiveRecording = true;
}
}
assertTrue(foundActiveSetting);
assertTrue(foundActiveRecording);
}
}
Loading
Loading