More Related Content
Similar to Gunosy_go_8_runtime (20)
Gunosy_go_8_runtime
- 4. runtime / Constants
• クロスコンパイル
例えば、macでlinuxやwindows用にコンパイルできます。
% brew install go –cross-compile-all
• linux
% GOOS=linux GOARCH=amd64 go build hello.go
• windows
% GOOS=windows GOARCH=amd64 go build hello.go
- 5. runtime / func Breakpoint
• ブレイクポイントの例
http://play.golang.org/p/-l2BnhsGxZ
- 6. runtime/func BreakPoint
go run hello.go
SIGTRAP: trace trap
PC=0x266c1
goroutine 16 [running]:
runtime.breakpoint()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/asm_amd64.s:113 +0x1
fp=0x220832bed0 sp=0x220832bec8
runtime.Breakpoint()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/proc.c:2015 +0x1a fp=0x220832bed8
sp=0x220832bed0
main.main()
/Users/masahiroyanai/gocode/test_code/hello.go:9 +0x1e fp=0x220832bf50
sp=0x220832bed8
runtime.main()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/proc.c:247 +0x11a fp=0x220832bfa8
sp=0x220832bf50
runtime.goexit()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/proc.c:1445 fp=0x220832bfb0
sp=0x220832bfa8
created by _rt0_go
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/asm_amd64.s:97 +0x120
goroutine 17 [runnable]:
runtime.MHeap_Scavenger()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/mheap.c:507
runtime.goexit()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/proc.c:1445
goroutine 18 [runnable]:
bgsweep()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/mgc0.c:1976
runtime.goexit()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/proc.c:1445
goroutine 19 [runnable]:
runfinq()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/mgc0.c:2606
runtime.goexit()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/proc.c:1445
rax 0x20817a120
rbx 0x208190180
rcx 0x20817a120
rdx 0x0
rdi 0x220817a000
rsi 0x208178160
rbp 0x0
rsp 0x220832bec8
r8 0x41
r9 0x16
r10 0x220832bdd8
r11 0x206
r12 0x8e98f1a0b590
r13 0x9326e5a20950
r14 0x1384cfc196147c00
r15 0x0
rip 0x266c1
rflags 0x202
cs 0x2b
fs 0x0
gs 0x8170000
exit status 2
- 7. runtime/func Callers
func Caller(skip int) (pc uintptr, file string, line int, ok bool)
• 実行中の行数とファイル名を取得する方法
引数の1はスタックをさかのぼる数です。
http://play.golang.org/p/hELH1OEPTo
• 現在のファイル名とディレクトリを取得する方法
http://play.golang.org/p/EQezfIvN-F
もともと、__FILE__や__DIR__がないのは、コンパイルを早くするため
だそうです。なので、欲しい場合は自分で実装します。
- 8. runtime / func GOMAXPROCS
• GOMAXPROCSは、同時に最大で実行可能な
CPU数をセットし、前に設定していた値を返し
ます。
http://play.golang.org/p/BJZ7nvhJou
- 9. runtime / func GOROOT
• Goツリーのrootを返します。
http://play.golang.org/p/XRwJ1i-JnQ
go run go_root.go
GOROOT : /usr/local/opt/go/libexec
- 10. runtime / func Goexit
• 呼び出したgoroutineを終了します。
http://play.golang.org/p/mLhLS31ofc
- 11. runtime / func GoroutineProfile
• 起動中のgoroutineのprofileを取得します。
http://play.golang.org/p/CvcNDiFl4G
• 下記はローカルで実行した結果
- 12. runtime / func Gosched
• 現在、動いてるgorutineが動いてるときに他
のgorutineが動けるようにする。でも、現在、
動いてるgorutineは中断されて止まることは
ない。
http://play.golang.org/p/Ovtx28IavM
- 13. runtime / func LockOSThread
• LockOSThreadは、オペレーティングシステムのカレントスレッ
ドと、この関数を呼び出したゴルーチンを関連付けます。呼
び出したゴルーチンが終了するか、もしくはUnlockOSThread
が呼び出されるまで、常にそのスレッド内で実行されます。
一方、他のゴルーチンはそのスレッド内では実行されなくなり
ます。LockOSThreadは、init関数内では使用できません。
http://play.golang.org/p/Q2paT00GNX
- 14. runtime / NumCPU
• 実行してるマシンのCPUのコア数を返す
• 下記みたいに、コア数を取得してセットするみ
たいな使い方かな
http://play.golang.org/p/YDLGX7yEwB
- 16. runtime / func ReadMemStats
goroutine: 100000
cpu: 4
time: 0.433748
memory all: 29.155067 MB
memory: 305.713040 byte/goroutine
http://play.golang.org/p/CAdFKJrh9v
- 17. runtime / func Version
• バージョンを取得できます。
http://play.golang.org/p/qVT6QpOFcL
- 18. runtime/ type Func
• func FuncForPC
リフレクションを使って関数名を取得するには?
http://play.golang.org/p/Nx2bs8ymen
http://play.golang.org/p/BzcgKYUBSQ
Editor's Notes
- まず、runtimeパッケージの概要をざっとお話します。
- 次にruntimeの定数について話します。 ごーおーえすとごーあーちについて、クロスコンパイルで使います
- ところどころ、説明を省きつつ、気になったところを話していきます
BreakPoint関数について playgroundで見てみましょう
- ローカルで実行するとこんな感じです。
- 引数の1はスタックをさかのぼる数です。
- Palygroundでやると、sandoboxのパスがでてきて、macのローカルでやるとhomebrewでインストールしたパスがでます
- イメージこんな感じです。
- サンドボックスではこんな感じ
ローカルで実行するといい感じでCPU使ってくれます
- コメントアウトして、あるときとないときを比べると出力の順番が変わることがわかる
- なんで、これだけ長いw サンプルコードが役にたつかわからないが、とりあえず
- サンドボックスのCPUのコア数は1なのかな
ローカルの4コアのMacでやると4 4 でした