-
Notifications
You must be signed in to change notification settings - Fork 66
/
cache.go
85 lines (72 loc) · 2.56 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2022 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package objectfile
import (
"errors"
"fmt"
"path"
"strconv"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/google/pprof/profile"
lru "github.com/hashicorp/golang-lru"
)
type Cache interface {
ObjectFileForProcess(pid uint32, m *profile.Mapping) (*MappedObjectFile, error)
}
type cache struct {
cache *lru.ARCCache
}
type noopCache struct{}
func (n noopCache) ObjectFileForProcess(pid uint32, m *profile.Mapping) (*MappedObjectFile, error) {
return fromProcess(pid, m)
}
// NewCache creates a new cache for object files.
func NewCache(logger log.Logger, size int) Cache {
c, err := lru.NewARC(size)
if err != nil {
level.Warn(logger).Log("msg", "failed to initialize cache", "err", err)
return &noopCache{}
}
return &cache{cache: c}
}
// ObjectFileForProcess returns the object file for the given mapping and process id.
// If object file is already in the cache, it is returned.
// Otherwise, the object file is loaded from the file system.
func (c *cache) ObjectFileForProcess(pid uint32, m *profile.Mapping) (*MappedObjectFile, error) {
if val, ok := c.cache.Get(m.BuildID); ok {
return val.(*MappedObjectFile), nil
}
objFile, err := fromProcess(pid, m)
if err != nil {
return nil, err
}
c.cache.Add(m.BuildID, objFile)
return objFile, nil
}
// fromProcess opens the specified executable or library file from the process.
func fromProcess(pid uint32, m *profile.Mapping) (*MappedObjectFile, error) {
if strings.EqualFold(m.File, "[vdso]") || strings.EqualFold(m.File, "[vsyscall]") {
return nil, errors.New("cannot load object file for mappings of [vdso] or [vsyscall]")
}
if m.File == "" {
return nil, errors.New("cannot load object file for mappings with empty file")
}
filePath := path.Join("/proc", strconv.FormatUint(uint64(pid), 10), "/root", m.File)
objFile, err := Open(filePath, m)
if err != nil {
return nil, fmt.Errorf("failed to open mapped file: %v", err)
}
return &MappedObjectFile{ObjectFile: objFile, PID: pid, File: m.File}, nil
}