ProfilingGo ApplicationGo Conference 2018 Autumn #gocon #gocon_a@orisano
View Slide
ࣗݾհ• @orisano• ۦ͚ग़͠ΞϓϦέʔγϣϯΤϯδχΞ• ޮతͳdocker buildΛࢦ͍ͯ͠Δ• ύϑΥʔϚϯεʹڵຯ͕͋Δ
ࠓ͢͜ͱ• ߴԽ, ϓϩϑΝΠϦϯάΛߦ͖࣮ͬͯͨྫ• Ͳ͏͍͏վળΛ͢Δͷ͔• Ͳ͏͍͏ϓϩηεͳͷ͔
ը૾มαʔόͷkubeletͷgo-swaggerͷ
ৄղγεςϜɾύϑΥʔϚϯεྠಡձ
ࡐͱͯ͠؆୯ͳը૾มαʔόΛѻͬͨ
ෛՙΛ͔͚ΔͱΞϓϦέʔγϣϯ͕མͪͨ
topΛΈͨ
མͪͨݪҼϝϞϦͷ༻ྔ
ϝϞϦϦʔΫʁ
pprofΛ͏GODEBUG=allocfreetrace=1͢Δ
runtimeʹखΛೖΕΔ
GC͍ͯ͠Δͱ͖ʹࢀরgraphΛdump͢ΔΑ͏ʹͨ͠
ͪΌΜͱpprofͷmemorygoroutineͷΛݟΔ
ಉ࣌มʹ੍ݶΛ͚ͭΔ
ແࣄղܾͨ͠
͋ͱߴԽ
disintegration/imaging
Boundary CheckElimination
GoجຊఴࣈΞΫηε͢ΔͱൣғνΣοΫ͕ೖΔ
ίϯύΠϥ͕҆શͩͱஅ͢Δͱফ͑Δ
͜ΕΛBoundary CheckElimination (BCE)ͱ͍͏
͜ΕΛBoundary CheckElimination (BCE)ͱ͍͏ref: https://go101.org/article/bounds-check-elimination.html
goͷඪ४ϥΠϒϥϦͰͦͦ͜͜ݟΔ
ը૾ͷಡΈࠐΈ, มͳͲheavyʹఴࣈΞΫηε͢Δ߹ͩͱޮՌ͕େ͖͍
https://github.com/disintegration/imaging/pull/81
pprofͳͲͰݪҼΛಛఆͨ͠Β
·ͣBenchmarkΛॻ͜͏*testing.B
Benchmarkͷ݁ՌΛͪΌΜͱݟΔ
golang.org/x/tools/cmd/benchcmpgolang.org/x/perf/cmd/benchstat
kubeletͷCPU͕ෛՙ͕ߴ͍ͱ૬ஊΛड͚ͨ
Datadog͕ୟ͍ͯΔMetrics༻ͷEndpoint͕ݪҼͳͷͰͱ͍͏ͩͬͨ
kubeletnet/http/pprofΛimport͍ͯ͠Δ
͕ى͖͍ͯΔڥͷใ͕औͬͯ͜ΕΔ
prometheus/commongoogle/cadvisorͷҰ෦͕ॏ͍
֘Օॴhttps://github.com/prometheus/common/pull/148ൃ֮ͷ1िؒલ͘Β͍ʹallocation freeʹͳͬͯ2ഒ͘Β͍ૣ͘ͳ͍ͬͯͨ
ͳΜ͔չ͔ͬͨ͠ͷͰhttps://github.com/prometheus/common/pull/150ߋʹͦΕΛ2ഒ͘Β͍ૣ͘͠·ͨ͠
७ਮʹWrite͕ଟ͔ͬͨ
strings.ReplacerʹWriteString͕͋Γ·͢
ஔରՕॴҎ֎·ͱΊͯWriteͯ͘͠ΕΔ
allocationى͜Βͳ͍ͷͰ࠷ߴ
࣍ʹ
google/cadvisorͷListContainers͕ॏ͍
google/cadvisorͷListContainers͕ॏ͍https://github.com/google/cadvisor/blob/8faf19092784b75fce10ce190d37e66f89de3612/container/common/helpers.go
ioutil.ReadDirͱsyscall͕ॏ͍ʁ
ioutil.ReadDirͷதΛݟͯΈΔ
ref: https://golang.org/src/io/ioutil/ioutil.go
ref: https://golang.org/src/os/dir_unix.go
ͳΜ͔ҎԼʹ͋ΔϑΝΠϧશ෦ʹlstatൃߦ͍ͯ͠Δ
readdirent͕ͬͯͭ͋Δ
Ͳ͏Β͜ΕentryͷtypeΛฦͯ͘͠ΕΔΒ͍͠
readdirentΛ͏·͑͘ΕsyscallͷճΛݮΒͤΔ
͔͠͠golangඪ४ͷreaddirenttypeΛऔಘͰ͖ͳ͍
͔࣮͠ʹඞཁͳॾʑ͕privateʹͳ͍ͬͯΔ
ඪ४͔Βͦ͜Λίϐϖ͢ΔͱcadvisorͷLICENSE Checkʹ௨Βͳ͍
࣮͜ͷํ๏͍ΖΜͳϥΠϒϥϦͰΘΕ͍ͯΔgoimports/internal/fastwalk֎෦͔Βར༻Ͱ͖ͳ͍
ѹతʹ֎෦͔Βར༻Ͱ͖ͳ͍
github.com/karrick/godirwalkΑ͏͘ݟ͚ͭͨ
https://github.com/google/cadvisor/pull/2085
Memoryܥͷऔಘ͚ͩͨΒ͍
https://lkml.org/lkml/2018/7/3/101cgroups v1ܥͷΒ͍͠kernelͷversionΛ͋͛Α͏
ࣗಈੜΛ݁ߏ͍ͬͯΔͷͰ͕͢
go-swagger͕͍
ૣ͘͠Α͏
cliܥͷϓϩϑΝΠϦϯάʹpkg/profile͕ศར
https://github.com/go-openapi/swag/pull/25
෦ͰຖճMustCompileͯͨ͠
https://github.com/go-openapi/loads/pull/19
͍json.UnmarshalΛdeepcopyͷͨΊʹ2ճͯͨ͠
json.Unmarshal 1ճ{de,en}coding/gobͰ͓Λͨ͠
·ͱΊ
ීஈ͍ͬͯΔͷͰΧϦΧϦʹνϡʔχϯά͞Ε͍ͯΔΘ͚Ͱͳ͍
͕͍ࣗͱײͨ͡Βܭଌͯ͠ΈΔվળͯ͠ΈΔ
cliͳΒpkg/profileΛࠐΜͰݟΔ
serverͳΒnet/http/pprofΛ͏
ݪҼͷಛఆͷखॿ͚ͷͨΊʹsvgʹͨ͠Γuber/go-torchΛͬͯflamegraphʹͨ͠ΓflamescopeΛͬͨΓ
ݪҼΛಛఆͨ͠ΒBenchmarkΛॻ͘
-count=10ͳͲΛͬͨ͋ͱʹbenchcmp or benchstatͯ͠ޮՌ͕ग़͍ͯΔ͜ͱΛ֬ೝ
͋ͱPRΛ͛Δ
͜ΜͳʹύϑΥʔϚϯενϡʔχϯά͍͢͠ݴޠଞʹͳ͍
͍ͬͯ͜͏
ඞཁΛײ͡Δ·Ͱνϡʔχϯά͠ͳ͍
Ͱܭଌ͕Ͱ͖Δڥ༻ҙ͖͢