github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/cache/lfucache/metrics.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 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 lfucache 16 17 import ( 18 "bytes" 19 "fmt" 20 "sync/atomic" 21 ) 22 23 func (lfc *LfuCache) MetricsInfo() string { 24 return lfc.Metrics().String() 25 } 26 27 func (lfc *LfuCache) Metrics() Metrics { 28 var m Metrics 29 m.ShardsMetrics = make([]ShardMetrics, lfc.shardNum) 30 for i := range lfc.shards { 31 s := lfc.shards[i] 32 s.mu.Lock() 33 atSize := s.arrayTableInuseSize() 34 memLen := len(s.mu.memQueue) 35 memSize := s.memTableInuseSize() 36 size := atSize + memSize 37 s.mu.Unlock() 38 m.ShardsMetrics[i] = ShardMetrics{ 39 Size: size, 40 InusePercent: int(size * 100 / s.maxSize), 41 MemLen: memLen, 42 MemCount: s.memTableCount(), 43 MemSize: memSize, 44 AtCount: s.arrayTableCount(), 45 AtSize: atSize, 46 } 47 m.Size += size 48 m.Hits += atomic.LoadInt64(&s.atomic.hits) 49 m.Misses += atomic.LoadInt64(&s.atomic.misses) 50 } 51 return m 52 } 53 54 type ShardMetrics struct { 55 Size uint64 56 InusePercent int 57 MemLen int 58 MemCount int 59 MemSize uint64 60 AtCount int 61 AtSize uint64 62 } 63 64 type Metrics struct { 65 Size uint64 66 Hits int64 67 Misses int64 68 ShardsMetrics []ShardMetrics 69 } 70 71 func (m Metrics) String() string { 72 var shards bytes.Buffer 73 for i := range m.ShardsMetrics { 74 shards.WriteString(fmt.Sprintf("[%d:%d:%d:%d:%d:%d:%d:%d]", 75 i, 76 m.ShardsMetrics[i].Size, 77 m.ShardsMetrics[i].InusePercent, 78 m.ShardsMetrics[i].MemLen, 79 m.ShardsMetrics[i].MemCount, 80 m.ShardsMetrics[i].MemSize, 81 m.ShardsMetrics[i].AtCount, 82 m.ShardsMetrics[i].AtSize)) 83 } 84 return fmt.Sprintf("size:%d hit:%d mis:%d shards:%s", m.Size, m.Hits, m.Misses, shards.String()) 85 }