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

Adding memory.events metrics in container stats #3345

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
20 changes: 14 additions & 6 deletions container/libcontainer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (h *Handler) GetStats() (*info.ContainerStats, error) {
libcontainerStats := &libcontainer.Stats{
CgroupStats: cgroupStats,
}
stats := newContainerStats(libcontainerStats, h.includedMetrics)
stats := newContainerStats(libcontainerStats, h.includedMetrics, h.cgroupManager.GetPaths())

if h.includedMetrics.Has(container.ProcessSchedulerMetrics) {
stats.Cpu.Schedstat, err = h.schedulerStatsFromProcs()
Expand Down Expand Up @@ -797,17 +797,26 @@ func setDiskIoStats(s *cgroups.Stats, ret *info.ContainerStats) {
ret.DiskIo.IoTime = diskStatsCopy(s.BlkioStats.IoTimeRecursive)
}

func setMemoryStats(s *cgroups.Stats, ret *info.ContainerStats) {
func setMemoryStats(s *cgroups.Stats, ret *info.ContainerStats, cgroupPaths map[string]string) {
ret.Memory.Usage = s.MemoryStats.Usage.Usage
ret.Memory.MaxUsage = s.MemoryStats.Usage.MaxUsage
ret.Memory.Failcnt = s.MemoryStats.Usage.Failcnt
ret.Memory.KernelUsage = s.MemoryStats.KernelUsage.Usage

klog.V(4).Info("Unable to get Process Scheduler Stats: Hi")
if cgroups.IsCgroup2UnifiedMode() {
ret.Memory.Cache = s.MemoryStats.Stats["file"]
ret.Memory.RSS = s.MemoryStats.Stats["anon"]
ret.Memory.Swap = s.MemoryStats.SwapUsage.Usage - s.MemoryStats.Usage.Usage
ret.Memory.MappedFile = s.MemoryStats.Stats["file_mapped"]
testPath, ok := common.GetControllerPath(cgroupPaths, "cpu", cgroups.IsCgroup2UnifiedMode())
if !ok {
klog.V(4).Info("Could not find cgroups CPU for container ")
}

klog.V(4).Infof("Printing test path: %s", testPath)

// /sys/fs/cgroup/user.slice/user-653073.slice/user@653073.service/app.slice/gnome-session-monitor.service/memory.events
ret.Memory.MemoryEvents = getMemoryEvents(cgroupPaths[""])
} else if s.MemoryStats.UseHierarchy {
ret.Memory.Cache = s.MemoryStats.Stats["total_cache"]
ret.Memory.RSS = s.MemoryStats.Stats["total_rss"]
Expand Down Expand Up @@ -907,17 +916,16 @@ func setThreadsStats(s *cgroups.Stats, ret *info.ContainerStats) {
}
}

func newContainerStats(libcontainerStats *libcontainer.Stats, includedMetrics container.MetricSet) *info.ContainerStats {
func newContainerStats(libcontainerStats *libcontainer.Stats, includedMetrics container.MetricSet, cgroupPaths map[string]string) *info.ContainerStats {
ret := &info.ContainerStats{
Timestamp: time.Now(),
}

if s := libcontainerStats.CgroupStats; s != nil {
setCPUStats(s, ret, includedMetrics.Has(container.PerCpuUsageMetrics))
if includedMetrics.Has(container.DiskIOMetrics) {
setDiskIoStats(s, ret)
}
setMemoryStats(s, ret)
setMemoryStats(s, ret, cgroupPaths)
if includedMetrics.Has(container.MemoryNumaMetrics) {
setMemoryNumaStats(s, ret)
}
Expand Down
34 changes: 34 additions & 0 deletions container/libcontainer/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package libcontainer

import (
"bufio"
"fmt"
"os"

info "github.com/google/cadvisor/info/v1"

Expand All @@ -25,6 +27,7 @@ import (

fs "github.com/opencontainers/runc/libcontainer/cgroups/fs"
fs2 "github.com/opencontainers/runc/libcontainer/cgroups/fs2"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
configs "github.com/opencontainers/runc/libcontainer/configs"
"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -168,3 +171,34 @@ func NewCgroupManager(name string, paths map[string]string) (cgroups.Manager, er

return fs.NewManager(config, paths)
}

func getMemoryEvents(path string) map[string]uint64 {
const file = "memory.events"
memoryEventsFile, err := cgroups.OpenFile(path, file, os.O_RDONLY)
if err != nil {
klog.V(4).Infof("Could not open %s/%s cgroup file: %v", path, file, err)
return nil
}

defer memoryEventsFile.Close()

memoryEvents := make(map[string]uint64)
sc := bufio.NewScanner(memoryEventsFile)
for sc.Scan() {
t, v, err := fscommon.ParseKeyValue(sc.Text())
if err != nil {
klog.V(4).Infof("Could not parse line from %s/%s cgroups file: %v", path, file, err)
return nil
}
memoryEvents[t] = v
}

klog.V(4).Infof("File Path %s/%s cgroup file: %v", path, file, err)

if err = cgroups.WriteFile(path, file, "high 100"); err != nil {
klog.V(4).Infof("Could not open %s/%s cgroup file: %v", path, file, err)
}


return memoryEvents
}
2 changes: 1 addition & 1 deletion deploy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ ENV CADVISOR_HEALTHCHECK_URL=http://localhost:8080/healthz
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget --quiet --tries=1 --spider $CADVISOR_HEALTHCHECK_URL || exit 1

ENTRYPOINT ["/usr/bin/cadvisor", "-logtostderr"]
ENTRYPOINT ["/usr/bin/cadvisor", "-log_dir=/tmp/", "-v=5", "-logtostderr=false"]
2 changes: 2 additions & 0 deletions info/v1/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ type MemoryStats struct {
// Units: Bytes.
KernelUsage uint64 `json:"kernel"`

MemoryEvents map[string]uint64 `json:"memory_events"`

ContainerData MemoryStatsMemoryData `json:"container_data,omitempty"`
HierarchicalData MemoryStatsMemoryData `json:"hierarchical_data,omitempty"`
}
Expand Down
Loading