Skip to content
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

support gpu monitor #425

Merged
merged 9 commits into from May 9, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ out/
*.swo
*.tar.gz
docs/_site
.idea
6 changes: 6 additions & 0 deletions modules/agent/funcs/funcs.go
Expand Up @@ -70,5 +70,11 @@ func BuildMappers() {
},
Interval: interval,
},
{
Fs: []func() []*model.MetricValue{
GpuMetrics,
},
Interval: interval,
},
}
}
106 changes: 106 additions & 0 deletions modules/agent/funcs/gpu.go
@@ -0,0 +1,106 @@
package funcs

import (
"log"

"github.com/mindprince/gonvml"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要同步更新一下这个文件: https://github.com/open-falcon/falcon-plus/blob/master/vendor/vendor.json

We use govendor to manage the golang packages. Please install govendor before compilation.

go get -u github.com/kardianos/govendor
Most depended packages are saved under ./vendor dir. If you want to add or update a package, just run govendor fetch xxxx@commitID or govendor fetch xxxx@v1.x.x, then you will find the package have been placed in ./vendor correctly.

"github.com/open-falcon/falcon-plus/common/model"
)

// 需要load libnvidia-ml.so.1库
func GpuMetrics() (L []*model.MetricValue) {

if err := gonvml.Initialize(); err != nil {
log.Println("Initialize error: ", err)
return
}

defer gonvml.Shutdown()

count, err := gonvml.DeviceCount()
if err != nil {
log.Println("DeviceCount error: ", err)
return
}

if count == 0 {
return
}

temperature := uint(0)
totalMemory := uint64(0)
usedMemory := uint64(0)
gpuUtilization := uint(0)
memoryUtilization := uint(0)
powerUsage := uint(0)
allUtilization := uint(0)
allMemoryUtilization := uint(0)

for i := 0; i < int(count); i++ {
dev, err := gonvml.DeviceHandleByIndex(uint(i))
if err != nil {
log.Println("DeviceHandleByIndex error:", err)
continue
}

uuid, err := dev.UUID()
if err != nil {
log.Println("dev.UUID error", err)
}

tag := "uuid=" + uuid

// 不是所有gpu都有风扇
fanSpeed, err := dev.FanSpeed()
if err != nil {
log.Println("dev.FanSpeed error: ", err)
} else {
L = append(L, GaugeValue("gpu.fan.speed", fanSpeed, tag))
}

temperature, err = dev.Temperature()
if err != nil {
log.Println("dev.Temperature error: ", err)
continue
}

totalMemory, usedMemory, err = dev.MemoryInfo()
if err != nil {
log.Println("dev.MemoryInfo error: ", err)
continue
}

// 单位换算为兆
totalBillion := float64(totalMemory / 1024 / 1024)
usedBillion := float64(usedMemory / 1024 / 1024)

gpuUtilization, memoryUtilization, err = dev.UtilizationRates()
if err != nil {
log.Println("dev.UtilizationRates error: ", err)
continue
}

allUtilization += gpuUtilization
allMemoryUtilization += memoryUtilization

powerUsage, err = dev.PowerUsage()
if err != nil {
log.Println("dev.PowerUsage error: ", err)
}

// 单位换算为瓦特
powerWatt := float64(powerUsage / 1000)

L = append(L, GaugeValue("gpu.temperature", temperature, tag))
L = append(L, GaugeValue("gpu.memory.total", totalBillion, tag))
L = append(L, GaugeValue("gpu.memory.used", usedBillion, tag))
L = append(L, GaugeValue("gpu.memory.util", memoryUtilization, tag))
L = append(L, GaugeValue("gpu.util", gpuUtilization, tag))
L = append(L, GaugeValue("gpu.power.usage", powerWatt, tag))
}

L = append(L, GaugeValue("gpu.count", count))
L = append(L, GaugeValue("gpu.util.avg", allUtilization/count))
L = append(L, GaugeValue("gpu.memory.util.avg", allMemoryUtilization/count))
return L
}
202 changes: 202 additions & 0 deletions vendor/github.com/mindprince/gonvml/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/mindprince/gonvml/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions vendor/github.com/mindprince/gonvml/NVML_NOTICE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.