Skip to content

Instantly share code, notes, and snippets.

@tam7t
Created April 21, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tam7t/f38ef0a4c7605eaada8f to your computer and use it in GitHub Desktop.
Save tam7t/f38ef0a4c7605eaada8f to your computer and use it in GitHub Desktop.
signal profiling
package main
import "fmt"
import "syscall"
import "os"
import "os/signal"
import "time"
import "runtime/pprof"
func main() {
messages := make(chan string)
// consumer
go func() {
for {
select {
case m := <-messages:
fmt.Println(m)
}
}
}()
// producer
go func() {
for {
time.Sleep(1 * time.Second)
messages <- `ping`
}
}()
go profile()
// block indefinately
select {}
}
func profile() {
c := make(chan os.Signal, 10)
signal.Notify(c, syscall.SIGUSR1, syscall.SIGUSR2)
fmt.Println("waiting for SIGUSR1/SIGUSR2")
for {
s := <-c
switch s {
case syscall.SIGUSR1:
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
case syscall.SIGUSR2:
pprof.Lookup("heap").WriteTo(os.Stdout, 1)
pprof.Lookup("block").WriteTo(os.Stdout, 1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment