How to profile memory usage in Golang - cloud-barista/cb-spider GitHub Wiki

[๋ชฉ์ฐจ]

  1. ํ”„๋กœํŒŒ์ผ๋ง ๊ฐœ์š”
  2. ํ”„๋กœํŒŒ์ผ๋ง ํ”„๋กœํŒŒ์ผ๋ง ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๋ฐ ๋ฐฉ๋ฒ•
  3. ํ”„๋กœํŒŒ์ผ๋ง ๊ฒฐ๊ณผ ์š”์•ฝ
  4. ์‹œํ—˜ ์ผ€์ด์Šค๋ณ„ ํ”„๋กœํŒŒ์ผ๋ง ๊ฒฐ๊ณผ Snapshot
  5. ํ”„๋กœํŒŒ์ผ๋ง ๋ถ„์„ ๊ฒฐ๊ณผ
  6. ํ”„๋กœํŒŒ์ผ๋ง ๋ถ„์„ ๊ฒฐ๋ก  ๋ฐ ๊ณ„ํš

1. ํ”„๋กœํŒŒ์ผ๋ง ๊ฐœ์š”

2. ํ”„๋กœํŒŒ์ผ๋ง ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๋ฐ ๋ฐฉ๋ฒ•

  • ํ”„๋กœํŒŒ์ผ๋ง์„ ์œ„ํ•œ pprof ์ฝ”๋“œ ์ ์šฉ ํ•„์š”
    • ์ ์šฉ ๋Œ€์ƒ ์†Œ์Šค: cb-spider/api-runtime/apiserver.go

    • ์ ์šฉ ์ฝ”๋“œ ์—์‹œ

       11 import (
            ... ์ƒ๋žต
       24
       25         ///////////////// for profiling
       26         "log"
       27         "net/http"
       28         _ "net/http/pprof"
       29 )
       30
       31 func main() {
       32         ///////////////// for profiling
       33         go func() {
       34                 log.Println(http.ListenAndServe("1.2.3.4:8282", nil))
       35         }()
       36         wg := new(sync.WaitGroup)
       37         wg.Add(1)
       38         ///////////////// for profiling
       39
       40         rootCmd := NewRootCmd()
       41         if err := rootCmd.Execute(); err != nil {
       42                 fmt.Printf("cb-spider terminated with error: %v\n", err)
       43         }
       44         ///////////////// for profiling
       45         wg.Wait()
       46 }
            ... ์ดํ•˜ ์ƒ๋žต
      
  • ์ค€๋น„ ํ™˜๊ฒฝ
    • ๋ณธ ์‹œํ—˜ ํ™˜๊ฒฝ: Ubuntu 18.04, go 1.16.5
    • Graph ์ถœ๋ ฅ ์ง€์› ๋„๊ตฌ dot, gv ์„ค์น˜
      sudo apt-get install -y graphviz gv
      
  • ํ”„๋กœํŒŒ์ผ๋ง ์ ˆ์ฐจ
    • pprof๋ฅผ ์ ์šฉํ•œ CB-Spider ์„œ๋ฒ„ ๊ฐ€๋™
    • Test Case ์‹คํ–‰์œผ๋กœ ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ ๋ณ€ํ™” ์œ ๋„
    • Browser ์ด์šฉํ•œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ํ˜„ํ™ฉ ์ฐธ๊ณ 
    • ๋ฉ”๋ชจ๋ฆฌ ํ˜„ํ™ฉ snapshot ์ด๋ฏธ์ง€ ๋ฝ‘๋Š” ๋ฐฉ๋ฒ•
      go tool pprof -png http://1.2.3.4:8282/debug/pprof/heap > data.png
      
  • ํ”„๋กœํŒŒ์ผ๋ง ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค

    • [1] Spider Server start ํ›„ ์ธก์ •
    • [2] AWS:VPC/Subnet, SecurityGroup, KeyPair ๋ฐ VM 1๊ฐœ ์ƒ์„ฑ,    Azure:VPC/Subnet, SecurityGroup, KeyPair ๋ฐ VM 1๊ฐœ ์ƒ์„ฑ ํ›„ ์ธก์ •
    • [3] AWS:ListImage ์‹คํ–‰ ํ›„ ์ธก์ •
    • [4] Azure:ListImage ์‹คํ–‰ ํ›„ ์ธก์ •
    • [5] Azure:ListImage ์‹คํ–‰ ํ›„ ์ธก์ •
    • [6] Azure:ListImage ์‹คํ–‰ ํ›„ ์ธก์ •
    • [7] ๋ชจ๋“  ์ž์›์„ ์‚ญ์ œ ํ›„ ์ธก์ •
    • [8] drop caches ํ›„ ์ธก์ •
    • [9] Spider Server stop ํ›„ ์ธก์ •

    โ€ป [2], [7]: CB-Spider REST API(Echo) ํ™œ์šฉ โ€ป [3] ~ [6]: CB-Spider Go API(gRPC) ํ™œ์šฉ โ€ป ์œ„์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์‹œํ—˜ ์ผ€์ด์Šค ์‹คํ–‰์‹œ ๋งˆ๋‹ค ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ํ˜„ํ™ฉ ํ”„๋กœํŒŒ์ผ๋ง

