Skip to content

Commit 3a692e4

Browse files
committed
pkg/symbolizer: some bench files
1 parent fb62aac commit 3a692e4

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

pkg/symbolizer/benchmark_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2025 syzkaller project authors. All rights reserved.
2+
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3+
4+
package symbolizer_test
5+
6+
import (
7+
"bufio"
8+
"bytes"
9+
"math/rand"
10+
"os"
11+
"os/exec"
12+
"path/filepath"
13+
"runtime"
14+
"strconv"
15+
"strings"
16+
"testing"
17+
"time"
18+
19+
"github.com/google/syzkaller/pkg/symbolizer"
20+
"github.com/google/syzkaller/sys/targets"
21+
)
22+
23+
func BenchmarkSymbolize(b *testing.B) {
24+
if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
25+
b.Skip("skipping on non-linux/amd64")
26+
}
27+
28+
// Find vmlinux.
29+
vmlinux := os.Getenv("VMLINUX")
30+
if vmlinux == "" {
31+
// Try default location.
32+
vmlinux = filepath.Join(os.Getenv("HOME"), "projects/fuzzing-qemu/linux-stable/vmlinux")
33+
}
34+
if _, err := os.Stat(vmlinux); err != nil {
35+
b.Skipf("vmlinux not found at %v: %v", vmlinux, err)
36+
}
37+
38+
// Create native symbolizer.
39+
target := targets.Get("linux", "amd64")
40+
target.KernelObject = vmlinux
41+
42+
symb, err := symbolizer.Make(target, vmlinux)
43+
if err != nil {
44+
b.Fatalf("failed to create symbolizer: %v", err)
45+
}
46+
defer symb.Close()
47+
48+
// Gather interesting PCs.
49+
// We want random PCs across the entire binary to stress the cache and searching.
50+
pcs := gatherAllPCs(nil, vmlinux)
51+
if len(pcs) == 0 {
52+
b.Fatal("no pcs found")
53+
}
54+
55+
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
56+
b.ResetTimer()
57+
58+
for i := 0; i < b.N; i++ {
59+
pc := pcs[rng.Intn(len(pcs))]
60+
_, err := symb.Symbolize(vmlinux, pc)
61+
if err != nil {
62+
b.Fatalf("symbolize failed: %v", err)
63+
}
64+
}
65+
}
66+
67+
func gatherAllPCs(t *testing.T, bin string) []uint64 {
68+
cmd := exec.Command("nm", "-n", bin)
69+
out, err := cmd.Output()
70+
if err != nil {
71+
if t != nil {
72+
t.Fatalf("nm failed: %v", err)
73+
}
74+
return nil
75+
}
76+
77+
var pcs []uint64
78+
scanner := bufio.NewScanner(bytes.NewReader(out))
79+
for scanner.Scan() {
80+
line := scanner.Text()
81+
parts := strings.Fields(line)
82+
if len(parts) < 3 {
83+
continue
84+
}
85+
ifParts := parts[1]
86+
if ifParts == "t" || ifParts == "T" {
87+
addr, err := strconv.ParseUint(parts[0], 16, 64)
88+
// Kernel text usually starts high.
89+
if err == nil && addr > 0xffffffff80000000 {
90+
pcs = append(pcs, addr)
91+
}
92+
}
93+
}
94+
return pcs
95+
}

pkg/symbolizer/make_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2026 syzkaller project authors. All rights reserved.
2+
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3+
4+
package symbolizer_test
5+
6+
import (
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
"time"
11+
12+
"github.com/google/syzkaller/pkg/symbolizer"
13+
"github.com/google/syzkaller/sys/targets"
14+
)
15+
16+
func BenchmarkMake(b *testing.B) {
17+
vmlinux := os.Getenv("VMLINUX")
18+
if vmlinux == "" {
19+
vmlinux = filepath.Join(os.Getenv("HOME"), "projects/fuzzing-qemu/linux-stable/vmlinux")
20+
}
21+
if _, err := os.Stat(vmlinux); err != nil {
22+
b.Skipf("vmlinux not found at %v: %v", vmlinux, err)
23+
}
24+
25+
target := targets.Get("linux", "amd64")
26+
target.KernelObject = vmlinux
27+
28+
b.ResetTimer()
29+
for i := 0; i < b.N; i++ {
30+
start := time.Now()
31+
symb, err := symbolizer.Make(target, vmlinux)
32+
if err != nil {
33+
b.Fatalf("failed to create symbolizer: %v", err)
34+
}
35+
b.ReportMetric(float64(time.Since(start).Seconds()), "sec/op")
36+
symb.Close()
37+
}
38+
}

0 commit comments

Comments
 (0)