Skip to content

Commit 6fc7077

Browse files
authored
Refactor the SmapEntry event generation (#9215)
1 parent 37a4462 commit 6fc7077

File tree

10 files changed

+1313
-313
lines changed

10 files changed

+1313
-313
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package datadog.trace.bootstrap.instrumentation.jfr;
22

33
import datadog.trace.api.Platform;
4-
import datadog.trace.bootstrap.instrumentation.jfr.directallocation.DirectAllocationTotalEvent;
54
import jdk.jfr.Event;
65
import jdk.jfr.FlightRecorder;
76

87
public final class JfrHelper {
98
public static void addPeriodicEvent(Class<? extends Event> eventClass, Runnable eventHook) {
109
if (!Platform.isNativeImageBuilder()) {
11-
FlightRecorder.addPeriodicEvent(DirectAllocationTotalEvent.class, eventHook);
10+
FlightRecorder.addPeriodicEvent(eventClass, eventHook);
1211
}
1312
}
1413
}

dd-java-agent/agent-profiling/profiling-controller-openjdk/src/main/java/com/datadog/profiling/controller/openjdk/OpenJdkController.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,27 @@ ProfilingConfig.PROFILING_ALLOCATION_ENABLED, isObjectAllocationSampleAvailable(
205205
}
206206
}
207207

208-
if (!configProvider.getBoolean(
208+
if (configProvider.getBoolean(
209209
ProfilingConfig.PROFILING_SMAP_COLLECTION_ENABLED,
210210
ProfilingConfig.PROFILING_SMAP_COLLECTION_ENABLED_DEFAULT)) {
211-
disableEvent(recordingSettings, "datadog.SmapEntry", "User disabled smaps collection");
212-
} else if (!configProvider.getBoolean(
211+
enableEvent(
212+
recordingSettings, "datadog.SmapEntry", "Smaps collection is enabled in the config");
213+
} else {
214+
disableEvent(
215+
recordingSettings, "datadog.SmapEntry", "Smaps collection is disabled in the config");
216+
}
217+
if (configProvider.getBoolean(
213218
ProfilingConfig.PROFILING_SMAP_AGGREGATION_ENABLED,
214219
ProfilingConfig.PROFILING_SMAP_AGGREGATION_ENABLED_DEFAULT)) {
220+
enableEvent(
221+
recordingSettings,
222+
"datadog.AggregatedSmapEntry",
223+
"Aggregated smaps collection is enabled in the config");
224+
} else {
215225
disableEvent(
216226
recordingSettings,
217227
"datadog.AggregatedSmapEntry",
218-
"User disabled aggregated smaps collection");
228+
"Aggregated smaps collection is disabled in the config");
219229
}
220230

221231
// Warn users for expensive events
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package com.datadog.profiling.controller.openjdk.events;
22

3-
import java.util.HashMap;
43
import java.util.List;
4+
import java.util.stream.Collectors;
55
import jdk.jfr.Category;
66
import jdk.jfr.DataAmount;
77
import jdk.jfr.Description;
88
import jdk.jfr.Event;
99
import jdk.jfr.EventType;
1010
import jdk.jfr.Label;
1111
import jdk.jfr.Name;
12-
import jdk.jfr.Period;
1312
import jdk.jfr.StackTrace;
1413

1514
@Name("datadog.AggregatedSmapEntry")
1615
@Label("Aggregated Smap Entry")
1716
@Description("Entry for the entries from the smaps file aggregated by NMT category")
1817
@Category("Datadog")
19-
@Period("beginChunk")
2018
@StackTrace(false)
2119
public class AggregatedSmapEntryEvent extends Event {
20+
private static final EventType TYPE = EventType.getEventType(AggregatedSmapEntryEvent.class);
21+
2222
@Label("NMT Category")
2323
private final String nmtCategory;
2424

@@ -31,26 +31,12 @@ public AggregatedSmapEntryEvent(String nmtCategory, long rss) {
3131
this.rss = rss;
3232
}
3333

34-
public static void emit() {
35-
if (EventType.getEventType(AggregatedSmapEntryEvent.class).isEnabled()) {
36-
HashMap<String, Long> aggregatedSmapEntries = new HashMap<>();
37-
List<? extends Event> collectedEvents = SmapEntryFactory.collectEvents();
38-
// A single entry should only be expected for the error cases
39-
if (collectedEvents.size() > 1) {
40-
collectedEvents.forEach(
41-
entry -> {
42-
aggregatedSmapEntries.merge(
43-
((SmapEntryEvent) entry).getNmtCategory(),
44-
((SmapEntryEvent) entry).getRss(),
45-
Long::sum);
46-
});
47-
aggregatedSmapEntries.forEach(
48-
(nmtCategory, rss) -> {
49-
new AggregatedSmapEntryEvent(nmtCategory, rss).commit();
50-
});
51-
} else {
52-
collectedEvents.forEach(Event::commit);
53-
}
34+
static void emit(List<SmapEntryEvent> events) {
35+
if (TYPE.isEnabled()) {
36+
events.stream()
37+
.collect(Collectors.groupingBy(e -> e.nmtCategory, Collectors.summingLong(e -> e.rss)))
38+
.forEach(
39+
(category, totalRss) -> new AggregatedSmapEntryEvent(category, totalRss).commit());
5440
}
5541
}
5642
}

0 commit comments

Comments
 (0)