summaryrefslogtreecommitdiff
path: root/vendor/github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi/perfmap.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-05-27 15:46:15 +0000
committerLibravatar GitHub <noreply@github.com>2024-05-27 17:46:15 +0200
commit1e7b32490dfdccddd04f46d4b0416b48d749d51b (patch)
tree62a11365933a5a11e0800af64cbdf9172e5e6e7a /vendor/github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi/perfmap.go
parent[chore] Small styling + link issues (#2933) (diff)
downloadgotosocial-1e7b32490dfdccddd04f46d4b0416b48d749d51b.tar.xz
[experiment] add alternative wasm sqlite3 implementation available via build-tag (#2863)
This allows for building GoToSocial with [SQLite transpiled to WASM](https://github.com/ncruces/go-sqlite3) and accessed through [Wazero](https://wazero.io/).
Diffstat (limited to 'vendor/github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi/perfmap.go')
-rw-r--r--vendor/github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi/perfmap.go96
1 files changed, 96 insertions, 0 deletions
diff --git a/vendor/github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi/perfmap.go b/vendor/github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi/perfmap.go
new file mode 100644
index 000000000..642c7f75d
--- /dev/null
+++ b/vendor/github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi/perfmap.go
@@ -0,0 +1,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)
+ }
+}