Created
April 21, 2015 14:07
-
-
Save tam7t/f38ef0a4c7605eaada8f to your computer and use it in GitHub Desktop.
signal profiling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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