-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge tag '3.6.1' into port/3.6.1-to-3.7.1
Libplanet 3.6.1
- Loading branch information
Showing
9 changed files
with
168 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System.Diagnostics; | ||
using System.Security.Cryptography; | ||
using System.Threading; | ||
using Bencodex.Types; | ||
using Libplanet.Common; | ||
using Libplanet.Store.Trie; | ||
using LruCacheNet; | ||
using Serilog; | ||
|
||
namespace Libplanet.Store | ||
{ | ||
/// <summary> | ||
/// A class used for internally caching hashed nodes of <see cref="MerkleTrie"/>s. | ||
/// </summary> | ||
public class HashNodeCache | ||
{ | ||
// FIXME: Tuned to 9c mainnet. Should be refactored to accept cache size as an argument. | ||
private const int _cacheSize = 1_048_576; | ||
private const int _reportPeriod = 60_000; | ||
|
||
private LruCache<HashDigest<SHA256>, IValue> _cache; | ||
private Stopwatch _stopwatch; | ||
private int _attempts; | ||
private int _hits; | ||
private object _reportLock; | ||
|
||
internal HashNodeCache() | ||
{ | ||
_cache = new LruCache<HashDigest<SHA256>, IValue>(_cacheSize); | ||
_stopwatch = new Stopwatch(); | ||
_attempts = 0; | ||
_hits = 0; | ||
_reportLock = new object(); | ||
_stopwatch.Start(); | ||
} | ||
|
||
public bool TryGetValue(HashDigest<SHA256> hash, out IValue? value) | ||
{ | ||
lock (_reportLock) | ||
{ | ||
Report(); | ||
} | ||
|
||
Interlocked.Increment(ref _attempts); | ||
if (_cache.TryGetValue(hash, out value)) | ||
{ | ||
Interlocked.Increment(ref _hits); | ||
return true; | ||
} | ||
else | ||
{ | ||
value = null; | ||
return false; | ||
} | ||
} | ||
|
||
public void AddOrUpdate(HashDigest<SHA256> hash, IValue value) | ||
{ | ||
_cache.AddOrUpdate(hash, value); | ||
} | ||
|
||
private void Report() | ||
{ | ||
long period = _stopwatch.ElapsedMilliseconds; | ||
if (period > _reportPeriod) | ||
{ | ||
Log | ||
.ForContext("Source", nameof(HashNodeCache)) | ||
.ForContext("Tag", "Metric") | ||
.ForContext("Subtag", "HashNodeCacheReport") | ||
.Debug( | ||
"Successfully fetched {HitCount} cached values out of last " + | ||
"{AttemptCount} attempts with hitrate of {HitRate} and " + | ||
"{CacheUsed}/{CacheSize} cache in use during last {PeriodMs} ms", | ||
_hits, | ||
_attempts, | ||
(double)_hits / _attempts, | ||
_cache.Count, | ||
_cache.Capacity, | ||
period); | ||
|
||
_stopwatch.Restart(); | ||
Interlocked.Exchange(ref _attempts, 0); | ||
Interlocked.Exchange(ref _hits, 0); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.