LoginSignup
10
10

More than 3 years have passed since last update.

go runtime

Last updated at Posted at 2015-01-18

golang runtime

1.2ぐらいの話だったはず。
http://www.slideshare.net/masahiroyanai/gunosy-go-8runtime
この話です。
下書き一覧にあったのでと投稿します

Packages

http://golang.org/pkg/runtime/
http://golang.org/pkg/runtime/cgo/
http://golang.org/pkg/runtime/debug/
http://golang.org/pkg/runtime/pprof/
http://golang.org/pkg/runtime/race/

  • runtime

goroutineを制御する関数などランタイムシステムと対話できるパッケージです。

  • runtime/cgo

GoからCの関数/型にアクセスするために用いるパッケージです。
cgoを使えば、GoからCのコードが呼べます。

  • runtime/debug

実行中のプログラムをデバッグするためのパッケージです。

  • runtime/pprof

実行中のプログラムで時間がかかる部分を特定するのに使います。
https://code.google.com/p/gperftools/

  • runtime/race

データのコンフリクトによってクラッシュした原因とかを調査するパッケージです。
http://golang.org/doc/articles/race_detector.html

runtime

Constants

  • Compiler gcかgccか選べます

GOARCH, GOOS
クロスコンパイルの時に使います。
例えばlinux

GOOS=linux GOARCH=amd64 go build hello.go

Variables

MemProfileRate
メモリプロファイルに記録・報告されるメモリアロケーションの割合を制御します。
全てプロファイルする場合は1、無視する場合は0を設定します。
また、testingで
-memprofile メモリプロファイルを出力
-memprofilerate runtime.MemProfileRate の値を指定

func BlockProfile

http://golang.org/pkg/runtime/pprof/
http://golang.org/pkg/testing/

で使います
-blockprofile goroutine のブロックプロファイルを出力
-blockprofilerate runtime.SetBlockProfileRate の値を指定

func Breakpoint

ブレイクポイントを設定します
例えば、ブレイクポイントを設定して

package main

import (
  "fmt"
  "runtime"
)

func main() {
  runtime.Breakpoint()
  fmt.Println("Hello world!!")
}

実行すると

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

こんな感じ

func CPUProfile

runtime/pprofで使ってるサンプルがおおいから、そっち側でかな。

func Caller

実行中の行数とファイル名を取得します。
引数の1はスタックをさかのぼる数です。

現在のファイル名とディレクトリはこちら

もともと、FILEDIRがないのは、コンパイルを早くするためだとか

func Callers

callerで取得できる内容をpcスライスに格納する

func GC

GCを走らせる

func GOMAXPROCS

GOMAXPROCSは、同時に最大で実行可能なCPU数をセットし、前に設定していた値を返します。

func GOROOT

Goツリーのrootを返します。

func Goexit

呼び出したgoroutineを終了します。

func GoroutineProfile

アクティブゴルーチンスタック·プロファイル内のレコードの数を返す

0
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072

こんな感じで返します

func Gosched

他のgoroutineが実行できるようにプロセッサーを割当るのかな

func LockOSThread

LockOSThreadは、オペレーティングシステムのカレントスレッドと、この関数を呼び出したゴルーチンを関連付けます。呼び出したゴルーチンが終了するか、もしくはUnlockOSThreadが呼び出されるまで、常にそのスレッド内で実行されます。一方、他のゴルーチンはそのスレッド内では実行されなくなります。

func MemProfile

func NumCPU

実行してるマシンのCPUのコア数を返す
下記みたいに、コア数を取得してセットするみたいな使い方かな

func NumGoroutine

現在動いてるgoroutineの数を取得

func ReadMemStats

playgroundでは動かないですが、localのmacで動かしたら下記のような感じ

goroutine: 100000
cpu: 4
time: 0.441048
memory all: 29.164619 MB
memory: 305.813200 byte/goroutine

func SetBlockProfileRate

testingで使うのかな
http://golang.org/cmd/go/#hdr-Description_of_testing_flags

go test コマンド

-blockprofilerate n
runtime.SetBlockProfileRateをnを指定して呼び出すことにより、goroutine
のブロックプロファイルに供給する詳細度を制御します。
'godoc runtime SetBlockProfileRate'を参照してください。
プロファイラはプログラムがブロックされているnナノ秒ごとに
1つのブロックイベントとして平均的にサンプルするように努めます。
デフォルトでは、-test.blockprofileをフラグなしで指定した場合には
すべてのブロックイベントが記録されます。-test.blockprofilerate=1
と同等となります。

func SetCPUProfileRate

debugで使うのかな

func SetFinalizer

func Stack

func ThreadCreateProfile

func UnlockOSThread

LockOSThreadを参照

func Version

バージョンを取得できます。
http://play.golang.org/p/qVT6QpOFcL

type BlockProfileRecord

type Error

type Func

func FuncForPC

func (*Func) Entry

func (*Func) FileLine

func (*Func) Name

type MemProfileRecord

func (*MemProfileRecord) InUseBytes

func (*MemProfileRecord) InUseObjects

func (*MemProfileRecord) Stack

type MemStats

type StackRecord

func (*StackRecord) Stack

type TypeAssertionError

func (*TypeAssertionError) Error

func (*TypeAssertionError) RuntimeError

runtime/debug

GC系の操作が多いですね。

FreeOSMemory

強制的にGCを行う

PrintStack

こんな感じで標準エラーのスタックトレースを返します

ReadGCStats

GCの統計情報を返します

SetGCPercent

デフォルトは100
debug.SetGCPercent(100)

debug.SetGCPercent(50)
だと、通常の半分のメモリ領域だけGCを行うのかな

debug.SetGCPercent(-1)
だとGCは無効になる

SetMaxStack

上記の例はstack overflowします。

1つのgoroutineにつき、使用できるメモリの最大量を設定できます。
デフォルト値は64ビットシステムで1ギガです。
設定した数を超えるとプログラムはクラッシュします。

SetMaxThreads

上記はthread exhaustionプログラムです

goプログラムが使用できるスレッドの最大数を設定できます。
デフォルトは1万スレッドです。
設定した数を超えるとプログラムはクラッシュします。

SetPanicOnFault

debug.SetPanicOnFault(true)
SetPanicOnFaultは、現在のゴルーチンにのみ適用されます。

Stack

この関数は非推奨

WriteHeapDump

WriteHeapDumpはヒープの説明と指定したファイル記述子にその中のオブジェクトを書き込みます。

runtime/pprof

StartCPUProfile

runtime/race

go run -race hello.go

10
10
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10