Skip to content

Commit

Permalink
Reduce memory usage by reclaimed MetricPoint (#5416)
Browse files Browse the repository at this point in the history
  • Loading branch information
specialforest authored Mar 6, 2024
1 parent a7f3400 commit bae8a6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 1 addition & 3 deletions src/OpenTelemetry/Metrics/AggregatorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ internal void SnapshotDeltaWithMetricPointReclaim()
{
var lookupData = metricPoint.LookupData;

// Setting `LookupData` to `null` to denote that this MetricPoint is reclaimed.
// Snapshot method can use this to skip trying to reclaim indices which have already been reclaimed and added to the queue.
metricPoint.LookupData = null;
metricPoint.Reclaim();

Debug.Assert(this.TagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");

Expand Down
20 changes: 15 additions & 5 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public struct MetricPoint
// ReferenceCount doesn't matter for MetricPoint with no tags and overflow attribute as they are never reclaimed.
internal int ReferenceCount;

// When the AggregatorStore is reclaiming MetricPoints, this serves the purpose of validating the a given thread is using the right
// MetricPoint for update by checking it against what as added in the Dictionary. Also, when a thread finds out that the MetricPoint
// that its using is already reclaimed, this helps avoid sorting of the tags for adding a new Dictionary entry.
internal LookupData? LookupData;

private const int DefaultSimpleReservoirPoolSize = 1;

private readonly AggregatorStore aggregatorStore;
Expand Down Expand Up @@ -155,6 +150,12 @@ internal MetricPointStatus MetricPointStatus
private set;
}

// When the AggregatorStore is reclaiming MetricPoints, this serves the purpose of validating the a given thread is using the right
// MetricPoint for update by checking it against what as added in the Dictionary. Also, when a thread finds out that the MetricPoint
// that its using is already reclaimed, this helps avoid sorting of the tags for adding a new Dictionary entry.
// Snapshot method can use this to skip trying to reclaim indices which have already been reclaimed and added to the queue.
internal LookupData? LookupData { readonly get; private set; }

internal readonly bool IsInitialized => this.aggregatorStore != null;

/// <summary>
Expand Down Expand Up @@ -1195,6 +1196,15 @@ internal void TakeSnapshotWithExemplar(bool outputDelta)
}
}

/// <summary>
/// Denote that this MetricPoint is reclaimed.
/// </summary>
internal void Reclaim()
{
this.LookupData = null;
this.mpComponents = null;
}

private void UpdateHistogram(double number, ReadOnlySpan<KeyValuePair<string, object?>> tags = default, bool isSampled = false)
{
Debug.Assert(this.mpComponents?.HistogramBuckets != null, "HistogramBuckets was null");
Expand Down

0 comments on commit bae8a6b

Please sign in to comment.