Merge pull request #10517 from filecoin-project/10504-cache-execution-traces
feat: Add small cache to execution traces
This commit is contained in:
commit
b4d644e768
@ -128,10 +128,43 @@ func (sm *StateManager) ExecutionTraceWithMonitor(ctx context.Context, ts *types
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (sm *StateManager) ExecutionTrace(ctx context.Context, ts *types.TipSet) (cid.Cid, []*api.InvocResult, error) {
|
func (sm *StateManager) ExecutionTrace(ctx context.Context, ts *types.TipSet) (cid.Cid, []*api.InvocResult, error) {
|
||||||
|
tsKey := ts.Key()
|
||||||
|
|
||||||
|
// check if we have the trace for this tipset in the cache
|
||||||
|
sm.execTraceCacheLock.Lock()
|
||||||
|
if entry, ok := sm.execTraceCache.Get(tsKey); ok {
|
||||||
|
// we have to make a deep copy since caller can modify the invocTrace
|
||||||
|
// and we don't want that to change what we store in cache
|
||||||
|
invocTraceCopy := makeDeepCopy(entry.invocTrace)
|
||||||
|
sm.execTraceCacheLock.Unlock()
|
||||||
|
return entry.postStateRoot, invocTraceCopy, nil
|
||||||
|
}
|
||||||
|
sm.execTraceCacheLock.Unlock()
|
||||||
|
|
||||||
var invocTrace []*api.InvocResult
|
var invocTrace []*api.InvocResult
|
||||||
st, err := sm.ExecutionTraceWithMonitor(ctx, ts, &InvocationTracer{trace: &invocTrace})
|
st, err := sm.ExecutionTraceWithMonitor(ctx, ts, &InvocationTracer{trace: &invocTrace})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid.Undef, nil, err
|
return cid.Undef, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
invocTraceCopy := makeDeepCopy(invocTrace)
|
||||||
|
|
||||||
|
sm.execTraceCacheLock.Lock()
|
||||||
|
sm.execTraceCache.Add(tsKey, tipSetCacheEntry{st, invocTraceCopy})
|
||||||
|
sm.execTraceCacheLock.Unlock()
|
||||||
|
|
||||||
return st, invocTrace, nil
|
return st, invocTrace, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeDeepCopy(invocTrace []*api.InvocResult) []*api.InvocResult {
|
||||||
|
c := make([]*api.InvocResult, len(invocTrace))
|
||||||
|
for i, ir := range invocTrace {
|
||||||
|
if ir == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tmp := *ir
|
||||||
|
c[i] = &tmp
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
lru "github.com/hashicorp/golang-lru/v2"
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
dstore "github.com/ipfs/go-datastore"
|
dstore "github.com/ipfs/go-datastore"
|
||||||
cbor "github.com/ipfs/go-ipld-cbor"
|
cbor "github.com/ipfs/go-ipld-cbor"
|
||||||
@ -39,6 +40,8 @@ import (
|
|||||||
const LookbackNoLimit = api.LookbackNoLimit
|
const LookbackNoLimit = api.LookbackNoLimit
|
||||||
const ReceiptAmtBitwidth = 3
|
const ReceiptAmtBitwidth = 3
|
||||||
|
|
||||||
|
const execTraceCacheSize = 16
|
||||||
|
|
||||||
var log = logging.Logger("statemgr")
|
var log = logging.Logger("statemgr")
|
||||||
|
|
||||||
type StateManagerAPI interface {
|
type StateManagerAPI interface {
|
||||||
@ -138,6 +141,13 @@ type StateManager struct {
|
|||||||
beacon beacon.Schedule
|
beacon beacon.Schedule
|
||||||
|
|
||||||
msgIndex index.MsgIndex
|
msgIndex index.MsgIndex
|
||||||
|
|
||||||
|
// We keep a small cache for calls to ExecutionTrace which helps improve
|
||||||
|
// performance for node operators like exchanges and block explorers
|
||||||
|
execTraceCache *lru.ARCCache[types.TipSetKey, tipSetCacheEntry]
|
||||||
|
// We need a lock while making the copy as to prevent other callers
|
||||||
|
// overwrite the cache while making the copy
|
||||||
|
execTraceCacheLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Caches a single state tree
|
// Caches a single state tree
|
||||||
@ -146,6 +156,11 @@ type treeCache struct {
|
|||||||
tree *state.StateTree
|
tree *state.StateTree
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type tipSetCacheEntry struct {
|
||||||
|
postStateRoot cid.Cid
|
||||||
|
invocTrace []*api.InvocResult
|
||||||
|
}
|
||||||
|
|
||||||
func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, us UpgradeSchedule, beacon beacon.Schedule, metadataDs dstore.Batching, msgIndex index.MsgIndex) (*StateManager, error) {
|
func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, us UpgradeSchedule, beacon beacon.Schedule, metadataDs dstore.Batching, msgIndex index.MsgIndex) (*StateManager, error) {
|
||||||
// If we have upgrades, make sure they're in-order and make sense.
|
// If we have upgrades, make sure they're in-order and make sense.
|
||||||
if err := us.Validate(); err != nil {
|
if err := us.Validate(); err != nil {
|
||||||
@ -185,6 +200,11 @@ func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
execTraceCache, err := lru.NewARC[types.TipSetKey, tipSetCacheEntry](execTraceCacheSize)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &StateManager{
|
return &StateManager{
|
||||||
networkVersions: networkVersions,
|
networkVersions: networkVersions,
|
||||||
latestVersion: lastVersion,
|
latestVersion: lastVersion,
|
||||||
@ -200,8 +220,9 @@ func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder,
|
|||||||
root: cid.Undef,
|
root: cid.Undef,
|
||||||
tree: nil,
|
tree: nil,
|
||||||
},
|
},
|
||||||
compWait: make(map[string]chan struct{}),
|
compWait: make(map[string]chan struct{}),
|
||||||
msgIndex: msgIndex,
|
msgIndex: msgIndex,
|
||||||
|
execTraceCache: execTraceCache,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user