Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d_lru: smaller limit for CodeDomain #12990

Merged
merged 8 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions erigon-lib/state/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ func (a *Aggregator) SetMergeWorkers(i int) { a.mergeWorkers = i }
func (a *Aggregator) SetCompressWorkers(i int) {
for _, d := range a.d {
d.compressCfg.Workers = i
d.History.compressorCfg.Workers = i
d.History.InvertedIndex.compressorCfg.Workers = i
}
for _, ii := range a.iis {
ii.compressorCfg.Workers = i
Expand Down
47 changes: 23 additions & 24 deletions erigon-lib/state/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type u192 struct{ hi, lo, ext uint64 } //nolint
type DomainGetFromFileCache struct {
*freelru.LRU[uint64, domainGetFromFileCacheItem]
enabled, trace bool
limit uint32
}

// nolint
Expand All @@ -34,30 +35,36 @@ var (
domainGetFromFileCacheEnabled = dbg.EnvBool("D_LRU_ENABLED", true)
)

func NewDomainGetFromFileCache() *DomainGetFromFileCache {
c, err := freelru.New[uint64, domainGetFromFileCacheItem](domainGetFromFileCacheLimit, u64noHash)
func NewDomainGetFromFileCache(limit uint32) *DomainGetFromFileCache {
c, err := freelru.New[uint64, domainGetFromFileCacheItem](limit, u64noHash)
if err != nil {
panic(err)
}
return &DomainGetFromFileCache{LRU: c, enabled: domainGetFromFileCacheEnabled, trace: domainGetFromFileCacheTrace}
return &DomainGetFromFileCache{LRU: c, enabled: domainGetFromFileCacheEnabled, trace: domainGetFromFileCacheTrace, limit: limit}
}

func (c *DomainGetFromFileCache) SetTrace(v bool) { c.trace = v }
func (c *DomainGetFromFileCache) LogStats(dt kv.Domain) {
func (c *DomainGetFromFileCache) LogStats(domain kv.Domain) {
if c == nil || !c.enabled || !c.trace {
return
}
m := c.Metrics()
log.Warn("[dbg] DomainGetFromFileCache", "a", dt.String(), "hit", m.Hits, "total", m.Hits+m.Misses, "Collisions", m.Collisions, "Evictions", m.Evictions, "Inserts", m.Inserts, "limit", domainGetFromFileCacheLimit, "ratio", fmt.Sprintf("%.2f", float64(m.Hits)/float64(m.Hits+m.Misses)))
log.Warn("[dbg] DomainGetFromFileCache", "a", domain.String(), "ratio", fmt.Sprintf("%.2f", float64(m.Hits)/float64(m.Hits+m.Misses)), "hit", m.Hits, "Collisions", m.Collisions, "Evictions", m.Evictions, "Inserts", m.Inserts, "limit", c.limit)
}

func NewDomainGetFromFileCacheAny() any { return NewDomainGetFromFileCache() }
func newDomainVisible(name kv.Domain, files []visibleFile) *domainVisible {
d := &domainVisible{
name: name,
files: files,
caches: &sync.Pool{New: NewDomainGetFromFileCacheAny},
name: name,
files: files,
}
limit := domainGetFromFileCacheLimit
if name == kv.CodeDomain {
limit = limit / 10 // CodeDomain has compressed values - means cache will store values (instead of pointers to mmap)
}
if limit == 0 {
domainGetFromFileCacheEnabled = false
}
d.caches = &sync.Pool{New: func() any { return NewDomainGetFromFileCache(limit) }}
return d
}

Expand All @@ -82,7 +89,8 @@ var (
)

type IISeekInFilesCache struct {
*freelru.LRU[uint64, iiSeekInFilesCacheItem]
*freelru.LRU[uint64, iiSeekInFilesCacheItem] // murmur3(key) -> {requestedTxNum, foundTxNum}

hit, total int
trace bool
}
Expand All @@ -106,29 +114,20 @@ func (c *IISeekInFilesCache) LogStats(fileBaseName string) {
return
}
m := c.Metrics()
log.Warn("[dbg] IISeekInFilesCache", "a", fileBaseName, "hit", c.hit, "total", c.total, "Collisions", m.Collisions, "Evictions", m.Evictions, "Inserts", m.Inserts, "limit", iiGetFromFileCacheLimit, "ratio", fmt.Sprintf("%.2f", float64(c.hit)/float64(c.total)))
log.Warn("[dbg] II_LRU", "a", fileBaseName, "ratio", fmt.Sprintf("%.2f", float64(c.hit)/float64(c.total)), "hit", c.hit, "collisions", m.Collisions, "evictions", m.Evictions, "inserts", m.Inserts, "removals", m.Removals, "limit", iiGetFromFileCacheLimit)
}

func NewIISeekInFilesCacheAny() any { return NewIISeekInFilesCache() }
func newIIVisible(name string, files []visibleFile) *iiVisible {
if iiGetFromFileCacheLimit == 0 {
iiGetFromFileCacheEnabled = false
}
ii := &iiVisible{
name: name,
files: files,
caches: &sync.Pool{New: NewIISeekInFilesCacheAny},
caches: &sync.Pool{New: func() any { return NewIISeekInFilesCache() }},
}
// Not on hot-path: better pre-alloc here
ii.preAlloc()
return ii
}
func (v *iiVisible) preAlloc() {
var preAlloc [10]any
for i := 0; i < len(preAlloc); i++ {
preAlloc[i] = v.caches.Get()
}
for i := 0; i < len(preAlloc); i++ {
v.caches.Put(preAlloc[i])
}
}
func (v *iiVisible) newSeekInFilesCache() *IISeekInFilesCache {
return v.caches.Get().(*IISeekInFilesCache)
}
Expand Down
Loading