package stats import ( "path/filepath" "time" "linkfog.com/public/lib/cgroup" "linkfog.com/public/lib/cgroup/fs" "linkfog.com/public/lib/cgroup/fs2" "linkfog.com/public/lib/cgroup/types" ) // GetMemoryWorkingSet return memory.usage_in_bytes - total_inactive_file func GetMemoryWorkingSet(cgroupDir string) (usage float64, err error) { var memStats types.MemoryStats memStats, err = GetMemoryStats(cgroupDir) if err != nil { return } usage = float64(memStats.WorkingSet) return } // GetMemoryUsage return memory.usage_in_bytes func GetMemoryUsage(cgroupDir string) (usage float64, err error) { var memStats types.MemoryStats memStats, err = GetMemoryStats(cgroupDir) if err != nil { return } usage = float64(memStats.Usage) return } func GetMemoryStats(cgroupDir string) (stats types.MemoryStats, err error) { if cgroup.IsCgroup2() { stats.Usage, stats.WorkingSet, err = fs2.GetMemoryUsage(cgroupDir) } else { stats.Usage, stats.WorkingSet, err = fs.GetMemoryUsage(cgroupDir) } if err != nil { return } stats.Timestamp = time.Now() return } func GetKernelMemoryUsage(cgroupDir string) (usage float64, err error) { var kmemUsage uint64 if cgroup.IsCgroup2() { kmemUsage, err = fs2.GetKernelMemoryUsage(cgroupDir) } else { kmemUsage, err = fs.GetKernelMemoryUsage(cgroupDir) } if err != nil { return } usage = float64(kmemUsage) return } func GetMemoryStatFields(cgroupDir string) (activeFile, inactiveFile, activeAnon, inactiveAnon uint64, err error) { var stats map[string]uint64 stats, err = fs.ParseMemoryStat(filepath.Join(cgroupDir, "memory.stat")) if err != nil { return } if cgroup.IsCgroup2() { if v, ok := stats["active_file"]; ok { activeFile = v } if v, ok := stats["inactive_file"]; ok { inactiveFile = v } if v, ok := stats["active_anon"]; ok { activeAnon = v } if v, ok := stats["inactive_anon"]; ok { inactiveAnon = v } } else { if v, ok := stats["total_active_file"]; ok { activeFile = v } if v, ok := stats["total_inactive_file"]; ok { inactiveFile = v } if v, ok := stats["total_active_anon"]; ok { activeAnon = v } if v, ok := stats["total_inactive_anon"]; ok { inactiveAnon = v } } return } func GetKernelMemorySlabInfo(cgroupDir string) (slabInfo fs.SlabInfo, err error) { if cgroup.IsCgroup2() { return fs2.GetKernelMemorySlabInfo(cgroupDir) } else { return fs.GetKernelMemorySlabInfo(cgroupDir) } }