diff --git a/src/hotspot/os/aix/os_aix.cpp b/src/hotspot/os/aix/os_aix.cpp
index 561ef77b18b..9335a6ae99f 100644
--- a/src/hotspot/os/aix/os_aix.cpp
+++ b/src/hotspot/os/aix/os_aix.cpp
@@ -273,6 +273,22 @@ julong os::Aix::available_memory() {
}
}
+jlong os::total_swap_space() {
+ perfstat_memory_total_t memory_info;
+ if (libperfstat::perfstat_memory_total(NULL, &memory_info, sizeof(perfstat_memory_total_t), 1) == -1) {
+ return -1;
+ }
+ return (jlong)(memory_info.pgsp_total * 4 * K);
+}
+
+jlong os::free_swap_space() {
+ perfstat_memory_total_t memory_info;
+ if (libperfstat::perfstat_memory_total(NULL, &memory_info, sizeof(perfstat_memory_total_t), 1) == -1) {
+ return -1;
+ }
+ return (jlong)(memory_info.pgsp_free * 4 * K);
+}
+
julong os::physical_memory() {
return Aix::physical_memory();
}
diff --git a/src/hotspot/os/bsd/os_bsd.cpp b/src/hotspot/os/bsd/os_bsd.cpp
index 1f2094fba1f..42a0b9c0832 100644
--- a/src/hotspot/os/bsd/os_bsd.cpp
+++ b/src/hotspot/os/bsd/os_bsd.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2024, 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
@@ -181,6 +181,32 @@ void os::Bsd::print_uptime_info(outputStream* st) {
}
}
+jlong os::total_swap_space() {
+#if defined(__APPLE__)
+ struct xsw_usage vmusage;
+ size_t size = sizeof(vmusage);
+ if (sysctlbyname("vm.swapusage", &vmusage, &size, NULL, 0) != 0) {
+ return -1;
+ }
+ return (jlong)vmusage.xsu_total;
+#else
+ return -1;
+#endif
+}
+
+jlong os::free_swap_space() {
+#if defined(__APPLE__)
+ struct xsw_usage vmusage;
+ size_t size = sizeof(vmusage);
+ if (sysctlbyname("vm.swapusage", &vmusage, &size, NULL, 0) != 0) {
+ return -1;
+ }
+ return (jlong)vmusage.xsu_avail;
+#else
+ return -1;
+#endif
+}
+
julong os::physical_memory() {
return Bsd::physical_memory();
}
diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp
index fb6701a5014..5741694a10c 100644
--- a/src/hotspot/os/linux/os_linux.cpp
+++ b/src/hotspot/os/linux/os_linux.cpp
@@ -289,6 +289,34 @@ julong os::Linux::free_memory() {
return free_mem;
}
+jlong os::total_swap_space() {
+ if (OSContainer::is_containerized()) {
+ if (OSContainer::memory_limit_in_bytes() > 0) {
+ return (jlong)(OSContainer::memory_and_swap_limit_in_bytes() - OSContainer::memory_limit_in_bytes());
+ }
+ }
+ struct sysinfo si;
+ int ret = sysinfo(&si);
+ if (ret != 0) {
+ return -1;
+ }
+ return (jlong)(si.totalswap * si.mem_unit);
+}
+
+jlong os::free_swap_space() {
+ if (OSContainer::is_containerized()) {
+ // TODO add a good implementation
+ return -1;
+ } else {
+ struct sysinfo si;
+ int ret = sysinfo(&si);
+ if (ret != 0) {
+ return -1;
+ }
+ return (jlong)(si.freeswap * si.mem_unit);
+ }
+}
+
julong os::physical_memory() {
jlong phys_mem = 0;
if (OSContainer::is_containerized()) {
diff --git a/src/hotspot/os/windows/os_windows.cpp b/src/hotspot/os/windows/os_windows.cpp
index c97630248ed..d5b17d0a317 100644
--- a/src/hotspot/os/windows/os_windows.cpp
+++ b/src/hotspot/os/windows/os_windows.cpp
@@ -840,6 +840,20 @@ julong os::win32::available_memory() {
return (julong)ms.ullAvailPhys;
}
+jlong os::total_swap_space() {
+ MEMORYSTATUSEX ms;
+ ms.dwLength = sizeof(ms);
+ GlobalMemoryStatusEx(&ms);
+ return (jlong) ms.ullTotalPageFile;
+}
+
+jlong os::free_swap_space() {
+ MEMORYSTATUSEX ms;
+ ms.dwLength = sizeof(ms);
+ GlobalMemoryStatusEx(&ms);
+ return (jlong) ms.ullAvailPageFile;
+}
+
julong os::physical_memory() {
return win32::physical_memory();
}
diff --git a/src/hotspot/share/jfr/metadata/metadata.xml b/src/hotspot/share/jfr/metadata/metadata.xml
index 53fa17555c6..34925674f8a 100644
--- a/src/hotspot/share/jfr/metadata/metadata.xml
+++ b/src/hotspot/share/jfr/metadata/metadata.xml
@@ -927,6 +927,11 @@
+
+
+
+
+
diff --git a/src/hotspot/share/jfr/periodic/jfrPeriodic.cpp b/src/hotspot/share/jfr/periodic/jfrPeriodic.cpp
index 0648a5e64db..65b23e3433a 100644
--- a/src/hotspot/share/jfr/periodic/jfrPeriodic.cpp
+++ b/src/hotspot/share/jfr/periodic/jfrPeriodic.cpp
@@ -527,6 +527,13 @@ TRACE_REQUEST_FUNC(PhysicalMemory) {
event.commit();
}
+TRACE_REQUEST_FUNC(SwapSpace) {
+ EventSwapSpace event;
+ event.set_totalSize(os::total_swap_space());
+ event.set_freeSize(os::free_swap_space());
+ event.commit();
+}
+
TRACE_REQUEST_FUNC(JavaThreadStatistics) {
EventJavaThreadStatistics event;
event.set_activeCount(ThreadService::get_live_thread_count());
diff --git a/src/hotspot/share/runtime/os.hpp b/src/hotspot/share/runtime/os.hpp
index 67e1ef7dcf7..df4b7c90559 100644
--- a/src/hotspot/share/runtime/os.hpp
+++ b/src/hotspot/share/runtime/os.hpp
@@ -328,6 +328,9 @@ class os: AllStatic {
static julong available_memory();
static julong free_memory();
+ static jlong total_swap_space();
+ static jlong free_swap_space();
+
static julong physical_memory();
static bool has_allocatable_memory_limit(size_t* limit);
static bool is_server_class_machine();
diff --git a/src/jdk.jfr/share/conf/jfr/default.jfc b/src/jdk.jfr/share/conf/jfr/default.jfc
index b9e1308cd21..cd6d45bf285 100644
--- a/src/jdk.jfr/share/conf/jfr/default.jfc
+++ b/src/jdk.jfr/share/conf/jfr/default.jfc
@@ -678,6 +678,11 @@
everyChunk
+
+ true
+ everyChunk
+
+
false
true
diff --git a/src/jdk.jfr/share/conf/jfr/profile.jfc b/src/jdk.jfr/share/conf/jfr/profile.jfc
index ccc8dc6b661..dc9c5c326f2 100644
--- a/src/jdk.jfr/share/conf/jfr/profile.jfc
+++ b/src/jdk.jfr/share/conf/jfr/profile.jfc
@@ -678,6 +678,11 @@
everyChunk
+
+ true
+ everyChunk
+
+
false
true
diff --git a/test/jdk/jdk/jfr/event/os/TestSwapSpaceEvent.java b/test/jdk/jdk/jfr/event/os/TestSwapSpaceEvent.java
new file mode 100644
index 00000000000..5ea2ed139ea
--- /dev/null
+++ b/test/jdk/jdk/jfr/event/os/TestSwapSpaceEvent.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2013, 2024, 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 jdk.jfr.event.os;
+
+import java.util.List;
+
+import jdk.jfr.Recording;
+import jdk.jfr.consumer.RecordedEvent;
+import jdk.test.lib.jfr.EventNames;
+import jdk.test.lib.jfr.Events;
+
+/**
+ * @test
+ * @key jfr
+ * @requires vm.hasJFR
+ * @requires (os.family != "linux")
+ * @library /test/lib
+ * @run main/othervm jdk.jfr.event.os.TestSwapSpaceEvent
+ */
+
+/**
+ * @test
+ * @key jfr
+ * @requires vm.hasJFR
+ * @requires (os.family == "linux")
+ * @library /test/lib
+ * @run main/othervm -XX:-UseContainerSupport jdk.jfr.event.os.TestSwapSpaceEvent
+ */
+public class TestSwapSpaceEvent {
+ private final static String EVENT_NAME = EventNames.SwapSpace;
+
+ public static void main(String[] args) throws Throwable {
+ Recording recording = new Recording();
+ recording.enable(EVENT_NAME);
+ recording.start();
+ recording.stop();
+ List events = Events.fromRecording(recording);
+ Events.hasEvents(events);
+ for (RecordedEvent event : events) {
+ System.out.println("Event: " + event);
+ long totalSize = Events.assertField(event, "totalSize").atLeast(0L).getValue();
+ Events.assertField(event, "freeSize").atLeast(0L);
+ Events.assertField(event, "freeSize").atMost(totalSize);
+ }
+ }
+}
diff --git a/test/lib/jdk/test/lib/jfr/EventNames.java b/test/lib/jdk/test/lib/jfr/EventNames.java
index 32bffc8dda7..3799af54804 100644
--- a/test/lib/jdk/test/lib/jfr/EventNames.java
+++ b/test/lib/jdk/test/lib/jfr/EventNames.java
@@ -186,6 +186,7 @@ public class EventNames {
public static final String NativeLibraryLoad = PREFIX + "NativeLibraryLoad";
public static final String NativeLibraryUnload = PREFIX + "NativeLibraryUnload";
public static final String PhysicalMemory = PREFIX + "PhysicalMemory";
+ public static final String SwapSpace = PREFIX + "SwapSpace";
public static final String NetworkUtilization = PREFIX + "NetworkUtilization";
public static final String ProcessStart = PREFIX + "ProcessStart";
public static final String ResidentSetSize = PREFIX + "ResidentSetSize";