Skip to content

Commit 0d681a2

Browse files
wanwusangzhisam.yang
andauthored
opt: optimization of machine performance data reading (#5174)
Co-authored-by: sam.yang <[email protected]>
1 parent 5ea027c commit 0d681a2

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

core/prof/runtime.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"io"
66
"os"
77
"runtime"
8+
"runtime/debug"
9+
"runtime/metrics"
810
"time"
911
)
1012

@@ -28,10 +30,29 @@ func displayStatsWithWriter(writer io.Writer, interval ...time.Duration) {
2830
ticker := time.NewTicker(duration)
2931
defer ticker.Stop()
3032
for range ticker.C {
31-
var m runtime.MemStats
32-
runtime.ReadMemStats(&m)
33+
var (
34+
alloc, totalAlloc, sys uint64
35+
samples = []metrics.Sample{
36+
{Name: "/memory/classes/heap/objects:bytes"},
37+
{Name: "/gc/heap/allocs:bytes"},
38+
{Name: "/memory/classes/total:bytes"},
39+
}
40+
)
41+
metrics.Read(samples)
42+
43+
if samples[0].Value.Kind() == metrics.KindUint64 {
44+
alloc = samples[0].Value.Uint64()
45+
}
46+
if samples[1].Value.Kind() == metrics.KindUint64 {
47+
totalAlloc = samples[1].Value.Uint64()
48+
}
49+
if samples[2].Value.Kind() == metrics.KindUint64 {
50+
sys = samples[2].Value.Uint64()
51+
}
52+
var stats debug.GCStats
53+
debug.ReadGCStats(&stats)
3354
fmt.Fprintf(writer, "Goroutines: %d, Alloc: %vm, TotalAlloc: %vm, Sys: %vm, NumGC: %v\n",
34-
runtime.NumGoroutine(), m.Alloc/mega, m.TotalAlloc/mega, m.Sys/mega, m.NumGC)
55+
runtime.NumGoroutine(), alloc/mega, totalAlloc/mega, sys/mega, stats.NumGC)
3556
}
3657
}()
3758
}

core/stat/usage.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package stat
22

33
import (
4-
"runtime"
4+
"runtime/debug"
5+
"runtime/metrics"
56
"sync/atomic"
67
"time"
78

@@ -56,8 +57,28 @@ func bToMb(b uint64) float32 {
5657
}
5758

5859
func printUsage() {
59-
var m runtime.MemStats
60-
runtime.ReadMemStats(&m)
60+
var (
61+
alloc, totalAlloc, sys uint64
62+
samples = []metrics.Sample{
63+
{Name: "/memory/classes/heap/objects:bytes"},
64+
{Name: "/gc/heap/allocs:bytes"},
65+
{Name: "/memory/classes/total:bytes"},
66+
}
67+
stats debug.GCStats
68+
)
69+
metrics.Read(samples)
70+
71+
if samples[0].Value.Kind() == metrics.KindUint64 {
72+
alloc = samples[0].Value.Uint64()
73+
}
74+
if samples[1].Value.Kind() == metrics.KindUint64 {
75+
totalAlloc = samples[1].Value.Uint64()
76+
}
77+
if samples[2].Value.Kind() == metrics.KindUint64 {
78+
sys = samples[2].Value.Uint64()
79+
}
80+
debug.ReadGCStats(&stats)
81+
6182
logx.Statf("CPU: %dm, MEMORY: Alloc=%.1fMi, TotalAlloc=%.1fMi, Sys=%.1fMi, NumGC=%d",
62-
CpuUsage(), bToMb(m.Alloc), bToMb(m.TotalAlloc), bToMb(m.Sys), m.NumGC)
83+
CpuUsage(), bToMb(alloc), bToMb(totalAlloc), bToMb(sys), stats.NumGC)
6384
}

0 commit comments

Comments
 (0)