Skip to content

Commit

Permalink
Merge pull request #34 from bobotu/counter
Browse files Browse the repository at this point in the history
Use segment local hit and miss counter.
  • Loading branch information
coocood authored Feb 6, 2018
2 parents 3e2d56b + 4ee4fdb commit 0eab74f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
41 changes: 18 additions & 23 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (
)

type Cache struct {
locks [256]sync.Mutex
segments [256]segment
hitCount int64
missCount int64
locks [256]sync.Mutex
segments [256]segment
}

func hashFunc(data []byte) uint64 {
Expand Down Expand Up @@ -53,11 +51,6 @@ func (cache *Cache) Get(key []byte) (value []byte, err error) {
cache.locks[segId].Lock()
value, _, err = cache.segments[segId].get(key, hashVal)
cache.locks[segId].Unlock()
if err == nil {
atomic.AddInt64(&cache.hitCount, 1)
} else {
atomic.AddInt64(&cache.missCount, 1)
}
return
}

Expand All @@ -68,11 +61,6 @@ func (cache *Cache) GetWithExpiration(key []byte) (value []byte, expireAt uint32
cache.locks[segId].Lock()
value, expireAt, err = cache.segments[segId].get(key, hashVal)
cache.locks[segId].Unlock()
if err == nil {
atomic.AddInt64(&cache.hitCount, 1)
} else {
atomic.AddInt64(&cache.missCount, 1)
}
return
}

Expand Down Expand Up @@ -153,20 +141,31 @@ func (cache *Cache) AverageAccessTime() int64 {
}
}

func (cache *Cache) HitCount() int64 {
return atomic.LoadInt64(&cache.hitCount)
func (cache *Cache) HitCount() (count int64) {
for i := range cache.segments {
count += atomic.LoadInt64(&cache.segments[i].hitCount)
}
return
}

func (cache *Cache) MissCount() (count int64) {
for i := range cache.segments {
count += atomic.LoadInt64(&cache.segments[i].missCount)
}
return
}

func (cache *Cache) LookupCount() int64 {
return atomic.LoadInt64(&cache.hitCount) + atomic.LoadInt64(&cache.missCount)
return cache.HitCount() + cache.MissCount()
}

func (cache *Cache) HitRate() float64 {
lookupCount := cache.LookupCount()
hitCount, missCount := cache.HitCount(), cache.MissCount()
lookupCount := hitCount + missCount
if lookupCount == 0 {
return 0
} else {
return float64(cache.HitCount()) / float64(lookupCount)
return float64(hitCount) / float64(lookupCount)
}
}

Expand All @@ -184,13 +183,9 @@ func (cache *Cache) Clear() {
cache.segments[i] = newSeg
cache.locks[i].Unlock()
}
atomic.StoreInt64(&cache.hitCount, 0)
atomic.StoreInt64(&cache.missCount, 0)
}

func (cache *Cache) ResetStatistics() {
atomic.StoreInt64(&cache.hitCount, 0)
atomic.StoreInt64(&cache.missCount, 0)
for i := 0; i < 256; i++ {
cache.locks[i].Lock()
cache.segments[i].resetStatistics()
Expand Down
7 changes: 7 additions & 0 deletions segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type entryHdr struct {
type segment struct {
rb RingBuf // ring buffer that stores data
segId int
hitCount int64
missCount int64
entryCount int64
totalCount int64 // number of entries in ring buffer, including deleted entries.
totalTime int64 // used to calculate least recent used entry.
Expand Down Expand Up @@ -194,6 +196,7 @@ func (seg *segment) get(key []byte, hashVal uint64) (value []byte, expireAt uint
idx, match := seg.lookup(slot, hash16, key)
if !match {
err = ErrNotFound
seg.missCount += 1
return
}
ptr := &slot[idx]
Expand All @@ -208,6 +211,7 @@ func (seg *segment) get(key []byte, hashVal uint64) (value []byte, expireAt uint
seg.delEntryPtr(slotId, hash16, ptr.offset)
seg.totalExpired++
err = ErrNotFound
seg.missCount += 1
return
}

Expand All @@ -217,6 +221,7 @@ func (seg *segment) get(key []byte, hashVal uint64) (value []byte, expireAt uint
value = make([]byte, hdr.valLen)

seg.rb.ReadAt(value, ptr.offset+ENTRY_HDR_SIZE+int64(hdr.keyLen))
seg.hitCount += 1
return
}

Expand Down Expand Up @@ -365,4 +370,6 @@ func (seg *segment) resetStatistics() {
seg.totalEvacuate = 0
seg.totalExpired = 0
seg.overwrites = 0
seg.hitCount = 0
seg.missCount = 0
}

0 comments on commit 0eab74f

Please sign in to comment.