Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
162ea2a
prepare traceevent integration
vaind Apr 23, 2023
68bafaa
wip: in-memory tracelog
vaind May 4, 2023
f6657f5
in-memory tracelog support
vaind May 5, 2023
9f8727f
profiler - remove temp dir
vaind May 8, 2023
3833b5f
tracelog streaming
vaind May 8, 2023
ad5e21f
fix sample collection
vaind May 9, 2023
1510734
migrate downsampling & fix tests
vaind May 9, 2023
886a2e9
fix: SparseArray
vaind May 10, 2023
2b70a79
update benchmark & profile builder
vaind May 10, 2023
ba3b4e1
minor changes rundown issue investigation
vaind May 11, 2023
079a72c
Merge branch 'main' into feat/streaming-tracelog
vaind May 18, 2023
25592ba
separate rundown session during tracelog init
vaind May 19, 2023
aa3ff4d
fixes
vaind May 20, 2023
3f7b063
Merge branch 'main' into feat/streaming-tracelog
vaind May 22, 2023
9a780eb
update profiling benchmark results
vaind May 22, 2023
61d2718
Merge branch 'main' into feat/streaming-tracelog
vaind May 22, 2023
cc6119f
update tests
vaind May 23, 2023
9005697
Merge branch 'main' into feat/streaming-tracelog
vaind May 23, 2023
f88a20d
update tests
vaind May 23, 2023
f864e7c
fixes
vaind May 23, 2023
04d38a5
update tests to support streaming tracelog
vaind May 30, 2023
8b3691d
Merge branch 'main' into feat/streaming-tracelog
vaind May 30, 2023
9b51611
Merge branch 'main' into feat/streaming-tracelog
vaind Jun 4, 2023
d171989
Merge branch 'main' into feat/streaming-tracelog
vaind Aug 4, 2023
c7f6929
update to latest perfview
vaind Aug 4, 2023
4ffb45a
submodule perfview sentry fork
bruno-garcia Aug 14, 2023
3a7a10f
Merge branch 'main' into chore/integrate-traceevent
bitsandfoxes Aug 16, 2023
5b159e1
fix test code
vaind Aug 16, 2023
f457e3a
Merge branch 'main' into feat/streaming-tracelog
vaind Aug 16, 2023
abc81db
Merge branch 'feat/streaming-tracelog' into chore/integrate-traceevent
vaind Aug 16, 2023
b4f9485
script & project updates
vaind Aug 16, 2023
5ff8f1a
profiling in core sln
bruno-garcia Aug 19, 2023
0bbb0bd
Merge branch 'chore/integrate-traceevent' into feat/profiling-low-ove…
bruno-garcia Aug 19, 2023
aee692b
perfview sampling
bruno-garcia Aug 20, 2023
3376bea
perfview under profiling
bruno-garcia Aug 20, 2023
6e2ae7c
remove profiling from sln/slnf
bruno-garcia Aug 20, 2023
790d774
internal perfview
bruno-garcia Aug 20, 2023
b346567
ignore build files from submodule perfview
bruno-garcia Aug 20, 2023
6cb6cd1
fix structure
bruno-garcia Aug 20, 2023
19e24a2
resolve errors
bruno-garcia Aug 20, 2023
1c98746
builds!
bruno-garcia Aug 20, 2023
40b6add
in SentryCore sln
bruno-garcia Aug 20, 2023
43f78a0
gitignore on module
bruno-garcia Aug 20, 2023
53715f4
set correct platform to profile
bruno-garcia Aug 20, 2023
67ef890
log on collection failure
bruno-garcia Aug 20, 2023
915a0f2
sample
bruno-garcia Aug 20, 2023
791cdc6
fixed sln file with all but mobile
bruno-garcia Aug 20, 2023
bec4641
ignore obj
bruno-garcia Aug 20, 2023
3a05994
fix benchmarks. Package it
bruno-garcia Aug 20, 2023
f1ea1ec
fix tfm in script
bruno-garcia Aug 20, 2023
7c7404d
verify
bruno-garcia Aug 20, 2023
20286f4
new full sln file
bruno-garcia Aug 20, 2023
aa01035
pack profiling
bruno-garcia Aug 20, 2023
b5cccee
add profile category
bruno-garcia Aug 20, 2023
1a593bc
fuck this slnf mess
bruno-garcia Aug 21, 2023
eaa320b
debugging on sample
bruno-garcia Aug 21, 2023
833a319
Merge branch 'main' into feat/profiling-low-overhead
vaind Aug 21, 2023
6a1df09
timeout update
vaind Aug 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
migrate downsampling & fix tests
  • Loading branch information
vaind committed May 9, 2023
commit 1510734d80b8fd6818c012a96f98f375bca49541
52 changes: 17 additions & 35 deletions src/Sentry.Profiling/Downsampler.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,34 @@
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.EventPipe;

