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

Track the number of allocations made in each span #597

Merged
merged 1 commit into from
Oct 30, 2018

Conversation

dasch
Copy link
Contributor

@dasch dasch commented Oct 24, 2018

Inspired by rails/rails@42fec4b, this change starts capturing the number of allocations made in each span.

I'm not sure how you'd like to integrate this data – do you just want to use a tag, or would you prefer a first-class attribute on Span?

@dasch
Copy link
Contributor Author

dasch commented Oct 24, 2018

The tests are failing because the Span class has too many lines now – can I ignore that error?

@delner
Copy link
Contributor

delner commented Oct 24, 2018

@dasch You can add a # rubocop:disable <rule name> at the top of the offending class. If you search the project, you'll find other examples of similar things.

end
else
def now_allocations
GC.stat :total_allocated_objects
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm digging this feature! But just a couple of general questions:

  1. How accurate/reliable is GC.stat for this? From what I remember playing around with this, it wasn't very deterministic. (Because other threads in the same process can allocate memory in parallel which changes this stat?) However, I can see this is exactly what the Rails guys did, so I presume they did their homework on this. I suppose as long as its in the ballpark and its generally useful even if not perfectly accurate, I'd be willing to give it the benefit of the doubt.
  2. Does this work for Ruby 1.9.3+? (JRuby excepted?) If not we might need to add in some compatibility or short circuit for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. It seems to be good enough to use in Rails, and it's it doesn't have to be 100% precise to be useful in practice, especially if you aggregate.
  2. I've added a compatibility check.

delner
delner previously approved these changes Oct 29, 2018
Copy link
Contributor

@delner delner left a comment

Choose a reason for hiding this comment

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

@dasch This is looking good to me overall. I gave this a test through a sample app, seems to be working fine.

In general I think we want to be careful with what's being sent to the agent, as it can reject some unexpected data, or increase the size of payloads. However the impact here is pretty small, and the traces were coming through okay, so this should be good to merge.

It might be a good idea to consider this a "beta" feature while we experiment with it, as I think we could reasonably expect our support for memory statistics and API for such features grow in the near future, and this might need to evolve and be incorporated into that. Nonetheless this is a great first step in that direction.

Thanks so much for the contribution, looking forward to trying this out more! 👍

@delner delner added this to the 0.17.0 milestone Oct 29, 2018
@delner delner added core Involves Datadog core libraries community Was opened by a community member feature Involves a product feature labels Oct 29, 2018
@delner
Copy link
Contributor

delner commented Oct 29, 2018

@dasch Can you rebase this against 0.17-dev? I'd like to merge and release this tomorrow (Oct 30) with 0.17.0 .

@dasch dasch changed the base branch from master to 0.17-dev October 30, 2018 09:25
@dasch
Copy link
Contributor Author

dasch commented Oct 30, 2018

@delner I've squashed and rebased on 0.17-dev. Not sure how to make this a beta feature, I assume you can figure that out once this has been merged.

@delner
Copy link
Contributor

delner commented Oct 30, 2018

@dasch Yeah, we're just going to mark it as such in release notes. Thank you!

@delner delner merged commit 117e4d6 into DataDog:0.17-dev Oct 30, 2018
@dasch dasch deleted the dasch-allocations branch October 30, 2018 16:36
ivoanjo added a commit that referenced this pull request Feb 20, 2023
**What does this PR do?**:

This PR adds a new profiler public API:
`Datadog::Profiling.allocation_count`.

The public documentation for this API is as follows:

> Returns an ever-increasing counter of the number of allocations
> observed by the profiler in this thread.
>
> Note 1: This counter may not start from zero on new threads. It
> should only be used to measure how many
> allocations have happened between two calls to this API:
> ```ruby
> allocations_before = Datadog::Profiling.allocation_count
> do_some_work()
> allocations_after = Datadog::Profiling.allocation_count
> puts "Allocations during do_some_work: #{allocations_after - allocations_before}"
> ```
> (This is similar to some OS-based time representations.)
>
> Note 2: All fibers in the same thread will share the same counter
> values.
>
> Only available when the profiler is running, the new CPU Profiling
> 2.0 profiler is in use, and allocation-related
> features are not disabled via configuration.
> For instructions on enabling CPU Profiling 2.0 see the ddtrace
> release notes.

As long as CPU Profiling 2.0 is in use, this API is enabled by
default. To disable it, this PR adds a new setting:

```ruby
Datadog.configure do |c|
  c.profiling.advanced.allocation_counting_enabled = # ...
end
```

**Motivation**:

This feature has long been something we want to provide with ddtrace,
see issues #2164 and #468, as well as PRs #1891, #1805, #597

As part of the ongoing work of enabling allocation profiling,
counting the number of allocations comes at a very cheap cost since
the profiler needs to have a `RUBY_INTERNAL_EVENT_NEWOBJ`
tracepoint anyway -- it's just a matter of also incrementing a
counter inside it.

**Additional Notes**:

Note that this does not yet change any user-visible feature for
ddtrace. I'm counting on @marcotc to pick up the task of using this
API to make some tracing magic :)

**How to test the change?**:

This change includes code coverage.

---

Fixes #2164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Was opened by a community member core Involves Datadog core libraries feature Involves a product feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants