Skip to content

Track the number of allocations made in each span#597

Merged
delner merged 1 commit into
DataDog:0.17-devfrom
dasch:dasch-allocations
Oct 30, 2018
Merged

Track the number of allocations made in each span#597
delner merged 1 commit into
DataDog:0.17-devfrom
dasch:dasch-allocations

Conversation

@dasch

@dasch dasch commented Oct 24, 2018

Copy link
Copy Markdown
Contributor

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
dasch force-pushed the dasch-allocations branch from e86f0e8 to da8c8c0 Compare October 24, 2018 08:56
@dasch

dasch commented Oct 24, 2018

Copy link
Copy Markdown
Contributor Author

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

@delner

delner commented Oct 24, 2018

Copy link
Copy Markdown
Contributor

@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.

Comment thread lib/ddtrace/span.rb Outdated

Copy link
Copy Markdown
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
Copy Markdown
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.

@dasch
dasch force-pushed the dasch-allocations branch from cdc1ad2 to 7dfcc99 Compare October 25, 2018 13:58
delner
delner previously approved these changes Oct 29, 2018

@delner delner left a comment

Copy link
Copy Markdown
Contributor

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

delner commented Oct 29, 2018

Copy link
Copy Markdown
Contributor

@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 force-pushed the dasch-allocations branch from 4ee1e94 to b8f7d9d Compare October 30, 2018 09:25
@dasch
dasch changed the base branch from master to 0.17-dev October 30, 2018 09:25
@dasch

dasch commented Oct 30, 2018

Copy link
Copy Markdown
Contributor Author

@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

delner commented Oct 30, 2018

Copy link
Copy Markdown
Contributor

@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.

2 participants