Skip to content

Add examples/procfs/meminfo #921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/procfs/meminfo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app
14 changes: 14 additions & 0 deletions examples/procfs/meminfo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# これは何?

[procfs](https://github.com/prometheus/procfs) を使って、/procファイルシステム上のメモリ情報を取得するサンプルです。

```sh
$ task
task: [clean] rm -f ./app
task: [build] go build -o app .
task: [run] free | head -n 2
total used free shared buff/cache available
Mem: 65841080 18350600 6361292 129440 41129188 46633780
task: [run] ./app
MemTotal=65841080KB(64297MB), Free=6358804KB(6209MB)
```
20 changes: 20 additions & 0 deletions examples/procfs/meminfo/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# https://taskfile.dev

version: '3'

tasks:
default:
cmds:
- task: clean
- task: build
- task: run
build:
cmds:
- go build -o app .
run:
cmds:
- free | head -n 2
- ./app
clean:
cmds:
- rm -f ./app
45 changes: 45 additions & 0 deletions examples/procfs/meminfo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"log"

"github.com/prometheus/procfs"
)

func main() {
log.SetFlags(0)

if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
var (
fs procfs.FS
err error
)
fs, err = procfs.NewDefaultFS() // デフォルトは /proc を見ている
if err != nil {
return err
}

var (
mem procfs.Meminfo
)
mem, err = fs.Meminfo()
if err != nil {
return err
}

var (
memTotal = *mem.MemTotal // システムに搭載されている物理メモリ(RAM)の総量をkB単位
memTotalBytes = *mem.MemTotalBytes // システムに搭載されている物理メモリ(RAM)の総量をバイト単位
freeTotal = *mem.MemFree // 空き容量をKB表示
freeTotalBytes = *mem.MemFreeBytes // 空き容量をバイト単位
toMB = func(v uint64) uint64 { return v >> 20 }
)
log.Printf("MemTotal=%dKB(%dMB), Free=%dKB(%dMB)", memTotal, toMB(memTotalBytes), freeTotal, toMB(freeTotalBytes))

return nil
}
Loading