From fb6fb6ed7e5734bffbff0af8d67a177ee7640f39 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 2 Apr 2024 12:04:54 +0200 Subject: pkg/symbolizer: add Cache type When the same crash happens all over again, we repeatedly symbolize the same PCs. This is slow and blocks VM loop in the manager. Cache PCs we already symbolize, we are likely to symbolize them again. --- pkg/symbolizer/cache.go | 42 ++++++++++++++++++++++++++++++++++++++++++ pkg/symbolizer/cache_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 pkg/symbolizer/cache.go create mode 100644 pkg/symbolizer/cache_test.go (limited to 'pkg/symbolizer') diff --git a/pkg/symbolizer/cache.go b/pkg/symbolizer/cache.go new file mode 100644 index 000000000..dcade3929 --- /dev/null +++ b/pkg/symbolizer/cache.go @@ -0,0 +1,42 @@ +// Copyright 2024 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package symbolizer + +import ( + "sync" +) + +// Cache caches symbolization results from Symbolizer in a thread-safe way. +type Cache struct { + mu sync.RWMutex + cache map[cacheKey]cacheVal +} + +type cacheKey struct { + bin string + pc uint64 +} + +type cacheVal struct { + frames []Frame + err error +} + +func (c *Cache) Symbolize(inner func(string, uint64) ([]Frame, error), bin string, pc uint64) ([]Frame, error) { + key := cacheKey{bin, pc} + c.mu.RLock() + val, ok := c.cache[key] + c.mu.RUnlock() + if ok { + return val.frames, val.err + } + frames, err := inner(bin, pc) + c.mu.Lock() + if c.cache == nil { + c.cache = make(map[cacheKey]cacheVal) + } + c.cache[key] = cacheVal{frames, err} + c.mu.Unlock() + return frames, err +} diff --git a/pkg/symbolizer/cache_test.go b/pkg/symbolizer/cache_test.go new file mode 100644 index 000000000..eceb61f30 --- /dev/null +++ b/pkg/symbolizer/cache_test.go @@ -0,0 +1,38 @@ +// Copyright 2024 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package symbolizer + +import ( + "errors" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCache(t *testing.T) { + called := make(map[cacheKey]bool) + inner := func(bin string, pc uint64) ([]Frame, error) { + key := cacheKey{bin, pc} + assert.False(t, called[key]) + called[key] = true + if bin == "error" { + return nil, fmt.Errorf("error %v", pc) + } + return []Frame{{PC: pc, Func: bin + "_func"}}, nil + } + var cache Cache + check := func(bin string, pc uint64, frames []Frame, err error) { + gotFrames, gotErr := cache.Symbolize(inner, bin, pc) + assert.Equal(t, gotFrames, frames) + assert.Equal(t, gotErr, err) + } + check("foo", 1, []Frame{{PC: 1, Func: "foo_func"}}, nil) + check("foo", 1, []Frame{{PC: 1, Func: "foo_func"}}, nil) + check("foo", 2, []Frame{{PC: 2, Func: "foo_func"}}, nil) + check("foo", 1, []Frame{{PC: 1, Func: "foo_func"}}, nil) + check("error", 10, nil, errors.New("error 10")) + check("error", 10, nil, errors.New("error 10")) + check("error", 11, nil, errors.New("error 11")) +} -- cgit mrf-deployment