Skip to content

Commit 3f6bb59

Browse files
authored
Merge pull request #809 from devlights/add-runtime-metrics-example
2 parents a93e121 + e10061d commit 3f6bb59

File tree

5 files changed

+196
-68
lines changed

5 files changed

+196
-68
lines changed

examples/basic/metricsop/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
このディレクトリには以下のサンプルがあります。
44

5-
| file | example name | note |
6-
| ------------- | ------------------ | ----------------------------------------------------------------------- |
7-
| heapmemory.go | metrics_heapmemory | runtime/metrics を利用してヒープメモリ関連の情報を取得するサンプルです. |
8-
| cpu.go | metrics_cpu | runtime/metrics を利用してCPU関連の情報を取得するサンプルです. |
5+
| file | example name | note |
6+
| -------- | ------------- | ----------------------------------------------------------------------- |
7+
| heap.go | metrics_heap | runtime/metrics を利用してヒープメモリ関連の情報を取得するサンプルです. |
8+
| cpu.go | metrics_cpu | runtime/metrics を利用してCPU関連の情報を取得するサンプルです. |
9+
| sched.go | metrics_sched | runtime/metrics を利用してスケジューラ関連の情報を取得するサンプルです. |

examples/basic/metricsop/cpu.go

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ func Cpu() error {
3838
var (
3939
ctx, cxl = context.WithTimeout(context.Background(), 1*time.Second)
4040
ready = make(chan bool)
41-
busyfn = func(ctx context.Context, ready chan<- bool) {
41+
busyfn = func(ctx context.Context, ready <-chan bool) {
4242
var i uint64
4343

44-
ready <- true
44+
<-ready
4545
for {
4646
select {
4747
case <-ctx.Done():
@@ -59,15 +59,29 @@ func Cpu() error {
5959
)
6060
defer cxl()
6161

62-
go busyfn(ctx, ready)
63-
<-ready
62+
for range runtime.GOMAXPROCS(0) - 1 {
63+
go busyfn(ctx, ready)
64+
}
65+
close(ready)
66+
67+
<-time.After(100 * time.Microsecond)
6468

6569
runtime.GC()
6670
metrics.Read(samples)
67-
6871
for _, s := range samples {
6972
output.Stdoutl("[Name ]", s.Name)
70-
output.Stdoutf("[Value]", "%+v\n", s.Value)
73+
74+
switch s.Value.Kind() {
75+
case metrics.KindUint64:
76+
output.Stdoutf("[Value]", "%v\n", s.Value.Uint64())
77+
case metrics.KindFloat64:
78+
output.Stdoutf("[Value]", "%v\n", s.Value.Float64())
79+
case metrics.KindFloat64Histogram:
80+
output.Stdoutf("[Value]", "Bucket Count: %d\n", len(s.Value.Float64Histogram().Buckets)-2)
81+
default:
82+
output.Stdoutl("[Value]", "INVALID")
83+
}
84+
7185
output.StdoutHr()
7286
}
7387

@@ -78,44 +92,44 @@ func Cpu() error {
7892
return nil
7993

8094
/*
81-
$ task
82-
task: [build] go build .
83-
task: [run] ./try-golang -onetime
84-
85-
ENTER EXAMPLE NAME: metrics_cpu
86-
87-
[Name] "metrics_cpu"
88-
[Name ] /cgo/go-to-c-calls:calls
89-
[Value] {kind:1 scalar:1 pointer:<nil>}
90-
--------------------------------------------------
91-
[Name ] /cpu/classes/gc/mark/assist:cpu-seconds
92-
[Value] {kind:2 scalar:4551153223746165794 pointer:<nil>}
93-
--------------------------------------------------
94-
[Name ] /cpu/classes/gc/mark/dedicated:cpu-seconds
95-
[Value] {kind:2 scalar:4562351584908057236 pointer:<nil>}
96-
--------------------------------------------------
97-
[Name ] /cpu/classes/gc/mark/idle:cpu-seconds
98-
[Value] {kind:2 scalar:0 pointer:<nil>}
99-
--------------------------------------------------
100-
[Name ] /cpu/classes/gc/pause:cpu-seconds
101-
[Value] {kind:2 scalar:4566896090190411183 pointer:<nil>}
102-
--------------------------------------------------
103-
[Name ] /cpu/classes/gc/total:cpu-seconds
104-
[Value] {kind:2 scalar:4569689296178052284 pointer:<nil>}
105-
--------------------------------------------------
106-
[Name ] /cpu/classes/idle:cpu-seconds
107-
[Value] {kind:2 scalar:4630569171334083814 pointer:<nil>}
108-
--------------------------------------------------
109-
[Name ] /cpu/classes/total:cpu-seconds
110-
[Value] {kind:2 scalar:4630572714109167296 pointer:<nil>}
111-
--------------------------------------------------
112-
[Name ] /cpu/classes/user:cpu-seconds
113-
[Value] {kind:2 scalar:4581969798399309374 pointer:<nil>}
114-
--------------------------------------------------
115-
[Buffer] 268435456
116-
117-
118-
[Elapsed] 1.002128694s
95+
$ task
96+
task: [build] go build .
97+
task: [run] ./try-golang -onetime
98+
99+
ENTER EXAMPLE NAME: metrics_cpu
100+
101+
[Name] "metrics_cpu"
102+
[Name ] /cgo/go-to-c-calls:calls
103+
[Value] 1
104+
--------------------------------------------------
105+
[Name ] /cpu/classes/gc/mark/assist:cpu-seconds
106+
[Value] 0.00011252
107+
--------------------------------------------------
108+
[Name ] /cpu/classes/gc/mark/dedicated:cpu-seconds
109+
[Value] 0.001100349
110+
--------------------------------------------------
111+
[Name ] /cpu/classes/gc/mark/idle:cpu-seconds
112+
[Value] 0
113+
--------------------------------------------------
114+
[Name ] /cpu/classes/gc/pause:cpu-seconds
115+
[Value] 0.047052128
116+
--------------------------------------------------
117+
[Name ] /cpu/classes/gc/total:cpu-seconds
118+
[Value] 0.048264997
119+
--------------------------------------------------
120+
[Name ] /cpu/classes/idle:cpu-seconds
121+
[Value] 32.647871189
122+
--------------------------------------------------
123+
[Name ] /cpu/classes/total:cpu-seconds
124+
[Value] 32.986254656
125+
--------------------------------------------------
126+
[Name ] /cpu/classes/user:cpu-seconds
127+
[Value] 0.290117141
128+
--------------------------------------------------
129+
[Buffer] 268435456
130+
131+
132+
[Elapsed] 1.008436872s
119133
*/
120134

121135
}

examples/basic/metricsop/examples.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func NewRegister() mapping.Register {
1313

1414
// Regist -- 登録します.
1515
func (r *register) Regist(m mapping.ExampleMapping) {
16-
m["metrics_heapmemory"] = HeapMemory
16+
m["metrics_heap"] = Heap
1717
m["metrics_cpu"] = Cpu
18+
m["metrics_sched"] = Sched
1819
}

examples/basic/metricsop/heapmemory.go renamed to examples/basic/metricsop/heap.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"github.com/devlights/gomy/output"
88
)
99

10-
// HeapMemory は、runtime/metrics を利用してヒープメモリ関連の情報を取得するサンプルです.
10+
// Heap は、runtime/metrics を利用してヒープメモリ関連の情報を取得するサンプルです.
1111
//
1212
// # REFERENCES
1313
// - https://pkg.go.dev/runtime/metrics@latest
14-
func HeapMemory() error {
14+
func Heap() error {
1515
var (
1616
items = []string{
1717
"/memory/classes/heap/free:bytes", // 完全に空いていて、システムに戻す資格があるが、戻されていないメモリ
@@ -37,10 +37,20 @@ func HeapMemory() error {
3737

3838
runtime.GC()
3939
metrics.Read(samples)
40-
4140
for _, s := range samples {
4241
output.Stdoutl("[Name ]", s.Name)
43-
output.Stdoutf("[Value]", "%+v\n", s.Value)
42+
43+
switch s.Value.Kind() {
44+
case metrics.KindUint64:
45+
output.Stdoutf("[Value]", "%v\n", s.Value.Uint64())
46+
case metrics.KindFloat64:
47+
output.Stdoutf("[Value]", "%v\n", s.Value.Float64())
48+
case metrics.KindFloat64Histogram:
49+
output.Stdoutf("[Value]", "Bucket Count: %d\n", len(s.Value.Float64Histogram().Buckets)-2)
50+
default:
51+
output.Stdoutl("[Value]", "INVALID")
52+
}
53+
4454
output.StdoutHr()
4555
}
4656

@@ -53,48 +63,48 @@ func HeapMemory() error {
5363
task: [build] go build .
5464
task: [run] ./try-golang -onetime
5565
56-
ENTER EXAMPLE NAME: metrics_heapmemory
66+
ENTER EXAMPLE NAME: metrics_heap
5767
58-
[Name] "metrics_heapmemory"
68+
[Name] "metrics_heap"
5969
[Name ] /memory/classes/heap/free:bytes
60-
[Value] {kind:1 scalar:268656640 pointer:<nil>}
70+
[Value] 237232128
6171
--------------------------------------------------
6272
[Name ] /memory/classes/heap/objects:bytes
63-
[Value] {kind:1 scalar:398432 pointer:<nil>}
73+
[Value] 395616
6474
--------------------------------------------------
6575
[Name ] /memory/classes/heap/released:bytes
66-
[Value] {kind:1 scalar:2433024 pointer:<nil>}
76+
[Value] 33882112
6777
--------------------------------------------------
6878
[Name ] /memory/classes/heap/stacks:bytes
69-
[Value] {kind:1 scalar:458752 pointer:<nil>}
79+
[Value] 425984
7080
--------------------------------------------------
7181
[Name ] /memory/classes/heap/unused:bytes
72-
[Value] {kind:1 scalar:682912 pointer:<nil>}
82+
[Value] 693920
7383
--------------------------------------------------
7484
[Name ] /gc/heap/allocs:bytes
75-
[Value] {kind:1 scalar:268953784 pointer:<nil>}
85+
[Value] 268953040
7686
--------------------------------------------------
7787
[Name ] /gc/heap/allocs:objects
78-
[Value] {kind:1 scalar:1662 pointer:<nil>}
88+
[Value] 1668
7989
--------------------------------------------------
8090
[Name ] /gc/heap/frees:bytes
81-
[Value] {kind:1 scalar:268555352 pointer:<nil>}
91+
[Value] 268557424
8292
--------------------------------------------------
8393
[Name ] /gc/heap/frees:objects
84-
[Value] {kind:1 scalar:450 pointer:<nil>}
94+
[Value] 459
8595
--------------------------------------------------
8696
[Name ] /gc/heap/goal:bytes
87-
[Value] {kind:1 scalar:4194304 pointer:<nil>}
97+
[Value] 4194304
8898
--------------------------------------------------
8999
[Name ] /gc/heap/live:bytes
90-
[Value] {kind:1 scalar:398720 pointer:<nil>}
100+
[Value] 395904
91101
--------------------------------------------------
92102
[Name ] /gc/heap/objects:objects
93-
[Value] {kind:1 scalar:1212 pointer:<nil>}
103+
[Value] 1209
94104
--------------------------------------------------
95105
[Buffer] 268435456
96106
97107
98-
[Elapsed] 2.96696ms
108+
[Elapsed] 4.183119ms
99109
*/
100110
}

examples/basic/metricsop/sched.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package metricsop
2+
3+
import (
4+
"context"
5+
"runtime"
6+
"runtime/metrics"
7+
"sync"
8+
"time"
9+
10+
"github.com/devlights/gomy/output"
11+
)
12+
13+
// Sched は、runtime/metrics を利用してスケジューラ関連の情報を取得するサンプルです.
14+
//
15+
// # REFERENCES
16+
// - https://pkg.go.dev/runtime/metrics@latest
17+
func Sched() error {
18+
var (
19+
items = []string{
20+
"/sched/gomaxprocs:threads", // ユーザーレベルのGoコードを同時に実行できるオペレーティング・システムのスレッド数
21+
"/sched/goroutines:goroutines", // 生きているゴルーチンの数
22+
"/sched/latencies:seconds", // ゴルーチンが実際に実行される前に、スケジューラ内で実行可能な状態で過ごした時間の分布
23+
"/sync/mutex/wait/total:seconds", // ゴルーチンがsync.Mutex、sync.RWMutex、またはランタイム内部ロックでブロックされた時間の累計
24+
}
25+
samples = make([]metrics.Sample, len(items))
26+
)
27+
28+
for i, name := range items {
29+
samples[i].Name = name
30+
}
31+
32+
var (
33+
ctx, cxl = context.WithTimeout(context.Background(), 2*time.Second)
34+
ready = make(chan bool)
35+
lock sync.Mutex
36+
busyfn = func(ctx context.Context, ready <-chan bool) {
37+
<-ready
38+
39+
lock.Lock()
40+
time.Sleep(100 * time.Millisecond)
41+
lock.Unlock()
42+
43+
<-ctx.Done()
44+
}
45+
)
46+
defer cxl()
47+
48+
for range runtime.GOMAXPROCS(0) - 1 {
49+
go busyfn(ctx, ready)
50+
}
51+
close(ready)
52+
53+
<-time.After(1 * time.Second)
54+
55+
metrics.Read(samples)
56+
for _, s := range samples {
57+
output.Stdoutl("[Name ]", s.Name)
58+
59+
switch s.Value.Kind() {
60+
case metrics.KindUint64:
61+
output.Stdoutf("[Value]", "%v\n", s.Value.Uint64())
62+
case metrics.KindFloat64:
63+
output.Stdoutf("[Value]", "%v\n", s.Value.Float64())
64+
case metrics.KindFloat64Histogram:
65+
output.Stdoutf("[Value]", "Bucket Count: %d\n", len(s.Value.Float64Histogram().Buckets)-2)
66+
default:
67+
output.Stdoutl("[Value]", "INVALID")
68+
}
69+
70+
output.StdoutHr()
71+
}
72+
73+
<-ctx.Done()
74+
75+
return nil
76+
77+
/*
78+
$ task
79+
task: [build] go build .
80+
task: [run] ./try-golang -onetime
81+
82+
ENTER EXAMPLE NAME: metrics_sched
83+
84+
[Name] "metrics_sched"
85+
[Name ] /sched/gomaxprocs:threads
86+
[Value] 16
87+
--------------------------------------------------
88+
[Name ] /sched/goroutines:goroutines
89+
[Value] 16
90+
--------------------------------------------------
91+
[Name ] /sched/latencies:seconds
92+
[Value] Bucket Count: 161
93+
--------------------------------------------------
94+
[Name ] /sync/mutex/wait/total:seconds
95+
[Value] 4.010605616
96+
--------------------------------------------------
97+
98+
99+
[Elapsed] 2.000425097s
100+
*/
101+
102+
}

0 commit comments

Comments
 (0)