Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0 #1

Merged
merged 14 commits into from
Sep 22, 2016
Merged

v0 #1

merged 14 commits into from
Sep 22, 2016

Conversation

clutchski
Copy link
Contributor

@clutchski clutchski commented Aug 25, 2016

first pass at a ruby client.

  • very similar to the python client, but an attempt at (readable) idiomatic ruby
  • woefully incomplete (no buffer, no threads, no tags, etc) but sends working traces to prod
  • works well enough (i think) to try instrumenting rails, sinatra or something similar.



tracer = Datadog::Tracer.new()
while true do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loop do is more idiomatic

@clutchski clutchski merged commit 2c36f61 into master Sep 22, 2016
@palazzem palazzem deleted the matt branch September 28, 2016 15:47
@palazzem palazzem added the core Involves Datadog core libraries label Oct 5, 2016
marcotc pushed a commit that referenced this pull request Oct 1, 2019
ivoanjo added a commit that referenced this pull request Oct 15, 2021
…s String

I noticed a Ruby app where the configuration mismatch warning was
popping up:

```
W, [2021-10-15T11:44:30.952540 #1]  WARN -- ddtrace: [ddtrace]
Configuration mismatch: values differ between "c.tracer.port" ('8126')
and DD_TRACE_AGENT_PORT environment variable ('8126'). Using '8126'.
```

The warning was being triggered because we convert the values read from
environment variables (`DD_TRACE_AGENT_PORT` or `DD_TRACE_AGENT_URL`)
into integers, but this app had `c.tracer.port` specified as a
string.

Because or that, since we collect all values, call `#uniq` on them
and see if we get more than one, the warning was being triggered
since the `8126` integer is not the same as the `'8126'` string.
ivoanjo added a commit that referenced this pull request Mar 20, 2023
The funny thing about this one is that this one is caused by
`#pragma GCC diagnostic ignored "-Wexpansion-to-defined"` on GCC
versions that don't the above warning.

So as far as I understood it, this is what happened:
1. On a modern GCC, we had a warning caused by the Ruby headers (the
   "expansion-to-defined" warning). We couldn't do anything about it,
   so we ignored it.
2. On an older GCC, the warning from #1 is not known, so it emits
   a different warning: a warning that it doesn't know
   "expansion-to-defined".
3. So we now need to ignore the unknown pragma warning...
   ... lol computers.
ivoanjo added a commit that referenced this pull request Dec 2, 2024
**What does this PR do?**

This PR raises the minimum Ruby version required for heap profiling from
the previous value of >= 2.7 to >= 3.1 due to a new VM bug discovered
(see below for details).

It's mostly a revert of #3366, where we had first tried to workaround
a Ruby 2.7/3.0 bug, but it turns out we missed a spot, and we
could trigger VM crashes because of that.

**Motivation:**

Ruby versions prior to 3.1 had a special optimization called
`rb_gc_force_recycle` which would allow objects to directly be
garbage collected (e.g. without needing to wait for the GC).

It turns out that `rb_gc_force_recycle` did not play well with the
changes in Ruby 2.7 to how object ids worked. We uncovered this earlier
on during the development of the heap profiler, and put in a workaround
for the bug that we thought was enough...

Unfortunately, it turns out that the workaround is not enough. The
following reproducer, when run on Ruby 2.7 or 3.0 shows how the Ruby VM
can segfault inside `id2ref` due to the issue above:

```ruby
puts RUBY_DESCRIPTION

require "datadog"
require "objspace"
require "pry"

NUM_OBJECTS = 10_000_000

recycled_ids = Array.new(NUM_OBJECTS) { 123 }
many_objects = Array.new(NUM_OBJECTS) { Object.new }

(0...NUM_OBJECTS).each do |i|
  recycled_ids[i] = many_objects[i].object_id
end

puts "Seeded objects!"
gets

(0...NUM_OBJECTS).each do |i|
  Datadog::Profiling::StackRecorder::Testing._native_gc_force_recycle(many_objects[i])
  many_objects[i] = nil
end

puts GC.stat

puts "Recycled objects!"
gets

many_objects = nil

10.times { GC.start }
Array.new(10_000) { Object.new }
10.times { GC.start }

puts GC.stat

puts "GC'd objects! (Ruby should have released pages?)"
gets

recycled_ids.each { |i|
  begin
    (nil == ObjectSpace._id2ref(i))
  rescue
    nil
  end
}
puts "Done!"
```

Crash details:

```
Program received signal SIGSEGV, Segmentation fault.
is_swept_object (ptr=93825033355200, objspace=<optimised out>) at gc.c:3868
3868	    return page->flags.before_sweep ? FALSE : TRUE;
(gdb) bt
 #0  is_swept_object (ptr=93825033355200, objspace=<optimised out>) at gc.c:3868
 #1  is_garbage_object (objspace=0x55555555d220, objspace=0x55555555d220, ptr=93825033355200) at gc.c:3887
 #2  is_live_object (ptr=93825033355200, objspace=0x55555555d220) at gc.c:3909
 #3  is_live_object (ptr=93825033355200, objspace=0x55555555d220) at gc.c:3898
 #4  id2ref (objid=8264881) at gc.c:3999
 #5  os_id2ref (os=<optimised out>, objid=<optimised out>) at gc.c:4019
```

This crash happens because of two things:

1. Ruby does not clean the object id entry for a recycled object
   from its internal hash map
2. If the memory page where the object lived is returned back to the
   OS, trying to `id2ref` on that id will cause Ruby to try to read
   invalid memory and crash.

**Additional Notes:**

I've chosen to disable heap profiling on 2.7 and 3.0 because
I can't think of a good workaround for the bug above, especially
not one that does not increase the overhead of heap profiling.

**How to test the change?**

This PR updates the test coverage to expect Ruby 3.1+ as the
minimum for the feature.

You can also quickly validate it doesn't get enabled on the older
Rubies using:

```
$ DD_PROFILING_ENABLED=true DD_PROFILING_EXPERIMENTAL_HEAP_ENABLED=true bundle exec ddprofrb exec ruby -e "puts RUBY_DESCRIPTION"
W, [2024-12-02T10:42:28.771611 #112585]  WARN -- datadog: [datadog] Current Ruby version
(3.0.5) cannot support heap profiling due to VM bugs/limitations. Please upgrade to Ruby
>= 3.1 in order to use this feature. Heap profiling has been disabled.
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Involves Datadog core libraries
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants