Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public final class JfrEvent {
public static final JfrEvent AllocationRequiringGC = create("jdk.AllocationRequiringGC");
public static final JfrEvent OldObjectSample = create("jdk.OldObjectSample");
public static final JfrEvent ObjectAllocationSample = create("jdk.ObjectAllocationSample", JfrEventFlags.SupportsThrottling);
public static final JfrEvent NativeMemoryUsage = create("jdk.NativeMemoryUsage");
public static final JfrEvent NativeMemoryUsageTotal = create("jdk.NativeMemoryUsageTotal");

private final long id;
private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Collections;
import java.util.List;

import com.oracle.svm.core.sampler.SamplerStatistics;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.hosted.Feature;
Expand All @@ -43,6 +42,7 @@
import com.oracle.svm.core.sampler.SamplerJfrStackTraceSerializer;
import com.oracle.svm.core.sampler.SamplerStackTraceSerializer;
import com.oracle.svm.core.sampler.SamplerStackWalkVisitor;
import com.oracle.svm.core.sampler.SamplerStatistics;
import com.oracle.svm.core.thread.ThreadListenerSupport;
import com.oracle.svm.core.thread.ThreadListenerSupportFeature;
import com.oracle.svm.core.util.UserError;
Expand Down Expand Up @@ -172,6 +172,9 @@ public void afterRegistration(AfterRegistrationAccess access) {
JfrSerializerSupport.get().register(new JfrGCNameSerializer());
JfrSerializerSupport.get().register(new JfrVMOperationNameSerializer());
JfrSerializerSupport.get().register(new JfrGCWhenSerializer());
if (VMInspectionOptions.hasNativeMemoryTrackingSupport()) {
JfrSerializerSupport.get().register(new JfrNmtCategorySerializer());
}

ThreadListenerSupport.get().register(SubstrateJVM.getThreadLocal());

Expand Down
42 changes: 15 additions & 27 deletions ...svm/core/nmt/NmtMallocMemorySnapshot.java → ...vm/core/jfr/JfrNmtCategorySerializer.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2023, Red Hat Inc. All rights reserved.
* 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
Expand All @@ -24,39 +24,27 @@
* questions.
*/

package com.oracle.svm.core.nmt;
package com.oracle.svm.core.jfr;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

import com.oracle.svm.core.Uninterruptible;

class NmtMallocMemorySnapshot {
private final NmtMallocMemoryInfo[] categories;
private final NmtMallocMemoryInfo total;
import com.oracle.svm.core.nmt.NmtCategory;

public class JfrNmtCategorySerializer implements JfrSerializer {
@Platforms(Platform.HOSTED_ONLY.class)
NmtMallocMemorySnapshot() {
total = new NmtMallocMemoryInfo();
categories = new NmtMallocMemoryInfo[NmtCategory.values().length];
for (int i = 0; i < categories.length; i++) {
categories[i] = new NmtMallocMemoryInfo();
}
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
NmtMallocMemoryInfo getInfoByCategory(NmtCategory category) {
return getInfoByCategory(category.ordinal());
public JfrNmtCategorySerializer() {
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
NmtMallocMemoryInfo getInfoByCategory(int category) {
assert category < categories.length;
return categories[category];
}
@Override
public void write(JfrChunkWriter writer) {
writer.writeCompressedLong(JfrType.NMTType.getId());

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
NmtMallocMemoryInfo getTotalInfo() {
return total;
NmtCategory[] nmtCategories = NmtCategory.values();
writer.writeCompressedLong(nmtCategories.length);
for (NmtCategory nmtCategory : nmtCategories) {
writer.writeCompressedInt(nmtCategory.ordinal());
writer.writeString(nmtCategory.getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public enum JfrType {
GCWhen("jdk.types.GCWhen"),
VMOperation("jdk.types.VMOperationType"),
MonitorInflationCause("jdk.types.InflateCause"),
OldObject("jdk.types.OldObject");
OldObject("jdk.types.OldObject"),
NMTType("jdk.types.NMTType");

private final long id;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 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
Expand Down Expand Up @@ -29,6 +30,7 @@
import org.graalvm.nativeimage.StackValue;

import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.VMInspectionOptions;
import com.oracle.svm.core.heap.Heap;
import com.oracle.svm.core.heap.PhysicalMemory;
import com.oracle.svm.core.heap.VMOperationInfos;
Expand All @@ -38,6 +40,8 @@
import com.oracle.svm.core.jfr.JfrNativeEventWriterData;
import com.oracle.svm.core.jfr.JfrNativeEventWriterDataAccess;
import com.oracle.svm.core.jfr.JfrTicks;
import com.oracle.svm.core.nmt.NativeMemoryTracking;
import com.oracle.svm.core.nmt.NmtCategory;
import com.oracle.svm.core.thread.JavaVMOperation;
import com.oracle.svm.core.thread.VMThreads;

Expand All @@ -54,6 +58,7 @@ public static void emit() {
emitPhysicalMemory();
emitClassLoadingStatistics();
emitPerThreadEvents();
emitNativeMemoryTrackingEvents();
}

@Uninterruptible(reason = "Accesses a JFR buffer.")
Expand Down Expand Up @@ -102,6 +107,71 @@ private static void emitClassLoadingStatistics() {
}
}

/**
* Emit events for NativeMemoryUsage and NativeMemoryUsageTotal. We do not guarantee consistent
* measurements across NMT categories and the total. Each individual NMT category uses atomic
* counters which may change while we are in this method. Similar to OpenJDK, it is only a
* best-effort approach.
*/
private static void emitNativeMemoryTrackingEvents() {
if (VMInspectionOptions.hasNativeMemoryTrackingSupport()) {
emitJdkNmtEvents(NmtCategory.values());
emitNmtPeakEvents();
}
}

/** Emit Native Image-specific events that report the peak memory usage. */
private static void emitNmtPeakEvents() {
NativeMemoryUsageTotalPeakEvent nmtTotalPeakEvent = new NativeMemoryUsageTotalPeakEvent();

long totalPeakUsed = NativeMemoryTracking.singleton().getPeakTotalUsedMemory();
nmtTotalPeakEvent.peakCommitted = totalPeakUsed;
nmtTotalPeakEvent.peakReserved = totalPeakUsed;
nmtTotalPeakEvent.countAtPeak = NativeMemoryTracking.singleton().getCountAtTotalPeakUsage();
nmtTotalPeakEvent.commit();

for (NmtCategory nmtCategory : NmtCategory.values()) {
NativeMemoryUsagePeakEvent nmtPeakEvent = new NativeMemoryUsagePeakEvent();
nmtPeakEvent.type = nmtCategory.getName();

long peakUsed = NativeMemoryTracking.singleton().getPeakUsedMemory(nmtCategory);
nmtPeakEvent.peakCommitted = peakUsed;
nmtPeakEvent.peakReserved = peakUsed;
nmtPeakEvent.countAtPeak = NativeMemoryTracking.singleton().getCountAtPeakUsage(nmtCategory);
nmtPeakEvent.commit();
}
}

@Uninterruptible(reason = "Accesses a JFR buffer.")
private static void emitJdkNmtEvents(NmtCategory[] nmtCategories) {
long timestamp = JfrTicks.elapsedTicks();
JfrNativeEventWriterData data = StackValue.get(JfrNativeEventWriterData.class);
JfrNativeEventWriterDataAccess.initializeThreadLocalNativeBuffer(data);

if (JfrEvent.NativeMemoryUsage.shouldEmit()) {
for (NmtCategory nmtCategory : nmtCategories) {
long usedMemory = NativeMemoryTracking.singleton().getUsedMemory(nmtCategory);

JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.NativeMemoryUsage);
JfrNativeEventWriter.putLong(data, timestamp);
JfrNativeEventWriter.putLong(data, nmtCategory.ordinal());
JfrNativeEventWriter.putLong(data, usedMemory); // reserved
JfrNativeEventWriter.putLong(data, usedMemory); // committed
JfrNativeEventWriter.endSmallEvent(data);
}
}

if (JfrEvent.NativeMemoryUsageTotal.shouldEmit()) {
long totalUsedMemory = NativeMemoryTracking.singleton().getTotalUsedMemory();

JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.NativeMemoryUsageTotal);
JfrNativeEventWriter.putLong(data, timestamp);
JfrNativeEventWriter.putLong(data, totalUsedMemory); // reserved
JfrNativeEventWriter.putLong(data, totalUsedMemory); // committed
JfrNativeEventWriter.endSmallEvent(data);
}
}

private static void emitPerThreadEvents() {
if (needsVMOperation()) {
EmitPeriodicPerThreadEventsOperation vmOp = new EmitPeriodicPerThreadEventsOperation();
Expand All @@ -111,7 +181,7 @@ private static void emitPerThreadEvents() {

@Uninterruptible(reason = "Used to avoid the VM operation if it is not absolutely needed.")
private static boolean needsVMOperation() {
/* The returned value is racy. */
/* The returned value is racy but this is fine because we recheck in the VM operation. */
return JfrEvent.ThreadCPULoad.shouldEmit() || JfrEvent.ThreadAllocationStatistics.shouldEmit();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.core.jfr.events;

import jdk.jfr.Category;
import jdk.jfr.Description;
import jdk.jfr.Event;
import jdk.jfr.Experimental;
import jdk.jfr.Label;
import jdk.jfr.Name;
import jdk.jfr.StackTrace;

/** Similar to the JFR event jdk.NativeMemoryUsage, except that it tracks the peak usage. */
@Experimental
@Name("jdk.NativeMemoryUsagePeak")
@Label("Native Memory Usage Peak")
@Description("Native memory peak usage for a given memory type in the JVM (GraalVM Native Image only).")
@Category({"Java Virtual Machine", "Memory"})
@StackTrace(false)
public class NativeMemoryUsagePeakEvent extends Event {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the other events with the "jdk" prefix have been ported over from OpenJDK. Maybe there should be some way of differentiating these NMT events. If we want to continue using the "jdk" prefix, do you think it would be a good idea to include a note in the event description indicating these events are built-in specific to Native Image?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've also marked the new events as experimental for now, that's how I would currently look for them. But yes, a short note like (GraalVM Native Image only) or so in the description sounds reasonable to me.

@Label("Memory Type") public String type;
@Label("Peak Reserved") public long peakReserved;
@Label("Peak Committed") public long peakCommitted;
@Label("Count At Peak") public long countAtPeak;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.core.jfr.events;

import jdk.jfr.Category;
import jdk.jfr.Description;
import jdk.jfr.Event;
import jdk.jfr.Experimental;
import jdk.jfr.Label;
import jdk.jfr.Name;
import jdk.jfr.StackTrace;

/** Similar to the JFR event jdk.NativeMemoryUsageTotal, except that it tracks the peak usage. */
@Experimental
@Name("jdk.NativeMemoryUsageTotalPeak")
@Label("Native Memory Usage Total Peak")
@Description("Total native memory peak usage for the JVM (GraalVM Native Image only). Might not be the exact sum of the NativeMemoryUsagePeak events due to timing.")
@Category({"Java Virtual Machine", "Memory"})
@StackTrace(false)
public class NativeMemoryUsageTotalPeakEvent extends Event {
@Label("Peak Reserved") public long peakReserved;
@Label("Peak Committed") public long peakCommitted;
@Label("Count At Peak") public long countAtPeak;
}
Loading