3. ํ”„๋กœํŒŒ์ผ๋ง ๊ฒฐ๊ณผ ์š”์•ฝ

  [1] [2] [3] [4] [5] [6] [7] [8]
Mem Usages 11.7MB 13.416MB 63MB 37MB 13MB 38MB 13.8MB 13.8MB
     
Profile Count Count Count Count Count Count Count Count
allocs 41 41 1532 2236 2469 2596 2614 2614
block 0 0 0 0 0 0 0 0
cmdline 0 0 0 0 0 0 0 0
goroutine 12 12 17 15 11 16 9 10
heap 41 41 1532 2236 2469 2596 2614 2614
mutex 0 0 0 0 0 0 0 0
profile 0 0 0 0 0 0 0 0
threadcreate 20 20 20 20 20 20 20 20
trace 0 0 0 0 0 0 0 0

Profile Description:

  • allocs: A sampling of all past memory allocations block: Stack traces that led to blocking on synchronization primitives
  • cmdline: The command line invocation of the current program
  • goroutine: Stack traces of all current goroutines
  • heap: A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.
  • mutex: Stack traces of holders of contended mutexes
  • profile: CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.
  • threadcreate: Stack traces that led to the creation of new OS threads
  • trace: A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.

4. ์‹œํ—˜ ์ผ€์ด์Šค๋ณ„ ํ”„๋กœํŒŒ์ผ๋ง ๊ฒฐ๊ณผ Snapshot

  • [1] Spider Server start ํ›„ ์ธก์ •

image

  • [2] AWS:VPC/Subnet, SecurityGroup, KeyPair ๋ฐ VM 1๊ฐœ ์ƒ์„ฑ,    Azure:VPC/Subnet, SecurityGroup, KeyPair ๋ฐ VM 1๊ฐœ ์ƒ์„ฑ ํ›„ ์ธก์ •

image

  • [3] AWS:ListImage ์‹คํ–‰ ํ›„ ์ธก์ •

image

  • [4] Azure:ListImage ์‹คํ–‰ ํ›„ ์ธก์ •

image

  • [5] Azure:ListImage ์‹คํ–‰ ํ›„ ์ธก์ •

image

  • [6] Azure:ListImage ์‹คํ–‰ ํ›„ ์ธก์ •

image

  • [7] ๋ชจ๋“  ์ž์›์„ ์‚ญ์ œ ํ›„ ์ธก์ •

image

  • [8] drop caches ํ›„ ์ธก์ •

    • [1] ~ [7] ๋™์•ˆ ์‹œ์Šคํ…œ ๋ฉ”๋ชจ๋ฆฌ ๋ณ€ํ™”: ๋ชจ๋“  ์‹œํ—˜ ๋™์•ˆ GB ๋‹จ์œ„์˜ ๋ฉ”๋ชจ๋ฆฌ ๋ณ€ํ™”๋Š” ์—†์Œ, ์ปค๋„ ํŒŒ์ผ ์บ์‹œ๋‚˜ ๋ฒ„ํผ๋กœ ์ด๋ฏธ 26GB๊ฐ€ ์‚ฌ์šฉ์ค‘์ด๋ฉฐ, ํ˜„์žฌ ์‹œํ—˜๊ณผ๋Š” ์ง์ ‘์ ์ธ ์˜ํ–ฅ์€ ์—†์Œ.

      image

    • ์ด๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด cache๋ฅผ drop ์‹œํ‚จ ํ›„ ๋‹ค์‹œ ์ธก์ • ํ•ด๋ด„.

      • โ€ป drop caches ํ›„ cache๋Š” ๋ฐ˜ํ™˜ ๋˜๊ณ , Spider ํ”„๋กœ์„ธ์Šค๋Š” GC ๋“ฑ์˜ ๋ณ€ํ™” ์—†์ด ๊ทธ๋Œ€๋กœ 13.8MB๊ฐ€ ์œ ์ง€๋จ(์•„๋ž˜ ํ”„๋กœํŒŒ์ผ๋ง snapshot ์ฐธ๊ณ ).

      image

    • ํ”„๋กœํŒŒ์ผ๋ง snapshot์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Œ image

  • [9] Spider Server stop ํ›„ ์ธก์ •

    • Spider ์ข…๋ฃŒ์‹œ profile ์„œ๋ฒ„ ํ•จ๊ป˜ ์ข…๋ฃŒ๋กœ ํ”„๋กœํŒŒ์ผ๋ง ์ธก์ • ๋ถˆ๊ฐ€

    • Spider Server ์ข…๋ฅ˜ ์ „-ํ›„ ๋ฉ”๋ชจ๋ฆฌ ์ฐจ์ด: 49MB

      image

