Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Enforce profiling.Prepare to enhance symbolization selection #104

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions cmd/wzprof/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,29 @@ func (prog *program) run(ctx context.Context) error {

p := wzprof.ProfilingFor(wasmCode)

cpu := p.CPUProfiler(wzprof.HostTime(prog.hostTime))
mem := p.MemoryProfiler(wzprof.InuseMemory(prog.inuseMemory))
runtime := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
WithDebugInfoEnabled(true).
WithCustomSections(true))

stdout.Printf("compiling wasm module %s", prog.filePath)
compiledModule, err := runtime.CompileModule(ctx, wasmCode)
if err != nil {
return fmt.Errorf("compiling wasm module: %w", err)
}
err = p.Prepare(compiledModule)
if err != nil {
return fmt.Errorf("preparing wasm module: %w", err)
}

cpu, err := p.CPUProfiler(wzprof.HostTime(prog.hostTime))
if err != nil {
return fmt.Errorf("creating cpu profiler: %w", err)
}

mem, err := p.MemoryProfiler(wzprof.InuseMemory(prog.inuseMemory))
if err != nil {
return fmt.Errorf("creating memory profiler: %w", err)
}

var listeners []experimental.FunctionListenerFactory
if prog.cpuProfile != "" || prog.pprofAddr != "" {
Expand All @@ -78,25 +99,6 @@ func (prog *program) run(ctx context.Context) error {
}
}

ctx = context.WithValue(ctx,
iamrajiv marked this conversation as resolved.
Show resolved Hide resolved
experimental.FunctionListenerFactoryKey{},
experimental.MultiFunctionListenerFactory(listeners...),
)

runtime := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
WithDebugInfoEnabled(true).
WithCustomSections(true))

stdout.Printf("compiling wasm module %s", prog.filePath)
compiledModule, err := runtime.CompileModule(ctx, wasmCode)
if err != nil {
return fmt.Errorf("compiling wasm module: %w", err)
}
err = p.Prepare(compiledModule)
if err != nil {
return fmt.Errorf("preparing wasm module: %w", err)
}

if prog.pprofAddr != "" {
u := &url.URL{Scheme: "http", Host: prog.pprofAddr, Path: "/debug/pprof"}
stdout.Printf("starting prrof http sever at %s", u)
Expand Down
15 changes: 12 additions & 3 deletions cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,31 @@ import (
)

func BenchmarkCPUProfilerOn(b *testing.B) {
p := ProfilingFor(nil).CPUProfiler()
p, err := ProfilingFor(nil).CPUProfiler()
if err != nil {
b.Fatal(err)
}
p.StartProfile()
benchmarkFunctionListener(b, p)
}

func BenchmarkCPUProfilerOff(b *testing.B) {
p := ProfilingFor(nil).CPUProfiler()
p, err := ProfilingFor(nil).CPUProfiler()
if err != nil {
b.Fatal(err)
}
benchmarkFunctionListener(b, p)
}

func TestCPUProfilerTime(t *testing.T) {
currentTime := int64(0)

p := ProfilingFor(nil).CPUProfiler(
p, err := ProfilingFor(nil).CPUProfiler(
TimeFunc(func() int64 { return currentTime }),
)
if err != nil {
t.Fatal(err)
}

module := wazerotest.NewModule(nil,
wazerotest.NewFunction(func(context.Context, api.Module) {}),
Expand Down
5 changes: 4 additions & 1 deletion mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
)

func BenchmarkMemoryProfiler(b *testing.B) {
p := ProfilingFor(nil).MemoryProfiler()
p, err := ProfilingFor(nil).MemoryProfiler()
if err != nil {
b.Fatal(err)
}
benchmarkFunctionListener(b, p)
}
38 changes: 25 additions & 13 deletions wzprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type Profiling struct {
symbols symbolizer
stackIterator func(mod api.Module, def api.FunctionDefinition, wasmsi experimental.StackIterator) experimental.StackIterator

lang language
lang language
prepareCalled bool // Flag to indicate if Prepare has been called
}

type language int8
Expand Down Expand Up @@ -95,21 +96,10 @@ func ProfilingFor(wasm []byte) *Profiling {
return r
}

// CPUProfiler constructs a new instance of CPUProfiler using the given time
// function to record the CPU time consumed.
func (p *Profiling) CPUProfiler(options ...CPUProfilerOption) *CPUProfiler {
return newCPUProfiler(p, options...)
}

// MemoryProfiler constructs a new instance of MemoryProfiler using the given
// time function to record the profile execution time.
func (p *Profiling) MemoryProfiler(options ...MemoryProfilerOption) *MemoryProfiler {
return newMemoryProfiler(p, options...)
}

// Prepare selects the most appropriate analysis functions for the guest
// code in the provided module.
func (p *Profiling) Prepare(mod wazero.CompiledModule) error {

switch p.lang {
case golang:
s, err := preparePclntabSymbolizer(p.wasm, mod)
Expand Down Expand Up @@ -147,9 +137,31 @@ func (p *Profiling) Prepare(mod wazero.CompiledModule) error {
}
p.symbols = buildDwarfSymbolizer(dwarf)
}

// Set the flag to true if Prepare succeeds
p.prepareCalled = true

return nil
}
iamrajiv marked this conversation as resolved.
Show resolved Hide resolved

// CPUProfiler constructs a new instance of CPUProfiler using the given time
// function to record the CPU time consumed.
func (p *Profiling) CPUProfiler(options ...CPUProfilerOption) (*CPUProfiler, error) {
if !p.prepareCalled {
return nil, fmt.Errorf("Prepare must be called on the Profiling instance before creating a CPUProfiler")
}
return newCPUProfiler(p, options...), nil
}

// MemoryProfiler constructs a new instance of MemoryProfiler using the given
// time function to record the profile execution time.
func (p *Profiling) MemoryProfiler(options ...MemoryProfilerOption) (*MemoryProfiler, error) {
if !p.prepareCalled {
return nil, fmt.Errorf("Prepare must be called on the Profiling instance before creating a MemoryProfiler")
}
return newMemoryProfiler(p, options...), nil
}

// profilingListener wraps a FunctionListener to adapt its stack iterator to the
// appropriate implementation according to the module support.
type profilingListener struct {
Expand Down