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

Refactor exporter - step 4 #1085

Merged
merged 5 commits into from
Aug 16, 2020
Merged

Refactor exporter - step 4 #1085

merged 5 commits into from
Aug 16, 2020

Conversation

reyang
Copy link
Member

@reyang reyang commented Aug 15, 2020

This is a follow up PR of #1078.

Changes

  • Added a multi-writer single-reader circular buffer, which will be used for batch exporting scenario. Key points:
    • The entire buffer is pre-allocated.
    • Consumer doesn't need to copy items to a new array/list, the consumption is happening "in-place".
  • Still need to read more about the CLR/JIT memory model (about the Int64 operation) to see if additional memory barrier should be added.
  • I'll do some initial perf test offline, additional optimization can be done later without having the affect the interface.
  • IEnumerator optimization will be covered separately in Perf improvement using zero-alloc foreach #1080.
  • [Don't merge] Stress test for CircularBuffer #1086 has the stress test code FYI.

For significant contributions please make sure you have completed the following items:

  • Design discussion issue #
  • Changes in public API reviewed

@reyang reyang requested a review from a team August 15, 2020 18:29
@codecov
Copy link

codecov bot commented Aug 15, 2020

Codecov Report

Merging #1085 into master will decrease coverage by 0.42%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1085      +/-   ##
==========================================
- Coverage   76.59%   76.17%   -0.43%     
==========================================
  Files         224      225       +1     
  Lines        6165     6199      +34     
==========================================
  Hits         4722     4722              
- Misses       1443     1477      +34     
Impacted Files Coverage Δ
src/OpenTelemetry/Internal/CircularBuffer.cs 0.00% <0.00%> (ø)


if (this.SwapIfNull(index, value))
{
this.head = headSnapshot + 1;
Copy link
Member

@CodeBlanch CodeBlanch Aug 15, 2020

Choose a reason for hiding this comment

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

I know it's a really really big number, but in theory it could wrap around to negative at some point which would throw overflow exception I think? If we switched to ulong head/tail and did unchecked (this.head = headSnapshot + 1) I think the wrap would be fine given the % this.capacity in the algorithm?

Copy link
Member Author

@reyang reyang Aug 15, 2020

Choose a reason for hiding this comment

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

I guess it is impractical to hit that number? Adding the wrapping support requires more than unchecked, it will touch other part of the lock-free algorithm and put additional requirements on memory barriers. So I would prefer to keep it as-is in favor of perf vs. mathematical correctness.


count = Math.Min(count, this.Count);

for (int i = 0; i < count; i++)

Choose a reason for hiding this comment

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

Do you want to provide an additional check with a boolean and an Interlock to prevent 2 threads from accidently entering if the single reader pattern is accidently broken.


if (this.SwapIfNull(index, value))
{
if (Interlocked.CompareExchange(ref this.head, headSnapshot + 1, headSnapshot) == headSnapshot)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is headSnapshot validation needed, or Interloaded.Add can work here?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's is required, otherwise it'll break the 🧙‍♀️.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, yes, it is required because the item pointed by headSnapshot maybe not in the range of [this.tail, this.head).

Copy link
Member

@CodeBlanch CodeBlanch left a comment

Choose a reason for hiding this comment

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

LGTM

@CodeBlanch CodeBlanch merged commit 90c370f into master Aug 16, 2020
@CodeBlanch CodeBlanch deleted the reyang/buffer branch August 16, 2020 05:13
@ThomsonTan
Copy link
Contributor

As the name implied it is a circular buffer, should it TryAdd overwrite the oldest item when the buffer is full instead of returning false on the most recent item?

throw new ArgumentNullException(nameof(value));
}

while (true)
Copy link
Contributor

Choose a reason for hiding this comment

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

Retry here introduces uncertainty of running time of TryAdd, perhaps return false instead of retry in the while(true) loop, or limit retry count?

Copy link
Member Author

Choose a reason for hiding this comment

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

The intention of this function is to return false only when queue is full.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ThomsonTan you've actually worried me! 😅 Here goes a PR #1088 to address it.

private long head = 0;
private long tail = 0;

public CircularBuffer(int capacity)
Copy link
Contributor

Choose a reason for hiding this comment

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

Restrict capacity to be power of 2, so the modulo operation % could replaced with a simple bitwise and?

Copy link
Member Author

Choose a reason for hiding this comment

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

How much (X%) perf improvement would you expect from modulo?

Copy link
Contributor

Choose a reason for hiding this comment

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

div could take tens times of CPU cycles than bitwise arithmetic instruction, but there is also trade off taking more memory (for ceiling(2**size)). This could be a low priority optimization and needs data to prove.

{
if (count <= 0)
{
yield break;
Copy link
Contributor

Choose a reason for hiding this comment

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

When the queue is empty, wait for notification of new items instead of polling?

Copy link
Member Author

Choose a reason for hiding this comment

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

That would make it a normal lock based queue instead of lock-free.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants