Skip to content

Increased CPU and memory overhead while profiling #3199

Closed

Description

@InterstellarStella

Package

Sentry.AspNetCore

.NET Flavor

.NET

.NET Version

.Net 7 / .Net 8

OS

Any (not platform specific)

SDK Version

4.1.2

Self-Hosted Sentry Version

No response

Steps to Reproduce

Enable profiling through the following setup:

SentrySdk.Init(options =>
{
    options.Dsn = sentryConfig.Dsn;
    options.TracesSampleRate = 0.1;
    options.ProfilesSampleRate = 0.1;
    options.Environment = sentryConfig.Environment;
    options.DiagnosticLevel = diagnosticLevel;
    options.AddIntegration(new ProfilingIntegration( TimeSpan.FromMilliseconds(500)));
});

Expected Result

The CPU overhead is about +5 to 10%.

Actual Result

The CPU overhead is around around +70%, which is seen through google cloud monitoring system.
The following Sentry packages are used:
Sentry 4.1.2
Sentry.AspNetCore 4.1.2
Sentry.Profiling 4.1.2

Slack conversation
Zendesk ticket

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Mar 6, 2024
  2. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Mar 6, 2024
  3. bruno-garcia commented on Mar 6, 2024

    @bruno-garcia
    Member

    cc @vaind

    Could we have some details about what the app is doing? With a repro we can look exactly what's going on and make improvements but with the currently provided information it's hard for us to figure out where the problem is.

    Is this a web server? How many requests in parallel are running?

    If this has high thrughput, changing sample rate to:

        options.TracesSampleRate = 0.001;
        options.ProfilesSampleRate = 0.001;
    

    Does that reduce the CPU overhead considerably?

  4. saschash commented on Mar 7, 2024

    @saschash

    @bruno-garcia I tried to put sample rates to 0.001 and there is still quite a significant spike (from 7 % CPU -> 40 % CPU)
    In fact, it was about the same as with sampling rate 1.0, which is weird...
    The behaviour is, it first jumps to 60% and then stabilises at 40%.
    It is a web server, but in sandbox environment, and we are running 20 workers in parallel

    I have mentioned before that we are using hang fire to queue background jobs and I have noticed call like that: Sentry.AspNetCore.SentryTracingMiddleware.InvokeAsync(HttpContext context). But when it is a hangfire background job HttpContext will be null, so that might somehow explain what is going on

  5. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Mar 7, 2024
  6. bruno-garcia commented on Mar 10, 2024

    @bruno-garcia
    Member

    I tried to put sample rates to 0.001 and there is still quite a significant spike (from 7 % CPU -> 40 % CPU)
    In fact, it was about the same as with sampling rate 1.0, which is weird...

    This didn't come up during our tests so I'm a bit surprised but I wonder if it might be the overhead of the .NET profiler itself which unfortunately has a high frequency and we down sample after the fact.

    I have mentioned before that we are using hang fire to queue background jobs and I have noticed call like that: Sentry.AspNetCore.SentryTracingMiddleware.InvokeAsync(HttpContext context). But when it is a hangfire background job HttpContext will be null, so that might somehow explain what is going on

    I wonder if this is to get the transaction name automatically.

    Do you get the overhead without Profiling turned on? Just keep transactions (TracesSampleRate set to some value)

    By any chance in the profiles in Sentry, do you see any frames from Sentry that seem to be taking significant part time?

  7. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Mar 10, 2024
  8. saschash commented on Mar 11, 2024

    @saschash

    Thanks @bruno-garcia. Without profiling it works like a charm without any noticable overhead.
    I could not find anything from Sentry taking a lot of time, in fact I couldn't see any sentry frames at all - which is weird, right, I should be able to see sentry-related process there...

  9. 60 remaining items

  10. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jun 5, 2024
  11. haneytron commented on Jun 6, 2024

    @haneytron

    @vaind ran a trace for almost 3 hours, ending just now:

    root@b4ee333c5f1c:~/site/wwwroot# dotnet-trace collect --profile cpu-sampling -p 106 -o /dev/null
    
    Provider Name                           Keywords            Level               Enabled By
    Microsoft-DotNETCore-SampleProfiler     0x0000F00000000000  Informational(4)    --profile 
    Microsoft-Windows-DotNETRuntime         0x00000014C14FCCBD  Informational(4)    --profile 
    
    Process        : /usr/share/dotnet/dotnet
    Output File    : /dev/null
    [00:02:50:37]   Recording trace 0.00     (B)
    Press <Enter> or <Ctrl+C> to exit...     (B)
    Stopping the trace. This may take several minutes depending on the application being traced.
    
    Trace completed.
    

    (output to dev/null to avoid file storage issues and I/O penalties)

    Result in CPU was high, but also profiling @ 100%:

    image

    Response times remained good (the longer ones are incorrect measuring on long-lived WebSockets SignalR conns):

    image

    No 5xx or 499 thrown during this period:

    image

    Average memory working set was constant w/ profiler active (no 2x jump like with the Sentry profiler):

    image

    Console / App / HTTP logs are clean. No app service restarts since I turned off the Sentry profiler yesterday afternoon.

    I suspect SignalR / WebSockets issues w/ the Sentry profiler remain.

  12. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jun 6, 2024
  13. vaind commented on Jun 7, 2024

    @vaind
    Contributor

    I've identified at least one memory leak in perfview EventPipe processing where it calls GC.KeepAlive() on objects so they never get freed. However, I can still see the memory growing, although not on the managed side so there may be something else going on as well.

  14. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jun 7, 2024
  15. vaind commented on Jun 27, 2024

    @vaind
    Contributor

    TLDR: not resolved yet

    I'll post a PR decreasing the circular buffer we use when setting up a diagnostic session but other than that, the main problem seems to be with object pinning in perfview and changing that would require changing quite a few things. In short, EventPipeScource doesn't copy incoming buffer but instead maps structs (EventPipeEventHeader) directly onto the buffer (unsafe but OK if pinned). To amplify the problem, parts of these structs are then referenced directly (as pointers) and it's not clear to me whether objects containing these copied pointers would live outside the event handles (it seems like they would). I'm not sure about the scope of the changes needed to make this work without pinning (or without a pinning handle leak while still staying safe), but I have a feeling it would require a lot of effort.

  16. added
    Bug
    on Jul 10, 2024
  17. vaind commented on Jul 24, 2024

    @vaind
    Contributor

    FYI, I've raised an issue over at dotnet/runtime#105132 for the unmanaged memory growth

  18. bruno-garcia commented on Oct 1, 2024

    @bruno-garcia
    Member

    @filipnavara has made a fix related to a leak.

    I've updated our fork to include the fix. We'll bump the submodule here and make a release.

  19. vaind commented on Oct 2, 2024

    @vaind
    Contributor

    according to my manual testing (running a sample app from #3645 for hours), the memory leak seem to be resolved by #3492

  20. bruno-garcia commented on Oct 3, 2024

    @bruno-garcia
    Member

    Woo! Thank you @vaind and @filipnavara

    I'm closing this issue as resolved.

    If you have any issues with our profiling offer, please raise a ticket. We'd like to make sure this is working properly to everyone!

  21. locked as resolved and limited conversation to collaborators on Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugOptimizationProduct: Profiling

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions