Skip to content
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY go.sum .
RUN go mod download
COPY . .
ARG VERSION=unknown
RUN CGO_ENABLED=1 go build -mod=readonly -ldflags "-X 'github.com/coroot/coroot-node-agent/flags.Version=${VERSION}'" -o coroot-node-agent .
RUN CGO_ENABLED=1 go build -mod=readonly -ldflags "-extldflags='-Wl,-z,lazy' -X 'github.com/coroot/coroot-node-agent/flags.Version=${VERSION}'" -o coroot-node-agent .

FROM registry.access.redhat.com/ubi9/ubi

Expand Down
2 changes: 2 additions & 0 deletions containers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func guessApplicationTypeByCmdline(cmdline []byte) string {
return "nats"
case bytes.HasSuffix(cmd, []byte("java")):
return "java"
case bytes.HasSuffix(cmd, []byte("ollama")):
return "ollama"
case bytes.Contains(cmd, []byte("victoria-metrics")) ||
bytes.Contains(cmd, []byte("vmstorage")) ||
bytes.Contains(cmd, []byte("vminsert")) ||
Expand Down
25 changes: 25 additions & 0 deletions containers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
gcInterval = 10 * time.Minute
pingTimeout = 300 * time.Millisecond
multilineCollectorTimeout = time.Second
gpuStatsWindow = 15 * time.Second
)

type ContainerID string
Expand Down Expand Up @@ -132,6 +133,8 @@ type Container struct {
l7Stats L7Stats
dnsStats *L7Metrics

gpuStats map[string]*GpuUsage

oomKills int
pythonThreadLockWaitTime time.Duration

Expand Down Expand Up @@ -181,6 +184,8 @@ func NewContainer(id ContainerID, cg *cgroup.Cgroup, md *ContainerMetadata, pid
l7Stats: L7Stats{},
dnsStats: &L7Metrics{},

gpuStats: map[string]*GpuUsage{},

mounts: map[string]proc.MountInfo{},
seenMounts: map[uint64]struct{}{},

Expand Down Expand Up @@ -370,7 +375,27 @@ func (c *Container) Collect(ch chan<- prometheus.Metric) {
process.dotNetMonitor.Collect(ch)
}
}

for _, usage := range c.gpuStats {
usage.Reset()
}
if usage := process.getGPUUsage(); usage != nil {
for uuid, u := range usage {
tu := c.gpuStats[uuid]
if tu == nil {
tu = &GpuUsage{}
c.gpuStats[uuid] = tu
}
tu.GPU += u.GPU
tu.Memory += u.Memory
}
}
}
for uuid, usage := range c.gpuStats {
ch <- gauge(metrics.GpuUsagePercent, usage.GPU, uuid)
ch <- gauge(metrics.GpuMemoryUsagePercent, usage.Memory, uuid)
}

for appType := range appTypes {
ch <- gauge(metrics.ApplicationType, 1, appType)
}
Expand Down
6 changes: 6 additions & 0 deletions containers/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ var metrics = struct {

PythonThreadLockWaitTime *prometheus.Desc

GpuUsagePercent *prometheus.Desc
GpuMemoryUsagePercent *prometheus.Desc

Ip2Fqdn *prometheus.Desc
}{
ContainerInfo: metric("container_info", "Meta information about the container", "image", "systemd_triggered_by"),
Expand Down Expand Up @@ -100,6 +103,9 @@ var metrics = struct {
Ip2Fqdn: metric("ip_to_fqdn", "Mapping IP addresses to FQDNs based on DNS requests initiated by containers", "ip", "fqdn"),

PythonThreadLockWaitTime: metric("container_python_thread_lock_wait_time_seconds", "Time spent waiting acquiring GIL in seconds"),

GpuUsagePercent: metric("container_resources_gpu_usage_percent", "Percent of GPU compute resources used by the container", "gpu_uuid"),
GpuMemoryUsagePercent: metric("container_resources_gpu_memory_usage_percent", "Percent of GPU memory used by the container", "gpu_uuid"),
}

var (
Expand Down
50 changes: 50 additions & 0 deletions containers/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ import (

"github.com/cilium/ebpf/link"
"github.com/coroot/coroot-node-agent/ebpftracer"
"github.com/coroot/coroot-node-agent/gpu"
"github.com/coroot/coroot-node-agent/proc"
"github.com/jpillora/backoff"
"github.com/mdlayher/taskstats"
)

type GpuUsage struct {
GPU float64
Memory float64
}

func (gu *GpuUsage) Reset() {
gu.Memory = 0
gu.GPU = 0
}

type Process struct {
Pid uint32
StartedAt time.Time
Expand All @@ -29,6 +40,8 @@ type Process struct {
goTlsUprobesChecked bool
openSslUprobesChecked bool
pythonGilChecked bool

gpuUsageSamples []gpu.ProcessUsageSample
}

func NewProcess(pid uint32, stats *taskstats.Stats, tracer *ebpftracer.Tracer) *Process {
Expand Down Expand Up @@ -97,6 +110,43 @@ func (p *Process) instrumentPython(cmdline []byte, tracer *ebpftracer.Tracer) {
p.uprobes = append(p.uprobes, tracer.AttachPythonThreadLockProbes(p.Pid)...)
}

func (p *Process) addGpuUsageSample(sample gpu.ProcessUsageSample) {
p.removeOldGpuUsageSamples(sample.Timestamp.Add(-gpuStatsWindow))
p.gpuUsageSamples = append(p.gpuUsageSamples, sample)
}

func (p *Process) getGPUUsage() map[string]*GpuUsage {
p.removeOldGpuUsageSamples(time.Now().Add(-gpuStatsWindow))
if len(p.gpuUsageSamples) == 0 {
return nil
}
gpuStatsWindowSeconds := gpuStatsWindow.Seconds()
res := make(map[string]*GpuUsage)
for _, sample := range p.gpuUsageSamples {
u := res[sample.UUID]
if u == nil {
u = &GpuUsage{}
res[sample.UUID] = u
}
u.GPU += float64(sample.GPUPercent) / gpuStatsWindowSeconds
u.Memory += float64(sample.MemoryPercent) / gpuStatsWindowSeconds
}
return res
}

func (p *Process) removeOldGpuUsageSamples(cutoff time.Time) {
i := 0
for ; i < len(p.gpuUsageSamples); i++ {
if p.gpuUsageSamples[i].Timestamp.After(cutoff) {
break
}
}
if i > 0 {
copy(p.gpuUsageSamples, p.gpuUsageSamples[i:])
p.gpuUsageSamples = p.gpuUsageSamples[:len(p.gpuUsageSamples)-i]
}
}

func (p *Process) Close() {
p.cancelFunc()
for _, u := range p.uprobes {
Expand Down
13 changes: 12 additions & 1 deletion containers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/coroot/coroot-node-agent/common"
"github.com/coroot/coroot-node-agent/ebpftracer"
"github.com/coroot/coroot-node-agent/flags"
"github.com/coroot/coroot-node-agent/gpu"
"github.com/coroot/coroot-node-agent/proc"
"github.com/prometheus/client_golang/prometheus"
"github.com/vishvananda/netns"
Expand Down Expand Up @@ -59,9 +60,11 @@ type Registry struct {
trafficStatsLastUpdated time.Time
trafficStatsLock sync.Mutex
trafficStatsUpdateCh chan *TrafficStatsUpdate

gpuProcessUsageSampleChan chan gpu.ProcessUsageSample
}

func NewRegistry(reg prometheus.Registerer, processInfoCh chan<- ProcessInfo) (*Registry, error) {
func NewRegistry(reg prometheus.Registerer, processInfoCh chan<- ProcessInfo, gpuProcessUsageSampleChan chan gpu.ProcessUsageSample) (*Registry, error) {
ns, err := proc.GetSelfNetNs()
if err != nil {
return nil, err
Expand Down Expand Up @@ -113,6 +116,8 @@ func NewRegistry(reg prometheus.Registerer, processInfoCh chan<- ProcessInfo) (*
tracer: ebpftracer.NewTracer(hostNetNs, selfNetNs, *flags.DisableL7Tracing),

trafficStatsUpdateCh: make(chan *TrafficStatsUpdate),

gpuProcessUsageSampleChan: gpuProcessUsageSampleChan,
}
if err = reg.Register(r); err != nil {
return nil, err
Expand Down Expand Up @@ -205,6 +210,12 @@ func (r *Registry) handleEvents(ch <-chan ebpftracer.Event) {
if c := r.containersByPid[u.Pid]; c != nil {
c.updateTrafficStats(u)
}
case sample := <-r.gpuProcessUsageSampleChan:
if c := r.containersByPid[sample.Pid]; c != nil {
if p := c.processes[sample.Pid]; p != nil {
p.addGpuUsageSample(sample)
}
}
case e, more := <-ch:
if !more {
return
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.8
require (
cloud.google.com/go/compute/metadata v0.5.2
github.com/ClickHouse/ch-go v0.62.0
github.com/NVIDIA/go-nvml v0.12.4-1
github.com/agoda-com/opentelemetry-logs-go v0.4.1
github.com/cilium/cilium v1.17.2
github.com/cilium/ebpf v0.17.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ github.com/Microsoft/hcsshim v0.9.12 h1:0Wgl1fRF4WmBuqP6EnHk2w3m7CCCumD/KUumZxp7
github.com/Microsoft/hcsshim v0.9.12/go.mod h1:qAiPvMgZoM0wpkVg6qMdSEu+1VtI6/qHOOPkTGt8ftQ=
github.com/Microsoft/hcsshim/test v0.0.0-20201218223536-d3e5debf77da/go.mod h1:5hlzMzRKMLyo42nCZ9oml8AdTlq/0cvIaBv6tK1RehU=
github.com/Microsoft/hcsshim/test v0.0.0-20210227013316-43a75bb4edd3/go.mod h1:mw7qgWloBUl75W/gVH3cQszUg1+gUITj7D6NY7ywVnY=
github.com/NVIDIA/go-nvml v0.12.4-1 h1:WKUvqshhWSNTfm47ETRhv0A0zJyr1ncCuHiXwoTrBEc=
github.com/NVIDIA/go-nvml v0.12.4-1/go.mod h1:8Llmj+1Rr+9VGGwZuRer5N/aCjxGuR5nPb/9ebBiIEQ=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
Expand Down
Loading