|
| 1 | +package fido |
| 2 | + |
| 3 | +import "unsafe" |
| 4 | + |
| 5 | +// MemoryStats describes retained memory, including expired entries and entries |
| 6 | +// awaiting eviction. Counts and bytes are estimates during concurrent updates. |
| 7 | +// Bytes excludes allocator rounding, goroutine stacks, in-flight loads, and the |
| 8 | +// internal allocations of xsync maps and locks. It is not process RSS. |
| 9 | +type MemoryStats struct { |
| 10 | + Entries int |
| 11 | + Capacity int |
| 12 | + PendingEntries int |
| 13 | + RetainedEntries int |
| 14 | + StructuralBytes uint64 |
| 15 | + PayloadBytes uint64 |
| 16 | +} |
| 17 | + |
| 18 | +// Bytes returns the estimated bytes accounted for by this snapshot. |
| 19 | +func (s MemoryStats) Bytes() uint64 { return s.StructuralBytes + s.PayloadBytes } |
| 20 | + |
| 21 | +// MemoryStats samples the cache without waiting for its writer lock. False |
| 22 | +// means the cache is busy; retain the previous sample and retry later. With a |
| 23 | +// non-nil sizeOf it takes O(N) time and should run periodically, not per request. |
| 24 | +// sizeOf reports referenced key/value storage beyond their inline Go headers; |
| 25 | +// for string keys and []byte values this is len(key)+cap(value). It must be fast, |
| 26 | +// must not mutate the cache or its values. Shared backing storage |
| 27 | +// can be counted multiple times. A nil sizeOf reports only structural bytes. |
| 28 | +func (c *Cache[K, V]) MemoryStats(sizeOf func(K, V) uint64) (MemoryStats, bool) { |
| 29 | + return c.memory.memoryStats(sizeOf) |
| 30 | +} |
| 31 | + |
| 32 | +// MemoryStats samples only the memory tier. Persistent storage is owned by |
| 33 | +// Store and is deliberately not scanned or contacted by this method. |
| 34 | +func (c *TieredCache[K, V]) MemoryStats(sizeOf func(K, V) uint64) (MemoryStats, bool) { |
| 35 | + return c.memory.memoryStats(sizeOf) |
| 36 | +} |
| 37 | + |
| 38 | +func (c *s3fifo[K, V]) memoryStats(sizeOf func(K, V) uint64) (MemoryStats, bool) { |
| 39 | + var s MemoryStats |
| 40 | + var recycledKey K |
| 41 | + var recycledValue V |
| 42 | + var recycled bool |
| 43 | + // Only queue metadata needs the writer lock. Traverse the concurrent map |
| 44 | + // and call user code after releasing it, like Range; diagnostics must not |
| 45 | + // stop insertions for the duration of a full-cache scan. |
| 46 | + if !func() bool { |
| 47 | + if !c.mu.TryLock() { |
| 48 | + return false |
| 49 | + } |
| 50 | + defer c.mu.Unlock() |
| 51 | + s = MemoryStats{Entries: c.len(), Capacity: c.capacity, RetainedEntries: c.entries.Size()} |
| 52 | + for _, e := range c.deathRow { |
| 53 | + if e != nil { |
| 54 | + s.PendingEntries++ |
| 55 | + } |
| 56 | + } |
| 57 | + var e entry[K, V] |
| 58 | + s.StructuralBytes = uint64(unsafe.Sizeof(*c)) + 2*uint64(unsafe.Sizeof(bloomFilter{})) + |
| 59 | + uint64(cap(c.ghostActive.data)+cap(c.ghostAging.data))*8 + |
| 60 | + uint64(cap(c.deathRow))*uint64(unsafe.Sizeof(&e)) + |
| 61 | + uint64(s.RetainedEntries)*uint64(unsafe.Sizeof(e)) |
| 62 | + if c.freeEntry != nil { |
| 63 | + s.StructuralBytes += uint64(unsafe.Sizeof(e)) |
| 64 | + if sizeOf != nil { |
| 65 | + recycledKey = c.freeEntry.key |
| 66 | + recycledValue, recycled = c.freeEntry.loadValue() |
| 67 | + } |
| 68 | + } |
| 69 | + return true |
| 70 | + }() { |
| 71 | + return MemoryStats{}, false |
| 72 | + } |
| 73 | + if sizeOf != nil { |
| 74 | + c.entries.Range(func(k K, e *entry[K, V]) bool { |
| 75 | + if v, ok := e.loadValue(); ok { |
| 76 | + s.PayloadBytes += sizeOf(k, v) |
| 77 | + } |
| 78 | + return true |
| 79 | + }) |
| 80 | + if recycled { |
| 81 | + s.PayloadBytes += sizeOf(recycledKey, recycledValue) |
| 82 | + } |
| 83 | + } |
| 84 | + return s, true |
| 85 | +} |
0 commit comments