Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2cbfe7

Browse files
committed
Add examples/procfs/meminfo
1 parent 2ce07a6 commit f2cbfe7

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

examples/procfs/meminfo/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app

examples/procfs/meminfo/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# これは何?
2+
3+
[procfs](https://github.com/prometheus/procfs) を使って、/procファイルシステム上のメモリ情報を取得するサンプルです。
4+
5+
```sh
6+
$ task
7+
task: [clean] rm -f ./app
8+
task: [build] go build -o app .
9+
task: [run] free | head -n 2
10+
total used free shared buff/cache available
11+
Mem: 65841080 18350600 6361292 129440 41129188 46633780
12+
task: [run] ./app
13+
MemTotal=65841080KB(64297MB), Free=6358804KB(6209MB)
14+
```

examples/procfs/meminfo/Taskfile.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# https://taskfile.dev
2+
3+
version: '3'
4+
5+
tasks:
6+
default:
7+
cmds:
8+
- task: clean
9+
- task: build
10+
- task: run
11+
build:
12+
cmds:
13+
- go build -o app .
14+
run:
15+
cmds:
16+
- free | head -n 2
17+
- ./app
18+
clean:
19+
cmds:
20+
- rm -f ./app

examples/procfs/meminfo/main.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/prometheus/procfs"
7+
)
8+
9+
func main() {
10+
log.SetFlags(0)
11+
12+
if err := run(); err != nil {
13+
log.Fatal(err)
14+
}
15+
}
16+
17+
func run() error {
18+
var (
19+
fs procfs.FS
20+
err error
21+
)
22+
fs, err = procfs.NewDefaultFS() // デフォルトは /proc を見ている
23+
if err != nil {
24+
return err
25+
}
26+
27+
var (
28+
mem procfs.Meminfo
29+
)
30+
mem, err = fs.Meminfo()
31+
if err != nil {
32+
return err
33+
}
34+
35+
var (
36+
memTotal = *mem.MemTotal // システムに搭載されている物理メモリ(RAM)の総量をkB単位
37+
memTotalBytes = *mem.MemTotalBytes // システムに搭載されている物理メモリ(RAM)の総量をバイト単位
38+
freeTotal = *mem.MemFree // 空き容量をKB表示
39+
freeTotalBytes = *mem.MemFreeBytes // 空き容量をバイト単位
40+
toMB = func(v uint64) uint64 { return v >> 20 }
41+
)
42+
log.Printf("MemTotal=%dKB(%dMB), Free=%dKB(%dMB)", memTotal, toMB(memTotalBytes), freeTotal, toMB(freeTotalBytes))
43+
44+
return nil
45+
}

0 commit comments

Comments
 (0)