go-profiler-notes/examples/block-vs-mutex/main.go
sunxunle 06e60a3cf4 chore: fix some typos
Signed-off-by: sunxunle <sunxunle@gmail.com>
2024-03-16 22:24:09 +08:00

55 lines
833 B
Go

package main
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
"sync"
"time"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
runtime.SetBlockProfileRate(1)
runtime.SetMutexProfileFraction(1)
acquired := make(chan struct{})
var m sync.Mutex
m.Lock()
go func() {
<-acquired
m.Lock()
acquired <- struct{}{}
}()
acquired <- struct{}{}
time.Sleep(time.Nanosecond)
m.Unlock()
<-acquired
if err := writeProfile("block"); err != nil {
return err
} else if err := writeProfile("mutex"); err != nil {
return err
}
return nil
}
func writeProfile(name string) error {
f, err := os.Create(name + ".pb.gz")
if err != nil {
return err
}
defer f.Close()
if err := pprof.Lookup(name).WriteTo(f, 0); err != nil {
return err
}
return nil
}