Skip to content
Open
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
15 changes: 11 additions & 4 deletions cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (

// MemoryStat holds memory statistics from the cgroup
type MemoryStat struct {
Usage uint64
Kernel uint64 // Separate kernel memory tracking (v1 only; in v2 it's included in Usage)
Usage uint64
Kernel uint64 // Separate kernel memory tracking (v1 only; in v2 it's included in Usage)
Hugetlb uint64 // Hugetlb usage (not included in standard RSS)
}

// CgroupManager provides an abstraction over cgroups v1 and v2
Expand Down Expand Up @@ -111,6 +112,7 @@ func newV2Manager(path string, memLimit int64) (*cgroupV2Manager, error) {
Memory: &cgroup2.Memory{
Max: &memLimit,
},
HugeTlb: &cgroup2.HugeTlb{},
}

// Remove leading slash if present for v2
Expand Down Expand Up @@ -142,9 +144,14 @@ func (m *cgroupV2Manager) Stat() (*MemoryStat, error) {
// In cgroups v2, kernel memory is included in the total usage and cannot
// be queried separately. The separate kmem controller was removed in v2.
// See: https://github.com/opencontainers/runtime-spec/issues/1005
var hugetlb uint64
for _, h := range stats.Hugetlb {
hugetlb += h.Current
}
return &MemoryStat{
Usage: stats.Memory.Usage,
Kernel: 0, // Not separately tracked in v2 (included in Usage)
Usage: stats.Memory.Usage,
Kernel: 0, // Not separately tracked in v2 (included in Usage)
Hugetlb: hugetlb,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ func main() {

var buf bytes.Buffer
bio := bufio.NewWriter(&buf)
if _, err := bio.WriteString("time\trss\tkernel\n"); err != nil {
if _, err := bio.WriteString("time\trss\tkernel\thugetlb\n"); err != nil {
log.Fatalf("bio.WriteString: %s", err)
}

start := stats.Rss[0].Time.UnixNano()
for i := 0; i < len(stats.Rss); i++ {
r := stats.Rss[i]
line := fmt.Sprintf("%d\t%d\t%d\n", r.Time.UnixNano()-start, r.Value, r.Kernel)
line := fmt.Sprintf("%d\t%d\t%d\t%d\n", r.Time.UnixNano()-start, r.Value, r.Kernel, r.Hugetlb)
if _, err := bio.WriteString(line); err != nil {
log.Fatalf("bufio.WriteString: %s", err)
}
Expand Down
9 changes: 5 additions & 4 deletions poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

type Record struct {
Time time.Time
Value uint64
Kernel uint64
Time time.Time
Value uint64
Kernel uint64
Hugetlb uint64
}

type Stats struct {
Expand Down Expand Up @@ -61,7 +62,7 @@ func (p *Poller) poll(t time.Time, cgroup CgroupManager) error {
return fmt.Errorf("cgroup.Stat: %w", err)
}

p.stats.Rss = append(p.stats.Rss, Record{t, stats.Usage, stats.Kernel})
p.stats.Rss = append(p.stats.Rss, Record{t, stats.Usage, stats.Kernel, stats.Hugetlb})

return nil
}
Expand Down