Skip to content

Commit e651ea2

Browse files
authored
DEBUG-2334 enable dynamic instrumentation line probe benchmarks (#4122)
1 parent d75902e commit e651ea2

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

benchmarks/di_instrument.rb

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,24 @@ def test_method_for_line_probe
6969
end
7070
end
7171

72-
def run_benchmark
72+
attr_reader :instrumenter
73+
74+
def logger
75+
@logger ||= Logger.new(STDERR)
76+
end
77+
78+
def configure
7379
settings = Datadog.configuration
74-
# We benchmark untargeted and targeted trace points; untargeted ones
75-
# are prohibited by default, permit them.
76-
begin
77-
settings.dynamic_instrumentation.internal.untargeted_trace_points = true
78-
rescue NoMethodError
79-
settings.dynamic_instrumentation.untargeted_trace_points = true
80-
end
80+
yield settings if block_given?
81+
8182
redactor = Datadog::DI::Redactor.new(settings)
8283
serializer = Datadog::DI::Serializer.new(settings, redactor)
83-
logger = Logger.new(STDERR)
84-
instrumenter = Datadog::DI::Instrumenter.new(settings, serializer, logger)
84+
@instrumenter = Datadog::DI::Instrumenter.new(settings, serializer, logger,
85+
code_tracker: Datadog::DI.code_tracker)
86+
end
87+
88+
def run_benchmark
89+
configure
8590

8691
m = Target.instance_method(:test_method_for_line_probe)
8792
file, line = m.source_location
@@ -103,9 +108,12 @@ def run_benchmark
103108
calls = 0
104109
probe = Datadog::DI::Probe.new(id: 1, type: :log,
105110
type_name: 'DIInstrumentBenchmark::Target', method_name: 'test_method')
106-
instrumenter.hook_method(probe) do
111+
rv = instrumenter.hook_method(probe) do
107112
calls += 1
108113
end
114+
unless rv
115+
raise "Method probe was not successfully installed"
116+
end
109117

110118
Benchmark.ips do |x|
111119
benchmark_time = VALIDATE_BENCHMARK_MODE ? { time: 0.01, warmup: 0 } : { time: 10, warmup: 2 }
@@ -131,21 +139,31 @@ def run_benchmark
131139

132140
instrumenter.unhook(probe)
133141

134-
=begin Line probes require more of DI code to be merged
142+
# We benchmark untargeted and targeted trace points; untargeted ones
143+
# are prohibited by default, permit them.
144+
# In order to install untargeted trace point, we currently need to
145+
# disable code tracking.
146+
Datadog::DI.deactivate_tracking!
147+
configure do |c|
148+
c.dynamic_instrumentation.internal.untargeted_trace_points = true
149+
end
150+
135151
calls = 0
136152
probe = Datadog::DI::Probe.new(id: 1, type: :log,
137153
file: file, line_no: line + 1)
138-
instrumenter.hook_line(probe) do
154+
rv = instrumenter.hook_line(probe) do
139155
calls += 1
140156
end
157+
unless rv
158+
raise "Line probe (in method) was not successfully installed"
159+
end
141160

142161
Benchmark.ips do |x|
143162
benchmark_time = VALIDATE_BENCHMARK_MODE ? { time: 0.01, warmup: 0 } : { time: 10, warmup: 2 }
144163
x.config(
145164
**benchmark_time,
146165
)
147-
148-
x.report('line instrumentation') do
166+
x.report('line instrumentation - untargeted') do
149167
Target.new.test_method_for_line_probe
150168
end
151169

@@ -161,7 +179,13 @@ def run_benchmark
161179
raise "Expected at least 1000 calls to the method, got #{calls}"
162180
end
163181

182+
instrumenter.unhook(probe)
183+
164184
Datadog::DI.activate_tracking!
185+
configure do |c|
186+
c.dynamic_instrumentation.internal.untargeted_trace_points = false
187+
end
188+
165189
if defined?(DITarget)
166190
raise "DITarget is already defined, this should not happen"
167191
end
@@ -173,13 +197,15 @@ def run_benchmark
173197
m = DITarget.instance_method(:test_method_for_line_probe)
174198
targeted_file, targeted_line = m.source_location
175199

176-
instrumenter.unhook(probe)
177200
calls = 0
178201
probe = Datadog::DI::Probe.new(id: 1, type: :log,
179202
file: targeted_file, line_no: targeted_line + 1)
180-
instrumenter.hook_line(probe) do
203+
rv = instrumenter.hook_line(probe) do
181204
calls += 1
182205
end
206+
unless rv
207+
raise "Line probe (targeted) was not successfully installed"
208+
end
183209

184210
Benchmark.ips do |x|
185211
benchmark_time = VALIDATE_BENCHMARK_MODE ? { time: 0.01, warmup: 0 } : { time: 10, warmup: 2 }
@@ -207,7 +233,6 @@ def run_benchmark
207233
# target code is approximately what it was prior to hook installation.
208234

209235
instrumenter.unhook(probe)
210-
=end
211236

212237
calls = 0
213238

lib/datadog/di/instrumenter.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ def hook_line(probe, &block)
266266
# If trace point is not targeted, we must verify that the invocation
267267
# is the file & line that we want, because untargeted trace points
268268
# are invoked for *each* line of Ruby executed.
269-
if iseq || tp.lineno == probe.line_no && probe.file_matches?(tp.path)
269+
# TODO find out exactly when the path in trace point is relative.
270+
# Looks like this is the case when line trace point is not targeted?
271+
if iseq || tp.lineno == probe.line_no && (
272+
probe.file == tp.path || probe.file_matches?(tp.path)
273+
)
270274
if rate_limiter.nil? || rate_limiter.allow?
271275
# & is to stop steep complaints, block is always present here.
272276
block&.call(probe: probe, trace_point: tp, caller_locations: caller_locations)
@@ -305,7 +309,9 @@ def hook_line(probe, &block)
305309
else
306310
tp.enable
307311
end
312+
# TracePoint#enable returns false when it succeeds.
308313
end
314+
true
309315
end
310316

311317
def unhook_line(probe)

spec/datadog/di/validate_benchmarks_spec.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
require "datadog/di/spec_helper"
22

3-
# rubocop:disable Style/BlockComments
4-
5-
=begin benchmarks require DI code to be merged
63
RSpec.describe "Dynamic instrumentation benchmarks", :memcheck_valgrind_skip do
74
di_test
85

@@ -29,6 +26,3 @@
2926
expect(benchmarks_to_validate).to contain_exactly(*all_benchmarks)
3027
end
3128
end
32-
=end
33-
34-
# rubocop:enable Style/BlockComments

0 commit comments

Comments
 (0)