- ์ด๋ถํ์
- LCs
- C++
- DFS
- ๋ค์ต์คํธ๋ผ
- ๋์ ํ๋ก๊ทธ๋๋ฐ
- ํ๋ก๊ทธ๋๋จธ์ค
- js
- ์์ฝ๋
- golang
- ๋นํธ๋งต
- Union-Find
- ์น๋ฆฐ์ด
- ์ฌ๋ผ์ด๋ฉ ์๋์ฐ
- ์นด์นด์ค2021
- ์๊ณ ๋ฆฌ์ฆ
- ์ํฐ๋
- nestjs
- DP
- ๋ฐฑ์๋ ํ๋ฆฌ์จ๋ณด๋ฉ
- ํธ๋ฆฌ
- Python
- BFS
- ๊ฐ์ฅ๊ฐ๊น์ด๊ณตํต์กฐ์
- ํ๋ฆฌ์จ๋ณด๋ฉ
- ๋นํธ๋ง์คํน
- go
- ๋ฐฑ์ค
- ์ฌ๊ท
- ์นด์นด์ค ์ฝํ
- Today
- Total
Hello Ocean! ๐ผ
[Go] ๋ฐ์ดํฐ ๊ฒฝํฉ์ ๊ฐ์งํด๋ณด์, Data Race Detector ๋ณธ๋ฌธ
์๋ก
๋์ ์ฒ๋ฆฌ ์์คํ (Concurrent System)์์ ๋ฐ์ดํฐ ๊ฒฝํฉ์ ๋งค์ฐ ํํ๊ณ , ๋๋ฒ๊น ํ๊ธฐ ํ๋ ๋ฌธ์ ์ด๋ค.
Go์์๋ ์ด๋ฐ ๋ฌธ์ ํด๊ฒฐ์ ๋๊ธฐ ์ํ built-in Data Race Detector๊ฐ ์กด์ฌํ๋ค! (์ผํธ๐)
์ด๋ป๊ฒ ์ฐ๋์?
์ฌ์ฉ๋ฐฉ๋ฒ์ ๋งค์ฐ ๋จ์ํ๋ค.
go command์ -race
ํ๋๊ทธ๋ฅผ ์ถ๊ฐํ๋ฉด ๋๋ค!
$ go test -race mypkg // to test the package
$ go run -race mysrc.go // to run the source file
$ go build -race mycmd // to build the command
$ go install -race mypkg // to install the package
race build tag๋ก ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ๊ธฐ
๋ชจ๋ ์ฝ๋์ ๋ชจ๋ ํ
์คํธ์ ๋ํด race detector๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋นํจ์จ์ ์ผ ์ ์๋ค.
๊ทธ๋ด ๋๋, race build tag๋ฅผ ์ฌ์ฉํ๋ฉด ์ข๋ค.
ํ์ผ ๋งจ ์ฒซ๋ฒ์งธ ๋ผ์ธ์ ์๋์ ๊ฐ์ด ์์ฑํด ํ์ฉํ ์ ์๋ค.
- ` // +build !race` : -race ํ๋๊ทธ๊ฐ ์์ ๋๋ง ๋น๋์ ํฌํจ๋จ
- race ๊ฒ์ฌ๊ฐ ๋ถํ์ํ ์ฝ๋/ํ ์คํธ
- ` // +build race` : -race ํ๋๊ทธ๊ฐ ์์ ๋๋ง ๋น๋์ ํฌํจ๋จ
- race ๊ฒ์ฌ๋ง์ ์ํ ์ฝ๋/ํ ์คํธ
์ด๋ป๊ฒ ์๋ ค์ฃผ๋์?
Race Decector๊ฐ race๋ฅผ ๋ฐ๊ฒฌํ๋ฉด, report๋ฅผ ์์ฑํด์ค๋ค.
report๋ ์๋์ ๋ด์ฉ๋ค์ ํฌํจํ๋ค.
- ๊ฒฝ๊ณ ๋ฉ์์ง:
- ์ฝ๊ธฐ ์์
์ ์ํํ goroutine์ ์คํ ํธ๋ ์ด์ค:
- ํจ์ ํธ์ถ๊ณผ ํ์ผ/๋ผ์ธ ์ ๋ณด ํฌํจ
- ์์:
net.(*pollServer).AddFD() src/net/fd_unix.go:89 +0x398
- ์ด์ ์ฐ๊ธฐ ์์
์ ์ํํ goroutine์ ์คํ ํธ๋ ์ด์ค:
- ํจ์ ํธ์ถ๊ณผ ํ์ผ/๋ผ์ธ ์ ๋ณด ํฌํจ
- ์์:
net.setWriteDeadline() src/net/sockopt_posix.go:135 +0xdf
- ์ฝ๊ธฐ ์์
์ ์ํํ goroutine์ ์์ฑ ์์น:
- ํจ์ ํธ์ถ๊ณผ ํ์ผ/๋ผ์ธ ์ ๋ณด ํฌํจ
- ์์:
net.func·061() src/net/timeout_test.go:609 +0x288
- ์ฐ๊ธฐ๋ฅผ ์ํํ goroutine์ ์์ฑ ์์น:
- ํจ์ ํธ์ถ๊ณผ ํ์ผ/๋ผ์ธ ์ ๋ณด ํฌํจ
- ์์:
net.TestProlongTimeout() src/net/timeout_test.go:618 +0x298
report ์์
==================
WARNING: DATA RACE
Write at 0x00c000098180 by goroutine 6:
runtime.mapaccess2_faststr()
/opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/map_faststr.go:108 +0x42c
main.main.func1()
/Users/bada/Projects/Concurrency-in-Go/main.go:67 +0x48
Previous write at 0x00c000098180 by main goroutine:
runtime.mapaccess2_faststr()
/opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/map_faststr.go:108 +0x42c
main.main()
/Users/bada/Projects/Concurrency-in-Go/main.go:70 +0xf4
Goroutine 6 (running) created at:
main.main()
/Users/bada/Projects/Concurrency-in-Go/main.go:66 +0xd8
==================
Found 1 data race(s)
report๋ฅผ ํตํด ๋ฐ์ดํฐ ๊ฒฝํฉ ๋ฌธ์ ๊ฐ ์ด๋์์ ์ด๋ป๊ฒ ๋ฐ์ํ๊ณ ์๋์ง ํ์ ํ ์ ์๋ค.
๋ค์ํ report๊ด๋ จ option๋ค
GORACE
ํ๊ฒฝ๋ณ์๋ฅผ ํตํด race detector์ ์ต์
๋ค์ ์ค์ ํ ์ ์๋ค.
์ฌ์ฉ ๊ฐ๋ฅํ ์ต์ ๋ชฉ๋ก์ ์๋์ ๊ฐ๋ค.
log_path
(default : stderr)- report ์์ฑ ์์น
stderr
,stdout
๋ก ์ค์ ํ๋ฉด ํ์ค์๋ฌ, ํ์ค์ถ๋ ฅ์ผ๋ก ์์ฑ๋จ- ํน์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์
๋ ฅํ๋ฉด
file_name.pid
ํ์์ ํ์ผ์ ์์ฑ๋จ
exitcode
(default : 66)- ๊ฒฝํฉ์ํ ๊ฐ์ง ํ ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃ๋ ๋ ์ฌ์ฉํ exit status๋ฅผ ์ค์
strip_path_prefix
(default : "")- report์ ํฌํจ๋ ๋ชจ๋ ํ์ผ ๊ฒฝ๋ก์์ ์๋ตํ prefix ์ง์
history_size
(default : 1)- ๊ฐ goroutine์ ๋ฉ๋ชจ๋ฆฌ ์ ๊ทผ ํ์คํ ๋ฆฌ๋ 32K * 2**history_size ๋ก ๊ตฌ์ฑ๋จ
- ์ด ๊ฐ์ ์ฆ๊ฐ์ํค๋ฉด
์คํ์ ๋ณต์ํ์ง ๋ชปํจ(failed to restore the stack)
์๋ฌ๋ฅผ ํผํ ์ ์์ง๋ง, ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ์ฆ๊ฐํจ
halt_on_error
(default : 0)- ์ฒซ ๋ฒ์งธ race๋ฅผ ํ์งํ๊ณ ๋ฐ๋ก ์ข ๋ฃํ ๊ฒ์ธ์ง, ์ด์ด์ ๊ณ์ ํ์งํ ๊ฒ์ธ์ง ์ค์
- 0: ์ข ๋ฃํ์ง ์์ / 1: ์ข ๋ฃํจ
atexit_sleep_ms
(default : 1000)- ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃ๋๊ธฐ ์ ์ main goroutine์์ ๋๊ธฐํ ์๊ฐ(ms)์ ์ค์
์ฌ์ฉ ์์
GORACE="log_path=/tmp/race/report strip_path_prefix=/my/go/sources/" go test -race
Test๋ฅผ ํตํ raceํ์ง์ ํ๊ณ์ ํด๊ฒฐ๋ฐฉ๋ฒ
go test -race๋ฅผ ์ฌ์ฉํ์ฌ race detector๋ฅผ ์คํํ ๊ฒฝ์ฐ, ์คํ์ค์ ๋ฐ์ํ๋ race๋ง ์ฐพ์ ์ ์๋ค.
(== ์คํ๋์ง ์๋ ์ฝ๋์์ ๋ฐ์ํ๋ ๋ ์ด์ค๋ ์ฐพ์ ์ ์์)
๋ฐ๋ผ์, test coverage๊ฐ ์ถฉ๋ถํ ๋์ง ์์ ๊ฒฝ์ฐ์๋ ,-race
ํ๋๊ทธ๋ก buildํ ๋ฐ์ด๋๋ฆฌ๋ฅผ ์ค์ ์ ์ ์ฌํ ํ๊ฒฝ
์์ ์คํํ๋ฉด ๋ ๋ง์ race๋ฅผ ํ์งํ ์ ์๋ค.
Runtime Overhead
race detector๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋๊ณผ ์คํ ์๊ฐ์ด ํฌ๊ฒ ์ฆ๊ฐํ ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ race detector๋ก ์ธํ ๋ฉ๋ชจ๋ฆฌ ์ฆ๊ฐ๋ runtime.ReadMemStats
๋ runtime/pprof
์ ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ํ๋กํ์ผ๋ง ๋๊ตฌ์์๋ ํ์ธํ ์ ์๊ธฐ ๋๋ฌธ์ ์ฃผ์ํด์ ์ฌ์ฉํด์ผ ํ๋ค.
- ์ผ๋ฐ์ ์ธ ํ๋ก๊ทธ๋จ์ ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด 5๋ฐฐ ~ 10๋ฐฐ ์ฆ๊ฐํ ์ ์๊ณ , ์คํ ์๊ฐ์ 2๋ฐฐ ~ 20๋ฐฐ ์ฆ๊ฐํ ์ ์์
- race dector๋
defer
๋ฐrecover
๋ฌธ๋ง๋ค ์ถ๊ฐ๋ก 8๋ฐ์ดํธ๋ฅผ ํ ๋นdefer
๋ฐrecover
๊ฐ ๋ง์ด ์ฌ์ฉ๋๋ ํ๋ก๊ทธ๋จ์ ๋ ๋ง์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ฒ ๋จ
- ์ด๋ฌํ ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ๊ณ ๋ฃจํด์ด ์ข
๋ฃ๋๊ณ ๋์ ํ์๋จ
- ๊ณ ๋ฃจํด์ด ์ค๋ซ๋์ ์คํ๋ ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๊ณ์ ์ฆ๊ฐํ ์ ์์
==> ์ฅ๊ธฐ๊ฐ ์คํ๋๋ ๊ณ ๋ฃจํด์ด ์ฃผ๊ธฐ์ ์ผ๋กdefer
์recover
ํธ์ถ์ ํ๋ ๊ฒฝ์ฐ, ํ๋ก๊ทธ๋จ์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๋ฌดํํ ์ฆ๊ฐํ ์ ์์ผ๋ฏ๋ก ์ฃผ์ํ์.
- ๊ณ ๋ฃจํด์ด ์ค๋ซ๋์ ์คํ๋ ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๊ณ์ ์ฆ๊ฐํ ์ ์์
์๋ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ ํฌ์คํ ์ ๋๋ค.
https://go.dev/doc/articles/race_detector
'Go' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Go] go-socket.io์ Redis Adapter (0) | 2022.03.03 |
---|