Skip to content

Commit

Permalink
Add NMT events to JFR and add peak tracking and some tests.
Browse files Browse the repository at this point in the history
add peak tracking and tests
  • Loading branch information
roberttoyonaga committed Mar 27, 2024
1 parent 4b1e416 commit 06c6404
Show file tree
Hide file tree
Showing 13 changed files with 453 additions and 11 deletions.
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 @@ -172,7 +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());

RuntimeClassInitializationSupport rci = ImageSingletons.lookup(RuntimeClassInitializationSupport.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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;

import com.oracle.svm.core.nmt.NmtCategory;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

public class JfrNmtCategorySerializer implements JfrSerializer {
@Platforms(Platform.HOSTED_ONLY.class)
public JfrNmtCategorySerializer() {
}

@Override
public void write(JfrChunkWriter writer) {
writer.writeCompressedLong(JfrType.NMTType.getId());

NmtCategory[] nmtCategories = NmtCategory.values();
writer.writeCompressedLong(nmtCategories.length);
for (int i = 0; i < nmtCategories.length; i++) {
writer.writeCompressedInt(i);
writer.writeString(nmtCategories[i].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 @@ -38,8 +39,11 @@
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.NmtCategory;
import com.oracle.svm.core.nmt.NativeMemoryTracking;
import com.oracle.svm.core.thread.JavaVMOperation;
import com.oracle.svm.core.thread.VMThreads;
import com.oracle.svm.core.VMInspectionOptions;

import jdk.jfr.Event;
import jdk.jfr.Name;
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,66 @@ 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();
}
}

/** These peak events are specific to SubstrateVM. */
private static void emitNmtPeakEvents() {
NativeMemoryUsageTotalPeakEvent nmtTotalPeakEvent = new NativeMemoryUsageTotalPeakEvent();
nmtTotalPeakEvent.countAtPeak = NativeMemoryTracking.singleton().getCountAtTotalPeakUsage();
nmtTotalPeakEvent.peakUsage = NativeMemoryTracking.singleton().getPeakTotalUsedMemory();
nmtTotalPeakEvent.commit();

for (NmtCategory nmtCategory : NmtCategory.values()) {
NativeMemoryUsagePeakEvent nmtPeakEvent = new NativeMemoryUsagePeakEvent();
nmtPeakEvent.type = nmtCategory.getName();
nmtPeakEvent.countAtPeak = NativeMemoryTracking.singleton().getCountAtPeakUsage(nmtCategory);
nmtPeakEvent.peakUsage = NativeMemoryTracking.singleton().getPeakUsedMemory(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) {
JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.NativeMemoryUsage);
JfrNativeEventWriter.putLong(data, timestamp);
/* Category */
JfrNativeEventWriter.putLong(data, nmtCategory.ordinal());
/* Reserved usage */
JfrNativeEventWriter.putLong(data, NativeMemoryTracking.singleton().getUsedMemory(nmtCategory));
/* Committed usage */
JfrNativeEventWriter.putLong(data, NativeMemoryTracking.singleton().getUsedMemory(nmtCategory));
JfrNativeEventWriter.endSmallEvent(data);
}
}
if (JfrEvent.NativeMemoryUsageTotal.shouldEmit()) {
JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.NativeMemoryUsageTotal);
JfrNativeEventWriter.putLong(data, timestamp);
/* Reserved usage */
JfrNativeEventWriter.putLong(data, NativeMemoryTracking.singleton().getTotalUsedMemory());
/* Committed usage */
JfrNativeEventWriter.putLong(data, NativeMemoryTracking.singleton().getTotalUsedMemory());
JfrNativeEventWriter.endSmallEvent(data);
}

}

private static void emitPerThreadEvents() {
if (needsVMOperation()) {
EmitPeriodicPerThreadEventsOperation vmOp = new EmitPeriodicPerThreadEventsOperation();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.Label;
import jdk.jfr.Name;
import jdk.jfr.StackTrace;

@Name("svm.NativeMemoryUsagePeak")
@Label("Native Memory Usage Peak")
@Description("Information about native memory peak usage of committed virtual memory and malloc.")
@Category("Native Image")
@StackTrace(false)
public class NativeMemoryUsagePeakEvent extends Event {
@Label("Memory Type") public String type;
@Label("Peak Usage") public long peakUsage;
@Label("Count At Peak") public long countAtPeak;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.Label;
import jdk.jfr.Name;
import jdk.jfr.StackTrace;

@Name("svm.NativeMemoryUsageTotalPeak")
@Label("Native Memory Usage Total Peak")
@Description("Information about native memory peak usage of committed virtual memory and malloc.")
@Category("Native Image")
@StackTrace(false)
public class NativeMemoryUsageTotalPeakEvent extends Event {
@Label("Peak Usage") public long peakUsage;
@Label("Count At Peak") public long countAtPeak;
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,41 @@ private static Pointer getInnerPointer(NmtMallocHeader mallocHeader) {
return ((Pointer) mallocHeader).add(sizeOfNmtHeader());
}

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public long getUsedMemory(NmtCategory category) {
return mallocMemorySnapshot.getInfoByCategory(category).getUsed();
}

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public long getPeakUsedMemory(NmtCategory category) {
return mallocMemorySnapshot.getInfoByCategory(category).getPeakUsed();
}

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public long getCountAtPeakUsage(NmtCategory category) {
return mallocMemorySnapshot.getInfoByCategory(category).getCountAtPeakUsage();
}

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public long getTotalCount() {
return mallocMemorySnapshot.getTotalInfo().getCount();
}

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public long getTotalUsedMemory() {
return mallocMemorySnapshot.getTotalInfo().getUsed();
}

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public long getPeakTotalUsedMemory() {
return mallocMemorySnapshot.getTotalInfo().getPeakUsed();
}

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public long getCountAtTotalPeakUsage() {
return mallocMemorySnapshot.getTotalInfo().getCountAtPeakUsage();
}

public static RuntimeSupport.Hook shutdownHook() {
return isFirstIsolate -> NativeMemoryTracking.singleton().printStatistics();
}
Expand All @@ -156,13 +187,17 @@ public void printStatistics() {
if (VMInspectionOptions.PrintNMTStatistics.getValue()) {
System.out.println();
System.out.println("Native memory tracking");
System.out.println(" Total used memory: " + mallocMemorySnapshot.getTotalInfo().getUsed() + " bytes");
System.out.println(" Total alive allocations: " + mallocMemorySnapshot.getTotalInfo().getCount());
System.out.println(" Peak total used memory: " + getPeakTotalUsedMemory() + " bytes");
System.out.println(" Total alive allocations at peak usage: " + getCountAtTotalPeakUsage());
System.out.println(" Total used memory: " + getTotalUsedMemory() + " bytes");
System.out.println(" Total alive allocations: " + getTotalCount());

for (int i = 0; i < NmtCategory.values().length; i++) {
String name = NmtCategory.values()[i].getName();
NmtMallocMemoryInfo info = mallocMemorySnapshot.getInfoByCategory(i);

System.out.println(" " + name + " peak used memory: " + info.getPeakUsed() + " bytes");
System.out.println(" " + name + " alive allocations at peak: " + info.getCountAtPeakUsage());
System.out.println(" " + name + " used memory: " + info.getUsed() + " bytes");
System.out.println(" " + name + " alive allocations: " + info.getCount());
}
Expand Down
Loading

0 comments on commit 06c6404

Please sign in to comment.