// TODO remove
/// <summary>
/// Reduce sampling rate from 1 Hz that is the default for the provider to the configured SamplingRateMs.
/// </summary>
internal class Downsampler
{
public double SamplingRateMs { get; set; } = (double)1_000 / 101; // 101 Hz
private double NextSampleCounter = 0;
private double NextSampleLow = 0;
private double NextSampleHigh = -1;
private static double _samplingRateMs = (double)1_000 / 101; // 101 Hz
private double _samplingGapMs = _samplingRateMs * 0.9;

public void AttachTo(TraceEventDispatcher source)
// Maps from ThreadIndex to the last sample timestamp for that thread.
private GrowableArray<double> _prevThreadSamples = new(10);

public void NewThreadAdded(int threadIndex)
{
source.AddDispatchHook(DispatchHook);
if (threadIndex >= _prevThreadSamples.Count)
{
_prevThreadSamples.Count = threadIndex + 1;
_prevThreadSamples[threadIndex] = Double.MinValue;
}
}

// Downsamples to the configured SamplingRateMs by keeping a shifting window of where the timestamp must fall.
// Alternatively, we could keep a map of the previous sample for each thread and check that instead but that would
// be a bit less performant (albeit more precise).
private void DispatchHook(TraceEvent traceEvent, Action<TraceEvent> realDispatch)
public bool ShouldSample(int threadIndex, double timestampMs)
{
if (traceEvent.ProviderGuid.Equals(SampleProfilerTraceEventParser.ProviderGuid))
Debug.Assert(threadIndex < _prevThreadSamples.Count, "ThreadIndex too large - you must call NewThreadAdded() if a new thread is added.");
if (_prevThreadSamples[threadIndex] + _samplingRateMs <= timestampMs)
{
var timestampMs = traceEvent.TimeStampRelativeMSec;
// Don't sample until the NextSampleLow is reached.
if (NextSampleLow >= timestampMs)
{
NextSampleHigh = -1;
return; // skip the event
}

// This is the first sample after reaching the lower bound - configure the Upper bound to some reasonable value.
if (NextSampleHigh < 0)
{
NextSampleHigh = timestampMs + 0.9;
}
// After the upper bound is breached, advance the lower bound to the next window we care about.
else if (NextSampleHigh < timestampMs)
{
NextSampleCounter += 1;
NextSampleLow = SamplingRateMs * NextSampleCounter - 0.5;
return; // skip the event
}
_prevThreadSamples[threadIndex] = timestampMs;
return true;
}

// Process the event.
realDispatch(traceEvent);
return false;
}
}
33 changes: 15 additions & 18 deletions src/Sentry.Profiling/SampleProfileBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Sentry.Profiling;
using SentryProfileStackTrace = HashableGrowableArray<int>;

/// <summary>
/// Processes TraceLog to produce a SampleProfile.
/// Build a SampleProfile from TraceEvent data.
/// </summary>
internal class SampleProfileBuilder
{
Expand All @@ -20,6 +20,7 @@ internal class SampleProfileBuilder
// Output profile being built.
public readonly SampleProfile Profile = new();

// TODO reevaluate the use of SparseArray after setting up continous profiling. Dictionary might be better.
// A sparse array that maps from StackSourceFrameIndex to an index in the output Profile.frames.
private readonly SparseScalarArray<int> _frameIndexes = new(-1, 1000);

Expand All @@ -29,6 +30,9 @@ internal class SampleProfileBuilder
// A sparse array mapping from a ThreadIndex to an index in Profile.Threads.
private readonly SparseScalarArray<int> _threadIndexes = new(-1, 10);

// TODO make downsampling conditional once this is available: https://github.com/dotnet/runtime/issues/82939
private readonly Downsampler _downsampler = new();

public SampleProfileBuilder(SentryOptions options, TraceLog traceLog)
{
_options = options;
Expand All @@ -51,14 +55,19 @@ internal void AddSample(TraceEvent data, double timestampMs)
return;
}

var stackIndex = AddStackTrace(callStackIndex);
if (stackIndex < 0)
var threadIndex = AddThread(thread);
if (threadIndex < 0)
{
return;
}

var threadIndex = AddThread(thread);
if (threadIndex < 0)
if (!_downsampler.ShouldSample(threadIndex, timestampMs))
{
return;
}

var stackIndex = AddStackTrace(callStackIndex);
if (stackIndex < 0)
{
return;
}
Expand Down Expand Up @@ -141,24 +150,12 @@ private int AddThread(TraceThread thread)
Name = thread.ThreadInfo ?? $"Thread {thread.ThreadID}",
});
_threadIndexes[key] = Profile.Threads.Count - 1;
_downsampler.NewThreadAdded(_threadIndexes[key]);
}

return _threadIndexes[key];
}

private static string ActivityPath(TraceActivity activity)
{
var creator = activity.Creator;
if (creator is null || creator.IsThreadActivity)
{
return activity.Index.ToString();
}
else
{
return $"{ActivityPath(creator)}/{activity.Index.ToString()}";
}
}

private SentryStackFrame CreateStackFrame(CodeAddressIndex codeAddressIndex)
{
var frame = new SentryStackFrame();
Expand Down
Binary file modified test/Sentry.Profiling.Tests/Resources/sample.etlx
Binary file not shown.
13 changes: 13 additions & 0 deletions test/Sentry.Profiling.Tests/SamplingTransactionProfilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,17 @@ async Task VerifyAsync(HttpRequestMessage message)
}
}
}

[Fact]
public void Downsampler_ShouldSample_Works()
{
var sut = new Downsampler();
sut.NewThreadAdded(0);
Assert.True(sut.ShouldSample(0, 5));
Assert.False(sut.ShouldSample(0, 3));
Assert.False(sut.ShouldSample(0, 6));
Assert.True(sut.ShouldSample(0, 15));
Assert.False(sut.ShouldSample(0, 6));
Assert.False(sut.ShouldSample(0, 16));
}
}
Loading