Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "runtime"
- "time"
- _ "net/http/pprof"
- "log"
- "net/http"
- "strings"
- )
- func g(in chan int) {
- for {
- select {
- case a := <-in:
- fmt.Println("Got a", a)
- case <-time.After(10 * time.Second):
- fmt.Println("second passed, still no a")
- }
- }
- }
- func LongDelay() {
- <-time.After(time.Hour)
- fmt.Println("Long delay finished")
- }
- func main() {
- runtime.SetBlockProfileRate(1)
- runtime.SetMutexProfileFraction(1)
- go func() {
- log.Println(http.ListenAndServe(":6060", nil))
- }()
- ch := make(chan int, 100)
- go g(ch)
- go LongDelay()
- for {
- var p []runtime.BlockProfileRecord
- n, ok := runtime.BlockProfile(nil)
- for {
- p = make([]runtime.BlockProfileRecord, n+50)
- n, ok = runtime.BlockProfile(p)
- if ok {
- p = p[:n]
- break
- }
- }
- fmt.Println("blockPrifleRecord reached", len(p))
- fmt.Println("====================")
- for _, rec := range p {
- frames := runtime.CallersFrames(rec.Stack())
- allFrames := make([]runtime.Frame, 0)
- for {
- frame, more := frames.Next()
- allFrames = append(allFrames, frame)
- if !more {
- break
- }
- }
- for i := len(allFrames) - 1; i >= 0; i-- {
- frame := allFrames[i]
- if strings.Contains(frame.Function, "runtime"){
- continue
- }
- fmt.Printf("%s %s:%d, count: %d, cycles %d\n", frame.Function, frame.File, frame.Line, rec.Count, rec.Cycles)
- break
- }
- }
- time.Sleep(200 * time.Millisecond)
- select {
- case ch <- 10:
- default:
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement