github.com/maypok86/otter@v1.2.1/internal/stats/stats.go (about) 1 // Copyright (c) 2023 Alexey Mayshev. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package stats 16 17 import ( 18 "sync/atomic" 19 "unsafe" 20 21 "github.com/maypok86/otter/internal/xruntime" 22 ) 23 24 // Stats is a thread-safe statistics collector. 25 type Stats struct { 26 hits *counter 27 misses *counter 28 rejectedSets *counter 29 evictedCountersPadding [xruntime.CacheLineSize - 2*unsafe.Sizeof(atomic.Int64{})]byte 30 evictedCount atomic.Int64 31 evictedCost atomic.Int64 32 } 33 34 // New creates a new Stats collector. 35 func New() *Stats { 36 return &Stats{ 37 hits: newCounter(), 38 misses: newCounter(), 39 rejectedSets: newCounter(), 40 } 41 } 42 43 // IncHits increments the hits counter. 44 func (s *Stats) IncHits() { 45 if s == nil { 46 return 47 } 48 49 s.hits.increment() 50 } 51 52 // Hits returns the number of cache hits. 53 func (s *Stats) Hits() int64 { 54 if s == nil { 55 return 0 56 } 57 58 return s.hits.value() 59 } 60 61 // IncMisses increments the misses counter. 62 func (s *Stats) IncMisses() { 63 if s == nil { 64 return 65 } 66 67 s.misses.increment() 68 } 69 70 // Misses returns the number of cache misses. 71 func (s *Stats) Misses() int64 { 72 if s == nil { 73 return 0 74 } 75 76 return s.misses.value() 77 } 78 79 // IncRejectedSets increments the rejectedSets counter. 80 func (s *Stats) IncRejectedSets() { 81 if s == nil { 82 return 83 } 84 85 s.rejectedSets.increment() 86 } 87 88 // RejectedSets returns the number of rejected sets. 89 func (s *Stats) RejectedSets() int64 { 90 if s == nil { 91 return 0 92 } 93 94 return s.rejectedSets.value() 95 } 96 97 // IncEvictedCount increments the evictedCount counter. 98 func (s *Stats) IncEvictedCount() { 99 if s == nil { 100 return 101 } 102 103 s.evictedCount.Add(1) 104 } 105 106 // EvictedCount returns the number of evicted entries. 107 func (s *Stats) EvictedCount() int64 { 108 if s == nil { 109 return 0 110 } 111 112 return s.evictedCount.Load() 113 } 114 115 // AddEvictedCost adds cost to the evictedCost counter. 116 func (s *Stats) AddEvictedCost(cost uint32) { 117 if s == nil { 118 return 119 } 120 121 s.evictedCost.Add(int64(cost)) 122 } 123 124 // EvictedCost returns the sum of costs of evicted entries. 125 func (s *Stats) EvictedCost() int64 { 126 if s == nil { 127 return 0 128 } 129 130 return s.evictedCost.Load() 131 } 132 133 func (s *Stats) Clear() { 134 if s == nil { 135 return 136 } 137 138 s.hits.reset() 139 s.misses.reset() 140 s.rejectedSets.reset() 141 s.evictedCount.Store(0) 142 s.evictedCost.Store(0) 143 }