Skip to content

Commit f558a6c

Browse files
committed
8298276: JFR: Update NMT events to make use of common periodic timestamp feature
Reviewed-by: mgronlun
1 parent a58fa6e commit f558a6c

File tree

4 files changed

+36
-51
lines changed

4 files changed

+36
-51
lines changed

src/hotspot/share/services/memJfrReporter.cpp renamed to src/hotspot/share/jfr/periodic/jfrNativeMemoryEvent.cpp

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,59 +24,37 @@
2424

2525
#include "precompiled.hpp"
2626
#include "jfr/jfrEvents.hpp"
27-
#include "services/memJfrReporter.hpp"
27+
#include "jfr/periodic/jfrNativeMemoryEvent.hpp"
2828
#include "services/memTracker.hpp"
2929
#include "services/nmtUsage.hpp"
3030
#include "utilities/globalDefinitions.hpp"
3131
#include "utilities/ticks.hpp"
3232

33-
// Helper class to avoid refreshing the NMTUsage to often and allow
34-
// the two JFR events to use the same data.
35-
class MemJFRCurrentUsage : public AllStatic {
36-
private:
37-
// The age threshold in milliseconds. If older than this refresh the usage.
38-
static const uint64_t AgeThreshold = 50;
33+
static NMTUsage* get_usage(const Ticks& timestamp) {
34+
static Ticks last_timestamp;
35+
static NMTUsage* usage = nullptr;
3936

40-
static Ticks _timestamp;
41-
static NMTUsage* _usage;
42-
43-
public:
44-
static NMTUsage* get_usage();
45-
static Ticks get_timestamp();
46-
};
47-
48-
Ticks MemJFRCurrentUsage::_timestamp;
49-
NMTUsage* MemJFRCurrentUsage::_usage = nullptr;
50-
51-
NMTUsage* MemJFRCurrentUsage::get_usage() {
52-
Tickspan since_baselined = Ticks::now() - _timestamp;
53-
54-
if (_usage == nullptr) {
37+
if (usage == nullptr) {
5538
// First time, create a new NMTUsage.
56-
_usage = new NMTUsage(NMTUsage::OptionsNoTS);
57-
} else if (since_baselined.milliseconds() < AgeThreshold) {
58-
// There is recent enough usage information, return it.
59-
return _usage;
39+
usage = new NMTUsage(NMTUsage::OptionsNoTS);
40+
usage->refresh();
41+
last_timestamp = timestamp;
6042
}
6143

62-
// Refresh the usage information.
63-
_usage->refresh();
64-
_timestamp.stamp();
65-
66-
return _usage;
67-
}
68-
69-
Ticks MemJFRCurrentUsage::get_timestamp() {
70-
return _timestamp;
44+
if (timestamp != last_timestamp) {
45+
// Refresh usage if new timestamp.
46+
usage->refresh();
47+
last_timestamp = timestamp;
48+
}
49+
return usage;
7150
}
7251

73-
void MemJFRReporter::send_total_event() {
52+
void JfrNativeMemoryEvent::send_total_event(const Ticks& timestamp) {
7453
if (!MemTracker::enabled()) {
7554
return;
7655
}
7756

78-
NMTUsage* usage = MemJFRCurrentUsage::get_usage();
79-
Ticks timestamp = MemJFRCurrentUsage::get_timestamp();
57+
NMTUsage* usage = get_usage(timestamp);
8058

8159
EventNativeMemoryUsageTotal event(UNTIMED);
8260
event.set_starttime(timestamp);
@@ -85,7 +63,7 @@ void MemJFRReporter::send_total_event() {
8563
event.commit();
8664
}
8765

88-
void MemJFRReporter::send_type_event(const Ticks& starttime, MEMFLAGS flag, size_t reserved, size_t committed) {
66+
void JfrNativeMemoryEvent::send_type_event(const Ticks& starttime, MEMFLAGS flag, size_t reserved, size_t committed) {
8967
EventNativeMemoryUsage event(UNTIMED);
9068
event.set_starttime(starttime);
9169
event.set_type(NMTUtil::flag_to_index(flag));
@@ -94,13 +72,12 @@ void MemJFRReporter::send_type_event(const Ticks& starttime, MEMFLAGS flag, size
9472
event.commit();
9573
}
9674

97-
void MemJFRReporter::send_type_events() {
75+
void JfrNativeMemoryEvent::send_type_events(const Ticks& timestamp) {
9876
if (!MemTracker::enabled()) {
9977
return;
10078
}
10179

102-
NMTUsage* usage = MemJFRCurrentUsage::get_usage();
103-
Ticks timestamp = MemJFRCurrentUsage::get_timestamp();
80+
NMTUsage* usage = get_usage(timestamp);
10481

10582
for (int index = 0; index < mt_number_of_types; index ++) {
10683
MEMFLAGS flag = NMTUtil::index_to_flag(index);

src/hotspot/share/services/memJfrReporter.hpp renamed to src/hotspot/share/jfr/periodic/jfrNativeMemoryEvent.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
*
2323
*/
2424

25-
#ifndef SHARE_SERVICES_MEMJFRREPORTER_HPP
26-
#define SHARE_SERVICES_MEMJFRREPORTER_HPP
25+
#ifndef SHARE_JFR_PERIODIC_JFRNATIVEMEMORYEVENT_HPP
26+
#define SHARE_JFR_PERIODIC_JFRNATIVEMEMORYEVENT_HPP
2727

2828
#include "memory/allocation.hpp"
2929
#include "services/nmtUsage.hpp"
@@ -33,12 +33,12 @@
3333
// MemJFRReporter is only used by threads sending periodic JFR
3434
// events. These threads are synchronized at a higher level,
3535
// so no more synchronization is needed.
36-
class MemJFRReporter : public AllStatic {
36+
class JfrNativeMemoryEvent : public AllStatic {
3737
private:
3838
static void send_type_event(const Ticks& starttime, MEMFLAGS flag, size_t reserved, size_t committed);
3939
public:
40-
static void send_total_event();
41-
static void send_type_events();
40+
static void send_total_event(const Ticks& timestamp);
41+
static void send_type_events(const Ticks& timestamp);
4242
};
4343

44-
#endif //SHARE_SERVICES_MEMJFRREPORTER_HPP
44+
#endif //SHARE_JFR_PERIODIC_JFRNATIVEMEMORYEVENT_HPP

src/hotspot/share/jfr/periodic/jfrPeriodic.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "jfr/periodic/jfrOSInterface.hpp"
4242
#include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
4343
#include "jfr/periodic/jfrThreadDumpEvent.hpp"
44+
#include "jfr/periodic/jfrNativeMemoryEvent.hpp"
4445
#include "jfr/periodic/jfrNetworkUtilization.hpp"
4546
#include "jfr/recorder/jfrRecorder.hpp"
4647
#include "jfr/utilities/jfrThreadIterator.hpp"
@@ -63,7 +64,6 @@
6364
#include "runtime/vm_version.hpp"
6465
#include "services/classLoadingService.hpp"
6566
#include "services/management.hpp"
66-
#include "services/memJfrReporter.hpp"
6767
#include "services/threadService.hpp"
6868
#include "utilities/exceptions.hpp"
6969
#include "utilities/globalDefinitions.hpp"
@@ -643,9 +643,9 @@ TRACE_REQUEST_FUNC(FinalizerStatistics) {
643643
}
644644

645645
TRACE_REQUEST_FUNC(NativeMemoryUsage) {
646-
MemJFRReporter::send_type_events();
646+
JfrNativeMemoryEvent::send_type_events(timestamp());
647647
}
648648

649649
TRACE_REQUEST_FUNC(NativeMemoryUsageTotal) {
650-
MemJFRReporter::send_total_event();
650+
JfrNativeMemoryEvent::send_total_event(timestamp());
651651
}

test/jdk/jdk/jfr/event/runtime/TestNativeMemoryUsageEvents.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
import static jdk.test.lib.Asserts.assertGreaterThan;
2727
import static jdk.test.lib.Asserts.assertTrue;
28+
import static jdk.test.lib.Asserts.assertEquals;
2829

30+
import java.time.Instant;
2931
import java.util.ArrayList;
3032
import java.util.List;
3133

@@ -132,6 +134,12 @@ private static void verifyExpectedEventTypes(List<RecordedEvent> events) throws
132134
for (String type : UsageEventTypes) {
133135
assertTrue(uniqueEventTypes.contains(type), "Events should include: " + type);
134136
}
137+
// Verify that events only have two timestamps
138+
List<Instant> timestamps = events.stream()
139+
.map(e -> e.getStartTime())
140+
.distinct()
141+
.toList();
142+
assertEquals(timestamps.size(), 2, "Expected two timestamps: " + timestamps);
135143
}
136144

137145
private static void verifyHeapGrowth(List<RecordedEvent> events) throws Exception {

0 commit comments

Comments
 (0)