Go言語のruntimeはOSやプロセスの情報を得ることができます。
runtime - The Go Programming Language
CPU情報はDeprecatedになってしまったようですね。
下記のサンプルコードをもとにちょっと自分でも書いてみました。
golangcode.com
package main import ( "runtime" "fmt" "time" ) func main() { fmt.Println("--- Params ---") fmt.Printf("GOROOT = %v\n",runtime.GOROOT()) fmt.Printf("NumCPU = %v\n", runtime.NumCPU()) fmt.Printf("NumGoroutine = %v\n", runtime.NumGoroutine()) fmt.Println("--- memory test ---") fmt.Println("--- before ---") PrintMemUsage() fmt.Println("--- start ---") var mem_alloc [][]int for i := 0 ; i < 4 ; i++ { a := make([]int, 0, 999999) mem_alloc = append(mem_alloc, a) PrintMemUsage() time.Sleep(time.Second) } PrintMemUsage() fmt.Println("--- After GC ---") runtime.GC() PrintMemUsage() } func PrintMemUsage() { var m runtime.MemStats runtime.ReadMemStats(&m) fmt.Printf("Alloc = %v MiB\t", bToMb(m.Alloc)) fmt.Printf("TotalAlloc = %v MiB\t", bToMb(m.TotalAlloc)) fmt.Printf("Sys = %v MiB\t", bToMb(m.Sys)) fmt.Printf("Mallocs = %v MiB\t", bToMb(m.Mallocs)) fmt.Printf("Frees = %v MiB\t", bToMb(m.Frees)) fmt.Printf("HeapAlloc = %v MiB\t", bToMb(m.HeapAlloc)) fmt.Printf("HeapSys = %v MiB\t", bToMb(m.HeapSys)) fmt.Printf("NumGC = %v", m.NumGC) fmt.Printf("\n") } func bToMb(b uint64) uint64 { return b / 1024 / 1024 }
こんな結果が取れます。
--- Params --- GOROOT = /opt/go NumCPU = 1 NumGoroutine = 1 --- memory test --- --- before --- Alloc = 0 MiB TotalAlloc = 0 MiB Sys = 1 MiB Mallocs = 0 MiB Frees = 0 MiB HeapAlloc = 0 MiB HeapSys = 0 MiB NumGC = 0 --- start --- Alloc = 7 MiB TotalAlloc = 7 MiB Sys = 11 MiB Mallocs = 0 MiB Frees = 0 MiB HeapAlloc = 7 MiB HeapSys = 8 MiB NumGC = 1 Alloc = 15 MiB TotalAlloc = 15 MiB Sys = 19 MiB Mallocs = 0 MiB Frees = 0 MiB HeapAlloc = 15 MiB HeapSys = 16 MiB NumGC = 2 Alloc = 22 MiB TotalAlloc = 22 MiB Sys = 26 MiB Mallocs = 0 MiB Frees = 0 MiB HeapAlloc = 22 MiB HeapSys = 23 MiB NumGC = 3 Alloc = 30 MiB TotalAlloc = 30 MiB Sys = 34 MiB Mallocs = 0 MiB Frees = 0 MiB HeapAlloc = 30 MiB HeapSys = 31 MiB NumGC = 4 Alloc = 30 MiB TotalAlloc = 30 MiB Sys = 34 MiB Mallocs = 0 MiB Frees = 0 MiB HeapAlloc = 30 MiB HeapSys = 31 MiB NumGC = 4 --- After GC --- Alloc = 0 MiB TotalAlloc = 30 MiB Sys = 34 MiB Mallocs = 0 MiB Frees = 0 MiB HeapAlloc = 0 MiB HeapSys = 31 MiB NumGC = 5