-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathDirectAllocationTrackingTest.groovy
More file actions
133 lines (116 loc) · 4.95 KB
/
DirectAllocationTrackingTest.groovy
File metadata and controls
133 lines (116 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import datadog.environment.JavaVirtualMachine
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
import datadog.trace.bootstrap.instrumentation.jfr.InstrumentationBasedProfiling
import jdk.jfr.FlightRecorder
import jdk.jfr.Recording
import jdk.jfr.consumer.RecordingFile
import spock.lang.Requires
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import java.nio.file.Files
import java.time.Instant
import java.util.concurrent.atomic.AtomicLong
import java.util.stream.Collectors
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
@Requires({
!JavaVirtualMachine.isJ9()
})
class DirectAllocationTrackingTest extends InstrumentationSpecification {
Recording recording
Instant start
@Override
protected void configurePreAgent() {
super.configurePreAgent()
injectSysConfig("dd.profiling.enabled", "true")
injectSysConfig("dd.integration.mmap.enabled", "true")
injectSysConfig("dd.profiling.directallocation.enabled", "true")
}
def "test track memory mapped file"() {
setup:
AtomicLong expectedSpanId = new AtomicLong()
setupRecording()
def file = File.createTempFile(getClass().getName() + "-" + UUID.randomUUID(), ".test")
file.deleteOnExit()
RandomAccessFile raf
when:
raf = new RandomAccessFile(file, "rw")
runUnderTrace("context", {
expectedSpanId.set(AgentTracer.activeSpan().getSpanId())
raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 20)
})
def directAllocations = getDirectAllocations()
then:
directAllocations.size() == 2
def sample = directAllocations.find({ it.getEventType().name.equals("datadog.DirectAllocationSample")})
sample.getLong("allocated") == 20
sample.getString("source") == "MMAP"
sample.getString("allocatingClass") == "org.codehaus.groovy.vmplugin.v8.IndyInterface" // With Groovy 3 it was "org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite"
sample.getLong("spanId") == expectedSpanId.get()
def total = directAllocations.find({ it.getEventType().name.equals("datadog.DirectAllocationTotal")})
total.getLong("allocated") == 20
total.getString("source") == "MMAP"
total.getString("allocatingClass") == "org.codehaus.groovy.vmplugin.v8.IndyInterface" // With Groovy 3 it was "org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite"
cleanup:
recording.close()
raf.close()
}
def "test track direct allocation"() {
when:
setupRecording()
AtomicLong expectedSpanId = new AtomicLong()
runUnderTrace("context", {
expectedSpanId.set(AgentTracer.activeSpan().getSpanId())
ByteBuffer.allocateDirect(10)
})
def directAllocations = getDirectAllocations()
then:
directAllocations.size() == 2
def sample = directAllocations.find({ it.getEventType().name.equals("datadog.DirectAllocationSample")})
sample.getLong("allocated") == 10
sample.getString("source") == "ALLOCATE_DIRECT"
sample.getString("allocatingClass") == "org.codehaus.groovy.vmplugin.v8.IndyInterface" // With Groovy 3 it was "java_nio_ByteBuffer\$allocateDirect"
sample.getLong("spanId") == expectedSpanId.get()
def total = directAllocations.find({ it.getEventType().name.equals("datadog.DirectAllocationTotal")})
total.getLong("allocated") == 10
total.getString("source") == "ALLOCATE_DIRECT"
total.getString("allocatingClass") == "org.codehaus.groovy.vmplugin.v8.IndyInterface"// With Groovy 3 it was "java_nio_ByteBuffer\$allocateDirect"
cleanup:
recording.close()
}
def "test slices and duplicates aren't instrumented"() {
when:
setupRecording()
ByteBuffer buffer = ByteBuffer.allocateDirect(10)
buffer.slice()
buffer.duplicate()
def directAllocations = getDirectAllocations()
then:
directAllocations.size() == 2
def sample = directAllocations.find({ it.getEventType().name.equals("datadog.DirectAllocationSample")})
sample.getLong("allocated") == 10
def total = directAllocations.find({ it.getEventType().name.equals("datadog.DirectAllocationTotal")})
total.getLong("allocated") == 10
cleanup:
recording.close()
}
def getDirectAllocations() {
def snapshot = FlightRecorder.getFlightRecorder().takeSnapshot()
def stream = snapshot.getStream(start, Instant.now())
File output = Files.createTempFile("recording", ".jfr").toFile()
output.deleteOnExit()
stream.transferTo(new FileOutputStream(output))
return RecordingFile.readAllEvents(output.toPath())
.stream()
.filter({ ["datadog.DirectAllocationSample", "datadog.DirectAllocationTotal"].contains(it.getEventType().name)})
.collect(Collectors.toList())
}
def setupRecording() {
recording = new Recording()
recording.enable("datadog.DirectAllocationSample")
recording.enable("datadog.DirectAllocationTotal")
recording.start()
start = Instant.now()
InstrumentationBasedProfiling.enableInstrumentationBasedProfiling()
}
}