github.com/cnotch/ipchub@v1.1.0/stats/runtime.go (about) 1 // Copyright (c) 2019,CAOHONGJU All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package stats 6 7 import ( 8 "runtime" 9 "time" 10 11 "github.com/kelindar/process" 12 ) 13 14 // 创建时间 15 var ( 16 StartingTime = time.Now() 17 ) 18 19 // Runtime 运行时统计 20 type Runtime struct { 21 Heap Heap `json:"heap"` 22 MCache Memory `json:"mcache"` // MemStats.MCacheInuse/MCacheSys 23 MSpan Memory `json:"mspan"` // MemStats.MSpanInuse/MSpanSys 24 Stack Memory `json:"stack"` // MemStats.StackInuse/StackSys 25 GC GC `json:"gc"` 26 Go Go `json:"go"` 27 } 28 29 // Proc 进程信息统计 30 type Proc struct { 31 CPU float64 `json:"cpu"` // cpu使用情况 32 Priv int32 `json:"priv"` // 私有内存 KB 33 Virt int32 `json:"virt"` // 虚拟内存 KB 34 Uptime int32 `json:"uptime"` // 运行时间 S 35 } 36 37 // Heap 运行是堆信息 38 type Heap struct { 39 Inuse int32 `json:"inuse"` // KB MemStats.HeapInuse 40 Sys int32 `json:"sys"` // KB MemStats.HeapSys 41 Alloc int32 `json:"alloc"` // KB MemStats.HeapAlloc 42 Idle int32 `json:"idle"` // KB MemStats.HeapIdle 43 Released int32 `json:"released"` // KB MemStats.HeapReleased 44 Objects int32 `json:"objects"` // = MemStats.HeapObjects 45 } 46 47 // Memory 通用内存信息 48 type Memory struct { 49 Inuse int32 `json:"inuse"` // KB 50 Sys int32 `json:"sys"` // KB 51 } 52 53 // GC 垃圾回收信息 54 type GC struct { 55 CPU float64 `json:"cpu"` // cpu使用情况 56 Sys int32 `json:"sys"` // KB MemStats.GCSys 57 } 58 59 // Go Go运行时 goroutines 、threads 和 total memory 60 type Go struct { 61 Count int32 `json:"count"` // runtime.NumGoroutine() 62 Procs int32 `json:"procs"` //runtime.NumCPU() 63 Sys int32 `json:"sys"` // KB MemStats.Sys 64 Alloc int32 `json:"alloc"` // KB MemStats.TotalAlloc 65 } 66 67 // MeasureRuntime 获取运行时信息。 68 func MeasureRuntime() Proc { 69 defer recover() 70 var memoryPriv, memoryVirtual int64 71 var cpu float64 72 process.ProcUsage(&cpu, &memoryPriv, &memoryVirtual) 73 return Proc{ 74 CPU: cpu, 75 Priv: toKB(uint64(memoryPriv)), 76 Virt: toKB(uint64(memoryVirtual)), 77 Uptime: int32(time.Now().Sub(StartingTime).Seconds()), 78 } 79 } 80 81 // MeasureFullRuntime 获取运行时信息。 82 func MeasureFullRuntime() *Runtime { 83 defer recover() 84 85 // Collect stats 86 var memory runtime.MemStats 87 runtime.ReadMemStats(&memory) 88 89 return &Runtime{ 90 // Measure heap information 91 Heap: Heap{ 92 Alloc: toKB(memory.HeapAlloc), 93 Idle: toKB(memory.HeapIdle), 94 Inuse: toKB(memory.HeapInuse), 95 Objects: int32(memory.HeapObjects), 96 Released: toKB(memory.HeapReleased), 97 Sys: toKB(memory.HeapSys), 98 }, 99 // Measure off heap memory 100 MCache: Memory{ 101 Inuse: toKB(memory.MCacheInuse), 102 Sys: toKB(memory.MCacheSys), 103 }, 104 MSpan: Memory{ 105 Inuse: toKB(memory.MSpanInuse), 106 Sys: toKB(memory.MSpanSys), 107 }, 108 // Measure memory 109 Stack: Memory{ 110 Inuse: toKB(memory.StackInuse), 111 Sys: toKB(memory.StackSys), 112 }, 113 // Measure GC 114 GC: GC{ 115 CPU: memory.GCCPUFraction, 116 Sys: toKB(memory.GCSys), 117 }, 118 // Measure goroutines and threads and total memory 119 Go: Go{ 120 Count: int32(runtime.NumGoroutine()), 121 Procs: int32(runtime.NumCPU()), 122 Sys: toKB(memory.Sys), 123 Alloc: toKB(memory.TotalAlloc), 124 }, 125 } 126 } 127 128 // Converts the memory in bytes to KBs, otherwise it would overflow our int32 129 func toKB(v uint64) int32 { 130 return int32(v / 1024) 131 }