Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e2c87cc

Browse files
authoredApr 25, 2025··
Merge pull request #916 from devlights/add-gopsutil-cpu
2 parents 8129339 + 9509531 commit e2c87cc

File tree

7 files changed

+192
-5
lines changed

7 files changed

+192
-5
lines changed
 

‎examples/psutil/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# これは何?
2+
3+
[gopsutil](https://github.com/shirou/gopsutil) のサンプルを配置しています。
4+
5+
pythonのpsutilをGoにポートされているライブラリで、非常に便利です。
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app

‎examples/psutil/cpu/percent/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# これは何?
2+
3+
[gopsutil](https://github.com/shirou/gopsutil)を用いてCPU使用率を取得するサンプルです。
4+
5+
```sh
6+
$ task
7+
task: [default] go build -o app .
8+
task: [default] ./app
9+
06:01:48 [4]
10+
06:01:49 [6]
11+
06:01:50 [8]
12+
06:01:51 [6]
13+
06:01:52 [10]
14+
task: [default] ./app -percpu
15+
06:01:54 [4 5]
16+
06:01:55 [5 6]
17+
06:01:56 [6 7]
18+
06:01:57 [8 9]
19+
06:01:58 [2 3]
20+
task: [default] ./app -percpu -spincpu
21+
06:02:00 [83 85]
22+
06:02:01 [84 82]
23+
06:02:02 [85 82]
24+
06:02:03 [84 82]
25+
06:02:04 [85 81]
26+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# https://taskfile.dev
2+
3+
version: '3'
4+
5+
tasks:
6+
default:
7+
cmds:
8+
- go build -o app .
9+
- ./app
10+
- ./app -percpu
11+
- ./app -percpu -spincpu

‎examples/psutil/cpu/percent/main.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"errors"
6+
"flag"
7+
"log"
8+
"math"
9+
"runtime"
10+
"time"
11+
12+
"github.com/shirou/gopsutil/v4/cpu"
13+
)
14+
15+
type (
16+
Args struct {
17+
perCpu bool
18+
spinCpu bool
19+
}
20+
)
21+
22+
var (
23+
args Args
24+
)
25+
26+
func init() {
27+
flag.BoolVar(&args.perCpu, "percpu", false, "per-cpu")
28+
flag.BoolVar(&args.spinCpu, "spincpu", false, "spin-cpu")
29+
}
30+
31+
func main() {
32+
log.SetFlags(log.Ltime)
33+
flag.Parse()
34+
35+
if err := run(); err != nil {
36+
log.Fatal(err)
37+
}
38+
}
39+
40+
func run() error {
41+
type (
42+
cpuval struct {
43+
val []int
44+
err error
45+
}
46+
)
47+
var (
48+
interval = 1 * time.Second
49+
percpu = args.perCpu
50+
cpuCh = make(chan *cpuval)
51+
ctx, cxl = context.WithTimeout(context.Background(), 6*time.Second)
52+
)
53+
defer cxl()
54+
defer close(cpuCh)
55+
56+
// 必要であれば無駄にCPUを回す
57+
if args.spinCpu {
58+
go spinCpu(ctx)
59+
}
60+
61+
// CPU使用率を取得
62+
go func(ctx context.Context, ch chan<- *cpuval) {
63+
for {
64+
select {
65+
case <-ctx.Done():
66+
return
67+
default:
68+
v, err := cpu.PercentWithContext(ctx, interval, percpu)
69+
70+
vals := make([]int, len(v))
71+
for i, f := range v {
72+
vals[i] = int(f)
73+
}
74+
75+
cpuCh <- &cpuval{vals, err}
76+
}
77+
}
78+
}(ctx, cpuCh)
79+
80+
// 出力
81+
for v := range cpuCh {
82+
if v.err != nil {
83+
if errors.Is(v.err, context.DeadlineExceeded) {
84+
return nil
85+
}
86+
return v.err
87+
}
88+
89+
log.Printf("%v\n", v.val)
90+
}
91+
92+
return nil
93+
}
94+
95+
func spinCpu(ctx context.Context) {
96+
for range runtime.NumCPU() {
97+
go func(ctx context.Context) {
98+
for {
99+
select {
100+
case <-ctx.Done():
101+
return
102+
default:
103+
for i := range 100000 {
104+
_ = math.Sqrt(float64(i * i))
105+
}
106+
time.Sleep(1 * time.Microsecond)
107+
}
108+
}
109+
}(ctx)
110+
}
111+
}

‎go.mod

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,17 @@ require (
2020

2121
require github.com/muesli/cancelreader v0.2.2
2222

23-
require github.com/devlights/fdpassing v1.0.1
23+
require (
24+
github.com/devlights/fdpassing v1.0.1
25+
github.com/shirou/gopsutil/v4 v4.25.3
26+
)
27+
28+
require (
29+
github.com/ebitengine/purego v0.8.2 // indirect
30+
github.com/go-ole/go-ole v1.2.6 // indirect
31+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
32+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
33+
github.com/tklauser/go-sysconf v0.3.12 // indirect
34+
github.com/tklauser/numcpus v0.6.1 // indirect
35+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
36+
)

‎go.sum

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,47 @@ github.com/devlights/fdpassing v1.0.1 h1:O9JBm3wpay8NIbjteVV7elZwtqi6ie+X1PbH1WR
44
github.com/devlights/fdpassing v1.0.1/go.mod h1:eNoNi77gbfz/CsUtmnH3kZ64HVcK3ttFsKYxJopSPZ4=
55
github.com/devlights/gomy v0.6.0 h1:7BT8bSxr+ZeNkgEYNufuM2rSc6kIoN6g2FSZvrcT9zw=
66
github.com/devlights/gomy v0.6.0/go.mod h1:d28qyQ+/s7JravMlss2kIFxWxhY2KLlpe7rIBZ5YKeA=
7+
github.com/ebitengine/purego v0.8.2 h1:jPPGWs2sZ1UgOSgD2bClL0MJIqu58nOmIcBuXr62z1I=
8+
github.com/ebitengine/purego v0.8.2/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
9+
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
10+
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
711
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
8-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
9-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
12+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
13+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
1014
github.com/integrii/flaggy v1.5.2 h1:bWV20MQEngo4hWhno3i5Z9ISPxLPKj9NOGNwTWb/8IQ=
1115
github.com/integrii/flaggy v1.5.2/go.mod h1:dO13u7SYuhk910nayCJ+s1DeAAGC1THCMj1uSFmwtQ8=
16+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
17+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
1218
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
1319
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
1420
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
1521
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
1622
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1723
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
24+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
25+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
26+
github.com/shirou/gopsutil/v4 v4.25.3 h1:SeA68lsu8gLggyMbmCn8cmp97V1TI9ld9sVzAUcKcKE=
27+
github.com/shirou/gopsutil/v4 v4.25.3/go.mod h1:xbuxyoZj+UsgnZrENu3lQivsngRR5BdjbJwf2fv4szA=
1828
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
1929
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
20-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
21-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
30+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
31+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
32+
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
33+
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
34+
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
35+
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
36+
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
37+
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
2238
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
2339
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
2440
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e h1:I88y4caeGeuDQxgdoFPUq097j7kNfw6uvuiNxUBfcBk=
2541
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
2642
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
2743
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
44+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
45+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
46+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
47+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2848
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
2949
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
3050
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=

0 commit comments

Comments
 (0)
Please sign in to comment.