5. ํ”„๋กœํŒŒ์ผ๋ง ๋ถ„์„ ๊ฒฐ๊ณผ

  • ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด(63MB) ์‚ฌ์šฉํ•˜๋Š” [3] ์‹œ๋‚˜๋ฆฌ์˜ค ํ˜„ํ™ฉ(์•„๋ž˜ ๊ทธ๋ฆผ, [3] ํ”„๋กœํŒŒ์ผ๋ง Snapshot ์ฐธ๊ณ )์ด ์•„๋ž˜์™€ ๊ฐ™์ด AdminWeb์˜ ImageList ์›น์„œ๋น„์Šค์—์„œ ์†Œ๋ชจ๋˜๊ณ  ์žˆ์Œ. (Image ๋ชฉ๋ก: 9,465๊ฐœ)
  • [3] ์‹œ๋‚˜๋ฆฌ์˜ค ์ˆ˜ํ–‰ ์ดํ›„ ImageList ๊ด€๋ จ ์‚ฌ์šฉํ–ˆ๋˜ 50MB ๋ฉ”๋ชจ๋ฆฌ ์ฒด์ธ์ด ์ •์ƒ์ ์œผ๋กœ ์‚ฌ๋ผ์ง.([4]-[8] ํ”„๋กœํŒŒ์ผ๋ง snapshot ์ฐธ๊ณ )

โ€ป ์ฐธ๊ณ : AWS:Oregon Region์˜ Image ๋ชฉ๋ก ์ถœ๋ ฅ ํ›„ ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์ƒํƒœ

   (Operation ์‹คํ–‰ ํ›„ ๊ฒฐ๊ณผ์ด๊ธฐ๋„ ํ•˜๊ณ , GC ์˜ํ–ฅ์œผ๋กœ ์ตœ๊ณ ์ ์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ํ˜„ํ™ฉ์€ ์•„๋‹ ์ˆ˜ ์žˆ์Œ)

image

โ€ป ๊ด€๋ จ ์ •๋ณด: Image ๋ชฉ๋ก 9,465๊ฐœ ==> HTML source ํฌ๊ธฐ๋งŒ: 10.4MB

   => 10.4MB ๋ฐ์ดํ„ฐ ๊ทœ๋ชจ์— 50MB ํ™œ์šฉ์€ ๊ฐœ์„  ํฌ์ธํŠธ๋Š” ์žˆ๊ฒ ์ง€๋งŒ, GB ๋‹จ์œ„ ์ดํ•˜์ด๋ฏ€๋กœ ํ˜„์žฌ๋Š” Pass

image

    ... ์ค‘๊ฐ„ ์ƒ๋žต

image

6. ํ”„๋กœํŒŒ์ผ๋ง ๋ถ„์„ ๊ฒฐ๋ก  ๋ฐ ๊ณ„ํš

  • ๊ฐ€์žฅ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ํ™œ์šฉํ•˜๋Š” ์‹œ๋‚˜๋ฆฌ์˜ค๋กœ ์žฌํ˜„์„ ์‹œ๋„ํ–ˆ์ง€๋งŒ, Criticalํ•œ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋Š” ์žฌํ˜„๋˜์ง€ ์•Š์Œ.
    • ๊ฐ€๋Šฅ์„ฑ์€ ํฌ๋ฐ•ํ•˜์ง€๋งŒ, ๊ทธ๊ฐ„ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ๋ฐ ๊ฐœ์„ ์— ์˜ํ•ด ์ž์—ฐ ํ•ด๊ฒฐ ๋˜์—ˆ์„ ์ˆ˜๋„...
  • ์‹œ๊ธฐ์ƒ ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ/๊ฐœ์„  ์šฐ์„  ์ถ”์ง„, ์ถ”๊ฐ€ ๋ถ„์„ ์ง„ํ–‰ํ•˜์ง€ ์•Š๊ณ  ๋งˆ๋ฌด๋ฆฌํ•จ.
  • ํ–ฅํ›„ ์—ฐ๊ด€ ์ด์Šˆ ๋ฐœ์ƒ์‹œ ์ถ”๊ฐ€ ์ƒ์„ธ ๋ถ„์„ ๋ฐ ๊ฐœ์„  ์ถ”์ง„, ๋˜๋Š”, ์•ˆ์ •ํ™” ์‹œ์ ์— ํ•„์š”์‹œ ์žฌ๋ถ„์„

โ€ป ์ฐธ๊ณ : Go GC(Garbage Collector) ์ •์ฑ…์— ์˜ํ•ด ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ๋Š” ์œ ๋™์ ์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋กœ ์ธํ•˜์—ฌ [1] ~ [8] ์‹œํ—˜ ํ›„ snapshot์ด ๊ฐ ์‹œํ—˜ ์ผ€์ด์Šค์˜ ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ๋ฅผ ๋Œ€ํ‘œํ•  ์ˆ˜๋Š” ์—†์Œ