summaryrefslogtreecommitdiff
path: root/vendor/github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi/perfmap.go
blob: 642c7f75dfd38355ff7ba42a51778893cb0e9f6b (plain)
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
86
87
88
89
90
91
92
93
94
95
96
package wazevoapi

import (
	"fmt"
	"os"
	"strconv"
	"sync"
)

var PerfMap *Perfmap

func init() {
	if PerfMapEnabled {
		pid := os.Getpid()
		filename := "/tmp/perf-" + strconv.Itoa(pid) + ".map"

		fh, err := os.OpenFile(filename, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0o644)
		if err != nil {
			panic(err)
		}

		PerfMap = &Perfmap{fh: fh}
	}
}

// Perfmap holds perfmap entries to be flushed into a perfmap file.
type Perfmap struct {
	entries []entry
	mux     sync.Mutex
	fh      *os.File
}

type entry struct {
	index  int
	offset int64
	size   uint64
	name   string
}

func (f *Perfmap) Lock() {
	f.mux.Lock()
}

func (f *Perfmap) Unlock() {
	f.mux.Unlock()
}

// AddModuleEntry adds a perfmap entry into the perfmap file.
// index is the index of the function in the module, offset is the offset of the function in the module,
// size is the size of the function, and name is the name of the function.
//
// Note that the entries are not flushed into the perfmap file until Flush is called,
// and the entries are module-scoped; Perfmap must be locked until Flush is called.
func (f *Perfmap) AddModuleEntry(index int, offset int64, size uint64, name string) {
	e := entry{index: index, offset: offset, size: size, name: name}
	if f.entries == nil {
		f.entries = []entry{e}
		return
	}
	f.entries = append(f.entries, e)
}

// Flush writes the perfmap entries into the perfmap file where the entries are adjusted by the given `addr` and `functionOffsets`.
func (f *Perfmap) Flush(addr uintptr, functionOffsets []int) {
	defer func() {
		_ = f.fh.Sync()
	}()

	for _, e := range f.entries {
		if _, err := f.fh.WriteString(fmt.Sprintf("%x %s %s\n",
			uintptr(e.offset)+addr+uintptr(functionOffsets[e.index]),
			strconv.FormatUint(e.size, 16),
			e.name,
		)); err != nil {
			panic(err)
		}
	}
	f.entries = f.entries[:0]
}

// Clear clears the perfmap entries not yet flushed.
func (f *Perfmap) Clear() {
	f.entries = f.entries[:0]
}

// AddEntry writes a perfmap entry directly into the perfmap file, not using the entries.
func (f *Perfmap) AddEntry(addr uintptr, size uint64, name string) {
	_, err := f.fh.WriteString(fmt.Sprintf("%x %s %s\n",
		addr,
		strconv.FormatUint(size, 16),
		name,
	))
	if err != nil {
		panic(err)
	}
}