Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
using System.Threading.Tasks;
using BitFaster.Caching.Concurrent;
using BitFaster.Caching.Counters;
using FluentAssertions;
using Xunit;

namespace BitFaster.Caching.UnitTests.Concurrent
namespace BitFaster.Caching.UnitTests.Counters
{
public class StripedLongAdderTests
{
[Fact]
public void InitialValueIsZero()
{
new LongAdder().Sum().Should().Be(0);
new Counter().Count().Should().Be(0);
}

[Fact]
public void WhenIncrementedOneIsAdded()
{
var adder = new LongAdder();
var adder = new Counter();

adder.Increment();

adder.Sum().Should().Be(1);
adder.Count().Should().Be(1);
}

[Fact]
public async Task WhenAddingConcurrentlySumIsCorrect()
{
var adder = new LongAdder();
var adder = new Counter();

await Threaded.Run(4, () =>
{
Expand All @@ -36,7 +36,7 @@ await Threaded.Run(4, () =>
}
});

adder.Sum().Should().Be(400000);
adder.Count().Should().Be(400000);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*
* See
* http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/share/classes/java/util/concurrent/atomic/LongAdder.java
*/

namespace BitFaster.Caching.Concurrent
namespace BitFaster.Caching.Counters
{
/// <summary>
/// A thread-safe counter suitable for high throuhgput counting across many concurrent threads.
/// </summary>
public sealed class LongAdder : Striped64
public sealed class Counter : Striped64
{
/// <summary>
/// Creates a new LongAdder with an intial sum of zero.
/// Creates a new Counter with an intial sum of zero.
/// </summary>
public LongAdder() { }
public Counter() { }

/// <summary>
/// Computes the current sum.
/// Computes the current count.
/// </summary>
/// <returns>The current sum.</returns>
public long Sum()
public long Count()
{
var @as = this.Cells; Cell a;
var sum = @base.VolatileRead();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Runtime.InteropServices;
using System.Threading;

namespace BitFaster.Caching.Concurrent
namespace BitFaster.Caching.Counters
{
/// <summary>
/// A long value padded by the size of a CPU cache line to mitigate false sharing.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.ConstrainedExecution;
using System.Threading;

/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*
* See
* http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java
*/

namespace BitFaster.Caching.Concurrent
namespace BitFaster.Caching.Counters
{
/*
* This class maintains a lazily-initialized table of atomically
Expand Down
8 changes: 4 additions & 4 deletions BitFaster.Caching/Lfu/ConcurrentLfu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using BitFaster.Caching.Buffers;
using BitFaster.Caching.Concurrent;
using BitFaster.Caching.Counters;
using BitFaster.Caching.Lru;
using BitFaster.Caching.Scheduler;

Expand Down Expand Up @@ -765,17 +765,17 @@ internal string Format()
internal class CacheMetrics : ICacheMetrics
{
public long requestHitCount;
public LongAdder requestMissCount = new LongAdder();
public Counter requestMissCount = new Counter();
public long updatedCount;
public long evictedCount;

public double HitRatio => (double)requestHitCount / (double)Total;

public long Total => requestHitCount + requestMissCount.Sum();
public long Total => requestHitCount + requestMissCount.Count();

public long Hits => requestHitCount;

public long Misses => requestMissCount.Sum();
public long Misses => requestMissCount.Count();

public long Updated => updatedCount;

Expand Down
28 changes: 14 additions & 14 deletions BitFaster.Caching/Lru/TelemetryPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Diagnostics;
using BitFaster.Caching.Concurrent;
using BitFaster.Caching.Counters;

namespace BitFaster.Caching.Lru
{
Expand All @@ -12,10 +12,10 @@ namespace BitFaster.Caching.Lru
[DebuggerDisplay("Hit = {Hits}, Miss = {Misses}, Upd = {Updated}, Evict = {Evicted}")]
public struct TelemetryPolicy<K, V> : ITelemetryPolicy<K, V>
{
private LongAdder hitCount;
private LongAdder missCount;
private LongAdder evictedCount;
private LongAdder updatedCount;
private Counter hitCount;
private Counter missCount;
private Counter evictedCount;
private Counter updatedCount;
private object eventSource;

///<inheritdoc/>
Expand All @@ -25,19 +25,19 @@ public struct TelemetryPolicy<K, V> : ITelemetryPolicy<K, V>
public double HitRatio => Total == 0 ? 0 : (double)Hits / (double)Total;

///<inheritdoc/>
public long Total => this.hitCount.Sum() + this.missCount.Sum();
public long Total => this.hitCount.Count() + this.missCount.Count();

///<inheritdoc/>
public long Hits => this.hitCount.Sum();
public long Hits => this.hitCount.Count();

///<inheritdoc/>
public long Misses => this.missCount.Sum();
public long Misses => this.missCount.Count();

///<inheritdoc/>
public long Evicted => this.evictedCount.Sum();
public long Evicted => this.evictedCount.Count();

///<inheritdoc/>
public long Updated => this.updatedCount.Sum();
public long Updated => this.updatedCount.Count();

///<inheritdoc/>
public void IncrementMiss()
Expand Down Expand Up @@ -72,10 +72,10 @@ public void OnItemUpdated(K key, V value)
///<inheritdoc/>
public void SetEventSource(object source)
{
this.hitCount = new LongAdder();
this.missCount = new LongAdder();
this.evictedCount = new LongAdder();
this.updatedCount = new LongAdder();
this.hitCount = new Counter();
this.missCount = new Counter();
this.evictedCount = new Counter();
this.updatedCount = new Counter();
this.eventSource = source;
}
}
Expand Down