Skip to content

Commit 1412165

Browse files
authored
introduce new stats item EvictedBytes (#93)
It's incremented on bucket clean up and reports bytes evicted from memory. It should help to understand an amount of extra memory needed for fastcache to serve requests rate on constantly evicted keys. Related PR VictoriaMetrics/VictoriaMetrics#9293
1 parent 2693e48 commit 1412165

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

fastcache.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ type Stats struct {
5656
// MaxBytesSize is the maximum allowed size of the cache in bytes (aka capacity).
5757
MaxBytesSize uint64
5858

59+
// EvictedBytes is the amount of bytes evicted from cache
60+
EvictedBytes uint64
61+
5962
// BigStats contains stats for GetBig/SetBig methods.
6063
BigStats
6164
}
@@ -234,8 +237,9 @@ type bucket struct {
234237
// idx points to chunks for writing the next (k, v) pair.
235238
idx uint64
236239

237-
collisions uint64
238-
corruptions uint64
240+
collisions uint64
241+
corruptions uint64
242+
evictedBytes uint64
239243
}
240244

241245
func (b *bucket) Init(maxBytes uint64) {
@@ -266,6 +270,7 @@ func (b *bucket) Reset() {
266270
atomic.StoreUint64(&b.misses, 0)
267271
atomic.StoreUint64(&b.collisions, 0)
268272
atomic.StoreUint64(&b.corruptions, 0)
273+
atomic.StoreUint64(&b.evictedBytes, 0)
269274
b.mu.Unlock()
270275
}
271276

@@ -303,6 +308,7 @@ func (b *bucket) UpdateStats(s *Stats) {
303308
s.Misses += atomic.LoadUint64(&b.misses)
304309
s.Collisions += atomic.LoadUint64(&b.collisions)
305310
s.Corruptions += atomic.LoadUint64(&b.corruptions)
311+
s.EvictedBytes += atomic.LoadUint64(&b.evictedBytes)
306312

307313
b.mu.RLock()
308314
s.EntriesCount += uint64(len(b.m))
@@ -371,6 +377,7 @@ func (b *bucket) Set(k, v []byte, h uint64) {
371377
b.idx = idxNew
372378
if needClean {
373379
b.cleanLocked()
380+
atomic.AddUint64(&b.evictedBytes, uint64(len(chunks)*chunkSize))
374381
}
375382
b.mu.Unlock()
376383
}

0 commit comments

Comments
 (0)