Advertisement
Guest User

Untitled

a guest
Jul 13th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.52 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "runtime"
  6.     "time"
  7.     _ "net/http/pprof"
  8.     "log"
  9.     "net/http"
  10.      "strings"
  11. )
  12.  
  13. func g(in chan int) {
  14.     for {
  15.         select {
  16.             case a := <-in:
  17.                 fmt.Println("Got a", a)
  18.             case <-time.After(10 * time.Second):
  19.                 fmt.Println("second passed, still no a")
  20.  
  21.         }
  22.     }
  23. }
  24.  
  25. func LongDelay() {
  26.     <-time.After(time.Hour)
  27.     fmt.Println("Long delay finished")
  28. }
  29.  
  30. func main() {
  31.     runtime.SetBlockProfileRate(1)
  32.     runtime.SetMutexProfileFraction(1)
  33.     go func() {
  34.         log.Println(http.ListenAndServe(":6060", nil))
  35.     }()
  36.     ch := make(chan int, 100)
  37.     go g(ch)
  38.     go LongDelay()
  39.  
  40.     for {
  41.         var p []runtime.BlockProfileRecord
  42.         n, ok := runtime.BlockProfile(nil)
  43.         for {
  44.             p = make([]runtime.BlockProfileRecord, n+50)
  45.             n, ok = runtime.BlockProfile(p)
  46.             if ok {
  47.                 p = p[:n]
  48.                 break
  49.             }
  50.         }
  51.         fmt.Println("blockPrifleRecord reached", len(p))
  52.         fmt.Println("====================")
  53.         for _, rec := range p {
  54.             frames := runtime.CallersFrames(rec.Stack())
  55.             allFrames := make([]runtime.Frame, 0)
  56.  
  57.             for {
  58.                 frame, more := frames.Next()
  59.                 allFrames = append(allFrames, frame)
  60.                 if !more {
  61.                     break
  62.                 }
  63.             }
  64.  
  65.             for i := len(allFrames) - 1; i >= 0; i-- {
  66.                 frame := allFrames[i]
  67.                 if strings.Contains(frame.Function, "runtime"){
  68.                     continue
  69.                 }
  70.                 fmt.Printf("%s %s:%d, count: %d, cycles %d\n", frame.Function, frame.File, frame.Line, rec.Count, rec.Cycles)
  71.                 break
  72.             }
  73.         }
  74.         time.Sleep(200 * time.Millisecond)
  75.         select {
  76.         case ch <- 10:
  77.         default:
  78.         }
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement