Skip to content

P/Invoke TickCount64 for .NET Standard on Windows #578

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

Merged
merged 3 commits into from
May 10, 2024
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
34 changes: 28 additions & 6 deletions BitFaster.Caching.UnitTests/DurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ public void SinceEpoch()
Duration.SinceEpoch().raw.Should().BeCloseTo(Environment.TickCount64, 15);
}
#else
// eps is 1/200 of a second
ulong eps = (ulong)(Stopwatch.Frequency / 200);
Duration.SinceEpoch().raw.Should().BeCloseTo(Stopwatch.GetTimestamp(), eps);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Duration.SinceEpoch().raw.Should().BeCloseTo(Duration.GetTickCount64(), 15);
}
else
{
// eps is 1/200 of a second
ulong eps = (ulong)(Stopwatch.Frequency / 200);
Duration.SinceEpoch().raw.Should().BeCloseTo(Stopwatch.GetTimestamp(), eps);
}
#endif
}

Expand All @@ -53,8 +60,15 @@ public void ToTimeSpan()
new Duration(1000).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10));
}
#else
// for Stopwatch.GetTimestamp() this is number of ticks
new Duration(1 * Stopwatch.Frequency).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10));
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
new Duration(1000).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10));
}
else
{
// for Stopwatch.GetTimestamp() this is number of ticks
new Duration(1 * Stopwatch.Frequency).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10));
}
#endif
}

Expand All @@ -73,8 +87,16 @@ public void FromTimeSpan()
.Should().Be((long)TimeSpan.FromSeconds(1).TotalMilliseconds);
}
#else
Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw
.Should().Be((long)TimeSpan.FromSeconds(1).TotalMilliseconds);
}
else
{
Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw
.Should().Be(Stopwatch.Frequency);
}
#endif
}

Expand Down
15 changes: 6 additions & 9 deletions BitFaster.Caching.UnitTests/Lru/TlruStopwatchPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ public void WhenTtlIsTimeSpanMaxThrow()
[Fact]
public void WhenTtlIsCloseToMaxAllow()
{
double maxTicks = long.MaxValue / 100.0d;
var ttl = TimeSpan.FromTicks((long)maxTicks) - TimeSpan.FromTicks(10);
var ttl = Duration.MaxRepresentable;

new TLruLongTicksPolicy<int, int>(ttl).TimeToLive.Should().BeCloseTo(ttl, TimeSpan.FromTicks(20));
new TLruLongTicksPolicy<int, int>(ttl).TimeToLive.Should().BeCloseTo(ttl, TimeSpan.FromSeconds(1));
}

[Fact]
Expand All @@ -58,9 +57,7 @@ public void CreateItemInitializesTimestampToNow()
{
var item = this.policy.CreateItem(1, 2);

// seconds = ticks / Stopwatch.Frequency
ulong epsilon = (ulong)(TimeSpan.FromMilliseconds(20).TotalSeconds * Stopwatch.Frequency);
item.TickCount.Should().BeCloseTo(Stopwatch.GetTimestamp(), epsilon);
item.TickCount.Should().BeCloseTo(Duration.SinceEpoch().raw, DurationTests.epsilon);
}

[Fact]
Expand Down Expand Up @@ -91,7 +88,7 @@ public async Task UpdateUpdatesTickCount()
public void WhenItemIsExpiredShouldDiscardIsTrue()
{
var item = this.policy.CreateItem(1, 2);
item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy<int, int>.ToTicks(TimeSpan.FromSeconds(11));
item.TickCount = Duration.SinceEpoch().raw - Duration.FromSeconds(11).raw;

this.policy.ShouldDiscard(item).Should().BeTrue();
}
Expand All @@ -100,7 +97,7 @@ public void WhenItemIsExpiredShouldDiscardIsTrue()
public void WhenItemIsNotExpiredShouldDiscardIsFalse()
{
var item = this.policy.CreateItem(1, 2);
item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy<int, int>.ToTicks(TimeSpan.FromSeconds(9));
item.TickCount = Duration.SinceEpoch().raw - Duration.FromSeconds(9).raw;

this.policy.ShouldDiscard(item).Should().BeFalse();
}
Expand Down Expand Up @@ -155,7 +152,7 @@ private LongTickCountLruItem<int, int> CreateItem(bool wasAccessed, bool isExpir

if (isExpired)
{
item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy<int, int>.ToTicks(TimeSpan.FromSeconds(11));
item.TickCount = Duration.SinceEpoch().raw - Duration.FromSeconds(11).raw;
}

return item;
Expand Down
35 changes: 32 additions & 3 deletions BitFaster.Caching/Duration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public readonly struct Duration

#if NETCOREAPP3_0_OR_GREATER
private static readonly bool IsMacOS = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
#else
private static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

[DllImport("kernel32")]
internal static extern long GetTickCount64();
#endif

internal Duration(long raw)
Expand All @@ -52,7 +57,15 @@ public static Duration SinceEpoch()
return new Duration(Environment.TickCount64);
}
#else
return new Duration(Stopwatch.GetTimestamp());
if (IsWindows)
{
return new Duration(GetTickCount64());
}
else
{
// Warning: not currently covered by unit tests
return new Duration(Stopwatch.GetTimestamp());
}
#endif
}

Expand All @@ -73,7 +86,15 @@ public TimeSpan ToTimeSpan()
return TimeSpan.FromMilliseconds(raw);
}
#else
return StopwatchTickConverter.FromTicks(raw);
if (IsWindows)
{
return TimeSpan.FromMilliseconds(raw);
}
else
{
// Warning: not currently covered by unit tests
return StopwatchTickConverter.FromTicks(raw);
}
#endif
}

Expand All @@ -95,7 +116,15 @@ public static Duration FromTimeSpan(TimeSpan timeSpan)
return new Duration((long)timeSpan.TotalMilliseconds);
}
#else
return new Duration(StopwatchTickConverter.ToTicks(timeSpan));
if (IsWindows)
{
return new Duration((long)timeSpan.TotalMilliseconds);
}
else
{
// Warning: not currently covered by unit tests
return new Duration(StopwatchTickConverter.ToTicks(timeSpan));
}
#endif
}

Expand Down
Loading