8324287: Record total and free swap space in JFR

Reviewed-by: jbechberger, lucy, egahlin
This commit is contained in:
Matthias Baesken 2024-02-01 12:50:18 +00:00
parent 6b84f9bb3e
commit cab74b075e
11 changed files with 178 additions and 1 deletions

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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()) {

View File

@ -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();
}

View File

@ -927,6 +927,11 @@
<Field type="ulong" contentType="bytes" name="usedSize" label="Used Size" description="Total amount of physical memory in use" />
</Event>
<Event name="SwapSpace" category="Operating System, Memory" label="Swap Space" description="OS Swap Space" period="everyChunk">
<Field type="long" contentType="bytes" name="totalSize" label="Total Size" description="Total swap space available to the OS" />
<Field type="long" contentType="bytes" name="freeSize" label="Free Size" description="Free swap space" />
</Event>
<Event name="ExecutionSample" category="Java Virtual Machine, Profiling" label="Method Profiling Sample" description="Snapshot of a threads state"
period="everyChunk">
<Field type="Thread" name="sampledThread" label="Thread" />

View File

@ -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());

View File

@ -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();

View File

@ -678,6 +678,11 @@
<setting name="period">everyChunk</setting>
</event>
<event name="jdk.SwapSpace">
<setting name="enabled">true</setting>
<setting name="period">everyChunk</setting>
</event>
<event name="jdk.ObjectAllocationInNewTLAB">
<setting name="enabled" control="gc-enabled-high">false</setting>
<setting name="stackTrace">true</setting>

View File

@ -678,6 +678,11 @@
<setting name="period">everyChunk</setting>
</event>
<event name="jdk.SwapSpace">
<setting name="enabled">true</setting>
<setting name="period">everyChunk</setting>
</event>
<event name="jdk.ObjectAllocationInNewTLAB">
<setting name="enabled" control="gc-enabled-high">false</setting>
<setting name="stackTrace">true</setting>

View File

@ -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<RecordedEvent> 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);
}
}
}

View File

@ -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";