Upgrade to Pro — share decks privately, control downloads, hide ads and more …

$ go tool traceを使ってみよう/ Let's use go tool trace

1b7098127bb4872f5fa10415d88479b7?s=47 nametake
December 11, 2017

$ go tool traceを使ってみよう/ Let's use go tool trace

golang.tokyo #11( https://techplay.jp/event/641403 )での発表資料です。

1b7098127bb4872f5fa10415d88479b7?s=128

nametake

December 11, 2017
Tweet

Transcript

  1. $ go tool trace
 ࢖ͬͯΈΑ͏ גࣜձࣾωΫετΧϨϯγʔ @nametake

  2. ࣗݾ঺հ

  3. @nametake

  4. Goྺ1೥ͪΐͬͱ

  5. ύϑΥʔϚϯεܭଌ

  6. runtime/pprof net/http/pprof

  7. package main import ( "log" "net/http" _ "net/http/pprof" "runtime" )

    func main() { runtime.SetBlockProfileRate(1) log.Println( http.ListenAndServe("0.0.0.0:6060", nil), ) }
  8. localhost:6060/debug/pprof/

  9. None
  10. None
  11. None
  12. None
  13. None
  14. $ go tool trace

  15. None
  16. ࢖͍ํઆ໌

  17. runtime/trace

  18. package main import ( "os" "runtime/trace" ) func main() {

    f, err := os.Create("trace.out") if err != nil { panic(err) } defer f.Close() err = trace.Start(f) if err != nil { panic(err) } defer trace.Stop() }
  19. $ go tool trace trace.out

  20. ϑΝΠϧॻ͖ࠐΈ

  21. f, err := os.Create("test.txt") if err != nil { panic(err)

    } defer f.Close() for i := 0; i < 100; i++ { fmt.Fprintf(f, "line number: %d\n", i) }
  22. None
  23. ॲཧ࣌ؒ

  24. ॲཧ࣌ؒ

  25. ϓϩάϥϜ͕֬อͨ͠ώʔϓྖҬ

  26. goroutineͷ਺ͱ࣮ߦ࣌ؒ

  27. OSͷThreadͷ਺ͱ࣮ߦ࣌ؒ

  28. Proc(CPU)Ͱॲཧ͍ͯ͠Δ಺༰

  29. ͳΜ͔৭ʑͳ৘ใ

  30. Goݴޠ͸ ׂΓͱؾܰʹ syscallΛݺͿ

  31. None
  32. syscallͷݺͼग़͠

  33. ͱͯ΋͋΍͍͠

  34. syscallΛݺΜͰΔ

  35. f, err := os.Create("test.txt") if err != nil { panic(err)

    } defer f.Close() for i := 0; i < 100; i++ { fmt.Fprintf(f, "line number: %d\n", i) }
  36. f, err := os.Create("test.txt") if err != nil { panic(err)

    } defer f.Close() for i := 0; i < 100; i++ { fmt.Fprintf(f, "line number: %d\n", i) }
  37. bufio

  38. f, err := os.Create("test.txt") if err != nil { panic(err)

    } defer f.Close() w := bufio.NewWriter(f) defer w.Flush() for i := 0; i < 100; i++ { fmt.Fprintf(w, "line number: %d\n", i) }
  39. None
  40. None
  41. None
  42. None
  43. None
  44. GUIͰ֬ೝग़དྷΔʂ

  45. GC

  46. None
  47. GC

  48. None
  49. GCຖʹ֬อ͠௚͍ͯ͠Δ

  50. n := 400000 list := []int{} for i := 0;

    i < n; i++ { list = append(list, i) }
  51. n := 400000 list := []int{} for i := 0;

    i < n; i++ { list = append(list, i) }
  52. n := 400000 list := make([]int, n) for i :=

    0; i < n; i++ { list[i] = i }
  53. None
  54. GC͕ൃੜ͍ͯ͠ͳ͍

  55. ࠷ॳʹΨοπϦྖҬ֬อ

  56. 6ms → 2ms

  57. ฒྻԽ

  58. https://making.pusher.com/go-tool-trace/ Connʹॻ͖ࠐΈ

  59. https://making.pusher.com/go-tool-trace/ ͜ͷ෦෼Ͱॻ͖ࠐΜͰ͍Δ

  60. https://making.pusher.com/go-tool-trace/

  61. https://making.pusher.com/go-tool-trace/ ଞͷϓϩηε࢖ͬͯͳ͍

  62. https://making.pusher.com/go-tool-trace/

  63. https://making.pusher.com/go-tool-trace/

  64. https://making.pusher.com/go-tool-trace/ CPUΛແବͳ͘ར༻

  65. ͍Ζ͍ΖΘ͔Δ

  66. શ෦ͷ໰୊͕ Θ͔ΔΘ͚Ͱ͸ͳ͍

  67. CPUͷ࢖༻཰͸ pprofͱ͔Λ ݟͨ΄͏͕͍͍

  68. ϓϩάϥϜͷ ྲྀΕΛ஌Δ

  69. ͱΓ͋͑ͣ ݟͯΈΔ͚ͩͰ΋ ͍͍͔΋

  70. go traceͷ͜ͱɺ ࣌ʑͰ͍͍͔Β……ɹ ࢥ͍ग़͍ͯͩ͘͠͞